[gjs/mozjs91: 69/87] Adapt to module API changes for Top Level Await and Import Assertions




commit 0284d462222ee7af0bca00c4b344ef5ad7bb5cb1
Author: Evan Welsh <contact evanwelsh com>
Date:   Sat Aug 7 14:22:21 2021 -0700

    Adapt to module API changes for Top Level Await and Import Assertions
    
    In anticipation of Top Level Await several changes were made to the
    dynamic imports API. First, dynamic imports now require a status of
    JS::DynamicImportStatus::Ok or ::Failed when resolving and completing
    the import. Next, JS::ModuleEvaluate now returns a value because with
    Top Level Await enabled a Promise can be returned. For now, we ignore
    the value as we aren't supporting Top Level Await.
    
    See: https://bugzilla.mozilla.org/show_bug.cgi?id=1519100
    Changeset: https://phabricator.services.mozilla.com/D95884
    
    In anticipation of Import Assertions instead of a string, the module
    hooks now receive an object containing the specifier string. This is
    so that assertions, once landed, can include additional metadata. For
    now they are just an additional step in our hook.
    
    See: https://bugzilla.mozilla.org/show_bug.cgi?id=1668330
    Changeset: https://phabricator.services.mozilla.com/D109495

 gjs/context.cpp  |  6 ++++--
 gjs/internal.cpp |  4 +++-
 gjs/module.cpp   | 41 +++++++++++++++++++++++++----------------
 gjs/module.h     |  7 ++++---
 4 files changed, 36 insertions(+), 22 deletions(-)
---
diff --git a/gjs/context.cpp b/gjs/context.cpp
index 9dfd7d92..401d3284 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -533,7 +533,8 @@ static void load_context_module(JSContext* cx, const char* uri,
         g_error("Failed to instantiate %s module.", debug_identifier);
     }
 
