Skip to content

Commit 08fb654

Browse files
authored
Merge pull request #28 from dhil/wasmfx-merge
Merge with upstream
2 parents 5e266c0 + 57ecd29 commit 08fb654

File tree

12 files changed

+309
-300
lines changed

12 files changed

+309
-300
lines changed

Cargo.lock

Lines changed: 211 additions & 234 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ resolver = "2"
1818

1919
[workspace.package]
2020
edition = "2021"
21-
version = "0.32.0"
21+
version = "0.33.0"
2222
license = "Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT"
2323
repository = "https://github.com/bytecodealliance/wasi-rs"
2424

@@ -32,21 +32,21 @@ indexmap = "2.0.0"
3232
prettyplease = "0.2.20"
3333
syn = { version = "2.0", features = ["printing"] }
3434

35-
wasmparser = { git = "https://github.com/wasmfx/wasmfx-tools", tag = "v1.217.0" }
36-
wasm-metadata = { git = "https://github.com/wasmfx/wasmfx-tools", tag = "v1.217.0" }
37-
wasm-encoder = { git = "https://github.com/wasmfx/wasmfx-tools", tag = "v1.217.0" }
38-
wit-parser = { git = "https://github.com/wasmfx/wasmfx-tools", tag = "v1.217.0" }
39-
wit-component = { git = "https://github.com/wasmfx/wasmfx-tools", tag = "v1.217.0" }
35+
wasmparser = { git = "https://github.com/wasmfx/wasmfx-tools", tag = "v1.218.0" }
36+
wasm-metadata = { git = "https://github.com/wasmfx/wasmfx-tools", tag = "v1.218.0" }
37+
wasm-encoder = { git = "https://github.com/wasmfx/wasmfx-tools", tag = "v1.218.0" }
38+
wit-parser = { git = "https://github.com/wasmfx/wasmfx-tools", tag = "v1.218.0" }
39+
wit-component = { git = "https://github.com/wasmfx/wasmfx-tools", tag = "v1.218.0" }
4040

41-
wit-bindgen-core = { path = 'crates/core', version = '0.32.0' }
42-
wit-bindgen-c = { path = 'crates/c', version = '0.32.0' }
43-
wit-bindgen-rust = { path = "crates/rust", version = "0.32.0" }
44-
wit-bindgen-teavm-java = { path = 'crates/teavm-java', version = '0.32.0' }
45-
wit-bindgen-go = { path = 'crates/go', version = '0.32.0' }
46-
wit-bindgen-csharp = { path = 'crates/csharp', version = '0.32.0' }
47-
wit-bindgen-markdown = { path = 'crates/markdown', version = '0.32.0' }
48-
wit-bindgen-moonbit = { path = 'crates/moonbit', version = '0.32.0' }
49-
wit-bindgen = { path = 'crates/guest-rust', version = '0.32.0', default-features = false }
41+
wit-bindgen-core = { path = 'crates/core', version = '0.33.0' }
42+
wit-bindgen-c = { path = 'crates/c', version = '0.33.0' }
43+
wit-bindgen-rust = { path = "crates/rust", version = "0.33.0" }
44+
wit-bindgen-teavm-java = { path = 'crates/teavm-java', version = '0.33.0' }
45+
wit-bindgen-go = { path = 'crates/go', version = '0.33.0' }
46+
wit-bindgen-csharp = { path = 'crates/csharp', version = '0.33.0' }
47+
wit-bindgen-markdown = { path = 'crates/markdown', version = '0.33.0' }
48+
wit-bindgen-moonbit = { path = 'crates/moonbit', version = '0.33.0' }
49+
wit-bindgen = { path = 'crates/guest-rust', version = '0.33.0', default-features = false }
5050

5151
[[bin]]
5252
name = "wit-bindgen"
@@ -86,8 +86,8 @@ moonbit = ['dep:wit-bindgen-moonbit']
8686

8787
[dev-dependencies]
8888
heck = { workspace = true }
89-
wasmtime = { version = "24.0.0", features = ['component-model'] }
90-
wasmtime-wasi = { version = "24.0.0" }
89+
wasmtime = { version = "25.0.0", features = ['component-model'] }
90+
wasmtime-wasi = { version = "25.0.0" }
9191
test-artifacts = { path = 'crates/test-rust-wasm/artifacts' }
9292
wit-parser = { workspace = true }
9393
wasmparser = { workspace = true }

