Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit 45ddb40

Browse files
committed
Use template for PropertyCallbackInfo
1 parent de7b0f5 commit 45ddb40

5 files changed

Lines changed: 91 additions & 118 deletions

File tree

ext/v8/init.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ extern "C" {
2727
Symbol::Init();
2828
Function::Init();
2929
FunctionCallbackInfo::Init();
30-
PropertyCallbackInfo::Init();
30+
PropertyCallbackInfoValue::Init();
3131
PropertyCallbackInfoVoid::Init();
3232
ReturnValue::Init();
3333
Script::Init();

ext/v8/object.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ namespace rr {
9090
return Bool::Maybe(object->SetAccessor(
9191
context,
9292
Name(name),
93-
&PropertyCallbackInfo::invokeGetter,
94-
RTEST(setter) ? &PropertyCallbackInfoVoid::invokeSetter : 0,
95-
v8::MaybeLocal<v8::Value>(PropertyCallbackInfo::wrapData(isolate, getter, setter, data)),
93+
&PropertyCallbackInfoValue::invoke,
94+
RTEST(setter) ? &PropertyCallbackInfoVoid::invoke : 0,
95+
v8::MaybeLocal<v8::Value>(PropertyCallbackInfo<v8::Value>::wrapData(isolate, getter, setter, data)),
9696
Enum<v8::AccessControl>(settings, v8::DEFAULT),
9797
Enum<v8::PropertyAttribute>(attribute, v8::None)
9898
));

ext/v8/property-callback-void.h

Lines changed: 0 additions & 84 deletions
This file was deleted.

ext/v8/property-callback.h

Lines changed: 87 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44

55
namespace rr {
66

7-
typedef Wrapper<v8::PropertyCallbackInfo<v8::Value>> PropertyCallbackInfoWrapper;
8-
9-
class PropertyCallbackInfo : public PropertyCallbackInfoWrapper {
7+
template <class T>
8+
class PropertyCallbackInfo : public Wrapper<v8::PropertyCallbackInfo<T>> {
109
public:
1110

12-
inline PropertyCallbackInfo(v8::PropertyCallbackInfo<v8::Value> info) :
13-
PropertyCallbackInfoWrapper(info) {}
11+
inline PropertyCallbackInfo(v8::PropertyCallbackInfo<T> info) :
12+
Wrapper<v8::PropertyCallbackInfo<T>>(info) {}
1413

15-
inline PropertyCallbackInfo(VALUE self) : PropertyCallbackInfoWrapper(self) {}
14+
inline PropertyCallbackInfo(VALUE self) : Wrapper<v8::PropertyCallbackInfo<T>>(self) {}
1615

1716
/**
1817
* Package up the callback data for this object so that it can
@@ -38,6 +37,38 @@ namespace rr {
3837
return holder;
3938
}
4039

40+
static VALUE This(VALUE self) {
41+
PropertyCallbackInfo<T> info(self);
42+
Locker lock(info->GetIsolate());
43+
return Object(info->GetIsolate(), info->This());
44+
}
45+
46+
static VALUE Data(VALUE self) {
47+
PropertyCallbackInfo<T> info(self);
48+
Isolate isolate(info->GetIsolate());
49+
Locker lock(isolate);
50+
51+
v8::Local<v8::Object> holder = v8::Local<v8::Object>::Cast<v8::Value>(info->Data());
52+
v8::Local<v8::String> data_key = v8::String::NewFromUtf8(isolate, "rr::data");
53+
v8::Local<v8::Value> data(holder->GetHiddenValue(data_key));
54+
55+
return Value::handleToRubyObject(info->GetIsolate(), data);
56+
}
57+
58+
static VALUE GetIsolate(VALUE self) {
59+
PropertyCallbackInfo<T> info(self);
60+
return Isolate(info->GetIsolate());
61+
}
62+
};
63+
64+
class PropertyCallbackInfoValue : public PropertyCallbackInfo<v8::Value> {
65+
public:
66+
67+
inline PropertyCallbackInfoValue(v8::PropertyCallbackInfo<v8::Value> info) :
68+
PropertyCallbackInfo<v8::Value>(info) {}
69+
70+
inline PropertyCallbackInfoValue(VALUE self) : PropertyCallbackInfo<v8::Value>(self) {}
71+
4172
/**
4273
* Call the Ruby code associated with this callback.
4374
*
@@ -46,7 +77,7 @@ namespace rr {
4677
*
4778
* Note: This function implements the `v8::AccessorNameGetterCallback` API.
4879
*/
49-
static void invokeGetter(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& info) {
80+
static void invoke(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& info) {
5081
v8::Isolate* isolate = info.GetIsolate();
5182
v8::Local<v8::Object> holder = v8::Local<v8::Object>::Cast<v8::Value>(info.Data());
5283
v8::Local<v8::String> callback_key = v8::String::NewFromUtf8(isolate, "rr::getter");
@@ -64,44 +95,71 @@ namespace rr {
6495
rb_funcall(code, rb_intern("call"), 2, rb_property, (VALUE)PropertyCallbackInfo(info));
6596
}
6697

67-
static VALUE This(VALUE self) {
68-
PropertyCallbackInfo info(self);
98+
static VALUE GetReturnValue(VALUE self) {
99+
PropertyCallbackInfoValue info(self);
69100
Locker lock(info->GetIsolate());
70-
return Object(info->GetIsolate(), info->This());
101+
return ReturnValue(info->GetReturnValue());
71102
}
72103

73-
static VALUE Data(VALUE self) {
74-
PropertyCallbackInfo info(self);
75-
Isolate isolate(info->GetIsolate());
76-
Locker lock(isolate);
104+
static inline void Init() {
105+
ClassBuilder("PropertyCallbackInfoValue").
106+
defineMethod("This", &This).
107+
defineMethod("Data", &Data).
108+
defineMethod("GetIsolate", &GetIsolate).
109+
defineMethod("GetReturnValue", &GetReturnValue).
110+
store(&Class);
111+
}
112+
};
77113

78-
v8::Local<v8::Object> holder = v8::Local<v8::Object>::Cast<v8::Value>(info->Data());
79-
v8::Local<v8::String> data_key = v8::String::NewFromUtf8(isolate, "rr::data");
80-
v8::Local<v8::Value> data(holder->GetHiddenValue(data_key));
114+
class PropertyCallbackInfoVoid : public PropertyCallbackInfo<void> {
115+
public:
81116

82-
return Value::handleToRubyObject(info->GetIsolate(), data);
83-
}
117+
inline PropertyCallbackInfoVoid(v8::PropertyCallbackInfo<void> info) :
118+
PropertyCallbackInfo<void>(info) {}
84119

85-
static VALUE GetIsolate(VALUE self) {
86-
PropertyCallbackInfo info(self);
87-
return Isolate(info->GetIsolate());
88-
}
120+
inline PropertyCallbackInfoVoid(VALUE self) : PropertyCallbackInfo<void>(self) {}
89121

90-
static VALUE GetReturnValue(VALUE self) {
91-
PropertyCallbackInfo info(self);
92-
Locker lock(info->GetIsolate());
93-
return ReturnValue(info->GetReturnValue());
122+
/**
123+
* Call the Ruby code associated with this callback.
124+
*
125+
* Unpack the Ruby code, and the callback data from the C++
126+
* callback data, and then invoke that code.
127+
*
128+
* Note: This function implements the `v8::AccessorNameSetterCallback` API.
129+
*/
130+
static void invoke(v8::Local<v8::Name> property, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void>& info) {
131+
v8::Isolate* isolate = info.GetIsolate();
132+
133+
v8::Local<v8::Object> holder = v8::Local<v8::Object>::Cast<v8::Value>(info.Data());
134+
v8::Local<v8::String> callback_key = v8::String::NewFromUtf8(isolate, "rr::setter");
135+
136+
VALUE code(External::unwrap(holder->GetHiddenValue(callback_key)));
137+
138+
VALUE rb_property;
139+
if (property->IsSymbol()) {
140+
rb_property = Symbol(isolate, v8::Local<v8::Symbol>::Cast(property));
141+
} else {
142+
rb_property = String(isolate, property->ToString());
143+
}
144+
145+
Unlocker unlock(info.GetIsolate());
146+
rb_funcall(
147+
code, rb_intern("call"), 3,
148+
rb_property,
149+
(VALUE)Value::handleToRubyObject(isolate, value),
150+
(VALUE)PropertyCallbackInfoVoid(info)
151+
);
94152
}
95153

96154
static inline void Init() {
97-
ClassBuilder("PropertyCallbackInfo").
155+
ClassBuilder("PropertyCallbackInfoVoid").
98156
defineMethod("This", &This).
99157
defineMethod("Data", &Data).
100158
defineMethod("GetIsolate", &GetIsolate).
101-
defineMethod("GetReturnValue", &GetReturnValue).
102159
store(&Class);
103160
}
104161
};
162+
105163
}
106164

107165
#endif /* RR_PROPERTY_CALLBACK_H */

ext/v8/rr.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ inline VALUE not_implemented(const char* message) {
5353

5454
#include "object.h"
5555
#include "return-value.h"
56-
#include "property-callback-void.h"
5756
#include "property-callback.h"
5857
#include "array.h"
5958

0 commit comments

Comments
 (0)