fix(napi): compile error for wasm32-unknown-unknown target

- Close https://github.com/napi-rs/napi-rs/issues/1816
This commit is contained in:
LongYinan 2023-11-20 17:10:58 +08:00 committed by GitHub
parent 7dced934a7
commit 8a9c42a985
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 49 additions and 49 deletions

View file

@ -1,2 +1,2 @@
[target.'cfg(target_os = "wasi")']
[target.'cfg(target_family = "wasm")']
rustflags = ["-C", "target-feature=+atomics,+bulk-memory"]

View file

@ -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);

View file

@ -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);

View file

@ -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);

View file

@ -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),*]);

View file

@ -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};

View file

@ -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",

View file

@ -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();

View file

@ -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;

View file

@ -45,7 +45,7 @@ pub unsafe extern "C" fn raw_finalize_unchecked<T: ObjectFinalize>(
{
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`

View file

@ -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<RegisteredClassesMap> = Lazy::new(Default::default);
static FN_REGISTER_MAP: Lazy<FnRegisterMap> = 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<sys::napi_threadsafe_function__> =
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<ThreadId, bool>,
@ -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::<ThreadId>()) };
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,

View file

@ -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<i64> {
Ok(0)

View file

@ -6,13 +6,13 @@ use tokio::runtime::Runtime;
use crate::{sys, JsDeferred, JsUnknown, NapiValue, Result};
fn create_runtime() -> Option<Runtime> {
#[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<Runtime> {
pub(crate) static RT: Lazy<RwLock<Option<Runtime>>> = 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);

View file

@ -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",

View file

@ -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<Buffer> {
#[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<Buffer> {
})
.await
}
#[cfg(target_os = "wasi")]
#[cfg(target_family = "wasm")]
{
let conetent = std::fs::read(path)?;
Ok(conetent.into())