Skip to content

Commit a3612b5

Browse files
authored
Merge pull request #32 from dhil/wasmfx-merge
Merge with upstream
2 parents 680f786 + b9496e0 commit a3612b5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+7612
-4049
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 18 additions & 15 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.36.0"
21+
version = "0.37.0"
2222
license = "Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT"
2323
repository = "https://github.com/bytecodealliance/wasi-rs"
2424

@@ -31,22 +31,23 @@ clap = { version = "4.3.19", features = ["derive"] }
3131
indexmap = "2.0.0"
3232
prettyplease = "0.2.20"
3333
syn = { version = "2.0.89", features = ["printing"] }
34+
futures = "0.3.31"
3435

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

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

5152
[[bin]]
5253
name = "wit-bindgen"
@@ -74,6 +75,7 @@ default = [
7475
'go',
7576
'csharp',
7677
'moonbit',
78+
'async',
7779
]
7880
c = ['dep:wit-bindgen-c']
7981
rust = ['dep:wit-bindgen-rust']
@@ -83,6 +85,7 @@ go = ['dep:wit-bindgen-go']
8385
csharp = ['dep:wit-bindgen-csharp']
8486
csharp-mono = ['csharp']
8587
moonbit = ['dep:wit-bindgen-moonbit']
88+
async = []
8689

