Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ext/napi): napi_is_buffer tests for ArrayBufferView #27956

Merged
merged 2 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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