From 46484202caf180d7c16734b6c5a3f0cc88ee6dd0 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] test: user data in function property descriptor PR-URL: https://github.com/nodejs/node-addon-api/pull/652 Reviewed-By: Gabriel Schulhof Reviewed-By: Chengzhong Wu Reviewed-By: Michael Dawson --- test/object/object.cc | 10 ++++++++++ test/object/object.js | 1 + 2 files changed, 11 insertions(+) diff --git a/test/object/object.cc b/test/object/object.cc index 1c9ce59f9..2c0ce420b 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* holder = reinterpret_cast(info.Data()); + return Number::New(info.Env(), holder->value); +} + Array GetPropertyNames(const CallbackInfo& info) { Object obj = info[0].As(); Array arr = obj.GetPropertyNames(); @@ -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(holder)), }); } else if (nameType.Utf8Value() == "string") { // VS2013 has lifetime issues when passing temporary objects into the constructor of another @@ -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), @@ -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(holder)), }); } else if (nameType.Utf8Value() == "value") { obj.DefineProperties({ @@ -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(holder)), }); } } diff --git a/test/object/object.js b/test/object/object.js index 2660e4b6e..8741e27f1 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(), obj.readonlyAccessorWithUserDataT); } testDefineProperties('literal');