diff options
| author | Divy Srivastava <dj.srivastava23@gmail.com> | 2022-10-05 07:06:44 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-10-05 19:36:44 +0530 |
| commit | 0b016a7fb8639ce49603c8c339539174b191a4b1 (patch) | |
| tree | c511060d701db60ede0214b7280e89c5749bbe62 /test_napi/src/properties.rs | |
| parent | 3a3a8484069c9c6955fcb83ea761f9f74638175a (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/properties.rs')
| -rw-r--r-- | test_napi/src/properties.rs | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/test_napi/src/properties.rs b/test_napi/src/properties.rs new file mode 100644 index 000000000..89d95d6c6 --- /dev/null +++ b/test_napi/src/properties.rs @@ -0,0 +1,89 @@ +// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. + +use napi_sys::PropertyAttributes::*; +use napi_sys::Status::napi_ok; +use napi_sys::*; +use std::ptr; + +pub fn init(env: napi_env, exports: napi_value) { + let mut number: napi_value = ptr::null_mut(); + assert!(unsafe { napi_create_double(env, 1.0, &mut number) } == napi_ok); + + // Key name as napi_value representing `v8::String` + let mut name_value: napi_value = ptr::null_mut(); + assert!( + unsafe { + napi_create_string_utf8( + env, + "key_v8_string".as_ptr() as *const i8, + usize::MAX, + &mut name_value, + ) + } == napi_ok + ); + + // Key symbol + let mut symbol_description: napi_value = ptr::null_mut(); + let mut name_symbol: napi_value = ptr::null_mut(); + assert!( + unsafe { + napi_create_string_utf8( + env, + "key_v8_symbol".as_ptr() as *const i8, + usize::MAX, + &mut symbol_description, + ) + } == napi_ok + ); + assert!( + unsafe { napi_create_symbol(env, symbol_description, &mut name_symbol) } + == napi_ok + ); + + let properties = &[ + napi_property_descriptor { + utf8name: "test_property_rw\0".as_ptr() as *const i8, + name: ptr::null_mut(), + method: None, + getter: None, + setter: None, + data: ptr::null_mut(), + attributes: enumerable | writable, + value: number, + }, + napi_property_descriptor { + utf8name: "test_property_r\0".as_ptr() as *const i8, + name: ptr::null_mut(), + method: None, + getter: None, + setter: None, + data: ptr::null_mut(), + attributes: enumerable, + value: number, + }, + napi_property_descriptor { + utf8name: ptr::null(), + name: name_value, + method: None, + getter: None, + setter: None, + data: ptr::null_mut(), + attributes: enumerable, + value: number, + }, + napi_property_descriptor { + utf8name: ptr::null(), + name: name_symbol, + method: None, + getter: None, + setter: None, + data: ptr::null_mut(), + attributes: enumerable, + value: number, + }, + ]; + + unsafe { + napi_define_properties(env, exports, properties.len(), properties.as_ptr()) + }; +} |
