Skip to content

Commit

Permalink
fix(napi): improve napi_is_detached_arraybuffer (denoland#17498)
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy committed Jan 23, 2023
1 parent 2e1df62 commit b96bbc3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
15 changes: 12 additions & 3 deletions cli/napi/js_native_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2106,13 +2106,22 @@ fn napi_is_date(

#[napi_sym::napi_sym]
fn napi_is_detached_arraybuffer(
_env: *mut Env,
env: *mut Env,
value: napi_value,
result: *mut bool,
) -> Result {
check_env!(env);
check_arg!(env, result);

let value = napi_value_unchecked(value);
let _ab = v8::Local::<v8::ArrayBuffer>::try_from(value).unwrap();
*result = _ab.was_detached();

*result = match v8::Local::<v8::ArrayBuffer>::try_from(value) {
Ok(array_buffer) => array_buffer.was_detached(),
Err(_) => false,
};

napi_clear_last_error(env);

Ok(())
}

Expand Down
14 changes: 13 additions & 1 deletion test_napi/arraybuffer_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

import { assertEquals, loadTestLibrary } from "./common.js";
import { assert, assertEquals, loadTestLibrary } from "./common.js";

const typedarray = loadTestLibrary();

Expand All @@ -10,3 +10,15 @@ Deno.test("napi arraybuffer detach", function () {
typedarray.test_detached(buf);
assertEquals(buf.byteLength, 0);
});

Deno.test("napi arraybuffer is detached", function () {
const buf = new ArrayBuffer(5);
assertEquals(buf.byteLength, 5);
assert(!typedarray.is_detached(buf));
typedarray.test_detached(buf);
assert(typedarray.is_detached(buf));

[2, {}, "foo", null, undefined, new Uint8Array(10)].forEach((value) => {
assert(!typedarray.is_detached(value));
});
});
21 changes: 20 additions & 1 deletion test_napi/src/arraybuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,27 @@ extern "C" fn test_detached(
args[0]
}

extern "C" fn is_detached(
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 value = false;
assert_napi_ok!(napi_is_detached_arraybuffer(env, args[0], &mut value));

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

result
}

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

assert_napi_ok!(napi_define_properties(
env,
Expand Down

0 comments on commit b96bbc3

Please sign in to comment.