Esempio n. 1
0
 internal static extern JsErrorCode JsRunSerializedScriptWithCallback(
     JavaScriptSerializedScriptLoadSourceCallback scriptLoadCallback,
     JavaScriptSerializedScriptUnloadCallback scriptUnloadCallback,
     byte[] buffer,
     JsSourceContext sourceContext,
     string sourceUrl,
     out JsValue result);
        /// <summary>
        /// Loads a source code of the serialized script
        /// </summary>
        /// <param name="sourceContext">A cookie identifying the script that can be used
        /// by debuggable script contexts</param>
        /// <param name="value">The script returned</param>
        /// <param name="parseAttributes">Attribute mask for parsing the script</param>
        /// <returns>true if the operation succeeded, false otherwise</returns>
        private bool LoadScriptSourceCode(JsSourceContext sourceContext, out JsValue value,
                                          out JsParseScriptAttributes parseAttributes)
        {
            if (_codeBytes == null)
            {
                lock (_scriptLoadingSynchronizer)
                {
                    if (_codeBytes == null)
                    {
                        Encoding encoding = _parseAttributes.HasFlag(JsParseScriptAttributes.ArrayBufferIsUtf16Encoded) ?
                                            Encoding.Unicode : Encoding.UTF8;
                        _codeBytes = encoding.GetBytes(_code);
                    }
                }
            }

            bool result;

            parseAttributes = _parseAttributes;

            try
            {
                value  = JsValue.CreateExternalArrayBuffer(_codeBytes);
                result = true;
            }
            catch (OriginalException)
            {
                value  = JsValue.Invalid;
                result = false;
            }

            return(result);
        }
Esempio n. 3
0
 internal static extern JsErrorCode JsParseModuleSource(
     JsModuleRecord requestModule,
     JsSourceContext sourceContext,
     byte[] script,
     uint scriptLength,
     JsParseModuleSourceFlags sourceFlag,
     out JsValue exception);
Esempio n. 4
0
        /// <summary>
        /// Parses a script and returns a <c>Function</c> representing the script
        /// </summary>
        /// <remarks>
        /// Requires an active script context.
        /// </remarks>
        /// <param name="script">The script to parse</param>
        /// <param name="sourceContext">The cookie identifying the script that can be used
        /// by script contexts that have debugging enabled</param>
        /// <param name="sourceName">The location the script came from</param>
        /// <returns>The <c>Function</c> representing the script code</returns>
        public static IeJsValue ParseScript(string script, JsSourceContext sourceContext, string sourceName)
        {
            IeJsValue result;

            IeJsErrorHelpers.ThrowIfError(IeNativeMethods.JsParseScript(script, sourceContext, sourceName, out result));

            return(result);
        }
        /// <summary>
        /// Parses a serialized script and returns a <c>Function</c> representing the script
        /// </summary>
        /// <remarks>
        /// Requires an active script context.
        /// </remarks>
        /// <param name="script">The script to parse</param>
        /// <param name="buffer">The serialized script</param>
        /// <param name="sourceContext">The cookie identifying the script that can be used
        /// by script contexts that have debugging enabled</param>
        /// <param name="sourceName">The location the script came from</param>
        /// <returns>The <c>Function</c> representing the script code</returns>
        public static EdgeJsValue ParseScript(string script, byte[] buffer, JsSourceContext sourceContext, string sourceName)
        {
            EdgeJsValue result;

            EdgeJsErrorHelpers.ThrowIfError(EdgeNativeMethods.JsParseSerializedScript(script, buffer, sourceContext, sourceName, out result));

            return(result);
        }
Esempio n. 6
0
        /// <summary>
        /// Runs a serialized script
        /// </summary>
        /// <remarks>
        /// Requires an active script context.
        /// </remarks>
        /// <param name="script">The source code of the serialized script</param>
        /// <param name="buffer">The serialized script</param>
        /// <param name="sourceContext">The cookie identifying the script that can be used by script contexts that have debugging enabled</param>
        /// <param name="sourceName">The location the script came from</param>
        /// <returns>The result of the script, if any</returns>
        public static IeJsValue RunScript(string script, byte[] buffer, JsSourceContext sourceContext, string sourceName)
        {
            IeJsValue result;

            IeJsErrorHelpers.ThrowIfError(IeNativeMethods.JsRunSerializedScript(script, buffer, sourceContext, sourceName, out result));

            return(result);
        }
        /// <summary>
        /// Executes a script
        /// </summary>
        /// <remarks>
        /// Requires an active script context.
        /// </remarks>
        /// <param name="script">The script to run</param>
        /// <param name="sourceContext">The cookie identifying the script that can be used
        /// by script contexts that have debugging enabled</param>
        /// <param name="sourceName">The location the script came from</param>
        /// <returns>The result of the script, if any</returns>
        public static EdgeJsValue RunScript(string script, JsSourceContext sourceContext, string sourceName)
        {
            EdgeJsValue result;

            EdgeJsErrorHelpers.ThrowIfError(EdgeNativeMethods.JsRunScript(script, sourceContext, sourceName, out result));

            return(result);
        }
