Example #1
0
        /// <summary>
        /// Evaluate JavaScript in specified window, and with specified scope.
        /// Throws GeckoJavaScriptException on error.
        /// </summary>
        /// <param name="javascript">The javascript to run.</param>
        /// <param name="window">The window to execuate javascript in. (ie. the global object)</param>
        /// <param name="scope">object to use as scope.</param>
        /// <returns>The return value of the script as a JsVal</returns>
        public JsVal EvaluateScript(string javascript, nsISupports window, nsISupports scope)
        {
            string msg            = String.Empty;
            JsVal  exceptionJsVal = default(JsVal);


            IntPtr globalObject = ConvertCOMObjectToJSObject(window);

            using (new JSAutoCompartment(ContextPointer, globalObject))
            {
                var old = SpiderMonkey.JS_SetErrorReporter(SpiderMonkey.JS_GetRuntime(ContextPointer),
                                                           (cx, message, report) =>
                {
                    var exception = SpiderMonkey.JS_GetPendingException(ContextPointer);
                    if (exception != IntPtr.Zero)
                    {
                        exceptionJsVal = JsVal.FromPtr(exception);
                    }
                    msg = message;
                });

                try
                {
                    var  retJsVal = new JsVal();
                    bool ret;
                    // If not running in window scope.
                    if (window != scope)
                    {
                        var scopeJSVal = JsVal.FromPtr(ConvertCOMObjectToJSObject(scope));
                        if (!SpiderMonkey.JS_SetProperty(ContextPointer, globalObject, "__RequestedScope", scopeJSVal))
                        {
                            throw new GeckoException("Failed to set __RequestedScope Property.");
                        }

                        javascript = InsertReturnStatement(javascript);
                        string s = "(function() { " + javascript + " }).call(this.__RequestedScope)";

                        ret = SpiderMonkey.JS_EvaluateScript(ContextPointer, s, (uint)s.Length, "script", 1,
                                                             ref retJsVal);
                    }
                    else
                    {
                        ret = SpiderMonkey.JS_EvaluateScript(ContextPointer, javascript, (uint)javascript.Length,
                                                             "script", 1, ref retJsVal);
                    }

                    if (ret)
                    {
                        return(retJsVal);
                    }

                    msg += GetStackTrace(globalObject, exceptionJsVal);
                    throw new GeckoJavaScriptException(String.Format("JSError : {0}", msg));
                }
                finally
                {
                    SpiderMonkey.JS_SetErrorReporter(SpiderMonkey.JS_GetRuntime(ContextPointer), old);
                }
            }
        }
Example #2
0
        private static void HandleInvokeFailure(IntPtr cx, IntPtr jsObject, string name)
        {
            var exception = SpiderMonkey.JS_GetPendingException(cx);

            if (exception != IntPtr.Zero)
            {
                var exceptionJsVal = JsVal.FromPtr(exception);

                string st = string.Empty;
                try
                {
                    st = GetStackTrace(cx, jsObject, exceptionJsVal);
                }
                catch
                {
                    // ignored
                    // we failed to get stack trace info, but still want to continue reporting exception.
                }
                var msg = exceptionJsVal.ToString();
                msg += st;
                throw new GeckoException($"Calling function '{name}' failed: '{msg}'");
            }

            throw new GeckoException($"Failed to call function '{name}'");
        }
Example #3
0
        /// <summary>
        /// Evaluate JavaScript in specified window, and with specified scope.
        /// Throws GeckoJavaScriptException on error.
        /// </summary>
        /// <param name="javascript">The javascript to run.</param>
        /// <param name="window">The window to execuate javascript in. (ie. the global object)</param>
        /// <param name="scope">object to use as scope.</param>
        /// <returns>The return value of the script as a JsVal</returns>
        public JsVal EvaluateScript(string javascript, nsISupports window, nsISupports scope)
        {
            string msg            = String.Empty;
            JsVal  exceptionJsVal = default(JsVal);

            using (var globalObject = ConvertCOMObjectToJSObject(window))
                using (new JSAutoCompartment(ContextPointer, globalObject.JSObject))
                {
                    var  retJsVal = new JsVal();
                    bool ret;
                    // If not running in window scope.
                    if (window != scope)
                    {
                        using (var scopeObject = ConvertCOMObjectToJSObject(scope))
                        {
                            var scopeJSVal = JsVal.FromPtr(scopeObject.JSObject);
                            var go         = globalObject.JSObject;
                            if (!SpiderMonkey.JS_SetProperty(ContextPointer, ref go, "__RequestedScope",
                                                             ref scopeJSVal))
                            {
                                throw new GeckoException("Failed to set __RequestedScope Property.");
                            }

                            javascript = InsertReturnStatement(javascript);
                            string s = "(function() { " + javascript + " }).call(this.__RequestedScope)";

                            ret = SpiderMonkey.JS_EvaluateScript(ContextPointer, s, (uint)s.Length, "script", 1,
                                                                 ref retJsVal);
                        }
                    }
                    else
                    {
                        ret = SpiderMonkey.JS_EvaluateScript(ContextPointer, javascript, (uint)javascript.Length,
                                                             "script", 1, ref retJsVal);
                    }

                    if (ret)
                    {
                        return(retJsVal);
                    }

                    var exception = SpiderMonkey.JS_GetPendingException(ContextPointer);
                    if (exception != IntPtr.Zero)
                    {
                        exceptionJsVal = JsVal.FromPtr(exception);
                    }
                    msg += exceptionJsVal.ToString();
                    msg += SpiderMonkey.GetStackTrace(ContextPointer, globalObject.JSObject, exceptionJsVal);
                    throw new GeckoJavaScriptException($"JSError : {msg}");
                }
        }