Skip to content

Commit 3f20b1d

Browse files
committed
Bug 1720570 - Add CreateCssModule; r=allstarschh
This adds a method to create a css module to the modules interface. Unlike JavaScript or JSON modules, we don't have an ability to parse CSS inside SpiderMonkey, so this method accepts an already parsed CSSStyleSheet value from the caller, and wraps it in a module. Differential Revision: https://phabricator.services.mozilla.com/D263410
1 parent ef5e3a7 commit 3f20b1d

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

js/public/Modules.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,15 @@ extern JS_PUBLIC_API JSObject* CompileJsonModule(
194194
JSContext* cx, const ReadOnlyCompileOptions& options,
195195
SourceText<mozilla::Utf8Unit>& srcBuf);
196196

197+
/**
198+
* Create a synthetic module record for a CSS module from the provided
199+
* CSSStyleSheet in cssValue. There's no capability to parse CSS in
200+
* the engine, so this must occur prior to calling this function.
201+
*/
202+
extern JS_PUBLIC_API JSObject* CreateCssModule(
203+
JSContext* cx, const ReadOnlyCompileOptions& options,
204+
const Value& cssValue);
205+
197206
/**
198207
* Set a private value associated with a source text module record.
199208
*/

js/src/vm/Modules.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,36 @@ JS_PUBLIC_API JSObject* JS::CompileJsonModule(
294294
return moduleObject;
295295
}
296296

297+
JS_PUBLIC_API JSObject* JS::CreateCssModule(
298+
JSContext* cx, const ReadOnlyCompileOptions& options,
299+
const Value& cssValue) {
300+
301+
Rooted<ExportNameVector> exportNames(cx);
302+
if (!exportNames.append(cx->names().default_)) {
303+
ReportOutOfMemory(cx);
304+
return nullptr;
305+
}
306+
307+
Rooted<ModuleObject*> moduleObject(
308+
cx, ModuleObject::createSynthetic(cx, &exportNames));
309+
if (!moduleObject) {
310+
return nullptr;
311+
}
312+
313+
RootedVector<Value> exportValues(cx);
314+
if (!exportValues.append(cssValue)) {
315+
ReportOutOfMemory(cx);
316+
return nullptr;
317+
}
318+
319+
if (!ModuleObject::createSyntheticEnvironment(cx, moduleObject,
320+
exportValues)) {
321+
return nullptr;
322+
}
323+
324+
return moduleObject;
325+
}
326+
297327
JS_PUBLIC_API void JS::SetModulePrivate(JSObject* module, const Value& value) {
298328
JSRuntime* rt = module->zone()->runtimeFromMainThread();
299329
module->as<ModuleObject>().scriptSourceObject()->setPrivate(rt, value);

0 commit comments

Comments
 (0)