8790
[dev-dependencies]
8891
heck = { workspace = true }

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,56 @@ Like with Rust, you can then inspect the output binary:
309309
wasm-tools component wit ./my-component.wasm
310310
```
311311

312+
### Guest C#
313+
314+
To generate the bindings:
315+
316+
```
317+
wit-bindgen c-sharp -w command -r native-aot --generate-stub wit/
318+
```
319+
320+
Now you create a c# project file:
321+
322+
```
323+
dotnet new console -o MyApp
324+
cd MyApp
325+
dotnet new nugetconfig
326+
```
327+
328+
In the `nuget.config` after `<clear />`make sure you have:
329+
330+
```
331+
<add key="dotnet-experimental" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-experimental/nuget/v3/index.json" />
332+
<add key="nuget" value="https://api.nuget.org/v3/index.json" />
333+
```
334+
335+
In the MyApp.csproj add the following to the property group:
336+
337+
```
338+
<RuntimeIdentifier>wasi-wasm</RuntimeIdentifier>
339+
<UseAppHost>false</UseAppHost>
340+
<PublishTrimmed>true</PublishTrimmed>
341+
<InvariantGlobalization>true</InvariantGlobalization>
342+
<SelfContained>true</SelfContained>
343+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
344+
<WASI_SDK_PATH>path/to/wasi-sdk</WASI_SDK_PATH>
345+
```
346+
347+
Add the native-aot compiler (substitute `win-x64` for `linux-x64` on Linux):
348+
349+
```
350+
dotnet add package Microsoft.DotNet.ILCompiler.LLVM --prerelease
351+
dotnet add package runtime.win-x64.Microsoft.DotNet.ILCompiler.LLVM --prerelease
352+
```
353+
354+
Now you can build with:
355+
356+
```
357+
dotnet publish
358+
```
359+
360+
Checkout out [componentize-dotnet](https://github.com/bytecodealliance/componentize-dotnet) for a simplified experience.
361+
312362
### Guest: Java
313363

314364
Java bytecode can be compiled to WebAssembly using

ci/rebuild-libcabi-realloc.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
# the `cabi_realloc` symbol from the final binary. This library, however,
88
# is not stable which means that we're publishing new versions of `wit-bindgen`
99
# over its development. This means that if `wit-bindgen`-the-crate were to
10-
# export a `#[no_mangle]` symbol of `cabi_realloc` then it wouldn't work to
11-
# have two versions of `wit-bindgen` in the same project. This can arise
12-
# relatively quickly, however, so this is something we want to solve.
10+
# export a `#[unsafe(no_mangle)]` symbol of `cabi_realloc` then it wouldn't
11+
# work to have two versions of `wit-bindgen` in the same project. This can
12+
# arise relatively quickly, however, so this is something we want to solve.
1313
#
1414
# The general idea of the solution here is to ensure that the `cabi_realloc`
1515
# symbol itself is declared as a weak symbol. A weakly-defined symbol means
@@ -46,7 +46,7 @@ sym=cabi_realloc_wit_bindgen_$version
4646
cat >./crates/guest-rust/rt/src/cabi_realloc.rs <<-EOF
4747
// This file is generated by $0
4848
49-
#[no_mangle]
49+
#[unsafe(no_mangle)]
5050
pub unsafe extern "C" fn $sym(
5151
old_ptr: *mut u8,
5252
old_len: usize,

crates/c/src/lib.rs

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,7 @@ fn is_prim_type_id(resolve: &Resolve, id: TypeId) -> bool {
718718
| TypeDefKind::Result(_)
719719
| TypeDefKind::Future(_)
720720
| TypeDefKind::Stream(_)
721+
| TypeDefKind::ErrorContext
721722
| TypeDefKind::Unknown => false,
722723
}
723724
}
@@ -779,8 +780,9 @@ pub fn push_ty_name(resolve: &Resolve, ty: &Type, src: &mut String) {
779780
src.push_str("list_");
780781
push_ty_name(resolve, ty, src);
781782
}
782-
TypeDefKind::Future(_) => unimplemented!(),
783-
TypeDefKind::Stream(_) => unimplemented!(),
783+
TypeDefKind::Future(_) => todo!(),
784+
TypeDefKind::Stream(_) => todo!(),
785+
TypeDefKind::ErrorContext => todo!(),
784786
TypeDefKind::Handle(Handle::Own(resource)) => {
785787
src.push_str("own_");
786788
push_ty_name(resolve, &Type::Id(*resource), src);
@@ -893,7 +895,12 @@ pub fn c_func_name(
893895
!in_import,
894896
renamed_interfaces,
895897
)),
896-
None => name.push_str(&world.to_snake_case()),
898+
None => {
899+
if !in_import {
900+
name.push_str("exports_");
901+
}
902+
name.push_str(&world.to_snake_case());
903+
}
897904
}
898905
name.push_str("_");
899906
name.push_str(&func.name.to_snake_case().replace('.', "_"));
@@ -992,6 +999,7 @@ impl Return {
992999

9931000
TypeDefKind::Future(_) => todo!("return_single for future"),
9941001
TypeDefKind::Stream(_) => todo!("return_single for stream"),
1002+
TypeDefKind::ErrorContext => todo!("return_single for error-context"),
9951003
TypeDefKind::Resource => todo!("return_single for resource"),
9961004
TypeDefKind::Unknown => unreachable!(),
9971005
}
@@ -1339,6 +1347,21 @@ void __wasm_export_{ns}_{snake}_dtor({ns}_{snake}_t* arg) {{
13391347
self.finish_typedef_struct(id);
13401348
}
13411349

1350+
fn type_future(&mut self, id: TypeId, name: &str, ty: &Option<Type>, docs: &Docs) {
1351+
_ = (id, name, ty, docs);
1352+
todo!()
1353+
}
1354+
1355+
fn type_stream(&mut self, id: TypeId, name: &str, ty: &Type, docs: &Docs) {
1356+
_ = (id, name, ty, docs);
1357+
todo!()
1358+
}
1359+
1360+
fn type_error_context(&mut self, id: TypeId, name: &str, docs: &Docs) {
1361+
_ = (id, name, docs);
1362+
todo!()
1363+
}
1364+
13421365
fn type_builtin(&mut self, id: TypeId, name: &str, ty: &Type, docs: &Docs) {
13431366
let _ = (id, name, ty, docs);
13441367
}
@@ -1427,12 +1450,16 @@ impl<'a> wit_bindgen_core::AnonymousTypeGenerator<'a> for InterfaceGenerator<'a>
14271450
todo!("print_anonymous_type for future");
14281451
}
14291452

1430-
fn anonymous_type_stream(&mut self, _id: TypeId, _ty: &Stream, _docs: &Docs) {
1453+
fn anonymous_type_stream(&mut self, _id: TypeId, _ty: &Type, _docs: &Docs) {
14311454
todo!("print_anonymous_type for stream");
14321455
}
14331456

1434-
fn anonymous_typ_type(&mut self, _id: TypeId, _ty: &Type, _docs: &Docs) {
1435-
todo!("print_anonymous_type for typ");
1457+
fn anonymous_type_error_context(&mut self) {
1458+
todo!("print_anonymous_type for error-context");
1459+
}
1460+
1461+
fn anonymous_type_type(&mut self, _id: TypeId, _ty: &Type, _docs: &Docs) {
1462+
todo!("print_anonymous_type for type");
14361463
}
14371464
}
14381465

@@ -1605,6 +1632,7 @@ impl InterfaceGenerator<'_> {
16051632
}
16061633
TypeDefKind::Future(_) => todo!("print_dtor for future"),
16071634
TypeDefKind::Stream(_) => todo!("print_dtor for stream"),
1635+
TypeDefKind::ErrorContext => todo!("print_dtor for error-context"),
16081636
TypeDefKind::Resource => {}
16091637
TypeDefKind::Handle(Handle::Borrow(id) | Handle::Own(id)) => {
16101638
self.free(&Type::Id(*id), "*ptr");
@@ -1750,6 +1778,7 @@ impl InterfaceGenerator<'_> {
17501778
LiftLower::LowerArgsLiftResults,
17511779
func,
17521780
&mut f,
1781+
false,
17531782
);
17541783

