aboutsummaryrefslogtreecommitdiffstats
path: root/src/bindings/eina_js/eina_js_compatibility.hh
blob: 75ada9db6a6d2ec5e31e88b6a4ea578076747261 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#ifndef EFL_EINA_JS_COMPATIBILITY_HH
#define EFL_EINA_JS_COMPATIBILITY_HH

#include <type_traits>

#include EINA_STRINGIZE(V8_INCLUDE_HEADER)

namespace v8 {

template <typename T>
struct FunctionCallbackInfo;
template <typename T>
struct PropertyCallbackInfo;
template <typename T>
struct UniquePersistent;

class AccessorInfo;

class Arguments;
  
}

namespace efl { namespace eina { namespace js {

template <typename T = v8::External, typename Enable = void>
struct _libv8_isolate_test;

template <typename T>
struct _libv8_isolate_test
<T, typename std::enable_if
 <std::is_same<decltype( & T::New)
               , v8::Local<T> (*)(v8::Isolate*, void*)>::value>::type>
  : std::true_type
{
};

template <typename T>
struct _libv8_isolate_test
<T, typename std::enable_if
 <std::is_same<decltype( & T::New)
               , v8::Local<T> (*)(void*)>::value>::type>
  : std::false_type
{
};

template <typename T = v8::FunctionTemplate, typename Enable = void>
struct _libv8_callback_info_test;

typedef v8::Handle<v8::Value>(*_libv8_invocation_callback)(v8::Arguments const&);
      
template <typename T>
struct _libv8_callback_info_test
<T, typename std::enable_if
 <!std::is_same<decltype( & T::SetCallHandler)
   , void (T::*)(_libv8_invocation_callback, v8::Handle<v8::Value>)>::value>::type>
  : std::true_type
{
};

template <typename T>
struct _libv8_callback_info_test
<T, typename std::enable_if
 <std::is_same<decltype( & T::SetCallHandler)
   , void (T::*)(_libv8_invocation_callback, v8::Handle<v8::Value>)>::value>::type>
  : std::false_type
{
};

template <typename T = v8::ObjectTemplate, typename Enable = void>
struct _libv8_property_callback_info_test
  : std::true_type {};

typedef v8::Handle<v8::Value>(*_libv8_getter_callback)(v8::Local<v8::String>, v8::AccessorInfo const&);
typedef void(*_libv8_setter_callback)(v8::Local<v8::String>, v8::Local<v8::Value>, v8::AccessorInfo const&);

template <typename T>
struct _libv8_property_callback_info_test
<T, typename std::enable_if
 <std::is_same<decltype( & T::SetAccessor)
   , void (T::*)
     (v8::Handle<v8::String>
      , _libv8_getter_callback
      , _libv8_setter_callback
      , v8::Handle<v8::Value>
      , v8::AccessControl
      , v8::PropertyAttribute
      , v8::Handle<v8::AccessorSignature>
      )>::value>::type>
  : std::false_type
{
};

static constexpr bool const v8_uses_isolate = _libv8_isolate_test<>::value;
static constexpr bool const v8_uses_callback_info = _libv8_callback_info_test<>::value;
static constexpr bool const v8_uses_property_callback_info = _libv8_property_callback_info_test<>::value;

using compatibility_return_type = std::conditional<v8_uses_callback_info, void, v8::Handle<v8::Value> >::type;
using compatibility_callback_info_type
 = std::conditional<v8_uses_callback_info, v8::FunctionCallbackInfo<v8::Value> const&, v8::Arguments const&>
  ::type;
using compatibility_callback_info_pointer
 = std::conditional<v8_uses_callback_info, v8::FunctionCallbackInfo<v8::Value> const*, v8::Arguments const*>
  ::type;

typedef compatibility_return_type(*compatibility_function_callback)(compatibility_callback_info_type);
      
using compatibility_accessor_getter_return_type
  = std::conditional<v8_uses_property_callback_info, void, v8::Handle<v8::Value> >::type;
using compatibility_accessor_callback_info_type
 = std::conditional<v8_uses_property_callback_info
                    , v8::PropertyCallbackInfo<v8::Value> const&, v8::AccessorInfo const&>
  ::type;
      
static_assert(v8_uses_property_callback_info == v8_uses_callback_info
              && v8_uses_callback_info == v8_uses_isolate, "");

template <typename T>
struct compatibility_type_tag {};

template <bool = v8_uses_isolate>
struct compatibility_string;

template <>
struct compatibility_string<true> : v8::String
{
  template <typename... Args>
  static v8::Local<v8::String> New(Args...args)
  {
    return NewFromUtf8(v8::Isolate::GetCurrent(), args...);
  }
};

template <>
struct compatibility_string<false> : v8::String
{
};

template <typename...Args>
auto compatibility_new_impl(v8::Isolate*, std::true_type, compatibility_type_tag<v8::String>
                            , Args...args) ->
  decltype(compatibility_string<>::New(args...)) 
{
  return compatibility_string<>::New(args...);
}

template <typename...Args>
auto compatibility_new_impl(v8::Isolate*, std::false_type, compatibility_type_tag<v8::String>
                            , Args...args) ->
  decltype(compatibility_string<>::New(args...)) 
{
  return compatibility_string<>::New(args...);
}

template <typename...Args>
auto compatibility_new_impl(std::nullptr_t, std::true_type, compatibility_type_tag<v8::String>
                            , Args...args) ->
  decltype(compatibility_string<>::New(args...))
{
  return compatibility_string<>::New(args...);
}

template <typename...Args>
auto compatibility_new_impl(std::nullptr_t, std::false_type, compatibility_type_tag<v8::String>
                            , Args...args) ->
  decltype(compatibility_string<>::New(args...)) 
{
  return compatibility_string<>::New(args...);
}
      
template <typename T, typename...Args>
auto compatibility_new_impl(v8::Isolate* isolate, std::true_type, compatibility_type_tag<T>
                            , Args...args) ->
  decltype(T::New(isolate, args...)) 
{
  return T::New(isolate, args...);
}

template <typename T, typename...Args>
auto compatibility_new_impl(v8::Isolate*, std::false_type, compatibility_type_tag<T>
                            , Args...args) ->
  decltype(T::New(args...)) 
{
  return T::New(args...);
}

template <typename T, typename...Args>
auto compatibility_new_impl(std::nullptr_t, std::true_type, compatibility_type_tag<T>
                            , Args...args) ->
  decltype(T::New(v8::Isolate::GetCurrent(), args...))
{
  return T::New(v8::Isolate::GetCurrent(), args...);
}

template <typename T, typename...Args>
auto compatibility_new_impl(std::nullptr_t, std::false_type, compatibility_type_tag<T>
                            , Args...args) ->
  decltype(T::New(args...)) 
{
  return T::New(args...);
}

template <typename T, typename...Args>
auto compatibility_new(v8::Isolate* isolate, Args...args) ->
  decltype(js::compatibility_new_impl<>
           (isolate, std::integral_constant<bool, v8_uses_isolate>()
            , compatibility_type_tag<T>()
            , args...))
{
  return js::compatibility_new_impl(isolate, std::integral_constant<bool, v8_uses_isolate>()
                                    , compatibility_type_tag<T>()
                                    , args...);
}

template <typename T, typename...Args>
auto compatibility_new(nullptr_t, Args...args) ->
  decltype(js::compatibility_new_impl<>(nullptr, std::integral_constant<bool, v8_uses_isolate>()
                                        , compatibility_type_tag<T>()
                                        , args...))
{
  return js::compatibility_new_impl<>(nullptr, std::integral_constant<bool, v8_uses_isolate>()
                                      , compatibility_type_tag<T>()
                                      , args...);
}

template <typename T, typename U>
inline void compatibility_return_impl(T object, U const& info, std::true_type)
{
  info.GetReturnValue().Set(object);
}

template <typename T>
inline v8::Handle<v8::Value>
compatibility_return_impl(T object, compatibility_callback_info_type, std::false_type)
{
  return object;
}

template <typename T>
inline v8::Handle<v8::Value>
compatibility_return_impl(T object, compatibility_accessor_callback_info_type, std::false_type)
{
  return object;
}
      
template <typename T>
compatibility_return_type
compatibility_return(T object, compatibility_callback_info_type args)
{
  return compatibility_return_impl(object, args, std::integral_constant<bool, v8_uses_callback_info>());
}

template <typename T>
compatibility_return_type
compatibility_return(T object, compatibility_accessor_callback_info_type args)
{
  return compatibility_return_impl(object, args, std::integral_constant<bool, v8_uses_property_callback_info>());
}
      
inline void compatibility_return_nil_impl(std::true_type) {}

inline v8::Handle<v8::Value>
compatibility_return_nil_impl(std::false_type)
{
  return v8::Handle<v8::Value>();
}

inline
compatibility_return_type
compatibility_return()
{
  return compatibility_return_nil_impl(std::integral_constant<bool, v8_uses_callback_info>());
}

struct _v8_isolate_throw_exception : v8::Isolate
{
  v8::Handle<v8::Value> ThrowException_impl(v8::Handle<v8::Value> v)
  {
    using namespace v8;
    return ThrowException(v);
  }
};

inline void
compatibility_throw_impl(v8::Isolate* isolate, v8::Local<v8::Value> exception, std::true_type)
{
  static_cast<_v8_isolate_throw_exception*>(isolate)->ThrowException_impl(exception);
}

inline v8::Handle<v8::Value>
compatibility_throw_impl(v8::Isolate* isolate, v8::Local<v8::Value> exception, std::false_type)
{
  return static_cast<_v8_isolate_throw_exception*>(isolate)->ThrowException_impl(exception);
}

inline std::conditional<v8_uses_isolate, void, v8::Handle<v8::Value> >::type
compatibility_throw(v8::Isolate* isolate, v8::Local<v8::Value> exception)
{
  return compatibility_throw_impl(isolate, exception, std::integral_constant<bool, v8_uses_isolate>());
}

inline void
compatibility_throw_impl(v8::Local<v8::Value> exception, std::true_type)
{
  static_cast<_v8_isolate_throw_exception*>(v8::Isolate::GetCurrent())->ThrowException_impl(exception);
}

inline v8::Handle<v8::Value>
compatibility_throw_impl(v8::Local<v8::Value> exception, std::false_type)
{
  return static_cast<_v8_isolate_throw_exception*>(v8::Isolate::GetCurrent())->ThrowException_impl(exception);
}
      
inline std::conditional<v8_uses_isolate, void, v8::Handle<v8::Value> >::type
compatibility_throw(v8::Local<v8::Value> exception)
{
  return compatibility_throw_impl(exception, std::integral_constant<bool, v8_uses_isolate>());
}

template <typename T, bool = v8_uses_isolate>
struct compatibility_persistent;

template <typename T>
struct compatibility_persistent<T, true> : v8::UniquePersistent<T>
{
  typedef v8::UniquePersistent<T> _base;

