|
3 | 3 | #define RR_RETURN_VALUE_H |
4 | 4 |
|
5 | 5 | namespace rr { |
6 | | - typedef Wrapper<v8::ReturnValue<v8::Value>> ReturnValueWrapper; |
7 | 6 |
|
8 | | - class ReturnValue : public ReturnValueWrapper { |
| 7 | + class ReturnValue { |
9 | 8 | public: |
10 | | - ReturnValue(v8::ReturnValue<v8::Value> value) : ReturnValueWrapper(value) {} |
11 | | - ReturnValue(VALUE self) : ReturnValueWrapper(self) {} |
12 | | - |
13 | | - static VALUE Set(VALUE self, VALUE handle) { |
14 | | - ReturnValue ret(self); |
15 | | - Locker lock(ret->GetIsolate()); |
16 | | - v8::Local<v8::Value> value((Value(handle))); |
17 | | - ret->Set(value); |
18 | | - return Qnil; |
19 | | - } |
20 | 9 |
|
21 | | - static VALUE Set_bool(VALUE self, VALUE value) { |
22 | | - ReturnValue ret(self); |
23 | | - Locker lock(ret->GetIsolate()); |
24 | | - ret->Set((bool)Bool(value)); |
25 | | - return Qnil; |
26 | | - } |
| 10 | + template <class T> |
| 11 | + class Base : public Wrapper<v8::ReturnValue<T>> { |
| 12 | + public: |
27 | 13 |
|
28 | | - static VALUE Set_double(VALUE self, VALUE value) { |
29 | | - ReturnValue ret(self); |
30 | | - Locker lock(ret->GetIsolate()); |
31 | | - ret->Set(NUM2DBL(value)); |
32 | | - return Qnil; |
33 | | - } |
| 14 | + Base(v8::ReturnValue<T> value) : Wrapper<v8::ReturnValue<T>>(value) {} |
| 15 | + Base(VALUE self) : Wrapper<v8::ReturnValue<T>>(self) {} |
34 | 16 |
|
35 | | - static VALUE Set_int32_t(VALUE self, VALUE i) { |
36 | | - ReturnValue ret(self); |
37 | | - Locker lock(ret->GetIsolate()); |
38 | | - ret->Set(NUM2INT(i)); |
39 | | - return Qnil; |
40 | | - } |
| 17 | + static VALUE GetIsolate(VALUE self) { |
| 18 | + Base<T> ret(self); |
| 19 | + return Isolate(ret->GetIsolate()); |
| 20 | + } |
41 | 21 |
|
42 | | - static VALUE Set_uint32_t(VALUE self, VALUE i) { |
43 | | - ReturnValue ret(self); |
44 | | - Locker lock(ret->GetIsolate()); |
45 | | - ret->Set(NUM2UINT(i)); |
46 | | - return Qnil; |
47 | | - } |
| 22 | + }; |
48 | 23 |
|
49 | | - static VALUE SetNull(VALUE self) { |
50 | | - ReturnValue ret(self); |
51 | | - Locker lock(ret->GetIsolate()); |
52 | | - ret->SetNull(); |
53 | | - return Qnil; |
54 | | - } |
| 24 | + class Value : public Base<v8::Value> { |
| 25 | + public: |
55 | 26 |
|
56 | | - static VALUE SetUndefined(VALUE self) { |
57 | | - ReturnValue ret(self); |
58 | | - Locker lock(ret->GetIsolate()); |
59 | | - ret->SetUndefined(); |
60 | | - return Qnil; |
61 | | - } |
| 27 | + Value(v8::ReturnValue<v8::Value> value) : Base(value) {} |
| 28 | + Value(VALUE self) : Base(self) {} |
62 | 29 |
|
63 | | - static VALUE SetEmptyString(VALUE self) { |
64 | | - ReturnValue ret(self); |
65 | | - Locker lock(ret->GetIsolate()); |
66 | | - ret->SetEmptyString(); |
67 | | - return Qnil; |
68 | | - } |
| 30 | + static VALUE Set(VALUE self, VALUE handle) { |
| 31 | + Value ret(self); |
| 32 | + Locker lock(ret->GetIsolate()); |
| 33 | + v8::Local<v8::Value> value((rr::Value(handle))); |
| 34 | + ret->Set(value); |
| 35 | + return Qnil; |
| 36 | + } |
69 | 37 |
|
70 | | - static VALUE GetIsolate(VALUE self) { |
71 | | - ReturnValue ret(self); |
72 | | - return Isolate(ret->GetIsolate()); |
73 | | - } |
| 38 | + static VALUE Set_bool(VALUE self, VALUE value) { |
| 39 | + Value ret(self); |
| 40 | + Locker lock(ret->GetIsolate()); |
| 41 | + ret->Set((bool)Bool(value)); |
| 42 | + return Qnil; |
| 43 | + } |
| 44 | + |
| 45 | + static VALUE Set_double(VALUE self, VALUE value) { |
| 46 | + Value ret(self); |
| 47 | + Locker lock(ret->GetIsolate()); |
| 48 | + ret->Set(NUM2DBL(value)); |
| 49 | + return Qnil; |
| 50 | + } |
| 51 | + |
| 52 | + static VALUE Set_int32_t(VALUE self, VALUE i) { |
| 53 | + Value ret(self); |
| 54 | + Locker lock(ret->GetIsolate()); |
| 55 | + ret->Set(NUM2INT(i)); |
| 56 | + return Qnil; |
| 57 | + } |
| 58 | + |
| 59 | + static VALUE Set_uint32_t(VALUE self, VALUE i) { |
| 60 | + Value ret(self); |
| 61 | + Locker lock(ret->GetIsolate()); |
| 62 | + ret->Set(NUM2UINT(i)); |
| 63 | + return Qnil; |
| 64 | + } |
74 | 65 |
|
75 | | - static inline void Init() { |
| 66 | + static VALUE SetNull(VALUE self) { |
| 67 | + Value ret(self); |
| 68 | + Locker lock(ret->GetIsolate()); |
| 69 | + ret->SetNull(); |
| 70 | + return Qnil; |
| 71 | + } |
| 72 | + |
| 73 | + static VALUE SetUndefined(VALUE self) { |
| 74 | + Value ret(self); |
| 75 | + Locker lock(ret->GetIsolate()); |
| 76 | + ret->SetUndefined(); |
| 77 | + return Qnil; |
| 78 | + } |
| 79 | + |
| 80 | + static VALUE SetEmptyString(VALUE self) { |
| 81 | + Value ret(self); |
| 82 | + Locker lock(ret->GetIsolate()); |
| 83 | + ret->SetEmptyString(); |
| 84 | + return Qnil; |
| 85 | + } |
| 86 | + |
| 87 | + static inline void Init() { |
| 88 | + ClassBuilder("Value", ReturnValue::Class, ReturnValue::Class). |
| 89 | + defineMethod("Set", &Set). |
| 90 | + defineMethod("Set_bool", &Set_bool). |
| 91 | + defineMethod("Set_double", &Set_double). |
| 92 | + defineMethod("Set_int32_t", &Set_int32_t). |
| 93 | + defineMethod("Set_uint32_t", &Set_uint32_t). |
| 94 | + defineMethod("SetNull", &SetNull). |
| 95 | + defineMethod("SetUndefined", &SetUndefined). |
| 96 | + defineMethod("SetEmptyString", &SetEmptyString). |
| 97 | + defineMethod("GetIsolate", &GetIsolate). |
| 98 | + store(&Class); |
| 99 | + } |
| 100 | + |
| 101 | + }; |
| 102 | + |
| 103 | + class Void : public Base<void> { |
| 104 | + public: |
| 105 | + |
| 106 | + Void(v8::ReturnValue<void> value) : Base(value) {} |
| 107 | + Void(VALUE self) : Base(self) {} |
| 108 | + |
| 109 | + static inline void Init() { |
| 110 | + ClassBuilder("Void", ReturnValue::Class, ReturnValue::Class). |
| 111 | + defineMethod("GetIsolate", &GetIsolate). |
| 112 | + store(&Class); |
| 113 | + } |
| 114 | + |
| 115 | + }; |
| 116 | + |
| 117 | + static VALUE Class; |
| 118 | + |
| 119 | + static void Init() { |
76 | 120 | ClassBuilder("ReturnValue"). |
77 | | - defineMethod("Set", &Set). |
78 | | - defineMethod("Set_bool", &Set_bool). |
79 | | - defineMethod("Set_double", &Set_double). |
80 | | - defineMethod("Set_int32_t", &Set_int32_t). |
81 | | - defineMethod("Set_uint32_t", &Set_uint32_t). |
82 | | - defineMethod("SetNull", &SetNull). |
83 | | - defineMethod("SetUndefined", &SetUndefined). |
84 | | - defineMethod("SetEmptyString", &SetEmptyString). |
85 | | - defineMethod("GetIsolate", &GetIsolate). |
86 | 121 | store(&Class); |
| 122 | + |
| 123 | + Value::Init(); |
| 124 | + Void::Init(); |
87 | 125 | } |
| 126 | + |
88 | 127 | }; |
89 | 128 | } |
90 | 129 | #endif /* RR_RETURN_VALUE_H */ |
0 commit comments