Skip to content

Commit cebdad8

Browse files
committed
[JSC] Implement import-assertion and JSON module
https://bugs.webkit.org/show_bug.cgi?id=243622 Reviewed by Alexey Shvayka and Ryosuke Niwa. This patch implements import-assertions and JSON module. 1. Import assertions' syntax work was already done. We propagate assertions through ScriptFetchParameters, which already exists. 2. Get "type" assertion, and based on that, we parse JSON if it is "json". 3. Add SyntheticModuleRecord (this is described in the spec), and materialize JSON module as SyntheticModuleRecord. 4. Fix MIMETypeRegistry::isSupportedJSONMIMEType. "text/json" is explicitly listed in [1], so we should say true for that. 5. Dynamic import also gets assertions via its second parameter. One test262 test is failing just because our harness is not handling it well (so this is not a bug in JSON module implementation). WPT failures are due to old module loader's behavior (so, fixing it requires update of module loader implementation, not related to JSON module itself). [1]: https://mimesniff.spec.whatwg.org/#json-mime-type * JSTests/test262/config.yaml: * JSTests/test262/expectations.yaml: * LayoutTests/TestExpectations: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/charset-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/content-type-checking-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/cors-crossorigin-requests-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/css-module-worker-test-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/import-css-module-basic-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/integrity-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/load-error-events-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/import-assertions/empty-assertion-clause-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/import-assertions/invalid-type-assertion-error-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/import-assertions/unsupported-assertion-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/json-module/charset-bom.any-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/json-module/charset-bom.any.sharedworker-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/json-module/charset-bom.any.worker-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/json-module/charset-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/json-module/cors-crossorigin-requests-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/json-module/integrity-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/json-module/json-module-service-worker-test.https-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/json-module/load-error-events-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/json-module/module-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/json-module/non-object.any-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/json-module/non-object.any.sharedworker-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/json-module/non-object.any.worker-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/json-module/repeated-imports.any-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/json-module/repeated-imports.any.sharedworker-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/json-module/repeated-imports.any.worker-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/json-module/valid-content-type-expected.txt: * LayoutTests/platform/gtk/TestExpectations: * Source/JavaScriptCore/API/JSAPIGlobalObject.mm: (JSC::JSAPIGlobalObject::moduleLoaderImportModule): * Source/JavaScriptCore/CMakeLists.txt: * Source/JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj: * Source/JavaScriptCore/Sources.txt: * Source/JavaScriptCore/builtins/ModuleLoader.js: (requestSatisfy): * Source/JavaScriptCore/heap/Heap.cpp: (JSC::Heap::Heap): * Source/JavaScriptCore/heap/Heap.h: * Source/JavaScriptCore/heap/HeapSubspaceTypes.h: * Source/JavaScriptCore/jsc.cpp: (GlobalObject::moduleLoaderImportModule): (GlobalObject::moduleLoaderFetch): * Source/JavaScriptCore/parser/ModuleAnalyzer.cpp: (JSC::ModuleAnalyzer::appendRequestedModule): * Source/JavaScriptCore/parser/ModuleAnalyzer.h: * Source/JavaScriptCore/parser/NodesAnalyzeModule.cpp: (JSC::tryCreateAssertion): (JSC::ImportDeclarationNode::analyzeModule): (JSC::ExportAllDeclarationNode::analyzeModule): (JSC::ExportNamedDeclarationNode::analyzeModule): * Source/JavaScriptCore/parser/Parser.cpp: (JSC::Parser<LexerType>::parseImportAssertions): * Source/JavaScriptCore/parser/SourceProvider.h: * Source/JavaScriptCore/runtime/AbstractModuleRecord.cpp: (JSC::AbstractModuleRecord::appendRequestedModule): (JSC::AbstractModuleRecord::link): (JSC::AbstractModuleRecord::evaluate): (JSC::AbstractModuleRecord::dump): * Source/JavaScriptCore/runtime/AbstractModuleRecord.h: (JSC::AbstractModuleRecord::requestedModules const): * Source/JavaScriptCore/runtime/Completion.cpp: (JSC::retrieveAssertionsFromDynamicImportOptions): * Source/JavaScriptCore/runtime/Completion.h: * Source/JavaScriptCore/runtime/JSGlobalObject.cpp: (JSC::JSGlobalObject::init): (JSC::JSGlobalObject::visitChildrenImpl): * Source/JavaScriptCore/runtime/JSGlobalObject.h: (JSC::JSGlobalObject::syntheticModuleRecordStructure const): * Source/JavaScriptCore/runtime/JSGlobalObjectFunctions.cpp: (JSC::JSC_DEFINE_HOST_FUNCTION): * Source/JavaScriptCore/runtime/JSModuleLoader.cpp: (JSC::JSC_DEFINE_HOST_FUNCTION): * Source/JavaScriptCore/runtime/JSONObject.cpp: (JSC::JSONParse): (JSC::JSONParseWithException): * Source/JavaScriptCore/runtime/JSONObject.h: * Source/JavaScriptCore/runtime/OptionsList.h: * Source/JavaScriptCore/runtime/ScriptFetchParameters.h: (JSC::ScriptFetchParameters::ScriptFetchParameters): (JSC::ScriptFetchParameters::type const): (JSC::ScriptFetchParameters::integrity const): (JSC::ScriptFetchParameters::isTopLevelModule const): (JSC::ScriptFetchParameters::create): (JSC::ScriptFetchParameters::~ScriptFetchParameters): Deleted. * Source/JavaScriptCore/runtime/SyntheticModuleRecord.cpp: Added. (JSC::SyntheticModuleRecord::createStructure): (JSC::SyntheticModuleRecord::create): (JSC::SyntheticModuleRecord::SyntheticModuleRecord): (JSC::SyntheticModuleRecord::destroy): (JSC::SyntheticModuleRecord::finishCreation): (JSC::SyntheticModuleRecord::visitChildrenImpl): (JSC::SyntheticModuleRecord::link): (JSC::SyntheticModuleRecord::evaluate): (JSC::SyntheticModuleRecord::tryCreateWithExportNamesAndValues): (JSC::SyntheticModuleRecord::tryCreateDefaultExportSyntheticModule): (JSC::SyntheticModuleRecord::parseJSONModule): * Source/JavaScriptCore/runtime/SyntheticModuleRecord.h: Added. * Source/JavaScriptCore/wasm/js/JSWebAssemblyInstance.cpp: (JSC::JSWebAssemblyInstance::tryCreate): * Source/WebCore/bindings/js/CachedModuleScriptLoader.cpp: (WebCore::CachedModuleScriptLoader::create): (WebCore::CachedModuleScriptLoader::CachedModuleScriptLoader): * Source/WebCore/bindings/js/CachedModuleScriptLoader.h: * Source/WebCore/bindings/js/ModuleScriptLoader.h: (WebCore::ModuleScriptLoader::parameters): (WebCore::ModuleScriptLoader::ModuleScriptLoader): * Source/WebCore/bindings/js/ScriptController.cpp: (WebCore::ScriptController::loadModuleScriptInWorld): (WebCore::ScriptController::loadModuleScript): (WebCore::ScriptController::evaluateModule): * Source/WebCore/bindings/js/ScriptController.h: * Source/WebCore/bindings/js/ScriptModuleLoader.cpp: (WebCore::ScriptModuleLoader::fetch): (WebCore::ScriptModuleLoader::importModule): (WebCore::ScriptModuleLoader::notifyFinished): * Source/WebCore/bindings/js/ScriptModuleLoader.h: * Source/WebCore/bindings/js/WorkerModuleScriptLoader.cpp: (WebCore::WorkerModuleScriptLoader::create): (WebCore::WorkerModuleScriptLoader::WorkerModuleScriptLoader): * Source/WebCore/bindings/js/WorkerModuleScriptLoader.h: * Source/WebCore/bindings/js/WorkerScriptFetcher.h: * Source/WebCore/dom/LoadableModuleScript.cpp: (WebCore::LoadableModuleScript::LoadableModuleScript): * Source/WebCore/dom/ModuleFetchParameters.h: (WebCore::ModuleFetchParameters::create): (WebCore::ModuleFetchParameters::ModuleFetchParameters): (WebCore::ModuleFetchParameters::integrity const): Deleted. (WebCore::ModuleFetchParameters::isTopLevelModule const): Deleted. * Source/WebCore/platform/MIMETypeRegistry.cpp: (WebCore::MIMETypeRegistry::isSupportedJSONMIMEType): * Source/WebCore/workers/WorkerOrWorkletScriptController.cpp: (WebCore::WorkerOrWorkletScriptController::loadAndEvaluateModule): * Source/WebCore/workers/WorkerThread.cpp: (WebCore::WorkerThread::evaluateScriptIfNecessary): * Tools/TestWebKitAPI/Tests/WebCore/MIMETypeRegistry.cpp: (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/253234@main
1 parent 6f10652 commit cebdad8

