From 8a9c42a9859f0730719c1d526752b25821305e30 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Mon, 20 Nov 2023 17:10:58 +0800 Subject: [PATCH] fix(napi): compile error for wasm32-unknown-unknown target - Close https://github.com/napi-rs/napi-rs/issues/1816 --- .cargo/config.toml | 2 +- crates/backend/src/codegen/const.rs | 4 +-- crates/backend/src/codegen/enum.rs | 4 +-- crates/backend/src/codegen/fn.rs | 4 +-- crates/backend/src/codegen/struct.rs | 8 +++--- crates/macro/src/lib.rs | 2 +- crates/napi/Cargo.toml | 4 +-- .../bindgen_runtime/js_values/arraybuffer.rs | 8 +++--- .../src/bindgen_runtime/js_values/buffer.rs | 4 +-- crates/napi/src/bindgen_runtime/mod.rs | 2 +- .../src/bindgen_runtime/module_register.rs | 26 +++++++++---------- crates/napi/src/env.rs | 4 +-- crates/napi/src/tokio_runtime.rs | 14 +++++----- examples/napi/Cargo.toml | 4 +-- examples/napi/src/async.rs | 8 +++--- 15 files changed, 49 insertions(+), 49 deletions(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index 8515f874..84e72904 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,2 +1,2 @@ -[target.'cfg(target_os = "wasi")'] +[target.'cfg(target_family = "wasm")'] rustflags = ["-C", "target-feature=+atomics,+bulk-memory"] diff --git a/crates/backend/src/codegen/const.rs b/crates/backend/src/codegen/const.rs index 8d217571..d58b5041 100644 --- a/crates/backend/src/codegen/const.rs +++ b/crates/backend/src/codegen/const.rs @@ -41,7 +41,7 @@ impl NapiConst { } #[allow(non_snake_case)] #[allow(clippy::all)] - #[cfg(all(not(test), not(feature = "noop"), not(target_os = "wasi")))] + #[cfg(all(not(test), not(feature = "noop"), not(target_family = "wasm")))] #[napi::bindgen_prelude::ctor] fn #register_name() { napi::bindgen_prelude::register_module_export(#js_mod_ident, #js_name_lit, #cb_name); @@ -49,7 +49,7 @@ impl NapiConst { #[allow(non_snake_case)] #[allow(clippy::all)] - #[cfg(all(not(test), not(feature = "noop"), target_os = "wasi"))] + #[cfg(all(not(test), not(feature = "noop"), target_family = "wasm"))] #[no_mangle] unsafe extern "C" fn #register_name() { napi::bindgen_prelude::register_module_export(#js_mod_ident, #js_name_lit, #cb_name); diff --git a/crates/backend/src/codegen/enum.rs b/crates/backend/src/codegen/enum.rs index 0be465d8..870d1c2f 100644 --- a/crates/backend/src/codegen/enum.rs +++ b/crates/backend/src/codegen/enum.rs @@ -158,14 +158,14 @@ impl NapiEnum { } #[allow(non_snake_case)] #[allow(clippy::all)] - #[cfg(all(not(test), not(feature = "noop"), not(target_os = "wasi")))] + #[cfg(all(not(test), not(feature = "noop"), not(target_family = "wasm")))] #[napi::bindgen_prelude::ctor] fn #register_name() { napi::bindgen_prelude::register_module_export(#js_mod_ident, #js_name_lit, #callback_name); } #[allow(non_snake_case)] #[allow(clippy::all)] - #[cfg(all(not(test), not(feature = "noop"), target_os = "wasi"))] + #[cfg(all(not(test), not(feature = "noop"), target_family = "wasm"))] #[no_mangle] extern "C" fn #register_name() { napi::bindgen_prelude::register_module_export(#js_mod_ident, #js_name_lit, #callback_name); diff --git a/crates/backend/src/codegen/fn.rs b/crates/backend/src/codegen/fn.rs index 0dba1c26..0998d694 100644 --- a/crates/backend/src/codegen/fn.rs +++ b/crates/backend/src/codegen/fn.rs @@ -598,7 +598,7 @@ impl NapiFn { #[allow(clippy::all)] #[allow(non_snake_case)] - #[cfg(all(not(test), not(feature = "noop"), not(target_os = "wasi")))] + #[cfg(all(not(test), not(feature = "noop"), not(target_family = "wasm")))] #[napi::bindgen_prelude::ctor] fn #module_register_name() { napi::bindgen_prelude::register_module_export(#js_mod_ident, #js_name, #cb_name); @@ -606,7 +606,7 @@ impl NapiFn { #[allow(clippy::all)] #[allow(non_snake_case)] - #[cfg(all(not(test), not(feature = "noop"), target_os = "wasi"))] + #[cfg(all(not(test), not(feature = "noop"), target_family = "wasm"))] #[no_mangle] extern "C" fn #module_register_name() { napi::bindgen_prelude::register_module_export(#js_mod_ident, #js_name, #cb_name); diff --git a/crates/backend/src/codegen/struct.rs b/crates/backend/src/codegen/struct.rs index b39103a0..4ff657eb 100644 --- a/crates/backend/src/codegen/struct.rs +++ b/crates/backend/src/codegen/struct.rs @@ -778,7 +778,7 @@ impl NapiStruct { quote! { #[allow(non_snake_case)] #[allow(clippy::all)] - #[cfg(all(not(test), not(feature = "noop"), not(target_os = "wasi")))] + #[cfg(all(not(test), not(feature = "noop"), not(target_family = "wasm")))] #[napi::bindgen_prelude::ctor] fn #struct_register_name() { napi::__private::register_class(#name_str, #js_mod_ident, #js_name, vec![#(#props),*]); @@ -786,7 +786,7 @@ impl NapiStruct { #[allow(non_snake_case)] #[allow(clippy::all)] - #[cfg(all(not(test), not(feature = "noop"), target_os = "wasi"))] + #[cfg(all(not(test), not(feature = "noop"), target_family = "wasm"))] #[no_mangle] extern "C" fn #struct_register_name() { napi::__private::register_class(#name_str, #js_mod_ident, #js_name, vec![#(#props),*]); @@ -901,13 +901,13 @@ impl NapiImpl { use super::*; #(#methods)* - #[cfg(all(not(test), not(feature = "noop"), not(target_os = "wasi")))] + #[cfg(all(not(test), not(feature = "noop"), not(target_family = "wasm")))] #[napi::bindgen_prelude::ctor] fn #register_name() { napi::__private::register_class(#name_str, #js_mod_ident, #js_name, vec![#(#props),*]); } - #[cfg(all(not(test), not(feature = "noop"), target_os = "wasi"))] + #[cfg(all(not(test), not(feature = "noop"), target_family = "wasm"))] #[no_mangle] extern "C" fn #register_name() { napi::__private::register_class(#name_str, #js_mod_ident, #js_name, vec![#(#props_wasm),*]); diff --git a/crates/macro/src/lib.rs b/crates/macro/src/lib.rs index bd7c09bc..30612ffd 100644 --- a/crates/macro/src/lib.rs +++ b/crates/macro/src/lib.rs @@ -151,7 +151,7 @@ pub fn module_exports(_attr: TokenStream, input: TokenStream) -> TokenStream { }; let register = quote! { - #[cfg_attr(not(target_os = "wasi"), napi::bindgen_prelude::ctor)] + #[cfg_attr(not(target_family = "wasm"), napi::bindgen_prelude::ctor)] fn __napi__explicit_module_register() { unsafe fn register(raw_env: napi::sys::napi_env, raw_exports: napi::sys::napi_value) -> napi::Result<()> { use napi::{Env, JsObject, NapiValue}; diff --git a/crates/napi/Cargo.toml b/crates/napi/Cargo.toml index 833b8a45..bebfa7e5 100644 --- a/crates/napi/Cargo.toml +++ b/crates/napi/Cargo.toml @@ -75,11 +75,11 @@ version = "0.8" optional = true version = "0.4" -[target.'cfg(target_os = "wasi")'.dependencies] +[target.'cfg(target_family = "wasm")'.dependencies] napi-derive = { path = "../macro", version = "2.10.1", default-features = false } tokio = { version = "1", optional = true, features = ["rt", "sync"] } -[target.'cfg(not(target_os = "wasi"))'.dependencies] +[target.'cfg(not(target_family = "wasm"))'.dependencies] tokio = { version = "1", optional = true, features = [ "rt", "rt-multi-thread", diff --git a/crates/napi/src/bindgen_runtime/js_values/arraybuffer.rs b/crates/napi/src/bindgen_runtime/js_values/arraybuffer.rs index 4bbacc12..d3bdde48 100644 --- a/crates/napi/src/bindgen_runtime/js_values/arraybuffer.rs +++ b/crates/napi/src/bindgen_runtime/js_values/arraybuffer.rs @@ -7,14 +7,14 @@ use std::sync::{ Arc, }; -#[cfg(all(feature = "napi4", not(feature = "noop"), not(target_os = "wasi")))] +#[cfg(all(feature = "napi4", not(feature = "noop"), not(target_family = "wasm")))] use crate::bindgen_prelude::{CUSTOM_GC_TSFN, CUSTOM_GC_TSFN_DESTROYED, THREADS_CAN_ACCESS_ENV}; pub use crate::js_values::TypedArrayType; use crate::{check_status, sys, Error, Result, Status}; use super::{FromNapiValue, ToNapiValue, TypeName, ValidateNapiValue}; -#[cfg(target_os = "wasi")] +#[cfg(target_family = "wasm")] extern "C" { fn emnapi_sync_memory( env: crate::sys::napi_env, @@ -80,7 +80,7 @@ macro_rules! impl_typed_array { if ref_.is_null() { return; } - #[cfg(all(feature = "napi4", not(feature = "noop"), not(target_os = "wasi")))] + #[cfg(all(feature = "napi4", not(feature = "noop"), not(target_family = "wasm")))] { if CUSTOM_GC_TSFN_DESTROYED.load(Ordering::SeqCst) { return; @@ -140,7 +140,7 @@ macro_rules! impl_typed_array { impl $name { fn noop_finalize(_data: *mut $rust_type, _length: usize) {} - #[cfg(target_os = "wasi")] + #[cfg(target_family = "wasm")] pub fn sync(&mut self, env: &crate::Env) { if let Some((reference, _)) = self.raw { let mut value = ptr::null_mut(); diff --git a/crates/napi/src/bindgen_runtime/js_values/buffer.rs b/crates/napi/src/bindgen_runtime/js_values/buffer.rs index 13cf98d3..4ef145a0 100644 --- a/crates/napi/src/bindgen_runtime/js_values/buffer.rs +++ b/crates/napi/src/bindgen_runtime/js_values/buffer.rs @@ -9,7 +9,7 @@ use std::sync::Arc; #[cfg(all(debug_assertions, not(windows)))] use std::sync::Mutex; -#[cfg(all(feature = "napi4", not(feature = "noop"), not(target_os = "wasi")))] +#[cfg(all(feature = "napi4", not(feature = "noop"), not(target_family = "wasm")))] use crate::bindgen_prelude::{CUSTOM_GC_TSFN, CUSTOM_GC_TSFN_DESTROYED, THREADS_CAN_ACCESS_ENV}; use crate::{bindgen_prelude::*, check_status, sys, Result, ValueType}; @@ -37,7 +37,7 @@ impl Drop for Buffer { if ref_.is_null() { return; } - #[cfg(all(feature = "napi4", not(feature = "noop"), not(target_os = "wasi")))] + #[cfg(all(feature = "napi4", not(feature = "noop"), not(target_family = "wasm")))] { if CUSTOM_GC_TSFN_DESTROYED.load(std::sync::atomic::Ordering::SeqCst) { return; diff --git a/crates/napi/src/bindgen_runtime/mod.rs b/crates/napi/src/bindgen_runtime/mod.rs index 82dfb3ee..97605869 100644 --- a/crates/napi/src/bindgen_runtime/mod.rs +++ b/crates/napi/src/bindgen_runtime/mod.rs @@ -45,7 +45,7 @@ pub unsafe extern "C" fn raw_finalize_unchecked( { let finalize_callbacks_rc = unsafe { Rc::from_raw(finalize_callbacks_ptr) }; - #[cfg(all(debug_assertions, not(target_os = "wasi")))] + #[cfg(all(debug_assertions, not(target_family = "wasm")))] { let rc_strong_count = Rc::strong_count(&finalize_callbacks_rc); // If `Rc` strong count is 2, it means the finalize of referenced `Object` is called before the `fn drop` of the `Reference` diff --git a/crates/napi/src/bindgen_runtime/module_register.rs b/crates/napi/src/bindgen_runtime/module_register.rs index d7542273..25f78a0f 100644 --- a/crates/napi/src/bindgen_runtime/module_register.rs +++ b/crates/napi/src/bindgen_runtime/module_register.rs @@ -3,10 +3,10 @@ use std::collections::HashMap; use std::collections::HashSet; use std::ffi::CStr; use std::ptr; -#[cfg(all(feature = "napi4", not(target_os = "wasi")))] +#[cfg(all(feature = "napi4", not(target_family = "wasm")))] use std::sync::atomic::AtomicPtr; #[cfg(all( - not(any(target_os = "macos", target_os = "wasi")), + not(any(target_os = "macos", target_family = "wasm")), feature = "napi4", feature = "tokio_rt" ))] @@ -74,12 +74,12 @@ static IS_FIRST_MODULE: AtomicBool = AtomicBool::new(true); static FIRST_MODULE_REGISTERED: AtomicBool = AtomicBool::new(false); static REGISTERED_CLASSES: Lazy = Lazy::new(Default::default); static FN_REGISTER_MAP: Lazy = Lazy::new(Default::default); -#[cfg(all(feature = "napi4", not(feature = "noop"), not(target_os = "wasi")))] +#[cfg(all(feature = "napi4", not(feature = "noop"), not(target_family = "wasm")))] pub(crate) static CUSTOM_GC_TSFN: AtomicPtr = AtomicPtr::new(ptr::null_mut()); -#[cfg(all(feature = "napi4", not(feature = "noop"), not(target_os = "wasi")))] +#[cfg(all(feature = "napi4", not(feature = "noop"), not(target_family = "wasm")))] pub(crate) static CUSTOM_GC_TSFN_DESTROYED: AtomicBool = AtomicBool::new(false); -#[cfg(all(feature = "napi4", not(feature = "noop"), not(target_os = "wasi")))] +#[cfg(all(feature = "napi4", not(feature = "noop"), not(target_family = "wasm")))] // Store thread id of the thread that created the CustomGC ThreadsafeFunction. pub(crate) static THREADS_CAN_ACCESS_ENV: once_cell::sync::Lazy< PersistedPerInstanceHashMap, @@ -251,7 +251,7 @@ fn load_host() { } } -#[cfg(all(target_os = "wasi", not(feature = "noop")))] +#[cfg(all(target_family = "wasm", not(feature = "noop")))] #[no_mangle] unsafe extern "C" fn napi_register_wasm_v1( env: sys::napi_env, @@ -463,7 +463,7 @@ pub unsafe extern "C" fn napi_register_module_v1( } #[cfg(all( - not(any(target_os = "macos", target_os = "wasi")), + not(any(target_os = "macos", target_family = "wasm")), feature = "napi4", feature = "tokio_rt" ))] @@ -482,7 +482,7 @@ pub unsafe extern "C" fn napi_register_module_v1( ) }; } - #[cfg(all(feature = "napi4", not(target_os = "wasi")))] + #[cfg(all(feature = "napi4", not(target_family = "wasm")))] create_custom_gc(env); FIRST_MODULE_REGISTERED.store(true, Ordering::SeqCst); exports @@ -506,7 +506,7 @@ pub(crate) unsafe extern "C" fn noop( ptr::null_mut() } -#[cfg(all(feature = "napi4", not(target_os = "wasi"), not(feature = "noop")))] +#[cfg(all(feature = "napi4", not(target_family = "wasm"), not(feature = "noop")))] fn create_custom_gc(env: sys::napi_env) { use std::os::raw::c_char; @@ -582,19 +582,19 @@ fn create_custom_gc(env: sys::napi_env) { ); } -#[cfg(all(feature = "napi4", not(target_os = "wasi"), not(feature = "noop")))] +#[cfg(all(feature = "napi4", not(target_family = "wasm"), not(feature = "noop")))] unsafe extern "C" fn remove_thread_id(id: *mut std::ffi::c_void) { let thread_id = unsafe { Box::from_raw(id.cast::()) }; THREADS_CAN_ACCESS_ENV.borrow_mut(|m| m.insert(*thread_id, false)); } -#[cfg(all(feature = "napi4", not(target_os = "wasi"), not(feature = "noop")))] +#[cfg(all(feature = "napi4", not(target_family = "wasm"), not(feature = "noop")))] #[allow(unused)] unsafe extern "C" fn empty(env: sys::napi_env, info: sys::napi_callback_info) -> sys::napi_value { ptr::null_mut() } -#[cfg(all(feature = "napi4", not(target_os = "wasi"), not(feature = "noop")))] +#[cfg(all(feature = "napi4", not(target_family = "wasm"), not(feature = "noop")))] #[allow(unused_variables)] unsafe extern "C" fn custom_gc_finalize( env: sys::napi_env, @@ -604,7 +604,7 @@ unsafe extern "C" fn custom_gc_finalize( CUSTOM_GC_TSFN_DESTROYED.store(true, Ordering::SeqCst); } -#[cfg(all(feature = "napi4", not(target_os = "wasi"), not(feature = "noop")))] +#[cfg(all(feature = "napi4", not(target_family = "wasm"), not(feature = "noop")))] // recycle the ArrayBuffer/Buffer Reference if the ArrayBuffer/Buffer is not dropped on the main thread extern "C" fn custom_gc( env: sys::napi_env, diff --git a/crates/napi/src/env.rs b/crates/napi/src/env.rs index bbb1bb36..3e578be3 100644 --- a/crates/napi/src/env.rs +++ b/crates/napi/src/env.rs @@ -374,7 +374,7 @@ impl Env { )) } - #[cfg(not(target_os = "wasi"))] + #[cfg(not(target_family = "wasm"))] /// This function gives V8 an indication of the amount of externally allocated memory that is kept alive by JavaScript objects (i.e. a JavaScript object that points to its own memory allocated by a native module). /// /// Registering externally allocated memory will trigger global garbage collections more often than it would otherwise. @@ -386,7 +386,7 @@ impl Env { Ok(changed) } - #[cfg(target_os = "wasi")] + #[cfg(target_family = "wasm")] #[allow(unused_variables)] pub fn adjust_external_memory(&mut self, size: i64) -> Result { Ok(0) diff --git a/crates/napi/src/tokio_runtime.rs b/crates/napi/src/tokio_runtime.rs index 02fc96d9..4728f6de 100644 --- a/crates/napi/src/tokio_runtime.rs +++ b/crates/napi/src/tokio_runtime.rs @@ -6,13 +6,13 @@ use tokio::runtime::Runtime; use crate::{sys, JsDeferred, JsUnknown, NapiValue, Result}; fn create_runtime() -> Option { - #[cfg(not(target_os = "wasi"))] + #[cfg(not(target_family = "wasm"))] { let runtime = tokio::runtime::Runtime::new().expect("Create tokio runtime failed"); Some(runtime) } - #[cfg(target_os = "wasi")] + #[cfg(target_family = "wasm")] { tokio::runtime::Builder::new_current_thread() .enable_all() @@ -23,14 +23,14 @@ fn create_runtime() -> Option { pub(crate) static RT: Lazy>> = Lazy::new(|| RwLock::new(create_runtime())); -#[cfg(not(any(target_os = "macos", target_os = "wasi")))] +#[cfg(not(any(target_os = "macos", target_family = "wasm")))] static RT_REFERENCE_COUNT: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); /// Ensure that the Tokio runtime is initialized. /// In windows the Tokio runtime will be dropped when Node env exits. /// But in Electron renderer process, the Node env will exits and recreate when the window reloads. /// So we need to ensure that the Tokio runtime is initialized when the Node env is created. -#[cfg(not(any(target_os = "macos", target_os = "wasi")))] +#[cfg(not(any(target_os = "macos", target_family = "wasm")))] pub(crate) fn ensure_runtime() { use std::sync::atomic::Ordering; @@ -42,7 +42,7 @@ pub(crate) fn ensure_runtime() { RT_REFERENCE_COUNT.fetch_add(1, Ordering::Relaxed); } -#[cfg(not(any(target_os = "macos", target_os = "wasi")))] +#[cfg(not(any(target_os = "macos", target_family = "wasm")))] pub(crate) unsafe extern "C" fn drop_runtime(_arg: *mut std::ffi::c_void) { use std::sync::atomic::Ordering; @@ -140,10 +140,10 @@ pub fn execute_tokio_future< } }; - #[cfg(not(target_os = "wasi"))] + #[cfg(not(target_family = "wasm"))] spawn(inner); - #[cfg(target_os = "wasi")] + #[cfg(target_family = "wasm")] { std::thread::spawn(|| { block_on(inner); diff --git a/examples/napi/Cargo.toml b/examples/napi/Cargo.toml index 4f4ad587..311e49a5 100644 --- a/examples/napi/Cargo.toml +++ b/examples/napi/Cargo.toml @@ -20,7 +20,7 @@ serde = "1" serde_derive = "1" serde_json = "1" -[target.'cfg(not(target_os = "wasi"))'.dependencies] +[target.'cfg(not(target_family = "wasm"))'.dependencies] napi = { path = "../../crates/napi", default-features = false, features = [ "napi9", "serde-json", @@ -36,7 +36,7 @@ napi = { path = "../../crates/napi", default-features = false, features = [ ] } tokio = { version = "1", features = ["rt", "time"] } -[target.'cfg(target_os = "wasi")'.dependencies] +[target.'cfg(target_family = "wasm")'.dependencies] napi = { path = "../../crates/napi", default-features = false, features = [ "napi9", "serde-json", diff --git a/examples/napi/src/async.rs b/examples/napi/src/async.rs index 724e3909..dde847fc 100644 --- a/examples/napi/src/async.rs +++ b/examples/napi/src/async.rs @@ -1,13 +1,13 @@ -#[cfg(not(target_os = "wasi"))] +#[cfg(not(target_family = "wasm"))] use futures::prelude::*; use napi::bindgen_prelude::*; use napi::tokio; -#[cfg(not(target_os = "wasi"))] +#[cfg(not(target_family = "wasm"))] use napi::tokio::fs; #[napi] async fn read_file_async(path: String) -> Result { - #[cfg(not(target_os = "wasi"))] + #[cfg(not(target_family = "wasm"))] { fs::read(path) .map(|r| match r { @@ -19,7 +19,7 @@ async fn read_file_async(path: String) -> Result { }) .await } - #[cfg(target_os = "wasi")] + #[cfg(target_family = "wasm")] { let conetent = std::fs::read(path)?; Ok(conetent.into())