fix(napi): fix is_detached_arraybuffer (#16478)

This commit is contained in:
Marcos Casagrande 2022-10-30 18:13:46 +01:00 committed by GitHub
parent 59ac110edd
commit 207dd8d111
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 52 additions and 4 deletions

View file

@ -1798,9 +1798,7 @@ fn napi_is_detached_arraybuffer(
) -> Result {
let value = transmute::<napi_value, v8::Local<v8::Value>>(value);
let _ab = v8::Local::<v8::ArrayBuffer>::try_from(value).unwrap();
// TODO: what is API for checking if ArrayBuffer is detached?
// there's only is_detachable I could find.
*result = false;
*result = _ab.was_detached();
Ok(())
}

View file

@ -11,7 +11,7 @@ publish = false
crate-type = ["cdylib"]
[dependencies]
napi-sys = { version = "2.2.2", default-features = false, features = ["napi4"] }
napi-sys = { version = "2.2.2", default-features = false, features = ["napi7"] }
[dev-dependencies]
test_util = { path = "../test_util" }

View file

@ -0,0 +1,12 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assertEquals, loadTestLibrary } from "./common.js";
const typedarray = loadTestLibrary();
Deno.test("napi arraybuffer detach", function () {
const buf = new ArrayBuffer(5);
assertEquals(buf.byteLength, 5);
typedarray.test_detached(buf);
assertEquals(buf.byteLength, 0);
});

View file

@ -0,0 +1,36 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
use napi_sys::Status::napi_ok;
use napi_sys::*;
use std::ptr;
extern "C" fn test_detached(
env: napi_env,
info: napi_callback_info,
) -> napi_value {
let (args, argc, _) = crate::get_callback_info!(env, info, 1);
assert_eq!(argc, 1);
let mut value = false;
assert!(
unsafe { napi_is_detached_arraybuffer(env, args[0], &mut value) }
== napi_ok
);
assert!(!value);
assert!(unsafe { napi_detach_arraybuffer(env, args[0]) } == napi_ok);
assert!(
unsafe { napi_is_detached_arraybuffer(env, args[0], &mut value) }
== napi_ok
);
assert!(value);
args[0]
}
pub fn init(env: napi_env, exports: napi_value) {
let properties =
&[crate::new_property!(env, "test_detached\0", test_detached)];
unsafe {
napi_define_properties(env, exports, properties.len(), properties.as_ptr())
};
}

View file

@ -5,6 +5,7 @@
use napi_sys::*;
pub mod array;
pub mod arraybuffer;
pub mod r#async;
pub mod callback;
pub mod coerce;
@ -67,6 +68,7 @@ unsafe extern "C" fn napi_register_module_v1(
strings::init(env, exports);
numbers::init(env, exports);
typedarray::init(env, exports);
arraybuffer::init(env, exports);
array::init(env, exports);
primitives::init(env, exports);
properties::init(env, exports);