Skip to content

Add experimental option to use source wasm phase imports #23175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ See docs/process.md for more on how version tagging works.

4.0.5 (in development)
----------------------
- Added initial support for wasm source phase imports via
`-sSOURCE_PHASE_IMPORTS`. This is currently experimental and not yet
implemented in browsers. (#23175)

4.0.4 - 02/25/25
----------------
Expand Down
12 changes: 12 additions & 0 deletions site/source/docs/tools_reference/settings_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3347,3 +3347,15 @@ Use _ for non-pointer arguments, p for pointer/i53 arguments, and P for optional
Example use -sSIGNATURE_CONVERSIONS=someFunction:_p,anotherFunction:p

Default value: []

.. _source_phase_imports:

SOURCE_PHASE_IMPORTS
====================

Experimental support for wasm source phase imports.
This is only currently implemented in the pre-release/nightly version of node,
and not yet supported by browsers.
Requires EXPORT_ES6

Default value: false
48 changes: 28 additions & 20 deletions src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -558,11 +558,32 @@ function instrumentWasmTableWithAbort() {
}
#endif

#if LOAD_SOURCE_MAP
function receiveSourceMapJSON(sourceMap) {
wasmSourceMap = new WasmSourceMap(sourceMap);
{{{ runIfMainThread("removeRunDependency('source-map');") }}}
}
#endif

#if (PTHREADS || WASM_WORKERS) && (LOAD_SOURCE_MAP || USE_OFFSET_CONVERTER)
// When using postMessage to send an object, it is processed by the structured
// clone algorithm. The prototype, and hence methods, on that object is then
// lost. This function adds back the lost prototype. This does not work with
// nested objects that has prototypes, but it suffices for WasmSourceMap and
// WasmOffsetConverter.
function resetPrototype(constructor, attrs) {
var object = Object.create(constructor.prototype);
return Object.assign(object, attrs);
}
#endif

#if !SOURCE_PHASE_IMPORTS
#if SINGLE_FILE
// In SINGLE_FILE mode the wasm binary is encoded inline here as a data: URL.
var wasmBinaryFile = '{{{ WASM_BINARY_FILE }}}';
#else
var wasmBinaryFile;

function findWasmBinary() {
#if EXPORT_ES6 && !AUDIO_WORKLET
if (Module['locateFile']) {
Expand Down Expand Up @@ -647,13 +668,6 @@ var splitModuleProxyHandler = {
};
#endif

#if LOAD_SOURCE_MAP
function receiveSourceMapJSON(sourceMap) {
wasmSourceMap = new WasmSourceMap(sourceMap);
{{{ runIfMainThread("removeRunDependency('source-map');") }}}
}
#endif

#if SPLIT_MODULE || !WASM_ASYNC_COMPILATION
function instantiateSync(file, info) {
var module;
Expand Down Expand Up @@ -701,18 +715,6 @@ function instantiateSync(file, info) {
}
#endif

#if (PTHREADS || WASM_WORKERS) && (LOAD_SOURCE_MAP || USE_OFFSET_CONVERTER)
// When using postMessage to send an object, it is processed by the structured
// clone algorithm. The prototype, and hence methods, on that object is then
// lost. This function adds back the lost prototype. This does not work with
// nested objects that has prototypes, but it suffices for WasmSourceMap and
// WasmOffsetConverter.
function resetPrototype(constructor, attrs) {
var object = Object.create(constructor.prototype);
return Object.assign(object, attrs);
}
#endif

#if WASM_ASYNC_COMPILATION
async function instantiateArrayBuffer(binaryFile, imports) {
try {
Expand Down Expand Up @@ -815,6 +817,7 @@ async function instantiateAsync(binary, binaryFile, imports) {
return instantiateArrayBuffer(binaryFile, imports);
}
#endif // WASM_ASYNC_COMPILATION
#endif // SOURCE_PHASE_IMPORTS

function getWasmImports() {
#if PTHREADS
Expand Down Expand Up @@ -1016,10 +1019,14 @@ function getWasmImports() {
}
#endif

#if SOURCE_PHASE_IMPORTS
var instance = await WebAssembly.instantiate(wasmModule, info);
var exports = receiveInstantiationResult({instance, 'module':wasmModule});
return exports;
#else
#if !SINGLE_FILE
wasmBinaryFile ??= findWasmBinary();
#endif

#if WASM_ASYNC_COMPILATION
#if RUNTIME_DEBUG
dbg('asynchronously preparing wasm');
Expand Down Expand Up @@ -1051,6 +1058,7 @@ function getWasmImports() {
return receiveInstance(result[0]);
#endif
#endif // WASM_ASYNC_COMPILATION
#endif // SOURCE_PHASE_IMPORTS
}

#if !WASM_BIGINT
Expand Down
7 changes: 7 additions & 0 deletions src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -2182,6 +2182,13 @@ var LEGACY_RUNTIME = false;
// [link]
var SIGNATURE_CONVERSIONS = [];

// Experimental support for wasm source phase imports.
// This is only currently implemented in the pre-release/nightly version of node,
// and not yet supported by browsers.
// Requires EXPORT_ES6
// [link]
var SOURCE_PHASE_IMPORTS = false;

// For renamed settings the format is:
// [OLD_NAME, NEW_NAME]
// For removed settings (which now effectively have a fixed value and can no
Expand Down
14 changes: 12 additions & 2 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,13 +358,23 @@ def test_emcc_generate_config(self, compiler):
@parameterized({
'': ([],),
'node': (['-sENVIRONMENT=node'],),
# load a worker before startup to check ES6 modules there as well
'pthreads': (['-pthread', '-sPTHREAD_POOL_SIZE=1'],),
})
def test_esm(self, args):
self.run_process([EMCC, '-o', 'hello_world.mjs',
'--extern-post-js', test_file('modularize_post_js.js'),
test_file('hello_world.c')] + args)
src = read_file('hello_world.mjs')
self.assertContained('export default Module;', src)
self.assertContained('export default Module;', read_file('hello_world.mjs'))
self.assertContained('hello, world!', self.run_js('hello_world.mjs'))

@requires_node_canary
def test_esm_source_phase_imports(self):
self.node_args += ['--experimental-wasm-modules']
self.run_process([EMCC, '-o', 'hello_world.mjs', '-sSOURCE_PHASE_IMPORTS',
'--extern-post-js', test_file('modularize_post_js.js'),
test_file('hello_world.c')])
self.assertContained('import source wasmModule from', read_file('hello_world.mjs'))
self.assertContained('hello, world!', self.run_js('hello_world.mjs'))

@parameterized({
Expand Down
6 changes: 6 additions & 0 deletions tools/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -1757,6 +1757,9 @@ def get_full_import_name(name):
if settings.ASYNCIFY == 2:
diagnostics.warning('experimental', '-sASYNCIFY=2 (JSPI) is still experimental')

if settings.SOURCE_PHASE_IMPORTS:
diagnostics.warning('experimental', '-sSOURCE_PHASE_IMPORTS is still experimental and not yet supported in browsers')

if settings.WASM2JS:
if settings.GENERATE_SOURCE_MAP:
exit_with_error('wasm2js does not support source maps yet (debug in wasm for now)')
Expand Down Expand Up @@ -2478,6 +2481,9 @@ def modularize():
})();
''' % {'EXPORT_NAME': settings.EXPORT_NAME}

if settings.SOURCE_PHASE_IMPORTS:
src = f"import source wasmModule from './{settings.WASM_BINARY_FILE}';\n\n" + src

# Given the async nature of how the Module function and Module object
# come into existence in AudioWorkletGlobalScope, store the Module
# function under a different variable name so that AudioWorkletGlobalScope
Expand Down