crates/c/src/component_type_object.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub fn object(
2323

2424
// Build a module with one function that's a "dummy function"
2525
let mut types = TypeSection::new();
26-
types.function([], []);
26+
types.ty().function([], []);
2727
module.section(&types);
2828
let mut funcs = FunctionSection::new();
2929
funcs.function(0);

crates/csharp/src/lib.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1956,6 +1956,7 @@ struct FunctionBindgen<'a, 'b> {
19561956
blocks: Vec<Block>,
19571957
payloads: Vec<String>,
19581958
needs_cleanup_list: bool,
1959+
needs_native_alloc_list: bool,
19591960
cleanup: Vec<Cleanup>,
19601961
import_return_pointer_area_size: usize,
19611962
import_return_pointer_area_align: usize,
@@ -1989,6 +1990,7 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
19891990
blocks: Vec::new(),
19901991
payloads: Vec::new(),
19911992
needs_cleanup_list: false,
1993+
needs_native_alloc_list: false,
19921994
cleanup: Vec::new(),
19931995
import_return_pointer_area_size: 0,
19941996
import_return_pointer_area_align: 0,
@@ -2185,7 +2187,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
21852187
Instruction::F64Load { offset } => results.push(format!("BitConverter.ToDouble(new Span<byte>((void*)({} + {offset}), 8))",operands[0])),
21862188
Instruction::I32Store { offset }
21872189
| Instruction::PointerStore { offset }
2188-
| Instruction::LengthStore { offset } => uwriteln!(self.src, "BitConverter.TryWriteBytes(new Span<byte>((void*)({} + {offset}), 4), unchecked((int){}));", operands[1], operands[0]),
2190+
| Instruction::LengthStore { offset } => uwriteln!(self.src, "BitConverter.TryWriteBytes(new Span<byte>((void*)({} + {offset}), 4), {});", operands[1], operands[0]),
21892191
Instruction::I32Store8 { offset } => uwriteln!(self.src, "*(byte*)({} + {offset}) = (byte){};", operands[1], operands[0]),
21902192
Instruction::I32Store16 { offset } => uwriteln!(self.src, "BitConverter.TryWriteBytes(new Span<byte>((void*)({} + {offset}), 2), (short){});", operands[1], operands[0]),
21912193
Instruction::I64Store { offset } => uwriteln!(self.src, "BitConverter.TryWriteBytes(new Span<byte>((void*)({} + {offset}), 8), unchecked((long){}));", operands[1], operands[0]),
@@ -2601,16 +2603,18 @@ impl Bindgen for FunctionBindgen<'_, '_> {
26012603
let ty = self.gen.type_name_with_qualifier(element, true);
26022604
let index = self.locals.tmp("index");
26032605

2604-
let buffer: String = self.locals.tmp("buffer");
2605-
let gc_handle = self.locals.tmp("gcHandle");
26062606
let address = self.locals.tmp("address");
2607+
let buffer_size = self.locals.tmp("bufferSize");
2608+
//TODO: wasm64
2609+
let align = self.gen.gen.sizes.align(element).align_wasm32();
2610+
self.needs_native_alloc_list = true;
26072611

26082612
uwrite!(
26092613
self.src,
26102614
"
2611-
byte[] {buffer} = new byte[{size} * {list}.Count];
2612-
var {gc_handle} = GCHandle.Alloc({buffer}, GCHandleType.Pinned);
2613-
var {address} = {gc_handle}.AddrOfPinnedObject();
2615+
var {buffer_size} = {size} * (nuint){list}.Count;
2616+
var {address} = NativeMemory.AlignedAlloc({buffer_size}, {align});
2617+
nativeAllocs.Add((IntPtr){address});
26142618
26152619
for (int {index} = 0; {index} < {list}.Count; ++{index}) {{
26162620
{ty} {block_element} = {list}[{index}];
@@ -2620,12 +2624,6 @@ impl Bindgen for FunctionBindgen<'_, '_> {
26202624
"
26212625
);
26222626

2623-
if realloc.is_none() {
2624-
self.cleanup.push(Cleanup {
2625-
address: gc_handle.clone(),
2626-
});
2627-
}
2628-
26292627
results.push(format!("(int){address}"));
26302628
results.push(format!("{list}.Count"));
26312629
}
@@ -2829,6 +2827,17 @@ impl Bindgen for FunctionBindgen<'_, '_> {
28292827
uwriteln!(self.src, "{address}.Free();");
28302828
}
28312829

2830+
if self.needs_native_alloc_list {
2831+
self.src.insert_str(0, "var nativeAllocs = new List<IntPtr>();
2832+
");
2833+
2834+
uwriteln!(self.src, "\
2835+
foreach (var nativeAlloc in nativeAllocs)
2836+
{{
2837+
NativeMemory.AlignedFree((void*)nativeAlloc);
2838+
}}");
2839+
}
2840+
28322841
if !matches!((self.gen.direction, self.kind), (Direction::Import, FunctionKind::Constructor(_))) {
28332842
match func.results.len() {
28342843
0 => (),
@@ -2920,7 +2929,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
29202929
Direction::Export => {
29212930
self.gen.gen.needs_rep_table = true;
29222931
let local_rep = self.locals.tmp("localRep");
2923-
let export_name = self.gen.gen.all_resources[&id].export_impl_name();
2932+
let export_name = self.gen.gen.all_resources[&id].export_impl_name();
29242933
if is_own {
29252934
// Note that we set `{op}.Handle` to zero below to ensure that application code doesn't
29262935
// try to use the instance while the host has ownership. We'll set it back to non-zero

crates/guest-rust/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ Used when compiling Rust programs to the component model.
1212
"""
1313

1414
[dependencies]
15-
wit-bindgen-rust-macro = { path = "./macro", optional = true, version = "0.32.0" }
16-
wit-bindgen-rt = { path = "./rt", version = "0.32.0", features = ["bitflags"] }
15+
wit-bindgen-rust-macro = { path = "./macro", optional = true, version = "0.33.0" }
16+
wit-bindgen-rt = { path = "./rt", version = "0.33.0", features = ["bitflags"] }
1717

1818
[features]
1919
default = ["macros", "realloc"]

crates/guest-rust/rt/src/cabi_realloc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
#include <stdint.h>
44

5-
extern void *cabi_realloc_wit_bindgen_0_32_0(void *ptr, size_t old_size, size_t align, size_t new_size);
5+
extern void *cabi_realloc_wit_bindgen_0_33_0(void *ptr, size_t old_size, size_t align, size_t new_size);
66

77
__attribute__((__weak__, __export_name__("cabi_realloc")))
88
void *cabi_realloc(void *ptr, size_t old_size, size_t align, size_t new_size) {
9-
return cabi_realloc_wit_bindgen_0_32_0(ptr, old_size, align, new_size);
9+
return cabi_realloc_wit_bindgen_0_33_0(ptr, old_size, align, new_size);
1010
}
0 Bytes
Binary file not shown.

crates/guest-rust/rt/src/cabi_realloc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// This file is generated by ./ci/rebuild-libcabi-realloc.sh
22

33
#[no_mangle]
4-
pub unsafe extern "C" fn cabi_realloc_wit_bindgen_0_32_0(
4+
pub unsafe extern "C" fn cabi_realloc_wit_bindgen_0_33_0(
55
old_ptr: *mut u8,
66
old_len: usize,
77
align: usize,
0 Bytes
Binary file not shown.

crates/moonbit/src/lib.rs

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -111,34 +111,34 @@ extern "wasm" fn copy_inline(dest : Int, src : Int, len : Int) =
111111
#|(func (param i32) (param i32) (param i32) local.get 0 local.get 1 local.get 2 memory.copy)
112112
113113
pub extern "wasm" fn str2ptr(str : String) -> Int =
114-
#|(func (param i32) (result i32) local.get 0 call $moonbit.decref local.get 0 i32.const 8 i32.add)
114+
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
115115
116116
pub extern "wasm" fn ptr2str(ptr : Int) -> String =
117117
#|(func (param i32) (result i32) local.get 0 i32.const 4 i32.sub i32.const 243 i32.store8 local.get 0 i32.const 8 i32.sub)
118118
119119
pub extern "wasm" fn bytes2ptr(bytes : Bytes) -> Int =
120-
#|(func (param i32) (result i32) local.get 0 call $moonbit.decref local.get 0 i32.const 8 i32.add)
120+
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
121121
122122
pub extern "wasm" fn ptr2bytes(ptr : Int) -> Bytes =
123123
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.sub)
124124
125125
pub extern "wasm" fn uint_array2ptr(array : FixedArray[UInt]) -> Int =
126-
#|(func (param i32) (result i32) local.get 0 call $moonbit.decref local.get 0 i32.const 8 i32.add)
126+
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
127127
128128
pub extern "wasm" fn uint64_array2ptr(array : FixedArray[UInt64]) -> Int =
129-
#|(func (param i32) (result i32) local.get 0 call $moonbit.decref local.get 0 i32.const 8 i32.add)
129+
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
130130
131131
pub extern "wasm" fn int_array2ptr(array : FixedArray[Int]) -> Int =
132-
#|(func (param i32) (result i32) local.get 0 call $moonbit.decref local.get 0 i32.const 8 i32.add)
132+
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
133133
134134
pub extern "wasm" fn int64_array2ptr(array : FixedArray[Int64]) -> Int =
135-
#|(func (param i32) (result i32) local.get 0 call $moonbit.decref local.get 0 i32.const 8 i32.add)
135+
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
136136
137137
pub extern "wasm" fn float_array2ptr(array : FixedArray[Float]) -> Int =
138-
#|(func (param i32) (result i32) local.get 0 call $moonbit.decref local.get 0 i32.const 8 i32.add)
138+
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
139139
140140
pub extern "wasm" fn double_array2ptr(array : FixedArray[Double]) -> Int =
141-
#|(func (param i32) (result i32) local.get 0 call $moonbit.decref local.get 0 i32.const 8 i32.add)
141+
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
142142
143143
pub extern "wasm" fn ptr2uint_array(ptr : Int) -> FixedArray[UInt] =
144144
#|(func (param i32) (result i32) local.get 0 i32.const 4 i32.sub i32.const 241 i32.store8 local.get 0 i32.const 8 i32.sub)
@@ -575,11 +575,17 @@ impl WorldGenerator for MoonBit {
575575
{ffi_qualifier}free(src_offset)
576576
dst
577577
}}
578-
579-
let return_area : Int = {ffi_qualifier}malloc({})
580-
",
581-
self.return_area_size,
578+
"
582579
);
580+
if self.return_area_size != 0 {
581+
uwriteln!(
582+
&mut body,
583+
"
584+
let return_area : Int = {ffi_qualifier}malloc({})
585+
",
586+
self.return_area_size,
587+
);
588+
}
583589
files.push(&format!("{EXPORT_DIR}/ffi.mbt"), indent(&body).as_bytes());
584590
self.export
585591
.insert("cabi_realloc".into(), "cabi_realloc".into());
@@ -797,7 +803,7 @@ impl InterfaceGenerator<'_> {
797803
.collect::<Vec<_>>()
798804
.join(", ");
799805

800-
let sig = self.sig_string(func);
806+
let sig = self.sig_string(func, false);
801807

802808
uwriteln!(
803809
self.ffi,
@@ -820,7 +826,7 @@ impl InterfaceGenerator<'_> {
820826
fn export(&mut self, interface_name: Option<&str>, func: &Function) {
821827
let sig = self.resolve.wasm_signature(AbiVariant::GuestExport, func);
822828

823-
let func_sig = self.sig_string(func);
829+
let func_sig = self.sig_string(func, true);
824830

825831
let export_name = func.core_export_name(interface_name);
826832

@@ -1039,7 +1045,7 @@ impl InterfaceGenerator<'_> {
10391045
}
10401046
}
10411047

1042-
fn sig_string(&mut self, func: &Function) -> String {
1048+
fn sig_string(&mut self, func: &Function, ignore_param: bool) -> String {
10431049
let name = match func.kind {
10441050
FunctionKind::Freestanding => func.name.to_moonbit_ident(),
10451051
FunctionKind::Constructor(_) => {
@@ -1074,7 +1080,11 @@ impl InterfaceGenerator<'_> {
10741080
.iter()
10751081
.map(|(name, ty)| {
10761082
let ty = self.type_name(ty, true);
1077-
let name = name.to_moonbit_ident();
1083+
let name = if ignore_param {
1084+
format!("_{}", name.to_moonbit_ident())
1085+
} else {
1086+
name.to_moonbit_ident()
1087+
};
10781088
format!("{name} : {ty}")
10791089
})
10801090
.collect::<Vec<_>>()
@@ -1202,7 +1212,7 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
12021212
&mut self.stub,
12031213
r#"
12041214
/// Destructor of the resource.
1205-
pub fn {name}::dtor(self : {name}) -> Unit {{
1215+
pub fn {name}::dtor(_self : {name}) -> Unit {{
12061216
abort("todo")
12071217
}}
12081218
"#
@@ -1570,8 +1580,10 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
15701580
.collect::<Vec<_>>()
15711581
.join(", ");
15721582

1573-
let payload = if self.gen.non_empty_type(ty.as_ref()).is_some() || is_result {
1583+
let payload = if self.gen.non_empty_type(ty.as_ref()).is_some() {
15741584
payload
1585+
} else if is_result {
1586+
format!("_{payload}")
15751587
} else {
15761588
String::new()
15771589
};
@@ -1601,7 +1613,6 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
16011613
r#"
16021614
match {op} {{
16031615
{cases}
1604-
_ => panic()
16051616
}}
16061617
"#
16071618
);
@@ -1611,7 +1622,6 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
16111622
r#"
16121623
let ({declarations}) = match {op} {{
16131624
{cases}
1614-
_ => panic()
16151625
}}
16161626
"#
16171627
);
@@ -2109,13 +2119,14 @@ impl Bindgen for FunctionBindgen<'_, '_> {
21092119
Type::U8 => {
21102120
let result = self.locals.tmp("result");
21112121
let address = &operands[0];
2112-
let _length = &operands[1];
2122+
let length = &operands[1];
21132123

21142124
uwrite!(
21152125
self.src,
21162126
"
2117-
let {result} = {ffi_qualifier}ptr2bytes({address})
2118-
"
2127+
ignore({length})
2128+
let {result} = {ffi_qualifier}ptr2bytes({address})
2129+
"
21192130
);
21202131

21212132
results.push(result);
@@ -2132,13 +2143,14 @@ impl Bindgen for FunctionBindgen<'_, '_> {
21322143

21332144
let result = self.locals.tmp("result");
21342145
let address = &operands[0];
2135-
let _length = &operands[1];
2146+
let length = &operands[1];
21362147

21372148
uwrite!(
21382149
self.src,
21392150
"
2140-
let {result} = {ffi_qualifier}ptr2{ty}_array({address})
2141-
"
2151+
ignore({length})
2152+
let {result} = {ffi_qualifier}ptr2{ty}_array({address})
2153+
"
21422154
);
21432155

21442156
results.push(result);
@@ -2159,11 +2171,12 @@ impl Bindgen for FunctionBindgen<'_, '_> {
21592171
Instruction::StringLift { .. } => {
21602172
let result = self.locals.tmp("result");
21612173
let address = &operands[0];
2162-
let _length = &operands[1];
2174+
let length = &operands[1];
21632175

21642176
uwrite!(
21652177
self.src,
21662178
"
2179+
ignore({length})
21672180
let {result} = {ffi_qualifier}ptr2str({address})
21682181
"
21692182
);
@@ -2502,6 +2515,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
25022515
"
25032516
match ({op}) {{
25042517
{cases}
2518+
_ => panic()
25052519
}}
25062520
"
25072521
);
@@ -2750,7 +2764,7 @@ impl ToMoonBitIdent for str {
27502764
"continue" | "for" | "match" | "if" | "pub" | "priv" | "readonly" | "break"
27512765
| "raise" | "try" | "except" | "catch" | "else" | "enum" | "struct" | "type"
27522766
| "trait" | "return" | "let" | "mut" | "while" | "loop" | "extern" | "with"
2753-
| "throw" | "init" | "main" | "test" | "in" => {
2767+
| "throw" | "init" | "main" | "test" | "in" | "guard" => {
27542768
format!("{self}_")
27552769
}
27562770
_ => self.to_snake_case(),

0 commit comments

Comments
 (0)