Repository navigation
Implement CSS module scripts - #72110
tuankiet65 wants to merge 1 commit into
Conversation
|
EWS run on previous version of this PR (hash d4027ef) Details |
| .disabled = false | ||
| }; | ||
| auto sheetOrException= CSSStyleSheet::create(*document, WTF::move(sheetInit)); | ||
| RETURN_IF_EXCEPTION(throwScope, jsUndefined()); |
There was a problem hiding this comment.
Can CSSStyleSheet::create() really throw a JS exception? I doubt it.
| auto sheet = sheetOrException.releaseReturnValue(); | ||
| // FIXME: exception here should bubble up to script's parse error | ||
| auto maybeException = sheet->replaceSync(source.toStringWithoutCopying()); | ||
| RETURN_IF_EXCEPTION(throwScope, jsUndefined()); |
There was a problem hiding this comment.
can replaceSync() really throw a JS exception? I doubt it.
There was a problem hiding this comment.
However, I suspect ignoring the exception potentially returned by replaceSync() and stored in maybeException is likely not good.
| @@ -108,7 +108,10 @@ void WorkerModuleScriptLoader::load(ScriptExecutionContext& context, URL&& sourc | |||
| } | |||
| } | |||
|
|
|||
| if (cspCheckFailed) { | |||
| // Workers and worklets are forbid from fetching CSS modules. | |||
| @@ -79,6 +79,7 @@ const GlobalObjectMethodTable* JSWorkerGlobalScopeBase::globalObjectMethodTable( | |||
| codeForEval, | |||
| canCompileStrings, | |||
| trustedScriptStructure, | |||
| nullptr // createCSSStyleSheetFromSourceScript, not available in Worker. | |||
There was a problem hiding this comment.
did you mean createCSSStyleSheetFromSource ?
| @@ -71,6 +71,7 @@ const GlobalObjectMethodTable* JSWorkletGlobalScopeBase::globalObjectMethodTable | |||
| codeForEval, | |||
| canCompileStrings, | |||
| trustedScriptStructure, | |||
| nullptr // createCSSStyleSheetFromSourceScript, not available in Worklet. | |||
There was a problem hiding this comment.
Did you mean createCSSStyleSheetFromSource ?
| @@ -97,6 +98,7 @@ class SourceProvider : public ThreadSafeRefCounted<SourceProvider> { | |||
| case SourceProviderSourceType::Module: | |||
| case SourceProviderSourceType::JSON: | |||
| case SourceProviderSourceType::Text: | |||
| case SourceProviderSourceType::CSS: | |||
There was a problem hiding this comment.
Curious what JSC people such as @Constellation think about using a WebCore concept (CSS) in JSC code. In the past, they have objected to things like this. However, this may be OK / unavoidable here. Just highlighting it for their attention.
There was a problem hiding this comment.
It seems like an “external” type that delegates to WebCore should be achievable.
There was a problem hiding this comment.
The module loader needs this type information as its key in the map is (URL, type) according to the spec. Thus I think it is fine to have them. If we are not in a situation where importing CSS implementation into JSC, I think this is fine 😉
| @@ -87,6 +87,7 @@ struct GlobalObjectMethodTable { | |||
| String (*codeForEval)(JSGlobalObject*, JSValue); | |||
| bool (*canCompileStrings)(JSGlobalObject*, CompilationType, String, const ArgList&); | |||
| Structure* (*trustedScriptStructure)(JSGlobalObject*); | |||
| JSValue (*createCSSStyleSheetFromSource)(JSGlobalObject*, StringView sourceString); | |||
There was a problem hiding this comment.
Assuming JSC reviewers object to having a WebCore concept here (please follow their advice) here are a couple of alternative proposals:
- Minimal
Generalize the hook and let WebCore switch on the type:
// GlobalObjectMethodTable.h
JSValue (*createSyntheticModuleDefaultExport)(JSGlobalObject*, ScriptFetchParameters::Type, StringView source);
SyntheticModuleRecord::createCSSStyleSheetFromSource becomes createHostDefinedModule(JSGlobalObject*, const Identifier&, SourceCode&&, ScriptFetchParameters::Type), with the CSS comments and the html-spec link moving to JSDOMWindowBase. JSC's null-hook error message becomes generic ("module type is not supported in this environment"). WebCore-side cost: one extra parameter and a switch; the WebCore function keeps its current name, which is fine there.
- Larger but cleaner I think?
Let the host build the whole record (I think this is what Blink does with v8::Module::CreateSyntheticModule):
AbstractModuleRecord* (*createHostDefinedModule)(JSGlobalObject*, const Identifier& moduleKey, const SourceCode&, ScriptFetchParameters::Type);
makeModule gets one delegating case, and SyntheticModuleRecord.cpp needs no change at all.
Cost: tryCreateDefaultExportSyntheticModule is currently private and unexported, so it'd need to become public + JS_EXPORT_PRIVATE.
There was a problem hiding this comment.
I just pushed a patch that does something different.
The current way is that JSC requests a fetch from WebCore. WebCore fetches then returns the source code to JSC. JSC then parses the returned source code. This works for JS/WASM/JSON/text because those must be handled by JSC, but for CSS modules, it can be handled by WebCore.
I changed it so that when JSC requests a fetch from WebCore, depending on the requested module type, WebCore can either return a source code (for JS/WASM/JSON/text), or a JSValue. Returning a JSValue means WebCore knows how to turn the module into a JSValue, and JSC should just use that as the default module export. In this patch, when the requested module type is CSS, then WebCore turns it in JSCSSStyleSheet before giving it to JSC.
This offers some benefits over the previous approach:
- JSC knows less about the CSS modules. All it knows is that CSS modules exist, and the host (WebCore) will deal with it somehow.
- No round tripping of CSS source code. In the previous approach, WebCore sends the source code to JSC, then JSC sends it back to WebCore to parse. With the new approach, the source code stays in WebCore
- As a consequence of (2), there's no need for a new method in the global object method table
d4027ef to
125a938
Compare
|
EWS run on previous version of this PR (hash 125a938) Details |
125a938 to
32d0e5b
Compare
|
EWS run on previous version of this PR (hash 32d0e5b) Details |
32d0e5b to
6079649
Compare
|
EWS run on previous version of this PR (hash 6079649) Details |
6079649 to
2b9b70d
Compare
|
EWS run on previous version of this PR (hash 2b9b70d) Details |
2b9b70d to
45e0444
Compare
|
EWS run on previous version of this PR (hash 45e0444) Details
|
45e0444 to
0ade3f6
Compare
|
EWS run on previous version of this PR (hash 0ade3f6) Details |
iOS Safer C++ Build #49611 (0ade3f6)❌ Found 1 failing file with 1 issue. Please address these issues before landing. See WebKit Guidelines for Safer C++ Programming. |
macOS Safer C++ Build #129607 (0ade3f6)❌ Found 1 failing file with 1 issue. Please address these issues before landing. See WebKit Guidelines for Safer C++ Programming. |
There was a problem hiding this comment.
I wonder if it makes sense to have a fast path here? WDYT about adding a microbenchmark and perf testing that to ensure there is no regression?
There was a problem hiding this comment.
I wonder if we can name this in a way that is more flexible?
There was a problem hiding this comment.
Not too sure if this comment is needed
0ade3f6 to
8e41c06
Compare
|
EWS run on previous version of this PR (hash 8e41c06) Details
|
rdar://80917329 https://bugs.webkit.org/show_bug.cgi?id=227967 Reviewed by NOBODY (OOPS!). Explanation of why this fixes the bug (OOPS!). No new tests (OOPS!). * LayoutTests/TestExpectations: * LayoutTests/imported/w3c/web-platform-tests/content-security-policy/style-src/import-style-allowed.sub-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/content-security-policy/style-src/import-style-blocked.sub-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/fetch/api/request/destination/fetch-destination.https-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/document-metadata/the-style-element/tentative/style-element-basic-import-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/document-metadata/the-style-element/tentative/style-element-fixed-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/charset-2-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/charset-bom-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/charset-bom.html: * 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/content-type-checking.html: * 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/credentials.sub-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/css-module-parse-error-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/import-css-module-dynamic-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/css-module/redirect-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/referrer-policies.sub-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/relative-urls-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/repeated-import-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/resources/should-not-be-loaded.js: Added. * LayoutTests/imported/w3c/web-platform-tests/shadow-dom/declarative/tentative/shadowrootadoptedstylesheets/shadowrootadoptedstylesheets-fetched-module-expected.txt: * Source/JavaScriptCore/inspector/agents/InspectorDebuggerAgent.cpp: (Inspector::scriptTypeForScript): * Source/JavaScriptCore/jsc.cpp: (GlobalObject::moduleLoaderFetch): * Source/JavaScriptCore/parser/NodesAnalyzeModule.cpp: (JSC::tryCreateAttributes): * Source/JavaScriptCore/parser/SourceProvider.h: (JSC::SourceProvider::isModuleType const): * Source/JavaScriptCore/runtime/AbstractModuleRecord.cpp: (JSC::AbstractModuleRecord::moduleType const): * Source/JavaScriptCore/runtime/Completion.cpp: (JSC::getSourceType): (JSC::retrieveTypeImportAttribute): * Source/JavaScriptCore/runtime/JSMicrotask.cpp: (JSC::moduleRegistryFetchSettled): (JSC::moduleLoadTopSettled): * Source/JavaScriptCore/runtime/JSModuleLoader.cpp: (JSC::JSModuleLoader::provideFetch): (JSC::JSModuleLoader::makeModule): (JSC::JSModuleLoader::hostLoadImportedModule): * Source/JavaScriptCore/runtime/JSModuleLoader.h: * Source/JavaScriptCore/runtime/ModuleRegistryEntry.cpp: (JSC::ModuleRegistryEntry::provideFetch): * Source/JavaScriptCore/runtime/ModuleRegistryEntry.h: * Source/JavaScriptCore/runtime/ScriptFetchParameters.h: (JSC::ScriptFetchParameters::parseType): * Source/JavaScriptCore/runtime/SyntheticModuleRecord.h: * Source/WTF/Scripts/Preferences/UnifiedWebPreferences.yaml: * Source/WebCore/bindings/js/CachedModuleScriptLoader.cpp: (WebCore::CachedModuleScriptLoader::load): (WebCore::CachedModuleScriptLoader::notifyFinished): (WebCore::CachedModuleScriptLoader::setCSSStyleSheet): (WebCore::CachedModuleScriptLoader::notifyFinishedInternal): * Source/WebCore/bindings/js/CachedModuleScriptLoader.h: * Source/WebCore/bindings/js/CachedScriptFetcher.cpp: (WebCore::CachedScriptFetcher::requestResourceWithCache const): * Source/WebCore/bindings/js/JSDOMGlobalObject.cpp: (WebCore::JSDOMGlobalObject::moduleTypeCanBeLoaded): * Source/WebCore/bindings/js/JSDOMGlobalObject.h: * Source/WebCore/bindings/js/JSDOMWindowBase.cpp: (WebCore::JSDOMWindowBase::globalObjectMethodTable): * Source/WebCore/bindings/js/ScriptModuleLoader.cpp: (WebCore::ScriptModuleLoader::importModule): (WebCore::createCSSModule): (WebCore::ScriptModuleLoader::notifyFinished): * Source/WebCore/bindings/js/ScriptModuleLoader.h: * Source/WebCore/bindings/js/WorkerModuleScriptLoader.cpp: (WebCore::WorkerModuleScriptLoader::load): * Source/WebCore/loader/cache/CachedCSSStyleSheet.cpp: (WebCore::CachedCSSStyleSheet::sheetText const): * Source/WebCore/loader/cache/CachedCSSStyleSheet.h: * Tools/Scripts/webkitpy/style/checkers/cpp.py:
8e41c06 to
28638cf
Compare
|
EWS run on current version of this PR (hash 28638cf) Details
|
|
This patch initially started out as a rough proof-of-concept. Since then I've iterated on it more and splitted it into multiple pieces for upstreaming. The last piece to enable CSS module scripts is here: #74961 |
🛠 ios-apple
28638cf
28638cf