forked from Tencent/ScriptX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNative.hpp
1028 lines (877 loc) · 36.3 KB
/
Native.hpp
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Tencent is pleased to support the open source community by making ScriptX available.
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <cassert>
#include <cstdint>
#include <optional>
#include <sstream>
#include <tuple>
#include <type_traits>
#include "Native.h"
#include "NativeConverter.hpp"
#include "Reference.h"
#include "Scope.h"
#include "Utils.h"
#include "types.h"
// script::internal are implementation details, not public API
namespace script::internal {
namespace traits {
template <typename Args>
struct TupleTrait;
template <typename HeadT, typename... TailT>
struct TupleTrait<std::tuple<HeadT, TailT...>> {
using Head = HeadT;
using Tail = std::tuple<TailT...>;
static constexpr size_t count = 1 + sizeof...(TailT);
template <size_t i>
using Arg = decltype(std::get<i>(std::declval<std::tuple<HeadT, TailT...>>()));
};
template <>
struct TupleTrait<std::tuple<>> {
using Tail = std::tuple<>;
static constexpr size_t count = 0;
};
template <typename Func, typename Enable = void>
struct FunctionTrait;
template <typename Ret, typename... Args>
struct FunctionTrait<Ret (*)(Args...)> {
using ReturnType = Ret;
using Arguments = std::tuple<Args...>;
};
template <typename C, typename Ret, typename... Args>
struct FunctionTrait<Ret (C::*)(Args...)> : FunctionTrait<Ret (*)(C*, Args...)> {};
template <typename C, typename Ret, typename... Args>
struct FunctionTrait<Ret (C::*)(Args...) const> : FunctionTrait<Ret (*)(C*, Args...)> {};
template <typename C, typename Ret, typename... Args>
struct FunctionTrait<Ret (C::*)(Args...) volatile> : FunctionTrait<Ret (*)(C*, Args...)> {};
template <typename C, typename Ret, typename... Args>
struct FunctionTrait<Ret (C::*)(Args...) const volatile> : FunctionTrait<Ret (*)(C*, Args...)> {};
// functor and lambda
template <typename Functor>
struct FunctionTrait<Functor, std::void_t<decltype(&Functor::operator())>> {
private:
using FunctorTrait = FunctionTrait<decltype(&Functor::operator())>;
public:
using ReturnType = typename FunctorTrait::ReturnType;
using Arguments = typename TupleTrait<typename FunctorTrait::Arguments>::Tail;
};
// decay: remove const, reference; function type to function pointer
template <typename Func>
struct FunctionTrait<Func, std::enable_if_t<!std::is_same_v<Func, std::decay_t<Func>>>>
: FunctionTrait<std::decay_t<Func>> {};
} // namespace traits
template <typename T>
using FuncTrait = traits::FunctionTrait<T>;
template <typename T>
using ArgsTrait = traits::TupleTrait<typename FuncTrait<T>::Arguments>;
// SFINAE helper
template <typename T, typename...>
using type_t = T;
template <typename T, typename Enable = void>
struct TypeHolder {
explicit TypeHolder(Local<Value> ref) : ref_(std::move(ref)) {}
template <typename CppType>
decltype(auto) toCpp() {
return TypeConverter<CppType>::toCpp(ref_);
}
private:
Local<Value> ref_;
};
template <typename T>
struct TypeHolder<T, std::enable_if_t<StringLikeConceptCondition(T)>> {
explicit TypeHolder(const Local<Value>& str) : stringHolder_(str.asString()) {}
template <typename CppType>
decltype(auto) toCpp() {
return TypeConverter<CppType>::toCpp(stringHolder_);
}
private:
StringHolder stringHolder_;
};
template <typename T, typename = void>
struct IsArgsConvertibleHelper : std::false_type {};
template <typename... Args>
struct IsArgsConvertibleHelper<
std::tuple<Args...>, std::enable_if_t<std::conjunction_v<IsConvertibleHelper<Args>...>, void>>
: std::true_type {};
template <typename T>
constexpr bool isArgsConvertible = IsArgsConvertibleHelper<T>::value;
inline Local<Value> handleException(const Exception& e, bool nothrow) {
if (!nothrow) throw e;
#ifndef NDEBUG
Logger() << e;
#endif
return {};
}
class OverloadInvalidArguments : public std::exception {};
template <typename>
struct ConvertingFuncCallHelper {};
template <typename Ret, typename... Args>
struct ConvertingFuncCallHelper<std::pair<Ret, std::tuple<Args...>>> {
private:
static constexpr auto ArgsLength = sizeof...(Args);
using TypeHolderTupleType = std::tuple<TypeHolder<Args>...>;
/**
* using template matching, to get an index of Args;
*/
template <typename Func, size_t... index>
static Local<Value> call(Func& func, const Arguments& args, std::index_sequence<index...>,
bool nothrow, bool throwForOverload) {
std::optional<TypeHolderTupleType> typeHolders;
std::optional<typename std::tuple<typename ConverterDecay<Args>::type...>> cppArgs;
// notice: avoid using std::optional::value, iOS support that only on 12+
// using std::optional::operator* instead
try {
if (args.size() != sizeof...(Args)) {
// fail fast
if (nothrow) return {};
std::ostringstream msg;
msg << "Argument count mismatch, expect:" << (sizeof...(Args)) << " got:" << args.size();
throw Exception(msg.str());
}
typeHolders.emplace(args[index]...);
cppArgs.emplace(std::get<index>(*typeHolders).template toCpp<Args>()...);
} catch (const Exception& e) {
if (!nothrow && throwForOverload) throw OverloadInvalidArguments();
return handleException(e, nothrow);
}
if constexpr (std::is_same_v<Ret, void>) {
std::apply(func, *std::move(cppArgs));
return {};
} else {
auto ret = std::apply(func, *std::move(cppArgs));
try {
return TypeConverter<Ret>::toScript(std::forward<Ret>(ret));
} catch (const Exception& e) {
return handleException(e, nothrow);
}
}
}
template <typename Func, typename Ins, size_t... index>
static Local<Value> callInstanceFunc(Func& func, Ins* ins, const Arguments& args,
std::index_sequence<index...>, bool nothrow,
bool throwForOverload) {
std::optional<TypeHolderTupleType> typeHolders;
std::optional<std::tuple<Ins*, typename ConverterDecay<Args>::type...>> cppArgs;
try {
if (args.size() != sizeof...(Args)) {
// fail fast
if (nothrow) return {};
std::ostringstream msg;
msg << "Argument count mismatch, expect:" << (sizeof...(Args)) << " got:" << args.size();
throw Exception(msg.str());
}
typeHolders.emplace(args[index]...);
cppArgs.emplace(ins, std::get<index>(*typeHolders).template toCpp<Args>()...);
} catch (const Exception& e) {
if (!nothrow && throwForOverload) throw OverloadInvalidArguments();
return handleException(e, nothrow);
}
if constexpr (std::is_same_v<Ret, void>) {
std::apply(func, *std::move(cppArgs));
return {};
} else {
auto ret = std::apply(func, *std::move(cppArgs));
try {
return TypeConverter<Ret>::toScript(std::forward<Ret>(ret));
} catch (const Exception& e) {
return handleException(e, nothrow);
}
}
}
public:
template <typename Func>
static Local<Value> call(Func& func, const Arguments& args, bool nothrow, bool throwForOverload) {
return call(func, args, std::make_index_sequence<ArgsLength>(), nothrow, throwForOverload);
}
template <typename Func, typename Ins>
static Local<Value> callInstanceFunc(Func& func, Ins* ins, const Arguments& args, bool nothrow,
bool throwForOverload) {
return callInstanceFunc(func, ins, args, std::make_index_sequence<ArgsLength>(), nothrow,
throwForOverload);
}
};
template <typename T, typename = void>
struct ClassConstructorHelper : std::false_type {};
template <typename T, typename Arg, typename = void>
struct IsScriptClassConstructible : public std::false_type {};
// we don't use std::is_constructible_v here, because it also implies to be destructible
// in our case, the destructible can be protected/private, and we want T be made pointer.
// so just use `new T` syntax to check on it.
template <typename T, typename Arg>
struct IsScriptClassConstructible<T, Arg, std::void_t<decltype(new T(std::declval<Arg>()))>>
: public std::true_type {};
template <typename T>
struct ClassConstructorHelper<T,
std::enable_if_t<IsScriptClassConstructible<T, Local<Object>>::value>>
: std::true_type {
static InstanceConstructor<T> ctor() {
return [](const Arguments& args) -> T* { return new T(args.thiz()); };
}
};
template <typename T>
struct ClassConstructorHelper<
T, std::enable_if_t<IsScriptClassConstructible<T, Arguments>::value
// add inverse check, to avoid both two specialize can fit
&& !IsScriptClassConstructible<T, Local<Object>>::value>> : std::true_type {
static InstanceConstructor<T> ctor() {
return [](const Arguments& args) -> T* { return new T(args); };
}
};
// bind static function
template <typename Func>
std::enable_if_t<::script::converter::isConvertible<typename FuncTrait<Func>::ReturnType> &&
isArgsConvertible<typename FuncTrait<Func>::Arguments>,
FunctionCallback>
bindStaticFunc(Func&& func, bool nothrow, bool throwForOverload = false) {
return [f = std::forward<Func>(func), nothrow,
throwForOverload](const Arguments& args) -> Local<Value> {
using Helper = ConvertingFuncCallHelper<
std::pair<typename FuncTrait<Func>::ReturnType, typename FuncTrait<Func>::Arguments>>;
return Helper::call(f, args, nothrow, throwForOverload);
};
}
// plain overload
inline FunctionCallback bindStaticFunc(FunctionCallback&& func, bool, bool = false) {
return std::move(func);
}
inline FunctionCallback bindStaticFunc(const FunctionCallback& func, bool, bool = false) {
return func;
}
template <typename... Func>
FunctionCallback adaptOverLoadedFunction(Func&&... functions) {
std::vector funcs{bindStaticFunc(std::forward<Func>(functions), false, true)...};
return [overload = std::move(funcs)](const Arguments& args) -> Local<Value> {
for (size_t i = 0; i < sizeof...(Func); ++i) {
try {
return std::invoke(overload[i], args);
} catch (const OverloadInvalidArguments&) {
if (i == sizeof...(Func) - 1) {
throw Exception("no valid overloaded function chosen");
}
}
}
return {};
};
}
// using strict type-check to have better SFINA support
template <typename Func>
std::enable_if_t<::script::converter::isConvertible<typename FuncTrait<Func>::ReturnType> &&
ArgsTrait<Func>::count == 0,
GetterCallback>
bindStaticGet(Func&& get, bool nothrow) {
return [g = std::forward<Func>(get), nothrow]() -> Local<Value> {
using Type = typename FuncTrait<Func>::ReturnType;
auto&& ret = std::invoke(g);
try {
return TypeConverter<Type>::toScript(ret);
} catch (Exception& e) {
return handleException(e, nothrow);
}
};
}
// type-helper
inline GetterCallback bindStaticGet(GetterCallback&& g, bool) { return std::move(g); }
inline GetterCallback bindStaticGet(const GetterCallback& g, bool) { return g; }
template <typename Func>
// using strict type-check to have better SFINA support
std::enable_if_t<std::is_same_v<typename FuncTrait<Func>::ReturnType, void> &&
ArgsTrait<Func>::count == 1 &&
isArgsConvertible<typename FuncTrait<Func>::Arguments>,
SetterCallback>
bindStaticSet(Func&& set, bool nothrow) {
return [s = std::forward<Func>(set), nothrow](const Local<Value>& value) {
using Type = typename ConverterDecay<typename ArgsTrait<Func>::Head>::type;
std::optional<TypeHolder<Type>> typeHolder;
std::optional<Type> arg;
try {
typeHolder.emplace(value);
arg = typeHolder->template toCpp<Type>();
} catch (Exception& e) {
handleException(e, nothrow);
}
std::invoke(s, *std::move(arg));
};
}
inline SetterCallback bindStaticSet(SetterCallback&& s, bool) { return std::move(s); }
inline SetterCallback bindStaticSet(const SetterCallback& s, bool) { return s; }
template <typename T>
std::enable_if_t<::script::converter::isConvertible<T>, std::pair<GetterCallback, SetterCallback>>
bindStaticProp(T* prop, bool nothrow) {
if constexpr (!std::is_const_v<T>) {
return {bindStaticGet([prop]() -> T { return *prop; }, nothrow),
bindStaticSet([prop](T val) { *prop = val; }, nothrow)};
} else {
return {bindStaticGet([prop]() -> T { return *prop; }, nothrow), nullptr};
}
}
template <typename Class, typename Func>
std::enable_if_t<::script::converter::isConvertible<typename FuncTrait<Func>::ReturnType> &&
// if Arg<0> is base class of Class
std::is_convertible_v<Class*, typename ArgsTrait<Func>::template Arg<0>> &&
isArgsConvertible<typename ArgsTrait<Func>::Tail>,
InstanceFunctionCallback<Class>>
bindInstanceFunc(Func&& func, bool nothrow, bool throwForOverload = false) {
return
[f = std::forward<Func>(func), nothrow, throwForOverload](Class* ins, const Arguments& args) {
using Helper = ConvertingFuncCallHelper<
std::pair<typename ConverterDecay<typename FuncTrait<Func>::ReturnType>::type,
typename ArgsTrait<Func>::Tail>>;
return Helper::callInstanceFunc(f, ins, args, nothrow, throwForOverload);
};
}
template <typename Class>
InstanceFunctionCallback<Class> bindInstanceFunc(InstanceFunctionCallback<Class>&& func, bool,
bool = false) {
return std::move(func);
}
template <typename Class>
InstanceFunctionCallback<Class> bindInstanceFunc(const InstanceFunctionCallback<Class>& func, bool,
bool = false) {
return func;
}
template <typename Class, typename... Func>
InstanceFunctionCallback<Class> adaptOverloadedInstanceFunction(Func&&... functions) {
std::vector funcs{bindInstanceFunc<Class>(std::forward<Func>(functions), false, true)...};
return [overload = std::move(funcs)](Class* thiz, const Arguments& args) -> Local<Value> {
for (size_t i = 0; i < sizeof...(Func); ++i) {
try {
return std::invoke(overload[i], thiz, args);
} catch (const OverloadInvalidArguments&) {
if (i == sizeof...(Func) - 1) {
throw Exception("no valid overloaded function chosen");
}
}
}
return {};
};
}
template <typename Class, typename Func>
std::enable_if_t<::script::converter::isConvertible<typename FuncTrait<Func>::ReturnType> &&
ArgsTrait<Func>::count == 1 &&
// if Arg<0> is base class of C
std::is_convertible_v<Class*, typename ArgsTrait<Func>::template Arg<0>>,
InstanceGetterCallback<Class>>
bindInstanceGet(Func&& get, bool nothrow) {
return [g = std::forward<Func>(get), nothrow](Class* thiz) -> Local<Value> {
using Type = typename FuncTrait<Func>::ReturnType;
try {
return TypeConverter<Type>::toScript(std::invoke(g, thiz));
} catch (Exception& e) {
return handleException(e, nothrow);
}
};
}
template <typename C>
InstanceGetterCallback<C> bindInstanceGet(InstanceGetterCallback<C>&& g, bool) {
return std::move(g);
}
template <typename C>
InstanceGetterCallback<C> bindInstanceGet(const InstanceGetterCallback<C>& g, bool) {
return g;
}
template <typename Class, typename Func>
std::enable_if_t<std::is_same_v<typename FuncTrait<Func>::ReturnType, void> &&
ArgsTrait<Func>::count == 2 &&
// if Arg<0> is base class of C
std::is_convertible_v<Class*, typename ArgsTrait<Func>::template Arg<0>> &&
isArgsConvertible<typename ArgsTrait<Func>::Tail>,
InstanceSetterCallback<Class>>
bindInstanceSet(Func&& get, bool nothrow) {
return [g = std::forward<Func>(get), nothrow](Class* thiz, const Local<Value>& value) -> void {
using SecondArg = typename ConverterDecay<typename ArgsTrait<Func>::template Arg<1>>::type;
std::optional<TypeHolder<SecondArg>> typeHolder;
std::optional<SecondArg> arg;
try {
typeHolder.emplace(value);
arg = typeHolder->template toCpp<SecondArg>();
} catch (Exception& e) {
handleException(e, nothrow);
}
std::invoke(g, thiz, *std::move(arg));
};
}
template <typename C>
InstanceSetterCallback<C> bindInstanceSet(InstanceSetterCallback<C>&& s, bool) {
return std::move(s);
}
template <typename C>
InstanceSetterCallback<C> bindInstanceSet(const InstanceSetterCallback<C>&& s, bool) {
return s;
}
// BaseClass maybe super type of Class or same as Class.
template <typename Class, typename BaseClass, typename T>
std::enable_if_t<std::is_convertible_v<Class*, BaseClass*>&& ::script::converter::isConvertible<T>,
std::pair<InstanceGetterCallback<Class>, InstanceSetterCallback<Class>>>
bindInstanceProp(T BaseClass::*prop, bool nothrow) {
if constexpr (!std::is_const_v<T>) {
return {
bindInstanceGet<Class>([prop](Class* thiz) -> T { return thiz->*prop; }, nothrow),
bindInstanceSet<Class>([prop](Class* thiz, T val) -> void { thiz->*prop = val; }, nothrow)};
} else {
return {bindInstanceGet<Class>([prop](Class* thiz) -> T { return thiz->*prop; }, nothrow),
nullptr};
}
}
template <typename T, typename = std::void_t<decltype(&ClassConstructorHelper<T>::ctor)>>
InstanceConstructor<T> bindConstructor() {
return ClassConstructorHelper<T>::ctor();
}
constexpr bool kBindingNoThrowDefaultValue =
#ifdef SCRIPTX_NO_EXCEPTION_ON_BIND_FUNCTION
true;
#else
false;
#endif
} // namespace script::internal
namespace script {
template <typename Func>
inline internal::type_t<
Local<Function>, std::void_t<decltype(internal::bindStaticFunc(std::declval<Func>(), false))>>
Function::newFunction(Func&& callback, bool nothrow) {
return newFunction(internal::bindStaticFunc(std::forward<Func>(callback), nothrow));
}
template <typename T>
inline internal::type_t<void, std::void_t<decltype(&internal::TypeConverter<T>::toScript)>>
Local<Object>::set(const Local<String>& key, T&& value) const {
auto val = internal::TypeConverter<T>::toScript(std::forward<T>(value));
// static_cast is crucial!!!
// force the compile chose the non-template version
// to avoid a recursive call
set(key, static_cast<const Local<Value>&>(val));
}
template <typename StringLike, typename T, typename>
inline internal::type_t<void, std::void_t<decltype(&internal::TypeConverter<T>::toScript)>>
Local<Object>::set(StringLike&& keyStringLike, T&& value) const {
auto val = internal::TypeConverter<T>::toScript(std::forward<T>(value));
set(String::newString(std::forward<StringLike>(keyStringLike)),
static_cast<const Local<Value>&>(val));
}
template <typename T>
inline internal::type_t<void, std::void_t<decltype(&internal::TypeConverter<T>::toScript)>>
Local<Array>::set(size_t index, T&& value) const {
auto val = internal::TypeConverter<T>::toScript(std::forward<T>(value));
set(index, static_cast<const Local<Value>&>(val));
}
template <typename T>
inline internal::type_t<void, std::void_t<decltype(&internal::TypeConverter<T>::toScript)>>
InternalStoreHelper::set(T&& value) const {
auto val = internal::TypeConverter<T>::toScript(std::forward<T>(value));
set(static_cast<const Local<Value>&>(val));
}
template <typename... T>
inline internal::type_t<Local<Value>,
std::void_t<decltype(&internal::TypeConverter<T>::toScript)>...>
Local<Function>::call(const Local<Value>& thiz, T&&... args) const {
return call(thiz, {internal::TypeConverter<T>::toScript(std::forward<T>(args))...});
}
template <typename... T>
inline internal::type_t<Local<Object>,
std::void_t<decltype(&internal::TypeConverter<T>::toScript)>...>
Object::newObject(const Local<Value>& type, T&&... args) {
return newObject(type, {internal::TypeConverter<T>::toScript(std::forward<T>(args))...});
}
inline Local<Object> Object::newObject(const Local<Value>& type,
const std::vector<Local<Value>>& args) {
return newObjectImpl(type, args.size(), args.data());
}
inline Local<Object> Object::newObject(const Local<Value>& type,
const std::initializer_list<Local<Value>>& args) {
return newObjectImpl(type, args.size(), args.begin());
}
inline Local<Array> Array::newArray(const std::vector<Local<Value>>& elements) {
return newArrayImpl(elements.size(), elements.data());
}
inline Local<Array> Array::newArray(const std::initializer_list<Local<Value>>& elements) {
return newArrayImpl(elements.size(), elements.begin());
}
template <typename... T>
inline internal::type_t<Local<Array>,
std::void_t<decltype(&internal::TypeConverter<T>::toScript)>...>
Array::of(T&&... args) {
return newArray({internal::TypeConverter<T>::toScript(std::forward<T>(args))...});
}
namespace internal {
#ifndef __cpp_char8_t
template <typename StringLike, StringLikeConcept(StringLike)>
std::string extractStringLike(StringLike&& str) {
return std::string{std::forward<StringLike>(str)};
}
#else
// tag dispatch
inline std::string extractStringLike(const char* str) { return str; }
inline std::string extractStringLike(std::string str) { return str; }
inline std::string extractStringLike(std::string_view str) { return std::string{str}; }
inline std::string extractStringLike(const char8_t* str) {
return reinterpret_cast<const char*>(str);
}
inline std::string extractStringLike(const std::u8string& str) {
return std::string{reinterpret_cast<const char*>(str.c_str()), str.size()};
}
inline std::string extractStringLike(std::u8string_view str) {
return std::string{reinterpret_cast<const char*>(str.data()), str.size()};
}
#endif
} // namespace internal
template <typename StringLike, typename>
Exception::Exception(StringLike&& messageStringLike)
: Exception(internal::extractStringLike(std::forward<StringLike>(messageStringLike))) {}
template <typename T>
inline internal::type_t<void, std::void_t<decltype(&internal::TypeConverter<T>::toScript)>>
ScriptEngine::set(const Local<String>& key, T&& value) {
auto val = internal::TypeConverter<T>::toScript(std::forward<T>(value));
set(key, static_cast<const Local<Value>&>(val));
}
template <typename StringLike, typename T, typename>
inline internal::type_t<void, std::void_t<decltype(&internal::TypeConverter<T>::toScript)>>
ScriptEngine::set(StringLike&& keyStringLike, T&& value) {
auto val = internal::TypeConverter<T>::toScript(std::forward<T>(value));
set(String::newString(std::forward<StringLike>(keyStringLike)),
static_cast<const Local<Value>&>(val));
}
template <typename D, typename... T>
inline internal::type_t<Local<Object>,
std::void_t<decltype(&internal::TypeConverter<T>::toScript)>...>
ScriptEngine::newNativeClass(T&&... args) {
return newNativeClass<D>({internal::TypeConverter<T>::toScript(std::forward<T>(args))...});
}
namespace internal {
template <typename T>
void validateClassDefine(const ClassDefine<T>* classDefine) {
auto throwException = [classDefine](const char* msg) {
std::string info = msg;
if (classDefine) {
info = "failed to valid class define [" + classDefine->className + "] " + msg;
}
if (EngineScope::currentEngine()) {
throw Exception(info);
} else {
throw std::runtime_error(info);
}
};
if (classDefine == nullptr) {
throwException("null class define");
}
if (classDefine->className.empty()) {
throwException("empty class name");
}
bool hasStatic =
!classDefine->staticDefine.functions.empty() || !classDefine->staticDefine.properties.empty();
bool hasInstance = static_cast<bool>(classDefine->instanceDefine.constructor) ||
!classDefine->instanceDefine.functions.empty() ||
!classDefine->instanceDefine.properties.empty();
if (!hasStatic && !hasInstance) {
throwException("both static and instance define are empty");
}
if (hasStatic) {
for (auto funcDef : classDefine->staticDefine.functions) {
if (funcDef.name.empty()) {
throwException("staticDefine.functions has no name");
}
if (funcDef.callback == nullptr) {
throwException("staticDefine.functions has no callback");
}
}
for (auto propDef : classDefine->staticDefine.properties) {
if (propDef.name.empty()) {
throwException("staticDefine.properties has no name");
}
if (propDef.getter == nullptr && propDef.setter == nullptr) {
throwException("staticDefine.functions has no getter&setter");
}
}
}
if (classDefine->instanceDefine.constructor) {
if (!std::is_base_of_v<ScriptClass, T>) {
throwException("ClassDefine with instance must have a valid type parameter");
}
for (auto funcDef : classDefine->instanceDefine.functions) {
if (funcDef.name.empty()) {
throwException("instanceDefine.functions has no name");
}
if (funcDef.callback == nullptr) {
throwException("instanceDefine.functions has no callback");
}
}
for (auto propDef : classDefine->instanceDefine.properties) {
if (propDef.name.empty()) {
throwException("instanceDefine.functions has no name");
}
if (propDef.getter == nullptr && propDef.setter == nullptr) {
throwException("instanceDefine.functions has no getter&setter");
}
}
} else {
if (!classDefine->instanceDefine.properties.empty() ||
!classDefine->instanceDefine.functions.empty()) {
throwException("instance has no constructor");
}
}
}
template <typename T>
class InstanceDefineBuilder {
template <typename...>
using sfina = ClassDefineBuilder<T>&;
ClassDefineBuilder<T>& thiz;
protected:
// instance
typename InstanceDefine<T>::Constructor constructor_{};
std::vector<typename InstanceDefine<T>::FunctionDefine> insFunctions_{};
std::vector<typename InstanceDefine<T>::PropertyDefine> insProperties_{};
explicit InstanceDefineBuilder(ClassDefineBuilder<T>& thiz) : thiz(thiz) {}
public:
ClassDefineBuilder<T>& constructor(InstanceConstructor<T> constructor) {
constructor_ = std::move(constructor);
return thiz;
}
/**
* A helper method to add constructor to class define
* if Class T has constructor in signature of
* 1. T::T(Local<Object>)
* 2. T::T(Arguments)
*/
ClassDefineBuilder<T>& constructor() {
static_assert(ClassConstructorHelper<T>::value);
constructor_ = internal::bindConstructor<T>();
return thiz;
}
/**
* A helper method to disallow construct of this Class. (It's constructor always throw exception)
*/
ClassDefineBuilder<T>& constructor(std::nullptr_t) {
constructor_ = [](const Arguments&) -> T* { return nullptr; };
return thiz;
}
ClassDefineBuilder<T>& instanceFunction(std::string name, InstanceFunctionCallback<T> func) {
insFunctions_.push_back(
typename InstanceDefine<T>::FunctionDefine{std::move(name), std::move(func), {}});
return thiz;
}
template <typename Func>
sfina<decltype(internal::bindInstanceFunc<T>(std::declval<Func>(), false))> instanceFunction(
std::string name, Func func, bool nothrow = kBindingNoThrowDefaultValue) {
insFunctions_.push_back(typename InstanceDefine<T>::FunctionDefine{
std::move(name), internal::bindInstanceFunc<T>(std::move(func), nothrow), {}});
return thiz;
}
template <typename G, typename S = InstanceSetterCallback<T>>
sfina<decltype(internal::bindInstanceGet<T>(std::declval<G>(), false)),
decltype(internal::bindInstanceSet<T>(std::declval<S>(), false))>
instanceProperty(std::string name, G&& getter, S&& setterCallback = nullptr,
bool nothrow = kBindingNoThrowDefaultValue) {
insProperties_.push_back(typename InstanceDefine<T>::PropertyDefine{
std::move(name),
internal::bindInstanceGet<T>(std::forward<G>(getter), nothrow),
internal::bindInstanceSet<T>(std::forward<S>(setterCallback), nothrow),
{}});
return thiz;
}
template <typename S>
sfina<decltype(internal::bindInstanceSet<T>(std::declval<S>(), false))> instanceProperty(
std::string name, S&& setterCallback, bool nothrow = kBindingNoThrowDefaultValue) {
insProperties_.push_back(typename InstanceDefine<T>::PropertyDefine{
std::move(name),
nullptr,
internal::bindInstanceSet<T>(std::forward<S>(setterCallback), nothrow),
{}});
return thiz;
}
template <typename P, typename BaseClass>
sfina<decltype(internal::bindInstanceProp<T, BaseClass, P>(std::declval<P BaseClass::*>(),
false))>
instanceProperty(std::string name, P BaseClass::*ptr,
bool nothrow = kBindingNoThrowDefaultValue) {
auto prop = internal::bindInstanceProp<T, BaseClass, P>(ptr, nothrow);
insProperties_.push_back(typename InstanceDefine<T>::PropertyDefine{
std::move(name), std::move(prop.first), std::move(prop.second), {}});
return thiz;
}
};
// specialize for void
template <>
class InstanceDefineBuilder<void> {
protected:
typename InstanceDefine<void>::Constructor constructor_{};
std::vector<typename InstanceDefine<void>::FunctionDefine> insFunctions_{};
std::vector<typename InstanceDefine<void>::PropertyDefine> insProperties_{};
explicit InstanceDefineBuilder(ClassDefineBuilder<void>&) {}
public:
// nothing here
};
} // namespace internal
template <typename T>
class ClassDefineBuilder : public internal::InstanceDefineBuilder<T> {
static_assert(std::is_void_v<T> || std::is_convertible_v<T*, ScriptClass*>,
"T must be subclass of ScriptClass, "
"and can be void if no instance is required.");
template <typename...>
using sfina = ClassDefineBuilder<T>&;
std::string className_{};
std::string nameSpace_{};
// static
std::vector<internal::StaticDefine::FunctionDefine> functions_{};
std::vector<internal::StaticDefine::PropertyDefine> properties{};
public:
explicit ClassDefineBuilder(std::string className)
: internal::InstanceDefineBuilder<T>(*this), className_(std::move(className)) {}
ClassDefineBuilder<T>& nameSpace(std::string nameSpace) {
nameSpace_ = std::move(nameSpace);
return *this;
}
ClassDefineBuilder<T>& function(std::string name, FunctionCallback func) {
functions_.push_back(internal::StaticDefine::FunctionDefine{
std::move(name), std::forward<FunctionCallback>(func), {}});
return *this;
}
template <typename Func>
sfina<decltype(internal::bindStaticFunc(std::declval<Func>(), false))> function(
std::string name, Func func, bool nothrow = internal::kBindingNoThrowDefaultValue) {
functions_.push_back(internal::StaticDefine::FunctionDefine{
std::move(name), internal::bindStaticFunc(std::forward<Func>(func), nothrow), {}});
return *this;
}
template <typename G, typename S = SetterCallback>
sfina<decltype(internal::bindStaticGet(std::declval<G>(), false)),
decltype(internal::bindStaticSet(std::declval<S>(), false))>
property(std::string name, G&& getter, S&& setterCallback = nullptr,
bool nothrow = internal::kBindingNoThrowDefaultValue) {
properties.push_back(internal::StaticDefine::PropertyDefine{
std::move(name),
internal::bindStaticGet(std::forward<G>(getter), nothrow),
internal::bindStaticSet(std::forward<S>(setterCallback), nothrow),
{}});
return *this;
}
template <typename S>
sfina<decltype(internal::bindStaticSet(std::declval<S>(), false))> property(std::string name,
S&& setterCallback) {
properties.push_back(internal::StaticDefine::PropertyDefine{
std::move(name), nullptr, internal::bindStaticSet(std::forward<S>(setterCallback)), {}});
return *this;
}
template <typename P>
sfina<decltype(internal::bindStaticProp(std::declval<P*>(), false))> property(
std::string name, P* ptr, bool nothrow = internal::kBindingNoThrowDefaultValue) {
auto prop = internal::bindStaticProp(ptr, nothrow);
properties.push_back(internal::StaticDefine::PropertyDefine{
std::move(name), std::move(prop.first), std::move(prop.second), {}});
return *this;
}
ClassDefine<T> build() {
using Instance = internal::InstanceDefineBuilder<T>;
// fill trace name
for (auto&& f : functions_) {
f.traceName = className_ + "::" + f.name;
}
for (auto&& p : properties) {
p.traceName = className_ + "::" + p.name;
}
for (auto&& f : Instance::insFunctions_) {
f.traceName = className_ + "::" + f.name;
}
for (auto&& p : Instance::insProperties_) {
p.traceName = className_ + "::" + p.name;
}
ClassDefine<T> define(std::move(className_), std::move(nameSpace_),
internal::StaticDefine{std::move(functions_), std::move(properties)},
internal::InstanceDefine<T>{std::move(Instance::constructor_),
std::move(Instance::insFunctions_),
std::move(Instance::insProperties_)});
::script::internal::validateClassDefine(&define);
return define;
}
};
template <typename T>
inline ClassDefineBuilder<T> defineClass(std::string name) {
return ClassDefineBuilder<T>(std::move(name));
}
class NativeRegister {
void (*registerFunc_)(const void* def, ScriptEngine* engine);
#ifdef __cpp_rtti
void (*visitFunc_)(const void* def, ClassDefineVisitor& visitor);
#endif
const void* define_;
template <typename T>
explicit NativeRegister(const ClassDefine<T>* define) : registerFunc_(nullptr), define_(define) {
registerFunc_ = [](const void* def, ScriptEngine* engine) {
engine->registerNativeClass(*static_cast<const ClassDefine<T>*>(def));
};
#ifdef __cpp_rtti
visitFunc_ = [](const void* def, ClassDefineVisitor& visitor) {
static_cast<const ClassDefine<T>*>(def)->visit(visitor);
};
#endif
}
template <typename T>
friend class ClassDefine;
public:
/**
* register the wrapped class define to engine.
* this is equivalent to call
* \code
* engine->registerNativeClass(wrappedDefine);
* \endcode
* @param engine
*/
void registerNativeClass(ScriptEngine* engine) const { registerFunc_(define_, engine); }
#ifdef __cpp_rtti
/**
* You must enable rtti feature to use this api.
* @param visitor
*/
void visit(ClassDefineVisitor& visitor) const { visitFunc_(define_, visitor); }
#endif
};
template <typename T>
inline NativeRegister ClassDefine<T>::getNativeRegister() const {
return NativeRegister(this);
}
#ifdef __cpp_rtti
template <typename T>
void script::ClassDefine<T>::visit(script::ClassDefineVisitor& visitor) const {
visitor.beginClassDefine(className, nameSpace);
for (auto&& prop : staticDefine.properties) {
visitor.visitStaticProperty(prop.name, prop.getter.target_type(), prop.setter.target_type());
}
for (auto&& function : staticDefine.functions) {
visitor.visitStaticFunction(function.name, function.callback.target_type());
}
if (instanceDefine.constructor) {
visitor.visitConstructor(instanceDefine.constructor.target_type());
}
for (auto&& prop : instanceDefine.properties) {
visitor.visitInstanceProperty(prop.name, prop.getter.target_type(), prop.setter.target_type());