summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cli/napi/js_native_api.rs4
-rw-r--r--test_napi/Cargo.toml2
-rw-r--r--test_napi/arraybuffer_test.js12
-rw-r--r--test_napi/src/arraybuffer.rs36
-rw-r--r--test_napi/src/lib.rs2
5 files changed, 52 insertions, 4 deletions
diff --git a/cli/napi/js_native_api.rs b/cli/napi/js_native_api.rs
index 32f4b787e..07b583beb 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::<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(())
}
diff --git a/test_napi/Cargo.toml b/test_napi/Cargo.toml
index 09faa4175..407275563 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 000000000..577443e8b
--- /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 000000000..cf170e713
--- /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 569256a0b..c1a13f6e4 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);