1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-28 16:20:57 -05:00

fix(napi): properly handle arguments in napi_get_cb_info (#19269)

Closes https://github.com/denoland/deno/issues/17213
This commit is contained in:
Bartek Iwańczuk 2023-05-26 07:02:12 +02:00 committed by GitHub
parent 512d5337c4
commit e95f098ae3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 16 deletions

View file

@ -1758,7 +1758,7 @@ fn napi_get_cb_info(
argc: *mut i32, argc: *mut i32,
argv: *mut napi_value, argv: *mut napi_value,
this_arg: *mut napi_value, this_arg: *mut napi_value,
cb_data: *mut *mut c_void, data: *mut *mut c_void,
) -> napi_status { ) -> napi_status {
check_env!(env); check_env!(env);
let env = unsafe { &mut *env }; let env = unsafe { &mut *env };
@ -1767,8 +1767,17 @@ fn napi_get_cb_info(
let cbinfo: &CallbackInfo = &*(cbinfo as *const CallbackInfo); let cbinfo: &CallbackInfo = &*(cbinfo as *const CallbackInfo);
let args = &*(cbinfo.args as *const v8::FunctionCallbackArguments); let args = &*(cbinfo.args as *const v8::FunctionCallbackArguments);
if !cb_data.is_null() { if !argv.is_null() {
*cb_data = cbinfo.cb_info; 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() { if !this_arg.is_null() {
@ -1776,20 +1785,11 @@ fn napi_get_cb_info(
*this_arg = this.into(); *this_arg = this.into();
} }
let len = args.length(); if !data.is_null() {
let mut v_argc = len; *data = cbinfo.cb_info;
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();
}
} }
napi_clear_last_error(env);
napi_ok napi_ok
} }

View file

@ -5,6 +5,7 @@ use crate::napi_get_callback_info;
use crate::napi_new_property; use crate::napi_new_property;
use napi_sys::ValueType::napi_function; use napi_sys::ValueType::napi_function;
use napi_sys::ValueType::napi_object; use napi_sys::ValueType::napi_object;
use napi_sys::ValueType::napi_undefined;
use napi_sys::*; use napi_sys::*;
use std::ptr; use std::ptr;
@ -13,7 +14,9 @@ extern "C" fn test_callback_run(
env: napi_env, env: napi_env,
info: napi_callback_info, info: napi_callback_info,
) -> napi_value { ) -> 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); assert_eq!(argc, 2);
let mut ty = -1; 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_napi_ok!(napi_typeof(env, args[1], &mut ty));
assert_eq!(ty, napi_object); 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; let mut len = 0;
assert_napi_ok!(napi_get_array_length(env, args[1], &mut len)); assert_napi_ok!(napi_get_array_length(env, args[1], &mut len));