summaryrefslogtreecommitdiff
path: root/tests/napi
diff options
context:
space:
mode:
Diffstat (limited to 'tests/napi')
-rw-r--r--tests/napi/callback_test.js11
-rw-r--r--tests/napi/object_test.js25
-rw-r--r--tests/napi/src/async.rs2
-rw-r--r--tests/napi/src/callback.rs30
-rw-r--r--tests/napi/src/object.rs29
-rw-r--r--tests/napi/src/tsfn.rs6
6 files changed, 98 insertions, 5 deletions
diff --git a/tests/napi/callback_test.js b/tests/napi/callback_test.js
index 98622d48d..c132fefa1 100644
--- a/tests/napi/callback_test.js
+++ b/tests/napi/callback_test.js
@@ -1,6 +1,6 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
-import { assertEquals, loadTestLibrary } from "./common.js";
+import { assertEquals, assertThrows, loadTestLibrary } from "./common.js";
const callback = loadTestLibrary();
@@ -36,3 +36,12 @@ Deno.test("napi callback run with args & recv", function () {
);
assertEquals(result, 69);
});
+
+Deno.test("napi callback handles errors correctly", function () {
+ const e = new Error("hi!");
+ assertThrows(() => {
+ callback.test_callback_throws(() => {
+ throw e;
+ });
+ }, e);
+});
diff --git a/tests/napi/object_test.js b/tests/napi/object_test.js
index 4bc5c3c9c..6226b0138 100644
--- a/tests/napi/object_test.js
+++ b/tests/napi/object_test.js
@@ -1,6 +1,11 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
-import { assert, assertEquals, loadTestLibrary } from "./common.js";
+import {
+ assert,
+ assertEquals,
+ assertThrows,
+ loadTestLibrary,
+} from "./common.js";
const object = loadTestLibrary();
@@ -12,4 +17,22 @@ Deno.test("napi object", function () {
const r1 = object.test_object_get(r);
assert(r === r1);
+
+ const r2 = object.test_object_attr_property(r);
+ assert(r === r2);
+ assertThrows(
+ () => {
+ r2.self = "2";
+ },
+ Error,
+ "Cannot assign to read only property 'self' of object '#<Object>'",
+ );
+
+ assertThrows(
+ () => {
+ r2.method = () => {};
+ },
+ Error,
+ "Cannot assign to read only property 'method' of object '#<Object>'",
+ );
});
diff --git a/tests/napi/src/async.rs b/tests/napi/src/async.rs
index 3d3827b51..367d2e9ef 100644
--- a/tests/napi/src/async.rs
+++ b/tests/napi/src/async.rs
@@ -95,7 +95,7 @@ extern "C" fn test_async_work(
));
let mut baton = unsafe { Box::from_raw(baton_ptr as *mut Baton) };
baton.task = async_work;
- Box::into_raw(baton);
+ let _ = Box::into_raw(baton);
assert_napi_ok!(napi_queue_async_work(env, async_work));
ptr::null_mut()
diff --git a/tests/napi/src/callback.rs b/tests/napi/src/callback.rs
index 8909f5176..2512f6a38 100644
--- a/tests/napi/src/callback.rs
+++ b/tests/napi/src/callback.rs
@@ -8,6 +8,7 @@ use napi_sys::ValueType::napi_object;
use napi_sys::ValueType::napi_undefined;
use napi_sys::*;
use std::ptr;
+use Status::napi_pending_exception;
/// `test_callback_run((a, b) => a + b, [1, 2])` => 3
extern "C" fn test_callback_run(
@@ -98,6 +99,34 @@ extern "C" fn test_callback_run_with_recv(
result
}
+extern "C" fn test_callback_throws(
+ env: napi_env,
+ info: napi_callback_info,
+) -> napi_value {
+ let (args, ..) = napi_get_callback_info!(env, info, 1);
+
+ let mut global: napi_value = ptr::null_mut();
+ assert_napi_ok!(napi_get_global(env, &mut global));
+
+ let mut argv = vec![];
+ let mut result: napi_value = ptr::null_mut();
+ assert_eq!(
+ unsafe {
+ napi_call_function(
+ env,
+ global, // recv
+ args[0], // cb
+ argv.len(),
+ argv.as_mut_ptr(),
+ &mut result,
+ )
+ },
+ napi_pending_exception
+ );
+
+ result
+}
+
pub fn init(env: napi_env, exports: napi_value) {
let properties = &[
napi_new_property!(env, "test_callback_run", test_callback_run),
@@ -106,6 +135,7 @@ pub fn init(env: napi_env, exports: napi_value) {
"test_callback_run_with_recv",
test_callback_run_with_recv
),
+ napi_new_property!(env, "test_callback_throws", test_callback_throws),
];
assert_napi_ok!(napi_define_properties(
diff --git a/tests/napi/src/object.rs b/tests/napi/src/object.rs
index aa34133dc..9876f4dae 100644
--- a/tests/napi/src/object.rs
+++ b/tests/napi/src/object.rs
@@ -40,10 +40,39 @@ extern "C" fn test_object_get(
obj
}
+extern "C" fn test_object_attr_property(
+ env: napi_env,
+ info: napi_callback_info,
+) -> napi_value {
+ let (args, argc, _) = napi_get_callback_info!(env, info, 1);
+ assert_eq!(argc, 1);
+
+ let obj = args[0];
+ let mut property = napi_new_property!(env, "self", test_object_new);
+ property.attributes = PropertyAttributes::enumerable;
+ property.method = None;
+ property.value = obj;
+ let mut method_property = napi_new_property!(env, "method", test_object_new);
+ method_property.attributes = PropertyAttributes::enumerable;
+ let properties = &[property, method_property];
+ assert_napi_ok!(napi_define_properties(
+ env,
+ obj,
+ properties.len(),
+ properties.as_ptr()
+ ));
+ obj
+}
+
pub fn init(env: napi_env, exports: napi_value) {
let properties = &[
napi_new_property!(env, "test_object_new", test_object_new),
napi_new_property!(env, "test_object_get", test_object_get),
+ napi_new_property!(
+ env,
+ "test_object_attr_property",
+ test_object_attr_property
+ ),
];
assert_napi_ok!(napi_define_properties(
diff --git a/tests/napi/src/tsfn.rs b/tests/napi/src/tsfn.rs
index dabc96f83..a3a231cec 100644
--- a/tests/napi/src/tsfn.rs
+++ b/tests/napi/src/tsfn.rs
@@ -46,6 +46,7 @@ fn create_custom_gc(env: sys::napi_env) {
"Create async resource string in napi_register_module_v1 napi_register_module_v1"
);
let mut custom_gc_tsfn = ptr::null_mut();
+ let context = Box::into_raw(Box::new(0)) as *mut c_void;
check_status_or_panic!(
unsafe {
sys::napi_create_threadsafe_function(
@@ -57,7 +58,7 @@ fn create_custom_gc(env: sys::napi_env) {
1,
ptr::null_mut(),
Some(custom_gc_finalize),
- ptr::null_mut(),
+ context,
Some(custom_gc),
&mut custom_gc_tsfn,
)
@@ -80,8 +81,9 @@ unsafe extern "C" fn empty(
unsafe extern "C" fn custom_gc_finalize(
_env: sys::napi_env,
_finalize_data: *mut c_void,
- _finalize_hint: *mut c_void,
+ finalize_hint: *mut c_void,
) {
+ let _ = Box::from_raw(finalize_hint as *mut i32);
}
extern "C" fn custom_gc(