diff --git a/cli/napi/js_native_api.rs b/cli/napi/js_native_api.rs index b189504034..ed8aacb477 100644 --- a/cli/napi/js_native_api.rs +++ b/cli/napi/js_native_api.rs @@ -1758,7 +1758,7 @@ fn napi_get_cb_info( argc: *mut i32, argv: *mut napi_value, this_arg: *mut napi_value, - cb_data: *mut *mut c_void, + data: *mut *mut c_void, ) -> napi_status { check_env!(env); let env = unsafe { &mut *env }; @@ -1767,8 +1767,17 @@ fn napi_get_cb_info( let cbinfo: &CallbackInfo = &*(cbinfo as *const CallbackInfo); let args = &*(cbinfo.args as *const v8::FunctionCallbackArguments); - if !cb_data.is_null() { - *cb_data = cbinfo.cb_info; + if !argv.is_null() { + check_arg!(env, argc); + let mut v_argv = std::slice::from_raw_parts_mut(argv, argc as usize); + for i in 0..*argc { + let mut arg = args.get(i); + v_argv[i as usize] = arg.into(); + } + } + + if !argc.is_null() { + *argc = args.length(); } if !this_arg.is_null() { @@ -1776,20 +1785,11 @@ fn napi_get_cb_info( *this_arg = this.into(); } - let len = args.length(); - let mut v_argc = len; - if !argc.is_null() { - *argc = len; - } - - if !argv.is_null() { - let mut v_argv = std::slice::from_raw_parts_mut(argv, v_argc as usize); - for i in 0..v_argc { - let mut arg = args.get(i); - v_argv[i as usize] = arg.into(); - } + if !data.is_null() { + *data = cbinfo.cb_info; } + napi_clear_last_error(env); napi_ok } diff --git a/test_napi/src/callback.rs b/test_napi/src/callback.rs index c863eb7a60..ae917e7b13 100644 --- a/test_napi/src/callback.rs +++ b/test_napi/src/callback.rs @@ -5,6 +5,7 @@ use crate::napi_get_callback_info; use crate::napi_new_property; use napi_sys::ValueType::napi_function; use napi_sys::ValueType::napi_object; +use napi_sys::ValueType::napi_undefined; use napi_sys::*; use std::ptr; @@ -13,7 +14,9 @@ extern "C" fn test_callback_run( env: napi_env, info: napi_callback_info, ) -> napi_value { - let (args, argc, _) = napi_get_callback_info!(env, info, 2); + // We want to have argv with size 4, even though the callback will have + // only two arguments. We'll assert that the remaining two args are undefined. + let (args, argc, _) = napi_get_callback_info!(env, info, 4); assert_eq!(argc, 2); let mut ty = -1; @@ -24,6 +27,14 @@ extern "C" fn test_callback_run( assert_napi_ok!(napi_typeof(env, args[1], &mut ty)); assert_eq!(ty, napi_object); + let mut ty = -1; + assert_napi_ok!(napi_typeof(env, args[2], &mut ty)); + assert_eq!(ty, napi_undefined); + + let mut ty = -1; + assert_napi_ok!(napi_typeof(env, args[3], &mut ty)); + assert_eq!(ty, napi_undefined); + let mut len = 0; assert_napi_ok!(napi_get_array_length(env, args[1], &mut len));