Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

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