1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-21 23:04:45 -05:00

fix(napi): improve napi_is_detached_arraybuffer (#17498)

This commit is contained in:
Divy Srivastava 2023-01-23 00:52:18 -08:00 committed by GitHub
parent 2e1df62380
commit b96bbc32c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 5 deletions

View file

@ -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(())
}

View file

@ -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();
@ -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));
});
});

View file

@ -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,