75 files changed

Lines changed: 767 additions & 274 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎JSTests/test262/config.yaml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ flags:
1111
array-grouping: useArrayGroupMethod
1212
Object.hasOwn: useHasOwn
1313
ShadowRealm: useShadowRealm
14+
import-assertions: useImportAssertion
15+
json-modules: useImportAssertion
1416
skip:
1517
features:
1618
- Atomics.waitAsync
1719
# https://bugs.webkit.org/show_bug.cgi?id=174931
1820
- regexp-lookbehind
1921
- regexp-v-flag
2022
- resizable-arraybuffer
21-
- import-assertions
22-
- json-modules
2323
- class-static-block
2424
- callable-boundary-realms
2525
- FinalizationRegistry.prototype.cleanupSome

‎JSTests/test262/expectations.yaml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2040,6 +2040,8 @@ test/language/module-code/early-dup-top-function-generator.js:
20402040
module: 'Test262: This statement should not be evaluated.'
20412041
test/language/module-code/early-dup-top-function.js:
20422042
module: 'Test262: This statement should not be evaluated.'
2043+
test/language/module-code/eval-gtbndng-indirect-faux-assertion.js:
2044+
raw: "SyntaxError: Unexpected identifier 'x'. import call expects one or two arguments."
20432045
test/language/module-code/parse-err-hoist-lex-fun.js:
20442046
module: 'Test262: This statement should not be evaluated.'
20452047
test/language/module-code/parse-err-hoist-lex-gen.js:

