Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
cpetig committed Oct 7, 2024
2 parents ee1d0c2 + 66bfda3 commit ad27e9c
Show file tree
Hide file tree
Showing 10 changed files with 246 additions and 182 deletions.
315 changes: 178 additions & 137 deletions Cargo.lock

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ resolver = "2"

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

Expand All @@ -38,15 +38,15 @@ wasm-metadata = { git = "https://github.com/cpetig/wasm-tools", branch = "symmet
wit-parser = { git = "https://github.com/cpetig/wasm-tools", branch = "symmetric" }
wit-component = { git = "https://github.com/cpetig/wasm-tools", branch = "symmetric" }

wit-bindgen-core = { path = 'crates/core', version = '0.32.0' }
wit-bindgen-c = { path = 'crates/c', version = '0.32.0' }
wit-bindgen-rust = { path = "crates/rust", version = "0.32.0" }
wit-bindgen-teavm-java = { path = 'crates/teavm-java', version = '0.32.0' }
wit-bindgen-go = { path = 'crates/go', version = '0.32.0' }
wit-bindgen-csharp = { path = 'crates/csharp', version = '0.32.0' }
wit-bindgen-markdown = { path = 'crates/markdown', version = '0.32.0' }
wit-bindgen-moonbit = { path = 'crates/moonbit', version = '0.32.0' }
wit-bindgen = { path = 'crates/guest-rust', version = '0.32.0', default-features = false }
wit-bindgen-core = { path = 'crates/core', version = '0.33.0' }
wit-bindgen-c = { path = 'crates/c', version = '0.33.0' }
wit-bindgen-rust = { path = "crates/rust", version = "0.33.0" }
wit-bindgen-teavm-java = { path = 'crates/teavm-java', version = '0.33.0' }
wit-bindgen-go = { path = 'crates/go', version = '0.33.0' }
wit-bindgen-csharp = { path = 'crates/csharp', version = '0.33.0' }
wit-bindgen-markdown = { path = 'crates/markdown', version = '0.33.0' }
wit-bindgen-moonbit = { path = 'crates/moonbit', version = '0.33.0' }
wit-bindgen = { path = 'crates/guest-rust', version = '0.33.0', default-features = false }

wit-bindgen-cpp = { path = 'crates/cpp', version = '0.3.0' }
wit-bindgen-bridge = { path = 'crates/bridge', version = '0.1.0' }
Expand Down
4 changes: 2 additions & 2 deletions crates/guest-rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ Used when compiling Rust programs to the component model.
"""

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

[features]
default = ["macros", "realloc"]
Expand Down
4 changes: 2 additions & 2 deletions crates/guest-rust/rt/src/cabi_realloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

#include <stdint.h>

extern void *cabi_realloc_wit_bindgen_0_32_0(void *ptr, size_t old_size, size_t align, size_t new_size);
extern void *cabi_realloc_wit_bindgen_0_33_0(void *ptr, size_t old_size, size_t align, size_t new_size);

__attribute__((__weak__, __export_name__("cabi_realloc")))
void *cabi_realloc(void *ptr, size_t old_size, size_t align, size_t new_size) {
return cabi_realloc_wit_bindgen_0_32_0(ptr, old_size, align, new_size);
return cabi_realloc_wit_bindgen_0_33_0(ptr, old_size, align, new_size);
}
Binary file modified crates/guest-rust/rt/src/cabi_realloc.o
Binary file not shown.
2 changes: 1 addition & 1 deletion crates/guest-rust/rt/src/cabi_realloc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This file is generated by ./ci/rebuild-libcabi-realloc.sh

#[no_mangle]
pub unsafe extern "C" fn cabi_realloc_wit_bindgen_0_32_0(
pub unsafe extern "C" fn cabi_realloc_wit_bindgen_0_33_0(
old_ptr: *mut u8,
old_len: usize,
align: usize,
Expand Down
Binary file modified crates/guest-rust/rt/src/libwit_bindgen_cabi_realloc.a
Binary file not shown.
70 changes: 42 additions & 28 deletions crates/moonbit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,34 +111,34 @@ extern "wasm" fn copy_inline(dest : Int, src : Int, len : Int) =
#|(func (param i32) (param i32) (param i32) local.get 0 local.get 1 local.get 2 memory.copy)
pub extern "wasm" fn str2ptr(str : String) -> Int =
#|(func (param i32) (result i32) local.get 0 call $moonbit.decref local.get 0 i32.const 8 i32.add)
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
pub extern "wasm" fn ptr2str(ptr : Int) -> String =
#|(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)
pub extern "wasm" fn bytes2ptr(bytes : Bytes) -> Int =
#|(func (param i32) (result i32) local.get 0 call $moonbit.decref local.get 0 i32.const 8 i32.add)
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
pub extern "wasm" fn ptr2bytes(ptr : Int) -> Bytes =
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.sub)
pub extern "wasm" fn uint_array2ptr(array : FixedArray[UInt]) -> Int =
#|(func (param i32) (result i32) local.get 0 call $moonbit.decref local.get 0 i32.const 8 i32.add)
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
pub extern "wasm" fn uint64_array2ptr(array : FixedArray[UInt64]) -> Int =
#|(func (param i32) (result i32) local.get 0 call $moonbit.decref local.get 0 i32.const 8 i32.add)
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
pub extern "wasm" fn int_array2ptr(array : FixedArray[Int]) -> Int =
#|(func (param i32) (result i32) local.get 0 call $moonbit.decref local.get 0 i32.const 8 i32.add)
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
pub extern "wasm" fn int64_array2ptr(array : FixedArray[Int64]) -> Int =
#|(func (param i32) (result i32) local.get 0 call $moonbit.decref local.get 0 i32.const 8 i32.add)
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
pub extern "wasm" fn float_array2ptr(array : FixedArray[Float]) -> Int =
#|(func (param i32) (result i32) local.get 0 call $moonbit.decref local.get 0 i32.const 8 i32.add)
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
pub extern "wasm" fn double_array2ptr(array : FixedArray[Double]) -> Int =
#|(func (param i32) (result i32) local.get 0 call $moonbit.decref local.get 0 i32.const 8 i32.add)
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
pub extern "wasm" fn ptr2uint_array(ptr : Int) -> FixedArray[UInt] =
#|(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)
Expand Down Expand Up @@ -575,11 +575,17 @@ impl WorldGenerator for MoonBit {
{ffi_qualifier}free(src_offset)
dst
}}
let return_area : Int = {ffi_qualifier}malloc({})
",
self.return_area_size.size_wasm32(),
"
);
if self.return_area_size != 0 {
uwriteln!(
&mut body,
"
let return_area : Int = {ffi_qualifier}malloc({})
",
self.return_area_size,
);
}
files.push(&format!("{EXPORT_DIR}/ffi.mbt"), indent(&body).as_bytes());
self.export
.insert("cabi_realloc".into(), "cabi_realloc".into());
Expand Down Expand Up @@ -798,7 +804,7 @@ impl InterfaceGenerator<'_> {
.collect::<Vec<_>>()
.join(", ");

let sig = self.sig_string(func);
let sig = self.sig_string(func, false);

uwriteln!(
self.ffi,
Expand All @@ -821,7 +827,7 @@ impl InterfaceGenerator<'_> {
fn export(&mut self, interface_name: Option<&str>, func: &Function) {
let sig = self.resolve.wasm_signature(AbiVariant::GuestExport, func);

let func_sig = self.sig_string(func);
let func_sig = self.sig_string(func, true);

let export_name = func.core_export_name(interface_name);

Expand Down Expand Up @@ -1041,7 +1047,7 @@ impl InterfaceGenerator<'_> {
}
}

fn sig_string(&mut self, func: &Function) -> String {
fn sig_string(&mut self, func: &Function, ignore_param: bool) -> String {
let name = match func.kind {
FunctionKind::Freestanding => func.name.to_moonbit_ident(),
FunctionKind::Constructor(_) => {
Expand Down Expand Up @@ -1076,7 +1082,11 @@ impl InterfaceGenerator<'_> {
.iter()
.map(|(name, ty)| {
let ty = self.type_name(ty, true);
let name = name.to_moonbit_ident();
let name = if ignore_param {
format!("_{}", name.to_moonbit_ident())
} else {
name.to_moonbit_ident()
};
format!("{name} : {ty}")
})
.collect::<Vec<_>>()
Expand Down Expand Up @@ -1204,7 +1214,7 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
&mut self.stub,
r#"
/// Destructor of the resource.
pub fn {name}::dtor(self : {name}) -> Unit {{
pub fn {name}::dtor(_self : {name}) -> Unit {{
abort("todo")
}}
"#
Expand Down Expand Up @@ -1572,8 +1582,10 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
.collect::<Vec<_>>()
.join(", ");

let payload = if self.gen.non_empty_type(ty.as_ref()).is_some() || is_result {
let payload = if self.gen.non_empty_type(ty.as_ref()).is_some() {
payload
} else if is_result {
format!("_{payload}")
} else {
String::new()
};
Expand Down Expand Up @@ -1603,7 +1615,6 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
r#"
match {op} {{
{cases}
_ => panic()
}}
"#
);
Expand All @@ -1613,7 +1624,6 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
r#"
let ({declarations}) = match {op} {{
{cases}
_ => panic()
}}
"#
);
Expand Down Expand Up @@ -2111,13 +2121,14 @@ impl Bindgen for FunctionBindgen<'_, '_> {
Type::U8 => {
let result = self.locals.tmp("result");
let address = &operands[0];
let _length = &operands[1];
let length = &operands[1];

uwrite!(
self.src,
"
let {result} = {ffi_qualifier}ptr2bytes({address})
"
ignore({length})
let {result} = {ffi_qualifier}ptr2bytes({address})
"
);

results.push(result);
Expand All @@ -2134,13 +2145,14 @@ impl Bindgen for FunctionBindgen<'_, '_> {

let result = self.locals.tmp("result");
let address = &operands[0];
let _length = &operands[1];
let length = &operands[1];

uwrite!(
self.src,
"
let {result} = {ffi_qualifier}ptr2{ty}_array({address})
"
ignore({length})
let {result} = {ffi_qualifier}ptr2{ty}_array({address})
"
);

results.push(result);
Expand All @@ -2161,11 +2173,12 @@ impl Bindgen for FunctionBindgen<'_, '_> {
Instruction::StringLift { .. } => {
let result = self.locals.tmp("result");
let address = &operands[0];
let _length = &operands[1];
let length = &operands[1];

uwrite!(
self.src,
"
ignore({length})
let {result} = {ffi_qualifier}ptr2str({address})
"
);
Expand Down Expand Up @@ -2518,6 +2531,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
"
match ({op}) {{
{cases}
_ => panic()
}}
"
);
Expand Down Expand Up @@ -2782,7 +2796,7 @@ impl ToMoonBitIdent for str {
"continue" | "for" | "match" | "if" | "pub" | "priv" | "readonly" | "break"
| "raise" | "try" | "except" | "catch" | "else" | "enum" | "struct" | "type"
| "trait" | "return" | "let" | "mut" | "while" | "loop" | "extern" | "with"
| "throw" | "init" | "main" | "test" | "in" => {
| "throw" | "init" | "main" | "test" | "in" | "guard" => {
format!("{self}_")
}
_ => self.to_snake_case(),
Expand Down
9 changes: 9 additions & 0 deletions crates/moonbit/tests/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ test_helpers::codegen_tests!();
fn verify(dir: &Path, _name: &str) {
let mut cmd = Command::new("moon");
cmd.arg("check")
.arg("--target")
.arg("wasm")
.arg("--deny-warn")
.arg("--source-dir")
.arg(dir);

test_helpers::run_command(&mut cmd);
let mut cmd = Command::new("moon");
cmd.arg("build")
.arg("--target")
.arg("wasm")
.arg("--source-dir")
Expand Down
4 changes: 2 additions & 2 deletions tests/runtime/flavorful.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl test_imports::Host for MyImports {
return Ok(());
}
test_imports::MyErrno::A.to_string();
format!("{:?}", test_imports::MyErrno::A);
let _ = format!("{:?}", test_imports::MyErrno::A);
fn assert_error<T: std::error::Error>() {}
assert_error::<test_imports::MyErrno>();
self.errored = true;
Expand Down Expand Up @@ -162,7 +162,7 @@ fn run_test(exports: Flavorful, store: &mut Store<crate::Wasi<MyImports>>) -> Re

assert!(exports.call_errno_result(&mut *store)?.is_err());
MyErrno::A.to_string();
format!("{:?}", MyErrno::A);
let _ = format!("{:?}", MyErrno::A);
fn assert_error<T: std::error::Error>() {}
assert_error::<MyErrno>();

Expand Down

0 comments on commit ad27e9c

Please sign in to comment.