-    if (!JS::ModuleEvaluate(cx, loader)) {
+    JS::RootedValue ignore(cx);
+    if (!JS::ModuleEvaluate(cx, loader, &ignore)) {
         gjs_log_exception(cx);
         g_error("Failed to evaluate %s module.", debug_identifier);
     }
@@ -1335,7 +1336,8 @@ bool GjsContextPrivate::eval_module(const char* identifier,
         return false;
     }
 
-    bool ok = JS::ModuleEvaluate(m_cx, obj);
+    JS::RootedValue ignore(m_cx);
+    bool ok = JS::ModuleEvaluate(m_cx, obj, &ignore);
 
     if (ok)
         m_main_loop.spin(this);
diff --git a/gjs/internal.cpp b/gjs/internal.cpp
index a8ed70fc..9b2e552a 100644
--- a/gjs/internal.cpp
+++ b/gjs/internal.cpp
@@ -93,8 +93,10 @@ bool gjs_load_internal_module(JSContext* cx, const char* identifier) {
 
     JS::RootedId key(cx, gjs_intern_string_to_id(cx, full_path));
 
+    JS::RootedValue ignore(cx);
     if (!gjs_global_registry_set(cx, registry, key, module) ||
-        !JS::ModuleInstantiate(cx, module) || !JS::ModuleEvaluate(cx, module)) {
+        !JS::ModuleInstantiate(cx, module) ||
+        !JS::ModuleEvaluate(cx, module, &ignore)) {
         return false;
     }
 
diff --git a/gjs/module.cpp b/gjs/module.cpp
index 60a6b18d..6d5fe262 100644
--- a/gjs/module.cpp
+++ b/gjs/module.cpp
@@ -501,7 +501,7 @@ bool gjs_populate_module_meta(JSContext* cx, JS::HandleValue private_ref,
  * @returns whether an error occurred while resolving the specifier.
  */
 JSObject* gjs_module_resolve(JSContext* cx, JS::HandleValue importingModulePriv,
-                             JS::HandleString specifier) {
+                             JS::HandleObject module_request) {
     g_assert((gjs_global_is_type(cx, GjsGlobalType::DEFAULT) ||
               gjs_global_is_type(cx, GjsGlobalType::INTERNAL)) &&
              "gjs_module_resolve can only be called from module-enabled "
@@ -509,6 +509,8 @@ JSObject* gjs_module_resolve(JSContext* cx, JS::HandleValue importingModulePriv,
     g_assert(importingModulePriv.isObject() &&
              "the importing module can't be null, don't add import to the "
              "bootstrap script");
+    JS::RootedString specifier(
+        cx, JS::GetModuleRequestSpecifier(cx, module_request));
 
     JS::RootedObject global(cx, JS::CurrentGlobalOrNull(cx));
     JS::RootedValue v_loader(
@@ -537,7 +539,8 @@ JSObject* gjs_module_resolve(JSContext* cx, JS::HandleValue importingModulePriv,
 // Can fail in JS::FinishDynamicModuleImport(), but will assert if anything
 // fails in fetching the stashed values, since that would be a serious GJS bug.
 GJS_JSAPI_RETURN_CONVENTION
-static bool finish_import(JSContext* cx, const JS::CallArgs& args) {
+static bool finish_import(JSContext* cx, JS::DynamicImportStatus status,
+                          const JS::CallArgs& args) {
     GjsContextPrivate* priv = GjsContextPrivate::from_cx(cx);
     priv->main_loop_release();
 
@@ -546,23 +549,24 @@ static bool finish_import(JSContext* cx, const JS::CallArgs& args) {
     JS::RootedObject callback_data(cx, &callback_priv.toObject());
 
     JS::RootedValue importing_module_priv(cx);
-    JS::RootedValue v_specifier(cx);
+    JS::RootedValue v_module_request(cx);
     JS::RootedValue v_internal_promise(cx);
     bool ok GJS_USED_ASSERT =
         JS_GetProperty(cx, callback_data, "priv", &importing_module_priv) &&
         JS_GetProperty(cx, callback_data, "promise", &v_internal_promise) &&
-        JS_GetProperty(cx, callback_data, "specifier", &v_specifier);
+        JS_GetProperty(cx, callback_data, "module_request", &v_module_request);
     g_assert(ok && "Wrong properties on private value");
 
-    g_assert(v_specifier.isString() && "Wrong type for specifier");
+    g_assert(v_module_request.isObject() && "Wrong type for module request");
     g_assert(v_internal_promise.isObject() && "Wrong type for promise");
 
-    JS::RootedString specifier(cx, v_specifier.toString());
+    JS::RootedObject module_request(cx, &v_module_request.toObject());
     JS::RootedObject internal_promise(cx, &v_internal_promise.toObject());
 
     args.rval().setUndefined();
-    return JS::FinishDynamicModuleImport(cx, importing_module_priv, specifier,
-                                         internal_promise);
+
+    return JS::FinishDynamicModuleImport_NoTLA(
+        cx, status, importing_module_priv, module_request, internal_promise);
 }
 
 // Failing a JSAPI function may result either in an exception pending on the
@@ -572,7 +576,7 @@ static bool finish_import(JSContext* cx, const JS::CallArgs& args) {
 GJS_JSAPI_RETURN_CONVENTION
 static bool fail_import(JSContext* cx, const JS::CallArgs& args) {
     if (JS_IsExceptionPending(cx))
-        return finish_import(cx, args);
+        return finish_import(cx, JS::DynamicImportStatus::Failed, args);
     return false;
 }
 
@@ -587,7 +591,7 @@ static bool import_rejected(JSContext* cx, unsigned argc, JS::Value* vp) {
     JS_SetPendingException(cx, args.get(0),
                            JS::ExceptionStackBehavior::DoNotCapture);
 
-    return finish_import(cx, args);
+    return finish_import(cx, JS::DynamicImportStatus::Failed, args);
 }
 
 GJS_JSAPI_RETURN_CONVENTION
@@ -602,15 +606,17 @@ static bool import_resolved(JSContext* cx, unsigned argc, JS::Value* vp) {
     g_assert(args[0].isObject());
     JS::RootedObject module(cx, &args[0].toObject());
 
-    if (!JS::ModuleInstantiate(cx, module) || !JS::ModuleEvaluate(cx, module))
+    JS::RootedValue ignore(cx);
+    if (!JS::ModuleInstantiate(cx, module) ||
+        !JS::ModuleEvaluate(cx, module, &ignore))
         return fail_import(cx, args);
 
-    return finish_import(cx, args);
+    return finish_import(cx, JS::DynamicImportStatus::Ok, args);
 }
 
 bool gjs_dynamic_module_resolve(JSContext* cx,
                                 JS::HandleValue importing_module_priv,
-                                JS::HandleString specifier,
+                                JS::HandleObject module_request,
                                 JS::HandleObject internal_promise) {
     g_assert(gjs_global_is_type(cx, GjsGlobalType::DEFAULT) &&
              "gjs_dynamic_module_resolve can only be called from the default "
@@ -623,10 +629,12 @@ bool gjs_dynamic_module_resolve(JSContext* cx,
         cx, gjs_get_global_slot(global, GjsGlobalSlot::MODULE_LOADER));
     g_assert(v_loader.isObject());
     JS::RootedObject loader(cx, &v_loader.toObject());
+    JS::RootedString specifier(
+        cx, JS::GetModuleRequestSpecifier(cx, module_request));
 
     JS::RootedObject callback_data(cx, JS_NewPlainObject(cx));
     if (!callback_data ||
-        !JS_DefineProperty(cx, callback_data, "specifier", specifier,
+        !JS_DefineProperty(cx, callback_data, "module_request", module_request,
                            JSPROP_PERMANENT) ||
         !JS_DefineProperty(cx, callback_data, "promise", internal_promise,
                            JSPROP_PERMANENT) ||
@@ -653,8 +661,9 @@ bool gjs_dynamic_module_resolve(JSContext* cx,
 
     JS::RootedValue result(cx);
     if (!JS::Call(cx, loader, "moduleResolveAsyncHook", args, &result))
-        return JS::FinishDynamicModuleImport(cx, importing_module_priv,
-                                             specifier, internal_promise);
+        return JS::FinishDynamicModuleImport_NoTLA(
+            cx, JS::DynamicImportStatus::Failed, importing_module_priv,
+            module_request, internal_promise);
 
     // Release in finish_import
     GjsContextPrivate* priv = GjsContextPrivate::from_cx(cx);
diff --git a/gjs/module.h b/gjs/module.h
index 39d550b4..f6b3a353 100644
--- a/gjs/module.h
+++ b/gjs/module.h
@@ -35,8 +35,9 @@ JSObject* gjs_module_load(JSContext* cx, const char* identifier,
                           const char* uri);
 
 GJS_JSAPI_RETURN_CONVENTION
-JSObject* gjs_module_resolve(JSContext* cx, JS::HandleValue mod_val,
-                             JS::HandleString specifier);
+JSObject* gjs_module_resolve(JSContext* cx,
+                             JS::HandleValue importing_module_priv,
+                             JS::HandleObject module_request);
 
 GJS_JSAPI_RETURN_CONVENTION
 bool gjs_populate_module_meta(JSContext* cx, JS::HandleValue private_ref,
@@ -45,7 +46,7 @@ bool gjs_populate_module_meta(JSContext* cx, JS::HandleValue private_ref,
 GJS_JSAPI_RETURN_CONVENTION
 bool gjs_dynamic_module_resolve(JSContext* cx,
                                 JS::HandleValue importing_module_priv,
-                                JS::HandleString specifier,
+                                JS::HandleObject module_request,
                                 JS::HandleObject internal_promise);
 
 #endif  // GJS_MODULE_H_


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]