‎LayoutTests/TestExpectations‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5440,3 +5440,8 @@ streams/readable-byte-stream-controller-worker.html [ Skip ]
54405440
webkit.org/b/241104 fast/block/fill-available-with-no-specified-containing-block-height.html [ ImageOnlyFailure ]
54415441
webkit.org/b/241104 fast/block/fill-available-with-absolute-position.html [ ImageOnlyFailure ]
54425442
webkit.org/b/241104 fast/css/fill-available-with-percent-height-no-quirk.html [ ImageOnlyFailure ]
5443+
5444+
# Flaky because of network ordering
5445+
imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/charset.html [ Pass Failure ]
5446+
imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/cors-crossorigin-requests.html [ Pass Failure ]
5447+
imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/json-module/cors-crossorigin-requests.html [ Pass Failure ]
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
CONSOLE MESSAGE: SyntaxError: Unexpected identifier 'assert'. Expected a ';' following a targeted import declaration.
2-
CONSOLE MESSAGE: SyntaxError: Unexpected identifier 'assert'. Expected a ';' following a targeted import declaration.
3-
CONSOLE MESSAGE: SyntaxError: Unexpected identifier 'assert'. Expected a ';' following a targeted import declaration.
4-
CONSOLE MESSAGE: SyntaxError: Unexpected identifier 'assert'. Expected a ';' following a targeted import declaration.
5-
CONSOLE MESSAGE: SyntaxError: Unexpected identifier 'assert'. Expected a ';' following a targeted import declaration.
1+
CONSOLE MESSAGE: TypeError: 'text/css' is not a valid JavaScript MIME type.
2+
CONSOLE MESSAGE: ReferenceError: Can't find variable: unreachable
3+
CONSOLE MESSAGE: TypeError: 'text/css' is not a valid JavaScript MIME type.
4+
CONSOLE MESSAGE: ReferenceError: Can't find variable: unreachable
65

