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);
                }
            }
        }
        public JsVal EvaluateScript(string javaScript)
        {
            string msg = String.Empty;
            var    old = SpiderMonkey.JS_SetErrorReporter(_cx, (cx, message, report) => { msg = message; });

            try
            {
                var jsValue = new JsVal();
                var ret     = SpiderMonkey.JS_EvaluateScript(_cx, PeekCompartmentScope(), javaScript, (uint)javaScript.Length, "script",
                                                             1, ref jsValue);

                if (!ret)
                {
                    throw new GeckoJavaScriptException(String.Format("JSError : {0}", msg));
                }

                return(jsValue);
            }
            finally
            {
                SpiderMonkey.JS_SetErrorReporter(_cx, old);
            }
        }
Example #3
0
        /// <summary>
        /// Evaluate JavaScript in specified window.
        /// Will throw a GeckoJavaScriptException if unable to convert window into a JsVal
        /// </summary>
        /// <param name="javascript">The javascript to run.</param>
        /// <param name="window">The window to execuate javascript in.</param>
        /// <returns>The return value of the script as a JsVal</returns>
        public JsVal EvaluateScript(string javascript, nsIDOMWindow window)
        {
            string msg          = String.Empty;
            IntPtr globalObject = ConvertCOMObjectToJSObject((nsISupports)window);

            using (new JSAutoCompartment(_cx, globalObject))
            {
                var old = SpiderMonkey.JS_SetErrorReporter(_cx, (cx, message, report) => { msg = message; });
                try
                {
                    var    windowJsVal = new JsVal();
                    string jsScript    = "this";
                    bool   ret         = SpiderMonkey.JS_EvaluateScript(_cx, globalObject, jsScript, (uint)jsScript.Length,
                                                                        "script", 1, ref windowJsVal);

                    if (!ret)
                    {
                        throw new GeckoJavaScriptException(String.Format("JSError : {0}", msg));
                    }

                    if (GetComponentsObject().GetUtilsAttribute().IsXrayWrapper(ref windowJsVal))
                    {
                        windowJsVal = GetComponentsObject().GetUtilsAttribute().WaiveXrays(ref windowJsVal, _cx);
                    }

                    using (nsAStringBase b = new nsAString(javascript))
                    {
                        return(GetComponentsObject().GetUtilsAttribute().EvalInWindow(b, ref windowJsVal, _cx));
                    }
                }
                finally
                {
                    SpiderMonkey.JS_SetErrorReporter(_cx, old);
                }
            }
        }