| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#include "builtin/ShadowRealm.h" |
| 8 |
|
| 9 |
#include "mozilla/Assertions.h" |
| 10 |
|
| 11 |
#include "jsapi.h" |
| 12 |
#include "jsfriendapi.h" |
| 13 |
#include "builtin/ModuleObject.h" |
| 14 |
#include "builtin/Promise.h" |
| 15 |
#include "builtin/WrappedFunctionObject.h" |
| 16 |
#include "frontend/BytecodeCompiler.h" // CompileEvalScript |
| 17 |
#include "js/ErrorReport.h" |
| 18 |
#include "js/Exception.h" |
| 19 |
#include "js/GlobalObject.h" |
| 20 |
#include "js/Principals.h" |
| 21 |
#include "js/Promise.h" |
| 22 |
#include "js/PropertyAndElement.h" |
| 23 |
#include "js/PropertyDescriptor.h" |
| 24 |
#include "js/ShadowRealmCallbacks.h" |
| 25 |
#include "js/SourceText.h" |
| 26 |
#include "js/StableStringChars.h" |
| 27 |
#include "js/StructuredClone.h" |
| 28 |
#include "js/TypeDecls.h" |
| 29 |
#include "js/Wrapper.h" |
| 30 |
#include "vm/GlobalObject.h" |
| 31 |
#include "vm/Interpreter.h" |
| 32 |
#include "vm/JSObject.h" |
| 33 |
#include "vm/ObjectOperations.h" |
| 34 |
|
| 35 |
#include "builtin/HandlerFunction-inl.h" |
| 36 |
#include "vm/Compartment-inl.h" |
| 37 |
#include "vm/JSObject-inl.h" |
| 38 |
#include "vm/Realm-inl.h" |
| 39 |
|
| 40 |
using namespace js; |
| 41 |
|
| 42 |
using JS::AutoStableStringChars; |
| 43 |
using JS::CompileOptions; |
| 44 |
using JS::SourceText; |
| 45 |
|
| 46 |
static JSObject* DefaultNewShadowRealmGlobal(JSContext* cx, |
| 47 |
JS::RealmOptions& options, |
| 48 |
JSPrincipals* principals, |
| 49 |
Handle<JSObject*> unused) { |
| 50 |
static const JSClass shadowRealmGlobal = { |
| 51 |
"ShadowRealmGlobal", |
| 52 |
JSCLASS_GLOBAL_FLAGS, |
| 53 |
&JS::DefaultGlobalClassOps, |
| 54 |
}; |
| 55 |
|
| 56 |
return JS_NewGlobalObject(cx, &shadowRealmGlobal, principals, |
| 57 |
JS::FireOnNewGlobalHook, options); |
| 58 |
} |
| 59 |
|
| 60 |
// https://tc39.es/proposal-shadowrealm/#sec-shadowrealm-constructor |
| 61 |
/*static*/ |
| 62 |
bool ShadowRealmObject::construct(JSContext* cx, unsigned argc, Value* vp) { |
| 63 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 64 |
|
| 65 |
// Step 1. If NewTarget is undefined, throw a TypeError exception. |
| 66 |
if (!args.isConstructing()) { |
| 67 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 68 |
JSMSG_NOT_CONSTRUCTOR, "ShadowRealm"); |
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
// Step 2. Let O be ? OrdinaryCreateFromConstructor(NewTarget, |
| 73 |
// "%ShadowRealm.prototype%", « [[ShadowRealm]], [[ExecutionContext]] »). |
| 74 |
Rooted<JSObject*> proto(cx); |
| 75 |
if (!GetPrototypeFromBuiltinConstructor(cx, args, JSProto_ShadowRealm, |
| 76 |
&proto)) { |
| 77 |
return false; |
| 78 |
} |
| 79 |
|
| 80 |
Rooted<ShadowRealmObject*> shadowRealmObj( |
| 81 |
cx, NewObjectWithClassProto<ShadowRealmObject>(cx, proto)); |
| 82 |
if (!shadowRealmObj) { |
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
// Instead of managing Realms, spidermonkey associates a realm with a global |
| 87 |
// object, and so we will manage and store a global. |
| 88 |
|
| 89 |
// Step 3. Let realmRec be CreateRealm(). |
| 90 |
|
| 91 |
// Initially steal creation options from current realm: |
| 92 |
JS::RealmOptions options(cx->realm()->creationOptions(), |
| 93 |
cx->realm()->behaviors()); |
| 94 |
|
| 95 |
// We don't want to have to deal with CCWs in addition to |
| 96 |
// WrappedFunctionObjects. |
| 97 |
options.creationOptions().setExistingCompartment(cx->compartment()); |
| 98 |
|
| 99 |
JS::GlobalCreationCallback newGlobal = |
| 100 |
cx->runtime()->getShadowRealmGlobalCreationCallback(); |
| 101 |
// If an embedding didn't provide a callback to initialize the global, |
| 102 |
// use the basic default one. |
| 103 |
if (!newGlobal) { |
| 104 |
newGlobal = DefaultNewShadowRealmGlobal; |
| 105 |
} |
| 106 |
|
| 107 |
// Our shadow realm inherits the principals of the current realm, |
| 108 |
// but is otherwise constrained. |
| 109 |
JSPrincipals* principals = JS::GetRealmPrincipals(cx->realm()); |
| 110 |
|
| 111 |
// Steps 5-11: In SpiderMonkey these fall under the aegis of the global |
| 112 |
// creation. It's worth noting that the newGlobal callback |
| 113 |
// needs to respect the SetRealmGlobalObject call below, which |
| 114 |
// sets the global to |
| 115 |
// OrdinaryObjectCreate(intrinsics.[[%Object.prototype%]]). |
| 116 |
// |
| 117 |
// Step 5. Let context be a new execution context. |
| 118 |
// Step 6. Set the Function of context to null. |
| 119 |
// Step 7. Set the Realm of context to realmRec. |
| 120 |
// Step 8. Set the ScriptOrModule of context to null. |
| 121 |
// Step 9. Set O.[[ExecutionContext]] to context. |
| 122 |
// Step 10. Perform ? SetRealmGlobalObject(realmRec, undefined, undefined). |
| 123 |
// Step 11. Perform ? SetDefaultGlobalBindings(O.[[ShadowRealm]]). |
| 124 |
Rooted<JSObject*> global(cx, |
| 125 |
newGlobal(cx, options, principals, cx->global())); |
| 126 |
if (!global) { |
| 127 |
return false; |
| 128 |
} |
| 129 |
|
| 130 |
// Make sure the new global hook obeyed our request in the |
| 131 |
// creation options to have a same compartment global. |
| 132 |
MOZ_RELEASE_ASSERT(global->compartment() == cx->compartment()); |
| 133 |
|
| 134 |
// Step 4. Set O.[[ShadowRealm]] to realmRec. |
| 135 |
shadowRealmObj->initFixedSlot(GlobalSlot, ObjectValue(*global)); |
| 136 |
|
| 137 |
// Step 12. Perform ? HostInitializeShadowRealm(O.[[ShadowRealm]]). |
| 138 |
JS::GlobalInitializeCallback hostInitializeShadowRealm = |
| 139 |
cx->runtime()->getShadowRealmInitializeGlobalCallback(); |
| 140 |
if (hostInitializeShadowRealm) { |
| 141 |
if (!hostInitializeShadowRealm(cx, global)) { |
| 142 |
return false; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
// Step 13. Return O. |
| 147 |
args.rval().setObject(*shadowRealmObj); |
| 148 |
return true; |
| 149 |
} |
| 150 |
|
| 151 |
// https://tc39.es/proposal-shadowrealm/#sec-validateshadowrealmobject |
| 152 |
// (slightly modified into a cast operator too) |
| 153 |
static ShadowRealmObject* ValidateShadowRealmObject(JSContext* cx, |
| 154 |
Handle<Value> value) { |
| 155 |
// Step 1. Perform ? RequireInternalSlot(O, [[ShadowRealm]]). |
| 156 |
// Step 2. Perform ? RequireInternalSlot(O, [[ExecutionContext]]). |
| 157 |
return UnwrapAndTypeCheckValue<ShadowRealmObject>(cx, value, [cx]() { |
| 158 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 159 |
JSMSG_NOT_SHADOW_REALM); |
| 160 |
}); |
| 161 |
} |
| 162 |
|
| 163 |
void js::ReportPotentiallyDetailedMessage(JSContext* cx, |
| 164 |
const unsigned detailedError, |
| 165 |
const unsigned genericError) { |
| 166 |
// Return for non-catchable exceptions like interrupt requests. |
| 167 |
if (!cx->isExceptionPending()) { |
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
Rooted<Value> exception(cx); |
| 172 |
if (!cx->getPendingException(&exception)) { |
| 173 |
return; |
| 174 |
} |
| 175 |
cx->clearPendingException(); |
| 176 |
|
| 177 |
JS::ErrorReportBuilder jsReport(cx); |
| 178 |
JS::ExceptionStack exnStack(cx, exception, nullptr); |
| 179 |
if (!jsReport.init(cx, exnStack, JS::ErrorReportBuilder::NoSideEffects)) { |
| 180 |
cx->clearPendingException(); |
| 181 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, genericError); |
| 182 |
return; |
| 183 |
} |
| 184 |
|
| 185 |
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, detailedError, |
| 186 |
jsReport.toStringResult().c_str()); |
| 187 |
} |
| 188 |
|
| 189 |
// PerformShadowRealmEval ( sourceText: a String, callerRealm: a Realm Record, |
| 190 |
// evalRealm: a Realm Record, ) |
| 191 |
// |
| 192 |
// https://tc39.es/proposal-shadowrealm/#sec-performshadowrealmeval |
| 193 |
static bool PerformShadowRealmEval(JSContext* cx, Handle<JSString*> sourceText, |
| 194 |
Realm* callerRealm, Realm* evalRealm, |
| 195 |
MutableHandle<Value> rval) { |
| 196 |
MOZ_ASSERT(callerRealm != evalRealm); |
| 197 |
|
| 198 |
// Step 1. Perform ? HostEnsureCanCompileStrings(callerRealm, evalRealm). |
| 199 |
JS::RootedVector<JSString*> parameterStrings(cx); |
| 200 |
JS::RootedVector<Value> parameterArgs(cx); |
| 201 |
bool canCompileStrings = false; |
| 202 |
if (!cx->isRuntimeCodeGenEnabled(JS::RuntimeCode::JS, sourceText, |
| 203 |
JS::CompilationType::Undefined, |
| 204 |
parameterStrings, nullptr, parameterArgs, |
| 205 |
NullHandleValue, &canCompileStrings)) { |
| 206 |
return false; |
| 207 |
} |
| 208 |
if (!canCompileStrings) { |
| 209 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 210 |
JSMSG_CSP_BLOCKED_SHADOWREALM); |
| 211 |
return false; |
| 212 |
} |
| 213 |
|
| 214 |
// Need to compile the script into the realm we will execute into. |
| 215 |
// |
| 216 |
// We hoist the error handling out however to ensure that errors |
| 217 |
// are thrown from the correct realm. |
| 218 |
bool compileSuccess = false; |
| 219 |
bool evalSuccess = false; |
| 220 |
|
| 221 |
do { |
| 222 |
Rooted<GlobalObject*> evalRealmGlobal(cx, evalRealm->maybeGlobal()); |
| 223 |
AutoRealm ar(cx, evalRealmGlobal); |
| 224 |
|
| 225 |
// Step 2. Perform the following substeps in an implementation-defined |
| 226 |
// order, possibly interleaving parsing and error detection: |
| 227 |
// a. Let script be ParseText(! StringToCodePoints(sourceText), Script). |
| 228 |
// b. If script is a List of errors, throw a SyntaxError exception. |
| 229 |
// c. If script Contains ScriptBody is false, return undefined. |
| 230 |
// d. Let body be the ScriptBody of script. |
| 231 |
// e. If body Contains NewTarget is true, throw a SyntaxError exception. |
| 232 |
// f. If body Contains SuperProperty is true, throw a SyntaxError |
| 233 |
// exception. g. If body Contains SuperCall is true, throw a SyntaxError |
| 234 |
// exception. |
| 235 |
|
| 236 |
AutoStableStringChars linearChars(cx); |
| 237 |
if (!linearChars.initTwoByte(cx, sourceText)) { |
| 238 |
return false; |
| 239 |
} |
| 240 |
SourceText<char16_t> srcBuf; |
| 241 |
if (!srcBuf.initMaybeBorrowed(cx, linearChars)) { |
| 242 |
return false; |
| 243 |
} |
| 244 |
|
| 245 |
// Lets propagate some information into the compilation here. |
| 246 |
// |
| 247 |
// We may need to censor the stacks eventually, see |
| 248 |
// https://bugzilla.mozilla.org/show_bug.cgi?id=1770017 |
| 249 |
RootedScript callerScript(cx); |
| 250 |
const char* filename; |
| 251 |
uint32_t lineno; |
| 252 |
uint32_t pcOffset; |
| 253 |
bool mutedErrors; |
| 254 |
DescribeScriptedCallerForCompilation(cx, &callerScript, &filename, &lineno, |
| 255 |
&pcOffset, &mutedErrors); |
| 256 |
|
| 257 |
CompileOptions options(cx); |
| 258 |
options.setIsRunOnce(true) |
| 259 |
.setNoScriptRval(false) |
| 260 |
.setMutedErrors(mutedErrors) |
| 261 |
.setFileAndLine(filename, lineno); |
| 262 |
|
| 263 |
Rooted<Scope*> enclosing(cx, &evalRealmGlobal->emptyGlobalScope()); |
| 264 |
RootedScript script( |
| 265 |
cx, frontend::CompileEvalScript(cx, options, srcBuf, enclosing, |
| 266 |
evalRealmGlobal)); |
| 267 |
|
| 268 |
compileSuccess = !!script; |
| 269 |
if (!compileSuccess) { |
| 270 |
break; |
| 271 |
} |
| 272 |
|
| 273 |
// Step 3. Let strictEval be IsStrict of script. |
| 274 |
// Step 4. Let runningContext be the running execution context. |
| 275 |
// Step 5. Let lexEnv be NewDeclarativeEnvironment(evalRealm.[[GlobalEnv]]). |
| 276 |
// Step 6. Let varEnv be evalRealm.[[GlobalEnv]]. |
| 277 |
// Step 7. If strictEval is true, set varEnv to lexEnv. |
| 278 |
// Step 8. If runningContext is not already suspended, suspend |
| 279 |
// runningContext. Step 9. Let evalContext be a new ECMAScript code |
| 280 |
// execution context. Step 10. Set evalContext's Function to null. Step 11. |
| 281 |
// Set evalContext's Realm to evalRealm. Step 12. Set evalContext's |
| 282 |
// ScriptOrModule to null. Step 13. Set evalContext's VariableEnvironment to |
| 283 |
// varEnv. Step 14. Set evalContext's LexicalEnvironment to lexEnv. Step 15. |
| 284 |
// Push evalContext onto the execution context stack; evalContext is |
| 285 |
// now the running execution context. |
| 286 |
// Step 16. Let result be EvalDeclarationInstantiation(body, varEnv, |
| 287 |
// lexEnv, null, strictEval). |
| 288 |
// Step 17. If result.[[Type]] is normal, then |
| 289 |
// a. Set result to the result of evaluating body. |
| 290 |
// Step 18. If result.[[Type]] is normal and result.[[Value]] is empty, then |
| 291 |
// a. Set result to NormalCompletion(undefined). |
| 292 |
|
| 293 |
// Step 19. Suspend evalContext and remove it from the execution context |
| 294 |
// stack. |
| 295 |
// Step 20. Resume the context that is now on the top of the execution |
| 296 |
// context stack as the running execution context. |
| 297 |
Rooted<JSObject*> environment(cx, &evalRealmGlobal->lexicalEnvironment()); |
| 298 |
evalSuccess = ExecuteKernel(cx, script, environment, |
| 299 |
/* evalInFrame = */ NullFramePtr(), rval); |
| 300 |
} while (false); // AutoRealm |
| 301 |
|
| 302 |
if (!compileSuccess) { |
| 303 |
if (!cx->isExceptionPending()) { |
| 304 |
return false; |
| 305 |
} |
| 306 |
|
| 307 |
// Clone the exception into the current global and re-throw, as the |
| 308 |
// exception has to come from the current global. |
| 309 |
Rooted<Value> exception(cx); |
| 310 |
if (!cx->getPendingException(&exception)) { |
| 311 |
return false; |
| 312 |
} |
| 313 |
|
| 314 |
// Clear our exception now that we've got it, so that we don't |
| 315 |
// do the following call with an exception already pending. |
| 316 |
cx->clearPendingException(); |
| 317 |
|
| 318 |
Rooted<Value> clonedException(cx); |
| 319 |
if (!JS_StructuredClone(cx, exception, &clonedException, nullptr, |
| 320 |
nullptr)) { |
| 321 |
return false; |
| 322 |
} |
| 323 |
|
| 324 |
cx->setPendingException(clonedException, ShouldCaptureStack::Always); |
| 325 |
return false; |
| 326 |
} |
| 327 |
|
| 328 |
if (!evalSuccess) { |
| 329 |
// Step 21. If result.[[Type]] is not normal, throw a TypeError |
| 330 |
// exception. |
| 331 |
// |
| 332 |
// The type error here needs to come from the calling global, so has to |
| 333 |
// happen outside the AutoRealm above. |
| 334 |
ReportPotentiallyDetailedMessage(cx, |
| 335 |
JSMSG_SHADOW_REALM_EVALUATE_FAILURE_DETAIL, |
| 336 |
JSMSG_SHADOW_REALM_EVALUATE_FAILURE); |
| 337 |
|
| 338 |
return false; |
| 339 |
} |
| 340 |
|
| 341 |
// Wrap |rval| into the current compartment. |
| 342 |
if (!cx->compartment()->wrap(cx, rval)) { |
| 343 |
return false; |
| 344 |
} |
| 345 |
|
| 346 |
// Step 22. Return ? GetWrappedValue(callerRealm, result.[[Value]]). |
| 347 |
return GetWrappedValue(cx, callerRealm, rval, rval); |
| 348 |
} |
| 349 |
|
| 350 |
// ShadowRealm.prototype.evaluate ( sourceText ) |
| 351 |
// https://tc39.es/proposal-shadowrealm/#sec-shadowrealm.prototype.evaluate |
| 352 |
static bool ShadowRealm_evaluate(JSContext* cx, unsigned argc, Value* vp) { |
| 353 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 354 |
|
| 355 |
// Step 1. Let O be this value. |
| 356 |
HandleValue obj = args.thisv(); |
| 357 |
|
| 358 |
// Step 2. Perform ? ValidateShadowRealmObject(O) |
| 359 |
Rooted<ShadowRealmObject*> shadowRealm(cx, |
| 360 |
ValidateShadowRealmObject(cx, obj)); |
| 361 |
if (!shadowRealm) { |
| 362 |
return false; |
| 363 |
} |
| 364 |
|
| 365 |
// Step 3. If Type(sourceText) is not String, throw a TypeError exception. |
| 366 |
if (!args.get(0).isString()) { |
| 367 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 368 |
JSMSG_SHADOW_REALM_EVALUATE_NOT_STRING); |
| 369 |
return false; |
| 370 |
} |
| 371 |
Rooted<JSString*> sourceText(cx, args.get(0).toString()); |
| 372 |
|
| 373 |
// Step 4. Let callerRealm be the current Realm Record. |
| 374 |
Realm* callerRealm = cx->realm(); |
| 375 |
|
| 376 |
// Step 5. Let evalRealm be O.[[ShadowRealm]]. |
| 377 |
Realm* evalRealm = shadowRealm->getShadowRealm(); |
| 378 |
// Step 6. Return ? PerformShadowRealmEval(sourceText, callerRealm, |
| 379 |
// evalRealm). |
| 380 |
return PerformShadowRealmEval(cx, sourceText, callerRealm, evalRealm, |
| 381 |
args.rval()); |
| 382 |
} |
| 383 |
|
| 384 |
enum class ImportValueIndices : uint32_t { |
| 385 |
CalleRealm = 0, |
| 386 |
|
| 387 |
ExportNameString, |
| 388 |
|
| 389 |
Length, |
| 390 |
}; |
| 391 |
|
| 392 |
// MG:XXX: Cribbed/Overlapping with StartDynamicModuleImport; may need to |
| 393 |
// refactor to share. |
| 394 |
// https://tc39.es/proposal-shadowrealm/#sec-shadowrealmimportvalue |
| 395 |
static JSObject* ShadowRealmImportValue(JSContext* cx, |
| 396 |
Handle<JSString*> specifierString, |
| 397 |
Handle<JSString*> exportName, |
| 398 |
Realm* callerRealm, Realm* evalRealm) { |
| 399 |
// Step 1. Assert: evalContext is an execution context associated to a |
| 400 |
// ShadowRealm instance's [[ExecutionContext]]. |
| 401 |
|
| 402 |
// Step 2. Let innerCapability be ! NewPromiseCapability(%Promise%). |
| 403 |
Rooted<JSObject*> promiseConstructor(cx, JS::GetPromiseConstructor(cx)); |
| 404 |
if (!promiseConstructor) { |
| 405 |
return nullptr; |
| 406 |
} |
| 407 |
|
| 408 |
Rooted<JSObject*> promiseObject(cx, JS::NewPromiseObject(cx, nullptr)); |
| 409 |
if (!promiseObject) { |
| 410 |
return nullptr; |
| 411 |
} |
| 412 |
|
| 413 |
Handle<PromiseObject*> promise = promiseObject.as<PromiseObject>(); |
| 414 |
|
| 415 |
JS::ModuleDynamicImportHook importHook = |
| 416 |
cx->runtime()->moduleDynamicImportHook; |
| 417 |
|
| 418 |
if (!importHook) { |
| 419 |
// Dynamic import can be disabled by a pref and is not supported in all |
| 420 |
// contexts (e.g. web workers). |
| 421 |
JS_ReportErrorASCII( |
| 422 |
cx, |
| 423 |
"Dynamic module import is disabled or not supported in this context"); |
| 424 |
if (!RejectPromiseWithPendingError(cx, promise)) { |
| 425 |
return nullptr; |
| 426 |
} |
| 427 |
return promise; |
| 428 |
} |
| 429 |
|
| 430 |
{ |
| 431 |
// Step 3. Let runningContext be the running execution context. (Implicit) |
| 432 |
// Step 4. If runningContext is not already suspended, suspend |
| 433 |
// runningContext. (Implicit) |
| 434 |
// Step 5. Push evalContext onto the execution context stack; evalContext is |
| 435 |
// now the running execution context. (Implicit) |
| 436 |
Rooted<GlobalObject*> evalRealmGlobal(cx, evalRealm->maybeGlobal()); |
| 437 |
AutoRealm ar(cx, evalRealmGlobal); |
| 438 |
|
| 439 |
// Not Speced: Get referencing private to pass to importHook. |
| 440 |
RootedScript script(cx); |
| 441 |
const char* filename; |
| 442 |
uint32_t lineno; |
| 443 |
uint32_t pcOffset; |
| 444 |
bool mutedErrors; |
| 445 |
DescribeScriptedCallerForCompilation(cx, &script, &filename, &lineno, |
| 446 |
&pcOffset, &mutedErrors); |
| 447 |
|
| 448 |
MOZ_ASSERT(script); |
| 449 |
|
| 450 |
Rooted<JSAtom*> specifierAtom(cx, AtomizeString(cx, specifierString)); |
| 451 |
if (!specifierAtom) { |
| 452 |
if (!RejectPromiseWithPendingError(cx, promise)) { |
| 453 |
return nullptr; |
| 454 |
} |
| 455 |
return promise; |
| 456 |
} |
| 457 |
|
| 458 |
Rooted<ImportAttributeVector> attributes(cx); |
| 459 |
Rooted<JSObject*> moduleRequest( |
| 460 |
cx, ModuleRequestObject::create(cx, specifierAtom, attributes)); |
| 461 |
if (!moduleRequest) { |
| 462 |
if (!RejectPromiseWithPendingError(cx, promise)) { |
| 463 |
return nullptr; |
| 464 |
} |
| 465 |
return promise; |
| 466 |
} |
| 467 |
|
| 468 |
// Step 6. Perform ! HostImportModuleDynamically(null, specifierString, |
| 469 |
// innerCapability). |
| 470 |
// |
| 471 |
// By specification, this is supposed to take ReferencingScriptOrModule as |
| 472 |
// null, see first parameter above. However, if we do that, we don't end up |
| 473 |
// with a script reference, which is used to figure out what the base-URI |
| 474 |
// should be So then we end up using the default one for the module loader; |
| 475 |
// which because of the way we set the parent module loader up, means we end |
| 476 |
// up having the incorrect base URI, as the module loader ends up just using |
| 477 |
// the document's base URI. |
| 478 |
// |
| 479 |
// I have filed https://github.com/tc39/proposal-shadowrealm/issues/363 to |
| 480 |
// discuss this. |
| 481 |
Rooted<Value> referencingPrivate(cx, script->sourceObject()->getPrivate()); |
| 482 |
if (!importHook(cx, referencingPrivate, moduleRequest, promise)) { |
| 483 |
// If there's no exception pending then the script is terminating |
| 484 |
// anyway, so just return nullptr. |
| 485 |
if (!cx->isExceptionPending() || |
| 486 |
!RejectPromiseWithPendingError(cx, promise)) { |
| 487 |
return nullptr; |
| 488 |
} |
| 489 |
return promise; |
| 490 |
} |
| 491 |
|
| 492 |
// Step 7. Suspend evalContext and remove it from the execution context |
| 493 |
// stack. (Implicit) |
| 494 |
// Step 8. Resume the context that is now on the top of the execution |
| 495 |
// context stack as the running execution context (Implicit) |
| 496 |
} |
| 497 |
|
| 498 |
// Step 9. Let steps be the steps of an ExportGetter function as described |
| 499 |
// below. |
| 500 |
// Step 10. Let onFulfilled be ! CreateBuiltinFunction(steps, 1, "", « |
| 501 |
// [[ExportNameString]] », callerRealm). |
| 502 |
|
| 503 |
// The handler can only hold onto a single object, so we pack that into a new |
| 504 |
// array, and store there. |
| 505 |
Rooted<ArrayObject*> handlerObject( |
| 506 |
cx, |
| 507 |
NewDenseFullyAllocatedArray(cx, uint32_t(ImportValueIndices::Length))); |
| 508 |
if (!handlerObject) { |
| 509 |
return nullptr; |
| 510 |
} |
| 511 |
|
| 512 |
handlerObject->setDenseInitializedLength( |
| 513 |
uint32_t(ImportValueIndices::Length)); |
| 514 |
handlerObject->initDenseElement(uint32_t(ImportValueIndices::CalleRealm), |
| 515 |
PrivateValue(callerRealm)); |
| 516 |
handlerObject->initDenseElement( |
| 517 |
uint32_t(ImportValueIndices::ExportNameString), StringValue(exportName)); |
| 518 |
|
| 519 |
Rooted<JSFunction*> onFulfilled( |
| 520 |
cx, |
| 521 |
NewHandlerWithExtra( |
| 522 |
cx, |
| 523 |
[](JSContext* cx, unsigned argc, Value* vp) { |
| 524 |
// This is the export getter function from |
| 525 |
// https://tc39.es/proposal-shadowrealm/#sec-shadowrealmimportvalue |
| 526 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 527 |
MOZ_ASSERT(args.length() == 1); |
| 528 |
|
| 529 |
auto* handlerObject = ExtraFromHandler<ArrayObject>(args); |
| 530 |
|
| 531 |
Rooted<Value> realmValue( |
| 532 |
cx, handlerObject->getDenseElement( |
| 533 |
uint32_t(ImportValueIndices::CalleRealm))); |
| 534 |
Rooted<Value> exportNameValue( |
| 535 |
cx, handlerObject->getDenseElement( |
| 536 |
uint32_t(ImportValueIndices::ExportNameString))); |
| 537 |
|
| 538 |
// Step 1. Assert: exports is a module namespace exotic object. |
| 539 |
Handle<Value> exportsValue = args[0]; |
| 540 |
MOZ_ASSERT(exportsValue.isObject() && |
| 541 |
exportsValue.toObject().is<ModuleNamespaceObject>()); |
| 542 |
|
| 543 |
Rooted<ModuleNamespaceObject*> exports( |
| 544 |
cx, &exportsValue.toObject().as<ModuleNamespaceObject>()); |
| 545 |
|
| 546 |
// Step 2. Let f be the active function object. (not implemented |
| 547 |
// this way) |
| 548 |
// |
| 549 |
// Step 3. Let string be f.[[ExportNameString]]. Step 4. |
| 550 |
// Assert: Type(string) is String. |
| 551 |
MOZ_ASSERT(exportNameValue.isString()); |
| 552 |
|
| 553 |
Rooted<JSAtom*> stringAtom( |
| 554 |
cx, AtomizeString(cx, exportNameValue.toString())); |
| 555 |
if (!stringAtom) { |
| 556 |
return false; |
| 557 |
} |
| 558 |
Rooted<jsid> stringId(cx, AtomToId(stringAtom)); |
| 559 |
|
| 560 |
// Step 5. Let hasOwn be ? HasOwnProperty(exports, string). |
| 561 |
bool hasOwn = false; |
| 562 |
if (!HasOwnProperty(cx, exports, stringId, &hasOwn)) { |
| 563 |
return false; |
| 564 |
} |
| 565 |
|
| 566 |
// Step 6. If hasOwn is false, throw a TypeError exception. |
| 567 |
if (!hasOwn) { |
| 568 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 569 |
JSMSG_SHADOW_REALM_VALUE_NOT_EXPORTED); |
| 570 |
return false; |
| 571 |
} |
| 572 |
|
| 573 |
// Step 7. Let value be ? Get(exports, string). |
| 574 |
Rooted<Value> value(cx); |
| 575 |
if (!GetProperty(cx, exports, exports, stringId, &value)) { |
| 576 |
return false; |
| 577 |
} |
| 578 |
|
| 579 |
// Step 8. Let realm be f.[[Realm]]. |
| 580 |
Realm* callerRealm = static_cast<Realm*>(realmValue.toPrivate()); |
| 581 |
|
| 582 |
// Step 9. Return ? GetWrappedValue(realm, value). |
| 583 |
return GetWrappedValue(cx, callerRealm, value, args.rval()); |
| 584 |
}, |
| 585 |
promise, handlerObject)); |
| 586 |
if (!onFulfilled) { |
| 587 |
return nullptr; |
| 588 |
} |
| 589 |
|
| 590 |
Rooted<JSFunction*> onRejected( |
| 591 |
cx, NewHandler( |
| 592 |
cx, |
| 593 |
[](JSContext* cx, unsigned argc, Value* vp) { |
| 594 |
JS_ReportErrorNumberASCII( |
| 595 |
cx, GetErrorMessage, nullptr, |
| 596 |
JSMSG_SHADOW_REALM_IMPORTVALUE_FAILED); |
| 597 |
return false; |
| 598 |
}, |
| 599 |
promise)); |
| 600 |
if (!onRejected) { |
| 601 |
return nullptr; |
| 602 |
} |
| 603 |
|
| 604 |
// Step 11. Set onFulfilled.[[ExportNameString]] to exportNameString. |
| 605 |
// Step 12. Let promiseCapability be ! NewPromiseCapability(%Promise%). |
| 606 |
// Step 13. Return ! PerformPromiseThen(innerCapability.[[Promise]], |
| 607 |
// onFulfilled, callerRealm.[[Intrinsics]].[[%ThrowTypeError%]], |
| 608 |
// promiseCapability). |
| 609 |
return OriginalPromiseThen(cx, promise, onFulfilled, onRejected); |
| 610 |
} |
| 611 |
|
| 612 |
// ShadowRealm.prototype.importValue ( specifier, exportName ) |
| 613 |
// https://tc39.es/proposal-shadowrealm/#sec-shadowrealm.prototype.importvalue |
| 614 |
static bool ShadowRealm_importValue(JSContext* cx, unsigned argc, Value* vp) { |
| 615 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 616 |
|
| 617 |
// Step 1. Let O be this value. |
| 618 |
HandleValue obj = args.thisv(); |
| 619 |
|
| 620 |
// Step 2. Perform ? ValidateShadowRealmObject(O). |
| 621 |
Rooted<ShadowRealmObject*> shadowRealm(cx, |
| 622 |
ValidateShadowRealmObject(cx, obj)); |
| 623 |
if (!shadowRealm) { |
| 624 |
return false; |
| 625 |
} |
| 626 |
|
| 627 |
// Step 3. Let specifierString be ? ToString(specifier). |
| 628 |
Rooted<JSString*> specifierString(cx, ToString<CanGC>(cx, args.get(0))); |
| 629 |
if (!specifierString) { |
| 630 |
return false; |
| 631 |
} |
| 632 |
|
| 633 |
// Step 4. If Type(exportName) is not String, throw a TypeError exception. |
| 634 |
if (!args.get(1).isString()) { |
| 635 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 636 |
JSMSG_SHADOW_REALM_EXPORT_NOT_STRING); |
| 637 |
return false; |
| 638 |
} |
| 639 |
|
| 640 |
Rooted<JSString*> exportName(cx, args.get(1).toString()); |
| 641 |
if (!exportName) { |
| 642 |
return false; |
| 643 |
} |
| 644 |
|
| 645 |
// Step 5. Let callerRealm be the current Realm Record. |
| 646 |
Realm* callerRealm = cx->realm(); |
| 647 |
|
| 648 |
// Step 6. Let evalRealm be O.[[ShadowRealm]]. |
| 649 |
Realm* evalRealm = shadowRealm->getShadowRealm(); |
| 650 |
|
| 651 |
// Step 7. Let evalContext be O.[[ExecutionContext]] |
| 652 |
// (we dont' pass this explicitly, instead using the realm+global to |
| 653 |
// represent) |
| 654 |
|
| 655 |
// Step 8. Return ? |
| 656 |
// ShadowRealmImportValue(specifierString, exportName, |
| 657 |
// callerRealm, evalRealm, |
| 658 |
// evalContext). |
| 659 |
|
| 660 |
JSObject* res = ShadowRealmImportValue(cx, specifierString, exportName, |
| 661 |
callerRealm, evalRealm); |
| 662 |
if (!res) { |
| 663 |
return false; |
| 664 |
} |
| 665 |
|
| 666 |
args.rval().set(ObjectValue(*res)); |
| 667 |
return true; |
| 668 |
} |
| 669 |
|
| 670 |
static const JSFunctionSpec shadowrealm_methods[] = { |
| 671 |
JS_FN("evaluate", ShadowRealm_evaluate, 1, 0), |
| 672 |
JS_FN("importValue", ShadowRealm_importValue, 2, 0), |
| 673 |
JS_FS_END, |
| 674 |
}; |
| 675 |
|
| 676 |
static const JSPropertySpec shadowrealm_properties[] = { |
| 677 |
JS_STRING_SYM_PS(toStringTag, "ShadowRealm", JSPROP_READONLY), |
| 678 |
JS_PS_END, |
| 679 |
}; |
| 680 |
|
| 681 |
static const ClassSpec ShadowRealmObjectClassSpec = { |
| 682 |
GenericCreateConstructor<ShadowRealmObject::construct, 0, |
| 683 |
gc::AllocKind::FUNCTION>, |
| 684 |
GenericCreatePrototype<ShadowRealmObject>, |
| 685 |
nullptr, // Static methods |
| 686 |
nullptr, // Static properties |
| 687 |
shadowrealm_methods, // Methods |
| 688 |
shadowrealm_properties, // Properties |
| 689 |
}; |
| 690 |
|
| 691 |
const JSClass ShadowRealmObject::class_ = { |
| 692 |
"ShadowRealm", |
| 693 |
JSCLASS_HAS_CACHED_PROTO(JSProto_ShadowRealm) | |
| 694 |
JSCLASS_HAS_RESERVED_SLOTS(ShadowRealmObject::SlotCount), |
| 695 |
JS_NULL_CLASS_OPS, |
| 696 |
&ShadowRealmObjectClassSpec, |
| 697 |
}; |
| 698 |
|
| 699 |
const JSClass ShadowRealmObject::protoClass_ = { |
| 700 |
"ShadowRealm.prototype", |
| 701 |
JSCLASS_HAS_CACHED_PROTO(JSProto_ShadowRealm), |
| 702 |
JS_NULL_CLASS_OPS, |
| 703 |
&ShadowRealmObjectClassSpec, |
| 704 |
}; |