Skip to content

Commit aefde43

Browse files
committed
test: user data in function property descriptor
PR-URL: nodejs/node-addon-api#652 Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
1 parent 5ad9b55 commit aefde43

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

test/object/object.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ Value TestFunction(const CallbackInfo& info) {
6464
return Boolean::New(info.Env(), true);
6565
}
6666

67+
Value TestFunctionWithUserData(const CallbackInfo& info) {
68+
UserDataHolder* holder = reinterpret_cast<UserDataHolder*>(info.Data());
69+
return Number::New(info.Env(), holder->value);
70+
}
71+
6772
Array GetPropertyNames(const CallbackInfo& info) {
6873
Object obj = info[0].As<Object>();
6974
Array arr = obj.GetPropertyNames();
@@ -104,6 +109,7 @@ void DefineProperties(const CallbackInfo& info) {
104109
PropertyDescriptor::Value("enumerableValue", trueValue, napi_enumerable),
105110
PropertyDescriptor::Value("configurableValue", trueValue, napi_configurable),
106111
PropertyDescriptor::Function(env, obj, "function", TestFunction),
112+
PropertyDescriptor::Function(env, obj, "functionWithUserData", TestFunctionWithUserData, napi_property_attributes::napi_default, reinterpret_cast<void*>(holder)),
107113
});
108114
} else if (nameType.Utf8Value() == "string") {
109115
// VS2013 has lifetime issues when passing temporary objects into the constructor of another
@@ -125,6 +131,7 @@ void DefineProperties(const CallbackInfo& info) {
125131
std::string str5("enumerableValue");
126132
std::string str6("configurableValue");
127133
std::string str7("function");
134+
std::string str8("functionWithUserData");
128135

129136
obj.DefineProperties({
130137
PropertyDescriptor::Accessor(env, obj, str1, TestGetter),
@@ -148,6 +155,7 @@ void DefineProperties(const CallbackInfo& info) {
148155
PropertyDescriptor::Value(str5, trueValue, napi_enumerable),
149156
PropertyDescriptor::Value(str6, trueValue, napi_configurable),
150157
PropertyDescriptor::Function(env, obj, str7, TestFunction),
158+
PropertyDescriptor::Function(env, obj, str8, TestFunctionWithUserData, napi_property_attributes::napi_default, reinterpret_cast<void*>(holder)),
151159
});
152160
} else if (nameType.Utf8Value() == "value") {
153161
obj.DefineProperties({
@@ -184,6 +192,8 @@ void DefineProperties(const CallbackInfo& info) {
184192
Napi::String::New(env, "configurableValue"), trueValue, napi_configurable),
185193
PropertyDescriptor::Function(env, obj,
186194
Napi::String::New(env, "function"), TestFunction),
195+
PropertyDescriptor::Function(env, obj,
196+
Napi::String::New(env, "functionWithUserData"), TestFunctionWithUserData, napi_property_attributes::napi_default, reinterpret_cast<void*>(holder)),
187197
});
188198
}
189199
}

test/object/object.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ function test(binding) {
9595
assertPropertyIsNot(obj, 'function', 'enumerable');
9696
assertPropertyIsNot(obj, 'function', 'configurable');
9797
assert.strictEqual(obj.function(), true);
98+
assert.strictEqual(obj.functionWithUserData(), obj.readonlyAccessorWithUserDataT);
9899
}
99100

100101
testDefineProperties('literal');

0 commit comments

Comments
 (0)