Skip to content

Commit bdd20af

Browse files
Add initial integration with wasm-bindgen (#23493)
When `-sWASM_BINDGEN` is set, Emscripten will now call out to `wasm-bindgen` in the users's `PATH` and integrate the wasm-bindgen JS with the normal Emscripten JS. Some wasm-bindgen features may not yet be fully supported.
1 parent 34ba269 commit bdd20af

13 files changed

Lines changed: 133 additions & 11 deletions

File tree

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ commands:
7272
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
7373
export PATH=${HOME}/.cargo/bin:${PATH}
7474
rustup target add wasm32-unknown-emscripten
75+
cargo install wasm-bindgen-cli
7576
echo "export PATH=\"\$HOME/.cargo/bin:\$PATH\"" >> $BASH_ENV
7677
install-node-version:
7778
description: "install a specific version of node"

ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ See docs/process.md for more on how version tagging works.
2222
----------------------
2323
- The `-sUSE_PTHREADS` and `-sMEMORY64` flags have been deprecated in favor of the
2424
more standard `-pthread` and `-m64` (or `--target=wasm64`) flags. (#27025)
25+
- Adds wasm-bindgen support. When `-sWASM_BINDGEN` is set, Emscripten will call
26+
out to `wasm-bindgen` in the users's path and integrate the wasm-bindgen JS
27+
with the normal Emscripten JS. Some wasm-bindgen features may not yet be fully
28+
supported. (#23493)
2529

2630
6.0.0 - 06/04/26
2731
----------------

emcc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ def compile_source_file(input_file):
578578
# Default to assuming the inputs are object files and pass them to the linker
579579
pass
580580

581-
return [f.value for f in linker_args]
581+
return linker_args
582582

583583

584584
if __name__ == '__main__':

site/source/docs/tools_reference/settings_reference.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3288,6 +3288,15 @@ Example use ``-sSIGNATURE_CONVERSIONS=someFunction:_p,anotherFunction:p``
32883288

32893289
Default value: []
32903290

3291+
.. _wasm_bindgen:
3292+
3293+
WASM_BINDGEN
3294+
============
3295+
3296+
Run wasm-bindgen and integrate the rust-exported symbols into the rest of Emscripten's JS output.
3297+
3298+
Default value: 0
3299+
32913300
.. _source_phase_imports:
32923301

32933302
SOURCE_PHASE_IMPORTS

src/settings.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2167,6 +2167,10 @@ var LEGACY_RUNTIME = false;
21672167
// [link]
21682168
var SIGNATURE_CONVERSIONS = [];
21692169

2170+
// Run wasm-bindgen and integrate the rust-exported symbols into the rest of Emscripten's JS output.
2171+
// [link]
2172+
var WASM_BINDGEN = 0;
2173+
21702174
// Experimental support for wasm source phase imports.
21712175
// This is only currently implemented in the pre-release/nightly version of
21722176
// node, and not yet supported by browsers.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[build]
2+
target = "wasm32-unknown-emscripten"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[build]
2+
target = "wasm32-unknown-emscripten"
3+
rustflags = [
4+
"-Cllvm-args=-enable-emscripten-cxx-exceptions=0",
5+
"-Cpanic=abort",
6+
"-Crelocation-model=static",
7+
]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "bindgen_integration"
3+
edition = "2021"
4+
5+
[lib]
6+
crate-type = ["staticlib"]
7+
8+
[dependencies]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use wasm_bindgen::prelude::*;
2+
3+
#[wasm_bindgen]
4+
pub fn rs_add(a: i32, b: i32) -> i32 {
5+
return a + b;
6+
}

test/test_other.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14830,6 +14830,22 @@ def test_rust_integration_basics(self):
1483014830
}''')
1483114831
self.do_runf('main.cpp', 'Hello from rust!', cflags=[lib])
1483214832

14833+
@requires_rust
14834+
def test_wasm_bindgen_integration(self):
14835+
copytree(test_file('rust/bindgen_integration'), '.')
14836+
self.run_process(['cargo', 'add', 'wasm-bindgen'])
14837+
self.run_process(['cargo', 'build'])
14838+
lib = 'target/wasm32-unknown-emscripten/debug/libbindgen_integration.a'
14839+
self.assertExists(lib)
14840+
14841+
create_file('empty.c', '')
14842+
create_file('post.js', '''
14843+
Module.onRuntimeInitialized = () => out(Module.rs_add(17, 25));
14844+
''')
14845+
emcc_args = [lib, '-sWASM_BINDGEN', '--post-js=post.js', '-lexports.js']
14846+
14847+
self.do_runf('empty.c', '42', cflags=emcc_args)
14848+
1483314849
def test_relative_em_cache(self):
1483414850
with env_modify({'EM_CACHE': 'foo'}):
1483514851
self.assert_fail([EMCC, '-c', test_file('hello_world.c')], 'emcc: error: environment variable EM_CACHE must be an absolute path: foo')

0 commit comments

Comments
 (0)