Skip to content

Commit b0ab0f3

Browse files
committed
Fix interceptor returning
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.
1 parent 212017b commit b0ab0f3

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

nan_callbacks.h

+13
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ template<typename T> class FunctionCallbackInfo;
1313
template<typename T> class PropertyCallbackInfo;
1414
template<typename T> class Global;
1515

16+
#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 12 || \
17+
(V8_MAJOR_VERSION == 12 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION > 4))
18+
namespace Intercepted {
19+
constexpr v8::Intercepted No() { return v8::Intercepted::kNo; }
20+
constexpr v8::Intercepted Yes() { return v8::Intercepted::kYes; }
21+
};
22+
#else
23+
namespace Intercepted {
24+
inline void No() {}
25+
inline void Yes() {}
26+
};
27+
#endif
28+
1629
typedef void(*FunctionCallback)(const FunctionCallbackInfo<v8::Value>&);
1730
typedef void(*GetterCallback)
1831
(v8::Local<v8::String>, const PropertyCallbackInfo<v8::Value>&);

test/cpp/indexedinterceptors.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ NAN_INDEX_GETTER(IndexedInterceptor::PropertyGetter) {
8080
} else {
8181
info.GetReturnValue().Set(Nan::New("bar").ToLocalChecked());
8282
}
83+
return Intercepted::Yes();
8384
}
8485

8586
NAN_INDEX_SETTER(IndexedInterceptor::PropertySetter) {
@@ -94,28 +95,34 @@ NAN_INDEX_SETTER(IndexedInterceptor::PropertySetter) {
9495
} else {
9596
info.GetReturnValue().Set(info.This());
9697
}
98+
return Intercepted::Yes();
9799
}
98100

99101
NAN_INDEX_ENUMERATOR(IndexedInterceptor::PropertyEnumerator) {
100102
v8::Local<v8::Array> arr = Nan::New<v8::Array>();
101103
Set(arr, 0, Nan::New(42));
102104
info.GetReturnValue().Set(arr);
105+
return Intercepted::Yes();
103106
}
104107

105108
NAN_INDEX_DELETER(IndexedInterceptor::PropertyDeleter) {
106109
IndexedInterceptor* interceptor =
107110
ObjectWrap::Unwrap<IndexedInterceptor>(info.Holder());
108111
std::strncpy(interceptor->buf, "goober", sizeof (interceptor->buf));
109112
info.GetReturnValue().Set(True());
113+
return Intercepted::Yes();
110114
}
111115

112116
NAN_INDEX_QUERY(IndexedInterceptor::PropertyQuery) {
113117
if (index == 1) {
114118
info.GetReturnValue().Set(Nan::New<v8::Integer>(v8::DontEnum));
119+
return Intercepted::Yes();
115120
}
116121
if (index == 42) {
117122
info.GetReturnValue().Set(Nan::New(0));
123+
return Intercepted::Yes();
118124
}
125+
return Intercepted::No();
119126
}
120127

121128
NODE_MODULE(indexedinterceptors, IndexedInterceptor::Init)

test/cpp/namedinterceptors.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ NAN_PROPERTY_GETTER(NamedInterceptor::PropertyGetter) {
8080
} else {
8181
info.GetReturnValue().Set(Nan::New("bar").ToLocalChecked());
8282
}
83+
return Intercepted::Yes();
8384
}
8485

8586
NAN_PROPERTY_SETTER(NamedInterceptor::PropertySetter) {
@@ -94,6 +95,7 @@ NAN_PROPERTY_SETTER(NamedInterceptor::PropertySetter) {
9495
} else {
9596
info.GetReturnValue().Set(info.This());
9697
}
98+
return Intercepted::Yes();
9799
}
98100

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

112115
NAN_PROPERTY_QUERY(NamedInterceptor::PropertyQuery) {
113116
Nan::Utf8String s(property);
114117
if (!std::strcmp(*s, "thing")) {
115-
return info.GetReturnValue().Set(Nan::New<v8::Integer>(v8::DontEnum));
118+
info.GetReturnValue().Set(Nan::New<v8::Integer>(v8::DontEnum));
119+
return Intercepted::Yes();
116120
}
117121
if (!std::strcmp(*s, "value")) {
118-
return info.GetReturnValue().Set(Nan::New(0));
122+
info.GetReturnValue().Set(Nan::New(0));
123+
return Intercepted::Yes();
119124
}
125+
return Intercepted::No();
120126
}
121127

122128
NODE_MODULE(namedinterceptors, NamedInterceptor::Init)

0 commit comments

Comments
 (0)