1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | #ifndef NAN_MAYBE_PRE_43_INL_H_
|
10 | #define NAN_MAYBE_PRE_43_INL_H_
|
11 |
|
12 | template<typename T>
|
13 | class MaybeLocal {
|
14 | public:
|
15 | inline MaybeLocal() : val_(v8::Local<T>()) {}
|
16 |
|
17 | template<typename S>
|
18 | # if NODE_MODULE_VERSION >= NODE_0_12_MODULE_VERSION
|
19 | inline
|
20 | MaybeLocal(v8::Local<S> that) : val_(that) {}
|
21 | # else
|
22 | inline
|
23 | MaybeLocal(v8::Local<S> that) :
|
24 | val_(*reinterpret_cast<v8::Local<T>*>(&that)) {}
|
25 | # endif
|
26 |
|
27 | inline bool IsEmpty() const { return val_.IsEmpty(); }
|
28 |
|
29 | template<typename S>
|
30 | inline bool ToLocal(v8::Local<S> *out) const {
|
31 | *out = val_;
|
32 | return !IsEmpty();
|
33 | }
|
34 |
|
35 | inline v8::Local<T> ToLocalChecked() const {
|
36 | #if defined(V8_ENABLE_CHECKS)
|
37 | assert(!IsEmpty() && "ToLocalChecked is Empty");
|
38 | #endif
|
39 | return val_;
|
40 | }
|
41 |
|
42 | template<typename S>
|
43 | inline v8::Local<S> FromMaybe(v8::Local<S> default_value) const {
|
44 | return IsEmpty() ? default_value : val_;
|
45 | }
|
46 |
|
47 | private:
|
48 | v8::Local<T> val_;
|
49 | };
|
50 |
|
51 | template<typename T>
|
52 | class Maybe {
|
53 | public:
|
54 | inline bool IsNothing() const { return !has_value_; }
|
55 | inline bool IsJust() const { return has_value_; }
|
56 |
|
57 | inline T FromJust() const {
|
58 | #if defined(V8_ENABLE_CHECKS)
|
59 | assert(IsJust() && "FromJust is Nothing");
|
60 | #endif
|
61 | return value_;
|
62 | }
|
63 |
|
64 | inline T FromMaybe(const T& default_value) const {
|
65 | return has_value_ ? value_ : default_value;
|
66 | }
|
67 |
|
68 | inline bool operator==(const Maybe &other) const {
|
69 | return (IsJust() == other.IsJust()) &&
|
70 | (!IsJust() || FromJust() == other.FromJust());
|
71 | }
|
72 |
|
73 | inline bool operator!=(const Maybe &other) const {
|
74 | return !operator==(other);
|
75 | }
|
76 |
|
77 | private:
|
78 | Maybe() : has_value_(false) {}
|
79 | explicit Maybe(const T& t) : has_value_(true), value_(t) {}
|
80 | bool has_value_;
|
81 | T value_;
|
82 |
|
83 | template<typename U>
|
84 | friend Maybe<U> Nothing();
|
85 | template<typename U>
|
86 | friend Maybe<U> Just(const U& u);
|
87 | };
|
88 |
|
89 | template<typename T>
|
90 | inline Maybe<T> Nothing() {
|
91 | return Maybe<T>();
|
92 | }
|
93 |
|
94 | template<typename T>
|
95 | inline Maybe<T> Just(const T& t) {
|
96 | return Maybe<T>(t);
|
97 | }
|
98 |
|
99 | inline
|
100 | MaybeLocal<v8::String> ToDetailString(v8::Handle<v8::Value> val) {
|
101 | return MaybeLocal<v8::String>(val->ToDetailString());
|
102 | }
|
103 |
|
104 | inline
|
105 | MaybeLocal<v8::Uint32> ToArrayIndex(v8::Handle<v8::Value> val) {
|
106 | return MaybeLocal<v8::Uint32>(val->ToArrayIndex());
|
107 | }
|
108 |
|
109 | inline
|
110 | Maybe<bool> Equals(v8::Handle<v8::Value> a, v8::Handle<v8::Value>(b)) {
|
111 | return Just<bool>(a->Equals(b));
|
112 | }
|
113 |
|
114 | inline
|
115 | MaybeLocal<v8::Object> NewInstance(v8::Handle<v8::Function> h) {
|
116 | return MaybeLocal<v8::Object>(h->NewInstance());
|
117 | }
|
118 |
|
119 | inline
|
120 | MaybeLocal<v8::Object> NewInstance(
|
121 | v8::Local<v8::Function> h
|
122 | , int argc
|
123 | , v8::Local<v8::Value> argv[]) {
|
124 | return MaybeLocal<v8::Object>(h->NewInstance(argc, argv));
|
125 | }
|
126 |
|
127 | inline
|
128 | MaybeLocal<v8::Object> NewInstance(v8::Handle<v8::ObjectTemplate> h) {
|
129 | return MaybeLocal<v8::Object>(h->NewInstance());
|
130 | }
|
131 |
|
132 | inline
|
133 | MaybeLocal<v8::Function> GetFunction(v8::Handle<v8::FunctionTemplate> t) {
|
134 | return MaybeLocal<v8::Function>(t->GetFunction());
|
135 | }
|
136 |
|
137 | inline Maybe<bool> Set(
|
138 | v8::Handle<v8::Object> obj
|
139 | , v8::Handle<v8::Value> key
|
140 | , v8::Handle<v8::Value> value) {
|
141 | return Just<bool>(obj->Set(key, value));
|
142 | }
|
143 |
|
144 | inline Maybe<bool> Set(
|
145 | v8::Handle<v8::Object> obj
|
146 | , uint32_t index
|
147 | , v8::Handle<v8::Value> value) {
|
148 | return Just<bool>(obj->Set(index, value));
|
149 | }
|
150 |
|
151 | inline Maybe<bool> ForceSet(
|
152 | v8::Handle<v8::Object> obj
|
153 | , v8::Handle<v8::Value> key
|
154 | , v8::Handle<v8::Value> value
|
155 | , v8::PropertyAttribute attribs = v8::None) {
|
156 | return Just<bool>(obj->ForceSet(key, value, attribs));
|
157 | }
|
158 |
|
159 | inline MaybeLocal<v8::Value> Get(
|
160 | v8::Handle<v8::Object> obj
|
161 | , v8::Handle<v8::Value> key) {
|
162 | return MaybeLocal<v8::Value>(obj->Get(key));
|
163 | }
|
164 |
|
165 | inline MaybeLocal<v8::Value> Get(
|
166 | v8::Handle<v8::Object> obj
|
167 | , uint32_t index) {
|
168 | return MaybeLocal<v8::Value>(obj->Get(index));
|
169 | }
|
170 |
|
171 | inline Maybe<v8::PropertyAttribute> GetPropertyAttributes(
|
172 | v8::Handle<v8::Object> obj
|
173 | , v8::Handle<v8::Value> key) {
|
174 | return Just<v8::PropertyAttribute>(obj->GetPropertyAttributes(key));
|
175 | }
|
176 |
|
177 | inline Maybe<bool> Has(
|
178 | v8::Handle<v8::Object> obj
|
179 | , v8::Handle<v8::String> key) {
|
180 | return Just<bool>(obj->Has(key));
|
181 | }
|
182 |
|
183 | inline Maybe<bool> Has(
|
184 | v8::Handle<v8::Object> obj
|
185 | , uint32_t index) {
|
186 | return Just<bool>(obj->Has(index));
|
187 | }
|
188 |
|
189 | inline Maybe<bool> Delete(
|
190 | v8::Handle<v8::Object> obj
|
191 | , v8::Handle<v8::String> key) {
|
192 | return Just<bool>(obj->Delete(key));
|
193 | }
|
194 |
|
195 | inline Maybe<bool> Delete(
|
196 | v8::Handle<v8::Object> obj
|
197 | , uint32_t index) {
|
198 | return Just<bool>(obj->Delete(index));
|
199 | }
|
200 |
|
201 | inline
|
202 | MaybeLocal<v8::Array> GetPropertyNames(v8::Handle<v8::Object> obj) {
|
203 | return MaybeLocal<v8::Array>(obj->GetPropertyNames());
|
204 | }
|
205 |
|
206 | inline
|
207 | MaybeLocal<v8::Array> GetOwnPropertyNames(v8::Handle<v8::Object> obj) {
|
208 | return MaybeLocal<v8::Array>(obj->GetOwnPropertyNames());
|
209 | }
|
210 |
|
211 | inline Maybe<bool> SetPrototype(
|
212 | v8::Handle<v8::Object> obj
|
213 | , v8::Handle<v8::Value> prototype) {
|
214 | return Just<bool>(obj->SetPrototype(prototype));
|
215 | }
|
216 |
|
217 | inline MaybeLocal<v8::String> ObjectProtoToString(
|
218 | v8::Handle<v8::Object> obj) {
|
219 | return MaybeLocal<v8::String>(obj->ObjectProtoToString());
|
220 | }
|
221 |
|
222 | inline Maybe<bool> HasOwnProperty(
|
223 | v8::Handle<v8::Object> obj
|
224 | , v8::Handle<v8::String> key) {
|
225 | return Just<bool>(obj->HasOwnProperty(key));
|
226 | }
|
227 |
|
228 | inline Maybe<bool> HasRealNamedProperty(
|
229 | v8::Handle<v8::Object> obj
|
230 | , v8::Handle<v8::String> key) {
|
231 | return Just<bool>(obj->HasRealNamedProperty(key));
|
232 | }
|
233 |
|
234 | inline Maybe<bool> HasRealIndexedProperty(
|
235 | v8::Handle<v8::Object> obj
|
236 | , uint32_t index) {
|
237 | return Just<bool>(obj->HasRealIndexedProperty(index));
|
238 | }
|
239 |
|
240 | inline Maybe<bool> HasRealNamedCallbackProperty(
|
241 | v8::Handle<v8::Object> obj
|
242 | , v8::Handle<v8::String> key) {
|
243 | return Just<bool>(obj->HasRealNamedCallbackProperty(key));
|
244 | }
|
245 |
|
246 | inline MaybeLocal<v8::Value> GetRealNamedPropertyInPrototypeChain(
|
247 | v8::Handle<v8::Object> obj
|
248 | , v8::Handle<v8::String> key) {
|
249 | return MaybeLocal<v8::Value>(
|
250 | obj->GetRealNamedPropertyInPrototypeChain(key));
|
251 | }
|
252 |
|
253 | inline MaybeLocal<v8::Value> GetRealNamedProperty(
|
254 | v8::Handle<v8::Object> obj
|
255 | , v8::Handle<v8::String> key) {
|
256 | return MaybeLocal<v8::Value>(obj->GetRealNamedProperty(key));
|
257 | }
|
258 |
|
259 | inline MaybeLocal<v8::Value> CallAsFunction(
|
260 | v8::Handle<v8::Object> obj
|
261 | , v8::Handle<v8::Object> recv
|
262 | , int argc
|
263 | , v8::Handle<v8::Value> argv[]) {
|
264 | return MaybeLocal<v8::Value>(obj->CallAsFunction(recv, argc, argv));
|
265 | }
|
266 |
|
267 | inline MaybeLocal<v8::Value> CallAsConstructor(
|
268 | v8::Handle<v8::Object> obj
|
269 | , int argc
|
270 | , v8::Local<v8::Value> argv[]) {
|
271 | return MaybeLocal<v8::Value>(obj->CallAsConstructor(argc, argv));
|
272 | }
|
273 |
|
274 | inline
|
275 | MaybeLocal<v8::String> GetSourceLine(v8::Handle<v8::Message> msg) {
|
276 | return MaybeLocal<v8::String>(msg->GetSourceLine());
|
277 | }
|
278 |
|
279 | inline Maybe<int> GetLineNumber(v8::Handle<v8::Message> msg) {
|
280 | return Just<int>(msg->GetLineNumber());
|
281 | }
|
282 |
|
283 | inline Maybe<int> GetStartColumn(v8::Handle<v8::Message> msg) {
|
284 | return Just<int>(msg->GetStartColumn());
|
285 | }
|
286 |
|
287 | inline Maybe<int> GetEndColumn(v8::Handle<v8::Message> msg) {
|
288 | return Just<int>(msg->GetEndColumn());
|
289 | }
|
290 |
|
291 | inline MaybeLocal<v8::Object> CloneElementAt(
|
292 | v8::Handle<v8::Array> array
|
293 | , uint32_t index) {
|
294 | return MaybeLocal<v8::Object>(array->CloneElementAt(index));
|
295 | }
|
296 |
|
297 | inline MaybeLocal<v8::Value> Call(
|
298 | v8::Local<v8::Function> fun
|
299 | , v8::Local<v8::Object> recv
|
300 | , int argc
|
301 | , v8::Local<v8::Value> argv[]) {
|
302 | return MaybeLocal<v8::Value>(fun->Call(recv, argc, argv));
|
303 | }
|
304 |
|
305 | #endif
|