Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: user data in function property descriptor #652

Closed
Closed
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
12 changes: 12 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* functionHolder = reinterpret_cast<UserDataHolder*>(info.Data());
return Number::New(info.Env(), functionHolder->value);
}

Array GetPropertyNames(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
Array arr = obj.GetPropertyNames();
Expand All @@ -77,7 +82,9 @@ void DefineProperties(const CallbackInfo& info) {

Boolean trueValue = Boolean::New(env, true);
UserDataHolder* holder = new UserDataHolder();
UserDataHolder* functionHolder = new UserDataHolder();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its safe to use the holder above rather than creating a new one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I thought about that, but... didn't 😄 ... Addressed

holder->value = 1234;
functionHolder->value = 4321;

if (nameType.Utf8Value() == "literal") {
obj.DefineProperties({
Expand All @@ -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<void*>(functionHolder)),
});
} else if (nameType.Utf8Value() == "string") {
// VS2013 has lifetime issues when passing temporary objects into the constructor of another
Expand All @@ -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),
Expand All @@ -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<void*>(functionHolder)),
});
} else if (nameType.Utf8Value() == "value") {
obj.DefineProperties({
Expand Down Expand Up @@ -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<void*>(functionHolder)),
});
}
}
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(), 4321);
}

testDefineProperties('literal');
Expand Down