perf: borrow Env in CallContext
This commit is contained in:
parent
617e9c78b0
commit
91cca630a9
6 changed files with 21 additions and 22 deletions
|
@ -8,11 +8,11 @@ use std::convert::TryInto;
|
||||||
|
|
||||||
register_module!(test_module, init);
|
register_module!(test_module, init);
|
||||||
|
|
||||||
fn init(env: &Env, exports: &mut Value<Object>) -> Result<Option<Value<Object>>> {
|
fn init(env: &Env, exports: &mut Value<Object>) -> Result<()> {
|
||||||
exports.set_named_property("testThrow", env.create_function("testThrow", test_throw)?)?;
|
exports.set_named_property("testThrow", env.create_function("testThrow", test_throw)?)?;
|
||||||
|
|
||||||
exports.set_named_property("fibonacci", env.create_function("fibonacci", fibonacci)?)?;
|
exports.set_named_property("fibonacci", env.create_function("fibonacci", fibonacci)?)?;
|
||||||
Ok(None)
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[js_function]
|
#[js_function]
|
||||||
|
|
|
@ -101,8 +101,8 @@ pub fn js_function(attr: TokenStream, input: TokenStream) -> TokenStream {
|
||||||
has_error = has_error && (Status::from(status) == Status::Ok);
|
has_error = has_error && (Status::from(status) == Status::Ok);
|
||||||
}
|
}
|
||||||
|
|
||||||
let env = Env::from_raw(raw_env);
|
let mut env = Env::from_raw(raw_env);
|
||||||
let call_ctx = CallContext::new(env, raw_this, raw_args, #arg_len_span);
|
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));
|
let result = call_ctx.and_then(|ctx| #new_fn_name(ctx));
|
||||||
has_error = has_error && result.is_err();
|
has_error = has_error && result.is_err();
|
||||||
|
|
||||||
|
|
|
@ -11,13 +11,6 @@ edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
futures = { version = "0.3", features = ["default", "thread-pool"] }
|
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]
|
[target.'cfg(windows)'.build-dependencies]
|
||||||
flate2 = "1.0"
|
flate2 = "1.0"
|
||||||
|
|
|
@ -1,15 +1,16 @@
|
||||||
use crate::{sys, Any, Env, Error, Result, Status, Value, ValueType};
|
use crate::{sys, Any, Env, Error, Result, Status, Value, ValueType};
|
||||||
|
|
||||||
pub struct CallContext<T: ValueType = Any> {
|
pub struct CallContext<'env, T: ValueType = Any> {
|
||||||
pub env: Env,
|
pub env: &'env Env,
|
||||||
pub this: Value<T>,
|
pub this: Value<T>,
|
||||||
args: [sys::napi_value; 8],
|
args: [sys::napi_value; 8],
|
||||||
arg_len: usize,
|
arg_len: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: ValueType> CallContext<T> {
|
impl<'env, T: ValueType> CallContext<'env, T> {
|
||||||
|
#[inline]
|
||||||
pub fn new(
|
pub fn new(
|
||||||
env: Env,
|
env: &'env Env,
|
||||||
this: sys::napi_value,
|
this: sys::napi_value,
|
||||||
args: [sys::napi_value; 8],
|
args: [sys::napi_value; 8],
|
||||||
arg_len: usize,
|
arg_len: usize,
|
||||||
|
@ -22,6 +23,7 @@ impl<T: ValueType> CallContext<T> {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub fn get<ArgType: ValueType>(&self, index: usize) -> Result<Value<ArgType>> {
|
pub fn get<ArgType: ValueType>(&self, index: usize) -> Result<Value<ArgType>> {
|
||||||
if index + 1 > self.arg_len {
|
if index + 1 > self.arg_len {
|
||||||
Err(Error {
|
Err(Error {
|
||||||
|
|
|
@ -144,10 +144,15 @@ macro_rules! register_module {
|
||||||
let result = $init(&env, &mut exports);
|
let result = $init(&env, &mut exports);
|
||||||
|
|
||||||
match result {
|
match result {
|
||||||
Ok(Some(exports)) => exports.into_raw(),
|
Ok(_) => exports.into_raw(),
|
||||||
Ok(None) => ptr::null_mut(),
|
|
||||||
Err(e) => {
|
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()
|
ptr::null_mut()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -221,8 +226,7 @@ impl Env {
|
||||||
|
|
||||||
pub fn create_uint32(&self, number: u32) -> Result<Value<Number>> {
|
pub fn create_uint32(&self, number: u32) -> Result<Value<Number>> {
|
||||||
let mut raw_value = ptr::null_mut();
|
let mut raw_value = ptr::null_mut();
|
||||||
let status =
|
let status = unsafe { sys::napi_create_uint32(self.0, number, &mut raw_value) };
|
||||||
unsafe { sys::napi_create_uint32(self.0, number, (&mut raw_value) as *mut sys::napi_value) };
|
|
||||||
check_status(status)?;
|
check_status(status)?;
|
||||||
Ok(Value::from_raw_value(self, raw_value, Number::U32(number)))
|
Ok(Value::from_raw_value(self, raw_value, Number::U32(number)))
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,10 +9,10 @@ use napi::{Any, CallContext, Env, Error, Object, Result, Status, Value};
|
||||||
|
|
||||||
register_module!(test_module, init);
|
register_module!(test_module, init);
|
||||||
|
|
||||||
fn init(env: &Env, exports: &mut Value<Object>) -> Result<Option<Value<Object>>> {
|
fn init(env: &Env, exports: &mut Value<Object>) -> Result<()> {
|
||||||
exports.set_named_property("testSpawn", env.create_function("testSpawn", test_spawn)?)?;
|
exports.set_named_property("testSpawn", env.create_function("testSpawn", test_spawn)?)?;
|
||||||
exports.set_named_property("testThrow", env.create_function("testThrow", test_throw)?)?;
|
exports.set_named_property("testThrow", env.create_function("testThrow", test_throw)?)?;
|
||||||
Ok(None)
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[js_function]
|
#[js_function]
|
||||||
|
|
Loading…
Reference in a new issue