UNPKG

18.2 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#if NODE_MAJOR_VERSION < 10
109 inline v8::Local<v8::Function> Callee() const { return info_.Callee(); }
110#endif
111 inline v8::Local<v8::Value> Data() const { return data_; }
112 inline v8::Local<v8::Object> Holder() const { return info_.Holder(); }
113 inline bool IsConstructCall() const { return info_.IsConstructCall(); }
114 inline int Length() const { return info_.Length(); }
115 inline v8::Local<v8::Value> operator[](int i) const { return info_[i]; }
116 inline v8::Local<v8::Object> This() const { return info_.This(); }
117 inline v8::Isolate *GetIsolate() const { return info_.GetIsolate(); }
118
119
120 protected:
121 static const int kHolderIndex = 0;
122 static const int kIsolateIndex = 1;
123 static const int kReturnValueDefaultValueIndex = 2;
124 static const int kReturnValueIndex = 3;
125 static const int kDataIndex = 4;
126 static const int kCalleeIndex = 5;
127 static const int kContextSaveIndex = 6;
128 static const int kArgsLength = 7;
129
130 private:
131 NAN_DISALLOW_ASSIGN_COPY_MOVE(FunctionCallbackInfo)
132};
133
134template<typename T>
135class PropertyCallbackInfo {
136 const v8::PropertyCallbackInfo<T> &info_;
137 const v8::Local<v8::Value> data_;
138
139 public:
140 explicit inline PropertyCallbackInfo(
141 const v8::PropertyCallbackInfo<T> &info
142 , const v8::Local<v8::Value> data) :
143 info_(info)
144 , data_(data) {}
145
146 inline v8::Isolate* GetIsolate() const { return info_.GetIsolate(); }
147 inline v8::Local<v8::Value> Data() const { return data_; }
148 inline v8::Local<v8::Object> This() const { return info_.This(); }
149 inline v8::Local<v8::Object> Holder() const { return info_.Holder(); }
150 inline ReturnValue<T> GetReturnValue() const {
151 return ReturnValue<T>(info_.GetReturnValue());
152 }
153
154 protected:
155 static const int kHolderIndex = 0;
156 static const int kIsolateIndex = 1;
157 static const int kReturnValueDefaultValueIndex = 2;
158 static const int kReturnValueIndex = 3;
159 static const int kDataIndex = 4;
160 static const int kThisIndex = 5;
161 static const int kArgsLength = 6;
162
163 private:
164 NAN_DISALLOW_ASSIGN_COPY_MOVE(PropertyCallbackInfo)
165};
166
167namespace imp {
168static
169void FunctionCallbackWrapper(const v8::FunctionCallbackInfo<v8::Value> &info) {
170 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
171 FunctionCallback callback = reinterpret_cast<FunctionCallback>(
172 reinterpret_cast<intptr_t>(
173 obj->GetInternalField(kFunctionIndex)
174 .As<v8::Value>().As<v8::External>()->Value()));
175 FunctionCallbackInfo<v8::Value>
176 cbinfo(info, obj->GetInternalField(kDataIndex).As<v8::Value>());
177 callback(cbinfo);
178}
179
180typedef void (*NativeFunction)(const v8::FunctionCallbackInfo<v8::Value> &);
181
182#if NODE_MODULE_VERSION > NODE_0_12_MODULE_VERSION
183static
184void GetterCallbackWrapper(
185 v8::Local<v8::Name> property
186 , const v8::PropertyCallbackInfo<v8::Value> &info) {
187 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
188 PropertyCallbackInfo<v8::Value>
189 cbinfo(info, obj->GetInternalField(kDataIndex).As<v8::Value>());
190 GetterCallback callback = reinterpret_cast<GetterCallback>(
191 reinterpret_cast<intptr_t>(
192 obj->GetInternalField(kGetterIndex)
193 .As<v8::Value>().As<v8::External>()->Value()));
194 callback(property.As<v8::String>(), cbinfo);
195}
196
197typedef void (*NativeGetter)
198 (v8::Local<v8::Name>, const v8::PropertyCallbackInfo<v8::Value> &);
199
200static
201void SetterCallbackWrapper(
202 v8::Local<v8::Name> property
203 , v8::Local<v8::Value> value
204 , const v8::PropertyCallbackInfo<void> &info) {
205 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
206 PropertyCallbackInfo<void>
207 cbinfo(info, obj->GetInternalField(kDataIndex).As<v8::Value>());
208 SetterCallback callback = reinterpret_cast<SetterCallback>(
209 reinterpret_cast<intptr_t>(
210 obj->GetInternalField(kSetterIndex)
211 .As<v8::Value>().As<v8::External>()->Value()));
212 callback(property.As<v8::String>(), value, cbinfo);
213}
214
215typedef void (*NativeSetter)(
216 v8::Local<v8::Name>
217 , v8::Local<v8::Value>
218 , const v8::PropertyCallbackInfo<void> &);
219#else
220static
221void GetterCallbackWrapper(
222 v8::Local<v8::String> property
223 , const v8::PropertyCallbackInfo<v8::Value> &info) {
224 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
225 PropertyCallbackInfo<v8::Value>
226 cbinfo(info, obj->GetInternalField(kDataIndex).As<v8::Value>());
227 GetterCallback callback = reinterpret_cast<GetterCallback>(
228 reinterpret_cast<intptr_t>(
229 obj->GetInternalField(kGetterIndex)
230 .As<v8::Value>().As<v8::External>()->Value()));
231 callback(property, cbinfo);
232}
233
234typedef void (*NativeGetter)
235 (v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Value> &);
236
237static
238void SetterCallbackWrapper(
239 v8::Local<v8::String> property
240 , v8::Local<v8::Value> value
241 , const v8::PropertyCallbackInfo<void> &info) {
242 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
243 PropertyCallbackInfo<void>
244 cbinfo(info, obj->GetInternalField(kDataIndex).As<v8::Value>());
245 SetterCallback callback = reinterpret_cast<SetterCallback>(
246 reinterpret_cast<intptr_t>(
247 obj->GetInternalField(kSetterIndex)
248 .As<v8::Value>().As<v8::External>()->Value()));
249 callback(property, value, cbinfo);
250}
251
252typedef void (*NativeSetter)(
253 v8::Local<v8::String>
254 , v8::Local<v8::Value>
255 , const v8::PropertyCallbackInfo<void> &);
256#endif
257
258#if NODE_MODULE_VERSION > NODE_0_12_MODULE_VERSION
259static
260void PropertyGetterCallbackWrapper(
261 v8::Local<v8::Name> property
262 , const v8::PropertyCallbackInfo<v8::Value> &info) {
263 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
264 PropertyCallbackInfo<v8::Value>
265 cbinfo(info, obj->GetInternalField(kDataIndex).As<v8::Value>());
266 PropertyGetterCallback callback = reinterpret_cast<PropertyGetterCallback>(
267 reinterpret_cast<intptr_t>(
268 obj->GetInternalField(kPropertyGetterIndex)
269 .As<v8::Value>().As<v8::External>()->Value()));
270 callback(property.As<v8::String>(), cbinfo);
271}
272
273typedef void (*NativePropertyGetter)
274 (v8::Local<v8::Name>, const v8::PropertyCallbackInfo<v8::Value> &);
275
276static
277void PropertySetterCallbackWrapper(
278 v8::Local<v8::Name> property
279 , v8::Local<v8::Value> value
280 , const v8::PropertyCallbackInfo<v8::Value> &info) {
281 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
282 PropertyCallbackInfo<v8::Value>
283 cbinfo(info, obj->GetInternalField(kDataIndex).As<v8::Value>());
284 PropertySetterCallback callback = reinterpret_cast<PropertySetterCallback>(
285 reinterpret_cast<intptr_t>(
286 obj->GetInternalField(kPropertySetterIndex)
287 .As<v8::Value>().As<v8::External>()->Value()));
288 callback(property.As<v8::String>(), value, cbinfo);
289}
290
291typedef void (*NativePropertySetter)(
292 v8::Local<v8::Name>
293 , v8::Local<v8::Value>
294 , const v8::PropertyCallbackInfo<v8::Value> &);
295
296static
297void PropertyEnumeratorCallbackWrapper(
298 const v8::PropertyCallbackInfo<v8::Array> &info) {
299 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
300 PropertyCallbackInfo<v8::Array>
301 cbinfo(info, obj->GetInternalField(kDataIndex).As<v8::Value>());
302 PropertyEnumeratorCallback callback =
303 reinterpret_cast<PropertyEnumeratorCallback>(reinterpret_cast<intptr_t>(
304 obj->GetInternalField(kPropertyEnumeratorIndex)
305 .As<v8::Value>().As<v8::External>()->Value()));
306 callback(cbinfo);
307}
308
309typedef void (*NativePropertyEnumerator)
310 (const v8::PropertyCallbackInfo<v8::Array> &);
311
312static
313void PropertyDeleterCallbackWrapper(
314 v8::Local<v8::Name> property
315 , const v8::PropertyCallbackInfo<v8::Boolean> &info) {
316 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
317 PropertyCallbackInfo<v8::Boolean>
318 cbinfo(info, obj->GetInternalField(kDataIndex).As<v8::Value>());
319 PropertyDeleterCallback callback = reinterpret_cast<PropertyDeleterCallback>(
320 reinterpret_cast<intptr_t>(
321 obj->GetInternalField(kPropertyDeleterIndex)
322 .As<v8::Value>().As<v8::External>()->Value()));
323 callback(property.As<v8::String>(), cbinfo);
324}
325
326typedef void (NativePropertyDeleter)
327 (v8::Local<v8::Name>, const v8::PropertyCallbackInfo<v8::Boolean> &);
328
329static
330void PropertyQueryCallbackWrapper(
331 v8::Local<v8::Name> property
332 , const v8::PropertyCallbackInfo<v8::Integer> &info) {
333 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
334 PropertyCallbackInfo<v8::Integer>
335 cbinfo(info, obj->GetInternalField(kDataIndex).As<v8::Value>());
336 PropertyQueryCallback callback = reinterpret_cast<PropertyQueryCallback>(
337 reinterpret_cast<intptr_t>(
338 obj->GetInternalField(kPropertyQueryIndex)
339 .As<v8::Value>().As<v8::External>()->Value()));
340 callback(property.As<v8::String>(), cbinfo);
341}
342
343typedef void (*NativePropertyQuery)
344 (v8::Local<v8::Name>, const v8::PropertyCallbackInfo<v8::Integer> &);
345#else
346static
347void PropertyGetterCallbackWrapper(
348 v8::Local<v8::String> property
349 , const v8::PropertyCallbackInfo<v8::Value> &info) {
350 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
351 PropertyCallbackInfo<v8::Value>
352 cbinfo(info, obj->GetInternalField(kDataIndex).As<v8::Value>());
353 PropertyGetterCallback callback = reinterpret_cast<PropertyGetterCallback>(
354 reinterpret_cast<intptr_t>(
355 obj->GetInternalField(kPropertyGetterIndex)
356 .As<v8::Value>().As<v8::External>()->Value()));
357 callback(property, cbinfo);
358}
359
360typedef void (*NativePropertyGetter)
361 (v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Value> &);
362
363static
364void PropertySetterCallbackWrapper(
365 v8::Local<v8::String> property
366 , v8::Local<v8::Value> value
367 , const v8::PropertyCallbackInfo<v8::Value> &info) {
368 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
369 PropertyCallbackInfo<v8::Value>
370 cbinfo(info, obj->GetInternalField(kDataIndex).As<v8::Value>());
371 PropertySetterCallback callback = reinterpret_cast<PropertySetterCallback>(
372 reinterpret_cast<intptr_t>(
373 obj->GetInternalField(kPropertySetterIndex)
374 .As<v8::Value>().As<v8::External>()->Value()));
375 callback(property, value, cbinfo);
376}
377
378typedef void (*NativePropertySetter)(
379 v8::Local<v8::String>
380 , v8::Local<v8::Value>
381 , const v8::PropertyCallbackInfo<v8::Value> &);
382
383static
384void PropertyEnumeratorCallbackWrapper(
385 const v8::PropertyCallbackInfo<v8::Array> &info) {
386 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
387 PropertyCallbackInfo<v8::Array>
388 cbinfo(info, obj->GetInternalField(kDataIndex).As<v8::Value>());
389 PropertyEnumeratorCallback callback =
390 reinterpret_cast<PropertyEnumeratorCallback>(reinterpret_cast<intptr_t>(
391 obj->GetInternalField(kPropertyEnumeratorIndex)
392 .As<v8::Value>().As<v8::External>()->Value()));
393 callback(cbinfo);
394}
395
396typedef void (*NativePropertyEnumerator)
397 (const v8::PropertyCallbackInfo<v8::Array> &);
398
399static
400void PropertyDeleterCallbackWrapper(
401 v8::Local<v8::String> property
402 , const v8::PropertyCallbackInfo<v8::Boolean> &info) {
403 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
404 PropertyCallbackInfo<v8::Boolean>
405 cbinfo(info, obj->GetInternalField(kDataIndex).As<v8::Value>());
406 PropertyDeleterCallback callback = reinterpret_cast<PropertyDeleterCallback>(
407 reinterpret_cast<intptr_t>(
408 obj->GetInternalField(kPropertyDeleterIndex)
409 .As<v8::Value>().As<v8::External>()->Value()));
410 callback(property, cbinfo);
411}
412
413typedef void (NativePropertyDeleter)
414 (v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Boolean> &);
415
416static
417void PropertyQueryCallbackWrapper(
418 v8::Local<v8::String> property
419 , const v8::PropertyCallbackInfo<v8::Integer> &info) {
420 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
421 PropertyCallbackInfo<v8::Integer>
422 cbinfo(info, obj->GetInternalField(kDataIndex).As<v8::Value>());
423 PropertyQueryCallback callback = reinterpret_cast<PropertyQueryCallback>(
424 reinterpret_cast<intptr_t>(
425 obj->GetInternalField(kPropertyQueryIndex)
426 .As<v8::Value>().As<v8::External>()->Value()));
427 callback(property, cbinfo);
428}
429
430typedef void (*NativePropertyQuery)
431 (v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Integer> &);
432#endif
433
434static
435void IndexGetterCallbackWrapper(
436 uint32_t index, const v8::PropertyCallbackInfo<v8::Value> &info) {
437 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
438 PropertyCallbackInfo<v8::Value>
439 cbinfo(info, obj->GetInternalField(kDataIndex).As<v8::Value>());
440 IndexGetterCallback callback = reinterpret_cast<IndexGetterCallback>(
441 reinterpret_cast<intptr_t>(
442 obj->GetInternalField(kIndexPropertyGetterIndex)
443 .As<v8::Value>().As<v8::External>()->Value()));
444 callback(index, cbinfo);
445}
446
447typedef void (*NativeIndexGetter)
448 (uint32_t, const v8::PropertyCallbackInfo<v8::Value> &);
449
450static
451void IndexSetterCallbackWrapper(
452 uint32_t index
453 , v8::Local<v8::Value> value
454 , const v8::PropertyCallbackInfo<v8::Value> &info) {
455 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
456 PropertyCallbackInfo<v8::Value>
457 cbinfo(info, obj->GetInternalField(kDataIndex).As<v8::Value>());
458 IndexSetterCallback callback = reinterpret_cast<IndexSetterCallback>(
459 reinterpret_cast<intptr_t>(
460 obj->GetInternalField(kIndexPropertySetterIndex)
461 .As<v8::Value>().As<v8::External>()->Value()));
462 callback(index, value, cbinfo);
463}
464
465typedef void (*NativeIndexSetter)(
466 uint32_t
467 , v8::Local<v8::Value>
468 , const v8::PropertyCallbackInfo<v8::Value> &);
469
470static
471void IndexEnumeratorCallbackWrapper(
472 const v8::PropertyCallbackInfo<v8::Array> &info) {
473 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
474 PropertyCallbackInfo<v8::Array>
475 cbinfo(info, obj->GetInternalField(kDataIndex).As<v8::Value>());
476 IndexEnumeratorCallback callback = reinterpret_cast<IndexEnumeratorCallback>(
477 reinterpret_cast<intptr_t>(
478 obj->GetInternalField(
479 kIndexPropertyEnumeratorIndex)
480 .As<v8::Value>().As<v8::External>()->Value()));
481 callback(cbinfo);
482}
483
484typedef void (*NativeIndexEnumerator)
485 (const v8::PropertyCallbackInfo<v8::Array> &);
486
487static
488void IndexDeleterCallbackWrapper(
489 uint32_t index, const v8::PropertyCallbackInfo<v8::Boolean> &info) {
490 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
491 PropertyCallbackInfo<v8::Boolean>
492 cbinfo(info, obj->GetInternalField(kDataIndex).As<v8::Value>());
493 IndexDeleterCallback callback = reinterpret_cast<IndexDeleterCallback>(
494 reinterpret_cast<intptr_t>(
495 obj->GetInternalField(kIndexPropertyDeleterIndex)
496 .As<v8::Value>().As<v8::External>()->Value()));
497 callback(index, cbinfo);
498}
499
500typedef void (*NativeIndexDeleter)
501 (uint32_t, const v8::PropertyCallbackInfo<v8::Boolean> &);
502
503static
504void IndexQueryCallbackWrapper(
505 uint32_t index, const v8::PropertyCallbackInfo<v8::Integer> &info) {
506 v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
507 PropertyCallbackInfo<v8::Integer>
508 cbinfo(info, obj->GetInternalField(kDataIndex).As<v8::Value>());
509 IndexQueryCallback callback = reinterpret_cast<IndexQueryCallback>(
510 reinterpret_cast<intptr_t>(
511 obj->GetInternalField(kIndexPropertyQueryIndex)
512 .As<v8::Value>().As<v8::External>()->Value()));
513 callback(index, cbinfo);
514}
515
516typedef void (*NativeIndexQuery)
517 (uint32_t, const v8::PropertyCallbackInfo<v8::Integer> &);
518} // end of namespace imp
519
520#endif // NAN_CALLBACKS_12_INL_H_