/// <summary>
        /// Parse the source for an ES module
        /// </summary>
        /// <remarks>
        /// This is basically ParseModule operation in ES6 spec. It is slightly different in that:
        /// a) The <code>JsModuleRecord</code> was initialized earlier, and passed in as an argument.
        /// b) This includes a check to see if the module being parsed is the last module in the
        ///    dependency tree. If it is it automatically triggers module instantiation.
        /// </remarks>
        /// <param name="script">The source script to be parsed, but not executed in this code</param>
        /// <param name="sourceContext">A cookie identifying the script that can be used by debuggable
        /// script contexts</param>
        /// <param name="exception">The error object if there is parse error</param>
        public void ParseSource(string script, JsSourceContext sourceContext, out JsValue exception)
        {
            Encoding encoding;
            JsParseModuleSourceFlags sourceFlags;

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                encoding    = Encoding.Unicode;
                sourceFlags = JsParseModuleSourceFlags.DataIsUTF16LE;
            }
            else
            {
                encoding    = Encoding.UTF8;
                sourceFlags = JsParseModuleSourceFlags.DataIsUTF8;
            }

            var byteArrayPool = ArrayPool <byte> .Shared;
            int bufferLength  = encoding.GetByteCount(script);

            byte[] buffer = byteArrayPool.Rent(bufferLength + 1);
            buffer[bufferLength] = 0;

            encoding.GetBytes(script, 0, script.Length, buffer, 0);

            try
            {
                NativeMethods.JsParseModuleSource(this, sourceContext, buffer, (uint)bufferLength,
                                                  sourceFlags, out exception);
            }
            finally
            {
                byteArrayPool.Return(buffer);
            }
        }
        /// <summary>
        /// Executes a script
        /// </summary>
        /// <remarks>
        /// Requires an active script context.
        /// </remarks>
        /// <param name="script">The script to run</param>
        /// <param name="sourceContext">A cookie identifying the script that can be used
        /// by debuggable script contexts</param>
        /// <param name="sourceUrl">The location the script came from</param>
        /// <param name="parseAttributes">Attribute mask for parsing the script</param>
        /// <returns>The result of the script, if any</returns>
        public static JsValue RunScript(string script, JsSourceContext sourceContext, string sourceUrl,
                                        ref JsParseScriptAttributes parseAttributes)
        {
            JsValue scriptValue = JsValue.FromString(script);

            scriptValue.AddRef();

            JsValue sourceUrlValue = JsValue.FromString(sourceUrl);

            sourceUrlValue.AddRef();

            JsValue result;

            try
            {
                JsErrorCode errorCode = NativeMethods.JsRun(scriptValue, sourceContext, sourceUrlValue,
                                                            parseAttributes, out result);
                JsErrorHelpers.ThrowIfError(errorCode);
            }
            finally
            {
                scriptValue.Release();
                sourceUrlValue.Release();
            }

            return(result);
        }
Beispiel #3
0
 internal static extern JsErrorCode JsRunSerialized(JsValue buffer,
                                                    JsSerializedLoadScriptCallback scriptLoadCallback, JsSourceContext sourceContext,
                                                    JsValue sourceUrl, out JsValue result);
Beispiel #4
0
 internal static extern JsErrorCode JsRun(JsValue script, JsSourceContext sourceContext, JsValue sourceUrl,
                                          JsParseScriptAttributes parseAttributes, out JsValue result);
Beispiel #5
0
 internal static extern JsErrorCode JsParseModuleSource(JsModuleRecord requestModule, JsSourceContext sourceContext,
                                                        byte[] script, uint scriptLength, JsParseModuleSourceFlags sourceFlag, out JsValue exception);
        /// <summary>
        /// Runs a serialized script
        /// </summary>
        /// <remarks>
        /// <para>Requires an active script context.</para>
        /// <para>The runtime will detach the data from the buffer and hold on to it until all
        /// instances of any functions created from the buffer are garbage collected.</para>
        /// </remarks>
        /// <param name="script">The source code of the serialized script</param>
        /// <param name="buffer">The serialized script</param>
        /// <param name="scriptLoadCallback">Callback to load the source code of the serialized script</param>
        /// <param name="sourceContext">A cookie identifying the script that can be used
        /// by debuggable script contexts</param>
        /// <param name="sourceUrl">The location the script came from</param>
        /// <returns>The result of running the script, if any</returns>
        public static JsValue RunSerializedScript(string script, byte[] buffer,
                                                  JsSerializedLoadScriptCallback scriptLoadCallback, JsSourceContext sourceContext, string sourceUrl)
        {
            JsValue bufferValue = JsValue.CreateExternalArrayBuffer(buffer);

            bufferValue.AddRef();

            JsValue sourceUrlValue = JsValue.FromString(sourceUrl);

            sourceUrlValue.AddRef();

            JsValue result;

            try
            {
                JsErrorCode errorCode = NativeMethods.JsRunSerialized(bufferValue, scriptLoadCallback,
                                                                      sourceContext, sourceUrlValue, out result);
                JsErrorHelpers.ThrowIfError(errorCode);
            }
            finally
            {
                bufferValue.Release();
                sourceUrlValue.Release();
            }

            return(result);
        }