Skip to content

Commit 5372294

Browse files
authored
Support the Tail Call Proposal (#4111)
1 parent 20c010d commit 5372294

File tree

11 files changed

+43
-21
lines changed

11 files changed

+43
-21
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
### Added
77

8+
* Added support for the WebAssembly `Tail Call` proposal.
9+
[#4111](https://github.com/rustwasm/wasm-bindgen/pull/4111)
10+
811
* Add bindings for `RTCPeerConnection.setConfiguration(RTCConfiguration)` method.
912
[#4105](https://github.com/rustwasm/wasm-bindgen/pull/4105)
1013

@@ -204,7 +207,7 @@ Released 2024-08-13
204207

205208
* Fix MDN links to static interface methods.
206209
[#4010](https://github.com/rustwasm/wasm-bindgen/pull/4010)
207-
210+
208211
* Fixed Deno support.
209212
[#3990](https://github.com/rustwasm/wasm-bindgen/pull/3990)
210213

crates/cli-support/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ serde = { version = "1.0", features = ["derive"] }
2222
serde_json = "1.0"
2323
tempfile = "3.0"
2424
unicode-ident = "1.0.5"
25-
walrus = "0.21"
25+
walrus = "0.21.2"
2626
wasm-bindgen-externref-xform = { path = '../externref-xform', version = '=0.2.93' }
2727
wasm-bindgen-multi-value-xform = { path = '../multi-value-xform', version = '=0.2.93' }
2828
wasm-bindgen-shared = { path = "../shared", version = '=0.2.93' }

crates/cli-support/src/descriptors.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@ impl WasmBindgenDescriptorsSection {
199199
self.found = true;
200200
}
201201
}
202+
203+
fn visit_return_call(&mut self, instr: &walrus::ir::ReturnCall) {
204+
if instr.func == self.wbindgen_describe_closure {
205+
self.found = true;
206+
}
207+
}
202208
}
203209

204210
struct UpdateDescribeClosure {
@@ -212,6 +218,12 @@ impl WasmBindgenDescriptorsSection {
212218
call.func = self.replacement;
213219
}
214220
}
221+
222+
fn visit_return_call_mut(&mut self, instr: &mut walrus::ir::ReturnCall) {
223+
if instr.func == self.wbindgen_describe_closure {
224+
instr.func = self.replacement;
225+
}
226+
}
215227
}
216228
}
217229
}

crates/cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ serde = { version = "1.0", features = ['derive'] }
3131
serde_derive = "1.0"
3232
serde_json = "1.0"
3333
ureq = { version = "2.7", default-features = false, features = ["brotli", "gzip"] }
34-
walrus = { version = "0.21", features = ['parallel'] }
34+
walrus = { version = "0.21.2", features = ['parallel'] }
3535
wasm-bindgen-cli-support = { path = "../cli-support", version = "=0.2.93" }
3636
wasm-bindgen-shared = { path = "../shared", version = "=0.2.93" }
3737

crates/externref-xform/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ version = "0.2.93"
1515

1616
[dependencies]
1717
anyhow = "1.0"
18-
walrus = "0.21"
18+
walrus = "0.21.2"
1919
wasm-bindgen-wasm-conventions = { path = "../wasm-conventions", version = "=0.2.93" }
2020

2121
[dev-dependencies]

