From 207dd8d111303f02b92d632ca00c165a657bc9c2 Mon Sep 17 00:00:00 2001 From: Marcos Casagrande Date: Sun, 30 Oct 2022 18:13:46 +0100 Subject: [PATCH] fix(napi): fix is_detached_arraybuffer (#16478) --- cli/napi/js_native_api.rs | 4 +--- test_napi/Cargo.toml | 2 +- test_napi/arraybuffer_test.js | 12 ++++++++++++ test_napi/src/arraybuffer.rs | 36 +++++++++++++++++++++++++++++++++++ test_napi/src/lib.rs | 2 ++ 5 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 test_napi/arraybuffer_test.js create mode 100644 test_napi/src/arraybuffer.rs diff --git a/cli/napi/js_native_api.rs b/cli/napi/js_native_api.rs index 32f4b787e0..07b583beb3 100644 --- a/cli/napi/js_native_api.rs +++ b/cli/napi/js_native_api.rs @@ -1798,9 +1798,7 @@ fn napi_is_detached_arraybuffer( ) -> Result { let value = transmute::>(value); let _ab = v8::Local::::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(()) } diff --git a/test_napi/Cargo.toml b/test_napi/Cargo.toml index 09faa4175b..4072755635 100644 --- a/test_napi/Cargo.toml +++ b/test_napi/Cargo.toml @@ -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" } diff --git a/test_napi/arraybuffer_test.js b/test_napi/arraybuffer_test.js new file mode 100644 index 0000000000..577443e8be --- /dev/null +++ b/test_napi/arraybuffer_test.js @@ -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); +}); diff --git a/test_napi/src/arraybuffer.rs b/test_napi/src/arraybuffer.rs new file mode 100644 index 0000000000..cf170e713c --- /dev/null +++ b/test_napi/src/arraybuffer.rs @@ -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()) + }; +} diff --git a/test_napi/src/lib.rs b/test_napi/src/lib.rs index 569256a0b1..c1a13f6e42 100644 --- a/test_napi/src/lib.rs +++ b/test_napi/src/lib.rs @@ -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);