From 9b338f9ed0a8dcb781566b61bc06b67f07ca23a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9e=20Kooi?= Date: Wed, 8 Apr 2020 17:19:49 +0200 Subject: [PATCH] Check `hasOwnProperty` after conversion to string I thought I could be clever and micro-optimize this a bit, but `has_own_property` really does require a string argument, so we have to do the conversion first. --- crates/neon-runtime/src/napi/object.rs | 24 ++++++++++++------------ test/napi/lib/hello.js | 2 +- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/crates/neon-runtime/src/napi/object.rs b/crates/neon-runtime/src/napi/object.rs index 5494f0aeb..5cefb253a 100644 --- a/crates/neon-runtime/src/napi/object.rs +++ b/crates/neon-runtime/src/napi/object.rs @@ -32,7 +32,7 @@ pub unsafe extern "C" fn get_own_property_names(out: &mut Local, env: Env, objec } let raw_names = raw_names.assume_init(); - let mut fixed_names = fixed_names.assume_init(); + let fixed_names = fixed_names.assume_init(); *out = fixed_names; @@ -47,17 +47,6 @@ pub unsafe extern "C" fn get_own_property_names(out: &mut Local, env: Env, objec continue; } - let mut is_own_property = false; - // May return a non-OK status if `key` is not a string or a Symbol, but here it is always - // a string. - if napi::napi_has_own_property(env, object, property_name, &mut is_own_property as *mut _) != napi::napi_status::napi_ok { - return false; - } - - if !is_own_property { - continue; - } - // Before https://github.com/nodejs/node/pull/27524, `napi_get_property_names` would return // numbers for numeric indices instead of strings. // Make sure we always return strings. @@ -72,6 +61,17 @@ pub unsafe extern "C" fn get_own_property_names(out: &mut Local, env: Env, objec property_name }; + let mut is_own_property = false; + // May return a non-OK status if `key` is not a string or a Symbol, but here it is always + // a string. + if napi::napi_has_own_property(env, object, property_name, &mut is_own_property as *mut _) != napi::napi_status::napi_ok { + return false; + } + + if !is_own_property { + continue; + } + let mut dummy = false; // If we can't convert assign to this array, something went wrong. if !set_index(&mut dummy, env, fixed_names, fixed_len, property_name) { diff --git a/test/napi/lib/hello.js b/test/napi/lib/hello.js index 1b07c2322..becef4295 100644 --- a/test/napi/lib/hello.js +++ b/test/napi/lib/hello.js @@ -25,6 +25,6 @@ describe('hello', function() { 0: 1, a: 1, whatever: true - }) + }); }); });