mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 17:34:47 -05:00
fix(napi): correctly handle name in napi_create_function (#17489)
Fixes https://github.com/denoland/deno/issues/17472
This commit is contained in:
parent
5928925541
commit
c3e3694b9d
2 changed files with 8 additions and 13 deletions
|
@ -597,7 +597,7 @@ fn napi_create_external_buffer(
|
||||||
#[napi_sym::napi_sym]
|
#[napi_sym::napi_sym]
|
||||||
fn napi_create_function(
|
fn napi_create_function(
|
||||||
env: *mut Env,
|
env: *mut Env,
|
||||||
name: *const u8,
|
name: *const c_char,
|
||||||
length: usize,
|
length: usize,
|
||||||
cb: napi_callback,
|
cb: napi_callback,
|
||||||
cb_info: napi_callback_info,
|
cb_info: napi_callback_info,
|
||||||
|
@ -606,21 +606,17 @@ fn napi_create_function(
|
||||||
check_env!(env);
|
check_env!(env);
|
||||||
check_arg!(env, result);
|
check_arg!(env, result);
|
||||||
check_arg_option!(env, cb);
|
check_arg_option!(env, cb);
|
||||||
check_arg!(env, name);
|
|
||||||
|
|
||||||
if length > INT_MAX as _ {
|
if length > INT_MAX as _ {
|
||||||
return Err(Error::InvalidArg);
|
return Err(Error::InvalidArg);
|
||||||
}
|
}
|
||||||
|
|
||||||
let name = std::slice::from_raw_parts(name, length);
|
let name = name
|
||||||
// If it ends with NULL
|
.as_ref()
|
||||||
let name = if name[name.len() - 1] == 0 {
|
.map(|_| check_new_from_utf8_len(env, name, length))
|
||||||
std::str::from_utf8_unchecked(&name[0..name.len() - 1])
|
.transpose()?;
|
||||||
} else {
|
|
||||||
std::str::from_utf8_unchecked(name)
|
|
||||||
};
|
|
||||||
|
|
||||||
*result = create_function(env, Some(name), cb, cb_info).into();
|
*result = create_function(env, name, cb, cb_info).into();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -51,7 +51,7 @@ extern "C" fn call_fn(info: *const v8::FunctionCallbackInfo) {
|
||||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||||
pub fn create_function<'a>(
|
pub fn create_function<'a>(
|
||||||
env_ptr: *mut Env,
|
env_ptr: *mut Env,
|
||||||
name: Option<&str>,
|
name: Option<v8::Local<v8::String>>,
|
||||||
cb: napi_callback,
|
cb: napi_callback,
|
||||||
cb_info: napi_callback_info,
|
cb_info: napi_callback_info,
|
||||||
) -> v8::Local<'a, v8::Function> {
|
) -> v8::Local<'a, v8::Function> {
|
||||||
|
@ -67,8 +67,7 @@ pub fn create_function<'a>(
|
||||||
.build(scope)
|
.build(scope)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
if let Some(name) = name {
|
if let Some(v8str) = name {
|
||||||
let v8str = v8::String::new(scope, name).unwrap();
|
|
||||||
function.set_name(v8str);
|
function.set_name(v8str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue