summaryrefslogtreecommitdiff
path: root/cli/napi/js_native_api.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2023-01-10 15:35:46 +0100
committerGitHub <noreply@github.com>2023-01-10 15:35:46 +0100
commit0329bc69dabbcc4d57ff9d34d695ffd4ddb1de4f (patch)
treeaa54ee523d47191c4816fe8d70839f8d892e3ba5 /cli/napi/js_native_api.rs
parent636352e0ca1e611c7673f2ab68538e1ddb2dc5b7 (diff)
fix(napi): handle static properties in classes (#17320)
Adds support for static properties when using "napi_define_class".
Diffstat (limited to 'cli/napi/js_native_api.rs')
-rw-r--r--cli/napi/js_native_api.rs40
1 files changed, 37 insertions, 3 deletions
diff --git a/cli/napi/js_native_api.rs b/cli/napi/js_native_api.rs
index 0c8bc31d7..c72da9b02 100644
--- a/cli/napi/js_native_api.rs
+++ b/cli/napi/js_native_api.rs
@@ -296,7 +296,6 @@ fn napi_create_error(
let msg = transmute::<napi_value, v8::Local<v8::Value>>(msg);
let msg = msg.to_string(&mut env.scope()).unwrap();
-
let error = v8::Exception::error(&mut env.scope(), msg);
*result = error.into();
@@ -1134,8 +1133,15 @@ fn napi_define_class(
let scope = &mut env.scope();
let napi_properties: &[napi_property_descriptor] =
std::slice::from_raw_parts(properties, property_count);
+ let mut static_property_count = 0;
for p in napi_properties {
+ if p.attributes & napi_static != 0 {
+ // Will be handled below
+ static_property_count += 1;
+ continue;
+ }
+
let name = if !p.utf8name.is_null() {
let name_str = CStr::from_ptr(p.utf8name).to_str().unwrap();
v8::String::new(scope, name_str).unwrap()
@@ -1197,9 +1203,37 @@ fn napi_define_class(
let value: v8::Local<v8::Value> = tpl.get_function(scope).unwrap().into();
*result = value.into();
+
+ if static_property_count > 0 {
+ let mut static_descriptors = Vec::with_capacity(static_property_count);
+
+ for p in napi_properties {
+ if p.attributes & napi_static != 0 {
+ static_descriptors.push(*p);
+ }
+ }
+
+ let res = napi_define_properties(
+ env_ptr,
+ *result,
+ static_descriptors.len(),
+ static_descriptors.as_ptr() as *const napi_property_descriptor,
+ );
+
+ napi_status_to_result(res)?;
+ }
+
Ok(())
}
+fn napi_status_to_result(status: napi_status) -> Result {
+ if status == napi_ok {
+ Ok(())
+ } else {
+ Err(status.into())
+ }
+}
+
#[napi_sym::napi_sym]
fn napi_define_properties(
env_ptr: *mut Env,
@@ -1211,10 +1245,10 @@ fn napi_define_properties(
let scope = &mut env.scope();
let object = transmute::<napi_value, v8::Local<v8::Object>>(obj);
let properties = std::slice::from_raw_parts(properties, property_count);
-
for property in properties {
let name = if !property.utf8name.is_null() {
- let name_str = CStr::from_ptr(property.utf8name).to_str().unwrap();
+ let name_str = CStr::from_ptr(property.utf8name);
+ let name_str = name_str.to_str().unwrap();
v8::String::new(scope, name_str).unwrap()
} else {
transmute::<napi_value, v8::Local<v8::String>>(property.name)