void gjs_explain_scope(JSContext *context, const char *title) { JSContext *load_context; JSContext *call_context; JSObject *global; JSObject *parent; GString *chain; gjs_debug(GJS_DEBUG_SCOPE, "=== %s ===", title); load_context = gjs_runtime_peek_load_context(JS_GetRuntime(context)); call_context = gjs_runtime_peek_call_context(JS_GetRuntime(context)); JS_BeginRequest(context); JS_BeginRequest(load_context); JS_BeginRequest(call_context); JS_EnterLocalRootScope(context); gjs_debug(GJS_DEBUG_SCOPE, " Context: %p %s", context, context == load_context ? "(LOAD CONTEXT)" : context == call_context ? "(CALL CONTEXT)" : ""); global = JS_GetGlobalObject(context); gjs_debug(GJS_DEBUG_SCOPE, " Global: %p %s", global, gjs_value_debug_string(context, OBJECT_TO_JSVAL(global))); parent = JS_GetScopeChain(context); chain = g_string_new(NULL); while (parent != NULL) { const char *debug; debug = gjs_value_debug_string(context, OBJECT_TO_JSVAL(parent)); if (chain->len > 0) g_string_append(chain, ", "); g_string_append_printf(chain, "%p %s", parent, debug); parent = JS_GetParent(context, parent); } gjs_debug(GJS_DEBUG_SCOPE, " Chain: %s", chain->str); g_string_free(chain, TRUE); JS_LeaveLocalRootScope(context); JS_EndRequest(call_context); JS_EndRequest(load_context); JS_EndRequest(context); }
// static JSBool XPCThrower::ThrowExceptionObject(JSContext* cx, nsIException* e) { JSBool success = JS_FALSE; if(e) { nsCOMPtr<nsIXPCException> xpcEx; jsval thrown; nsXPConnect* xpc; // If we stored the original thrown JS value in the exception // (see XPCConvert::ConstructException) and we are in a web // context (i.e., not chrome), rethrow the original value. if(!IsCallerChrome(cx) && (xpcEx = do_QueryInterface(e)) && NS_SUCCEEDED(xpcEx->StealJSVal(&thrown))) { if (!JS_WrapValue(cx, &thrown)) return JS_FALSE; JS_SetPendingException(cx, thrown); success = JS_TRUE; } else if((xpc = nsXPConnect::GetXPConnect())) { JSObject* glob = JS_GetScopeChain(cx); if(!glob) return JS_FALSE; glob = JS_GetGlobalForObject(cx, glob); nsCOMPtr<nsIXPConnectJSObjectHolder> holder; nsresult rv = xpc->WrapNative(cx, glob, e, NS_GET_IID(nsIException), getter_AddRefs(holder)); if(NS_SUCCEEDED(rv) && holder) { JSObject* obj; if(NS_SUCCEEDED(holder->GetJSObject(&obj))) { JS_SetPendingException(cx, OBJECT_TO_JSVAL(obj)); success = JS_TRUE; } } } } return success; }
static JSBool js_on_exit(JSContext *cx, uintN argc, jsval *arglist) { JSObject *scope=JS_GetScopeChain(cx); JSObject *glob=JS_GetGlobalObject(cx); jsval *argv=JS_ARGV(cx, arglist); global_private_t* pd; str_list_t list; str_list_t oldlist; char *p; JS_SET_RVAL(cx, arglist, JSVAL_VOID); if(glob==scope) { if((pd=(global_private_t*)JS_GetPrivate(cx,glob))==NULL) return(JS_FALSE); if(pd->exit_func==NULL) pd->exit_func=strListInit(); list=pd->exit_func; } else { list=(str_list_t)JS_GetPrivate(cx,scope); if(list==NULL) { list=strListInit(); JS_SetPrivate(cx,scope,list); } } JSVALUE_TO_MSTRING(cx, argv[0], p, NULL); HANDLE_PENDING(cx); if(!p) return JS_TRUE; oldlist=list; strListPush(&list,p); free(p); if(oldlist != list) { if(glob==scope) pd->exit_func=list; else JS_SetPrivate(cx,scope,list); } return(JS_TRUE); }
JSBool XPC_COW_RewrapForContent(JSContext *cx, JSObject *wrapperObj, jsval *vp) { jsval v = *vp; if (JSVAL_IS_PRIMITIVE(v)) { return JS_TRUE; } JSObject *obj = GetWrappedJSObject(cx, JSVAL_TO_OBJECT(v)); if (!obj) { *vp = JSVAL_NULL; return JS_TRUE; } if (JS_ObjectIsFunction(cx, obj)) { return XPC_COW_WrapFunction(cx, wrapperObj, obj, vp); } return XPC_COW_WrapObject(cx, JS_GetScopeChain(cx), OBJECT_TO_JSVAL(obj), vp); }