From ec4b71e0fd716c5229e55e68cd8a9c21f81b8542 Mon Sep 17 00:00:00 2001 From: Kevin Eady <8634912+KevinEady@users.noreply.github.com> Date: Sat, 18 Jan 2020 13:01:31 +0100 Subject: [PATCH 1/2] test: user data in function property descriptor --- test/object/object.cc | 12 ++++++++++++ test/object/object.js | 1 + 2 files changed, 13 insertions(+) diff --git a/test/object/object.cc b/test/object/object.cc index 1c9ce59f9..82b8e29ae 100644 --- a/test/object/object.cc +++ b/test/object/object.cc @@ -64,6 +64,11 @@ Value TestFunction(const CallbackInfo& info) { return Boolean::New(info.Env(), true); } +Value TestFunctionWithUserData(const CallbackInfo& info) { + UserDataHolder* functionHolder = reinterpret_cast(info.Data()); + return Number::New(info.Env(), functionHolder->value); +} + Array GetPropertyNames(const CallbackInfo& info) { Object obj = info[0].As(); Array arr = obj.GetPropertyNames(); @@ -77,7 +82,9 @@ void DefineProperties(const CallbackInfo& info) { Boolean trueValue = Boolean::New(env, true); UserDataHolder* holder = new UserDataHolder(); + UserDataHolder* functionHolder = new UserDataHolder(); holder->value = 1234; + functionHolder->value = 4321; if (nameType.Utf8Value() == "literal") { obj.DefineProperties({ @@ -104,6 +111,7 @@ void DefineProperties(const CallbackInfo& info) { PropertyDescriptor::Value("enumerableValue", trueValue, napi_enumerable), PropertyDescriptor::Value("configurableValue", trueValue, napi_configurable), PropertyDescriptor::Function(env, obj, "function", TestFunction), + PropertyDescriptor::Function(env, obj, "functionWithUserData", TestFunctionWithUserData, napi_property_attributes::napi_default, reinterpret_cast(functionHolder)), }); } else if (nameType.Utf8Value() == "string") { // VS2013 has lifetime issues when passing temporary objects into the constructor of another @@ -125,6 +133,7 @@ void DefineProperties(const CallbackInfo& info) { std::string str5("enumerableValue"); std::string str6("configurableValue"); std::string str7("function"); + std::string str8("functionWithUserData"); obj.DefineProperties({ PropertyDescriptor::Accessor(env, obj, str1, TestGetter), @@ -148,6 +157,7 @@ void DefineProperties(const CallbackInfo& info) { PropertyDescriptor::Value(str5, trueValue, napi_enumerable), PropertyDescriptor::Value(str6, trueValue, napi_configurable), PropertyDescriptor::Function(env, obj, str7, TestFunction), + PropertyDescriptor::Function(env, obj, str8, TestFunctionWithUserData, napi_property_attributes::napi_default, reinterpret_cast(functionHolder)), }); } else if (nameType.Utf8Value() == "value") { obj.DefineProperties({ @@ -184,6 +194,8 @@ void DefineProperties(const CallbackInfo& info) { Napi::String::New(env, "configurableValue"), trueValue, napi_configurable), PropertyDescriptor::Function(env, obj, Napi::String::New(env, "function"), TestFunction), + PropertyDescriptor::Function(env, obj, + Napi::String::New(env, "functionWithUserData"), TestFunctionWithUserData, napi_property_attributes::napi_default, reinterpret_cast(functionHolder)), }); } } diff --git a/test/object/object.js b/test/object/object.js index 2660e4b6e..fc6da8e7f 100644 --- a/test/object/object.js +++ b/test/object/object.js @@ -95,6 +95,7 @@ function test(binding) { assertPropertyIsNot(obj, 'function', 'enumerable'); assertPropertyIsNot(obj, 'function', 'configurable'); assert.strictEqual(obj.function(), true); + assert.strictEqual(obj.functionWithUserData(), 4321); } testDefineProperties('literal'); From 5f89e634efa02870e98bf0ef96dec7695320386c Mon Sep 17 00:00:00 2001 From: Kevin Eady <8634912+KevinEady@users.noreply.github.com> Date: Sat, 18 Jan 2020 15:56:14 +0100 Subject: [PATCH 2/2] test: user data in function property descriptor --- test/object/object.cc | 12 +++++------- test/object/object.js | 2 +- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/test/object/object.cc b/test/object/object.cc index 82b8e29ae..2c0ce420b 100644 --- a/test/object/object.cc +++ b/test/object/object.cc @@ -65,8 +65,8 @@ Value TestFunction(const CallbackInfo& info) { } Value TestFunctionWithUserData(const CallbackInfo& info) { - UserDataHolder* functionHolder = reinterpret_cast(info.Data()); - return Number::New(info.Env(), functionHolder->value); + UserDataHolder* holder = reinterpret_cast(info.Data()); + return Number::New(info.Env(), holder->value); } Array GetPropertyNames(const CallbackInfo& info) { @@ -82,9 +82,7 @@ void DefineProperties(const CallbackInfo& info) { Boolean trueValue = Boolean::New(env, true); UserDataHolder* holder = new UserDataHolder(); - UserDataHolder* functionHolder = new UserDataHolder(); holder->value = 1234; - functionHolder->value = 4321; if (nameType.Utf8Value() == "literal") { obj.DefineProperties({ @@ -111,7 +109,7 @@ void DefineProperties(const CallbackInfo& info) { PropertyDescriptor::Value("enumerableValue", trueValue, napi_enumerable), PropertyDescriptor::Value("configurableValue", trueValue, napi_configurable), PropertyDescriptor::Function(env, obj, "function", TestFunction), - PropertyDescriptor::Function(env, obj, "functionWithUserData", TestFunctionWithUserData, napi_property_attributes::napi_default, reinterpret_cast(functionHolder)), + PropertyDescriptor::Function(env, obj, "functionWithUserData", TestFunctionWithUserData, napi_property_attributes::napi_default, reinterpret_cast(holder)), }); } else if (nameType.Utf8Value() == "string") { // VS2013 has lifetime issues when passing temporary objects into the constructor of another @@ -157,7 +155,7 @@ void DefineProperties(const CallbackInfo& info) { PropertyDescriptor::Value(str5, trueValue, napi_enumerable), PropertyDescriptor::Value(str6, trueValue, napi_configurable), PropertyDescriptor::Function(env, obj, str7, TestFunction), - PropertyDescriptor::Function(env, obj, str8, TestFunctionWithUserData, napi_property_attributes::napi_default, reinterpret_cast(functionHolder)), + PropertyDescriptor::Function(env, obj, str8, TestFunctionWithUserData, napi_property_attributes::napi_default, reinterpret_cast(holder)), }); } else if (nameType.Utf8Value() == "value") { obj.DefineProperties({ @@ -195,7 +193,7 @@ void DefineProperties(const CallbackInfo& info) { PropertyDescriptor::Function(env, obj, Napi::String::New(env, "function"), TestFunction), PropertyDescriptor::Function(env, obj, - Napi::String::New(env, "functionWithUserData"), TestFunctionWithUserData, napi_property_attributes::napi_default, reinterpret_cast(functionHolder)), + Napi::String::New(env, "functionWithUserData"), TestFunctionWithUserData, napi_property_attributes::napi_default, reinterpret_cast(holder)), }); } } diff --git a/test/object/object.js b/test/object/object.js index fc6da8e7f..8741e27f1 100644 --- a/test/object/object.js +++ b/test/object/object.js @@ -95,7 +95,7 @@ function test(binding) { assertPropertyIsNot(obj, 'function', 'enumerable'); assertPropertyIsNot(obj, 'function', 'configurable'); assert.strictEqual(obj.function(), true); - assert.strictEqual(obj.functionWithUserData(), 4321); + assert.strictEqual(obj.functionWithUserData(), obj.readonlyAccessorWithUserDataT); } testDefineProperties('literal');