UNPKG

17.5 kBtext/x-cView Raw
1/*********************************************************************
2 * NAN - Native Abstractions for Node.js
3 *
4 * Copyright (c) 2018 NAN contributors
5 *
6 * MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
7 ********************************************************************/
8
9#ifndef NAN_CALLBACKS_12_INL_H_
10#define NAN_CALLBACKS_12_INL_H_
11
12template<typename T>
13class ReturnValue {
14 v8::ReturnValue<T> value_;
15
16 public:
17 template <class S>
18 explicit inline ReturnValue(const v8::ReturnValue<S> &value) :
19 value_(value) {}
20 template <class S>
21 explicit inline ReturnValue(const ReturnValue<S>& that)
22 : value_(that.value_) {
23 TYPE_CHECK(T, S);
24 }
25
26 // Handle setters
27 template <typename S> inline void Set(const v8::Local<S> &handle) {
28 TYPE_CHECK(T, S);
29 value_.Set(handle);
30 }
31
32 template <typename S> inline void Set(const Global<S> &handle) {
33 TYPE_CHECK(T, S);
34#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
35 (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && \
36 (V8_MINOR_VERSION > 5 || (V8_MINOR_VERSION == 5 && \
37 defined(V8_BUILD_NUMBER) && V8_BUILD_NUMBER >= 8))))
38 value_.Set(handle);
39#else
40 value_.Set(*reinterpret_cast<const v8::Persistent<S>*>(&handle));
41 const_cast<Global<S> &>(handle).Reset();
42#endif
43 }
44
45 // Fast primitive setters
46 inline void Set(bool value) {
47 TYPE_CHECK(T, v8::Boolean);
48 value_.Set(value);
49 }
50
51 inline void Set(double i) {
52 TYPE_CHECK(T, v8::Number);
53 value_.Set(i);
54 }
55
56 inline void Set(int32_t i) {
57 TYPE_CHECK(T, v8::Integer);
58 value_.Set(i);
59 }
60
61 inline void Set(uint32_t i) {
62 TYPE_CHECK(T, v8::Integer);
63 value_.Set(i);
64 }
65
66 // Fast JS primitive setters
67 inline void SetNull() {
68 TYPE_CHECK(T, v8::Primitive);
69 value_.SetNull();
70 }
71
72 inline void SetUndefined() {
73 TYPE_CHECK(T, v8::Primitive);
74 value_.SetUndefined();
75 }
76
77 inline void SetEmptyString() {
78 TYPE_CHECK(T, v8::String);
79 value_.SetEmptyString();
80 }
81
82 // Convenience getter for isolate
83 inline v8::Isolate *GetIsolate() const {
84 return value_.GetIsolate();
85 }
86
87 // Pointer setter: Uncompilable to prevent inadvertent misuse.
88 template<typename S>
89 inline void Set(S *whatever) { TYPE_CHECK(S*, v8::Primitive); }
90};
91
92template<typename T>
93class FunctionCallbackInfo {
94 const v8::FunctionCallbackInfo<T> &info_;
95 const v8::Local<v8::Value> data_;
96
97 public:
98 explicit inline FunctionCallbackInfo(
99 const v8::FunctionCallbackInfo<T> &info
100 , v8::Local<v8::Value> data) :
101 info_(info)
102 , data_(data) {}
103
104 inline ReturnValue<T> GetReturnValue() const {
105 return ReturnValue<T>(info_.GetReturnValue());
106 }
107
108 inline v8::Local<v8::Function> Callee() const { return info_.Callee(); }
109 inline v8::Local<v8::Value> Data() const { return data_; }
110 inline v8::Local<v8::Object> Holder() const { return info_.Holder(); }
111 inline bool IsConstructCall() const { return info_.IsConstructCall(); }
112 inline int Length() const { return info_.Length(); }
113 inline v8::Local<v8::Value> operator[](int i) const { return info_[i]; }
114 inline v8::Local<v8::Object> This() const { return info_.This(); }
115 inline v8::Isolate *GetIsolate() const { return info_.GetIsolate(); }
116
117
118 protected:
119 static const int kHolderIndex = 0;
120 static const int kIsolateIndex = 1;
121 static const int kReturnValueDefaultValueIndex = 2;
122 static const int kReturnValueIndex = 3;
123 static const int kDataIndex = 4;
124 static const int kCalleeIndex = 5;
125 static const int kContextSaveIndex = 6;
126 static const int kArgsLength = 7;
127
128 private:
129 NAN_DISALLOW_ASSIGN_COPY_MOVE(FunctionCallbackInfo)
130};
131
132template<typename T>
133class PropertyCallbackInfo {
134 const v8::PropertyCallbackInfo<T> &info_;
135 const v8::Local<v8::Value> data_;
136
137 public:
138 explicit inline PropertyCallbackInfo(
139 const v8::PropertyCallbackInfo<T> &info
140 , const v8::Local<v8::Value> data) :
141 info_(info)
142 , data_(data) {}
143
144 inline v8::Isolate* GetIsolate() const { return info_.GetIsolate(); }
145 inline v8::Local<v8::Value> Data() const { return data_; }
146 inline v8::Local<v8::Object> This() const { return info_.This(); }
147 inline v8::Local<v8::Object> Holder() const { return info_.Holder(); }
148 inline ReturnValue<T> GetReturnValue() const {
149 return ReturnValue<T>(info_.GetReturnValue());
150 }
151
152 protected:
153 static const int kHolderIndex = 0;
154 static const int kIsolateIndex = 1;
155 static const int kReturnValueDefaultValueIndex = 2;
156 static const int kReturnValueIndex = 3;
157 static const int kDataIndex = 4;
158 static const int kThisIndex = 5;
159 static const int kArgsLength = 6;
160
161 private:
162 NAN_DISALLOW_ASSIGN_COPY_MOVE(PropertyCallbackInfo)
163};
164
165namespace imp {
166static
167void FunctionCallbackWrapper(const v8::FunctionCallbackInfo<v8::Value> &info) {
168 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
169 FunctionCallback callback = reinterpret_cast<FunctionCallback>(
170 reinterpret_cast<intptr_t>(
171 obj->GetInternalField(kFunctionIndex).As<v8::External>()->Value()));
172 FunctionCallbackInfo<v8::Value>
173 cbinfo(info, obj->GetInternalField(kDataIndex));
174 callback(cbinfo);
175}
176
177typedef void (*NativeFunction)(const v8::FunctionCallbackInfo<v8::Value> &);
178
179#if NODE_MODULE_VERSION > NODE_0_12_MODULE_VERSION
180static
181void GetterCallbackWrapper(
182 v8::Local<v8::Name> property
183 , const v8::PropertyCallbackInfo<v8::Value> &info) {
184 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
185 PropertyCallbackInfo<v8::Value>
186 cbinfo(info, obj->GetInternalField(kDataIndex));
187 GetterCallback callback = reinterpret_cast<GetterCallback>(
188 reinterpret_cast<intptr_t>(
189 obj->GetInternalField(kGetterIndex).As<v8::External>()->Value()));
190 callback(property.As<v8::String>(), cbinfo);
191}
192
193typedef void (*NativeGetter)
194 (v8::Local<v8::Name>, const v8::PropertyCallbackInfo<v8::Value> &);
195
196static
197void SetterCallbackWrapper(
198 v8::Local<v8::Name> property
199 , v8::Local<v8::Value> value
200 , const v8::PropertyCallbackInfo<void> &info) {
201 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
202 PropertyCallbackInfo<void>
203 cbinfo(info, obj->GetInternalField(kDataIndex));
204 SetterCallback callback = reinterpret_cast<SetterCallback>(
205 reinterpret_cast<intptr_t>(
206 obj->GetInternalField(kSetterIndex).As<v8::External>()->Value()));
207 callback(property.As<v8::String>(), value, cbinfo);
208}
209
210typedef void (*NativeSetter)(
211 v8::Local<v8::Name>
212 , v8::Local<v8::Value>
213 , const v8::PropertyCallbackInfo<void> &);
214#else
215static
216void GetterCallbackWrapper(
217 v8::Local<v8::String> property
218 , const v8::PropertyCallbackInfo<v8::Value> &info) {
219 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
220 PropertyCallbackInfo<v8::Value>
221 cbinfo(info, obj->GetInternalField(kDataIndex));
222 GetterCallback callback = reinterpret_cast<GetterCallback>(
223 reinterpret_cast<intptr_t>(
224 obj->GetInternalField(kGetterIndex).As<v8::External>()->Value()));
225 callback(property, cbinfo);
226}
227
228typedef void (*NativeGetter)
229 (v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Value> &);
230
231static
232void SetterCallbackWrapper(
233 v8::Local<v8::String> property
234 , v8::Local<v8::Value> value
235 , const v8::PropertyCallbackInfo<void> &info) {
236 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
237 PropertyCallbackInfo<void>
238 cbinfo(info, obj->GetInternalField(kDataIndex));
239 SetterCallback callback = reinterpret_cast<SetterCallback>(
240 reinterpret_cast<intptr_t>(
241 obj->GetInternalField(kSetterIndex).As<v8::External>()->Value()));
242 callback(property, value, cbinfo);
243}
244
245typedef void (*NativeSetter)(
246 v8::Local<v8::String>
247 , v8::Local<v8::Value>
248 , const v8::PropertyCallbackInfo<void> &);
249#endif
250
251#if NODE_MODULE_VERSION > NODE_0_12_MODULE_VERSION
252static
253void PropertyGetterCallbackWrapper(
254 v8::Local<v8::Name> property
255 , const v8::PropertyCallbackInfo<v8::Value> &info) {
256 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
257 PropertyCallbackInfo<v8::Value>
258 cbinfo(info, obj->GetInternalField(kDataIndex));
259 PropertyGetterCallback callback = reinterpret_cast<PropertyGetterCallback>(
260 reinterpret_cast<intptr_t>(
261 obj->GetInternalField(kPropertyGetterIndex)
262 .As<v8::External>()->Value()));
263 callback(property.As<v8::String>(), cbinfo);
264}
265
266typedef void (*NativePropertyGetter)
267 (v8::Local<v8::Name>, const v8::PropertyCallbackInfo<v8::Value> &);
268
269static
270void PropertySetterCallbackWrapper(
271 v8::Local<v8::Name> property
272 , v8::Local<v8::Value> value
273 , const v8::PropertyCallbackInfo<v8::Value> &info) {
274 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
275 PropertyCallbackInfo<v8::Value>
276 cbinfo(info, obj->GetInternalField(kDataIndex));
277 PropertySetterCallback callback = reinterpret_cast<PropertySetterCallback>(
278 reinterpret_cast<intptr_t>(
279 obj->GetInternalField(kPropertySetterIndex)
280 .As<v8::External>()->Value()));
281 callback(property.As<v8::String>(), value, cbinfo);
282}
283
284typedef void (*NativePropertySetter)(
285 v8::Local<v8::Name>
286 , v8::Local<v8::Value>
287 , const v8::PropertyCallbackInfo<v8::Value> &);
288
289static
290void PropertyEnumeratorCallbackWrapper(
291 const v8::PropertyCallbackInfo<v8::Array> &info) {
292 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
293 PropertyCallbackInfo<v8::Array>
294 cbinfo(info, obj->GetInternalField(kDataIndex));
295 PropertyEnumeratorCallback callback =
296 reinterpret_cast<PropertyEnumeratorCallback>(reinterpret_cast<intptr_t>(
297 obj->GetInternalField(kPropertyEnumeratorIndex)
298 .As<v8::External>()->Value()));
299 callback(cbinfo);
300}
301
302typedef void (*NativePropertyEnumerator)
303 (const v8::PropertyCallbackInfo<v8::Array> &);
304
305static
306void PropertyDeleterCallbackWrapper(
307 v8::Local<v8::Name> property
308 , const v8::PropertyCallbackInfo<v8::Boolean> &info) {
309 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
310 PropertyCallbackInfo<v8::Boolean>
311 cbinfo(info, obj->GetInternalField(kDataIndex));
312 PropertyDeleterCallback callback = reinterpret_cast<PropertyDeleterCallback>(
313 reinterpret_cast<intptr_t>(
314 obj->GetInternalField(kPropertyDeleterIndex)
315 .As<v8::External>()->Value()));
316 callback(property.As<v8::String>(), cbinfo);
317}
318
319typedef void (NativePropertyDeleter)
320 (v8::Local<v8::Name>, const v8::PropertyCallbackInfo<v8::Boolean> &);
321
322static
323void PropertyQueryCallbackWrapper(
324 v8::Local<v8::Name> property
325 , const v8::PropertyCallbackInfo<v8::Integer> &info) {
326 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
327 PropertyCallbackInfo<v8::Integer>
328 cbinfo(info, obj->GetInternalField(kDataIndex));
329 PropertyQueryCallback callback = reinterpret_cast<PropertyQueryCallback>(
330 reinterpret_cast<intptr_t>(
331 obj->GetInternalField(kPropertyQueryIndex)
332 .As<v8::External>()->Value()));
333 callback(property.As<v8::String>(), cbinfo);
334}
335
336typedef void (*NativePropertyQuery)
337 (v8::Local<v8::Name>, const v8::PropertyCallbackInfo<v8::Integer> &);
338#else
339static
340void PropertyGetterCallbackWrapper(
341 v8::Local<v8::String> property
342 , const v8::PropertyCallbackInfo<v8::Value> &info) {
343 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
344 PropertyCallbackInfo<v8::Value>
345 cbinfo(info, obj->GetInternalField(kDataIndex));
346 PropertyGetterCallback callback = reinterpret_cast<PropertyGetterCallback>(
347 reinterpret_cast<intptr_t>(
348 obj->GetInternalField(kPropertyGetterIndex)
349 .As<v8::External>()->Value()));
350 callback(property, cbinfo);
351}
352
353typedef void (*NativePropertyGetter)
354 (v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Value> &);
355
356static
357void PropertySetterCallbackWrapper(
358 v8::Local<v8::String> property
359 , v8::Local<v8::Value> value
360 , const v8::PropertyCallbackInfo<v8::Value> &info) {
361 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
362 PropertyCallbackInfo<v8::Value>
363 cbinfo(info, obj->GetInternalField(kDataIndex));
364 PropertySetterCallback callback = reinterpret_cast<PropertySetterCallback>(
365 reinterpret_cast<intptr_t>(
366 obj->GetInternalField(kPropertySetterIndex)
367 .As<v8::External>()->Value()));
368 callback(property, value, cbinfo);
369}
370
371typedef void (*NativePropertySetter)(
372 v8::Local<v8::String>
373 , v8::Local<v8::Value>
374 , const v8::PropertyCallbackInfo<v8::Value> &);
375
376static
377void PropertyEnumeratorCallbackWrapper(
378 const v8::PropertyCallbackInfo<v8::Array> &info) {
379 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
380 PropertyCallbackInfo<v8::Array>
381 cbinfo(info, obj->GetInternalField(kDataIndex));
382 PropertyEnumeratorCallback callback =
383 reinterpret_cast<PropertyEnumeratorCallback>(reinterpret_cast<intptr_t>(
384 obj->GetInternalField(kPropertyEnumeratorIndex)
385 .As<v8::External>()->Value()));
386 callback(cbinfo);
387}
388
389typedef void (*NativePropertyEnumerator)
390 (const v8::PropertyCallbackInfo<v8::Array> &);
391
392static
393void PropertyDeleterCallbackWrapper(
394 v8::Local<v8::String> property
395 , const v8::PropertyCallbackInfo<v8::Boolean> &info) {
396 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
397 PropertyCallbackInfo<v8::Boolean>
398 cbinfo(info, obj->GetInternalField(kDataIndex));
399 PropertyDeleterCallback callback = reinterpret_cast<PropertyDeleterCallback>(
400 reinterpret_cast<intptr_t>(
401 obj->GetInternalField(kPropertyDeleterIndex)
402 .As<v8::External>()->Value()));
403 callback(property, cbinfo);
404}
405
406typedef void (NativePropertyDeleter)
407 (v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Boolean> &);
408
409static
410void PropertyQueryCallbackWrapper(
411 v8::Local<v8::String> property
412 , const v8::PropertyCallbackInfo<v8::Integer> &info) {
413 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
414 PropertyCallbackInfo<v8::Integer>
415 cbinfo(info, obj->GetInternalField(kDataIndex));
416 PropertyQueryCallback callback = reinterpret_cast<PropertyQueryCallback>(
417 reinterpret_cast<intptr_t>(
418 obj->GetInternalField(kPropertyQueryIndex)
419 .As<v8::External>()->Value()));
420 callback(property, cbinfo);
421}
422
423typedef void (*NativePropertyQuery)
424 (v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Integer> &);
425#endif
426
427static
428void IndexGetterCallbackWrapper(
429 uint32_t index, const v8::PropertyCallbackInfo<v8::Value> &info) {
430 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
431 PropertyCallbackInfo<v8::Value>
432 cbinfo(info, obj->GetInternalField(kDataIndex));
433 IndexGetterCallback callback = reinterpret_cast<IndexGetterCallback>(
434 reinterpret_cast<intptr_t>(
435 obj->GetInternalField(kIndexPropertyGetterIndex)
436 .As<v8::External>()->Value()));
437 callback(index, cbinfo);
438}
439
440typedef void (*NativeIndexGetter)
441 (uint32_t, const v8::PropertyCallbackInfo<v8::Value> &);
442
443static
444void IndexSetterCallbackWrapper(
445 uint32_t index
446 , v8::Local<v8::Value> value
447 , const v8::PropertyCallbackInfo<v8::Value> &info) {
448 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
449 PropertyCallbackInfo<v8::Value>
450 cbinfo(info, obj->GetInternalField(kDataIndex));
451 IndexSetterCallback callback = reinterpret_cast<IndexSetterCallback>(
452 reinterpret_cast<intptr_t>(
453 obj->GetInternalField(kIndexPropertySetterIndex)
454 .As<v8::External>()->Value()));
455 callback(index, value, cbinfo);
456}
457
458typedef void (*NativeIndexSetter)(
459 uint32_t
460 , v8::Local<v8::Value>
461 , const v8::PropertyCallbackInfo<v8::Value> &);
462
463static
464void IndexEnumeratorCallbackWrapper(
465 const v8::PropertyCallbackInfo<v8::Array> &info) {
466 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
467 PropertyCallbackInfo<v8::Array>
468 cbinfo(info, obj->GetInternalField(kDataIndex));
469 IndexEnumeratorCallback callback = reinterpret_cast<IndexEnumeratorCallback>(
470 reinterpret_cast<intptr_t>(
471 obj->GetInternalField(
472 kIndexPropertyEnumeratorIndex).As<v8::External>()->Value()));
473 callback(cbinfo);
474}
475
476typedef void (*NativeIndexEnumerator)
477 (const v8::PropertyCallbackInfo<v8::Array> &);
478
479static
480void IndexDeleterCallbackWrapper(
481 uint32_t index, const v8::PropertyCallbackInfo<v8::Boolean> &info) {
482 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
483 PropertyCallbackInfo<v8::Boolean>
484 cbinfo(info, obj->GetInternalField(kDataIndex));
485 IndexDeleterCallback callback = reinterpret_cast<IndexDeleterCallback>(
486 reinterpret_cast<intptr_t>(
487 obj->GetInternalField(kIndexPropertyDeleterIndex)
488 .As<v8::External>()->Value()));
489 callback(index, cbinfo);
490}
491
492typedef void (*NativeIndexDeleter)
493 (uint32_t, const v8::PropertyCallbackInfo<v8::Boolean> &);
494
495static
496void IndexQueryCallbackWrapper(
497 uint32_t index, const v8::PropertyCallbackInfo<v8::Integer> &info) {
498 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
499 PropertyCallbackInfo<v8::Integer>
500 cbinfo(info, obj->GetInternalField(kDataIndex));
501 IndexQueryCallback callback = reinterpret_cast<IndexQueryCallback>(
502 reinterpret_cast<intptr_t>(
503 obj->GetInternalField(kIndexPropertyQueryIndex)
504 .As<v8::External>()->Value()));
505 callback(index, cbinfo);
506}
507
508typedef void (*NativeIndexQuery)
509 (uint32_t, const v8::PropertyCallbackInfo<v8::Integer> &);
510} // end of namespace imp
511
512#endif // NAN_CALLBACKS_12_INL_H_