diff options
author | snek <snek@deno.com> | 2024-10-16 12:30:19 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-16 10:30:19 +0000 |
commit | ea9c3ffaa240fc968832871000832eda1b92934a (patch) | |
tree | e646d5fb6ae7a84ff5889a2b5fad89e5bedcab59 /tests/napi | |
parent | 2929d583d2f690b5a3d600296363a8ecd90440eb (diff) |
fix: node-api function call should use preamble (#26297)
`napi_call_function` should use our equiv of NAPI_PREAMBLE (`&mut Env`
instead of `*mut Env`) since it needs to set error codes based on
whether the body of the function raised a JS exception.
Fixes: https://github.com/denoland/deno/issues/26282
Diffstat (limited to 'tests/napi')
-rw-r--r-- | tests/napi/callback_test.js | 11 | ||||
-rw-r--r-- | tests/napi/src/callback.rs | 30 |
2 files changed, 40 insertions, 1 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/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( |