diff options
author | Divy Srivastava <dj.srivastava23@gmail.com> | 2023-02-10 18:20:47 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-10 12:50:47 +0000 |
commit | 4baaa246a256a82e725ddf07015bad72ec13f3e5 (patch) | |
tree | 6c98c16cc443b76700357e6fc9edcfa133ac5940 /test_napi/src | |
parent | 1740e0fc3650dbf072ada4a530618db2a84a0854 (diff) |
fix(cli/napi): handle all property variants in napi_define_properties (#17680)
Fixes https://github.com/denoland/deno/issues/17509
This fixes the bug that blocked loading `fsevents` in Deno.
Diffstat (limited to 'test_napi/src')
-rw-r--r-- | test_napi/src/lib.rs | 2 | ||||
-rw-r--r-- | test_napi/src/properties.rs | 66 |
2 files changed, 48 insertions, 20 deletions
diff --git a/test_napi/src/lib.rs b/test_napi/src/lib.rs index ed0afb741..dba9f65a5 100644 --- a/test_napi/src/lib.rs +++ b/test_napi/src/lib.rs @@ -27,7 +27,7 @@ pub mod typedarray; #[macro_export] macro_rules! cstr { ($s: literal) => {{ - std::ffi::CString::new($s).unwrap().as_ptr() + std::ffi::CString::new($s).unwrap().into_raw() }}; } diff --git a/test_napi/src/properties.rs b/test_napi/src/properties.rs index a54738cb5..1b6c9488b 100644 --- a/test_napi/src/properties.rs +++ b/test_napi/src/properties.rs @@ -1,11 +1,28 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. use crate::assert_napi_ok; +use crate::cstr; use napi_sys::PropertyAttributes::*; use napi_sys::*; -use std::os::raw::c_char; use std::ptr; +static NICE: i64 = 69; + +fn init_constants(env: napi_env) -> napi_value { + let mut constants: napi_value = ptr::null_mut(); + let mut value: napi_value = ptr::null_mut(); + + assert_napi_ok!(napi_create_object(env, &mut constants)); + assert_napi_ok!(napi_create_int64(env, NICE, &mut value)); + assert_napi_ok!(napi_set_named_property( + env, + constants, + cstr!("nice"), + value + )); + constants +} + pub fn init(env: napi_env, exports: napi_value) { let mut number: napi_value = ptr::null_mut(); assert_napi_ok!(napi_create_double(env, 1.0, &mut number)); @@ -14,7 +31,7 @@ pub fn init(env: napi_env, exports: napi_value) { let mut name_value: napi_value = ptr::null_mut(); assert_napi_ok!(napi_create_string_utf8( env, - "key_v8_string".as_ptr() as *const c_char, + cstr!("key_v8_string"), usize::MAX, &mut name_value, )); @@ -24,7 +41,7 @@ pub fn init(env: napi_env, exports: napi_value) { let mut name_symbol: napi_value = ptr::null_mut(); assert_napi_ok!(napi_create_string_utf8( env, - "key_v8_symbol".as_ptr() as *const c_char, + cstr!("key_v8_symbol"), usize::MAX, &mut symbol_description, )); @@ -36,38 +53,28 @@ pub fn init(env: napi_env, exports: napi_value) { let properties = &[ napi_property_descriptor { - utf8name: "test_property_rw\0".as_ptr() as *const c_char, + utf8name: cstr!("test_simple_property"), name: ptr::null_mut(), method: None, getter: None, setter: None, data: ptr::null_mut(), attributes: enumerable | writable, - value: number, + value: init_constants(env), }, napi_property_descriptor { - utf8name: "test_property_r\0".as_ptr() as *const c_char, + utf8name: cstr!("test_property_rw"), 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, + attributes: enumerable | writable, value: number, }, napi_property_descriptor { - utf8name: ptr::null(), - name: name_symbol, + utf8name: cstr!("test_property_r"), + name: ptr::null_mut(), method: None, getter: None, setter: None, @@ -75,6 +82,27 @@ pub fn init(env: napi_env, exports: napi_value) { attributes: enumerable, value: number, }, + // TODO(@littledivy): Fix this. + // 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, + // }, ]; assert_napi_ok!(napi_define_properties( |