17551784
let FunctionBindgen {
@@ -1822,6 +1851,7 @@ impl InterfaceGenerator<'_> {
18221851
LiftLower::LiftArgsLowerResults,
18231852
func,
18241853
&mut f,
1854+
false,
18251855
);
18261856
let FunctionBindgen { src, .. } = f;
18271857
self.src.c_adapters(&src);
@@ -1852,7 +1882,7 @@ impl InterfaceGenerator<'_> {
18521882

18531883
let mut f = FunctionBindgen::new(self, c_sig, &import_name);
18541884
f.params = params;
1855-
abi::post_return(f.gen.resolve, func, &mut f);
1885+
abi::post_return(f.gen.resolve, func, &mut f, false);
18561886
let FunctionBindgen { src, .. } = f;
18571887
self.src.c_fns(&src);
18581888
self.src.c_fns("}\n");
@@ -2075,17 +2105,8 @@ impl InterfaceGenerator<'_> {
20752105

20762106
TypeDefKind::List(ty) => self.contains_droppable_borrow(ty),
20772107

2078-
TypeDefKind::Future(r) => r
2079-
.as_ref()
2080-
.map_or(false, |ty| self.contains_droppable_borrow(ty)),
2081-
2082-
TypeDefKind::Stream(s) => {
2083-
s.element
2084-
.as_ref()
2085-
.map_or(false, |ty| self.contains_droppable_borrow(ty))
2086-
|| s.end
2087-
.as_ref()
2088-
.map_or(false, |ty| self.contains_droppable_borrow(ty))
2108+
TypeDefKind::Future(_) | TypeDefKind::Stream(_) | TypeDefKind::ErrorContext => {
2109+
false
20892110
}
20902111

20912112
TypeDefKind::Type(ty) => self.contains_droppable_borrow(ty),
@@ -2753,7 +2774,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
27532774
self.src.push_str(");\n");
27542775
}
27552776

2756-
Instruction::CallInterface { func } => {
2777+
Instruction::CallInterface { func, .. } => {
27572778
let mut args = String::new();
27582779
for (i, (op, (byref, _))) in operands.iter().zip(&self.sig.params).enumerate() {
27592780
if i > 0 {
@@ -3037,6 +3058,10 @@ impl Bindgen for FunctionBindgen<'_, '_> {
30373058
uwriteln!(self.src, "}}");
30383059
}
30393060

3061+
Instruction::Flush { amt } => {
3062+
results.extend(operands.iter().take(*amt).map(|v| v.clone()));
3063+
}
3064+
30403065
i => unimplemented!("{:?}", i),
30413066
}
30423067
}
@@ -3145,6 +3170,7 @@ pub fn is_arg_by_pointer(resolve: &Resolve, ty: &Type) -> bool {
31453170
TypeDefKind::Tuple(_) | TypeDefKind::Record(_) | TypeDefKind::List(_) => true,
31463171
TypeDefKind::Future(_) => todo!("is_arg_by_pointer for future"),
31473172
TypeDefKind::Stream(_) => todo!("is_arg_by_pointer for stream"),
3173+
TypeDefKind::ErrorContext => todo!("is_arg_by_pointer for error-context"),
31483174
TypeDefKind::Resource => todo!("is_arg_by_pointer for resource"),
31493175
TypeDefKind::Unknown => unreachable!(),
31503176
},

crates/c/tests/codegen.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ use std::process::Command;
66
use wit_parser::{Resolve, UnresolvedPackageGroup};
77

88
macro_rules! codegen_test {
9+
// TODO: implement support for stream, future, and error-context, and then
10+
// remove these lines:
11+
(streams $name:tt $test:tt) => {};
12+
(futures $name:tt $test:tt) => {};
13+
(resources_with_streams $name:tt $test:tt) => {};
14+
(resources_with_futures $name:tt $test:tt) => {};
15+
(error_context $name:tt $test:tt) => {};
16+
917
($id:ident $name:tt $test:tt) => {
1018
#[test]
1119
fn $id() {

0 commit comments

Comments
 (0)