Bug 231114

Summary: [WebAssembly][modules] Importing globals into Wasm from JS doesn't work
Product: WebKit Reporter: Asumu Takikawa <asumu>
Component: WebAssemblyAssignee: Nobody <webkit-unassigned>
Status: RESOLVED FIXED    
Severity: Normal CC: ews-watchlist, fpizlo, keith_miller, mark.lam, msaboff, saam, tzagallo, webkit-bug-importer, ysuzuki
Priority: P2 Keywords: InRadar
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
Attachments:
Description Flags
Patch
none
Patch none

Asumu Takikawa
Reported 2021-10-01 17:22:51 PDT
Wasm/ES module integration should allow globals and other values to be imported in the JS->Wasm direction, such as in the example below. This is based on this example from the proposal explainer: https://github.com/WebAssembly/esm-integration/blob/main/proposals/esm-integration/EXAMPLES.md#value-imports Entry script (global.js): ``` import * as assert from '../assert.js'; import {incrementCount} from "./global.wasm"; import {count} from "./global.mjs"; incrementCount(); assert.eq(count.valueOf(), 43); ``` global.mjs: ``` export const count = new WebAssembly.Global({ value: 'i32', mutable: true, }, 42); ``` global.wat: ``` ;; global.wat --> global.wasm (module ;; this import should work normally (import "./global.mjs" "count" (global (mut i32))) (func (export "incrementCount") (global.set 0 (i32.add (global.get 0) (i32.const 1)))) ) ``` Currently, this doesn't work in JSC and produces a `LinkError` like the following: ``` $ ~/WebKit/WebKitBuild/GTK/Release/bin/jsc -m global.js Exception: LinkError: imported global ./global.mjs:count must be a WebAssembly.Global object since it is mutable moduleDeclarationInstantiation@[native code] link@[native code] link@[native code] linkAndEvaluateModule@[native code] @[native code] asyncFunctionResume@[native code] @[native code] promiseReactionJobWithoutPromise@[native code] promiseReactionJob@[native code] ``` The issue appears to be that Wasm is fetching the value from JS too early, when it's still `undefined`. JSC's Wasm module records look at the imports in the `link` step of module execution, rather than in the `evaluate` step (as the Wasm/ESM proposal spec says: https://webassembly.github.io/esm-integration/js-api/index.html#module-execution). I'm working on a draft patch that would move the work in the `link` step into the `evaluate` step. This appears to pass almost all of the current Wasm modules tests (except in one case where I think the test might be wrong). I'll share the patch after writing more tests to check various edge cases. Even with this change, more work is probably needed for `WebAssembly.Memory` to work (because of https://bugs.webkit.org/show_bug.cgi?id=184745).
Attachments
Patch (33.48 KB, patch)
2021-10-19 14:05 PDT, Asumu Takikawa
no flags
Patch (33.65 KB, patch)
2021-10-21 16:58 PDT, Asumu Takikawa
no flags
Radar WebKit Bug Importer
Comment 1 2021-10-08 17:23:21 PDT
Asumu Takikawa
Comment 2 2021-10-19 14:05:00 PDT
Yusuke Suzuki
Comment 3 2021-10-21 09:01:03 PDT
Comment on attachment 441794 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=441794&action=review r=me > Source/JavaScriptCore/runtime/AbstractModuleRecord.cpp:840 > return jsModuleRecord->evaluate(globalObject, sentValue, resumeMode); Replace it with RELEASE_AND_RETURN(scope, jsModuleRecord->evaluate(globalObject, sentValue, resumeMode)); since scope is defined now. > Source/JavaScriptCore/runtime/AbstractModuleRecord.cpp:848 > return wasmModuleRecord->evaluate(globalObject); Replace it with RELEASE_AND_RETURN(scope, wasmModuleRecord->evaluate(globalObject));
Asumu Takikawa
Comment 4 2021-10-21 16:58:24 PDT
EWS
Comment 5 2021-10-22 11:35:48 PDT
Committed r284702 (243419@main): <https://commits.webkit.org/243419@main> All reviewed patches have been landed. Closing bug and clearing flags on attachment 442089 [details].
Note You need to log in before you can comment on or make changes to this bug.