Skip to content

Commit

Permalink
Enable correct interceptor return values
Browse files Browse the repository at this point in the history
This requires minor changes in user code due to the need to call
and return from Intercepted::No or Intercepted::Yes. While an
operator conversion on classes would have been nicer,
implicit conversion to void is not possible.
  • Loading branch information
kkoopa committed Oct 19, 2024
1 parent 4a8af32 commit 7bb2b59
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
13 changes: 13 additions & 0 deletions nan_callbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ template<typename T> class FunctionCallbackInfo;
template<typename T> class PropertyCallbackInfo;
template<typename T> class Global;

#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 12 || \
(V8_MAJOR_VERSION == 12 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION > 4))
namespace Intercepted {
constexpr v8::Intercepted No() { return v8::Intercepted::kNo; }
constexpr v8::Intercepted Yes() { return v8::Intercepted::kYes; }
};
#else
namespace Intercepted {
inline void No() {}
inline void Yes() {}
};
#endif

typedef void(*FunctionCallback)(const FunctionCallbackInfo<v8::Value>&);
typedef void(*GetterCallback)
(v8::Local<v8::String>, const PropertyCallbackInfo<v8::Value>&);
Expand Down
7 changes: 7 additions & 0 deletions test/cpp/indexedinterceptors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ NAN_INDEX_GETTER(IndexedInterceptor::PropertyGetter) {
} else {
info.GetReturnValue().Set(Nan::New("bar").ToLocalChecked());
}
return Intercepted::Yes();
}

NAN_INDEX_SETTER(IndexedInterceptor::PropertySetter) {
Expand All @@ -94,28 +95,34 @@ NAN_INDEX_SETTER(IndexedInterceptor::PropertySetter) {
} else {
info.GetReturnValue().Set(info.This());
}
return Intercepted::Yes();
}

NAN_INDEX_ENUMERATOR(IndexedInterceptor::PropertyEnumerator) {
v8::Local<v8::Array> arr = Nan::New<v8::Array>();
Set(arr, 0, Nan::New(42));
info.GetReturnValue().Set(arr);
return Intercepted::Yes();
}

NAN_INDEX_DELETER(IndexedInterceptor::PropertyDeleter) {
IndexedInterceptor* interceptor =
ObjectWrap::Unwrap<IndexedInterceptor>(info.Holder());
std::strncpy(interceptor->buf, "goober", sizeof (interceptor->buf));
info.GetReturnValue().Set(True());
return Intercepted::Yes();
}

NAN_INDEX_QUERY(IndexedInterceptor::PropertyQuery) {
if (index == 1) {
info.GetReturnValue().Set(Nan::New<v8::Integer>(v8::DontEnum));
return Intercepted::Yes();
}
if (index == 42) {
info.GetReturnValue().Set(Nan::New(0));
return Intercepted::Yes();
}
return Intercepted::No();
}

NODE_MODULE(indexedinterceptors, IndexedInterceptor::Init)
10 changes: 8 additions & 2 deletions test/cpp/namedinterceptors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ NAN_PROPERTY_GETTER(NamedInterceptor::PropertyGetter) {
} else {
info.GetReturnValue().Set(Nan::New("bar").ToLocalChecked());
}
return Intercepted::Yes();
}

NAN_PROPERTY_SETTER(NamedInterceptor::PropertySetter) {
Expand All @@ -94,6 +95,7 @@ NAN_PROPERTY_SETTER(NamedInterceptor::PropertySetter) {
} else {
info.GetReturnValue().Set(info.This());
}
return Intercepted::Yes();
}

NAN_PROPERTY_ENUMERATOR(NamedInterceptor::PropertyEnumerator) {
Expand All @@ -107,16 +109,20 @@ NAN_PROPERTY_DELETER(NamedInterceptor::PropertyDeleter) {
ObjectWrap::Unwrap<NamedInterceptor>(info.Holder());
std::strncpy(interceptor->buf, "goober", sizeof (interceptor->buf));
info.GetReturnValue().Set(True());
return Intercepted::Yes();
}

NAN_PROPERTY_QUERY(NamedInterceptor::PropertyQuery) {
Nan::Utf8String s(property);
if (!std::strcmp(*s, "thing")) {
return info.GetReturnValue().Set(Nan::New<v8::Integer>(v8::DontEnum));
info.GetReturnValue().Set(Nan::New<v8::Integer>(v8::DontEnum));
return Intercepted::Yes();
}
if (!std::strcmp(*s, "value")) {
return info.GetReturnValue().Set(Nan::New(0));
info.GetReturnValue().Set(Nan::New(0));
return Intercepted::Yes();
}
return Intercepted::No();
}

NODE_MODULE(namedinterceptors, NamedInterceptor::Init)

0 comments on commit 7bb2b59

Please sign in to comment.