7-
Harness Error (FAIL), message = SyntaxError: Unexpected identifier 'assert'. Expected a ';' following a targeted import declaration.
6+
Harness Error (FAIL), message = ReferenceError: Can't find variable: unreachable
87

98

Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
CONSOLE MESSAGE: SyntaxError: Unexpected identifier 'assert'. Expected a ';' following a targeted import declaration.
2-
CONSOLE MESSAGE: SyntaxError: Unexpected identifier 'assert'. Expected a ';' following a targeted import declaration.
3-
CONSOLE MESSAGE: SyntaxError: Unexpected identifier 'assert'. Expected a ';' following a targeted import declaration.
4-
CONSOLE MESSAGE: SyntaxError: Unexpected identifier 'assert'. Expected a ';' following a targeted import declaration.
5-
CONSOLE MESSAGE: SyntaxError: Unexpected identifier 'assert'. Expected a ';' following a targeted import declaration.
1+
CONSOLE MESSAGE: TypeError: 'text/css' is not a valid JavaScript MIME type.
2+
CONSOLE MESSAGE: TypeError: 'application/css' is not a valid JavaScript MIME type.
3+
CONSOLE MESSAGE: TypeError: 'text/html css' is not a valid JavaScript MIME type.
4+
CONSOLE MESSAGE: TypeError: 'text/css' is not a valid JavaScript MIME type.
5+
CONSOLE MESSAGE: TypeError: 'text/css' is not a valid JavaScript MIME type.
66

7-
Harness Error (FAIL), message = SyntaxError: Unexpected identifier 'assert'. Expected a ';' following a targeted import declaration.
8-
9-
NOTRUN text/css
10-
NOTRUN application/css
11-
NOTRUN text/html+css
12-
NOTRUN text/css;boundary=something
13-
NOTRUN text/css;foo=bar
7+
FAIL text/css assert_unreached: Reached unreachable code
8+
PASS application/css
9+
PASS text/html+css
10+
FAIL text/css;boundary=something assert_unreached: Reached unreachable code
11+
FAIL text/css;foo=bar assert_unreached: Reached unreachable code
1412

Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
CONSOLE MESSAGE: SyntaxError: Unexpected identifier 'assert'. Expected a ';' following a targeted import declaration.
2-
CONSOLE MESSAGE: SyntaxError: Unexpected identifier 'assert'. Expected a ';' following a targeted import declaration.
3-
CONSOLE MESSAGE: SyntaxError: Unexpected identifier 'assert'. Expected a ';' following a targeted import declaration.
1+
Blocked access to external URL http://www2.localhost:8800/html/semantics/scripting-1/the-script-element/css-module/resources/basic.css?pipe=header(Access-Control-Allow-Origin,*)
2+
Blocked access to external URL http://www2.localhost:8800/html/semantics/scripting-1/the-script-element/css-module/resources/parse-error.css?pipe=header(Access-Control-Allow-Origin,*)
3+
Blocked access to external URL http://www2.localhost:8800/html/semantics/scripting-1/the-script-element/css-module/resources/basic.css
44
css-module-crossorigin
55

66

77

8-
FAIL Imported CSS module, cross-origin with CORS assert_equals: Unexpected _log value expected "imported CSS: #test { background-color: rgb(255, 0, 0); }" but got "10-0"
9-
FAIL Imported CSS module, cross-origin, missing CORS ACAO header assert_equals: Unexpected _log value expected "error" but got "10-0"
10-
FAIL Imported CSS module with parse error, cross-origin, with CORS assert_equals: Unexpected _log value expected "imported CSS rules count: 0" but got "10-0"
8+
FAIL Imported CSS module, cross-origin with CORS assert_equals: Unexpected _log value expected "imported CSS: #test { background-color: rgb(255, 0, 0); }" but got ""
9+
PASS Imported CSS module, cross-origin, missing CORS ACAO header
10+
FAIL Imported CSS module with parse error, cross-origin, with CORS assert_equals: Unexpected _log value expected "imported CSS rules count: 0" but got ""
1111

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
PASS A static import CSS Module within a web worker should not load and should not attempt to fetch the module.
2+
FAIL A static import CSS Module within a web worker should not load and should not attempt to fetch the module. assert_equals: Shouldn't have tried fetching CSS module in worker expected 0 but got 1
33
FAIL A dynamic import CSS Module within a web worker should not load and should not attempt to fetch the module. assert_equals: Shouldn't have tried fetching CSS module in worker expected 0 but got 1
44
PASS An attempt to load a CSS module as a worker should fail.
55

