Skip to content

Commit 98339cf

Browse files
authored
fix(ext/napi): napi_is_buffer tests for ArrayBufferView (#27956)
use correct type check Fixes: #27951
1 parent c2832d7 commit 98339cf

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

ext/napi/node_api.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -400,18 +400,8 @@ fn napi_is_buffer(
400400
check_arg!(env, value);
401401
check_arg!(env, result);
402402

403-
let buffer_constructor =
404-
v8::Local::new(&mut env.scope(), &env.buffer_constructor);
405-
406-
let Some(is_buffer) = value
407-
.unwrap()
408-
.instance_of(&mut env.scope(), buffer_constructor.into())
409-
else {
410-
return napi_set_last_error(env, napi_generic_failure);
411-
};
412-
413403
unsafe {
414-
*result = is_buffer;
404+
*result = value.unwrap().is_array_buffer_view();
415405
}
416406

417407
napi_clear_last_error(env)

tests/napi/src/typedarray.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,26 @@ extern "C" fn test_external(
144144
typedarray
145145
}
146146

147+
extern "C" fn test_is_buffer(
148+
env: napi_env,
149+
info: napi_callback_info,
150+
) -> napi_value {
151+
let (args, argc, _) = napi_get_callback_info!(env, info, 1);
152+
assert_eq!(argc, 1);
153+
154+
let mut is_buffer: bool = false;
155+
assert_napi_ok!(napi_is_buffer(env, args[0], &mut is_buffer));
156+
157+
let mut result: napi_value = std::ptr::null_mut();
158+
assert_napi_ok!(napi_get_boolean(env, is_buffer, &mut result));
159+
result
160+
}
161+
147162
pub fn init(env: napi_env, exports: napi_value) {
148163
let properties = &[
149164
napi_new_property!(env, "test_external", test_external),
150165
napi_new_property!(env, "test_multiply", test_multiply),
166+
napi_new_property!(env, "test_is_buffer", test_is_buffer),
151167
];
152168

153169
assert_napi_ok!(napi_define_properties(

tests/napi/typedarray_test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright 2018-2025 the Deno authors. MIT license.
22

3+
import { Buffer } from "node:buffer";
34
import { assert, assertEquals, loadTestLibrary } from "./common.js";
45

56
const typedarray = loadTestLibrary();
@@ -28,6 +29,14 @@ Deno.test("napi typedarray float64", function () {
2829
assertEquals(Math.round(10 * doubleResult[2]) / 10, -6.6);
2930
});
3031

32+
Deno.test("napi_is_buffer", () => {
33+
assert(!typedarray.test_is_buffer(5));
34+
assert(!typedarray.test_is_buffer([]));
35+
assert(typedarray.test_is_buffer(new Uint8Array()));
36+
assert(typedarray.test_is_buffer(new Uint32Array()));
37+
assert(typedarray.test_is_buffer(new Buffer([])));
38+
});
39+
3140
// TODO(bartlomieju): this test causes segfaults when used with jemalloc.
3241
// Node documentation provides a hint that this function is not supported by
3342
// other runtime like electron.

0 commit comments

Comments
 (0)