Skip to content

Commit 293a36f

Browse files
authored
fix: more node-api fixes (denoland#24220)
- add fallback impls of external string apis which always copy. after upstream changes to rusty_v8 we can support non-copying api as well. - `napi_get_buffer_data` needs to work on all TypedArray instances. - Fixes: denoland#24209 - `target_defaults.default_configuration` is used by some modules to find the corresponding node file from node-gyp - `node_api_get_module_filename` expects the filename to be a `file:` url.
1 parent 6c6ee02 commit 293a36f

File tree

4 files changed

+58
-30
lines changed

4 files changed

+58
-30
lines changed

cli/napi/js_native_api.rs

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,27 +1180,57 @@ fn napi_create_string_utf16(
11801180
#[napi_sym]
11811181
fn node_api_create_external_string_latin1(
11821182
env_ptr: *mut Env,
1183-
_string: *const c_char,
1184-
_length: usize,
1185-
_nogc_finalize_callback: napi_finalize,
1186-
_finalize_hint: *mut c_void,
1187-
_result: *mut napi_value,
1188-
_copied: *mut bool,
1183+
string: *const c_char,
1184+
length: usize,
1185+
nogc_finalize_callback: Option<napi_finalize>,
1186+
finalize_hint: *mut c_void,
1187+
result: *mut napi_value,
1188+
copied: *mut bool,
11891189
) -> napi_status {
1190-
return napi_set_last_error(env_ptr, napi_generic_failure);
1190+
let status =
1191+
unsafe { napi_create_string_latin1(env_ptr, string, length, result) };
1192+
1193+
if status == napi_ok {
1194+
unsafe {
1195+
*copied = true;
1196+
}
1197+
1198+
if let Some(finalize) = nogc_finalize_callback {
1199+
unsafe {
1200+
finalize(env_ptr as napi_env, string as *mut c_void, finalize_hint);
1201+
}
1202+
}
1203+
}
1204+
1205+
status
11911206
}
11921207

11931208
#[napi_sym]
11941209
fn node_api_create_external_string_utf16(
11951210
env_ptr: *mut Env,
1196-
_string: *const u16,
1197-
_length: usize,
1198-
_nogc_finalize_callback: napi_finalize,
1199-
_finalize_hint: *mut c_void,
1200-
_result: *mut napi_value,
1201-
_copied: *mut bool,
1211+
string: *const u16,
1212+
length: usize,
1213+
nogc_finalize_callback: Option<napi_finalize>,
1214+
finalize_hint: *mut c_void,
1215+
result: *mut napi_value,
1216+
copied: *mut bool,
12021217
) -> napi_status {
1203-
return napi_set_last_error(env_ptr, napi_generic_failure);
1218+
let status =
1219+
unsafe { napi_create_string_utf16(env_ptr, string, length, result) };
1220+
1221+
if status == napi_ok {
1222+
unsafe {
1223+
*copied = true;
1224+
}
1225+
1226+
if let Some(finalize) = nogc_finalize_callback {
1227+
unsafe {
1228+
finalize(env_ptr as napi_env, string as *mut c_void, finalize_hint);
1229+
}
1230+
}
1231+
}
1232+
1233+
status
12041234
}
12051235

12061236
#[napi_sym]
@@ -2793,8 +2823,8 @@ fn napi_instanceof(
27932823
unsafe {
27942824
napi_throw_type_error(
27952825
env,
2796-
"ERR_NAPI_CONS_FUNCTION\0".as_ptr() as _,
2797-
"Constructor must be a function\0".as_ptr() as _,
2826+
c"ERR_NAPI_CONS_FUNCTION".as_ptr(),
2827+
c"Constructor must be a function".as_ptr(),
27982828
);
27992829
}
28002830
return napi_function_expected;
@@ -3147,8 +3177,8 @@ fn napi_create_dataview<'s>(
31473177
unsafe {
31483178
return napi_throw_range_error(
31493179
env,
3150-
"ERR_NAPI_INVALID_DATAVIEW_ARGS\0".as_ptr() as _,
3151-
"byte_offset + byte_length should be less than or equal to the size in bytes of the array passed in\0".as_ptr() as _,
3180+
c"ERR_NAPI_INVALID_DATAVIEW_ARGS".as_ptr(),
3181+
c"byte_offset + byte_length should be less than or equal to the size in bytes of the array passed in".as_ptr(),
31523182
);
31533183
}
31543184
}

cli/napi/node_api.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -426,22 +426,14 @@ fn napi_get_buffer_info(
426426
let env = check_env!(env);
427427
check_arg!(env, value);
428428

429+
// NB: Any TypedArray instance seems to be accepted by this function
430+
// in Node.js.
429431
let Some(ta) =
430432
value.and_then(|v| v8::Local::<v8::TypedArray>::try_from(v).ok())
431433
else {
432434
return napi_set_last_error(env, napi_invalid_arg);
433435
};
434436

435-
let buffer_constructor =
436-
v8::Local::new(&mut env.scope(), &env.buffer_constructor);
437-
438-
if !ta
439-
.instance_of(&mut env.scope(), buffer_constructor.into())
440-
.unwrap_or(false)
441-
{
442-
return napi_set_last_error(env, napi_invalid_arg);
443-
}
444-
445437
if !data.is_null() {
446438
unsafe {
447439
*data = ta.data();

ext/napi/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use core::ptr::NonNull;
99
use deno_core::error::type_error;
1010
use deno_core::error::AnyError;
1111
use deno_core::op2;
12+
use deno_core::url::Url;
1213
use deno_core::ExternalOpsTracker;
1314
use deno_core::OpState;
1415
use deno_core::V8CrossThreadTaskSpawner;
@@ -528,7 +529,10 @@ where
528529
let type_tag = v8::Private::new(scope, Some(type_tag_name));
529530
let type_tag = v8::Global::new(scope, type_tag);
530531

531-
let env_shared = EnvShared::new(napi_wrap, type_tag, path.clone());
532+
let url_filename =
533+
Url::from_file_path(&path).map_err(|_| type_error("Invalid path"))?;
534+
let env_shared =
535+
EnvShared::new(napi_wrap, type_tag, format!("{url_filename}\0"));
532536

533537
let ctx = scope.get_current_context();
534538
let mut env = Env::new(

ext/node/polyfills/process.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,9 @@ Process.prototype.chdir = chdir;
403403

404404
/** https://nodejs.org/api/process.html#processconfig */
405405
Process.prototype.config = {
406-
target_defaults: {},
406+
target_defaults: {
407+
default_configuration: "Release",
408+
},
407409
variables: {},
408410
};
409411

0 commit comments

Comments
 (0)