Skip to content

Commit

Permalink
n-api: name CallbackBundle function fields
Browse files Browse the repository at this point in the history
Use field names rather than indices.

Refs: #21072

PR-URL: #21240
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Minwoo Jung <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
  • Loading branch information
addaleax committed Jun 14, 2018
1 parent 52bb84b commit 2a08925
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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));
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 2a08925

Please sign in to comment.