From 0b016a7fb8639ce49603c8c339539174b191a4b1 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Wed, 5 Oct 2022 07:06:44 -0700 Subject: feat(npm): implement Node API (#13633) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR implements the NAPI for loading native modules into Deno. Co-authored-by: Bartek IwaƄczuk Co-authored-by: DjDeveloper <43033058+DjDeveloperr@users.noreply.github.com> Co-authored-by: Ryan Dahl --- test_napi/src/typedarray.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 test_napi/src/typedarray.rs (limited to 'test_napi/src/typedarray.rs') diff --git a/test_napi/src/typedarray.rs b/test_napi/src/typedarray.rs new file mode 100644 index 000000000..f7d2e2f24 --- /dev/null +++ b/test_napi/src/typedarray.rs @@ -0,0 +1,53 @@ +// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. + +use core::ffi::c_void; +use napi_sys::Status::napi_ok; +use napi_sys::TypedarrayType::uint8_array; +use napi_sys::*; +use std::ptr; + +extern "C" fn test_external( + env: napi_env, + _info: napi_callback_info, +) -> napi_value { + let mut arraybuffer: napi_value = ptr::null_mut(); + let mut external: Box<[u8; 4]> = Box::new([0, 1, 2, 3]); + assert!( + unsafe { + napi_create_external_arraybuffer( + env, + external.as_mut_ptr() as *mut c_void, + external.len(), + None, + ptr::null_mut(), + &mut arraybuffer, + ) + } == napi_ok + ); + + let mut typedarray: napi_value = ptr::null_mut(); + assert!( + unsafe { + napi_create_typedarray( + env, + uint8_array, + external.len(), + arraybuffer, + 0, + &mut typedarray, + ) + } == napi_ok + ); + + std::mem::forget(external); // Leak into JS land + typedarray +} + +pub fn init(env: napi_env, exports: napi_value) { + let properties = + &[crate::new_property!(env, "test_external\0", test_external)]; + + unsafe { + napi_define_properties(env, exports, properties.len(), properties.as_ptr()) + }; +} -- cgit v1.2.3