Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
run format-cpp
  • Loading branch information
miguelmarcondesf committed Sep 20, 2025
commit 508a4bdcef86c66079a36b3834d5d7d129e3522b
31 changes: 19 additions & 12 deletions benchmark/napi/create_object_with_properties/binding.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <node_api.h>
#include <assert.h>
#include <node_api.h>

// Creating with many options because complains are when ~20 properties
static void CreateTestProperties(napi_env env,
Expand Down Expand Up @@ -72,12 +72,8 @@ static napi_value CreateObjectWithPropertiesNew(napi_env env,

for (uint32_t i = 0; i < count; i++) {
napi_value obj;
napi_create_object_with_properties(env,
null_prototype,
names,
values,
20,
&obj);
napi_create_object_with_properties(
env, null_prototype, names, values, 20, &obj);
}

napi_call_function(env, bench_obj, end_fn, 1, &count_val, nullptr);
Expand Down Expand Up @@ -138,13 +134,24 @@ static napi_value CreateObjectWithPropertiesOld(napi_env env,

NAPI_MODULE_INIT() {
napi_property_descriptor desc[] = {
{ "createObjectWithPropertiesNew", 0, CreateObjectWithPropertiesNew, 0, 0,
0, napi_default, 0 },
{ "createObjectWithPropertiesOld", 0, CreateObjectWithPropertiesOld, 0, 0,
0, napi_default, 0 },
{"createObjectWithPropertiesNew",
0,
CreateObjectWithPropertiesNew,
0,
0,
0,
napi_default,
0},
{"createObjectWithPropertiesOld",
0,
CreateObjectWithPropertiesOld,
0,
0,
0,
napi_default,
0},
};

napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
return exports;
}

14 changes: 7 additions & 7 deletions src/js_native_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -628,13 +628,13 @@ NAPI_EXTERN napi_status NAPI_CDECL napi_object_freeze(napi_env env,
napi_value object);
NAPI_EXTERN napi_status NAPI_CDECL napi_object_seal(napi_env env,
napi_value object);
NAPI_EXTERN napi_status NAPI_CDECL napi_create_object_with_properties(
napi_env env,
napi_value prototype_or_null,
napi_value* property_names,
napi_value* property_values,
size_t property_count,
napi_value* result);
NAPI_EXTERN napi_status NAPI_CDECL
napi_create_object_with_properties(napi_env env,
Comment thread
miguelmarcondesf marked this conversation as resolved.
Outdated
napi_value prototype_or_null,
napi_value* property_names,
napi_value* property_values,
size_t property_count,
napi_value* result);
#endif // NAPI_VERSION >= 8

EXTERN_C_END
Expand Down
23 changes: 10 additions & 13 deletions src/js_native_api_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1591,13 +1591,13 @@ napi_status NAPI_CDECL napi_create_object(napi_env env, napi_value* result) {
return napi_clear_last_error(env);
}

napi_status NAPI_CDECL napi_create_object_with_properties(
napi_env env,
napi_value prototype_or_null,
napi_value* property_names,
napi_value* property_values,
size_t property_count,
napi_value* result) {
napi_status NAPI_CDECL
napi_create_object_with_properties(napi_env env,
napi_value prototype_or_null,
napi_value* property_names,
napi_value* property_values,
size_t property_count,
napi_value* result) {
CHECK_ENV_NOT_IN_GC(env);
CHECK_ARG(env, result);

Expand All @@ -1607,7 +1607,7 @@ napi_status NAPI_CDECL napi_create_object_with_properties(
}

v8::Local<v8::Value> v8_prototype_or_null =
v8impl::V8LocalValueFromJsValue(prototype_or_null);
v8impl::V8LocalValueFromJsValue(prototype_or_null);

v8::Local<v8::Object> obj;

Expand All @@ -1629,11 +1629,8 @@ napi_status NAPI_CDECL napi_create_object_with_properties(
v8_values.data(),
property_count);
} else {
obj = v8::Object::New(env->isolate,
v8_prototype_or_null,
nullptr,
nullptr,
0);
obj = v8::Object::New(
env->isolate, v8_prototype_or_null, nullptr, nullptr, 0);
Comment thread
miguelmarcondesf marked this conversation as resolved.
Outdated
}

RETURN_STATUS_IF_FALSE(env, !obj.IsEmpty(), napi_generic_failure);
Expand Down
64 changes: 43 additions & 21 deletions test/js-native-api/test_object/test_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -710,40 +710,49 @@ CheckTypeTag(napi_env env, napi_callback_info info) {
return js_result;
}

static napi_value TestCreateObjectWithProperties(napi_env env, napi_callback_info info) {
static napi_value TestCreateObjectWithProperties(napi_env env,
napi_callback_info info) {
napi_value names[3];
napi_value values[3];
napi_value result;

NODE_API_CALL(env, napi_create_string_utf8(env, "name", NAPI_AUTO_LENGTH, &names[0]));
NODE_API_CALL(env, napi_create_string_utf8(env, "Foo", NAPI_AUTO_LENGTH, &values[0]));
NODE_API_CALL(
env, napi_create_string_utf8(env, "name", NAPI_AUTO_LENGTH, &names[0]));
NODE_API_CALL(
env, napi_create_string_utf8(env, "Foo", NAPI_AUTO_LENGTH, &values[0]));

NODE_API_CALL(env, napi_create_string_utf8(env, "age", NAPI_AUTO_LENGTH, &names[1]));
NODE_API_CALL(
env, napi_create_string_utf8(env, "age", NAPI_AUTO_LENGTH, &names[1]));
NODE_API_CALL(env, napi_create_int32(env, 42, &values[1]));

NODE_API_CALL(env, napi_create_string_utf8(env, "active", NAPI_AUTO_LENGTH, &names[2]));
NODE_API_CALL(
env, napi_create_string_utf8(env, "active", NAPI_AUTO_LENGTH, &names[2]));
NODE_API_CALL(env, napi_get_boolean(env, true, &values[2]));

napi_value null_prototype;
NODE_API_CALL(env, napi_get_null(env, &null_prototype));
NODE_API_CALL(env, napi_create_object_with_properties(
env, null_prototype, names, values, 3, &result));
NODE_API_CALL(env,
napi_create_object_with_properties(
env, null_prototype, names, values, 3, &result));

return result;
}

static napi_value TestCreateObjectWithPropertiesEmpty(napi_env env, napi_callback_info info) {
static napi_value TestCreateObjectWithPropertiesEmpty(napi_env env,
napi_callback_info info) {
napi_value result;

napi_value null_prototype;
NODE_API_CALL(env, napi_get_null(env, &null_prototype));
NODE_API_CALL(env, napi_create_object_with_properties(
env, null_prototype, NULL, NULL, 0, &result));
NODE_API_CALL(env,
napi_create_object_with_properties(
env, null_prototype, NULL, NULL, 0, &result));

return result;
}

static napi_value TestCreateObjectWithCustomPrototype(napi_env env, napi_callback_info info) {
static napi_value TestCreateObjectWithCustomPrototype(napi_env env,
napi_callback_info info) {
napi_value prototype;
napi_value method_name;
napi_value method_func;
Expand All @@ -752,16 +761,26 @@ static napi_value TestCreateObjectWithCustomPrototype(napi_env env, napi_callbac
napi_value result;

NODE_API_CALL(env, napi_create_object(env, &prototype));
NODE_API_CALL(env, napi_create_string_utf8(env, "test", NAPI_AUTO_LENGTH, &method_name));
NODE_API_CALL(env, napi_create_function(env, "test", NAPI_AUTO_LENGTH,
TestCreateObjectWithProperties, NULL, &method_func));
NODE_API_CALL(env, napi_set_property(env, prototype, method_name, method_func));
NODE_API_CALL(
env,
napi_create_string_utf8(env, "test", NAPI_AUTO_LENGTH, &method_name));
NODE_API_CALL(env,
napi_create_function(env,
"test",
NAPI_AUTO_LENGTH,
TestCreateObjectWithProperties,
NULL,
&method_func));
NODE_API_CALL(env,
napi_set_property(env, prototype, method_name, method_func));

NODE_API_CALL(env, napi_create_string_utf8(env, "value", NAPI_AUTO_LENGTH, &names[0]));
NODE_API_CALL(
env, napi_create_string_utf8(env, "value", NAPI_AUTO_LENGTH, &names[0]));
NODE_API_CALL(env, napi_create_int32(env, 42, &values[0]));

NODE_API_CALL(env, napi_create_object_with_properties(
env, prototype, names, values, 1, &result));
NODE_API_CALL(env,
napi_create_object_with_properties(
env, prototype, names, values, 1, &result));

return result;
}
Expand Down Expand Up @@ -799,9 +818,12 @@ napi_value Init(napi_env env, napi_value exports) {
DECLARE_NODE_API_PROPERTY("TestGetProperty", TestGetProperty),
DECLARE_NODE_API_PROPERTY("TestFreeze", TestFreeze),
DECLARE_NODE_API_PROPERTY("TestSeal", TestSeal),
DECLARE_NODE_API_PROPERTY("TestCreateObjectWithProperties", TestCreateObjectWithProperties),
DECLARE_NODE_API_PROPERTY("TestCreateObjectWithPropertiesEmpty", TestCreateObjectWithPropertiesEmpty),
DECLARE_NODE_API_PROPERTY("TestCreateObjectWithCustomPrototype", TestCreateObjectWithCustomPrototype),
DECLARE_NODE_API_PROPERTY("TestCreateObjectWithProperties",
TestCreateObjectWithProperties),
DECLARE_NODE_API_PROPERTY("TestCreateObjectWithPropertiesEmpty",
TestCreateObjectWithPropertiesEmpty),
DECLARE_NODE_API_PROPERTY("TestCreateObjectWithCustomPrototype",
TestCreateObjectWithCustomPrototype),
};

init_test_null(env, exports);
Expand Down
Loading