1 /*********************************************************************
2 * NAN - Native Abstractions for Node.js
4 * Copyright (c) 2017 NAN contributors
6 * MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
7 ********************************************************************/
9 #ifndef NAN_CALLBACKS_12_INL_H_
10 #define NAN_CALLBACKS_12_INL_H_
14 v8::ReturnValue<T> value_;
18 explicit inline ReturnValue(const v8::ReturnValue<S> &value) :
21 explicit inline ReturnValue(const ReturnValue<S>& that)
22 : value_(that.value_) {
27 template <typename S> inline void Set(const v8::Local<S> &handle) {
32 template <typename S> inline void Set(const Global<S> &handle) {
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))))
40 value_.Set(*reinterpret_cast<const v8::Persistent<S>*>(&handle));
41 const_cast<Global<S> &>(handle).Reset();
45 // Fast primitive setters
46 inline void Set(bool value) {
47 TYPE_CHECK(T, v8::Boolean);
51 inline void Set(double i) {
52 TYPE_CHECK(T, v8::Number);
56 inline void Set(int32_t i) {
57 TYPE_CHECK(T, v8::Integer);
61 inline void Set(uint32_t i) {
62 TYPE_CHECK(T, v8::Integer);
66 // Fast JS primitive setters
67 inline void SetNull() {
68 TYPE_CHECK(T, v8::Primitive);
72 inline void SetUndefined() {
73 TYPE_CHECK(T, v8::Primitive);
74 value_.SetUndefined();
77 inline void SetEmptyString() {
78 TYPE_CHECK(T, v8::String);
79 value_.SetEmptyString();
82 // Convenience getter for isolate
83 inline v8::Isolate *GetIsolate() const {
84 return value_.GetIsolate();
87 // Pointer setter: Uncompilable to prevent inadvertent misuse.
89 inline void Set(S *whatever) { TYPE_CHECK(S*, v8::Primitive); }
93 class FunctionCallbackInfo {
94 const v8::FunctionCallbackInfo<T> &info_;
95 const v8::Local<v8::Value> data_;
98 explicit inline FunctionCallbackInfo(
99 const v8::FunctionCallbackInfo<T> &info
100 , v8::Local<v8::Value> data) :
104 inline ReturnValue<T> GetReturnValue() const {
105 return ReturnValue<T>(info_.GetReturnValue());
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(); }
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;
129 NAN_DISALLOW_ASSIGN_COPY_MOVE(FunctionCallbackInfo)
133 class PropertyCallbackInfo {
134 const v8::PropertyCallbackInfo<T> &info_;
135 const v8::Local<v8::Value> data_;
138 explicit inline PropertyCallbackInfo(
139 const v8::PropertyCallbackInfo<T> &info
140 , const v8::Local<v8::Value> data) :
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());
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;
162 NAN_DISALLOW_ASSIGN_COPY_MOVE(PropertyCallbackInfo)
167 void 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));
177 typedef void (*NativeFunction)(const v8::FunctionCallbackInfo<v8::Value> &);
179 #if NODE_MODULE_VERSION > NODE_0_12_MODULE_VERSION
181 void 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);
193 typedef void (*NativeGetter)
194 (v8::Local<v8::Name>, const v8::PropertyCallbackInfo<v8::Value> &);
197 void 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);
210 typedef void (*NativeSetter)(
212 , v8::Local<v8::Value>
213 , const v8::PropertyCallbackInfo<void> &);
216 void 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);
228 typedef void (*NativeGetter)
229 (v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Value> &);
232 void 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);
245 typedef void (*NativeSetter)(
246 v8::Local<v8::String>
247 , v8::Local<v8::Value>
248 , const v8::PropertyCallbackInfo<void> &);
251 #if NODE_MODULE_VERSION > NODE_0_12_MODULE_VERSION
253 void 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);
266 typedef void (*NativePropertyGetter)
267 (v8::Local<v8::Name>, const v8::PropertyCallbackInfo<v8::Value> &);
270 void 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);
284 typedef void (*NativePropertySetter)(
286 , v8::Local<v8::Value>
287 , const v8::PropertyCallbackInfo<v8::Value> &);
290 void 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()));
302 typedef void (*NativePropertyEnumerator)
303 (const v8::PropertyCallbackInfo<v8::Array> &);
306 void 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);
319 typedef void (NativePropertyDeleter)
320 (v8::Local<v8::Name>, const v8::PropertyCallbackInfo<v8::Boolean> &);
323 void 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);
336 typedef void (*NativePropertyQuery)
337 (v8::Local<v8::Name>, const v8::PropertyCallbackInfo<v8::Integer> &);
340 void 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);
353 typedef void (*NativePropertyGetter)
354 (v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Value> &);
357 void 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);
371 typedef void (*NativePropertySetter)(
372 v8::Local<v8::String>
373 , v8::Local<v8::Value>
374 , const v8::PropertyCallbackInfo<v8::Value> &);
377 void 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()));
389 typedef void (*NativePropertyEnumerator)
390 (const v8::PropertyCallbackInfo<v8::Array> &);
393 void 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);
406 typedef void (NativePropertyDeleter)
407 (v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Boolean> &);
410 void 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);
423 typedef void (*NativePropertyQuery)
424 (v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Integer> &);
428 void 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);
440 typedef void (*NativeIndexGetter)
441 (uint32_t, const v8::PropertyCallbackInfo<v8::Value> &);
444 void IndexSetterCallbackWrapper(
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);
458 typedef void (*NativeIndexSetter)(
460 , v8::Local<v8::Value>
461 , const v8::PropertyCallbackInfo<v8::Value> &);
464 void 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()));
476 typedef void (*NativeIndexEnumerator)
477 (const v8::PropertyCallbackInfo<v8::Array> &);
480 void 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);
492 typedef void (*NativeIndexDeleter)
493 (uint32_t, const v8::PropertyCallbackInfo<v8::Boolean> &);
496 void 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);
508 typedef void (*NativeIndexQuery)
509 (uint32_t, const v8::PropertyCallbackInfo<v8::Integer> &);
510 } // end of namespace imp
512 #endif // NAN_CALLBACKS_12_INL_H_