From b96bbc32c8a0828f997f6148111a31ec27ec08b1 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Mon, 23 Jan 2023 00:52:18 -0800 Subject: [PATCH] fix(napi): improve napi_is_detached_arraybuffer (#17498) --- cli/napi/js_native_api.rs | 15 ++++++++++++--- test_napi/arraybuffer_test.js | 14 +++++++++++++- test_napi/src/arraybuffer.rs | 21 ++++++++++++++++++++- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/cli/napi/js_native_api.rs b/cli/napi/js_native_api.rs index 9f043ec94c..05e07b1e5b 100644 --- a/cli/napi/js_native_api.rs +++ b/cli/napi/js_native_api.rs @@ -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::::try_from(value).unwrap(); - *result = _ab.was_detached(); + + *result = match v8::Local::::try_from(value) { + Ok(array_buffer) => array_buffer.was_detached(), + Err(_) => false, + }; + + napi_clear_last_error(env); + Ok(()) } diff --git a/test_napi/arraybuffer_test.js b/test_napi/arraybuffer_test.js index c08ffc7d60..3e4e3c6c1f 100644 --- a/test_napi/arraybuffer_test.js +++ b/test_napi/arraybuffer_test.js @@ -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)); + }); +}); diff --git a/test_napi/src/arraybuffer.rs b/test_napi/src/arraybuffer.rs index 96d89f9d96..8d6f756bfb 100644 --- a/test_napi/src/arraybuffer.rs +++ b/test_napi/src/arraybuffer.rs @@ -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,