コード例 #1
0
        /// <summary>
        /// Runs the given script.
        /// </summary>
        /// <param name="script">The script.</param>
        /// <param name="sourceUrl">The source URL.</param>
        public void RunScript(string script, string sourceUrl)
        {
            if (script == null)
            {
                throw new ArgumentNullException(nameof(script));
            }
            if (sourceUrl == null)
            {
                throw new ArgumentNullException(nameof(sourceUrl));
            }

            string source = LoadScriptAsync(script).Result;

            try
            {
                _context = JavaScriptSourceContext.Increment(_context);
                JavaScriptContext.RunScript(source, _context, sourceUrl);
            }
            catch (JavaScriptScriptException ex)
            {
                var jsonError  = JavaScriptValueToJTokenConverter.Convert(ex.Error);
                var message    = jsonError.Value <string>("message");
                var stackTrace = jsonError.Value <string>("stack");
                throw new Modules.Core.JavaScriptException(message ?? ex.Message, stackTrace, ex);
            }
        }
コード例 #2
0
 private void EvaluateScript(string script, string sourceUrl)
 {
     try
     {
         _context = JavaScriptSourceContext.Increment(_context);
         JavaScriptContext.RunScript(script, _context, sourceUrl);
     }
     catch (JavaScriptScriptException ex)
     {
         var jsonError  = JavaScriptValueToJTokenConverter.Convert(ex.Error);
         var message    = jsonError.Value <string>("message");
         var stackTrace = jsonError.Value <string>("stack");
         throw new Modules.Core.JavaScriptException(message ?? ex.Message, stackTrace, ex);
     }
 }
コード例 #3
0
        protected override object RunScript(string javaScriptString, string filename)
        {
            IntPtr returnValue;

            try
            {
                JavaScriptValue result;

                switchContextifNeeded();
                if (filename != null && filename != "")
                {
                    result = JavaScriptContext.RunScript(javaScriptString, currentSourceContext, filename);
                    currentSourceContext = JavaScriptSourceContext.Increment(currentSourceContext);
                }
                else
                {
                    result = JavaScriptContext.RunScript(javaScriptString, JavaScriptSourceContext.None, string.Empty);
                }

                // Execute promise tasks stored in taskQueue
                while (_jsTaskQueue.Count != 0)
                {
                    JavaScriptValue jsTask = (JavaScriptValue)_jsTaskQueue.Dequeue();
                    JavaScriptValue promiseResult;
                    JavaScriptValue global;
                    Native.JsGetGlobalObject(out global);
                    JavaScriptValue[] args = new JavaScriptValue[1] {
                        global
                    };
                    Native.JsCallFunction(jsTask, args, 1, out promiseResult);
                }

                // Convert the return value.
                JavaScriptValue stringResult;
                UIntPtr         stringLength;
                Native.ThrowIfError(Native.JsConvertValueToString(result, out stringResult));
                Native.ThrowIfError(Native.JsStringToPointer(stringResult, out returnValue, out stringLength));
            }
            catch (Exception e)
            {
                // throw e;
                NKLogging.log(e.Message);
                return(null);
            }
            return(Marshal.PtrToStringUni(returnValue));
        }