@ugo-studio/jspp 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +25 -25
- package/README.md +20 -12
- package/dist/analysis/scope.js +5 -3
- package/dist/analysis/typeAnalyzer.js +21 -25
- package/dist/cli/index.js +14 -4
- package/dist/cli/utils.js +61 -0
- package/dist/core/codegen/control-flow-handlers.js +10 -9
- package/dist/core/codegen/declaration-handlers.js +10 -3
- package/dist/core/codegen/destructuring-handlers.js +9 -4
- package/dist/core/codegen/expression-handlers.js +40 -29
- package/dist/core/codegen/function-handlers.js +78 -12
- package/dist/core/codegen/helpers.js +91 -14
- package/dist/core/codegen/index.js +4 -2
- package/dist/core/codegen/statement-handlers.js +8 -6
- package/package.json +2 -2
- package/scripts/precompile-headers.ts +249 -50
- package/scripts/setup-compiler.ts +63 -63
- package/src/prelude/any_value.cpp +636 -0
- package/src/prelude/any_value.hpp +23 -16
- package/src/prelude/{exception_helpers.hpp → exception.cpp} +53 -53
- package/src/prelude/exception.hpp +27 -27
- package/src/prelude/iterator_instantiations.hpp +10 -0
- package/src/prelude/{index.hpp → jspp.hpp} +10 -16
- package/src/prelude/library/array.cpp +191 -0
- package/src/prelude/library/array.hpp +5 -178
- package/src/prelude/library/console.cpp +125 -0
- package/src/prelude/library/console.hpp +9 -97
- package/src/prelude/library/error.cpp +100 -0
- package/src/prelude/library/error.hpp +8 -108
- package/src/prelude/library/function.cpp +69 -0
- package/src/prelude/library/function.hpp +6 -5
- package/src/prelude/library/global.cpp +96 -0
- package/src/prelude/library/global.hpp +12 -28
- package/src/prelude/library/global_usings.hpp +15 -0
- package/src/prelude/library/math.cpp +258 -0
- package/src/prelude/library/math.hpp +6 -288
- package/src/prelude/library/object.cpp +379 -0
- package/src/prelude/library/object.hpp +5 -267
- package/src/prelude/library/performance.cpp +21 -0
- package/src/prelude/library/performance.hpp +5 -20
- package/src/prelude/library/process.cpp +38 -0
- package/src/prelude/library/process.hpp +3 -31
- package/src/prelude/library/promise.cpp +131 -0
- package/src/prelude/library/promise.hpp +5 -116
- package/src/prelude/library/symbol.cpp +56 -0
- package/src/prelude/library/symbol.hpp +5 -46
- package/src/prelude/library/timer.cpp +88 -0
- package/src/prelude/library/timer.hpp +11 -87
- package/src/prelude/runtime.cpp +19 -0
- package/src/prelude/types.hpp +17 -12
- package/src/prelude/utils/access.hpp +122 -31
- package/src/prelude/utils/assignment_operators.hpp +99 -99
- package/src/prelude/utils/log_any_value/array.hpp +61 -40
- package/src/prelude/utils/log_any_value/function.hpp +39 -39
- package/src/prelude/utils/log_any_value/object.hpp +60 -3
- package/src/prelude/utils/operators.hpp +25 -10
- package/src/prelude/utils/operators_primitive.hpp +336 -336
- package/src/prelude/utils/well_known_symbols.hpp +24 -24
- package/src/prelude/values/array.cpp +1399 -0
- package/src/prelude/values/array.hpp +4 -0
- package/src/prelude/values/async_iterator.cpp +251 -0
- package/src/prelude/values/async_iterator.hpp +60 -32
- package/src/prelude/values/function.cpp +262 -0
- package/src/prelude/values/function.hpp +10 -30
- package/src/prelude/values/iterator.cpp +309 -0
- package/src/prelude/values/iterator.hpp +33 -64
- package/src/prelude/values/number.cpp +176 -0
- package/src/prelude/values/object.cpp +159 -0
- package/src/prelude/values/object.hpp +4 -0
- package/src/prelude/values/promise.cpp +479 -0
- package/src/prelude/values/promise.hpp +9 -2
- package/src/prelude/values/prototypes/array.hpp +46 -1348
- package/src/prelude/values/prototypes/async_iterator.hpp +19 -61
- package/src/prelude/values/prototypes/function.hpp +7 -46
- package/src/prelude/values/prototypes/iterator.hpp +15 -191
- package/src/prelude/values/prototypes/number.hpp +23 -210
- package/src/prelude/values/prototypes/object.hpp +7 -23
- package/src/prelude/values/prototypes/promise.hpp +8 -186
- package/src/prelude/values/prototypes/string.hpp +28 -553
- package/src/prelude/values/prototypes/symbol.hpp +9 -70
- package/src/prelude/values/shape.hpp +52 -52
- package/src/prelude/values/string.cpp +485 -0
- package/src/prelude/values/symbol.cpp +89 -0
- package/src/prelude/values/symbol.hpp +101 -101
- package/src/prelude/any_value_access.hpp +0 -170
- package/src/prelude/any_value_defines.hpp +0 -190
- package/src/prelude/any_value_helpers.hpp +0 -374
- package/src/prelude/values/helpers/array.hpp +0 -199
- package/src/prelude/values/helpers/async_iterator.hpp +0 -275
- package/src/prelude/values/helpers/function.hpp +0 -109
- package/src/prelude/values/helpers/iterator.hpp +0 -145
- package/src/prelude/values/helpers/object.hpp +0 -104
- package/src/prelude/values/helpers/promise.hpp +0 -254
- package/src/prelude/values/helpers/string.hpp +0 -37
- package/src/prelude/values/helpers/symbol.hpp +0 -21
|
@@ -13,6 +13,7 @@ namespace jspp
|
|
|
13
13
|
std::vector<AnyValue> dense; // dense storage for small/contiguous indices
|
|
14
14
|
std::unordered_map<uint32_t, AnyValue> sparse; // sparse indices (very large indices)
|
|
15
15
|
std::unordered_map<std::string, AnyValue> props; // non-index string properties
|
|
16
|
+
std::map<AnyValue, AnyValue> symbol_props;
|
|
16
17
|
AnyValue proto;
|
|
17
18
|
uint64_t length = 0;
|
|
18
19
|
|
|
@@ -25,10 +26,13 @@ namespace jspp
|
|
|
25
26
|
std::string to_std_string() const;
|
|
26
27
|
|
|
27
28
|
bool has_property(const std::string &key) const;
|
|
29
|
+
bool has_symbol_property(const AnyValue &key) const;
|
|
28
30
|
AnyValue get_property(const std::string &key, const AnyValue &thisVal);
|
|
29
31
|
AnyValue get_property(uint32_t idx);
|
|
32
|
+
AnyValue get_symbol_property(const AnyValue &key, const AnyValue &thisVal);
|
|
30
33
|
AnyValue set_property(const std::string &key, const AnyValue &value, const AnyValue &thisVal);
|
|
31
34
|
AnyValue set_property(uint32_t idx, const AnyValue &value);
|
|
35
|
+
AnyValue set_symbol_property(const AnyValue &key, const AnyValue &value, const AnyValue &thisVal);
|
|
32
36
|
|
|
33
37
|
static bool is_array_index(const std::string &s)
|
|
34
38
|
{
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
#include "jspp.hpp"
|
|
2
|
+
#include "values/async_iterator.hpp"
|
|
3
|
+
#include "values/prototypes/async_iterator.hpp"
|
|
4
|
+
|
|
5
|
+
namespace jspp {
|
|
6
|
+
|
|
7
|
+
template <typename T>
|
|
8
|
+
JsAsyncIterator<T>::JsAsyncIterator(handle_type h) : handle(h) {}
|
|
9
|
+
|
|
10
|
+
template <typename T>
|
|
11
|
+
JsAsyncIterator<T>::JsAsyncIterator(JsAsyncIterator &&other) noexcept
|
|
12
|
+
: handle(std::exchange(other.handle, nullptr)),
|
|
13
|
+
props(std::move(other.props)),
|
|
14
|
+
symbol_props(std::move(other.symbol_props)) {}
|
|
15
|
+
|
|
16
|
+
template <typename T>
|
|
17
|
+
JsAsyncIterator<T>::~JsAsyncIterator() { if (handle) handle.destroy(); }
|
|
18
|
+
|
|
19
|
+
template <typename T>
|
|
20
|
+
JsAsyncIterator<T> JsAsyncIterator<T>::promise_type::get_return_object() {
|
|
21
|
+
return JsAsyncIterator{std::coroutine_handle<promise_type>::from_promise(*this)};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
template <typename T>
|
|
25
|
+
std::suspend_always JsAsyncIterator<T>::promise_type::initial_suspend() noexcept { return {}; }
|
|
26
|
+
|
|
27
|
+
template <typename T>
|
|
28
|
+
std::suspend_always JsAsyncIterator<T>::promise_type::final_suspend() noexcept { return {}; }
|
|
29
|
+
|
|
30
|
+
template <typename T>
|
|
31
|
+
bool JsAsyncIterator<T>::promise_type::YieldAwaiter::await_ready() { return false; }
|
|
32
|
+
|
|
33
|
+
template <typename T>
|
|
34
|
+
void JsAsyncIterator<T>::promise_type::YieldAwaiter::await_suspend(std::coroutine_handle<promise_type> h) {}
|
|
35
|
+
|
|
36
|
+
template <typename T>
|
|
37
|
+
T JsAsyncIterator<T>::promise_type::YieldAwaiter::await_resume() { return p.current_input; }
|
|
38
|
+
|
|
39
|
+
template <typename T>
|
|
40
|
+
void JsAsyncIterator<T>::promise_type::unhandled_exception() {
|
|
41
|
+
try {
|
|
42
|
+
std::rethrow_exception(std::current_exception());
|
|
43
|
+
} catch (const Exception &e) {
|
|
44
|
+
fail_all(e.data);
|
|
45
|
+
} catch (const std::exception &e) {
|
|
46
|
+
fail_all(AnyValue::make_string(e.what()));
|
|
47
|
+
} catch (...) {
|
|
48
|
+
fail_all(AnyValue::make_string("Unknown error in async generator"));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
template <typename T>
|
|
53
|
+
bool JsAsyncIterator<T>::promise_type::AsyncIterAwaiter::await_ready() { return base_awaiter.await_ready(); }
|
|
54
|
+
|
|
55
|
+
template <typename T>
|
|
56
|
+
void JsAsyncIterator<T>::promise_type::AsyncIterAwaiter::await_suspend(std::coroutine_handle<promise_type> h) {
|
|
57
|
+
if (!base_awaiter.value.is_promise()) {
|
|
58
|
+
jspp::Scheduler::instance().enqueue([h]() mutable {
|
|
59
|
+
auto &pr = h.promise();
|
|
60
|
+
pr.is_awaiting = false;
|
|
61
|
+
pr.is_running = true;
|
|
62
|
+
h.resume();
|
|
63
|
+
pr.is_running = false;
|
|
64
|
+
if (!h.done() && !pr.is_awaiting && !pr.pending_calls.empty()) {
|
|
65
|
+
while (!h.done() && !pr.is_awaiting && !pr.pending_calls.empty()) {
|
|
66
|
+
pr.is_running = true;
|
|
67
|
+
pr.current_input = pr.pending_calls.front().second;
|
|
68
|
+
h.resume();
|
|
69
|
+
pr.is_running = false;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
auto p = base_awaiter.value.as_promise();
|
|
76
|
+
p->then(
|
|
77
|
+
[h](AnyValue v) mutable {
|
|
78
|
+
auto &pr = h.promise();
|
|
79
|
+
pr.is_awaiting = false;
|
|
80
|
+
pr.is_running = true;
|
|
81
|
+
h.resume();
|
|
82
|
+
pr.is_running = false;
|
|
83
|
+
if (!h.done() && !pr.is_awaiting && !pr.pending_calls.empty()) {
|
|
84
|
+
while (!h.done() && !pr.is_awaiting && !pr.pending_calls.empty()) {
|
|
85
|
+
pr.is_running = true;
|
|
86
|
+
pr.current_input = pr.pending_calls.front().second;
|
|
87
|
+
h.resume();
|
|
88
|
+
pr.is_running = false;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
[h](AnyValue e) mutable {
|
|
93
|
+
auto &pr = h.promise();
|
|
94
|
+
pr.is_awaiting = false;
|
|
95
|
+
pr.is_running = true;
|
|
96
|
+
h.resume();
|
|
97
|
+
pr.is_running = false;
|
|
98
|
+
}
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
template <typename T>
|
|
103
|
+
AnyValue JsAsyncIterator<T>::promise_type::AsyncIterAwaiter::await_resume() { return base_awaiter.await_resume(); }
|
|
104
|
+
|
|
105
|
+
template <typename T>
|
|
106
|
+
typename JsAsyncIterator<T>::promise_type::AsyncIterAwaiter JsAsyncIterator<T>::promise_type::await_transform(AnyValue value) {
|
|
107
|
+
is_awaiting = true;
|
|
108
|
+
return AsyncIterAwaiter{AnyValueAwaiter{std::move(value)}, *this};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
template <typename T>
|
|
112
|
+
std::string JsAsyncIterator<T>::to_std_string() const { return "[object AsyncGenerator]"; }
|
|
113
|
+
|
|
114
|
+
template <typename T>
|
|
115
|
+
void JsAsyncIterator<T>::resume_next()
|
|
116
|
+
{
|
|
117
|
+
if (!handle || handle.done()) return;
|
|
118
|
+
auto &p = handle.promise();
|
|
119
|
+
if (p.is_awaiting || p.is_running) return;
|
|
120
|
+
if (p.pending_calls.empty()) return;
|
|
121
|
+
p.is_running = true;
|
|
122
|
+
auto &next_call = p.pending_calls.front();
|
|
123
|
+
p.current_input = next_call.second;
|
|
124
|
+
handle.resume();
|
|
125
|
+
p.is_running = false;
|
|
126
|
+
if (!p.pending_calls.empty() && !p.is_awaiting && !handle.done()) {
|
|
127
|
+
this->ref();
|
|
128
|
+
Scheduler::instance().enqueue([this]() { this->resume_next(); this->deref(); });
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
template <typename T>
|
|
133
|
+
JsPromise JsAsyncIterator<T>::next(const T &val)
|
|
134
|
+
{
|
|
135
|
+
JsPromise p;
|
|
136
|
+
if (handle) {
|
|
137
|
+
if (handle.done()) p.resolve(AnyValue::make_object({{"value", Constants::UNDEFINED}, {"done", Constants::TRUE}}));
|
|
138
|
+
else { handle.promise().pending_calls.push({p, val}); resume_next(); }
|
|
139
|
+
} else p.resolve(AnyValue::make_object({{"value", Constants::UNDEFINED}, {"done", Constants::TRUE}}));
|
|
140
|
+
return p;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
template <typename T>
|
|
144
|
+
bool JsAsyncIterator<T>::has_symbol_property(const AnyValue &key) const { return symbol_props.count(key) > 0; }
|
|
145
|
+
|
|
146
|
+
template <typename T>
|
|
147
|
+
AnyValue JsAsyncIterator<T>::get_property(const std::string &key, AnyValue thisVal)
|
|
148
|
+
{
|
|
149
|
+
auto it = props.find(key);
|
|
150
|
+
if (it == props.end()) {
|
|
151
|
+
if constexpr (std::is_same_v<T, AnyValue>) {
|
|
152
|
+
auto proto_it = AsyncIteratorPrototypes::get(key);
|
|
153
|
+
if (proto_it.has_value()) return AnyValue::resolve_property_for_read(proto_it.value(), thisVal, key);
|
|
154
|
+
}
|
|
155
|
+
return Constants::UNDEFINED;
|
|
156
|
+
}
|
|
157
|
+
return AnyValue::resolve_property_for_read(it->second, thisVal, key);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
template <typename T>
|
|
161
|
+
AnyValue JsAsyncIterator<T>::get_symbol_property(const AnyValue &key, AnyValue thisVal)
|
|
162
|
+
{
|
|
163
|
+
auto it = symbol_props.find(key);
|
|
164
|
+
if (it == symbol_props.end()) {
|
|
165
|
+
if constexpr (std::is_same_v<T, AnyValue>) {
|
|
166
|
+
auto proto_it = AsyncIteratorPrototypes::get(key);
|
|
167
|
+
if (proto_it.has_value()) return AnyValue::resolve_property_for_read(proto_it.value(), thisVal, key.to_std_string());
|
|
168
|
+
}
|
|
169
|
+
return Constants::UNDEFINED;
|
|
170
|
+
}
|
|
171
|
+
return AnyValue::resolve_property_for_read(it->second, thisVal, key.to_std_string());
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
template <typename T>
|
|
175
|
+
AnyValue JsAsyncIterator<T>::set_property(const std::string &key, AnyValue value, AnyValue thisVal)
|
|
176
|
+
{
|
|
177
|
+
if constexpr (std::is_same_v<T, AnyValue>) {
|
|
178
|
+
auto proto_it = AsyncIteratorPrototypes::get(key);
|
|
179
|
+
if (proto_it.has_value()) {
|
|
180
|
+
auto proto_value = proto_it.value();
|
|
181
|
+
if (proto_value.is_accessor_descriptor()) return AnyValue::resolve_property_for_write(proto_value, thisVal, value, key);
|
|
182
|
+
if (proto_value.is_data_descriptor() && !proto_value.as_data_descriptor()->writable) return AnyValue::resolve_property_for_write(proto_value, thisVal, value, key);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
auto it = props.find(key);
|
|
186
|
+
if (it != props.end()) return jspp::AnyValue::resolve_property_for_write(it->second, thisVal, value, key);
|
|
187
|
+
else { props[key] = value; return value; }
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
template <typename T>
|
|
191
|
+
AnyValue JsAsyncIterator<T>::set_symbol_property(const AnyValue &key, AnyValue value, AnyValue thisVal)
|
|
192
|
+
{
|
|
193
|
+
auto it = symbol_props.find(key);
|
|
194
|
+
if (it != symbol_props.end()) return AnyValue::resolve_property_for_write(it->second, thisVal, value, key.to_std_string());
|
|
195
|
+
else { symbol_props[key] = value; return value; }
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// Explicit template instantiation
|
|
199
|
+
template class JsAsyncIterator<AnyValue>;
|
|
200
|
+
|
|
201
|
+
namespace AsyncIteratorPrototypes {
|
|
202
|
+
|
|
203
|
+
AnyValue &get_toString_fn()
|
|
204
|
+
{
|
|
205
|
+
static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue>) -> AnyValue
|
|
206
|
+
{ return AnyValue::make_string(thisVal.as_async_iterator()->to_std_string()); },
|
|
207
|
+
"toString");
|
|
208
|
+
return fn;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
AnyValue &get_asyncIterator_fn()
|
|
212
|
+
{
|
|
213
|
+
static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue>) -> AnyValue
|
|
214
|
+
{ return thisVal; },
|
|
215
|
+
"Symbol.asyncIterator");
|
|
216
|
+
return fn;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
AnyValue &get_next_fn()
|
|
220
|
+
{
|
|
221
|
+
static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
|
|
222
|
+
{
|
|
223
|
+
AnyValue val = args.empty() ? Constants::UNDEFINED : args[0];
|
|
224
|
+
auto res = thisVal.as_async_iterator()->next(val);
|
|
225
|
+
return AnyValue::make_promise(res); },
|
|
226
|
+
"next");
|
|
227
|
+
return fn;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
std::optional<AnyValue> get(const std::string &key)
|
|
231
|
+
{
|
|
232
|
+
if (key == "toString") return get_toString_fn();
|
|
233
|
+
if (key == "next") return get_next_fn();
|
|
234
|
+
return std::nullopt;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
std::optional<AnyValue> get(const AnyValue &key)
|
|
238
|
+
{
|
|
239
|
+
if (key.is_string())
|
|
240
|
+
return get(key.as_string()->value);
|
|
241
|
+
|
|
242
|
+
if (key == AnyValue::from_symbol(WellKnownSymbols::toStringTag)) return get_toString_fn();
|
|
243
|
+
if (key == AnyValue::from_symbol(WellKnownSymbols::asyncIterator)) return get_asyncIterator_fn();
|
|
244
|
+
if (key == "next") return get_next_fn();
|
|
245
|
+
|
|
246
|
+
return std::nullopt;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
} // namespace AsyncIteratorPrototypes
|
|
250
|
+
|
|
251
|
+
} // namespace jspp
|
|
@@ -4,16 +4,12 @@
|
|
|
4
4
|
#include <coroutine>
|
|
5
5
|
#include <optional>
|
|
6
6
|
#include <queue>
|
|
7
|
-
#include <
|
|
8
|
-
#include <utility>
|
|
9
|
-
#include <exception>
|
|
10
|
-
#include "values/promise.hpp"
|
|
11
|
-
#include "scheduler.hpp"
|
|
7
|
+
#include <vector>
|
|
12
8
|
|
|
13
9
|
namespace jspp
|
|
14
10
|
{
|
|
15
|
-
// Forward declaration of AnyValue
|
|
16
11
|
class AnyValue;
|
|
12
|
+
struct JsPromise;
|
|
17
13
|
|
|
18
14
|
template <typename T>
|
|
19
15
|
class JsAsyncIterator : public HeapObject
|
|
@@ -28,55 +24,87 @@ namespace jspp
|
|
|
28
24
|
bool is_running = false;
|
|
29
25
|
T current_input;
|
|
30
26
|
|
|
31
|
-
JsAsyncIterator get_return_object()
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
std::coroutine_handle<promise_type>::from_promise(*this)};
|
|
35
|
-
}
|
|
27
|
+
JsAsyncIterator get_return_object();
|
|
28
|
+
std::suspend_always initial_suspend() noexcept;
|
|
29
|
+
std::suspend_always final_suspend() noexcept;
|
|
36
30
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
31
|
+
struct YieldAwaiter
|
|
32
|
+
{
|
|
33
|
+
promise_type &p;
|
|
34
|
+
bool await_ready();
|
|
35
|
+
void await_suspend(std::coroutine_handle<promise_type> h);
|
|
36
|
+
T await_resume();
|
|
37
|
+
};
|
|
40
38
|
|
|
41
|
-
// Declarations
|
|
42
39
|
template <typename From>
|
|
43
|
-
|
|
40
|
+
YieldAwaiter yield_value(From &&from) {
|
|
41
|
+
if (!pending_calls.empty()) {
|
|
42
|
+
auto call = pending_calls.front();
|
|
43
|
+
pending_calls.pop();
|
|
44
|
+
AnyValue result = AnyValue::make_object({{"value", std::forward<From>(from)}, {"done", Constants::FALSE}});
|
|
45
|
+
call.first.resolve(result);
|
|
46
|
+
}
|
|
47
|
+
return YieldAwaiter{*this};
|
|
48
|
+
}
|
|
44
49
|
|
|
45
50
|
template <typename From>
|
|
46
|
-
void return_value(From &&from)
|
|
51
|
+
void return_value(From &&from) {
|
|
52
|
+
if (!pending_calls.empty()) {
|
|
53
|
+
auto call = pending_calls.front();
|
|
54
|
+
pending_calls.pop();
|
|
55
|
+
AnyValue result = AnyValue::make_object({{"value", std::forward<From>(from)}, {"done", Constants::TRUE}});
|
|
56
|
+
call.first.resolve(result);
|
|
57
|
+
}
|
|
58
|
+
while (!pending_calls.empty()) {
|
|
59
|
+
auto call = pending_calls.front();
|
|
60
|
+
pending_calls.pop();
|
|
61
|
+
AnyValue result = AnyValue::make_object({{"value", Constants::UNDEFINED}, {"done", Constants::TRUE}});
|
|
62
|
+
call.first.resolve(result);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
47
65
|
|
|
48
66
|
void unhandled_exception();
|
|
67
|
+
|
|
68
|
+
void fail_all(const AnyValue &reason) {
|
|
69
|
+
while (!pending_calls.empty()) {
|
|
70
|
+
auto call = pending_calls.front();
|
|
71
|
+
pending_calls.pop();
|
|
72
|
+
call.first.reject(reason);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
49
75
|
|
|
50
|
-
|
|
76
|
+
struct AsyncIterAwaiter
|
|
77
|
+
{
|
|
78
|
+
AnyValueAwaiter base_awaiter;
|
|
79
|
+
promise_type &p_ref;
|
|
80
|
+
|
|
81
|
+
bool await_ready();
|
|
82
|
+
void await_suspend(std::coroutine_handle<promise_type> h);
|
|
83
|
+
AnyValue await_resume();
|
|
84
|
+
};
|
|
51
85
|
|
|
52
|
-
|
|
86
|
+
AsyncIterAwaiter await_transform(AnyValue value);
|
|
53
87
|
};
|
|
54
88
|
|
|
55
89
|
using handle_type = std::coroutine_handle<promise_type>;
|
|
56
90
|
handle_type handle;
|
|
57
91
|
|
|
58
|
-
explicit JsAsyncIterator(handle_type h)
|
|
59
|
-
JsAsyncIterator(JsAsyncIterator &&other) noexcept
|
|
60
|
-
: handle(std::exchange(other.handle, nullptr)),
|
|
61
|
-
props(std::move(other.props)) {}
|
|
62
|
-
|
|
92
|
+
explicit JsAsyncIterator(handle_type h);
|
|
93
|
+
JsAsyncIterator(JsAsyncIterator &&other) noexcept;
|
|
63
94
|
JsAsyncIterator(const JsAsyncIterator &) = delete;
|
|
64
95
|
JsAsyncIterator &operator=(const JsAsyncIterator &) = delete;
|
|
65
|
-
|
|
66
|
-
~JsAsyncIterator()
|
|
67
|
-
{
|
|
68
|
-
if (handle)
|
|
69
|
-
handle.destroy();
|
|
70
|
-
}
|
|
96
|
+
~JsAsyncIterator();
|
|
71
97
|
|
|
72
98
|
std::unordered_map<std::string, AnyValue> props;
|
|
99
|
+
std::map<AnyValue, AnyValue> symbol_props;
|
|
73
100
|
|
|
74
101
|
std::string to_std_string() const;
|
|
75
|
-
|
|
76
102
|
JsPromise next(const T &val = T());
|
|
77
|
-
|
|
103
|
+
bool has_symbol_property(const AnyValue &key) const;
|
|
78
104
|
AnyValue get_property(const std::string &key, AnyValue thisVal);
|
|
105
|
+
AnyValue get_symbol_property(const AnyValue &key, AnyValue thisVal);
|
|
79
106
|
AnyValue set_property(const std::string &key, AnyValue value, AnyValue thisVal);
|
|
107
|
+
AnyValue set_symbol_property(const AnyValue &key, AnyValue value, AnyValue thisVal);
|
|
80
108
|
|
|
81
109
|
void resume_next();
|
|
82
110
|
};
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
#include "jspp.hpp"
|
|
2
|
+
#include "values/function.hpp"
|
|
3
|
+
#include "values/prototypes/function.hpp"
|
|
4
|
+
|
|
5
|
+
namespace jspp {
|
|
6
|
+
|
|
7
|
+
// --- JsFunction Implementation ---
|
|
8
|
+
|
|
9
|
+
JsFunction::JsFunction(const JsFunctionCallable &c,
|
|
10
|
+
std::optional<std::string> n,
|
|
11
|
+
std::unordered_map<std::string, AnyValue> p,
|
|
12
|
+
std::map<AnyValue, AnyValue> sp,
|
|
13
|
+
bool is_cls,
|
|
14
|
+
bool is_ctor)
|
|
15
|
+
: callable(c),
|
|
16
|
+
name(std::move(n)),
|
|
17
|
+
props(std::move(p)),
|
|
18
|
+
symbol_props(std::move(sp)),
|
|
19
|
+
proto(Constants::Null),
|
|
20
|
+
is_generator(callable.index() == 1),
|
|
21
|
+
is_async(callable.index() == 2),
|
|
22
|
+
is_class(is_cls),
|
|
23
|
+
is_constructor(is_ctor && !is_generator && !is_async)
|
|
24
|
+
{
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
JsFunction::JsFunction(const JsFunctionCallable &c,
|
|
28
|
+
bool is_gen,
|
|
29
|
+
std::optional<std::string> n,
|
|
30
|
+
std::unordered_map<std::string, AnyValue> p,
|
|
31
|
+
std::map<AnyValue, AnyValue> sp,
|
|
32
|
+
bool is_cls,
|
|
33
|
+
bool is_ctor)
|
|
34
|
+
: callable(c),
|
|
35
|
+
name(std::move(n)),
|
|
36
|
+
props(std::move(p)),
|
|
37
|
+
symbol_props(std::move(sp)),
|
|
38
|
+
proto(Constants::Null),
|
|
39
|
+
is_generator(is_gen),
|
|
40
|
+
is_async(callable.index() == 2),
|
|
41
|
+
is_class(is_cls),
|
|
42
|
+
is_constructor(is_ctor && !is_gen && !is_async)
|
|
43
|
+
{
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
JsFunction::JsFunction(const JsFunctionCallable &c,
|
|
47
|
+
bool is_gen,
|
|
48
|
+
bool is_async_func,
|
|
49
|
+
std::optional<std::string> n,
|
|
50
|
+
std::unordered_map<std::string, AnyValue> p,
|
|
51
|
+
std::map<AnyValue, AnyValue> sp,
|
|
52
|
+
bool is_cls,
|
|
53
|
+
bool is_ctor)
|
|
54
|
+
: callable(c),
|
|
55
|
+
name(std::move(n)),
|
|
56
|
+
props(std::move(p)),
|
|
57
|
+
symbol_props(std::move(sp)),
|
|
58
|
+
proto(Constants::Null),
|
|
59
|
+
is_generator(is_gen),
|
|
60
|
+
is_async(is_async_func),
|
|
61
|
+
is_class(is_cls),
|
|
62
|
+
is_constructor(is_ctor && !is_gen && !is_async_func)
|
|
63
|
+
{
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
std::string JsFunction::to_std_string() const
|
|
67
|
+
{
|
|
68
|
+
std::string type_part = this->is_async ? "async function" : this->is_generator ? "function*"
|
|
69
|
+
: "function";
|
|
70
|
+
std::string name_part = this->name.value_or("");
|
|
71
|
+
return type_part + " " + name_part + "() { [native code] }";
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
AnyValue JsFunction::call(AnyValue thisVal, std::span<const AnyValue> args)
|
|
75
|
+
{
|
|
76
|
+
if (std::function<AnyValue(AnyValue, std::span<const AnyValue>)> *func = std::get_if<0>(&callable))
|
|
77
|
+
{
|
|
78
|
+
return (*func)(thisVal, args);
|
|
79
|
+
}
|
|
80
|
+
else if (std::function<jspp::JsIterator<jspp::AnyValue>(AnyValue, std::vector<AnyValue>)> *func = std::get_if<1>(&callable))
|
|
81
|
+
{
|
|
82
|
+
return AnyValue::from_iterator((*func)(thisVal, std::vector<AnyValue>(args.begin(), args.end())));
|
|
83
|
+
}
|
|
84
|
+
else if (std::function<jspp::JsPromise(AnyValue, std::vector<AnyValue>)> *func = std::get_if<2>(&callable))
|
|
85
|
+
{
|
|
86
|
+
return AnyValue::from_promise((*func)(thisVal, std::vector<AnyValue>(args.begin(), args.end())));
|
|
87
|
+
}
|
|
88
|
+
else if (std::function<jspp::JsAsyncIterator<jspp::AnyValue>(AnyValue, std::vector<AnyValue>)> *func = std::get_if<3>(&callable))
|
|
89
|
+
{
|
|
90
|
+
return AnyValue::from_async_iterator((*func)(thisVal, std::vector<AnyValue>(args.begin(), args.end())));
|
|
91
|
+
}
|
|
92
|
+
else
|
|
93
|
+
{
|
|
94
|
+
return Constants::UNDEFINED;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
bool JsFunction::has_property(const std::string &key) const
|
|
99
|
+
{
|
|
100
|
+
if (props.find(key) != props.end())
|
|
101
|
+
return true;
|
|
102
|
+
if (!proto.is_null() && !proto.is_undefined())
|
|
103
|
+
{
|
|
104
|
+
if (proto.has_property(key))
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
if (FunctionPrototypes::get(key).has_value())
|
|
108
|
+
return true;
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
bool JsFunction::has_symbol_property(const AnyValue &key) const
|
|
113
|
+
{
|
|
114
|
+
if (symbol_props.count(key))
|
|
115
|
+
return true;
|
|
116
|
+
if (!proto.is_null() && !proto.is_undefined())
|
|
117
|
+
{
|
|
118
|
+
if (proto.has_property(key))
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
if (FunctionPrototypes::get(key).has_value())
|
|
122
|
+
return true;
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
AnyValue JsFunction::get_property(const std::string &key, AnyValue thisVal)
|
|
127
|
+
{
|
|
128
|
+
auto it = props.find(key);
|
|
129
|
+
if (it == props.end())
|
|
130
|
+
{
|
|
131
|
+
if (!proto.is_null() && !proto.is_undefined())
|
|
132
|
+
{
|
|
133
|
+
if (proto.has_property(key))
|
|
134
|
+
{
|
|
135
|
+
return proto.get_property_with_receiver(key, thisVal);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
auto proto_it = FunctionPrototypes::get(key);
|
|
140
|
+
if (proto_it.has_value())
|
|
141
|
+
{
|
|
142
|
+
return AnyValue::resolve_property_for_read(proto_it.value(), thisVal, key);
|
|
143
|
+
}
|
|
144
|
+
return Constants::UNDEFINED;
|
|
145
|
+
}
|
|
146
|
+
return AnyValue::resolve_property_for_read(it->second, thisVal, key);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
AnyValue JsFunction::get_symbol_property(const AnyValue &key, AnyValue thisVal)
|
|
150
|
+
{
|
|
151
|
+
auto it = symbol_props.find(key);
|
|
152
|
+
if (it == symbol_props.end())
|
|
153
|
+
{
|
|
154
|
+
if (!proto.is_null() && !proto.is_undefined())
|
|
155
|
+
{
|
|
156
|
+
auto res = proto.get_symbol_property_with_receiver(key, thisVal);
|
|
157
|
+
if (!res.is_undefined())
|
|
158
|
+
return res;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
auto proto_it = FunctionPrototypes::get(key);
|
|
162
|
+
if (proto_it.has_value())
|
|
163
|
+
{
|
|
164
|
+
return AnyValue::resolve_property_for_read(proto_it.value(), thisVal, key.to_std_string());
|
|
165
|
+
}
|
|
166
|
+
return Constants::UNDEFINED;
|
|
167
|
+
}
|
|
168
|
+
return AnyValue::resolve_property_for_read(it->second, thisVal, key.to_std_string());
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
AnyValue JsFunction::set_property(const std::string &key, AnyValue value, AnyValue thisVal)
|
|
172
|
+
{
|
|
173
|
+
auto proto_it = FunctionPrototypes::get(key);
|
|
174
|
+
if (proto_it.has_value())
|
|
175
|
+
{
|
|
176
|
+
auto proto_value = proto_it.value();
|
|
177
|
+
if (proto_value.is_accessor_descriptor())
|
|
178
|
+
{
|
|
179
|
+
return AnyValue::resolve_property_for_write(proto_value, thisVal, value, key);
|
|
180
|
+
}
|
|
181
|
+
if (proto_value.is_data_descriptor() && !proto_value.as_data_descriptor()->writable)
|
|
182
|
+
{
|
|
183
|
+
return AnyValue::resolve_property_for_write(proto_value, thisVal, value, key);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
auto it = props.find(key);
|
|
188
|
+
if (it != props.end())
|
|
189
|
+
{
|
|
190
|
+
return AnyValue::resolve_property_for_write(it->second, thisVal, value, key);
|
|
191
|
+
}
|
|
192
|
+
else
|
|
193
|
+
{
|
|
194
|
+
props[key] = value;
|
|
195
|
+
return value;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
AnyValue JsFunction::set_symbol_property(const AnyValue &key, AnyValue value, AnyValue thisVal)
|
|
200
|
+
{
|
|
201
|
+
auto it = symbol_props.find(key);
|
|
202
|
+
if (it != symbol_props.end())
|
|
203
|
+
{
|
|
204
|
+
return AnyValue::resolve_property_for_write(it->second, thisVal, value, key.to_std_string());
|
|
205
|
+
}
|
|
206
|
+
else
|
|
207
|
+
{
|
|
208
|
+
symbol_props[key] = value;
|
|
209
|
+
return value;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// --- FunctionPrototypes Implementation ---
|
|
214
|
+
|
|
215
|
+
namespace FunctionPrototypes {
|
|
216
|
+
|
|
217
|
+
AnyValue &get_toString_fn()
|
|
218
|
+
{
|
|
219
|
+
static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> _) -> AnyValue
|
|
220
|
+
{ return AnyValue::make_string(thisVal.as_function()->to_std_string()); },
|
|
221
|
+
"toString");
|
|
222
|
+
return fn;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
AnyValue &get_call_fn()
|
|
226
|
+
{
|
|
227
|
+
static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
|
|
228
|
+
{
|
|
229
|
+
AnyValue thisArg = Constants::UNDEFINED;
|
|
230
|
+
std::span<const AnyValue> fnArgs;
|
|
231
|
+
|
|
232
|
+
if (!args.empty())
|
|
233
|
+
{
|
|
234
|
+
thisArg = args[0];
|
|
235
|
+
fnArgs = args.subspan(1);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return thisVal.call(thisArg, fnArgs); },
|
|
239
|
+
"call");
|
|
240
|
+
return fn;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
std::optional<AnyValue> get(const std::string &key)
|
|
244
|
+
{
|
|
245
|
+
if (key == "toString") return get_toString_fn();
|
|
246
|
+
if (key == "call") return get_call_fn();
|
|
247
|
+
return std::nullopt;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
std::optional<AnyValue> get(const AnyValue &key)
|
|
251
|
+
{
|
|
252
|
+
if (key.is_string())
|
|
253
|
+
return get(key.as_string()->value);
|
|
254
|
+
|
|
255
|
+
if (key == AnyValue::from_symbol(WellKnownSymbols::toStringTag)) return get_toString_fn();
|
|
256
|
+
if (key == "call") return get_call_fn();
|
|
257
|
+
return std::nullopt;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
} // namespace FunctionPrototypes
|
|
261
|
+
|
|
262
|
+
} // namespace jspp
|