Skip to content

Commit 169bff3

Browse files
addaleaxtargos
authored andcommitted
n-api: name CallbackBundle function fields
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]>
1 parent c1d53f8 commit 169bff3

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

src/node_api.cc

+11-18
Original file line numberDiff line numberDiff line change
@@ -476,15 +476,6 @@ class TryCatch : public v8::TryCatch {
476476

477477
//=== Function napi_callback wrapper =================================
478478

479-
// TODO(somebody): these constants can be removed with relevant changes
480-
// in CallbackWrapperBase<> and CallbackBundle.
481-
// Leave them for now just to keep the change set and cognitive load minimal.
482-
static const int kFunctionIndex = 0; // Used in CallbackBundle::cb[]
483-
static const int kGetterIndex = 0; // Used in CallbackBundle::cb[]
484-
static const int kSetterIndex = 1; // Used in CallbackBundle::cb[]
485-
static const int kCallbackCount = 2; // Used in CallbackBundle::cb[]
486-
// Max is "getter + setter" case
487-
488479
// Use this data structure to associate callback data with each N-API function
489480
// exposed to JavaScript. The structure is stored in a v8::External which gets
490481
// passed into our callback wrapper. This reduces the performance impact of
@@ -501,7 +492,8 @@ struct CallbackBundle {
501492

502493
napi_env env; // Necessary to invoke C++ NAPI callback
503494
void* cb_data; // The user provided callback data
504-
napi_callback cb[kCallbackCount]; // Max capacity is 2 (getter + setter)
495+
napi_callback function_or_getter;
496+
napi_callback setter;
505497
node::Persistent<v8::Value> handle; // Die with this JavaScript object
506498

507499
private:
@@ -539,7 +531,7 @@ class CallbackWrapper {
539531
void* _data;
540532
};
541533

542-
template <typename Info, int kInternalFieldIndex>
534+
template <typename Info, napi_callback CallbackBundle::*FunctionField>
543535
class CallbackWrapperBase : public CallbackWrapper {
544536
public:
545537
CallbackWrapperBase(const Info& cbinfo, const size_t args_length)
@@ -561,7 +553,7 @@ class CallbackWrapperBase : public CallbackWrapper {
561553

562554
// All other pointers we need are stored in `_bundle`
563555
napi_env env = _bundle->env;
564-
napi_callback cb = _bundle->cb[kInternalFieldIndex];
556+
napi_callback cb = _bundle->*FunctionField;
565557

566558
napi_value result;
567559
NAPI_CALL_INTO_MODULE_THROW(env, result = cb(env, cbinfo_wrapper));
@@ -577,7 +569,7 @@ class CallbackWrapperBase : public CallbackWrapper {
577569

578570
class FunctionCallbackWrapper
579571
: public CallbackWrapperBase<v8::FunctionCallbackInfo<v8::Value>,
580-
kFunctionIndex> {
572+
&CallbackBundle::function_or_getter> {
581573
public:
582574
static void Invoke(const v8::FunctionCallbackInfo<v8::Value>& info) {
583575
FunctionCallbackWrapper cbwrapper(info);
@@ -623,7 +615,7 @@ class FunctionCallbackWrapper
623615

624616
class GetterCallbackWrapper
625617
: public CallbackWrapperBase<v8::PropertyCallbackInfo<v8::Value>,
626-
kGetterIndex> {
618+
&CallbackBundle::function_or_getter> {
627619
public:
628620
static void Invoke(v8::Local<v8::Name> property,
629621
const v8::PropertyCallbackInfo<v8::Value>& info) {
@@ -654,7 +646,8 @@ class GetterCallbackWrapper
654646
};
655647

656648
class SetterCallbackWrapper
657-
: public CallbackWrapperBase<v8::PropertyCallbackInfo<void>, kSetterIndex> {
649+
: public CallbackWrapperBase<v8::PropertyCallbackInfo<void>,
650+
&CallbackBundle::setter> {
658651
public:
659652
static void Invoke(v8::Local<v8::Name> property,
660653
v8::Local<v8::Value> value,
@@ -698,7 +691,7 @@ v8::Local<v8::Value> CreateFunctionCallbackData(napi_env env,
698691
napi_callback cb,
699692
void* data) {
700693
CallbackBundle* bundle = new CallbackBundle();
701-
bundle->cb[kFunctionIndex] = cb;
694+
bundle->function_or_getter = cb;
702695
bundle->cb_data = data;
703696
bundle->env = env;
704697
v8::Local<v8::Value> cbdata = v8::External::New(env->isolate, bundle);
@@ -716,8 +709,8 @@ v8::Local<v8::Value> CreateAccessorCallbackData(napi_env env,
716709
napi_callback setter,
717710
void* data) {
718711
CallbackBundle* bundle = new CallbackBundle();
719-
bundle->cb[kGetterIndex] = getter;
720-
bundle->cb[kSetterIndex] = setter;
712+
bundle->function_or_getter = getter;
713+
bundle->setter = setter;
721714
bundle->cb_data = data;
722715
bundle->env = env;
723716
v8::Local<v8::Value> cbdata = v8::External::New(env->isolate, bundle);

0 commit comments

Comments
 (0)