Skip to content

Commit

Permalink
test: user data in function property descriptor
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Marlyfleitas committed Jan 21, 2020
1 parent 077a5d6 commit a4cf4ec
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
10 changes: 10 additions & 0 deletions test/object/object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ Value TestFunction(const CallbackInfo& info) {
return Boolean::New(info.Env(), true);
}

Value TestFunctionWithUserData(const CallbackInfo& info) {
UserDataHolder* holder = reinterpret_cast<UserDataHolder*>(info.Data());
return Number::New(info.Env(), holder->value);
}

Array GetPropertyNames(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
Array arr = obj.GetPropertyNames();
Expand Down Expand Up @@ -104,6 +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<void*>(holder)),
});
} else if (nameType.Utf8Value() == "string") {
// VS2013 has lifetime issues when passing temporary objects into the constructor of another
Expand All @@ -125,6 +131,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),
Expand All @@ -148,6 +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<void*>(holder)),
});
} else if (nameType.Utf8Value() == "value") {
obj.DefineProperties({
Expand Down Expand Up @@ -184,6 +192,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<void*>(holder)),
});
}
}
Expand Down
1 change: 1 addition & 0 deletions test/object/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ function test(binding) {
assertPropertyIsNot(obj, 'function', 'enumerable');
assertPropertyIsNot(obj, 'function', 'configurable');
assert.strictEqual(obj.function(), true);
assert.strictEqual(obj.functionWithUserData(), obj.readonlyAccessorWithUserDataT);
}

testDefineProperties('literal');
Expand Down

0 comments on commit a4cf4ec

Please sign in to comment.