@@ -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+
6772Array 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}
0 commit comments