44#include < cstdio>
55#include < js/CompilationAndEvaluation.h>
66#include < js/MapAndSet.h>
7+ #include < js/ScriptPrivate.h>
78#include < js/Value.h>
89#include < jsfriendapi.h>
910#include < sys/stat.h>
@@ -310,36 +311,54 @@ static JSObject* get_builtin_module(JSContext* cx, HandleValue id, HandleObject
310311 return module ;
311312}
312313
313- JSObject* module_resolve_hook (JSContext* cx, HandleValue referencingPrivate,
314- HandleObject moduleRequest) {
314+ bool module_load_hook (JSContext* cx, JS::HandleScript referrer,
315+ JS::HandleObject moduleRequest, JS::HandleValue hostDefined,
316+ JS::HandleValue payload, uint32_t lineNumber,
317+ JS::ColumnNumberOneOrigin columnNumber) {
315318 RootedString specifier (cx, GetModuleRequestSpecifier (cx, moduleRequest));
316319 if (!specifier) {
317- return nullptr ;
320+ return false ;
318321 }
319322
320323 RootedValue path_val (cx, StringValue (specifier));
321324 auto path = JS_EncodeStringToUTF8 (cx, specifier);
322325 if (!path) {
323- return nullptr ;
326+ return false ;
324327 }
325328
326329 RootedValue builtin_val (cx);
327330 if (!MapGet (cx, builtinModules, path_val, &builtin_val)) {
328- return nullptr ;
331+ return false ;
329332 }
330333 if (!builtin_val.isUndefined ()) {
331334 RootedValue specifier_val (cx, StringValue (specifier));
332335 RootedObject builtin_obj (cx, &builtin_val.toObject ());
333- return get_builtin_module (cx, specifier_val, builtin_obj);
336+ RootedObject result (cx, get_builtin_module (cx, specifier_val, builtin_obj));
337+ if (!result) {
338+ return false ;
339+ }
340+ return FinishLoadingImportedModule (cx, referrer, moduleRequest, payload,
341+ result, false );
342+ }
343+
344+ RootedValue referencingPrivate (cx);
345+ if (referrer) {
346+ referencingPrivate = JS::GetScriptPrivate (referrer);
347+ }
348+
349+ if (referencingPrivate.isUndefined () || !referencingPrivate.isObject ()) {
350+ JS_ReportErrorASCII (cx, " Module referrer has no private value" );
351+ return false ;
334352 }
335353
336354 RootedObject info (cx, &referencingPrivate.toObject ());
337355 RootedValue parent_path_val (cx);
338356 if (!JS_GetProperty (cx, info, " id" , &parent_path_val)) {
339- return nullptr ;
357+ return false ;
340358 }
341359 if (!parent_path_val.isString ()) {
342- return nullptr ;
360+ JS_ReportErrorASCII (cx, " Module referrer has no id property" );
361+ return false ;
343362 }
344363
345364 HostString str = core::encode (cx, parent_path_val);
@@ -348,7 +367,12 @@ JSObject* module_resolve_hook(JSContext* cx, HandleValue referencingPrivate,
348367 JS::CompileOptions opts (cx, *COMPILE_OPTS);
349368 auto stripped = strip_prefix (resolved_path, PATH_PREFIX);
350369 opts.setFileAndLine (stripped.c_str (), 1 );
351- return get_module (cx, path.get (), resolved_path, opts);
370+ RootedObject result (cx, get_module (cx, path.get (), resolved_path, opts));
371+ if (!result) {
372+ return false ;
373+ }
374+ return FinishLoadingImportedModule (cx, referrer, moduleRequest, payload,
375+ result, false );
352376}
353377
354378bool module_metadata_hook (JSContext* cx, HandleValue referencingPrivate, HandleObject metaObject) {
@@ -384,7 +408,7 @@ ScriptLoader::ScriptLoader(api::Engine* engine, JS::CompileOptions *opts,
384408 MOZ_RELEASE_ASSERT (moduleRegistry);
385409 MOZ_RELEASE_ASSERT (builtinModules);
386410 JSRuntime *rt = JS_GetRuntime (cx);
387- SetModuleResolveHook (rt, module_resolve_hook );
411+ SetModuleLoadHook (rt, module_load_hook );
388412 SetModuleMetadataHook (rt, module_metadata_hook);
389413}
390414
@@ -491,7 +515,20 @@ bool ScriptLoader::eval_top_level_script(std::string_view path, JS::SourceText<m
491515 if (!module ) {
492516 return false ;
493517 }
494- if (!ModuleLink (cx, module )) {
518+ RootedValue hostDefined (cx, ObjectValue (*module ));
519+ if (!LoadRequestedModules (cx, module , hostDefined,
520+ [](JSContext* cx, JS::Handle<JS::Value> hd) {
521+ JS::RootedObject mod (cx, &hd.toObject ());
522+ return JS::ModuleLink (cx, mod);
523+ },
524+ [](JSContext* cx, JS::Handle<JS::Value>,
525+ JS::Handle<JS::Value> error) {
526+ JS_SetPendingException (cx, error);
527+ return true ;
528+ })) {
529+ return false ;
530+ }
531+ if (JS_IsExceptionPending (cx)) {
495532 return false ;
496533 }
497534 } else {
0 commit comments