From 83ad9fa780295a272a7ca789f8a44cd58600a522 Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Tue, 12 Jul 2022 02:32:23 -0700 Subject: [PATCH] [napi] Implement `napi_get_property_names` --- src/bun.js/bindings/napi.cpp | 27 +++++++++++++++++++++++++++ src/symbols.dyn | 1 + src/symbols.txt | 3 ++- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/bun.js/bindings/napi.cpp b/src/bun.js/bindings/napi.cpp index 295a1e02b2..5ed1ef142f 100644 --- a/src/bun.js/bindings/napi.cpp +++ b/src/bun.js/bindings/napi.cpp @@ -1289,5 +1289,32 @@ extern "C" napi_status napi_coerce_to_string(napi_env env, napi_value value, return napi_generic_failure; } scope.clearException(); + return napi_ok; +} + +extern "C" napi_status napi_get_property_names(napi_env env, napi_value object, + napi_value* result) +{ + + Zig::GlobalObject* globalObject = toJS(env); + JSC::VM& vm = globalObject->vm(); + + JSC::JSValue jsValue = JSC::JSValue::decode(reinterpret_cast(object)); + if (!jsValue || !jsValue.isObject()) { + return napi_invalid_arg; + } + + auto scope = DECLARE_CATCH_SCOPE(vm); + JSC::EnsureStillAliveScope ensureStillAlive(jsValue); + JSC::JSValue value = JSC::ownPropertyKeys(globalObject, jsValue.getObject(), PropertyNameMode::Strings, DontEnumPropertiesMode::Include, std::nullopt); + if (UNLIKELY(scope.exception())) { + *result = reinterpret_cast(JSC::JSValue::encode(JSC::jsUndefined())); + return napi_generic_failure; + } + scope.clearException(); + JSC::EnsureStillAliveScope ensureStillAlive1(value); + + *result = toNapi(value); + return napi_ok; } \ No newline at end of file diff --git a/src/symbols.dyn b/src/symbols.dyn index 7957ef59a5..0e02635bcf 100644 --- a/src/symbols.dyn +++ b/src/symbols.dyn @@ -65,6 +65,7 @@ _napi_get_node_version; _napi_get_null; _napi_get_property; + _napi_get_property_names; _napi_get_prototype; _napi_get_reference_value; _napi_get_threadsafe_function_context; diff --git a/src/symbols.txt b/src/symbols.txt index 3ac1fa8432..a250f77eaa 100644 --- a/src/symbols.txt +++ b/src/symbols.txt @@ -64,6 +64,7 @@ _napi_get_new_target _napi_get_node_version _napi_get_null _napi_get_property +_napi_get_property_names _napi_get_prototype _napi_get_reference_value _napi_get_threadsafe_function_context @@ -122,4 +123,4 @@ _napi_throw_type_error _napi_typeof _napi_unref_threadsafe_function _napi_unwrap -_napi_wrap +_napi_wrap \ No newline at end of file