  compatibility_persistent(compatibility_persistent&& other)
    : _base(other.Pass())
  {
  }
  compatibility_persistent& operator=(compatibility_persistent&& other)
  {
    this->_base::operator=(other.Pass());
    return *this;
  }
  
  compatibility_persistent() {}
  compatibility_persistent(v8::Isolate* isolate, v8::Handle<T> v)
    : _base(isolate, v)
  {
  }
  
  T* operator->() const
  {
    return *handle();
  }

  v8::Handle<T> handle() const { return v8::Local<T>::New(v8::Isolate::GetCurrent(), *this); }
};

template <typename T>
struct compatibility_persistent<T, false> : v8::Persistent<T>
{
  typedef v8::Persistent<T> _base;

  compatibility_persistent() {}
  compatibility_persistent(v8::Isolate*, v8::Handle<T> v)
    : _base(v)
  {
  }

  v8::Handle<T>& handle() { return *this; }
  v8::Handle<T> const& handle() const { return *this; }
};
      
template <typename T = std::integral_constant<bool, v8_uses_isolate> >
struct _v8_object_internal_field;

template <>
struct _v8_object_internal_field<std::true_type> : v8::Object
{
};

inline void* GetPointerFromInternalField(int) { return nullptr; }
inline void SetPointerInInternalField(int, void*) {}
      
template <>
struct _v8_object_internal_field<std::false_type> : v8::Object
{
  void* GetAlignedPointerFromInternalField(int index)
  {
    return GetPointerFromInternalField(index);
  }
  void SetAlignedPointerInInternalField(int index, void* p)
  {
    SetPointerInInternalField(index, p);
  }
};
      
template <typename T = void*>
inline T compatibility_get_pointer_internal_field(v8::Handle<v8::Object> object, std::size_t index)
{
  return reinterpret_cast<T>
    (static_cast<_v8_object_internal_field<>*>(*object)->GetAlignedPointerFromInternalField(index));
}

template <typename T>
inline void compatibility_set_pointer_internal_field(v8::Handle<v8::Object> object, std::size_t index
                                                     , T* pointer)
{
  static_cast<_v8_object_internal_field<>*>(*object)->SetAlignedPointerInInternalField(index, pointer);
}
      
template <typename T = void, bool = v8_uses_isolate>
struct compatibility_handle_scope_impl;

template <typename T>
struct compatibility_handle_scope_impl<T, true> : v8::HandleScope
{
  compatibility_handle_scope_impl(v8::Isolate* isolate)
    : HandleScope(isolate)
  {}
};

template <typename T>
struct compatibility_handle_scope_impl<T, false> : v8::HandleScope
{
  compatibility_handle_scope_impl(v8::Isolate*)
  {}
};

using compatibility_handle_scope = compatibility_handle_scope_impl<>;

template <bool = v8_uses_isolate>
struct _v8_initialize_icu;

template <>
struct _v8_initialize_icu<true> : v8::V8
{
};

template <>
struct _v8_initialize_icu<false> : v8::V8
{
  static bool InitializeICU(const char* = NULL)
  {
    return true;
  }
};
      
inline void compatibility_initialize()
{
  v8::V8::Initialize();
  static_cast<_v8_initialize_icu<>*>(nullptr)->InitializeICU();
}

template <typename T, typename U>
v8::Local<T> compatibility_cast(v8::Local<U> v)
{
  return *static_cast<v8::Local<T>*>(static_cast<void*>(&v));
}

template <typename T, typename U>
v8::Local<T> compatibility_cast(U* v)
{
  return *static_cast<v8::Local<T>*>(static_cast<void*>(v));
}

template <typename T = v8::Isolate, bool = v8_uses_isolate>
struct _v8_get_current_context;

template <typename T>
struct _v8_get_current_context<T, false> : v8::Context
{
};

template <typename T>
struct _v8_get_current_context<T, true> : T
{
  static v8::Local<v8::Context> GetCurrent()
  {
    return T::GetCurrent()->GetCurrentContext();
  }
};
      
inline v8::Local<v8::Object> compatibility_global()
{
  return _v8_get_current_context<>::GetCurrent()->Global();
}

template<class T = v8::StackTrace>
typename std::enable_if<!v8_uses_isolate, v8::Local<T>>::type
compatibility_current_stack_trace(v8::Isolate*, int frame_limit,
				  v8::StackTrace::StackTraceOptions options)
{
  return T::CurrentStackTrace(frame_limit, options);
}

template<class T = v8::StackTrace>
typename std::enable_if<v8_uses_isolate, v8::Local<T>>::type
compatibility_current_stack_trace(v8::Isolate *isolate, int frame_limit,
				  v8::StackTrace::StackTraceOptions options)
{
  return T::CurrentStackTrace(isolate, frame_limit, options);
}

} } }

#endif