diff options
author | Anna Henningsen <anna@addaleax.net> | 2018-06-10 02:36:11 +0200 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2018-06-14 13:13:20 +0200 |
commit | 2a08925896872c345e2948778eab01e9853c22cb (patch) | |
tree | c00e11667b603fb7b701d60104600e04d170ffb4 /src | |
parent | 52bb84b47d261319d98dc5acc5cab0d088ae09cc (diff) | |
download | android-node-v8-2a08925896872c345e2948778eab01e9853c22cb.tar.gz android-node-v8-2a08925896872c345e2948778eab01e9853c22cb.tar.bz2 android-node-v8-2a08925896872c345e2948778eab01e9853c22cb.zip |
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 <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/node_api.cc | 29 |
1 files changed, 11 insertions, 18 deletions
diff --git a/src/node_api.cc b/src/node_api.cc index 149cb8731d..b1b498958b 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<v8::Value> handle; // Die with this JavaScript object private: @@ -539,7 +531,7 @@ class CallbackWrapper { void* _data; }; -template <typename Info, int kInternalFieldIndex> +template <typename Info, napi_callback CallbackBundle::*FunctionField> 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<v8::FunctionCallbackInfo<v8::Value>, - kFunctionIndex> { + &CallbackBundle::function_or_getter> { public: static void Invoke(const v8::FunctionCallbackInfo<v8::Value>& info) { FunctionCallbackWrapper cbwrapper(info); @@ -623,7 +615,7 @@ class FunctionCallbackWrapper class GetterCallbackWrapper : public CallbackWrapperBase<v8::PropertyCallbackInfo<v8::Value>, - kGetterIndex> { + &CallbackBundle::function_or_getter> { public: static void Invoke(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& info) { @@ -654,7 +646,8 @@ class GetterCallbackWrapper }; class SetterCallbackWrapper - : public CallbackWrapperBase<v8::PropertyCallbackInfo<void>, kSetterIndex> { + : public CallbackWrapperBase<v8::PropertyCallbackInfo<void>, + &CallbackBundle::setter> { public: static void Invoke(v8::Local<v8::Name> property, v8::Local<v8::Value> value, @@ -698,7 +691,7 @@ v8::Local<v8::Value> 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<v8::Value> cbdata = v8::External::New(env->isolate, bundle); @@ -716,8 +709,8 @@ v8::Local<v8::Value> 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<v8::Value> cbdata = v8::External::New(env->isolate, bundle); |