1 /*********************************************************************
2 * NAN - Native Abstractions for Node.js
4 * Copyright (c) 2018 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 #if NODE_MAJOR_VERSION < 10
109 inline v8::Local<v8::Function> Callee() const { return info_.Callee(); }
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(); }
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;
131 NAN_DISALLOW_ASSIGN_COPY_MOVE(FunctionCallbackInfo)
135 class PropertyCallbackInfo {
136 const v8::PropertyCallbackInfo<T> &info_;
137 const v8::Local<v8::Value> data_;
140 explicit inline PropertyCallbackInfo(
141 const v8::PropertyCallbackInfo<T> &info
142 , const v8::Local<v8::Value> data) :
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());
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;
164 NAN_DISALLOW_ASSIGN_COPY_MOVE(PropertyCallbackInfo)
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));
179 typedef void (*NativeFunction)(const v8::FunctionCallbackInfo<v8::Value> &);
181 #if NODE_MODULE_VERSION > NODE_0_12_MODULE_VERSION
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);
195 typedef void (*NativeGetter)
196 (v8::Local<v8::Name>, const v8::PropertyCallbackInfo<v8::Value> &);
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);
212 typedef void (*NativeSetter)(
214 , v8::Local<v8::Value>
215 , const v8::PropertyCallbackInfo<void> &);
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);
230 typedef void (*NativeGetter)
231 (v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Value> &);
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);
247 typedef void (*NativeSetter)(
248 v8::Local<v8::String>
249 , v8::Local<v8::Value>
250 , const v8::PropertyCallbackInfo<void> &);
253 #if NODE_MODULE_VERSION > NODE_0_12_MODULE_VERSION
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);
268 typedef void (*NativePropertyGetter)
269 (v8::Local<v8::Name>, const v8::PropertyCallbackInfo<v8::Value> &);
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);
286 typedef void (*NativePropertySetter)(
288 , v8::Local<v8::Value>
289 , const v8::PropertyCallbackInfo<v8::Value> &);
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()));
304 typedef void (*NativePropertyEnumerator)
305 (const v8::PropertyCallbackInfo<v8::Array> &);
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);
321 typedef void (NativePropertyDeleter)
322 (v8::Local<v8::Name>, const v8::PropertyCallbackInfo<v8::Boolean> &);
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);
338 typedef void (*NativePropertyQuery)
339 (v8::Local<v8::Name>, const v8::PropertyCallbackInfo<v8::Integer> &);
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);
355 typedef void (*NativePropertyGetter)
356 (v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Value> &);
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);
373 typedef void (*NativePropertySetter)(
374 v8::Local<v8::String>
375 , v8::Local<v8::Value>
376 , const v8::PropertyCallbackInfo<v8::Value> &);
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()));
391 typedef void (*NativePropertyEnumerator)
392 (const v8::PropertyCallbackInfo<v8::Array> &);
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);
408 typedef void (NativePropertyDeleter)
409 (v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Boolean> &);
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);
425 typedef void (*NativePropertyQuery)
426 (v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Integer> &);
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);
442 typedef void (*NativeIndexGetter)
443 (uint32_t, const v8::PropertyCallbackInfo<v8::Value> &);
446 void IndexSetterCallbackWrapper(
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);
460 typedef void (*NativeIndexSetter)(
462 , v8::Local<v8::Value>
463 , const v8::PropertyCallbackInfo<v8::Value> &);
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()));
478 typedef void (*NativeIndexEnumerator)
479 (const v8::PropertyCallbackInfo<v8::Array> &);
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);
494 typedef void (*NativeIndexDeleter)
495 (uint32_t, const v8::PropertyCallbackInfo<v8::Boolean> &);
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);
510 typedef void (*NativeIndexQuery)
511 (uint32_t, const v8::PropertyCallbackInfo<v8::Integer> &);
512 } // end of namespace imp
514 #endif // NAN_CALLBACKS_12_INL_H_