commit 30962accea32a45169ce87100b19a53628f9153f
parent 998d38948a9f6f1357e7b5ed5250c11358fa16b0
Author: Cornelius Emase <corneliuslochipi@gmail.com>
Date: Thu, 6 Nov 2025 20:27:26 +0000
Bug 1998012 - Refactor synthetic module creation to shared CreateDefaultExportSyntheticModule. r=dminor,allstarschh
Rename CreateCSSModule to CreateDefaultExportSyntheticModule and remove the
unused ReadOnlyCompileOptions parameter. Update the call in ModuleLoader.cpp
to use the new function signature, and replace the duplicate code in
CompileJsonModule.
Differential Revision: https://phabricator.services.mozilla.com/D271453
Diffstat:
3 files changed, 13 insertions(+), 35 deletions(-)
diff --git a/dom/script/ModuleLoader.cpp b/dom/script/ModuleLoader.cpp
@@ -432,7 +432,7 @@ nsresult ModuleLoader::CompileCssModule(
}
// Steps. 1 - 4 (re-ordered), 7, 8
- cssModule.set(JS::CreateCssModule(aCx, aOptions, val));
+ cssModule.set(JS::CreateDefaultExportSyntheticModule(aCx, val));
};
maybeSource.mapNonEmpty(compile);
diff --git a/js/public/Modules.h b/js/public/Modules.h
@@ -197,13 +197,15 @@ extern JS_PUBLIC_API JSObject* CompileJsonModule(
SourceText<mozilla::Utf8Unit>& srcBuf);
/**
- * Create a synthetic module record for a CSS module from the provided
- * CSSStyleSheet in cssValue. There's no capability to parse CSS in
- * the engine, so this must occur prior to calling this function.
+ * Create a synthetic module record that exports a single value as its default
+ * export. The caller is responsible for providing the already-constructed
+ * value to export.
+ *
+ * This matches the ECMAScript specification's definition:
+ * https://tc39.es/ecma262/#sec-create-default-export-synthetic-module
*/
-extern JS_PUBLIC_API JSObject* CreateCssModule(
- JSContext* cx, const ReadOnlyCompileOptions& options,
- const Value& cssValue);
+extern JS_PUBLIC_API JSObject* CreateDefaultExportSyntheticModule(
+ JSContext* cx, const Value& defaultExport);
/**
* Set a private value associated with a source text module record.
diff --git a/js/src/vm/Modules.cpp b/js/src/vm/Modules.cpp
@@ -268,35 +268,11 @@ JS_PUBLIC_API JSObject* JS::CompileJsonModule(
return nullptr;
}
- Rooted<ExportNameVector> exportNames(cx);
- if (!exportNames.append(cx->names().default_)) {
- ReportOutOfMemory(cx);
- return nullptr;
- }
-
- Rooted<ModuleObject*> moduleObject(
- cx, ModuleObject::createSynthetic(cx, &exportNames));
- if (!moduleObject) {
- return nullptr;
- }
-
- RootedVector<Value> exportValues(cx);
- if (!exportValues.append(jsonValue)) {
- ReportOutOfMemory(cx);
- return nullptr;
- }
-
- if (!ModuleObject::createSyntheticEnvironment(cx, moduleObject,
- exportValues)) {
- return nullptr;
- }
-
- return moduleObject;
+ return CreateDefaultExportSyntheticModule(cx, jsonValue);
}
-JS_PUBLIC_API JSObject* JS::CreateCssModule(
- JSContext* cx, const ReadOnlyCompileOptions& options,
- const Value& cssValue) {
+JS_PUBLIC_API JSObject* JS::CreateDefaultExportSyntheticModule(
+ JSContext* cx, const Value& defaultExport) {
Rooted<ExportNameVector> exportNames(cx);
if (!exportNames.append(cx->names().default_)) {
ReportOutOfMemory(cx);
@@ -310,7 +286,7 @@ JS_PUBLIC_API JSObject* JS::CreateCssModule(
}
RootedVector<Value> exportValues(cx);
- if (!exportValues.append(cssValue)) {
+ if (!exportValues.append(defaultExport)) {
ReportOutOfMemory(cx);
return nullptr;
}