Skip to content

Commit

Permalink
fix(ext/napi): napi_is_buffer tests for ArrayBufferView (#27956)
Browse files Browse the repository at this point in the history
use correct type check

Fixes: #27951
  • Loading branch information
devsnek authored Feb 4, 2025
1 parent c2832d7 commit 98339cf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
12 changes: 1 addition & 11 deletions ext/napi/node_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,18 +400,8 @@ fn napi_is_buffer(
check_arg!(env, value);
check_arg!(env, result);

let buffer_constructor =
v8::Local::new(&mut env.scope(), &env.buffer_constructor);

let Some(is_buffer) = value
.unwrap()
.instance_of(&mut env.scope(), buffer_constructor.into())
else {
return napi_set_last_error(env, napi_generic_failure);
};

unsafe {
*result = is_buffer;
*result = value.unwrap().is_array_buffer_view();
}

napi_clear_last_error(env)
Expand Down
16 changes: 16 additions & 0 deletions tests/napi/src/typedarray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,26 @@ extern "C" fn test_external(
typedarray
}

extern "C" fn test_is_buffer(
env: napi_env,
info: napi_callback_info,
) -> napi_value {
let (args, argc, _) = napi_get_callback_info!(env, info, 1);
assert_eq!(argc, 1);

let mut is_buffer: bool = false;
assert_napi_ok!(napi_is_buffer(env, args[0], &mut is_buffer));

let mut result: napi_value = std::ptr::null_mut();
assert_napi_ok!(napi_get_boolean(env, is_buffer, &mut result));
result
}

pub fn init(env: napi_env, exports: napi_value) {
let properties = &[
napi_new_property!(env, "test_external", test_external),
napi_new_property!(env, "test_multiply", test_multiply),
napi_new_property!(env, "test_is_buffer", test_is_buffer),
];

assert_napi_ok!(napi_define_properties(
Expand Down
9 changes: 9 additions & 0 deletions tests/napi/typedarray_test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright 2018-2025 the Deno authors. MIT license.

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

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

Deno.test("napi_is_buffer", () => {
assert(!typedarray.test_is_buffer(5));
assert(!typedarray.test_is_buffer([]));
assert(typedarray.test_is_buffer(new Uint8Array()));
assert(typedarray.test_is_buffer(new Uint32Array()));
assert(typedarray.test_is_buffer(new Buffer([])));
});

// TODO(bartlomieju): this test causes segfaults when used with jemalloc.
// Node documentation provides a hint that this function is not supported by
// other runtime like electron.
Expand Down

0 comments on commit 98339cf

Please sign in to comment.