forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathval.h
More file actions
877 lines (715 loc) · 25.2 KB
/
val.h
File metadata and controls
877 lines (715 loc) · 25.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
/*
* Copyright 2012 The Emscripten Authors. All rights reserved.
* Emscripten is available under two separate licenses, the MIT license and the
* University of Illinois/NCSA Open Source License. Both these licenses can be
* found in the LICENSE file.
*/
#pragma once
#include <emscripten/wire.h>
#include <array>
#include <cassert>
#include <climits>
#include <cstdint> // uintptr_t
#include <optional>
#include <pthread.h>
#include <type_traits>
#include <vector>
#if __cplusplus >= 202002L
#include <coroutine>
#include <exception>
#include <variant>
#endif
namespace emscripten {
class val;
typedef struct _EM_VAL* EM_VAL;
namespace internal {
template<typename WrapperType>
val wrapped_extend(const std::string&, const val&);
enum class EM_INVOKER_KIND {
FUNCTION,
METHOD,
CONSTRUCTOR,
CAST,
};
// Implemented in JavaScript. Don't call these directly.
extern "C" {
void _emval_register_symbol(const char*);
enum {
_EMVAL_UNDEFINED = 2,
_EMVAL_NULL = 4,
_EMVAL_TRUE = 6,
_EMVAL_FALSE = 8,
_EMVAL_LAST_RESERVED_HANDLE = 8,
};
typedef struct _EM_DESTRUCTORS* EM_DESTRUCTORS;
typedef struct _EM_INVOKER* EM_INVOKER;
typedef double EM_GENERIC_WIRE_TYPE;
typedef const void* EM_VAR_ARGS;
void _emval_incref(EM_VAL value);
void _emval_decref(EM_VAL value);
void _emval_run_destructors(EM_DESTRUCTORS handle);
EM_VAL _emval_new_array(void);
EM_VAL _emval_new_array_from_memory_view(EM_VAL mv);
void _emval_array_to_memory_view(EM_VAL dst, EM_VAL src);
EM_VAL _emval_new_object(void);
EM_VAL _emval_new_cstring(const char*);
EM_VAL _emval_new_u8string(const char*);
EM_VAL _emval_new_u16string(const char16_t*);
EM_VAL _emval_get_global(const char* name);
EM_VAL _emval_get_module_property(const char* name);
EM_VAL _emval_get_property(EM_VAL object, EM_VAL key);
void _emval_set_property(EM_VAL object, EM_VAL key, EM_VAL value);
bool _emval_equals(EM_VAL first, EM_VAL second);
bool _emval_strictly_equals(EM_VAL first, EM_VAL second);
bool _emval_greater_than(EM_VAL first, EM_VAL second);
bool _emval_less_than(EM_VAL first, EM_VAL second);
bool _emval_not(EM_VAL object);
// DO NOT call this more than once per signature. It will
// leak generated function objects!
EM_INVOKER _emval_create_invoker(
unsigned argCount, // including return value
const TYPEID argTypes[],
EM_INVOKER_KIND kind);
EM_GENERIC_WIRE_TYPE _emval_invoke(
EM_INVOKER caller,
EM_VAL handle,
const char* methodName,
EM_DESTRUCTORS* destructors,
EM_VAR_ARGS argv);
int64_t _emval_invoke_i64(
EM_INVOKER caller,
EM_VAL handle,
const char* methodName,
EM_DESTRUCTORS* destructors,
EM_VAR_ARGS argv);
EM_VAL _emval_typeof(EM_VAL value);
bool _emval_instanceof(EM_VAL object, EM_VAL constructor);
bool _emval_is_number(EM_VAL object);
bool _emval_is_string(EM_VAL object);
bool _emval_in(EM_VAL item, EM_VAL object);
bool _emval_delete(EM_VAL object, EM_VAL property);
[[noreturn]] bool _emval_throw(EM_VAL object);
EM_VAL _emval_await(EM_VAL promise);
EM_VAL _emval_iter_begin(EM_VAL iterable);
EM_VAL _emval_iter_next(EM_VAL iterator);
#if __cplusplus >= 202002L
void _emval_coro_suspend(EM_VAL promise, void* coro_ptr);
EM_VAL _emval_from_current_cxa_exception();
EM_VAL _emval_coro_make_promise(EM_VAL *resolve, EM_VAL *reject);
#endif
} // extern "C"
template<const char* address>
struct symbol_registrar {
symbol_registrar() {
internal::_emval_register_symbol(address);
}
};
struct DestructorsRunner {
public:
explicit DestructorsRunner(EM_DESTRUCTORS d)
: destructors(d)
{}
~DestructorsRunner() {
if (destructors) {
_emval_run_destructors(destructors);
}
}
DestructorsRunner(const DestructorsRunner&) = delete;
void operator=(const DestructorsRunner&) = delete;
private:
EM_DESTRUCTORS destructors;
};
template<typename WireType>
struct GenericWireTypeConverter {
static WireType from(double wt) {
return static_cast<WireType>(wt);
}
};
template<typename Pointee>
struct GenericWireTypeConverter<Pointee*> {
static Pointee* from(double wt) {
return reinterpret_cast<Pointee*>(static_cast<uintptr_t>(wt));
}
};
template<>
struct GenericWireTypeConverter<BindingType<void>::WireType> {
static BindingType<void>::WireType from(double) {
return {};
}
};
template<typename... Args>
struct PackSize;
template<>
struct PackSize<> {
static constexpr size_t value = 0;
};
template<typename Arg, typename... Args>
struct PackSize<Arg, Args...> {
static constexpr size_t value = (sizeof(typename BindingType<Arg>::WireType) + 7) / 8 + PackSize<Args...>::value;
};
union GenericWireType {
union {
unsigned u;
size_t s;
float f;
const void* p;
} w[2];
double d;
uint64_t u;
};
static_assert(sizeof(GenericWireType) == 2*sizeof(void*), "GenericWireType must be size of 2 pointers");
static_assert(alignof(GenericWireType) == 8, "GenericWireType must be 8-byte-aligned");
inline void writeGenericWireType(GenericWireType*& cursor, float wt) {
cursor->w[0].f = wt;
++cursor;
}
inline void writeGenericWireType(GenericWireType*& cursor, double wt) {
cursor->d = wt;
++cursor;
}
inline void writeGenericWireType(GenericWireType*& cursor, int64_t wt) {
cursor->u = wt;
++cursor;
}
inline void writeGenericWireType(GenericWireType*& cursor, uint64_t wt) {
cursor->u = wt;
++cursor;
}
// Explicit overload for size_t to prevent fallback to the 32-bit generic template
inline void writeGenericWireType(GenericWireType*& cursor, std::size_t wt) {
cursor->w[0].s = wt; // Uses the size_t member (64-bit in Memory64)
++cursor;
}
template<typename T>
void writeGenericWireType(GenericWireType*& cursor, T* wt) {
cursor->w[0].p = wt;
++cursor;
}
template<typename ElementType>
inline void writeGenericWireType(GenericWireType*& cursor, const memory_view<ElementType>& wt) {
cursor->w[0].s = wt.size;
cursor->w[1].p = (void*)wt.data;
++cursor;
}
template<typename T>
void writeGenericWireType(GenericWireType*& cursor, T wt) {
static_assert(sizeof(T) <= sizeof(cursor->w[0].u), "Generic wire type must be smaller than unsigned.");
cursor->w[0].u = static_cast<unsigned>(wt);
++cursor;
}
inline void writeGenericWireTypes(GenericWireType*&) {
}
template<typename First, typename... Rest>
EMSCRIPTEN_ALWAYS_INLINE void writeGenericWireTypes(GenericWireType*& cursor, First&& first, Rest&&... rest) {
writeGenericWireType(cursor, BindingType<First>::toWireType(std::forward<First>(first), rvp::default_tag{}));
writeGenericWireTypes(cursor, std::forward<Rest>(rest)...);
}
template<typename... Args>
struct WireTypePack {
WireTypePack(Args&&... args) {
GenericWireType* cursor = elements.data();
writeGenericWireTypes(cursor, std::forward<Args>(args)...);
}
operator EM_VAR_ARGS() const {
return elements.data();
}
private:
std::array<GenericWireType, PackSize<Args...>::value> elements;
};
} // end namespace internal
#define EMSCRIPTEN_SYMBOL(name) \
static const char name##_symbol[] = #name; \
static const ::emscripten::internal::symbol_registrar<name##_symbol> name##_registrar
class EMBIND_VISIBILITY_DEFAULT val {
public:
// missing operators:
// * ~ - + ++ --
// * * / %
// * + -
// * << >> >>>
// * & ^ | && || ?:
//
// exposing void, comma, and conditional is unnecessary
// same with: = += -= *= /= %= <<= >>= >>>= &= ^= |=
static val array() {
return val(internal::_emval_new_array());
}
template<typename Iter>
static val array(Iter begin, Iter end) {
#if __cplusplus >= 202002L
if constexpr (std::contiguous_iterator<Iter> &&
internal::typeSupportsMemoryView<
typename std::iterator_traits<Iter>::value_type>()) {
val view{ typed_memory_view(std::distance(begin, end), std::to_address(begin)) };
return val(internal::_emval_new_array_from_memory_view(view.as_handle()));
}
// For numeric arrays, the following code is unreachable and the compiler
// will do 'dead code elimination'.
// Others fallback old way.
#endif
val new_array = array();
for (auto it = begin; it != end; ++it) {
new_array.call<void>("push", *it);
}
return new_array;
}
template<typename T>
static val array(const std::vector<T>& vec) {
if constexpr (internal::typeSupportsMemoryView<T>()) {
// for numeric types, pass memory view and copy in JS side one-off
val view{ typed_memory_view(vec.size(), vec.data()) };
return val(internal::_emval_new_array_from_memory_view(view.as_handle()));
} else {
return array(vec.begin(), vec.end());
}
}
static val object() {
return val(internal::_emval_new_object());
}
static val u8string(const char* s) {
return val(internal::_emval_new_u8string(s));
}
static val u16string(const char16_t* s) {
return val(internal::_emval_new_u16string(s));
}
static val undefined() {
return val(EM_VAL(internal::_EMVAL_UNDEFINED));
}
static val null() {
return val(EM_VAL(internal::_EMVAL_NULL));
}
static val take_ownership(EM_VAL e) {
return val(e);
}
static val global(const char* name = 0) {
return val(internal::_emval_get_global(name));
}
static val module_property(const char* name) {
return val(internal::_emval_get_module_property(name));
}
template<typename T, typename... Policies>
explicit val(T&& value, Policies...) {
using namespace internal;
new (this) val(internalCallWithPolicy<EM_INVOKER_KIND::CAST, WithPolicies<Policies...>, val>(nullptr, nullptr, std::forward<T>(value)));
}
val() : val(EM_VAL(internal::_EMVAL_UNDEFINED)) {}
explicit val(const char* v)
: val(internal::_emval_new_cstring(v))
{}
// Note: unlike other constructors, this doesn't use as_handle() because
// it just moves a value and doesn't need to go via incref/decref.
// This means it's safe to move values across threads - an error will
// only arise if you access or free it from the wrong thread later.
val(val&& v) : handle(v.handle), thread(v.thread) {
v.handle = 0;
}
val(const val& v) : val(v.as_handle()) {
if (uses_ref_count()) {
internal::_emval_incref(handle);
}
}
// Add an explicit overload for `val&` as well.
// Without it, C++ will try to use the `T&&` constructor instead of the more
// efficient `val(const val&)` when trying to copy a `val` instance.
val(val& v) : val(static_cast<const val&>(v)) {}
~val() {
if (uses_ref_count()) {
internal::_emval_decref(as_handle());
handle = 0;
}
}
EM_VAL as_handle() const {
#ifdef _REENTRANT
assert(pthread_equal(thread, pthread_self()) && "val accessed from wrong thread");
#endif
return handle;
}
// Takes ownership of the handle away from, and invalidates, this instance.
EM_VAL release_ownership() {
EM_VAL taken = as_handle();
handle = 0;
return taken;
}
val& operator=(val&& v) & {
val tmp(std::move(v));
this->~val();
new (this) val(std::move(tmp));
return *this;
}
val& operator=(const val& v) & {
return *this = val(v);
}
bool hasOwnProperty(const char* key) const {
return val::global("Object")["prototype"]["hasOwnProperty"].call<bool>("call", *this, val(key));
}
bool isNull() const {
return as_handle() == EM_VAL(internal::_EMVAL_NULL);
}
bool isUndefined() const {
return as_handle() == EM_VAL(internal::_EMVAL_UNDEFINED);
}
bool isTrue() const {
return as_handle() == EM_VAL(internal::_EMVAL_TRUE);
}
bool isFalse() const {
return as_handle() == EM_VAL(internal::_EMVAL_FALSE);
}
bool isNumber() const {
return internal::_emval_is_number(as_handle());
}
bool isString() const {
return internal::_emval_is_string(as_handle());
}
bool isArray() const {
return instanceof(global("Array"));
}
bool equals(const val& v) const {
return internal::_emval_equals(as_handle(), v.as_handle());
}
bool operator==(const val& v) const {
return equals(v);
}
bool operator!=(const val& v) const {
return !equals(v);
}
bool strictlyEquals(const val& v) const {
return internal::_emval_strictly_equals(as_handle(), v.as_handle());
}
bool operator>(const val& v) const {
return internal::_emval_greater_than(as_handle(), v.as_handle());
}
bool operator>=(const val& v) const {
return (*this > v) || (*this == v);
}
bool operator<(const val& v) const {
return internal::_emval_less_than(as_handle(), v.as_handle());
}
bool operator<=(const val& v) const {
return (*this < v) || (*this == v);
}
bool operator!() const {
return internal::_emval_not(as_handle());
}
template<typename T>
val operator[](const T& key) const {
return val(internal::_emval_get_property(as_handle(), val_ref(key).as_handle()));
}
template<typename K, typename V, typename... Policies>
void set(const K& key, const V& value, Policies... policies) {
internal::_emval_set_property(as_handle(), val_ref(key).as_handle(), val_ref(value, policies...).as_handle());
}
template<typename T>
bool delete_(const T& property) const {
return internal::_emval_delete(as_handle(), val_ref(property).as_handle());
}
template<typename... Args>
val new_(Args&&... args) const {
using namespace internal;
return internalCall<EM_INVOKER_KIND::CONSTRUCTOR, val>(as_handle(), nullptr, std::forward<Args>(args)...);
}
template<typename... Args>
val operator()(Args&&... args) const {
using namespace internal;
return internalCall<EM_INVOKER_KIND::FUNCTION, val>(as_handle(), nullptr, std::forward<Args>(args)...);
}
template<typename ReturnValue, typename... Args>
ReturnValue call(const char* name, Args&&... args) const {
using namespace internal;
return internalCall<EM_INVOKER_KIND::METHOD, ReturnValue>(as_handle(), name, std::forward<Args>(args)...);
}
template<typename T, typename ...Policies>
T as(Policies...) const {
using namespace internal;
return internalCallWithPolicy<EM_INVOKER_KIND::CAST, WithPolicies<Policies...>, T>(as_handle(), nullptr, *this);
}
// Prefer calling val::typeOf() over val::typeof(), since this form works in both C++11 and GNU++11 build modes. "typeof" is a reserved word in GNU++11 extensions.
val typeOf() const {
return val(internal::_emval_typeof(as_handle()));
}
// If code is not being compiled with GNU extensions enabled, typeof() is a valid identifier, so support that as a member function.
#if __is_identifier(typeof)
[[deprecated("Use typeOf() instead.")]]
val typeof() const {
return typeOf();
}
#endif
bool instanceof(const val& v) const {
return internal::_emval_instanceof(as_handle(), v.as_handle());
}
bool in(const val& v) const {
return internal::_emval_in(as_handle(), v.as_handle());
}
[[noreturn]] void throw_() const {
internal::_emval_throw(as_handle());
}
val await() const {
return val(internal::_emval_await(as_handle()));
}
struct iterator;
iterator begin() const;
// our iterators are sentinel-based range iterators; use nullptr as the end sentinel
constexpr nullptr_t end() const { return nullptr; }
#if __cplusplus >= 202002L
class awaiter;
awaiter operator co_await() const;
class promise_type;
#endif
private:
// takes ownership, assumes handle already incref'd and lives on the same thread
explicit val(EM_VAL handle) :
#ifdef _REENTRANT
thread(pthread_self()),
#endif
handle(handle) {}
// Whether this value is a uses incref/decref (true) or is a special reserved
// value (false).
bool uses_ref_count() const {
return handle > reinterpret_cast<EM_VAL>(internal::_EMVAL_LAST_RESERVED_HANDLE);
}
template<typename WrapperType>
friend val internal::wrapped_extend(const std::string& , const val& );
template<internal::EM_INVOKER_KIND Kind, typename Ret, typename... Args>
static Ret internalCall(EM_VAL handle, const char *methodName, Args&&... args) {
using namespace internal;
using Policy = WithPolicies<FilterTypes<isPolicy, Args...>>;
auto filteredArgs = Filter<isNotPolicy>(args...);
return std::apply(
[&](auto&&... actualArgs) -> decltype(auto) {
return internalCallWithPolicy<Kind, Policy, Ret>(handle, methodName, std::forward<decltype(actualArgs)>(actualArgs)...);
},
filteredArgs
);
}
template<internal::EM_INVOKER_KIND Kind, typename Policy, typename Ret, typename... Args>
static Ret internalCallWithPolicy(EM_VAL handle, const char *methodName, Args&&... args) {
using namespace internal;
using RetWire = typename BindingType<Ret>::WireType;
static constexpr typename Policy::template ArgTypeList<Ret, Args...> argTypes;
thread_local EM_INVOKER mc = _emval_create_invoker(argTypes.getCount(), argTypes.getTypes(), Kind);
WireTypePack<Args...> argv(std::forward<Args>(args)...);
EM_DESTRUCTORS destructors = nullptr;
RetWire result;
if constexpr (std::is_integral<RetWire>::value && sizeof(RetWire) == 8) {
// 64-bit integers can't go through "generic wire type" because double and int64 have different ABI.
result = static_cast<RetWire>(_emval_invoke_i64(
mc,
handle,
methodName,
&destructors,
argv));
} else {
result = GenericWireTypeConverter<RetWire>::from(_emval_invoke(
mc,
handle,
methodName,
&destructors,
argv));
}
DestructorsRunner rd(destructors);
return BindingType<Ret>::fromWireType(result);
}
template<typename T, typename... Policies>
val val_ref(const T& v, Policies... policies) const {
return val(v, policies...);
}
const val& val_ref(const val& v) const {
return v;
}
pthread_t thread;
EM_VAL handle;
template <typename T, typename>
friend struct ::emscripten::internal::BindingType;
};
struct val::iterator {
iterator() = delete;
// Make sure iterator is only moveable, not copyable as it represents a mutable state.
iterator(iterator&&) = default;
iterator(const val& v) : iter(internal::_emval_iter_begin(v.as_handle())) {
this->operator++();
}
val&& operator*() { return std::move(cur_value); }
const val& operator*() const { return cur_value; }
void operator++() { cur_value = val(internal::_emval_iter_next(iter.as_handle())); }
bool operator!=(nullptr_t) const { return cur_value.as_handle() != nullptr; }
private:
val iter;
val cur_value;
};
inline val::iterator val::begin() const {
return iterator(*this);
}
#if __cplusplus >= 202002L
// Awaiter defines a set of well-known methods that compiler uses
// to drive the argument of the `co_await` operator (regardless
// of the type of the parent coroutine).
// This one is used for Promises represented by the `val` type.
class val::awaiter {
// State machine holding awaiter's current state. One of:
// - initially created with promise
// - waiting with a given coroutine handle
// - completed with a result
std::variant<val, std::coroutine_handle<val::promise_type>, val> state;
constexpr static std::size_t STATE_PROMISE = 0;
constexpr static std::size_t STATE_CORO = 1;
constexpr static std::size_t STATE_RESULT = 2;
public:
awaiter(const val& promise)
: state(std::in_place_index<STATE_PROMISE>, promise) {}
// just in case, ensure nobody moves / copies this type around
awaiter(awaiter&&) = delete;
// Promises don't have a synchronously accessible "ready" state.
bool await_ready() { return false; }
// On suspend, store the coroutine handle and invoke a helper that will do
// a rough equivalent of
// `promise.then(value => this.resume_with(value)).catch(error => this.reject_with(error))`.
void await_suspend(std::coroutine_handle<val::promise_type> handle) {
internal::_emval_coro_suspend(std::get<STATE_PROMISE>(state).as_handle(), this);
state.emplace<STATE_CORO>(handle);
}
// When JS invokes `resume_with` with some value, store that value and resume
// the coroutine.
void resume_with(val&& result) {
auto coro = std::move(std::get<STATE_CORO>(state));
state.emplace<STATE_RESULT>(std::move(result));
coro.resume();
}
// When JS invokes `reject_with` with some error value, reject currently suspended
// coroutine's promise with the error value and destroy coroutine frame, because
// in this scenario coroutine never reaches final_suspend point to be destroyed automatically.
void reject_with(val&& error);
// `await_resume` finalizes the awaiter and should return the result
// of the `co_await ...` expression - in our case, the stored value.
val await_resume() {
return std::move(std::get<STATE_RESULT>(state));
}
};
inline val::awaiter val::operator co_await() const {
return {*this};
}
// `promise_type` is a well-known subtype with well-known method names
// that compiler uses to drive the coroutine itself
// (`T::promise_type` is used for any coroutine with declared return type `T`).
class val::promise_type {
val promise, resolve, reject;
public:
// Create a `new Promise` and store it alongside the `resolve` and `reject`
// callbacks that can be used to fulfill it.
promise_type() {
EM_VAL resolve_handle;
EM_VAL reject_handle;
promise = val(internal::_emval_coro_make_promise(&resolve_handle, &reject_handle));
resolve = val(resolve_handle);
reject = val(reject_handle);
}
// Return the stored promise as the actual return value of the coroutine.
val get_return_object() { return promise; }
// For similarity with JS async functions, our coroutines are eagerly evaluated.
auto initial_suspend() noexcept { return std::suspend_never{}; }
auto final_suspend() noexcept { return std::suspend_never{}; }
// When exceptions are disabled we don't define unhandled_exception and rely
// on the default terminate behavior.
#ifdef __cpp_exceptions
// On an unhandled exception, reject the stored promise instead of throwing
// it asynchronously where it can't be handled.
void unhandled_exception() {
try {
std::rethrow_exception(std::current_exception());
} catch (const val& error) {
reject(error);
} catch (...) {
val error = val(internal::_emval_from_current_cxa_exception());
reject(error);
}
}
#endif
// Reject the stored promise due to rejection deeper in the call chain
void reject_with(val&& error) {
reject(std::move(error));
}
// Resolve the stored promise on `co_return value`.
template<typename T>
void return_value(T&& value) {
resolve(std::forward<T>(value));
}
};
inline void val::awaiter::reject_with(val&& error) {
auto coro = std::move(std::get<STATE_CORO>(state));
auto& promise = coro.promise();
promise.reject_with(std::move(error));
coro.destroy();
}
#endif
// Declare a custom type that can be used in conjunction with
// emscripten::register_type to emit custom TypeScript definitions for val
// types.
#define EMSCRIPTEN_DECLARE_VAL_TYPE(name) \
struct name : public ::emscripten::val { \
explicit name(val const &other) : val(other) {} \
};
namespace internal {
template<typename T>
struct BindingType<T, typename std::enable_if<std::is_base_of<val, T>::value &&
!std::is_const<T>::value>::type> {
typedef EM_VAL WireType;
// Marshal to JS with move semantics when we can invalidate the temporary val
// object.
static WireType toWireType(val&& v, rvp::default_tag) {
return v.release_ownership();
}
// Marshal to JS with copy semantics when we cannot transfer the val object's
// reference count.
static WireType toWireType(const val& v, rvp::default_tag) {
EM_VAL handle = v.as_handle();
if (v.uses_ref_count()) {
_emval_incref(handle);
}
return handle;
}
static T fromWireType(WireType v) {
return T(val::take_ownership(v));
}
};
template <typename T>
struct BindingType<std::optional<T>> {
using ValBinding = BindingType<val>;
using WireType = ValBinding::WireType;
template<typename ReturnPolicy = void>
static WireType toWireType(std::optional<T> value, rvp::default_tag) {
if (value) {
return ValBinding::toWireType(val(*value, allow_raw_pointers()), rvp::default_tag{});
}
return ValBinding::toWireType(val::undefined(), rvp::default_tag{});
}
static std::optional<T> fromWireType(WireType value) {
val optional = val::take_ownership(value);
if (optional.isUndefined()) {
return {};
}
return optional.as<T>();
}
};
}
template <typename T, typename... Policies>
std::vector<T> vecFromJSArray(const val& v, Policies... policies) {
const uint32_t l = v["length"].as<uint32_t>();
std::vector<T> rv;
rv.reserve(l);
for (uint32_t i = 0; i < l; ++i) {
rv.push_back(v[i].as<T>(std::forward<Policies>(policies)...));
}
return rv;
}
template <typename T>
std::vector<T> convertJSArrayToNumberVector(const val& v) {
const size_t l = v["length"].as<size_t>();
std::vector<T> rv;
rv.resize(l);
// Copy the array into our vector through the use of typed arrays.
// It will try to convert each element through Number().
// See https://www.ecma-international.org/ecma-262/6.0/#sec-%typedarray%.prototype.set-array-offset
// and https://www.ecma-international.org/ecma-262/6.0/#sec-tonumber
val memoryView{ typed_memory_view(l, rv.data()) };
internal::_emval_array_to_memory_view(memoryView.as_handle(), v.as_handle());
return rv;
}
} // end namespace emscripten