diff --git a/Cargo.toml b/Cargo.toml index e1cc40cd69..9b8f767dd2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -159,6 +159,8 @@ opt-level = 3 opt-level = 3 [profile.release.package.deno_napi] opt-level = 3 +[profile.release.package.test_napi] +opt-level = 3 [profile.release.package.num-bigint-dig] opt-level = 3 [profile.release.package.v8] diff --git a/cli/bench/napi/bench.js b/cli/bench/napi/bench.js new file mode 100644 index 0000000000..5917d3a28c --- /dev/null +++ b/cli/bench/napi/bench.js @@ -0,0 +1,6 @@ +import { loadTestLibrary } from "../../../test_napi/common.js"; + +const lib = loadTestLibrary(); + +Deno.bench("warmup", () => {}); +Deno.bench("napi_get_undefined", () => lib.test_get_undefined(0)); diff --git a/cli/bench/napi/bench_node.mjs b/cli/bench/napi/bench_node.mjs new file mode 100644 index 0000000000..7bfb638143 --- /dev/null +++ b/cli/bench/napi/bench_node.mjs @@ -0,0 +1,10 @@ +import { run, bench } from "mitata"; +import { createRequire } from "module"; + +const require = createRequire(import.meta.url); +const lib = require("../../../test_napi.node"); + +bench("warmup", () => {}); +bench("napi_get_undefined", () => lib.test_get_undefined(0)); + +run(); \ No newline at end of file diff --git a/cli/napi/js_native_api.rs b/cli/napi/js_native_api.rs index 31e021f708..32f4b787e0 100644 --- a/cli/napi/js_native_api.rs +++ b/cli/napi/js_native_api.rs @@ -1333,7 +1333,7 @@ fn napi_get_boolean( ) -> Result { let env: &mut Env = env.as_mut().ok_or(Error::InvalidArg)?; let value: v8::Local = - v8::Boolean::new(&mut env.scope(), value).into(); + v8::Boolean::new(env.isolate(), value).into(); *result = value.into(); Ok(()) } @@ -1520,7 +1520,7 @@ fn napi_get_new_target( fn napi_get_null(env: *mut Env, result: *mut napi_value) -> Result { let env: &mut Env = env.as_mut().ok_or(Error::InvalidArg)?; - let value: v8::Local = v8::null(&mut env.scope()).into(); + let value: v8::Local = v8::null(env.isolate()).into(); *result = value.into(); Ok(()) } @@ -1611,7 +1611,7 @@ fn napi_get_typedarray_info( #[napi_sym::napi_sym] fn napi_get_undefined(env: *mut Env, result: *mut napi_value) -> Result { let env: &mut Env = env.as_mut().ok_or(Error::InvalidArg)?; - let value: v8::Local = v8::undefined(&mut env.scope()).into(); + let value: v8::Local = v8::undefined(env.isolate()).into(); *result = value.into(); Ok(()) } diff --git a/serde_v8/ser.rs b/serde_v8/ser.rs index 615c9b782b..dc3fbd1430 100644 --- a/serde_v8/ser.rs +++ b/serde_v8/ser.rs @@ -3,6 +3,7 @@ use serde::ser; use serde::ser::Serialize; use std::cell::RefCell; +use std::ops::DerefMut; use crate::error::{Error, Result}; use crate::keys::v8_struct_key; @@ -435,7 +436,8 @@ impl<'a, 'b, 'c> ser::Serializer for Serializer<'a, 'b, 'c> { } fn serialize_f64(self, v: f64) -> JsResult<'a> { - Ok(v8::Number::new(&mut self.scope.borrow_mut(), v).into()) + let scope = &mut self.scope.borrow_mut(); + Ok(v8::Number::new(scope.deref_mut(), v).into()) } fn serialize_bool(self, v: bool) -> JsResult<'a> { diff --git a/test_napi/src/lib.rs b/test_napi/src/lib.rs index e058686c53..dd0c7cbed2 100644 --- a/test_napi/src/lib.rs +++ b/test_napi/src/lib.rs @@ -10,6 +10,7 @@ pub mod callback; pub mod coerce; pub mod numbers; pub mod object_wrap; +pub mod primitives; pub mod promise; pub mod properties; pub mod strings; @@ -67,6 +68,7 @@ unsafe extern "C" fn napi_register_module_v1( numbers::init(env, exports); typedarray::init(env, exports); array::init(env, exports); + primitives::init(env, exports); properties::init(env, exports); promise::init(env, exports); coerce::init(env, exports); diff --git a/test_napi/src/primitives.rs b/test_napi/src/primitives.rs new file mode 100644 index 0000000000..c5fbbba041 --- /dev/null +++ b/test_napi/src/primitives.rs @@ -0,0 +1,23 @@ +use napi_sys::*; +use std::ptr; + +extern "C" fn test_get_undefined( + env: napi_env, + _: napi_callback_info, +) -> napi_value { + let mut result = ptr::null_mut(); + unsafe { napi_get_undefined(env, &mut result) }; + result +} + +pub fn init(env: napi_env, exports: napi_value) { + let properties = &[crate::new_property!( + env, + "test_get_undefined\0", + test_get_undefined + )]; + + unsafe { + napi_define_properties(env, exports, properties.len(), properties.as_ptr()) + }; +}