PrincipalHolder *ph = (PrincipalHolder *)JS_GetPrivate(cx, sandbox); nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(ph);
nsIScriptObjectPrincipal *sop = (nsIScriptObjectPrincipal*)JS_GetPrivate(cx, sandbox); NS_ASSERTION(sop, "Invalid sandbox passed");
nsXPCComponents_Utils::EvalInSandbox(const nsAString &source){#ifdef XPCONNECT_STANDALONE    return NS_ERROR_NOT_AVAILABLE;#else    nsresult rv;    nsCOMPtr<nsIXPConnect> xpc(do_GetService(nsIXPConnect::GetCID(), &rv));    if(NS_FAILED(rv))        return rv;    // get the xpconnect native call context    nsCOMPtr<nsIXPCNativeCallContext> cc;    xpc->GetCurrentNativeCallContext(getter_AddRefs(cc));    if(!cc)        return NS_ERROR_FAILURE;    // Get JSContext of current call    JSContext* cx;    rv = cc->GetJSContext(&cx);    if(NS_FAILED(rv) || !cx)        return NS_ERROR_FAILURE;    // get place for return value    jsval *rval = nsnull;    rv = cc->GetRetValPtr(&rval);    if(NS_FAILED(rv) || !rval)        return NS_ERROR_FAILURE;    // get argc and argv and verify arg count    PRUint32 argc;    rv = cc->GetArgc(&argc);    if(NS_FAILED(rv))        return rv;    if (argc < 2)        return NS_ERROR_XPC_NOT_ENOUGH_ARGS;    // The second argument is the sandbox object. It is required.    jsval *argv;    rv = cc->GetArgvPtr(&argv);    if (NS_FAILED(rv))        return rv;    if (JSVAL_IS_PRIMITIVE(argv[1]))        return NS_ERROR_INVALID_ARG;    JSObject *sandbox = JSVAL_TO_OBJECT(argv[1]);    if (JS_GetClass(cx, sandbox) != &SandboxClass)        return NS_ERROR_INVALID_ARG;    PrincipalHolder *ph = (PrincipalHolder *)JS_GetPrivate(cx, sandbox);    nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(ph);    nsCOMPtr<nsIPrincipal> prin = sop->GetPrincipal();    JSPrincipals *jsPrincipals;    if (!prin ||        NS_FAILED(prin->GetJSPrincipals(cx, &jsPrincipals)) ||        !jsPrincipals) {        return NS_ERROR_FAILURE;    }    JSContext *sandcx = JS_NewContext(JS_GetRuntime(cx), 1024);    if(!sandcx) {        JS_ReportError(cx, "Can't prepare context for evalInSandbox");        JSPRINCIPALS_DROP(cx, jsPrincipals);        return NS_ERROR_OUT_OF_MEMORY;    }    JS_SetGlobalObject(sandcx, sandbox);    // Capture uncaught exceptions reported as errors on sandcx and    // re-throw them on cx.    JS_SetContextPrivate(sandcx, cx);    JS_SetErrorReporter(sandcx, SandboxErrorReporter);    // Push a fake frame onto sandcx so that we can properly propagate uncaught    // exceptions.    JSStackFrame frame;    memset(&frame, 0, sizeof frame);    sandcx->fp = &frame;    // Get the current source info from xpc. Use the codebase as a fallback,    // though.    nsXPIDLCString filename;    PRInt32 lineNo = 0;    {        nsCOMPtr<nsIStackFrame> frame;        xpc->GetCurrentJSStack(getter_AddRefs(frame));        if (frame) {            frame->GetFilename(getter_Copies(filename));            frame->GetLineNumber(&lineNo);        } else {            filename.Assign(jsPrincipals->codebase);            lineNo = 1;        }    }    if (!JS_EvaluateUCScriptForPrincipals(sandcx, sandbox, jsPrincipals,                                          NS_REINTERPRET_CAST(const jschar *,                                              PromiseFlatString(source).get()),                                          source.Length(), filename.get(),                                          lineNo, rval)) {        jsval exn;        if (JS_GetPendingException(sandcx, &exn)) {            JS_SetPendingException(cx, exn);            cc->SetExceptionWasThrown(PR_TRUE);        } else {            rv = NS_ERROR_OUT_OF_MEMORY;        }    } else {        cc->SetReturnValueWasSet(PR_TRUE);    }    JS_DestroyContextNoGC(sandcx);    JSPRINCIPALS_DROP(cx, jsPrincipals);    return rv;#endif /* !XPCONNECT_STANDALONE */}