crates/externref-xform/src/lib.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -722,18 +722,19 @@ impl Transform<'_> {
722722
impl VisitorMut for Rewrite<'_, '_> {
723723
fn start_instr_seq_mut(&mut self, seq: &mut InstrSeq) {
724724
for i in (0..seq.instrs.len()).rev() {
725-
let call = match &mut seq.instrs[i].0 {
726-
Instr::Call(call) => call,
725+
let func = match &mut seq.instrs[i].0 {
726+
Instr::Call(Call { func }) => func,
727+
Instr::ReturnCall(ReturnCall { func }) => func,
727728
_ => continue,
728729
};
729-
let intrinsic = match self.xform.intrinsic_map.get(&call.func) {
730+
let intrinsic = match self.xform.intrinsic_map.get(func) {
730731
Some(f) => f,
731732
None => {
732733
// If this wasn't a call of an intrinsic, but it was a
733734
// call of one of our old import functions then we
734735
// switch the functions we're calling here.
735-
if let Some(f) = self.xform.import_map.get(&call.func) {
736-
call.func = *f;
736+
if let Some(f) = self.xform.import_map.get(func) {
737+
*func = *f;
737738
}
738739
continue;
739740
}
@@ -776,8 +777,8 @@ impl Transform<'_> {
776777
seq.instrs
777778
.insert(i, (RefNull { ty }.into(), InstrLocId::default()));
778779
}
779-
Intrinsic::DropRef => call.func = self.heap_dealloc,
780-
Intrinsic::CloneRef => call.func = self.clone_ref,
780+
Intrinsic::DropRef => *func = self.heap_dealloc,
781+
Intrinsic::CloneRef => *func = self.clone_ref,
781782
}
782783
}
783784
}

crates/multi-value-xform/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ version = "0.2.93"
1515

1616
[dependencies]
1717
anyhow = "1.0"
18-
walrus = "0.21"
18+
walrus = "0.21.2"
1919
wasm-bindgen-wasm-conventions = { path = "../wasm-conventions", version = "=0.2.93" }
2020

2121
[dev-dependencies]

crates/threads-xform/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ version = "0.2.93"
1515

1616
[dependencies]
1717
anyhow = "1.0"
18-
walrus = "0.21"
18+
walrus = "0.21.2"
1919
wasm-bindgen-wasm-conventions = { path = "../wasm-conventions", version = "=0.2.93" }
2020

2121
[dev-dependencies]

crates/wasm-conventions/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ version = "0.2.93"
1313

1414
[dependencies]
1515
leb128 = "0.2"
16-
walrus = "0.21"
16+
walrus = "0.21.2"
1717
# Matching the version `walrus` depends on.
1818
anyhow = "1.0"
1919
log = "0.4"

crates/wasm-interpreter/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ version = "0.2.93"
1616
[dependencies]
1717
anyhow = "1.0"
1818
log = "0.4"
19-
walrus = "0.21"
19+
walrus = "0.21.2"
2020
wasm-bindgen-wasm-conventions = { path = "../wasm-conventions", version = "0.2.93" }
2121

2222
[dev-dependencies]

crates/wasm-interpreter/src/lib.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,14 @@ impl Frame<'_> {
338338
stack.pop().unwrap();
339339
}
340340

341-
Instr::Call(e) => {
341+
Instr::Call(Call { func }) | Instr::ReturnCall(ReturnCall { func }) => {
342+
let func = *func;
342343
// If this function is calling the `__wbindgen_describe`
343344
// function, which we've precomputed the id for, then
344345
// it's telling us about the next `u32` element in the
345346
// descriptor to return. We "call" the imported function
346347
// here by directly inlining it.
347-
if Some(e.func) == self.interp.describe_id {
348+
if Some(func) == self.interp.describe_id {
348349
let val = stack.pop().unwrap();
349350
log::debug!("__wbindgen_describe({})", val);
350351
self.interp.descriptor.push(val as u32);
@@ -354,7 +355,7 @@ impl Frame<'_> {
354355
// slightly different signature. Note that we don't eval the
355356
// previous arguments because they shouldn't have any side
356357
// effects we're interested in.
357-
} else if Some(e.func) == self.interp.describe_closure_id {
358+
} else if Some(func) == self.interp.describe_closure_id {
358359
let val = stack.pop().unwrap();
359360
stack.pop();
360361
stack.pop();
@@ -368,7 +369,7 @@ impl Frame<'_> {
368369
if self
369370
.module
370371
.funcs
371-
.get(e.func)
372+
.get(func)
372373
.name
373374
.as_ref()
374375
.is_some_and(|name| {
@@ -380,12 +381,17 @@ impl Frame<'_> {
380381
return Ok(());
381382
}
382383

383-
let ty = self.module.types.get(self.module.funcs.get(e.func).ty());
384+
let ty = self.module.types.get(self.module.funcs.get(func).ty());
384385
let args = (0..ty.params().len())
385386
.map(|_| stack.pop().unwrap())
386387
.collect::<Vec<_>>();
387388

388-
self.interp.call(e.func, self.module, &args);
389+
self.interp.call(func, self.module, &args);
390+
}
391+
392+
if let Instr::ReturnCall(_) = instr {
393+
log::debug!("return_call");
394+
self.done = true;
389395
}
390396
}
391397

0 commit comments

Comments
 (0)