Websocket
[VSoRC/.git] / node_modules / websocket / node_modules / nan / nan_callbacks_12_inl.h
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
12 template<typename T>
13 class 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
92 template<typename T>
93 class 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
134 template<typename T>
135 class 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
167 namespace imp {
168 static
169 void 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).As<v8::External>()->Value()));
174   FunctionCallbackInfo<v8::Value>
175       cbinfo(info, obj->GetInternalField(kDataIndex));
176   callback(cbinfo);
177 }
178
179 typedef void (*NativeFunction)(const v8::FunctionCallbackInfo<v8::Value> &);
180
181 #if NODE_MODULE_VERSION > NODE_0_12_MODULE_VERSION
182 static
183 void GetterCallbackWrapper(
184     v8::Local<v8::Name> property
185   , const v8::PropertyCallbackInfo<v8::Value> &info) {
186   v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
187   PropertyCallbackInfo<v8::Value>
188       cbinfo(info, obj->GetInternalField(kDataIndex));
189   GetterCallback callback = reinterpret_cast<GetterCallback>(
190       reinterpret_cast<intptr_t>(
191           obj->GetInternalField(kGetterIndex).As<v8::External>()->Value()));
192   callback(property.As<v8::String>(), cbinfo);
193 }
194
195 typedef void (*NativeGetter)
196     (v8::Local<v8::Name>, const v8::PropertyCallbackInfo<v8::Value> &);
197
198 static
199 void SetterCallbackWrapper(
200     v8::Local<v8::Name> property
201   , v8::Local<v8::Value> value
202   , const v8::PropertyCallbackInfo<void> &info) {
203   v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
204   PropertyCallbackInfo<void>
205       cbinfo(info, obj->GetInternalField(kDataIndex));
206   SetterCallback callback = reinterpret_cast<SetterCallback>(
207       reinterpret_cast<intptr_t>(
208           obj->GetInternalField(kSetterIndex).As<v8::External>()->Value()));
209   callback(property.As<v8::String>(), value, cbinfo);
210 }
211
212 typedef void (*NativeSetter)(
213     v8::Local<v8::Name>
214   , v8::Local<v8::Value>
215   , const v8::PropertyCallbackInfo<void> &);
216 #else
217 static
218 void GetterCallbackWrapper(
219     v8::Local<v8::String> property
220   , const v8::PropertyCallbackInfo<v8::Value> &info) {
221   v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
222   PropertyCallbackInfo<v8::Value>
223       cbinfo(info, obj->GetInternalField(kDataIndex));
224   GetterCallback callback = reinterpret_cast<GetterCallback>(
225       reinterpret_cast<intptr_t>(
226           obj->GetInternalField(kGetterIndex).As<v8::External>()->Value()));
227   callback(property, cbinfo);
228 }
229
230 typedef void (*NativeGetter)
231     (v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Value> &);
232
233 static
234 void SetterCallbackWrapper(
235     v8::Local<v8::String> property
236   , v8::Local<v8::Value> value
237   , const v8::PropertyCallbackInfo<void> &info) {
238   v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
239   PropertyCallbackInfo<void>
240       cbinfo(info, obj->GetInternalField(kDataIndex));
241   SetterCallback callback = reinterpret_cast<SetterCallback>(
242       reinterpret_cast<intptr_t>(
243           obj->GetInternalField(kSetterIndex).As<v8::External>()->Value()));
244   callback(property, value, cbinfo);
245 }
246
247 typedef void (*NativeSetter)(
248     v8::Local<v8::String>
249   , v8::Local<v8::Value>
250   , const v8::PropertyCallbackInfo<void> &);
251 #endif
252
253 #if NODE_MODULE_VERSION > NODE_0_12_MODULE_VERSION
254 static
255 void PropertyGetterCallbackWrapper(
256     v8::Local<v8::Name> property
257   , const v8::PropertyCallbackInfo<v8::Value> &info) {
258   v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
259   PropertyCallbackInfo<v8::Value>
260       cbinfo(info, obj->GetInternalField(kDataIndex));
261   PropertyGetterCallback callback = reinterpret_cast<PropertyGetterCallback>(
262       reinterpret_cast<intptr_t>(
263           obj->GetInternalField(kPropertyGetterIndex)
264               .As<v8::External>()->Value()));
265   callback(property.As<v8::String>(), cbinfo);
266 }
267
268 typedef void (*NativePropertyGetter)
269     (v8::Local<v8::Name>, const v8::PropertyCallbackInfo<v8::Value> &);
270
271 static
272 void PropertySetterCallbackWrapper(
273     v8::Local<v8::Name> property
274   , v8::Local<v8::Value> value
275   , const v8::PropertyCallbackInfo<v8::Value> &info) {
276   v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
277   PropertyCallbackInfo<v8::Value>
278       cbinfo(info, obj->GetInternalField(kDataIndex));
279   PropertySetterCallback callback = reinterpret_cast<PropertySetterCallback>(
280       reinterpret_cast<intptr_t>(
281           obj->GetInternalField(kPropertySetterIndex)
282               .As<v8::External>()->Value()));
283   callback(property.As<v8::String>(), value, cbinfo);
284 }
285
286 typedef void (*NativePropertySetter)(
287     v8::Local<v8::Name>
288   , v8::Local<v8::Value>
289   , const v8::PropertyCallbackInfo<v8::Value> &);
290
291 static
292 void PropertyEnumeratorCallbackWrapper(
293     const v8::PropertyCallbackInfo<v8::Array> &info) {
294   v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
295   PropertyCallbackInfo<v8::Array>
296       cbinfo(info, obj->GetInternalField(kDataIndex));
297   PropertyEnumeratorCallback callback =
298       reinterpret_cast<PropertyEnumeratorCallback>(reinterpret_cast<intptr_t>(
299           obj->GetInternalField(kPropertyEnumeratorIndex)
300               .As<v8::External>()->Value()));
301   callback(cbinfo);
302 }
303
304 typedef void (*NativePropertyEnumerator)
305     (const v8::PropertyCallbackInfo<v8::Array> &);
306
307 static
308 void PropertyDeleterCallbackWrapper(
309     v8::Local<v8::Name> property
310   , const v8::PropertyCallbackInfo<v8::Boolean> &info) {
311   v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
312   PropertyCallbackInfo<v8::Boolean>
313       cbinfo(info, obj->GetInternalField(kDataIndex));
314   PropertyDeleterCallback callback = reinterpret_cast<PropertyDeleterCallback>(
315       reinterpret_cast<intptr_t>(
316           obj->GetInternalField(kPropertyDeleterIndex)
317               .As<v8::External>()->Value()));
318   callback(property.As<v8::String>(), cbinfo);
319 }
320
321 typedef void (NativePropertyDeleter)
322     (v8::Local<v8::Name>, const v8::PropertyCallbackInfo<v8::Boolean> &);
323
324 static
325 void PropertyQueryCallbackWrapper(
326     v8::Local<v8::Name> property
327   , const v8::PropertyCallbackInfo<v8::Integer> &info) {
328   v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
329   PropertyCallbackInfo<v8::Integer>
330       cbinfo(info, obj->GetInternalField(kDataIndex));
331   PropertyQueryCallback callback = reinterpret_cast<PropertyQueryCallback>(
332       reinterpret_cast<intptr_t>(
333           obj->GetInternalField(kPropertyQueryIndex)
334               .As<v8::External>()->Value()));
335   callback(property.As<v8::String>(), cbinfo);
336 }
337
338 typedef void (*NativePropertyQuery)
339     (v8::Local<v8::Name>, const v8::PropertyCallbackInfo<v8::Integer> &);
340 #else
341 static
342 void PropertyGetterCallbackWrapper(
343     v8::Local<v8::String> property
344   , const v8::PropertyCallbackInfo<v8::Value> &info) {
345   v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
346   PropertyCallbackInfo<v8::Value>
347       cbinfo(info, obj->GetInternalField(kDataIndex));
348   PropertyGetterCallback callback = reinterpret_cast<PropertyGetterCallback>(
349       reinterpret_cast<intptr_t>(
350           obj->GetInternalField(kPropertyGetterIndex)
351               .As<v8::External>()->Value()));
352   callback(property, cbinfo);
353 }
354
355 typedef void (*NativePropertyGetter)
356     (v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Value> &);
357
358 static
359 void PropertySetterCallbackWrapper(
360     v8::Local<v8::String> property
361   , v8::Local<v8::Value> value
362   , const v8::PropertyCallbackInfo<v8::Value> &info) {
363   v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
364   PropertyCallbackInfo<v8::Value>
365       cbinfo(info, obj->GetInternalField(kDataIndex));
366   PropertySetterCallback callback = reinterpret_cast<PropertySetterCallback>(
367       reinterpret_cast<intptr_t>(
368           obj->GetInternalField(kPropertySetterIndex)
369               .As<v8::External>()->Value()));
370   callback(property, value, cbinfo);
371 }
372
373 typedef void (*NativePropertySetter)(
374     v8::Local<v8::String>
375   , v8::Local<v8::Value>
376   , const v8::PropertyCallbackInfo<v8::Value> &);
377
378 static
379 void PropertyEnumeratorCallbackWrapper(
380     const v8::PropertyCallbackInfo<v8::Array> &info) {
381   v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
382   PropertyCallbackInfo<v8::Array>
383       cbinfo(info, obj->GetInternalField(kDataIndex));
384   PropertyEnumeratorCallback callback =
385       reinterpret_cast<PropertyEnumeratorCallback>(reinterpret_cast<intptr_t>(
386           obj->GetInternalField(kPropertyEnumeratorIndex)
387               .As<v8::External>()->Value()));
388   callback(cbinfo);
389 }
390
391 typedef void (*NativePropertyEnumerator)
392     (const v8::PropertyCallbackInfo<v8::Array> &);
393
394 static
395 void PropertyDeleterCallbackWrapper(
396     v8::Local<v8::String> property
397   , const v8::PropertyCallbackInfo<v8::Boolean> &info) {
398   v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
399   PropertyCallbackInfo<v8::Boolean>
400       cbinfo(info, obj->GetInternalField(kDataIndex));
401   PropertyDeleterCallback callback = reinterpret_cast<PropertyDeleterCallback>(
402       reinterpret_cast<intptr_t>(
403           obj->GetInternalField(kPropertyDeleterIndex)
404               .As<v8::External>()->Value()));
405   callback(property, cbinfo);
406 }
407
408 typedef void (NativePropertyDeleter)
409     (v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Boolean> &);
410
411 static
412 void PropertyQueryCallbackWrapper(
413     v8::Local<v8::String> property
414   , const v8::PropertyCallbackInfo<v8::Integer> &info) {
415   v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
416   PropertyCallbackInfo<v8::Integer>
417       cbinfo(info, obj->GetInternalField(kDataIndex));
418   PropertyQueryCallback callback = reinterpret_cast<PropertyQueryCallback>(
419       reinterpret_cast<intptr_t>(
420           obj->GetInternalField(kPropertyQueryIndex)
421               .As<v8::External>()->Value()));
422   callback(property, cbinfo);
423 }
424
425 typedef void (*NativePropertyQuery)
426     (v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Integer> &);
427 #endif
428
429 static
430 void IndexGetterCallbackWrapper(
431     uint32_t index, const v8::PropertyCallbackInfo<v8::Value> &info) {
432   v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
433   PropertyCallbackInfo<v8::Value>
434       cbinfo(info, obj->GetInternalField(kDataIndex));
435   IndexGetterCallback callback = reinterpret_cast<IndexGetterCallback>(
436       reinterpret_cast<intptr_t>(
437           obj->GetInternalField(kIndexPropertyGetterIndex)
438               .As<v8::External>()->Value()));
439   callback(index, cbinfo);
440 }
441
442 typedef void (*NativeIndexGetter)
443     (uint32_t, const v8::PropertyCallbackInfo<v8::Value> &);
444
445 static
446 void IndexSetterCallbackWrapper(
447     uint32_t index
448   , v8::Local<v8::Value> value
449   , const v8::PropertyCallbackInfo<v8::Value> &info) {
450   v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
451   PropertyCallbackInfo<v8::Value>
452       cbinfo(info, obj->GetInternalField(kDataIndex));
453   IndexSetterCallback callback = reinterpret_cast<IndexSetterCallback>(
454       reinterpret_cast<intptr_t>(
455           obj->GetInternalField(kIndexPropertySetterIndex)
456               .As<v8::External>()->Value()));
457   callback(index, value, cbinfo);
458 }
459
460 typedef void (*NativeIndexSetter)(
461     uint32_t
462   , v8::Local<v8::Value>
463   , const v8::PropertyCallbackInfo<v8::Value> &);
464
465 static
466 void IndexEnumeratorCallbackWrapper(
467     const v8::PropertyCallbackInfo<v8::Array> &info) {
468   v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
469   PropertyCallbackInfo<v8::Array>
470       cbinfo(info, obj->GetInternalField(kDataIndex));
471   IndexEnumeratorCallback callback = reinterpret_cast<IndexEnumeratorCallback>(
472       reinterpret_cast<intptr_t>(
473           obj->GetInternalField(
474               kIndexPropertyEnumeratorIndex).As<v8::External>()->Value()));
475   callback(cbinfo);
476 }
477
478 typedef void (*NativeIndexEnumerator)
479     (const v8::PropertyCallbackInfo<v8::Array> &);
480
481 static
482 void IndexDeleterCallbackWrapper(
483     uint32_t index, const v8::PropertyCallbackInfo<v8::Boolean> &info) {
484   v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
485   PropertyCallbackInfo<v8::Boolean>
486       cbinfo(info, obj->GetInternalField(kDataIndex));
487   IndexDeleterCallback callback = reinterpret_cast<IndexDeleterCallback>(
488       reinterpret_cast<intptr_t>(
489           obj->GetInternalField(kIndexPropertyDeleterIndex)
490               .As<v8::External>()->Value()));
491   callback(index, cbinfo);
492 }
493
494 typedef void (*NativeIndexDeleter)
495     (uint32_t, const v8::PropertyCallbackInfo<v8::Boolean> &);
496
497 static
498 void IndexQueryCallbackWrapper(
499     uint32_t index, const v8::PropertyCallbackInfo<v8::Integer> &info) {
500   v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
501   PropertyCallbackInfo<v8::Integer>
502       cbinfo(info, obj->GetInternalField(kDataIndex));
503   IndexQueryCallback callback = reinterpret_cast<IndexQueryCallback>(
504       reinterpret_cast<intptr_t>(
505           obj->GetInternalField(kIndexPropertyQueryIndex)
506               .As<v8::External>()->Value()));
507   callback(index, cbinfo);
508 }
509
510 typedef void (*NativeIndexQuery)
511     (uint32_t, const v8::PropertyCallbackInfo<v8::Integer> &);
512 }  // end of namespace imp
513
514 #endif  // NAN_CALLBACKS_12_INL_H_