-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathquickjs.hpp
More file actions
2533 lines (2177 loc) · 64.2 KB
/
quickjs.hpp
File metadata and controls
2533 lines (2177 loc) · 64.2 KB
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
/*
* quickjs-cpp
*
* Copyright (c) 2020 Thomas Bluemel <thomas@reactsoft.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef __QUICKJS__HPP
#define __QUICKJS__HPP
#include <quickjs.h>
#include <memory>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <map>
#include <string>
#include <vector>
#include <exception>
#include <functional>
#if 0
#include <iostream>
#define QJSCPP_DEBUG(stmt) \
do { \
std::cerr << "[quickjs] " __FILE__ << ':' << __LINE__ << ": " << stmt << std::endl; \
} while(0)
#else
#define QJSCPP_DEBUG(stmt)
#endif
namespace quickjs
{
class runtime;
class context;
class value;
class args;
class exception:
public std::exception
{
protected:
std::string str_;
exception() = default;
public:
template <typename T>
exception(T what):
str_(what)
{
}
virtual ~exception()
{
}
virtual const char* what() const noexcept
{
return str_.c_str();
}
};
class invalid_context:
public exception
{
public:
invalid_context():
exception("invalid context")
{
}
};
class cstring
{
friend class context;
friend class value;
JSContext* ctx_{nullptr};
const char* cstr_{nullptr};
cstring(JSContext* ctx, JSValueConst val):
ctx_(ctx),
cstr_(JS_ToCString(ctx, val))
{
}
public:
cstring(const cstring&) = delete;
cstring& operator=(const cstring&) = delete;
cstring() = default;
cstring(cstring&& from):
ctx_(from.ctx_),
cstr_(from.cstr_)
{
from.ctx_ = nullptr;
from.cstr_ = nullptr;
}
cstring& operator=(cstring&& from)
{
if (&from != this)
{
if (ctx_ && cstr_)
JS_FreeCString(ctx_, cstr_);
ctx_ = from.ctx_;
from.ctx_ = nullptr;
cstr_ = from.cstr_;
from.cstr_ = nullptr;
}
return *this;
}
~cstring()
{
if (ctx_ && cstr_)
JS_FreeCString(ctx_, cstr_);
}
operator std::string() const
{
return cstr_ ? std::string(cstr_) : std::string();
}
operator const char*() const
{
return cstr_;
}
std::string str() const
{
return cstr_ ? std::string(cstr_) : std::string();
}
const char* c_str() const
{
return cstr_;
}
operator bool() const
{
return cstr_ != nullptr;
}
};
namespace detail
{
struct jsvalue_list;
template <typename Owned>
class owner;
class list_entry
{
template <typename Owned> friend class owner;
list_entry *flink_;
list_entry *blink_;
inline void do_move(list_entry&& from)
{
auto next = from.flink_;
auto prev = from.blink_;
next->blink_ = this;
prev->flink_ = this;
flink_ = next;
blink_ = prev;
from.flink_ = &from;
from.blink_ = &from;
}
public:
list_entry():
flink_(this),
blink_(this)
{
}
~list_entry()
{
assert(!is_linked());
}
list_entry(const list_entry&) = delete;
list_entry& operator=(const list_entry&) = delete;
list_entry& operator=(list_entry&& from)
{
if (&from != this)
{
if (is_linked())
unlink();
if (from.is_linked())
do_move(std::move(from));
}
return *this;
}
list_entry(list_entry&& from)
{
if (from.is_linked())
do_move(std::move(from));
else
{
flink_ = this;
blink_ = this;
}
}
inline bool is_linked() const
{
return flink_ != this && blink_ != this;
}
inline void unlink()
{
if (is_linked())
{
auto next = flink_;
auto prev = blink_;
next->blink_ = prev;
prev->flink_ = next;
flink_ = this;
blink_ = this;
}
}
};
template <typename Owned>
class owner
{
list_entry head_;
public:
owner() = default;
owner(const owner&) = delete;
owner(owner&&) = delete;
owner& operator=(const owner&) = delete;
owner& operator=(owner&&) = delete;
~owner()
{
assert(!head_.is_linked());
}
template <typename F>
void for_each(F f)
{
for (auto current = head_.flink_; current != &head_; )
{
auto next = current->flink_;
f(static_cast<Owned*>(current));
current = next;
}
}
inline void insert_head(list_entry& entry)
{
assert(&entry != &head_);
assert(!entry.is_linked());
list_entry* next = head_.flink_;
head_.flink_ = &entry;
entry.blink_ = &head_;
entry.flink_ = next;
next->blink_ = &entry;
if (head_.blink_ == &head_)
head_.blink_ = &entry;
}
};
}
class value_error:
public exception
{
friend class value;
std::string stack_;
template <typename T>
value_error(T val):
exception(val)
{
}
value_error(const cstring& val, const cstring& stack):
exception(val.str()),
stack_(stack.str())
{
}
public:
const char* stack() const
{
return stack_.c_str();
}
};
class value_exception;
template <typename ClassType> class class_def;
template <typename ClassType> class class_def_shared;
namespace detail
{
template <typename T>
struct func_traits:
public func_traits<decltype(&T::operator())>
{
};
template <typename R, typename... Args>
struct func_traits<R(*)(Args...)>
{
enum { arity = sizeof...(Args) };
typedef R result_type;
template <size_t i>
struct arg
{
typedef typename std::tuple_element<i, std::tuple<Args...>>::type type;
};
};
template <typename C, typename R, typename... Args>
struct func_traits<R(C::*)(Args...) const>
{
enum { arity = sizeof...(Args) };
typedef R result_type;
template <size_t i>
struct arg
{
typedef typename std::tuple_element<i, std::tuple<Args...>>::type type;
};
};
template <size_t... Ns>
struct indices
{
typedef indices<Ns..., sizeof...(Ns)> next;
};
template <size_t N>
struct make_indices
{
typedef typename make_indices<N - 1>::type::next type;
};
template<>
struct make_indices<0>
{
typedef indices<> type;
};
template <typename... Args>
struct args_traits
{
enum { arity = sizeof...(Args) };
template <size_t i>
struct arg
{
typedef typename std::tuple_element<i, std::tuple<Args...>>::type type;
};
};
struct closures_common
{
template <typename Func, size_t N>
static JSValue handle_closure_expand(Func f, JSContext* ctx, JSValueConst this_val, int argc, JSValueConst *argv);
template <typename Func, size_t N>
static JSValue handle_closure_expand_with_args(Func f, JSContext* ctx, JSValueConst this_val, int argc, JSValueConst *argv);
};
template <typename FirstArg>
struct closures
{
template <size_t N, typename R, typename... A>
struct function
{
static JSValue handler(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int /*magic*/, void *opaque)
{
typedef R(*Func)(A...);
Func f = reinterpret_cast<Func>(opaque);
return closures_common::handle_closure_expand<Func, N>(*f, ctx, this_val, argc, argv);
}
};
template<typename R, typename... A>
static JSValue create(JSContext* ctx, R(*f)(A...))
{
using traits = detail::func_traits<decltype(f)>;
return JS_NewCClosure(ctx, function<traits::arity, R, A...>::handler, traits::arity, 0, reinterpret_cast<void*>(f), nullptr);
}
template <typename Func, size_t N>
struct functor
{
static JSValue handler(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst *argv, int /*magic*/, void* opaque)
{
Func* f = reinterpret_cast<Func*>(opaque);
return closures_common::handle_closure_expand<Func, N>(*f, ctx, this_val, argc, argv);
}
};
template<typename Func>
static JSValue create(JSContext* ctx, Func f)
{
using traits = detail::func_traits<decltype(f)>;
std::unique_ptr<decltype(f)> fcopy(new Func(std::move(f)));
JSValue ret = JS_NewCClosure(ctx, functor<decltype(f), traits::arity>::handler, traits::arity, 0, reinterpret_cast<void*>(fcopy.get()),
[](void* opaque)
{
delete reinterpret_cast<Func*>(opaque);
});
if (JS_IsException(ret))
return ret;
fcopy.release();
return ret;
}
};
template<>
struct closures<const args&>
{
template <size_t N, typename R, typename... A>
struct function
{
static JSValue handler(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int /*magic*/, void *opaque)
{
typedef R(*Func)(A...);
Func f = reinterpret_cast<Func>(opaque);
return closures_common::handle_closure_expand_with_args<Func, N>(*f, ctx, this_val, argc, argv);
}
};
template<typename R, typename... A>
static JSValue create(JSContext* ctx, R(*f)(A...))
{
using traits = detail::func_traits<decltype(f)>;
return JS_NewCClosure(ctx, function<traits::arity - 1, R, A...>::handler, traits::arity - 1, 0, reinterpret_cast<void*>(f), nullptr);
}
template <typename Func, size_t N>
struct functor
{
static JSValue handler(JSContext *ctx, JSValue this_val, int argc, JSValueConst *argv, int /*magic*/, void* opaque)
{
Func* f = reinterpret_cast<Func*>(opaque);
return closures_common::handle_closure_expand_with_args<Func, N>(*f, ctx, this_val, argc, argv);
}
};
template<typename Func>
static JSValue create(JSContext* ctx, Func f)
{
using traits = detail::func_traits<decltype(f)>;
std::unique_ptr<decltype(f)> fcopy(new Func(std::move(f)));
JSValue ret = JS_NewCClosure(ctx, functor<decltype(f), traits::arity - 1>::handler, traits::arity - 1, 0, reinterpret_cast<void*>(fcopy.get()),
[](void* opaque)
{
delete reinterpret_cast<Func*>(opaque);
});
if (JS_IsException(ret))
return ret;
fcopy.release();
return ret;
}
};
// based on: https://stackoverflow.com/a/30766365
template <typename T>
struct is_iterator
{
static char test(...);
template <typename U,
typename = typename std::iterator_traits<U>::difference_type,
typename = typename std::iterator_traits<U>::pointer,
typename = typename std::iterator_traits<U>::reference,
typename = typename std::iterator_traits<U>::value_type,
typename = typename std::iterator_traits<U>::iterator_category
> static long test(U&&);
constexpr static bool value = std::is_same<decltype(test(std::declval<T>())),long>::value;
};
template<typename Arg1, typename Arg2>
struct is_iterator_args
{
constexpr static bool value = is_iterator<Arg1>::value && is_iterator<Arg2>::value;
};
struct functions
{
static value call_common_args(const value& func, JSContext* ctx, const value& thisObj, size_t acnt, JSValue* avals);
template <typename... Args>
static value call_common(const value& func, JSContext* ctx, const value& thisObj, Args&&... a);
template <typename Begin, typename End>
static value call_common_it(const value& func, JSContext* ctx, const value& thisObj, Begin&& begin, End&& end);
static value call(const value& func, JSContext* ctx, const value& thisObj);
template <typename A>
static value call(const value& func, JSContext* ctx, const value& thisObj, A&& a);
template <typename A1, typename A2, typename... Args,
typename = typename std::enable_if<!is_iterator_args<A1, A2>::value>::type>
static value call(const value& func, JSContext* ctx, const value& thisObj, A1&& a1, A2&& a2, Args&&... a);
template <typename Begin, typename End,
typename = typename std::enable_if<is_iterator_args<Begin, End>::value>::type>
static value call(const value& func, JSContext* ctx, const value& thisObj, Begin&& begin, End&& end);
};
struct classes
{
template <typename ClassType, typename std::enable_if<std::is_base_of<class_def<ClassType>, decltype(ClassType::class_definition)>::value>::type*>
static ClassType* get_raw_inst(JSValueConst val);
template <typename ClassType, typename std::enable_if<std::is_base_of<class_def_shared<ClassType>, decltype(ClassType::class_definition)>::value>::type*>
static std::shared_ptr<ClassType>* get_raw_inst(JSValueConst val);
template <typename ClassType, typename std::enable_if<std::is_base_of<class_def<ClassType>, decltype(ClassType::class_definition)>::value>::type*>
static ClassType* get_inst(JSValueConst val);
template <typename ClassType, typename std::enable_if<std::is_base_of<class_def_shared<ClassType>, decltype(ClassType::class_definition)>::value>::type*>
static std::shared_ptr<ClassType> get_inst(JSValueConst val);
template <typename ClassType, typename std::enable_if<std::is_base_of<class_def_shared<ClassType>, decltype(ClassType::class_definition)>{}, int>::type>
static JSValue class_make_object_for_inst(JSContext* ctx, const std::shared_ptr<ClassType>& inst);
template <typename ClassType, typename std::enable_if<std::is_base_of<class_def<ClassType>, decltype(ClassType::class_definition)>{}, int>::type>
static JSValue class_make_inst(JSContext* ctx, JSValueConst new_target, int argc, JSValueConst *argv);
template <typename ClassType, typename std::enable_if<std::is_base_of<class_def_shared<ClassType>, decltype(ClassType::class_definition)>{}, int>::type>
static JSValue class_make_inst(JSContext* ctx, JSValueConst new_target, int argc, JSValueConst *argv);
template <typename ClassType>
static ClassType* raw_to_inst_ptr(ClassType* raw_ptr)
{
return raw_ptr;
}
template <typename ClassType>
static ClassType* raw_to_inst_ptr(std::shared_ptr<ClassType>* raw_ptr)
{
return raw_ptr->get();
}
template <typename ClassType>
static ClassType* raw_to_inst(ClassType* raw_ptr)
{
return raw_ptr;
}
template <typename ClassType>
static std::shared_ptr<ClassType>& raw_to_inst(std::shared_ptr<ClassType>* raw_ptr)
{
return *raw_ptr;
}
template <typename ClassType, typename std::enable_if<std::is_base_of<class_def<ClassType>, decltype(ClassType::class_definition)>::value>::type*>
static void finalizer(JSRuntime *rt, JSValue val);
template <typename ClassType, typename std::enable_if<std::is_base_of<class_def_shared<ClassType>, decltype(ClassType::class_definition)>::value>::type*>
static void finalizer(JSRuntime *rt, JSValue val);
template <typename ClassType>
static JSValue ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *argv);
template <typename...>
using void_t = void;
template <typename, typename = void>
struct has_gc_mark: std::false_type{};
template <typename C>
struct has_gc_mark<C, void_t<decltype(&C::gc_mark)>>: std::is_same<void, decltype(std::declval<C>().gc_mark(nullptr))>{};
template <typename ClassType, typename std::enable_if<has_gc_mark<ClassType>::value, ClassType>::type*>
static void gc_mark(JSRuntime *rt, JSValueConst val, JS_MarkFunc *mark_func);
template <typename ClassType, typename std::enable_if<!has_gc_mark<ClassType>::value, ClassType>::type* = nullptr>
static void gc_mark(JSRuntime *rt, JSValueConst val, JS_MarkFunc *mark_func)
{
}
template <typename ClassType, value(ClassType::*Func)(const args&)>
static JSValue invoke_member(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv);
template <typename ClassType, value(ClassType::*Getter)(const value&)>
static JSValue invoke_getter(JSContext *ctx, JSValueConst this_val);
template <typename ClassType, void(ClassType::*Getter)(const value&, const value&)>
static JSValue invoke_setter(JSContext *ctx, JSValueConst this_val, JSValueConst val);
};
struct members
{
template <typename MemberType>
static void add_function_list_entry(JSCFunctionListEntry* entries, size_t& idx, MemberType member);
template <typename ClassType, typename ClassDefType, typename... Args>
static void create_class_def(ClassDefType& cdef, const char* name, int ctor_argc, Args&&... args);
};
}
class value:
private detail::list_entry
{
friend class context;
friend class runtime;
friend class args;
friend class context_exception;
friend class detail::jsvalue_list;
friend class detail::closures_common;
friend class detail::functions;
template <typename FirstType> friend class detail::closures;
friend class detail::classes;
template <typename Owned> friend class detail::owner;
template <typename ClassType> friend class class_builder;
context* owner_{nullptr};
JSContext* ctx_{nullptr};
JSValue val_{0}; // valid if ctx_ is not null
void track();
void track(value& from);
void untrack();
inline void validate() const
{
if (!valid())
throw_value_exception("no context");
}
inline void throw_value_exception(const char* msg) const;
inline void do_throw(value exval);
void handle_pending_exception();
inline void check_throw(bool check_exceptions)
{
if (ctx_)
{
if (check_exceptions)
handle_pending_exception();
if (JS_IsException(val_))
do_throw(value(ctx_, JS_GetException(ctx_)));
}
}
JSValue steal()
{
validate();
JSValue ret = val_;
val_ = {0};
ctx_ = nullptr;
return ret;
}
explicit value(JSContext* ctx, JSValue val, bool dup = false):
ctx_(ctx),
val_(ctx && dup ? JS_DupValue(ctx, val) : val)
{
QJSCPP_DEBUG("value(JSContext*, JSValue) @" << (void*)this);
validate();
track();
}
public:
typedef std::function<void(JSValue val)> mark_func;
value()
{
QJSCPP_DEBUG("value() @" << (void*)this);
}
value(value&& from):
ctx_(from.ctx_),
val_(from.val_)
{
QJSCPP_DEBUG("value(value&& @" << (void*)&from << ") @" << (void*)this << " -> " << (from.valid() ? from.as_cstring() : "[nothing]"));
if (&from != this)
{
from.ctx_ = nullptr;
from.val_ = {0};
if (ctx_)
track(from);
}
}
explicit value(JSContext *ctx, const value& other)
{
QJSCPP_DEBUG("value(JSContext, const value& @" << (void*)&other << ") @" << (void*)this << " -> " << (other.valid() ? other.as_cstring() : "[nothing]"));
if (ctx && other.ctx_)
{
ctx_ = ctx;
val_ = JS_DupValue(ctx_, other.val_);
track();
}
else
{
ctx_ = nullptr;
val_ = {0};
}
}
value(const value& other):
ctx_(other.ctx_)
{
QJSCPP_DEBUG("value(const value& @" << (void*)&other << ") @" << (void*)this << " -> " << (other.valid() ? other.as_cstring() : "[nothing]"));
if (ctx_)
{
track();
val_ = JS_DupValue(ctx_, other.val_);
}
}
value& operator=(const value& other)
{
QJSCPP_DEBUG("value& operator=(const value& @" << (void*)&other << ") @" << (void*)this << " -> " << (other.valid() ? other.as_cstring() : "[nothing]"));
if (&other != this)
{
if (ctx_)
{
untrack();
JS_FreeValue(ctx_, val_);
}
ctx_ = other.ctx_;
if (ctx_)
{
track();
val_ = JS_DupValue(ctx_, other.val_);
}
else
val_ = {0};
}
return *this;
}
value& operator=(value&& from)
{
QJSCPP_DEBUG("value& operator=(value&& @" << (void*)&from << ") @" << (void*)this << " -> " << (from.valid() ? from.as_cstring() : "[nothing]"));
if (&from != this)
{
if (ctx_)
{
JS_FreeValue(ctx_, val_);
if (!from.ctx_)
untrack();
}
else if (from.ctx_)
track();
ctx_ = from.ctx_;
from.ctx_ = nullptr;
val_ = from.val_;
from.val_ = {0};
if (ctx_)
from.untrack();
}
return *this;
}
value(JSContext* ctx, const char* str):
ctx_(ctx),
val_(JS_NewStringLen(ctx_, str, ::strlen(str)))
{
QJSCPP_DEBUG("value(JSContext*, std::string) @" << (void*)this);
validate();
track();
}
value(JSContext* ctx, const std::string& str):
ctx_(ctx),
val_(JS_NewStringLen(ctx_, str.data(), str.length()))
{
QJSCPP_DEBUG("value(JSContext*, std::string) @" << (void*)this);
validate();
track();
}
template<typename R, typename... A>
value(JSContext* ctx, R(*f)(A...)):
ctx_(ctx)
{
QJSCPP_DEBUG("value(JSContext*, int, [function]) @" << (void*)this);
validate();
using traits = detail::func_traits<decltype(f)>;
val_ = detail::closures<typename traits::template arg<0>::type>::template create(ctx_, f);
check_throw(false);
track();
}
template<typename R>
value(JSContext* ctx, R(*f)()):
ctx_(ctx)
{
QJSCPP_DEBUG("value(JSContext*, int, [function]) @" << (void*)this);
validate();
val_ = detail::closures<void>::template create(ctx_, f);
check_throw(false);
track();
}
template <typename Func>
value(JSContext* ctx, Func f):
ctx_(ctx)
{
QJSCPP_DEBUG("value(JSContext*, int, [functor]) @" << (void*)this);
validate();
using traits = detail::func_traits<decltype(f)>;
val_ = detail::closures<typename traits::template arg<0>::type>::template create(ctx_, std::move(f));
check_throw(false);
track();
}
template <typename ClassType>
value(JSContext* ctx, const std::shared_ptr<ClassType>& inst):
ctx_(ctx)
{
QJSCPP_DEBUG("value(JSContext*, [object]) @" << (void*)this);
validate();
val_ = detail::classes::class_make_object_for_inst<ClassType>(ctx_, inst);
check_throw(false);
track();
}
virtual ~value()
{
QJSCPP_DEBUG("~value() @" << (void*)this);
if (ctx_)
JS_FreeValue(ctx_, val_);
untrack();
}
bool valid() const
{
return ctx_ != nullptr;
}
void abandon()
{
if (ctx_)
{
QJSCPP_DEBUG("abandon value @" << (void*)this);
JS_FreeValue(ctx_, val_);
}
if (owner_)
{
owner_ = nullptr;
unlink();
}
ctx_ = nullptr;
val_ = {0};
}
bool is_null() const
{
validate();
return JS_IsNull(val_);
}
bool is_undefined() const
{
validate();
return JS_IsUndefined(val_);
}
bool is_exception() const
{
validate();
return JS_IsError(ctx_, val_);
}
bool is_function() const
{
validate();
return JS_IsFunction(ctx_, val_);
}
bool is_number() const
{
validate();
return JS_IsNumber(val_);
}
bool is_object() const
{
validate();
return JS_IsObject(val_);
}
bool is_string() const
{
validate();
return JS_IsString(val_);
}
bool is_bool() const
{
validate();
return JS_IsBool(val_);
}
bool as_bool() const
{
validate();
bool ret = false;
if (!as_bool(ret))
throw_value_exception("not a int32 value");
return ret;
}
bool as_bool(bool& val) const
{
if (!valid())
{
val = false;
return false;
}
int ret = JS_ToBool(ctx_, val_);
if (ret < 0)
{
val = false;
return false;
}
val = (ret != 0);
return true;
}
double as_double() const
{
validate();
double ret = 0;
if (!as_double(ret))
throw_value_exception("not a double value");
return ret;
}
bool as_double(double& val) const
{
if (!valid() || JS_ToFloat64(ctx_, &val, val_) < 0)
{
val = 0.0;
return false;
}
return true;
}
int32_t as_int32() const
{
validate();
int32_t ret = 0;
if (!as_int32(ret))
throw_value_exception("not a int32 value");
return ret;
}
bool as_int32(int32_t& val) const
{
if (!valid() || JS_ToInt32(ctx_, &val, val_) < 0)
{
val = 0;
return false;
}
return true;
}
uint32_t as_uint32() const
{
validate();
uint32_t ret = 0;
if (!as_uint32(ret))