Esempio n. 8
0
        private static void Main()
        {
            try
            {
                var js = "(() => { return 'Hello, World!'; })()";

                var sourceContext = JsSourceContext.FromIntPtr(IntPtr.Zero);
                using (var runtime = new ChakraRuntime())
                {
                    ChakraCore.JsRun(JsValueRef.FromString(js), sourceContext++, JsValueRef.FromString(""), JsParseScriptAttributes.StrictMode, out var result);

                    Console.WriteLine(result.ToString());
                    Console.WriteLine(runtime.IsExecutionDisabled);
                    Console.WriteLine(runtime.MemoryLimit);
                    Console.WriteLine(runtime.MemoryUsage);

                    Console.ReadLine();
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
            }
        }
		internal static extern JsErrorCode JsRunScript(string script, JsSourceContext sourceContext, string sourceUrl, out EdgeJsValue result);
		internal static extern JsErrorCode JsRunSerializedScriptWithCallback(
			JsSerializedScriptLoadSourceCallback scriptLoadCallback,
			JsSerializedScriptUnloadCallback scriptUnloadCallback, byte[] buffer,
			JsSourceContext sourceContext, string sourceUrl, out JsValue result);
		internal static extern JsErrorCode JsRunSerialized(JsValue buffer,
			JsSerializedLoadScriptCallback scriptLoadCallback, JsSourceContext sourceContext,
			JsValue sourceUrl, out JsValue result);
		internal static extern JsErrorCode JsRun(JsValue script, JsSourceContext sourceContext, JsValue sourceUrl,
			JsParseScriptAttributes parseAttributes, out JsValue result);
Esempio n. 13
0
 internal static extern JsErrorCode JsRunSerializedScript(string script,
                                                          byte[] buffer,
                                                          JsSourceContext sourceContext,
                                                          string sourceUrl,
                                                          out JsValue result);
Esempio n. 14
0
 internal static extern JsErrorCode JsRunScript(string script, JsSourceContext sourceContext, string sourceUrl,
                                                out JsValue result);
        /// <summary>
        /// Runs a serialized script
        /// </summary>
        /// <remarks>
        /// Requires an active script context.
        /// </remarks>
        /// <param name="script">The source code of the serialized script</param>
        /// <param name="buffer">The serialized script</param>
        /// <param name="sourceContext">The cookie identifying the script that can be used
        /// by script contexts that have debugging enabled</param>
        /// <param name="sourceName">The location the script came from</param>
        /// <returns>The result of the script, if any</returns>
        public static EdgeJsValue RunScript(string script, byte[] buffer, JsSourceContext sourceContext, string sourceName)
        {
            EdgeJsValue result;
            EdgeJsErrorHelpers.ThrowIfError(EdgeNativeMethods.JsRunSerializedScript(script, buffer, sourceContext, sourceName, out result));

            return result;
        }
Esempio n. 16
0
        /// <summary>
        /// Parses a serialized script and returns a <c>Function</c> representing the script
        /// </summary>
        /// <remarks>
        /// Requires an active script context.
        /// </remarks>
        /// <param name="script">The script to parse</param>
        /// <param name="buffer">The serialized script</param>
        /// <param name="sourceContext">The cookie identifying the script that can be used
        /// by script contexts that have debugging enabled</param>
        /// <param name="sourceName">The location the script came from</param>
        /// <returns>The <c>Function</c> representing the script code</returns>
        public static IeJsValue ParseScript(string script, byte[] buffer, JsSourceContext sourceContext, string sourceName)
        {
            IeJsValue result;
            IeJsErrorHelpers.ThrowIfError(IeNativeMethods.JsParseSerializedScript(script, buffer, sourceContext, sourceName, out result));

            return result;
        }
Esempio n. 17
0
 public static Task <T> EvalAsync <T>(this JsContext context, string script, byte[] buffer, JsSourceContext sourceContext, string sourceName)
 {
     return(context.RequestScopeAsync(session => session.RunScript(script, buffer, sourceContext, sourceName).ToObject <T>()));
 }
Esempio n. 18
0
 public static Task <JsValue> EvalAsync(this JsContext context, string script, JsSourceContext sourceContext, string sourceName)
 {
     return(context.RequestScopeAsync(session => session.RunScript(script, sourceContext, sourceName)));
 }
Esempio n. 19
0
        /// <summary>
        /// Executes a script
        /// </summary>
        /// <remarks>
        /// Requires an active script context.
        /// </remarks>
        /// <param name="script">The script to run</param>
        /// <param name="sourceContext">The cookie identifying the script that can be used by script contexts that have debugging enabled</param>
        /// <param name="sourceName">The location the script came from</param>
        /// <returns>The result of the script, if any</returns>
        public static IeJsValue RunScript(string script, JsSourceContext sourceContext, string sourceName)
        {
            IeJsValue result;
            IeJsErrorHelpers.ThrowIfError(IeNativeMethods.JsRunScript(script, sourceContext, sourceName, out result));

            return result;
        }
Esempio n. 20
0
        static void Main(string[] args)
        {
            using (JsRuntime runtime = JsRuntime.Create())
            {
                JsContext context = runtime.CreateContext();
                context.AddRef();

                JsSourceContext sourceContext = JsSourceContext.FromIntPtr(IntPtr.Zero);
                var             moduleManager = new EsModuleManager(() => sourceContext++);
                var             scope         = new JsScope(context);

                try
                {
                    JsValue moduleNamespace;

                    // It's not working. Always returns a result equal to `undefined`.
                    JsValue resultValue = moduleManager.EvaluateModuleCode(
                        @"import * as geometry from './geometry/geometry.js';
new geometry.Square(15).area;",
                        "Files/main-with-return-value.js",
                        out moduleNamespace
                        );
                    WriteLine("Return value: {0}", resultValue.ConvertToString().ToString());

                    // It's works. We can return the result value by using the default export.
                    moduleManager.EvaluateModuleCode(
                        @"import * as geometry from './geometry/geometry.js';
export default new geometry.Square(20).area;",
                        "Files/main-with-default-export.js",
                        out moduleNamespace
                        );
                    JsPropertyId defaultPropertyId    = JsPropertyId.FromString("default");
                    JsValue      defaultPropertyValue = moduleNamespace.GetProperty(defaultPropertyId);
                    WriteLine("Default export: {0}", defaultPropertyValue.ConvertToString().ToString());

                    // It's works. We can return the result value by using the named export.
                    moduleManager.EvaluateModuleCode(
                        @"import * as geometry from './geometry/geometry.js';
export let squareArea = new geometry.Square(25).area;",
                        "Files/main-with-named-export.js",
                        out moduleNamespace
                        );
                    JsPropertyId squareAreaPropertyId    = JsPropertyId.FromString("squareArea");
                    JsValue      squareAreaPropertyValue = moduleNamespace.GetProperty(squareAreaPropertyId);
                    WriteLine("Named export: {0}", squareAreaPropertyValue.ConvertToString().ToString());
                }
                catch (JsException e)
                {
                    WriteLine("During working of JavaScript engine an error occurred.");
                    WriteLine();
                    Write(e.Message);

                    var scriptException = e as JsScriptException;
                    if (scriptException != null)
                    {
                        WriteLine();

                        JsValue     errorValue     = scriptException.Metadata.GetProperty("exception");
                        JsValueType errorValueType = errorValue.ValueType;

                        if (errorValueType == JsValueType.Error || errorValueType == JsValueType.Object)
                        {
                            JsValue      messageValue;
                            JsPropertyId stackPropertyId = JsPropertyId.FromString("stack");

                            if (errorValue.HasProperty(stackPropertyId))
                            {
                                messageValue = errorValue.GetProperty(stackPropertyId);
                            }
                            else
                            {
                                messageValue = errorValue.GetProperty("message");
                            }

                            WriteLine(messageValue.ToString());
                        }
                        else if (errorValueType == JsValueType.String)
                        {
                            WriteLine(errorValue.ToString());
                        }
                        else
                        {
                            WriteLine(errorValue.ConvertToString().ToString());
                        }
                    }
                }
                finally
                {
                    scope.Dispose();
                    moduleManager?.Dispose();
                    context.Release();
                }
            }
        }
		internal static extern JsErrorCode JsRunSerializedScript(string script, byte[] buffer, JsSourceContext sourceContext, string sourceUrl, out EdgeJsValue result);
		/// <summary>
		/// Executes a script
		/// </summary>
		/// <remarks>
		/// Requires an active script context.
		/// </remarks>
		/// <param name="script">The script to run</param>
		/// <param name="sourceContext"> The cookie identifying the script that can be used
		/// by script contexts that have debugging enabled</param>
		/// <param name="sourceName">The location the script came from</param>
		/// <returns>The result of the script, if any</returns>
		public static JsValue RunScript(string script, JsSourceContext sourceContext, string sourceName)
		{
			JsValue result;
			JsErrorCode errorCode;

			if (Utils.IsWindows())
			{
				errorCode = NativeMethods.JsRunScript(script, sourceContext, sourceName, out result);
				JsErrorHelpers.ThrowIfError(errorCode);
			}
			else
			{
				JsValue scriptValue = JsValue.FromString(script);
				scriptValue.AddRef();

				JsValue sourceUrlValue = JsValue.FromString(sourceName);
				sourceUrlValue.AddRef();

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

			return result;
		}
        /// <summary>
        /// Parses a script and returns a <c>Function</c> representing the script
        /// </summary>
        /// <remarks>
        /// Requires an active script context.
        /// </remarks>
        /// <param name="script">The script to parse</param>
        /// <param name="sourceContext">The cookie identifying the script that can be used
        /// by script contexts that have debugging enabled</param>
        /// <param name="sourceName">The location the script came from</param>
        /// <returns>The <c>Function</c> representing the script code</returns>
        public static EdgeJsValue ParseScript(string script, JsSourceContext sourceContext, string sourceName)
        {
            EdgeJsValue result;
            EdgeJsErrorHelpers.ThrowIfError(EdgeNativeMethods.JsParseScript(script, sourceContext, sourceName, out result));

            return result;
        }