From 2a08925896872c345e2948778eab01e9853c22cb Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sun, 10 Jun 2018 02:36:11 +0200 Subject: [PATCH] n-api: name CallbackBundle function fields Use field names rather than indices. Refs: https://github.com/nodejs/node/pull/21072 PR-URL: https://github.com/nodejs/node/pull/21240 Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Minwoo Jung Reviewed-By: Michael Dawson --- src/node_api.cc | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/src/node_api.cc b/src/node_api.cc index 149cb8731d44f7..b1b498958b85df 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -476,15 +476,6 @@ class TryCatch : public v8::TryCatch { //=== Function napi_callback wrapper ================================= -// TODO(somebody): these constants can be removed with relevant changes -// in CallbackWrapperBase<> and CallbackBundle. -// Leave them for now just to keep the change set and cognitive load minimal. -static const int kFunctionIndex = 0; // Used in CallbackBundle::cb[] -static const int kGetterIndex = 0; // Used in CallbackBundle::cb[] -static const int kSetterIndex = 1; // Used in CallbackBundle::cb[] -static const int kCallbackCount = 2; // Used in CallbackBundle::cb[] - // Max is "getter + setter" case - // Use this data structure to associate callback data with each N-API function // exposed to JavaScript. The structure is stored in a v8::External which gets // passed into our callback wrapper. This reduces the performance impact of @@ -501,7 +492,8 @@ struct CallbackBundle { napi_env env; // Necessary to invoke C++ NAPI callback void* cb_data; // The user provided callback data - napi_callback cb[kCallbackCount]; // Max capacity is 2 (getter + setter) + napi_callback function_or_getter; + napi_callback setter; node::Persistent handle; // Die with this JavaScript object private: @@ -539,7 +531,7 @@ class CallbackWrapper { void* _data; }; -template +template class CallbackWrapperBase : public CallbackWrapper { public: CallbackWrapperBase(const Info& cbinfo, const size_t args_length) @@ -561,7 +553,7 @@ class CallbackWrapperBase : public CallbackWrapper { // All other pointers we need are stored in `_bundle` napi_env env = _bundle->env; - napi_callback cb = _bundle->cb[kInternalFieldIndex]; + napi_callback cb = _bundle->*FunctionField; napi_value result; NAPI_CALL_INTO_MODULE_THROW(env, result = cb(env, cbinfo_wrapper)); @@ -577,7 +569,7 @@ class CallbackWrapperBase : public CallbackWrapper { class FunctionCallbackWrapper : public CallbackWrapperBase, - kFunctionIndex> { + &CallbackBundle::function_or_getter> { public: static void Invoke(const v8::FunctionCallbackInfo& info) { FunctionCallbackWrapper cbwrapper(info); @@ -623,7 +615,7 @@ class FunctionCallbackWrapper class GetterCallbackWrapper : public CallbackWrapperBase, - kGetterIndex> { + &CallbackBundle::function_or_getter> { public: static void Invoke(v8::Local property, const v8::PropertyCallbackInfo& info) { @@ -654,7 +646,8 @@ class GetterCallbackWrapper }; class SetterCallbackWrapper - : public CallbackWrapperBase, kSetterIndex> { + : public CallbackWrapperBase, + &CallbackBundle::setter> { public: static void Invoke(v8::Local property, v8::Local value, @@ -698,7 +691,7 @@ v8::Local CreateFunctionCallbackData(napi_env env, napi_callback cb, void* data) { CallbackBundle* bundle = new CallbackBundle(); - bundle->cb[kFunctionIndex] = cb; + bundle->function_or_getter = cb; bundle->cb_data = data; bundle->env = env; v8::Local cbdata = v8::External::New(env->isolate, bundle); @@ -716,8 +709,8 @@ v8::Local CreateAccessorCallbackData(napi_env env, napi_callback setter, void* data) { CallbackBundle* bundle = new CallbackBundle(); - bundle->cb[kGetterIndex] = getter; - bundle->cb[kSetterIndex] = setter; + bundle->function_or_getter = getter; + bundle->setter = setter; bundle->cb_data = data; bundle->env = env; v8::Local cbdata = v8::External::New(env->isolate, bundle);