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 : v8::Local<S>(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 | #include "nan_define_own_property_helper.h"
|
152 |
|
153 | inline Maybe<bool> DefineOwnProperty(
|
154 | v8::Handle<v8::Object> obj
|
155 | , v8::Handle<v8::String> key
|
156 | , v8::Handle<v8::Value> value
|
157 | , v8::PropertyAttribute attribs = v8::None) {
|
158 | v8::PropertyAttribute current = obj->GetPropertyAttributes(key);
|
159 | return imp::DefineOwnPropertyHelper(current, obj, key, value, attribs);
|
160 | }
|
161 |
|
162 | NAN_DEPRECATED inline Maybe<bool> ForceSet(
|
163 | v8::Handle<v8::Object> obj
|
164 | , v8::Handle<v8::Value> key
|
165 | , v8::Handle<v8::Value> value
|
166 | , v8::PropertyAttribute attribs = v8::None) {
|
167 | return Just<bool>(obj->ForceSet(key, value, attribs));
|
168 | }
|
169 |
|
170 | inline MaybeLocal<v8::Value> Get(
|
171 | v8::Handle<v8::Object> obj
|
172 | , v8::Handle<v8::Value> key) {
|
173 | return MaybeLocal<v8::Value>(obj->Get(key));
|
174 | }
|
175 |
|
176 | inline MaybeLocal<v8::Value> Get(
|
177 | v8::Handle<v8::Object> obj
|
178 | , uint32_t index) {
|
179 | return MaybeLocal<v8::Value>(obj->Get(index));
|
180 | }
|
181 |
|
182 | inline Maybe<v8::PropertyAttribute> GetPropertyAttributes(
|
183 | v8::Handle<v8::Object> obj
|
184 | , v8::Handle<v8::Value> key) {
|
185 | return Just<v8::PropertyAttribute>(obj->GetPropertyAttributes(key));
|
186 | }
|
187 |
|
188 | inline Maybe<bool> Has(
|
189 | v8::Handle<v8::Object> obj
|
190 | , v8::Handle<v8::String> key) {
|
191 | return Just<bool>(obj->Has(key));
|
192 | }
|
193 |
|
194 | inline Maybe<bool> Has(
|
195 | v8::Handle<v8::Object> obj
|
196 | , uint32_t index) {
|
197 | return Just<bool>(obj->Has(index));
|
198 | }
|
199 |
|
200 | inline Maybe<bool> Delete(
|
201 | v8::Handle<v8::Object> obj
|
202 | , v8::Handle<v8::String> key) {
|
203 | return Just<bool>(obj->Delete(key));
|
204 | }
|
205 |
|
206 | inline Maybe<bool> Delete(
|
207 | v8::Handle<v8::Object> obj
|
208 | , uint32_t index) {
|
209 | return Just<bool>(obj->Delete(index));
|
210 | }
|
211 |
|
212 | inline
|
213 | MaybeLocal<v8::Array> GetPropertyNames(v8::Handle<v8::Object> obj) {
|
214 | return MaybeLocal<v8::Array>(obj->GetPropertyNames());
|
215 | }
|
216 |
|
217 | inline
|
218 | MaybeLocal<v8::Array> GetOwnPropertyNames(v8::Handle<v8::Object> obj) {
|
219 | return MaybeLocal<v8::Array>(obj->GetOwnPropertyNames());
|
220 | }
|
221 |
|
222 | inline Maybe<bool> SetPrototype(
|
223 | v8::Handle<v8::Object> obj
|
224 | , v8::Handle<v8::Value> prototype) {
|
225 | return Just<bool>(obj->SetPrototype(prototype));
|
226 | }
|
227 |
|
228 | inline MaybeLocal<v8::String> ObjectProtoToString(
|
229 | v8::Handle<v8::Object> obj) {
|
230 | return MaybeLocal<v8::String>(obj->ObjectProtoToString());
|
231 | }
|
232 |
|
233 | inline Maybe<bool> HasOwnProperty(
|
234 | v8::Handle<v8::Object> obj
|
235 | , v8::Handle<v8::String> key) {
|
236 | return Just<bool>(obj->HasOwnProperty(key));
|
237 | }
|
238 |
|
239 | inline Maybe<bool> HasRealNamedProperty(
|
240 | v8::Handle<v8::Object> obj
|
241 | , v8::Handle<v8::String> key) {
|
242 | return Just<bool>(obj->HasRealNamedProperty(key));
|
243 | }
|
244 |
|
245 | inline Maybe<bool> HasRealIndexedProperty(
|
246 | v8::Handle<v8::Object> obj
|
247 | , uint32_t index) {
|
248 | return Just<bool>(obj->HasRealIndexedProperty(index));
|
249 | }
|
250 |
|
251 | inline Maybe<bool> HasRealNamedCallbackProperty(
|
252 | v8::Handle<v8::Object> obj
|
253 | , v8::Handle<v8::String> key) {
|
254 | return Just<bool>(obj->HasRealNamedCallbackProperty(key));
|
255 | }
|
256 |
|
257 | inline MaybeLocal<v8::Value> GetRealNamedPropertyInPrototypeChain(
|
258 | v8::Handle<v8::Object> obj
|
259 | , v8::Handle<v8::String> key) {
|
260 | return MaybeLocal<v8::Value>(
|
261 | obj->GetRealNamedPropertyInPrototypeChain(key));
|
262 | }
|
263 |
|
264 | inline MaybeLocal<v8::Value> GetRealNamedProperty(
|
265 | v8::Handle<v8::Object> obj
|
266 | , v8::Handle<v8::String> key) {
|
267 | return MaybeLocal<v8::Value>(obj->GetRealNamedProperty(key));
|
268 | }
|
269 |
|
270 | inline MaybeLocal<v8::Value> CallAsFunction(
|
271 | v8::Handle<v8::Object> obj
|
272 | , v8::Handle<v8::Object> recv
|
273 | , int argc
|
274 | , v8::Handle<v8::Value> argv[]) {
|
275 | return MaybeLocal<v8::Value>(obj->CallAsFunction(recv, argc, argv));
|
276 | }
|
277 |
|
278 | inline MaybeLocal<v8::Value> CallAsConstructor(
|
279 | v8::Handle<v8::Object> obj
|
280 | , int argc
|
281 | , v8::Local<v8::Value> argv[]) {
|
282 | return MaybeLocal<v8::Value>(obj->CallAsConstructor(argc, argv));
|
283 | }
|
284 |
|
285 | inline
|
286 | MaybeLocal<v8::String> GetSourceLine(v8::Handle<v8::Message> msg) {
|
287 | return MaybeLocal<v8::String>(msg->GetSourceLine());
|
288 | }
|
289 |
|
290 | inline Maybe<int> GetLineNumber(v8::Handle<v8::Message> msg) {
|
291 | return Just<int>(msg->GetLineNumber());
|
292 | }
|
293 |
|
294 | inline Maybe<int> GetStartColumn(v8::Handle<v8::Message> msg) {
|
295 | return Just<int>(msg->GetStartColumn());
|
296 | }
|
297 |
|
298 | inline Maybe<int> GetEndColumn(v8::Handle<v8::Message> msg) {
|
299 | return Just<int>(msg->GetEndColumn());
|
300 | }
|
301 |
|
302 | inline MaybeLocal<v8::Object> CloneElementAt(
|
303 | v8::Handle<v8::Array> array
|
304 | , uint32_t index) {
|
305 | return MaybeLocal<v8::Object>(array->CloneElementAt(index));
|
306 | }
|
307 |
|
308 | inline MaybeLocal<v8::Value> Call(
|
309 | v8::Local<v8::Function> fun
|
310 | , v8::Local<v8::Object> recv
|
311 | , int argc
|
312 | , v8::Local<v8::Value> argv[]) {
|
313 | return MaybeLocal<v8::Value>(fun->Call(recv, argc, argv));
|
314 | }
|
315 |
|
316 | #endif
|