summaryrefslogtreecommitdiff
path: root/test_napi/src/typedarray.rs
diff options
context:
space:
mode:
authorDivy Srivastava <dj.srivastava23@gmail.com>2022-10-05 07:06:44 -0700
committerGitHub <noreply@github.com>2022-10-05 19:36:44 +0530
commit0b016a7fb8639ce49603c8c339539174b191a4b1 (patch)
treec511060d701db60ede0214b7280e89c5749bbe62 /test_napi/src/typedarray.rs
parent3a3a8484069c9c6955fcb83ea761f9f74638175a (diff)
feat(npm): implement Node API (#13633)
This PR implements the NAPI for loading native modules into Deno. Co-authored-by: Bartek IwaƄczuk <biwanczuk@gmail.com> Co-authored-by: DjDeveloper <43033058+DjDeveloperr@users.noreply.github.com> Co-authored-by: Ryan Dahl <ry@tinyclouds.org>
Diffstat (limited to 'test_napi/src/typedarray.rs')
-rw-r--r--test_napi/src/typedarray.rs53
1 files changed, 53 insertions, 0 deletions
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())
+ };
+}