diff --git a/js/src/builtin/TestingFunctions.cpp b/js/src/builtin/TestingFunctions.cpp index 2175ff4faedf..73f1b14cf102 100644 --- a/js/src/builtin/TestingFunctions.cpp +++ b/js/src/builtin/TestingFunctions.cpp @@ -5103,6 +5103,29 @@ ScriptedCallerGlobal(JSContext* cx, unsigned argc, Value* vp) return true; } +static bool +ObjectGlobal(JSContext* cx, unsigned argc, Value* vp) +{ + CallArgs args = CallArgsFromVp(argc, vp); + RootedObject callee(cx, &args.callee()); + + if (!args.get(0).isObject()) { + ReportUsageErrorASCII(cx, callee, "Argument must be an object"); + return false; + } + + RootedObject obj(cx, &args[0].toObject()); + if (IsWrapper(obj)) { + args.rval().setNull(); + return true; + } + + obj = ToWindowProxyIfWindow(&obj->global()); + + args.rval().setObject(*obj); + return true; +} + JSScript* js::TestingFunctionArgumentToScript(JSContext* cx, HandleValue v, @@ -5898,6 +5921,10 @@ gc::ZealModeHelpText), "scriptedCallerGlobal()", " Get the caller's global (or null). See JS::GetScriptedCallerGlobal.\n"), + JS_FN_HELP("objectGlobal", ObjectGlobal, 1, 0, +"objectGlobal(obj)", +" Returns the object's global object or null if the object is a wrapper.\n"), + JS_FN_HELP("baselineCompile", BaselineCompile, 2, 0, "baselineCompile([fun/code], forceDebugInstrumentation=false)", " Baseline-compiles the given JS function or script.\n" diff --git a/js/src/jit-test/tests/realms/basic.js b/js/src/jit-test/tests/realms/basic.js index b4b15f27c8fe..62d12511ac5a 100644 --- a/js/src/jit-test/tests/realms/basic.js +++ b/js/src/jit-test/tests/realms/basic.js @@ -2,3 +2,9 @@ var g1 = newGlobal({sameCompartmentAs: this}); var g2 = newGlobal({sameCompartmentAs: g1}); g2.x = g1; gc(); +assertEq(objectGlobal(Math), this); +assertEq(objectGlobal(g1.print), g1); +assertEq(objectGlobal(g2.x), g1); + +// Different-compartment realms have wrappers. +assertEq(objectGlobal(newGlobal().Math), null);