From 667a4f72ec91103cfc957e8c8991c96f1030bb01 Mon Sep 17 00:00:00 2001 From: Greg Johnston Date: Thu, 12 Jan 2023 07:47:37 -0500 Subject: [PATCH 1/4] Add WASI check to Emscripten check --- crates/backend/src/codegen.rs | 26 +++++++++++++------------- src/lib.rs | 14 ++++++++++---- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/crates/backend/src/codegen.rs b/crates/backend/src/codegen.rs index b0ff20e7d27..681057952fd 100644 --- a/crates/backend/src/codegen.rs +++ b/crates/backend/src/codegen.rs @@ -190,12 +190,12 @@ impl ToTokens for ast::Struct { let ptr = wasm_bindgen::convert::IntoWasmAbi::into_abi(value); #[link(wasm_import_module = "__wbindgen_placeholder__")] - #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] + #[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))] extern "C" { fn #new_fn(ptr: u32) -> u32; } - #[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))] + #[cfg(not(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi")))))] unsafe fn #new_fn(_: u32) -> u32 { panic!("cannot convert to JsValue outside of the wasm target") } @@ -207,7 +207,7 @@ impl ToTokens for ast::Struct { } } - #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] + #[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))] #[automatically_derived] const _: () = { #[no_mangle] @@ -295,7 +295,7 @@ impl ToTokens for ast::StructField { (quote! { #[automatically_derived] const _: () = { - #[cfg_attr(all(target_arch = "wasm32", not(target_os = "emscripten")), no_mangle)] + #[cfg_attr(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))), no_mangle)] #[doc(hidden)] pub unsafe extern "C" fn #getter(js: u32) -> <#ty as wasm_bindgen::convert::IntoWasmAbi>::Abi @@ -329,7 +329,7 @@ impl ToTokens for ast::StructField { } (quote! { - #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] + #[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))] #[automatically_derived] const _: () = { #[no_mangle] @@ -555,7 +555,7 @@ impl TryToTokens for ast::Export { const _: () = { #(#attrs)* #[cfg_attr( - all(target_arch = "wasm32", not(target_os = "emscripten")), + all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))), export_name = #export_name, )] pub unsafe extern "C" fn #generated_name(#(#args),*) -> #projection::Abi { @@ -803,11 +803,11 @@ impl ToTokens for ast::ImportType { impl JsCast for #rust_name { fn instanceof(val: &JsValue) -> bool { #[link(wasm_import_module = "__wbindgen_placeholder__")] - #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] + #[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))] extern "C" { fn #instanceof_shim(val: u32) -> u32; } - #[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))] + #[cfg(not(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi")))))] unsafe fn #instanceof_shim(_: u32) -> u32 { panic!("cannot check instanceof on non-wasm targets"); } @@ -1145,14 +1145,14 @@ impl TryToTokens for ast::ImportFunction { // the best we can in the meantime. let extern_fn = respan( quote! { - #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] + #[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))] #(#attrs)* #[link(wasm_import_module = "__wbindgen_placeholder__")] extern "C" { fn #import_name(#(#abi_arguments),*) -> #abi_ret; } - #[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))] + #[cfg(not(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi")))))] unsafe fn #import_name(#(#abi_arguments),*) -> #abi_ret { #( drop(#abi_argument_names); @@ -1315,11 +1315,11 @@ impl ToTokens for ast::ImportStatic { #vis static #name: wasm_bindgen::JsStatic<#ty> = { fn init() -> #ty { #[link(wasm_import_module = "__wbindgen_placeholder__")] - #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] + #[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))] extern "C" { fn #shim_name() -> <#ty as wasm_bindgen::convert::FromWasmAbi>::Abi; } - #[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))] + #[cfg(not(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi")))))] unsafe fn #shim_name() -> <#ty as wasm_bindgen::convert::FromWasmAbi>::Abi { panic!("cannot access imported statics on non-wasm targets") } @@ -1381,7 +1381,7 @@ impl<'a, T: ToTokens> ToTokens for Descriptor<'a, T> { let inner = &self.inner; let attrs = &self.attrs; (quote! { - #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] + #[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))] #[automatically_derived] const _: () = { #(#attrs)* diff --git a/src/lib.rs b/src/lib.rs index 50920321b37..a58c45a3f7e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,14 +28,14 @@ macro_rules! if_std { macro_rules! externs { ($(#[$attr:meta])* extern "C" { $(fn $name:ident($($args:tt)*) -> $ret:ty;)* }) => ( - #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] + #[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))] $(#[$attr])* extern "C" { $(fn $name($($args)*) -> $ret;)* } $( - #[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))] + #[cfg(not(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi")))))] #[allow(unused_variables)] unsafe extern fn $name($($args)*) -> $ret { panic!("function not implemented on non-wasm32 targets") @@ -1318,7 +1318,10 @@ pub trait UnwrapThrowExt: Sized { impl UnwrapThrowExt for Option { fn expect_throw(self, message: &str) -> T { - if cfg!(all(target_arch = "wasm32", not(target_os = "emscripten"))) { + if cfg!(all( + target_arch = "wasm32", + not(any(target_os = "emscripten", target_os = "wasi")) + )) { match self { Some(val) => val, None => throw_str(message), @@ -1334,7 +1337,10 @@ where E: core::fmt::Debug, { fn expect_throw(self, message: &str) -> T { - if cfg!(all(target_arch = "wasm32", not(target_os = "emscripten"))) { + if cfg!(all( + target_arch = "wasm32", + not(any(target_os = "emscripten", target_os = "wasi")) + )) { match self { Ok(val) => val, Err(_) => throw_str(message), From 9991fd57ef116af2ec0f584a121a3332f1e7e37c Mon Sep 17 00:00:00 2001 From: Greg Johnston Date: Sun, 6 Aug 2023 20:56:29 -0400 Subject: [PATCH 2/4] add note to CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cb435f8dd9e..7ee63b4ec91 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,10 @@ It was also automatically changed for `IdbFileHandle`, which is deprecated. [#3537](https://github.com/rustwasm/wasm-bindgen/pull/3537) +* Changed behavior when compiling to `wasm32-wasi` to match `wasm32-emscripten` and + non-WASM targets, generating a stub that panics when called rather than a wasm- + bindgen placeholder. [#3233](https://github.com/rustwasm/wasm-bindgen/pull/3233) + ### Fixed * Fixed bindings and comments for `Atomics.wait`. From 8c6923d7018b48c08ff7dd4a703629ea159d6b9f Mon Sep 17 00:00:00 2001 From: Greg Johnston Date: Sun, 6 Aug 2023 20:59:28 -0400 Subject: [PATCH 3/4] typo --- crates/backend/src/codegen.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/backend/src/codegen.rs b/crates/backend/src/codegen.rs index 952d7b61193..08e6c31869c 100644 --- a/crates/backend/src/codegen.rs +++ b/crates/backend/src/codegen.rs @@ -542,7 +542,7 @@ impl TryToTokens for ast::Export { quote! { () }, quote! { <#syn_ret as #wasm_bindgen::__rt::Start>::start(#ret) }, ) - } else {fle + } else { (quote! { #syn_ret }, quote! { #syn_ret }, quote! { #ret }) }; From 633b48f0e4242f15fed0dbf56d3ba0e8b2c720a8 Mon Sep 17 00:00:00 2001 From: daxpedda Date: Mon, 7 Aug 2023 12:59:25 +0200 Subject: [PATCH 4/4] Fix changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ee63b4ec91..3185009da14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,7 +42,8 @@ * Changed behavior when compiling to `wasm32-wasi` to match `wasm32-emscripten` and non-WASM targets, generating a stub that panics when called rather than a wasm- - bindgen placeholder. [#3233](https://github.com/rustwasm/wasm-bindgen/pull/3233) + bindgen placeholder. + [#3233](https://github.com/rustwasm/wasm-bindgen/pull/3233) ### Fixed