Merge pull request #43 from Brooooooklyn/perf-tweak

perf: borrow Env in CallContext
This commit is contained in:
LongYinan 2020-05-09 15:32:17 +08:00 committed by GitHub
commit fe77ac7b8e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 21 additions and 22 deletions

View file

@ -8,11 +8,11 @@ use std::convert::TryInto;
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("fibonacci", env.create_function("fibonacci", fibonacci)?)?;
Ok(None)
Ok(())
}
#[js_function]

View file

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

View file

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

View file

@ -1,15 +1,16 @@
use crate::{sys, Any, Env, Error, Result, Status, Value, ValueType};
pub struct CallContext<T: ValueType = Any> {
pub env: Env,
pub struct CallContext<'env, T: ValueType = Any> {
pub env: &'env Env,
pub this: Value<T>,
args: [sys::napi_value; 8],
arg_len: usize,
}
impl<T: ValueType> CallContext<T> {
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<T: ValueType> CallContext<T> {
})
}
#[inline]
pub fn get<ArgType: ValueType>(&self, index: usize) -> Result<Value<ArgType>> {
if index + 1 > self.arg_len {
Err(Error {

View file

@ -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<Value<Number>> {
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)))
}

View file

@ -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<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("testThrow", env.create_function("testThrow", test_throw)?)?;
Ok(None)
Ok(())
}
#[js_function]