Skip to content

Commit

Permalink
fix(napi): fix is_detached_arraybuffer (denoland#16478)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosc90 committed Oct 30, 2022
1 parent 59ac110 commit 207dd8d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 4 deletions.
4 changes: 1 addition & 3 deletions cli/napi/js_native_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

Expand Down
2 changes: 1 addition & 1 deletion test_napi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
12 changes: 12 additions & 0 deletions test_napi/arraybuffer_test.js
Original file line number Diff line number Diff line change
@@ -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);
});
36 changes: 36 additions & 0 deletions test_napi/src/arraybuffer.rs
Original file line number Diff line number Diff line change
@@ -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())
};
}
2 changes: 2 additions & 0 deletions test_napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use napi_sys::*;

pub mod array;
pub mod arraybuffer;
pub mod r#async;
pub mod callback;
pub mod coerce;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 207dd8d

Please sign in to comment.