diff --git a/napi-derive-example/src/lib.rs b/napi-derive-example/src/lib.rs index 6dded1cc..699721a4 100644 --- a/napi-derive-example/src/lib.rs +++ b/napi-derive-example/src/lib.rs @@ -8,11 +8,11 @@ use std::convert::TryInto; register_module!(test_module, init); -fn init(env: &Env, exports: &mut Value) -> Result>> { +fn init(env: &Env, exports: &mut Value) -> Result<()> { exports.set_named_property("testThrow", env.create_function("testThrow", test_throw)?)?; exports.set_named_property("fibonacci", env.create_function("fibonacci", fibonacci)?)?; - Ok(None) + Ok(()) } #[js_function] diff --git a/napi-derive/src/lib.rs b/napi-derive/src/lib.rs index f3b44834..fe647863 100644 --- a/napi-derive/src/lib.rs +++ b/napi-derive/src/lib.rs @@ -101,8 +101,8 @@ pub fn js_function(attr: TokenStream, input: TokenStream) -> TokenStream { has_error = has_error && (Status::from(status) == Status::Ok); } - let env = Env::from_raw(raw_env); - let call_ctx = CallContext::new(env, raw_this, raw_args, #arg_len_span); + let mut env = Env::from_raw(raw_env); + let call_ctx = CallContext::new(&mut env, raw_this, raw_args, #arg_len_span); let result = call_ctx.and_then(|ctx| #new_fn_name(ctx)); has_error = has_error && result.is_err(); diff --git a/napi/Cargo.toml b/napi/Cargo.toml index eefd3f40..3b0b387c 100644 --- a/napi/Cargo.toml +++ b/napi/Cargo.toml @@ -11,13 +11,6 @@ edition = "2018" [dependencies] futures = { version = "0.3", features = ["default", "thread-pool"] } -serde = { version = "1", optional = true } -serde_json = { version = "1", optional = true } - -[features] -serde-json = ["serde", "serde_json"] - -default = ["serde-json"] [target.'cfg(windows)'.build-dependencies] flate2 = "1.0" diff --git a/napi/src/call_context.rs b/napi/src/call_context.rs index d85060ff..4a57daaf 100644 --- a/napi/src/call_context.rs +++ b/napi/src/call_context.rs @@ -1,15 +1,16 @@ use crate::{sys, Any, Env, Error, Result, Status, Value, ValueType}; -pub struct CallContext { - pub env: Env, +pub struct CallContext<'env, T: ValueType = Any> { + pub env: &'env Env, pub this: Value, args: [sys::napi_value; 8], arg_len: usize, } -impl CallContext { +impl<'env, T: ValueType> CallContext<'env, T> { + #[inline] pub fn new( - env: Env, + env: &'env Env, this: sys::napi_value, args: [sys::napi_value; 8], arg_len: usize, @@ -22,6 +23,7 @@ impl CallContext { }) } + #[inline] pub fn get(&self, index: usize) -> Result> { if index + 1 > self.arg_len { Err(Error { diff --git a/napi/src/lib.rs b/napi/src/lib.rs index 8986372d..a7dbb547 100644 --- a/napi/src/lib.rs +++ b/napi/src/lib.rs @@ -144,10 +144,15 @@ macro_rules! register_module { let result = $init(&env, &mut exports); match result { - Ok(Some(exports)) => exports.into_raw(), - Ok(None) => ptr::null_mut(), + Ok(_) => exports.into_raw(), Err(e) => { - let _ = writeln!(::std::io::stderr(), "Error initializing module: {:?}", e); + unsafe { + sys::napi_throw_error( + raw_env, + ptr::null(), + format!("Error initializing module: {:?}", e).as_ptr() as *const _, + ) + }; ptr::null_mut() } } @@ -221,8 +226,7 @@ impl Env { pub fn create_uint32(&self, number: u32) -> Result> { let mut raw_value = ptr::null_mut(); - let status = - unsafe { sys::napi_create_uint32(self.0, number, (&mut raw_value) as *mut sys::napi_value) }; + let status = unsafe { sys::napi_create_uint32(self.0, number, &mut raw_value) }; check_status(status)?; Ok(Value::from_raw_value(self, raw_value, Number::U32(number))) } diff --git a/test_module/src/lib.rs b/test_module/src/lib.rs index d93cbabd..7131f50e 100644 --- a/test_module/src/lib.rs +++ b/test_module/src/lib.rs @@ -9,10 +9,10 @@ use napi::{Any, CallContext, Env, Error, Object, Result, Status, Value}; register_module!(test_module, init); -fn init(env: &Env, exports: &mut Value) -> Result>> { +fn init(env: &Env, exports: &mut Value) -> Result<()> { exports.set_named_property("testSpawn", env.create_function("testSpawn", test_spawn)?)?; exports.set_named_property("testThrow", env.create_function("testThrow", test_throw)?)?; - Ok(None) + Ok(()) } #[js_function]