‎LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/import-css-module-basic-expected.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ I am a test div.
55
I am a test div.
66
I am a test div.
77

8-
Harness Error (FAIL), message = SyntaxError: Unexpected identifier 'assert'. Expected a ';' following a targeted import declaration.
8+
Harness Error (FAIL), message = ReferenceError: Can't find variable: unreachable
99

1010

Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

2-
Harness Error (FAIL), message = SyntaxError: Unexpected identifier 'assert'. Expected a ';' following a targeted import declaration.
3-
2+
FAIL The integrity attribute must be verified on the top-level of a module loading a CSS module and allow it to execute when it matches assert_array_equals: The module and its dependency must have executed lengths differ, expected array ["integrity-matches,css:#test { background-color: rgb(255, 0, 0); }"] length 1, got [] length 0
3+
PASS The integrity attribute must be verified on the top-level of a module loading a CSS module and not allow it to execute when there's a mismatch
44

Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
1-
CONSOLE MESSAGE: SyntaxError: Unexpected identifier 'assert'. Expected a ';' following a targeted import declaration.
2-
CONSOLE MESSAGE: SyntaxError: Unexpected identifier 'assert'. Expected a ';' following a targeted import declaration.
3-
CONSOLE MESSAGE: SyntaxError: Unexpected identifier 'assert'. Expected a ';' following a targeted import declaration.
4-
CONSOLE MESSAGE: SyntaxError: Unexpected identifier 'assert'. Expected a ';' following a targeted import declaration.
5-
CONSOLE MESSAGE: SyntaxError: Unexpected identifier 'assert'. Expected a ';' following a targeted import declaration.
6-
CONSOLE MESSAGE: SyntaxError: Unexpected identifier 'assert'. Expected a ';' following a targeted import declaration.
7-
CONSOLE MESSAGE: SyntaxError: Unexpected identifier 'assert'. Expected a ';' following a targeted import declaration.
8-
CONSOLE MESSAGE: SyntaxError: Unexpected identifier 'assert'. Expected a ';' following a targeted import declaration.
1+
CONSOLE MESSAGE: TypeError: 'text/css' is not a valid JavaScript MIME type.
2+
CONSOLE MESSAGE: TypeError: 'text/css' is not a valid JavaScript MIME type.
3+
CONSOLE MESSAGE: TypeError: 'text/css' is not a valid JavaScript MIME type.
4+
CONSOLE MESSAGE: TypeError: 'text/css' is not a valid JavaScript MIME type.
95

10-
Harness Error (FAIL), message = SyntaxError: Unexpected identifier 'assert'. Expected a ';' following a targeted import declaration.
11-
12-
NOTRUN inline, 200, parser-inserted
13-
NOTRUN inline, 404, parser-inserted
14-
NOTRUN src, 200, parser-inserted
15-
NOTRUN src, 404, parser-inserted
16-
NOTRUN src, 200, not parser-inserted
17-
NOTRUN src, 404, not parser-inserted
18-
NOTRUN inline, 200, not parser-inserted
19-
NOTRUN inline, 404, not parser-inserted
6+
FAIL inline, 200, parser-inserted assert_unreached: Error event should not be fired. Reached unreachable code
7+
PASS inline, 404, parser-inserted
8+
FAIL src, 200, parser-inserted assert_unreached: Error event should not be fired. Reached unreachable code
9+
PASS src, 404, parser-inserted
10+
FAIL src, 200, not parser-inserted assert_unreached: Error event should not be fired. Reached unreachable code
11+
PASS src, 404, not parser-inserted
12+
FAIL inline, 200, not parser-inserted assert_unreached: Error event should not be fired. Reached unreachable code
13+
PASS inline, 404, not parser-inserted
2014

0 commit comments

Comments
 (0)