| 1 |
/* -*- Mode: javascript; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 |
| 2 |
* -*- */ |
| 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 "shell/ModuleLoader.h" |
| 8 |
|
| 9 |
#include "mozilla/DebugOnly.h" |
| 10 |
#include "mozilla/TextUtils.h" |
| 11 |
|
| 12 |
#include "jsapi.h" |
| 13 |
#include "NamespaceImports.h" |
| 14 |
|
| 15 |
#include "builtin/TestingUtility.h" // js::CreateScriptPrivate |
| 16 |
#include "js/Conversions.h" |
| 17 |
#include "js/MapAndSet.h" |
| 18 |
#include "js/Modules.h" |
| 19 |
#include "js/PropertyAndElement.h" // JS_DefineProperty, JS_GetProperty |
| 20 |
#include "js/SourceText.h" |
| 21 |
#include "js/StableStringChars.h" |
| 22 |
#include "shell/jsshell.h" |
| 23 |
#include "shell/OSObject.h" |
| 24 |
#include "shell/StringUtils.h" |
| 25 |
#include "util/Text.h" |
| 26 |
#include "vm/JSAtomUtils.h" // AtomizeString, PinAtom |
| 27 |
#include "vm/JSContext.h" |
| 28 |
#include "vm/StringType.h" |
| 29 |
|
| 30 |
#include "vm/NativeObject-inl.h" |
| 31 |
|
| 32 |
using namespace js; |
| 33 |
using namespace js::shell; |
| 34 |
|
| 35 |
static constexpr char16_t JavaScriptScheme[] = u"javascript:"; |
| 36 |
|
| 37 |
static bool IsJavaScriptURL(Handle<JSLinearString*> path) { |
| 38 |
return StringStartsWith(path, JavaScriptScheme); |
| 39 |
} |
| 40 |
|
| 41 |
static JSString* ExtractJavaScriptURLSource(JSContext* cx, |
| 42 |
Handle<JSLinearString*> path) { |
| 43 |
MOZ_ASSERT(IsJavaScriptURL(path)); |
| 44 |
|
| 45 |
const size_t schemeLength = js_strlen(JavaScriptScheme); |
| 46 |
return SubString(cx, path, schemeLength); |
| 47 |
} |
| 48 |
|
| 49 |
bool ModuleLoader::init(JSContext* cx, HandleString loadPath) { |
| 50 |
loadPathStr = AtomizeString(cx, loadPath); |
| 51 |
if (!loadPathStr || !PinAtom(cx, loadPathStr)) { |
| 52 |
return false; |
| 53 |
} |
| 54 |
|
| 55 |
MOZ_ASSERT(IsAbsolutePath(loadPathStr)); |
| 56 |
|
| 57 |
char16_t sep = PathSeparator; |
| 58 |
pathSeparatorStr = AtomizeChars(cx, &sep, 1); |
| 59 |
if (!pathSeparatorStr || !PinAtom(cx, pathSeparatorStr)) { |
| 60 |
return false; |
| 61 |
} |
| 62 |
|
| 63 |
JSRuntime* rt = cx->runtime(); |
| 64 |
JS::SetModuleResolveHook(rt, ModuleLoader::ResolveImportedModule); |
| 65 |
JS::SetModuleMetadataHook(rt, ModuleLoader::GetImportMetaProperties); |
| 66 |
JS::SetModuleDynamicImportHook(rt, ModuleLoader::ImportModuleDynamically); |
| 67 |
return true; |
| 68 |
} |
| 69 |
|
| 70 |
// static |
| 71 |
JSObject* ModuleLoader::ResolveImportedModule( |
| 72 |
JSContext* cx, JS::HandleValue referencingPrivate, |
| 73 |
JS::HandleObject moduleRequest) { |
| 74 |
ShellContext* scx = GetShellContext(cx); |
| 75 |
return scx->moduleLoader->resolveImportedModule(cx, referencingPrivate, |
| 76 |
moduleRequest); |
| 77 |
} |
| 78 |
|
| 79 |
// static |
| 80 |
bool ModuleLoader::GetImportMetaProperties(JSContext* cx, |
| 81 |
JS::HandleValue privateValue, |
| 82 |
JS::HandleObject metaObject) { |
| 83 |
ShellContext* scx = GetShellContext(cx); |
| 84 |
return scx->moduleLoader->populateImportMeta(cx, privateValue, metaObject); |
| 85 |
} |
| 86 |
|
| 87 |
bool ModuleLoader::ImportMetaResolve(JSContext* cx, unsigned argc, Value* vp) { |
| 88 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 89 |
RootedValue modulePrivate( |
| 90 |
cx, js::GetFunctionNativeReserved(&args.callee(), ModulePrivateSlot)); |
| 91 |
|
| 92 |
// https://html.spec.whatwg.org/#hostgetimportmetaproperties |
| 93 |
// Step 4.1. Set specifier to ? ToString(specifier). |
| 94 |
// |
| 95 |
// https://tc39.es/ecma262/#sec-tostring |
| 96 |
RootedValue v(cx, args.get(ImportMetaResolveSpecifierArg)); |
| 97 |
RootedString specifier(cx, JS::ToString(cx, v)); |
| 98 |
if (!specifier) { |
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
// Step 4.2, 4.3 are implemented in importMetaResolve. |
| 103 |
ShellContext* scx = GetShellContext(cx); |
| 104 |
RootedString url(cx); |
| 105 |
if (!scx->moduleLoader->importMetaResolve(cx, modulePrivate, specifier, |
| 106 |
&url)) { |
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
// Step 4.4. Return the serialization of url. |
| 111 |
args.rval().setString(url); |
| 112 |
return true; |
| 113 |
} |
| 114 |
|
| 115 |
// static |
| 116 |
bool ModuleLoader::ImportModuleDynamically(JSContext* cx, |
| 117 |
JS::HandleValue referencingPrivate, |
| 118 |
JS::HandleObject moduleRequest, |
| 119 |
JS::HandleObject promise) { |
| 120 |
ShellContext* scx = GetShellContext(cx); |
| 121 |
return scx->moduleLoader->dynamicImport(cx, referencingPrivate, moduleRequest, |
| 122 |
promise); |
| 123 |
} |
| 124 |
|
| 125 |
bool ModuleLoader::loadRootModule(JSContext* cx, HandleString path) { |
| 126 |
RootedValue rval(cx); |
| 127 |
if (!loadAndExecute(cx, path, nullptr, &rval)) { |
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
RootedObject evaluationPromise(cx, &rval.toObject()); |
| 132 |
if (evaluationPromise == nullptr) { |
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
return JS::ThrowOnModuleEvaluationFailure(cx, evaluationPromise); |
| 137 |
} |
| 138 |
|
| 139 |
bool ModuleLoader::registerTestModule(JSContext* cx, HandleObject moduleRequest, |
| 140 |
Handle<ModuleObject*> module) { |
| 141 |
Rooted<JSLinearString*> path( |
| 142 |
cx, resolve(cx, moduleRequest, UndefinedHandleValue)); |
| 143 |
if (!path) { |
| 144 |
return false; |
| 145 |
} |
| 146 |
|
| 147 |
path = normalizePath(cx, path); |
| 148 |
if (!path) { |
| 149 |
return false; |
| 150 |
} |
| 151 |
|
| 152 |
JS::ModuleType moduleType = |
| 153 |
moduleRequest->as<ModuleRequestObject>().moduleType(); |
| 154 |
|
| 155 |
return addModuleToRegistry(cx, moduleType, path, module); |
| 156 |
} |
| 157 |
|
| 158 |
void ModuleLoader::clearModules(JSContext* cx) { |
| 159 |
Handle<GlobalObject*> global = cx->global(); |
| 160 |
global->setReservedSlot(GlobalAppSlotModuleRegistry, UndefinedValue()); |
| 161 |
} |
| 162 |
|
| 163 |
bool ModuleLoader::loadAndExecute(JSContext* cx, HandleString path, |
| 164 |
HandleObject moduleRequestArg, |
| 165 |
MutableHandleValue rval) { |
| 166 |
RootedObject module(cx, loadAndParse(cx, path, moduleRequestArg)); |
| 167 |
if (!module) { |
| 168 |
return false; |
| 169 |
} |
| 170 |
|
| 171 |
if (!JS::ModuleLink(cx, module)) { |
| 172 |
return false; |
| 173 |
} |
| 174 |
|
| 175 |
return JS::ModuleEvaluate(cx, module, rval); |
| 176 |
} |
| 177 |
|
| 178 |
JSObject* ModuleLoader::resolveImportedModule( |
| 179 |
JSContext* cx, JS::HandleValue referencingPrivate, |
| 180 |
JS::HandleObject moduleRequest) { |
| 181 |
Rooted<JSLinearString*> path(cx, |
| 182 |
resolve(cx, moduleRequest, referencingPrivate)); |
| 183 |
if (!path) { |
| 184 |
return nullptr; |
| 185 |
} |
| 186 |
|
| 187 |
return loadAndParse(cx, path, moduleRequest); |
| 188 |
} |
| 189 |
|
| 190 |
bool ModuleLoader::populateImportMeta(JSContext* cx, |
| 191 |
JS::HandleValue privateValue, |
| 192 |
JS::HandleObject metaObject) { |
| 193 |
Rooted<JSLinearString*> path(cx); |
| 194 |
if (!privateValue.isUndefined()) { |
| 195 |
if (!getScriptPath(cx, privateValue, &path)) { |
| 196 |
return false; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
if (!path) { |
| 201 |
path = NewStringCopyZ<CanGC>(cx, "(unknown)"); |
| 202 |
if (!path) { |
| 203 |
return false; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
RootedValue pathValue(cx, StringValue(path)); |
| 208 |
if (!JS_DefineProperty(cx, metaObject, "url", pathValue, JSPROP_ENUMERATE)) { |
| 209 |
return false; |
| 210 |
} |
| 211 |
|
| 212 |
JSFunction* resolveFunc = js::DefineFunctionWithReserved( |
| 213 |
cx, metaObject, "resolve", ImportMetaResolve, ImportMetaResolveNumArgs, |
| 214 |
JSPROP_ENUMERATE); |
| 215 |
if (!resolveFunc) { |
| 216 |
return false; |
| 217 |
} |
| 218 |
|
| 219 |
RootedObject resolveFuncObj(cx, JS_GetFunctionObject(resolveFunc)); |
| 220 |
js::SetFunctionNativeReserved(resolveFuncObj, ModulePrivateSlot, |
| 221 |
privateValue); |
| 222 |
|
| 223 |
return true; |
| 224 |
} |
| 225 |
|
| 226 |
bool ModuleLoader::importMetaResolve(JSContext* cx, |
| 227 |
JS::Handle<JS::Value> referencingPrivate, |
| 228 |
JS::Handle<JSString*> specifier, |
| 229 |
JS::MutableHandle<JSString*> urlOut) { |
| 230 |
Rooted<JSLinearString*> path(cx, resolve(cx, specifier, referencingPrivate)); |
| 231 |
if (!path) { |
| 232 |
return false; |
| 233 |
} |
| 234 |
|
| 235 |
urlOut.set(path); |
| 236 |
return true; |
| 237 |
} |
| 238 |
|
| 239 |
bool ModuleLoader::dynamicImport(JSContext* cx, |
| 240 |
JS::HandleValue referencingPrivate, |
| 241 |
JS::HandleObject moduleRequest, |
| 242 |
JS::HandleObject promise) { |
| 243 |
// To make this more realistic, use a promise to delay the import and make it |
| 244 |
// happen asynchronously. This method packages up the arguments and creates a |
| 245 |
// resolved promise, which on fullfillment calls doDynamicImport with the |
| 246 |
// original arguments. |
| 247 |
|
| 248 |
MOZ_ASSERT(promise); |
| 249 |
RootedValue moduleRequestValue(cx, ObjectValue(*moduleRequest)); |
| 250 |
RootedValue promiseValue(cx, ObjectValue(*promise)); |
| 251 |
RootedObject closure(cx, JS_NewObjectWithGivenProto(cx, nullptr, nullptr)); |
| 252 |
if (!closure || |
| 253 |
!JS_DefineProperty(cx, closure, "referencingPrivate", referencingPrivate, |
| 254 |
JSPROP_ENUMERATE) || |
| 255 |
!JS_DefineProperty(cx, closure, "moduleRequest", moduleRequestValue, |
| 256 |
JSPROP_ENUMERATE) || |
| 257 |
!JS_DefineProperty(cx, closure, "promise", promiseValue, |
| 258 |
JSPROP_ENUMERATE)) { |
| 259 |
return false; |
| 260 |
} |
| 261 |
|
| 262 |
RootedFunction onResolved( |
| 263 |
cx, NewNativeFunction(cx, DynamicImportDelayFulfilled, 1, nullptr)); |
| 264 |
if (!onResolved) { |
| 265 |
return false; |
| 266 |
} |
| 267 |
|
| 268 |
RootedFunction onRejected( |
| 269 |
cx, NewNativeFunction(cx, DynamicImportDelayRejected, 1, nullptr)); |
| 270 |
if (!onRejected) { |
| 271 |
return false; |
| 272 |
} |
| 273 |
|
| 274 |
RootedObject delayPromise(cx); |
| 275 |
RootedValue closureValue(cx, ObjectValue(*closure)); |
| 276 |
delayPromise = PromiseObject::unforgeableResolve(cx, closureValue); |
| 277 |
if (!delayPromise) { |
| 278 |
return false; |
| 279 |
} |
| 280 |
|
| 281 |
return JS::AddPromiseReactions(cx, delayPromise, onResolved, onRejected); |
| 282 |
} |
| 283 |
|
| 284 |
bool ModuleLoader::DynamicImportDelayFulfilled(JSContext* cx, unsigned argc, |
| 285 |
Value* vp) { |
| 286 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 287 |
RootedObject closure(cx, &args[0].toObject()); |
| 288 |
|
| 289 |
RootedValue referencingPrivate(cx); |
| 290 |
RootedValue moduleRequestValue(cx); |
| 291 |
RootedValue promiseValue(cx); |
| 292 |
if (!JS_GetProperty(cx, closure, "referencingPrivate", &referencingPrivate) || |
| 293 |
!JS_GetProperty(cx, closure, "moduleRequest", &moduleRequestValue) || |
| 294 |
!JS_GetProperty(cx, closure, "promise", &promiseValue)) { |
| 295 |
return false; |
| 296 |
} |
| 297 |
|
| 298 |
RootedObject moduleRequest(cx, &moduleRequestValue.toObject()); |
| 299 |
RootedObject promise(cx, &promiseValue.toObject()); |
| 300 |
|
| 301 |
ShellContext* scx = GetShellContext(cx); |
| 302 |
return scx->moduleLoader->doDynamicImport(cx, referencingPrivate, |
| 303 |
moduleRequest, promise); |
| 304 |
} |
| 305 |
|
| 306 |
bool ModuleLoader::DynamicImportDelayRejected(JSContext* cx, unsigned argc, |
| 307 |
Value* vp) { |
| 308 |
MOZ_CRASH("This promise should never be rejected"); |
| 309 |
} |
| 310 |
|
| 311 |
bool ModuleLoader::doDynamicImport(JSContext* cx, |
| 312 |
JS::HandleValue referencingPrivate, |
| 313 |
JS::HandleObject moduleRequest, |
| 314 |
JS::HandleObject promise) { |
| 315 |
// Exceptions during dynamic import are handled by calling |
| 316 |
// FinishDynamicModuleImport with a pending exception on the context. |
| 317 |
RootedValue rval(cx); |
| 318 |
bool ok = |
| 319 |
tryDynamicImport(cx, referencingPrivate, moduleRequest, promise, &rval); |
| 320 |
JSObject* evaluationObject = ok ? &rval.toObject() : nullptr; |
| 321 |
RootedObject evaluationPromise(cx, evaluationObject); |
| 322 |
return JS::FinishDynamicModuleImport( |
| 323 |
cx, evaluationPromise, referencingPrivate, moduleRequest, promise); |
| 324 |
} |
| 325 |
|
| 326 |
bool ModuleLoader::tryDynamicImport(JSContext* cx, |
| 327 |
JS::HandleValue referencingPrivate, |
| 328 |
JS::HandleObject moduleRequest, |
| 329 |
JS::HandleObject promise, |
| 330 |
JS::MutableHandleValue rval) { |
| 331 |
Rooted<JSLinearString*> path(cx, |
| 332 |
resolve(cx, moduleRequest, referencingPrivate)); |
| 333 |
if (!path) { |
| 334 |
return false; |
| 335 |
} |
| 336 |
|
| 337 |
return loadAndExecute(cx, path, moduleRequest, rval); |
| 338 |
} |
| 339 |
|
| 340 |
JSLinearString* ModuleLoader::resolve(JSContext* cx, |
| 341 |
HandleObject moduleRequestArg, |
| 342 |
HandleValue referencingInfo) { |
| 343 |
ModuleRequestObject* moduleRequest = |
| 344 |
&moduleRequestArg->as<ModuleRequestObject>(); |
| 345 |
if (moduleRequest->specifier()->length() == 0) { |
| 346 |
JS_ReportErrorASCII(cx, "Invalid module specifier"); |
| 347 |
return nullptr; |
| 348 |
} |
| 349 |
|
| 350 |
Rooted<JSLinearString*> name( |
| 351 |
cx, JS_EnsureLinearString(cx, moduleRequest->specifier())); |
| 352 |
if (!name) { |
| 353 |
return nullptr; |
| 354 |
} |
| 355 |
|
| 356 |
return resolve(cx, name, referencingInfo); |
| 357 |
} |
| 358 |
|
| 359 |
JSLinearString* ModuleLoader::resolve(JSContext* cx, HandleString specifier, |
| 360 |
HandleValue referencingInfo) { |
| 361 |
Rooted<JSLinearString*> name(cx, JS_EnsureLinearString(cx, specifier)); |
| 362 |
if (!name) { |
| 363 |
return nullptr; |
| 364 |
} |
| 365 |
|
| 366 |
if (IsJavaScriptURL(name) || IsAbsolutePath(name)) { |
| 367 |
return name; |
| 368 |
} |
| 369 |
|
| 370 |
// Treat |name| as a relative path if it starts with either "./" or "../". |
| 371 |
bool isRelative = |
| 372 |
StringStartsWith(name, u"./") || StringStartsWith(name, u"../") |
| 373 |
#ifdef XP_WIN |
| 374 |
|| StringStartsWith(name, u".\\") || StringStartsWith(name, u"..\\") |
| 375 |
#endif |
| 376 |
; |
| 377 |
|
| 378 |
RootedString path(cx, loadPathStr); |
| 379 |
|
| 380 |
if (isRelative) { |
| 381 |
if (referencingInfo.isUndefined()) { |
| 382 |
JS_ReportErrorASCII(cx, "No referencing module for relative import"); |
| 383 |
return nullptr; |
| 384 |
} |
| 385 |
|
| 386 |
Rooted<JSLinearString*> refPath(cx); |
| 387 |
if (!getScriptPath(cx, referencingInfo, &refPath)) { |
| 388 |
return nullptr; |
| 389 |
} |
| 390 |
|
| 391 |
if (!refPath) { |
| 392 |
JS_ReportErrorASCII(cx, "No path set for referencing module"); |
| 393 |
return nullptr; |
| 394 |
} |
| 395 |
|
| 396 |
int32_t sepIndex = LastIndexOf(refPath, u'/'); |
| 397 |
#ifdef XP_WIN |
| 398 |
sepIndex = std::max(sepIndex, LastIndexOf(refPath, u'\\')); |
| 399 |
#endif |
| 400 |
if (sepIndex >= 0) { |
| 401 |
path = SubString(cx, refPath, 0, sepIndex); |
| 402 |
if (!path) { |
| 403 |
return nullptr; |
| 404 |
} |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
RootedString result(cx); |
| 409 |
RootedString pathSep(cx, pathSeparatorStr); |
| 410 |
result = JS_ConcatStrings(cx, path, pathSep); |
| 411 |
if (!result) { |
| 412 |
return nullptr; |
| 413 |
} |
| 414 |
|
| 415 |
result = JS_ConcatStrings(cx, result, name); |
| 416 |
if (!result) { |
| 417 |
return nullptr; |
| 418 |
} |
| 419 |
|
| 420 |
Rooted<JSLinearString*> linear(cx, JS_EnsureLinearString(cx, result)); |
| 421 |
if (!linear) { |
| 422 |
return nullptr; |
| 423 |
} |
| 424 |
return normalizePath(cx, linear); |
| 425 |
} |
| 426 |
|
| 427 |
JSObject* ModuleLoader::loadAndParse(JSContext* cx, HandleString pathArg, |
| 428 |
JS::HandleObject moduleRequestArg) { |
| 429 |
Rooted<JSLinearString*> path(cx, JS_EnsureLinearString(cx, pathArg)); |
| 430 |
if (!path) { |
| 431 |
return nullptr; |
| 432 |
} |
| 433 |
|
| 434 |
path = normalizePath(cx, path); |
| 435 |
if (!path) { |
| 436 |
return nullptr; |
| 437 |
} |
| 438 |
|
| 439 |
JS::ModuleType moduleType = JS::ModuleType::JavaScript; |
| 440 |
if (moduleRequestArg) { |
| 441 |
moduleType = moduleRequestArg->as<ModuleRequestObject>().moduleType(); |
| 442 |
} |
| 443 |
|
| 444 |
RootedObject module(cx); |
| 445 |
if (!lookupModuleInRegistry(cx, moduleType, path, &module)) { |
| 446 |
return nullptr; |
| 447 |
} |
| 448 |
|
| 449 |
if (module) { |
| 450 |
return module; |
| 451 |
} |
| 452 |
|
| 453 |
UniqueChars filename = JS_EncodeStringToUTF8(cx, path); |
| 454 |
if (!filename) { |
| 455 |
return nullptr; |
| 456 |
} |
| 457 |
|
| 458 |
JS::CompileOptions options(cx); |
| 459 |
options.setFileAndLine(filename.get(), 1); |
| 460 |
|
| 461 |
RootedString source(cx, fetchSource(cx, path)); |
| 462 |
if (!source) { |
| 463 |
return nullptr; |
| 464 |
} |
| 465 |
|
| 466 |
JS::AutoStableStringChars linearChars(cx); |
| 467 |
if (!linearChars.initTwoByte(cx, source)) { |
| 468 |
return nullptr; |
| 469 |
} |
| 470 |
|
| 471 |
JS::SourceText<char16_t> srcBuf; |
| 472 |
if (!srcBuf.initMaybeBorrowed(cx, linearChars)) { |
| 473 |
return nullptr; |
| 474 |
} |
| 475 |
|
| 476 |
switch (moduleType) { |
| 477 |
case JS::ModuleType::Unknown: |
| 478 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 479 |
JSMSG_BAD_MODULE_TYPE); |
| 480 |
return nullptr; |
| 481 |
case JS::ModuleType::JavaScript: { |
| 482 |
module = JS::CompileModule(cx, options, srcBuf); |
| 483 |
if (!module) { |
| 484 |
return nullptr; |
| 485 |
} |
| 486 |
|
| 487 |
RootedObject info(cx, js::CreateScriptPrivate(cx, path)); |
| 488 |
if (!info) { |
| 489 |
return nullptr; |
| 490 |
} |
| 491 |
|
| 492 |
JS::SetModulePrivate(module, ObjectValue(*info)); |
| 493 |
} break; |
| 494 |
case JS::ModuleType::JSON: |
| 495 |
module = JS::CompileJsonModule(cx, options, srcBuf); |
| 496 |
if (!module) { |
| 497 |
return nullptr; |
| 498 |
} |
| 499 |
break; |
| 500 |
} |
| 501 |
|
| 502 |
if (!addModuleToRegistry(cx, moduleType, path, module)) { |
| 503 |
return nullptr; |
| 504 |
} |
| 505 |
|
| 506 |
return module; |
| 507 |
} |
| 508 |
|
| 509 |
bool ModuleLoader::lookupModuleInRegistry(JSContext* cx, |
| 510 |
JS::ModuleType moduleType, |
| 511 |
HandleString path, |
| 512 |
MutableHandleObject moduleOut) { |
| 513 |
moduleOut.set(nullptr); |
| 514 |
|
| 515 |
RootedObject registry(cx, getOrCreateModuleRegistry(cx, moduleType)); |
| 516 |
if (!registry) { |
| 517 |
return false; |
| 518 |
} |
| 519 |
|
| 520 |
RootedValue pathValue(cx, StringValue(path)); |
| 521 |
RootedValue moduleValue(cx); |
| 522 |
if (!JS::MapGet(cx, registry, pathValue, &moduleValue)) { |
| 523 |
return false; |
| 524 |
} |
| 525 |
|
| 526 |
if (!moduleValue.isUndefined()) { |
| 527 |
moduleOut.set(&moduleValue.toObject()); |
| 528 |
} |
| 529 |
|
| 530 |
return true; |
| 531 |
} |
| 532 |
|
| 533 |
bool ModuleLoader::addModuleToRegistry(JSContext* cx, JS::ModuleType moduleType, |
| 534 |
HandleString path, HandleObject module) { |
| 535 |
RootedObject registry(cx, getOrCreateModuleRegistry(cx, moduleType)); |
| 536 |
if (!registry) { |
| 537 |
return false; |
| 538 |
} |
| 539 |
|
| 540 |
RootedValue pathValue(cx, StringValue(path)); |
| 541 |
RootedValue moduleValue(cx, ObjectValue(*module)); |
| 542 |
return JS::MapSet(cx, registry, pathValue, moduleValue); |
| 543 |
} |
| 544 |
|
| 545 |
static ArrayObject* GetOrCreateRootRegistry(JSContext* cx) { |
| 546 |
Handle<GlobalObject*> global = cx->global(); |
| 547 |
RootedValue value(cx, global->getReservedSlot(GlobalAppSlotModuleRegistry)); |
| 548 |
if (!value.isUndefined()) { |
| 549 |
return &value.toObject().as<ArrayObject>(); |
| 550 |
} |
| 551 |
|
| 552 |
uint32_t numberOfModuleTypes = uint32_t(JS::ModuleType::Limit) + 1; |
| 553 |
|
| 554 |
Rooted<ArrayObject*> registry( |
| 555 |
cx, NewDenseFullyAllocatedArray(cx, numberOfModuleTypes, TenuredObject)); |
| 556 |
if (!registry) { |
| 557 |
return nullptr; |
| 558 |
} |
| 559 |
registry->ensureDenseInitializedLength(0, numberOfModuleTypes); |
| 560 |
|
| 561 |
Rooted<JSObject*> innerRegistry(cx); |
| 562 |
for (size_t i = 0; i < numberOfModuleTypes; ++i) { |
| 563 |
innerRegistry = JS::NewMapObject(cx); |
| 564 |
if (!innerRegistry) { |
| 565 |
return nullptr; |
| 566 |
} |
| 567 |
registry->initDenseElement(i, ObjectValue(*innerRegistry)); |
| 568 |
} |
| 569 |
|
| 570 |
global->setReservedSlot(GlobalAppSlotModuleRegistry, ObjectValue(*registry)); |
| 571 |
|
| 572 |
return registry; |
| 573 |
} |
| 574 |
|
| 575 |
JSObject* ModuleLoader::getOrCreateModuleRegistry(JSContext* cx, |
| 576 |
JS::ModuleType moduleType) { |
| 577 |
Rooted<ArrayObject*> rootRegistry(cx, GetOrCreateRootRegistry(cx)); |
| 578 |
if (!rootRegistry) { |
| 579 |
return nullptr; |
| 580 |
} |
| 581 |
|
| 582 |
uint32_t index = uint32_t(moduleType); |
| 583 |
MOZ_ASSERT(rootRegistry->containsDenseElement(index)); |
| 584 |
return &rootRegistry->getDenseElement(index).toObject(); |
| 585 |
} |
| 586 |
|
| 587 |
bool ModuleLoader::getScriptPath(JSContext* cx, HandleValue privateValue, |
| 588 |
MutableHandle<JSLinearString*> pathOut) { |
| 589 |
pathOut.set(nullptr); |
| 590 |
|
| 591 |
RootedObject infoObj(cx, &privateValue.toObject()); |
| 592 |
RootedValue pathValue(cx); |
| 593 |
if (!JS_GetProperty(cx, infoObj, "path", &pathValue)) { |
| 594 |
return false; |
| 595 |
} |
| 596 |
|
| 597 |
if (pathValue.isUndefined()) { |
| 598 |
return true; |
| 599 |
} |
| 600 |
|
| 601 |
RootedString path(cx, pathValue.toString()); |
| 602 |
pathOut.set(JS_EnsureLinearString(cx, path)); |
| 603 |
return pathOut; |
| 604 |
} |
| 605 |
|
| 606 |
JSLinearString* ModuleLoader::normalizePath(JSContext* cx, |
| 607 |
Handle<JSLinearString*> pathArg) { |
| 608 |
Rooted<JSLinearString*> path(cx, pathArg); |
| 609 |
|
| 610 |
if (IsJavaScriptURL(path)) { |
| 611 |
return path; |
| 612 |
} |
| 613 |
|
| 614 |
#ifdef XP_WIN |
| 615 |
// Replace all forward slashes with backward slashes. |
| 616 |
path = ReplaceCharGlobally(cx, path, u'/', PathSeparator); |
| 617 |
if (!path) { |
| 618 |
return nullptr; |
| 619 |
} |
| 620 |
|
| 621 |
// Remove the drive letter, if present. |
| 622 |
Rooted<JSLinearString*> drive(cx); |
| 623 |
if (path->length() > 2 && mozilla::IsAsciiAlpha(CharAt(path, 0)) && |
| 624 |
CharAt(path, 1) == u':' && CharAt(path, 2) == u'\\') { |
| 625 |
drive = SubString(cx, path, 0, 2); |
| 626 |
path = SubString(cx, path, 2); |
| 627 |
if (!drive || !path) { |
| 628 |
return nullptr; |
| 629 |
} |
| 630 |
} |
| 631 |
#endif // XP_WIN |
| 632 |
|
| 633 |
// Normalize the path by removing redundant path components. |
| 634 |
Rooted<GCVector<JSLinearString*>> components(cx, cx); |
| 635 |
size_t lastSep = 0; |
| 636 |
while (lastSep < path->length()) { |
| 637 |
int32_t i = IndexOf(path, PathSeparator, lastSep); |
| 638 |
if (i < 0) { |
| 639 |
i = path->length(); |
| 640 |
} |
| 641 |
|
| 642 |
Rooted<JSLinearString*> part(cx, SubString(cx, path, lastSep, i)); |
| 643 |
if (!part) { |
| 644 |
return nullptr; |
| 645 |
} |
| 646 |
|
| 647 |
lastSep = i + 1; |
| 648 |
|
| 649 |
// Remove "." when preceded by a path component. |
| 650 |
if (StringEquals(part, u".") && !components.empty()) { |
| 651 |
continue; |
| 652 |
} |
| 653 |
|
| 654 |
if (StringEquals(part, u"..") && !components.empty()) { |
| 655 |
// Replace "./.." with "..". |
| 656 |
if (StringEquals(components.back(), u".")) { |
| 657 |
components.back() = part; |
| 658 |
continue; |
| 659 |
} |
| 660 |
|
| 661 |
// When preceded by a non-empty path component, remove ".." and the |
| 662 |
// preceding component, unless the preceding component is also "..". |
| 663 |
if (!StringEquals(components.back(), u"") && |
| 664 |
!StringEquals(components.back(), u"..")) { |
| 665 |
components.popBack(); |
| 666 |
continue; |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
if (!components.append(part)) { |
| 671 |
return nullptr; |
| 672 |
} |
| 673 |
} |
| 674 |
|
| 675 |
Rooted<JSLinearString*> pathSep(cx, pathSeparatorStr); |
| 676 |
RootedString normalized(cx, JoinStrings(cx, components, pathSep)); |
| 677 |
if (!normalized) { |
| 678 |
return nullptr; |
| 679 |
} |
| 680 |
|
| 681 |
#ifdef XP_WIN |
| 682 |
if (drive) { |
| 683 |
normalized = JS_ConcatStrings(cx, drive, normalized); |
| 684 |
if (!normalized) { |
| 685 |
return nullptr; |
| 686 |
} |
| 687 |
} |
| 688 |
#endif |
| 689 |
|
| 690 |
return JS_EnsureLinearString(cx, normalized); |
| 691 |
} |
| 692 |
|
| 693 |
JSString* ModuleLoader::fetchSource(JSContext* cx, |
| 694 |
Handle<JSLinearString*> path) { |
| 695 |
if (IsJavaScriptURL(path)) { |
| 696 |
return ExtractJavaScriptURLSource(cx, path); |
| 697 |
} |
| 698 |
|
| 699 |
RootedString resolvedPath(cx, ResolvePath(cx, path, RootRelative)); |
| 700 |
if (!resolvedPath) { |
| 701 |
return nullptr; |
| 702 |
} |
| 703 |
|
| 704 |
return FileAsString(cx, resolvedPath); |
| 705 |
} |