@ugo-studio/jspp 0.2.8 → 0.3.0
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/dist/analysis/typeAnalyzer.js +42 -27
- package/dist/core/codegen/class-handlers.js +6 -6
- package/dist/core/codegen/control-flow-handlers.js +4 -4
- package/dist/core/codegen/declaration-handlers.js +21 -3
- package/dist/core/codegen/destructuring-handlers.js +187 -0
- package/dist/core/codegen/expression-handlers.js +7 -0
- package/dist/core/codegen/function-handlers.js +58 -36
- package/dist/core/codegen/helpers.js +288 -52
- package/dist/core/codegen/index.js +7 -4
- package/dist/core/codegen/statement-handlers.js +43 -23
- package/package.json +1 -1
- package/scripts/precompile-headers.ts +13 -5
- package/src/prelude/any_value.hpp +362 -361
- package/src/prelude/any_value_access.hpp +170 -170
- package/src/prelude/any_value_defines.hpp +189 -189
- package/src/prelude/any_value_helpers.hpp +374 -365
- package/src/prelude/library/array.hpp +185 -185
- package/src/prelude/library/console.hpp +111 -111
- package/src/prelude/library/error.hpp +112 -112
- package/src/prelude/library/function.hpp +10 -10
- package/src/prelude/library/math.hpp +307 -307
- package/src/prelude/library/object.hpp +275 -275
- package/src/prelude/library/performance.hpp +1 -1
- package/src/prelude/library/process.hpp +39 -39
- package/src/prelude/library/promise.hpp +123 -123
- package/src/prelude/library/symbol.hpp +52 -52
- package/src/prelude/library/timer.hpp +91 -91
- package/src/prelude/types.hpp +178 -178
- package/src/prelude/utils/access.hpp +411 -393
- package/src/prelude/utils/operators.hpp +336 -329
- package/src/prelude/values/array.hpp +0 -1
- package/src/prelude/values/async_iterator.hpp +83 -81
- package/src/prelude/values/function.hpp +82 -82
- package/src/prelude/values/helpers/array.hpp +198 -208
- package/src/prelude/values/helpers/async_iterator.hpp +275 -271
- package/src/prelude/values/helpers/function.hpp +108 -108
- package/src/prelude/values/helpers/iterator.hpp +144 -107
- package/src/prelude/values/helpers/promise.hpp +253 -253
- package/src/prelude/values/helpers/string.hpp +37 -47
- package/src/prelude/values/iterator.hpp +32 -5
- package/src/prelude/values/promise.hpp +72 -72
- package/src/prelude/values/prototypes/array.hpp +54 -42
- package/src/prelude/values/prototypes/iterator.hpp +201 -74
- package/src/prelude/values/prototypes/promise.hpp +196 -196
- package/src/prelude/values/prototypes/string.hpp +564 -542
- package/src/prelude/values/string.hpp +25 -26
|
@@ -1,254 +1,254 @@
|
|
|
1
|
-
#pragma once
|
|
2
|
-
|
|
3
|
-
#include "types.hpp"
|
|
4
|
-
#include "values/promise.hpp"
|
|
5
|
-
#include "any_value.hpp"
|
|
6
|
-
#include "values/prototypes/promise.hpp"
|
|
7
|
-
|
|
8
|
-
namespace jspp
|
|
9
|
-
{
|
|
10
|
-
|
|
11
|
-
inline PromiseState::PromiseState() : result(Constants::UNDEFINED) {}
|
|
12
|
-
|
|
13
|
-
inline JsPromise::JsPromise() : state(std::make_shared<PromiseState>()) {}
|
|
14
|
-
|
|
15
|
-
inline void JsPromise::resolve(
|
|
16
|
-
{
|
|
17
|
-
if (state->status != PromiseStatus::Pending)
|
|
18
|
-
return;
|
|
19
|
-
|
|
20
|
-
if (value.is_promise())
|
|
21
|
-
{
|
|
22
|
-
auto p = value.as_promise();
|
|
23
|
-
if (p->state == state)
|
|
24
|
-
{
|
|
25
|
-
reject(AnyValue::make_string("TypeError: Chaining cycle detected for promise"));
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
auto weak_state = std::weak_ptr<PromiseState>(state);
|
|
30
|
-
|
|
31
|
-
p->then(
|
|
32
|
-
[weak_state](
|
|
33
|
-
{
|
|
34
|
-
if (auto s = weak_state.lock())
|
|
35
|
-
{
|
|
36
|
-
// We can't easily use JsPromise here because it's a HeapObject now.
|
|
37
|
-
// But we can manually resolve the state if we have a way.
|
|
38
|
-
// Actually, we can create a temporary AnyValue from the state.
|
|
39
|
-
// But AnyValue expects a JsPromise* which was allocated with new.
|
|
40
|
-
// This is tricky.
|
|
41
|
-
// Let's assume we can just modify the status and result of the state directly.
|
|
42
|
-
s->status = PromiseStatus::Fulfilled;
|
|
43
|
-
s->result = v;
|
|
44
|
-
auto callbacks = s->onFulfilled;
|
|
45
|
-
s->onFulfilled.clear();
|
|
46
|
-
s->onRejected.clear();
|
|
47
|
-
for (auto &cb : callbacks)
|
|
48
|
-
jspp::Scheduler::instance().enqueue([cb, v]()
|
|
49
|
-
{ cb(v); });
|
|
50
|
-
}
|
|
51
|
-
},
|
|
52
|
-
[weak_state](
|
|
53
|
-
{
|
|
54
|
-
if (auto s = weak_state.lock())
|
|
55
|
-
{
|
|
56
|
-
s->status = PromiseStatus::Rejected;
|
|
57
|
-
s->result = r;
|
|
58
|
-
auto callbacks = s->onRejected;
|
|
59
|
-
s->onFulfilled.clear();
|
|
60
|
-
s->onRejected.clear();
|
|
61
|
-
for (auto &cb : callbacks)
|
|
62
|
-
jspp::Scheduler::instance().enqueue([cb, r]()
|
|
63
|
-
{ cb(r); });
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
state->status = PromiseStatus::Fulfilled;
|
|
70
|
-
state->result = value;
|
|
71
|
-
|
|
72
|
-
// Schedule callbacks
|
|
73
|
-
auto callbacks = state->onFulfilled;
|
|
74
|
-
state->onFulfilled.clear();
|
|
75
|
-
state->onRejected.clear();
|
|
76
|
-
|
|
77
|
-
for (auto &cb : callbacks)
|
|
78
|
-
{
|
|
79
|
-
jspp::Scheduler::instance().enqueue([cb, value]()
|
|
80
|
-
{ cb(value); });
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
inline void JsPromise::reject(
|
|
85
|
-
{
|
|
86
|
-
if (state->status != PromiseStatus::Pending)
|
|
87
|
-
return;
|
|
88
|
-
state->status = PromiseStatus::Rejected;
|
|
89
|
-
state->result = reason;
|
|
90
|
-
|
|
91
|
-
auto callbacks = state->onRejected;
|
|
92
|
-
state->onFulfilled.clear();
|
|
93
|
-
state->onRejected.clear();
|
|
94
|
-
|
|
95
|
-
for (auto &cb : callbacks)
|
|
96
|
-
{
|
|
97
|
-
jspp::Scheduler::instance().enqueue([cb, reason]()
|
|
98
|
-
{ cb(reason); });
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
inline void JsPromise::then(std::function<void(
|
|
103
|
-
{
|
|
104
|
-
if (state->status == PromiseStatus::Fulfilled)
|
|
105
|
-
{
|
|
106
|
-
if (onFulfilled)
|
|
107
|
-
{
|
|
108
|
-
AnyValue val = state->result;
|
|
109
|
-
jspp::Scheduler::instance().enqueue([onFulfilled, val]()
|
|
110
|
-
{ onFulfilled(val); });
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
else if (state->status == PromiseStatus::Rejected)
|
|
114
|
-
{
|
|
115
|
-
if (onRejected)
|
|
116
|
-
{
|
|
117
|
-
AnyValue val = state->result;
|
|
118
|
-
jspp::Scheduler::instance().enqueue([onRejected, val]()
|
|
119
|
-
{ onRejected(val); });
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
else
|
|
123
|
-
{
|
|
124
|
-
if (onFulfilled)
|
|
125
|
-
state->onFulfilled.push_back(onFulfilled);
|
|
126
|
-
if (onRejected)
|
|
127
|
-
state->onRejected.push_back(onRejected);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
inline auto JsPromise::operator co_await() const
|
|
132
|
-
{
|
|
133
|
-
// This is safe because AnyValue::make_promise copies the JsPromise (which is a HeapObject)
|
|
134
|
-
// Actually, AnyValue::make_promise(const JsPromise&) does from_ptr(new JsPromise(promise)).
|
|
135
|
-
return AnyValueAwaiter{AnyValue::make_promise(*this)};
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
inline std::string JsPromise::to_std_string() const
|
|
139
|
-
{
|
|
140
|
-
return "[object Promise]";
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
inline AnyValue JsPromise::get_property(const std::string &key,
|
|
144
|
-
{
|
|
145
|
-
// Prototype lookup
|
|
146
|
-
auto proto_it = PromisePrototypes::get(key);
|
|
147
|
-
if (proto_it.has_value())
|
|
148
|
-
{
|
|
149
|
-
return AnyValue::resolve_property_for_read(proto_it.value(), thisVal, key);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
auto it = props.find(key);
|
|
153
|
-
if (it != props.end())
|
|
154
|
-
{
|
|
155
|
-
return AnyValue::resolve_property_for_read(it->second, thisVal, key);
|
|
156
|
-
}
|
|
157
|
-
return Constants::UNDEFINED;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
inline AnyValue JsPromise::set_property(const std::string &key,
|
|
161
|
-
{
|
|
162
|
-
auto it = props.find(key);
|
|
163
|
-
if (it != props.end())
|
|
164
|
-
{
|
|
165
|
-
return AnyValue::resolve_property_for_write(it->second, thisVal, value, key);
|
|
166
|
-
}
|
|
167
|
-
else
|
|
168
|
-
{
|
|
169
|
-
props[key] = value;
|
|
170
|
-
return value;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
// --- Coroutine Methods ---
|
|
175
|
-
|
|
176
|
-
inline void JsPromisePromiseType::return_value(
|
|
177
|
-
{
|
|
178
|
-
promise.resolve(val);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
inline void JsPromisePromiseType::unhandled_exception()
|
|
182
|
-
{
|
|
183
|
-
try
|
|
184
|
-
{
|
|
185
|
-
throw;
|
|
186
|
-
}
|
|
187
|
-
catch (const Exception &e)
|
|
188
|
-
{
|
|
189
|
-
promise.reject(e.data);
|
|
190
|
-
}
|
|
191
|
-
catch (const std::exception &e)
|
|
192
|
-
{
|
|
193
|
-
promise.reject(AnyValue::make_string(e.what()));
|
|
194
|
-
}
|
|
195
|
-
catch (...)
|
|
196
|
-
{
|
|
197
|
-
promise.reject(AnyValue::make_string("Unknown exception"));
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
inline auto JsPromisePromiseType::await_transform(
|
|
202
|
-
{
|
|
203
|
-
return AnyValueAwaiter{value};
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
inline auto JsPromisePromiseType::await_transform(const JsPromise &value)
|
|
207
|
-
{
|
|
208
|
-
// value is a JsPromise& which is a HeapObject.
|
|
209
|
-
// We wrap it in a temporary AnyValue for Awaiter.
|
|
210
|
-
// Wait, AnyValue::make_promise(value) will allocate a new JsPromise on heap.
|
|
211
|
-
// This is fine for now.
|
|
212
|
-
return AnyValueAwaiter{AnyValue::make_promise(value)};
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
// --- AnyValueAwaiter ---
|
|
216
|
-
|
|
217
|
-
inline bool AnyValueAwaiter::await_ready()
|
|
218
|
-
{
|
|
219
|
-
return false;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
inline void AnyValueAwaiter::await_suspend(std::coroutine_handle<> h)
|
|
223
|
-
{
|
|
224
|
-
if (!value.is_promise())
|
|
225
|
-
{
|
|
226
|
-
jspp::Scheduler::instance().enqueue([h]() mutable
|
|
227
|
-
{ h.resume(); });
|
|
228
|
-
return;
|
|
229
|
-
}
|
|
230
|
-
auto p = value.as_promise();
|
|
231
|
-
|
|
232
|
-
p->then(
|
|
233
|
-
[h](AnyValue v) mutable
|
|
234
|
-
{ h.resume(); },
|
|
235
|
-
[h](AnyValue e) mutable
|
|
236
|
-
{ h.resume(); });
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
inline AnyValue AnyValueAwaiter::await_resume()
|
|
240
|
-
{
|
|
241
|
-
if (!value.is_promise())
|
|
242
|
-
return value;
|
|
243
|
-
auto p = value.as_promise();
|
|
244
|
-
if (p->state->status == PromiseStatus::Fulfilled)
|
|
245
|
-
{
|
|
246
|
-
return p->state->result;
|
|
247
|
-
}
|
|
248
|
-
else
|
|
249
|
-
{
|
|
250
|
-
throw Exception(p->state->result);
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "types.hpp"
|
|
4
|
+
#include "values/promise.hpp"
|
|
5
|
+
#include "any_value.hpp"
|
|
6
|
+
#include "values/prototypes/promise.hpp"
|
|
7
|
+
|
|
8
|
+
namespace jspp
|
|
9
|
+
{
|
|
10
|
+
|
|
11
|
+
inline PromiseState::PromiseState() : result(Constants::UNDEFINED) {}
|
|
12
|
+
|
|
13
|
+
inline JsPromise::JsPromise() : state(std::make_shared<PromiseState>()) {}
|
|
14
|
+
|
|
15
|
+
inline void JsPromise::resolve(AnyValue value)
|
|
16
|
+
{
|
|
17
|
+
if (state->status != PromiseStatus::Pending)
|
|
18
|
+
return;
|
|
19
|
+
|
|
20
|
+
if (value.is_promise())
|
|
21
|
+
{
|
|
22
|
+
auto p = value.as_promise();
|
|
23
|
+
if (p->state == state)
|
|
24
|
+
{
|
|
25
|
+
reject(AnyValue::make_string("TypeError: Chaining cycle detected for promise"));
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
auto weak_state = std::weak_ptr<PromiseState>(state);
|
|
30
|
+
|
|
31
|
+
p->then(
|
|
32
|
+
[weak_state](AnyValue v)
|
|
33
|
+
{
|
|
34
|
+
if (auto s = weak_state.lock())
|
|
35
|
+
{
|
|
36
|
+
// We can't easily use JsPromise here because it's a HeapObject now.
|
|
37
|
+
// But we can manually resolve the state if we have a way.
|
|
38
|
+
// Actually, we can create a temporary AnyValue from the state.
|
|
39
|
+
// But AnyValue expects a JsPromise* which was allocated with new.
|
|
40
|
+
// This is tricky.
|
|
41
|
+
// Let's assume we can just modify the status and result of the state directly.
|
|
42
|
+
s->status = PromiseStatus::Fulfilled;
|
|
43
|
+
s->result = v;
|
|
44
|
+
auto callbacks = s->onFulfilled;
|
|
45
|
+
s->onFulfilled.clear();
|
|
46
|
+
s->onRejected.clear();
|
|
47
|
+
for (auto &cb : callbacks)
|
|
48
|
+
jspp::Scheduler::instance().enqueue([cb, v]()
|
|
49
|
+
{ cb(v); });
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
[weak_state](AnyValue r)
|
|
53
|
+
{
|
|
54
|
+
if (auto s = weak_state.lock())
|
|
55
|
+
{
|
|
56
|
+
s->status = PromiseStatus::Rejected;
|
|
57
|
+
s->result = r;
|
|
58
|
+
auto callbacks = s->onRejected;
|
|
59
|
+
s->onFulfilled.clear();
|
|
60
|
+
s->onRejected.clear();
|
|
61
|
+
for (auto &cb : callbacks)
|
|
62
|
+
jspp::Scheduler::instance().enqueue([cb, r]()
|
|
63
|
+
{ cb(r); });
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
state->status = PromiseStatus::Fulfilled;
|
|
70
|
+
state->result = value;
|
|
71
|
+
|
|
72
|
+
// Schedule callbacks
|
|
73
|
+
auto callbacks = state->onFulfilled;
|
|
74
|
+
state->onFulfilled.clear();
|
|
75
|
+
state->onRejected.clear();
|
|
76
|
+
|
|
77
|
+
for (auto &cb : callbacks)
|
|
78
|
+
{
|
|
79
|
+
jspp::Scheduler::instance().enqueue([cb, value]()
|
|
80
|
+
{ cb(value); });
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
inline void JsPromise::reject(AnyValue reason)
|
|
85
|
+
{
|
|
86
|
+
if (state->status != PromiseStatus::Pending)
|
|
87
|
+
return;
|
|
88
|
+
state->status = PromiseStatus::Rejected;
|
|
89
|
+
state->result = reason;
|
|
90
|
+
|
|
91
|
+
auto callbacks = state->onRejected;
|
|
92
|
+
state->onFulfilled.clear();
|
|
93
|
+
state->onRejected.clear();
|
|
94
|
+
|
|
95
|
+
for (auto &cb : callbacks)
|
|
96
|
+
{
|
|
97
|
+
jspp::Scheduler::instance().enqueue([cb, reason]()
|
|
98
|
+
{ cb(reason); });
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
inline void JsPromise::then(std::function<void(AnyValue)> onFulfilled, std::function<void(AnyValue)> onRejected)
|
|
103
|
+
{
|
|
104
|
+
if (state->status == PromiseStatus::Fulfilled)
|
|
105
|
+
{
|
|
106
|
+
if (onFulfilled)
|
|
107
|
+
{
|
|
108
|
+
AnyValue val = state->result;
|
|
109
|
+
jspp::Scheduler::instance().enqueue([onFulfilled, val]()
|
|
110
|
+
{ onFulfilled(val); });
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
else if (state->status == PromiseStatus::Rejected)
|
|
114
|
+
{
|
|
115
|
+
if (onRejected)
|
|
116
|
+
{
|
|
117
|
+
AnyValue val = state->result;
|
|
118
|
+
jspp::Scheduler::instance().enqueue([onRejected, val]()
|
|
119
|
+
{ onRejected(val); });
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
else
|
|
123
|
+
{
|
|
124
|
+
if (onFulfilled)
|
|
125
|
+
state->onFulfilled.push_back(onFulfilled);
|
|
126
|
+
if (onRejected)
|
|
127
|
+
state->onRejected.push_back(onRejected);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
inline auto JsPromise::operator co_await() const
|
|
132
|
+
{
|
|
133
|
+
// This is safe because AnyValue::make_promise copies the JsPromise (which is a HeapObject)
|
|
134
|
+
// Actually, AnyValue::make_promise(const JsPromise&) does from_ptr(new JsPromise(promise)).
|
|
135
|
+
return AnyValueAwaiter{AnyValue::make_promise(*this)};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
inline std::string JsPromise::to_std_string() const
|
|
139
|
+
{
|
|
140
|
+
return "[object Promise]";
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
inline AnyValue JsPromise::get_property(const std::string &key, AnyValue thisVal)
|
|
144
|
+
{
|
|
145
|
+
// Prototype lookup
|
|
146
|
+
auto proto_it = PromisePrototypes::get(key);
|
|
147
|
+
if (proto_it.has_value())
|
|
148
|
+
{
|
|
149
|
+
return AnyValue::resolve_property_for_read(proto_it.value(), thisVal, key);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
auto it = props.find(key);
|
|
153
|
+
if (it != props.end())
|
|
154
|
+
{
|
|
155
|
+
return AnyValue::resolve_property_for_read(it->second, thisVal, key);
|
|
156
|
+
}
|
|
157
|
+
return Constants::UNDEFINED;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
inline AnyValue JsPromise::set_property(const std::string &key, AnyValue value, AnyValue thisVal)
|
|
161
|
+
{
|
|
162
|
+
auto it = props.find(key);
|
|
163
|
+
if (it != props.end())
|
|
164
|
+
{
|
|
165
|
+
return AnyValue::resolve_property_for_write(it->second, thisVal, value, key);
|
|
166
|
+
}
|
|
167
|
+
else
|
|
168
|
+
{
|
|
169
|
+
props[key] = value;
|
|
170
|
+
return value;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// --- Coroutine Methods ---
|
|
175
|
+
|
|
176
|
+
inline void JsPromisePromiseType::return_value(AnyValue val)
|
|
177
|
+
{
|
|
178
|
+
promise.resolve(val);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
inline void JsPromisePromiseType::unhandled_exception()
|
|
182
|
+
{
|
|
183
|
+
try
|
|
184
|
+
{
|
|
185
|
+
throw;
|
|
186
|
+
}
|
|
187
|
+
catch (const Exception &e)
|
|
188
|
+
{
|
|
189
|
+
promise.reject(e.data);
|
|
190
|
+
}
|
|
191
|
+
catch (const std::exception &e)
|
|
192
|
+
{
|
|
193
|
+
promise.reject(AnyValue::make_string(e.what()));
|
|
194
|
+
}
|
|
195
|
+
catch (...)
|
|
196
|
+
{
|
|
197
|
+
promise.reject(AnyValue::make_string("Unknown exception"));
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
inline auto JsPromisePromiseType::await_transform(AnyValue value)
|
|
202
|
+
{
|
|
203
|
+
return AnyValueAwaiter{value};
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
inline auto JsPromisePromiseType::await_transform(const JsPromise &value)
|
|
207
|
+
{
|
|
208
|
+
// value is a JsPromise& which is a HeapObject.
|
|
209
|
+
// We wrap it in a temporary AnyValue for Awaiter.
|
|
210
|
+
// Wait, AnyValue::make_promise(value) will allocate a new JsPromise on heap.
|
|
211
|
+
// This is fine for now.
|
|
212
|
+
return AnyValueAwaiter{AnyValue::make_promise(value)};
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// --- AnyValueAwaiter ---
|
|
216
|
+
|
|
217
|
+
inline bool AnyValueAwaiter::await_ready()
|
|
218
|
+
{
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
inline void AnyValueAwaiter::await_suspend(std::coroutine_handle<> h)
|
|
223
|
+
{
|
|
224
|
+
if (!value.is_promise())
|
|
225
|
+
{
|
|
226
|
+
jspp::Scheduler::instance().enqueue([h]() mutable
|
|
227
|
+
{ h.resume(); });
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
auto p = value.as_promise();
|
|
231
|
+
|
|
232
|
+
p->then(
|
|
233
|
+
[h](AnyValue v) mutable
|
|
234
|
+
{ h.resume(); },
|
|
235
|
+
[h](AnyValue e) mutable
|
|
236
|
+
{ h.resume(); });
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
inline AnyValue AnyValueAwaiter::await_resume()
|
|
240
|
+
{
|
|
241
|
+
if (!value.is_promise())
|
|
242
|
+
return value;
|
|
243
|
+
auto p = value.as_promise();
|
|
244
|
+
if (p->state->status == PromiseStatus::Fulfilled)
|
|
245
|
+
{
|
|
246
|
+
return p->state->result;
|
|
247
|
+
}
|
|
248
|
+
else
|
|
249
|
+
{
|
|
250
|
+
throw Exception(p->state->result);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
254
|
}
|
|
@@ -1,47 +1,37 @@
|
|
|
1
|
-
#pragma once
|
|
2
|
-
|
|
3
|
-
#include "types.hpp"
|
|
4
|
-
#include "values/array.hpp"
|
|
5
|
-
#include "values/string.hpp"
|
|
6
|
-
#include "exception.hpp"
|
|
7
|
-
#include "any_value.hpp"
|
|
8
|
-
#include "values/prototypes/string.hpp"
|
|
9
|
-
|
|
10
|
-
inline std::string jspp::JsString::to_std_string() const
|
|
11
|
-
{
|
|
12
|
-
return value;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
inline jspp::
|
|
16
|
-
{
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
{
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
if (
|
|
33
|
-
{
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
inline jspp::AnyValue jspp::JsString::get_property(uint32_t idx)
|
|
41
|
-
{
|
|
42
|
-
if (idx < value.length())
|
|
43
|
-
{
|
|
44
|
-
return AnyValue::make_string(std::string(1, value[idx]));
|
|
45
|
-
}
|
|
46
|
-
return Constants::UNDEFINED;
|
|
47
|
-
}
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "types.hpp"
|
|
4
|
+
#include "values/array.hpp"
|
|
5
|
+
#include "values/string.hpp"
|
|
6
|
+
#include "exception.hpp"
|
|
7
|
+
#include "any_value.hpp"
|
|
8
|
+
#include "values/prototypes/string.hpp"
|
|
9
|
+
|
|
10
|
+
inline std::string jspp::JsString::to_std_string() const
|
|
11
|
+
{
|
|
12
|
+
return value;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
inline jspp::AnyValue jspp::JsString::get_property(const std::string &key, const AnyValue &thisVal)
|
|
16
|
+
{
|
|
17
|
+
auto proto_fn = StringPrototypes::get(key);
|
|
18
|
+
if (proto_fn.has_value())
|
|
19
|
+
{
|
|
20
|
+
return AnyValue::resolve_property_for_read(proto_fn.value(), thisVal, key);
|
|
21
|
+
}
|
|
22
|
+
if (JsArray::is_array_index(key))
|
|
23
|
+
{
|
|
24
|
+
uint32_t idx = static_cast<uint32_t>(std::stoull(key));
|
|
25
|
+
return get_property(idx);
|
|
26
|
+
}
|
|
27
|
+
return Constants::UNDEFINED;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
inline jspp::AnyValue jspp::JsString::get_property(uint32_t idx)
|
|
31
|
+
{
|
|
32
|
+
if (idx < value.length())
|
|
33
|
+
{
|
|
34
|
+
return AnyValue::make_string(std::string(1, value[idx]));
|
|
35
|
+
}
|
|
36
|
+
return Constants::UNDEFINED;
|
|
37
|
+
}
|
|
@@ -12,6 +12,9 @@ namespace jspp
|
|
|
12
12
|
// Forward declaration of AnyValue
|
|
13
13
|
class AnyValue;
|
|
14
14
|
|
|
15
|
+
// Special exception to signal a return from a generator
|
|
16
|
+
struct GeneratorReturnException {};
|
|
17
|
+
|
|
15
18
|
template <typename T>
|
|
16
19
|
class JsIterator : public HeapObject
|
|
17
20
|
{
|
|
@@ -30,6 +33,9 @@ namespace jspp
|
|
|
30
33
|
std::exception_ptr exception_;
|
|
31
34
|
T input_value;
|
|
32
35
|
|
|
36
|
+
std::exception_ptr pending_exception = nullptr;
|
|
37
|
+
bool pending_return = false;
|
|
38
|
+
|
|
33
39
|
JsIterator get_return_object()
|
|
34
40
|
{
|
|
35
41
|
return JsIterator{
|
|
@@ -52,7 +58,18 @@ namespace jspp
|
|
|
52
58
|
promise_type &p;
|
|
53
59
|
bool await_ready() { return false; }
|
|
54
60
|
void await_suspend(std::coroutine_handle<promise_type>) {}
|
|
55
|
-
T await_resume() {
|
|
61
|
+
T await_resume() {
|
|
62
|
+
if (p.pending_exception) {
|
|
63
|
+
auto ex = p.pending_exception;
|
|
64
|
+
p.pending_exception = nullptr;
|
|
65
|
+
std::rethrow_exception(ex);
|
|
66
|
+
}
|
|
67
|
+
if (p.pending_return) {
|
|
68
|
+
p.pending_return = false;
|
|
69
|
+
throw GeneratorReturnException{};
|
|
70
|
+
}
|
|
71
|
+
return p.input_value;
|
|
72
|
+
}
|
|
56
73
|
};
|
|
57
74
|
return Awaiter{*this};
|
|
58
75
|
}
|
|
@@ -68,7 +85,13 @@ namespace jspp
|
|
|
68
85
|
|
|
69
86
|
void unhandled_exception()
|
|
70
87
|
{
|
|
71
|
-
|
|
88
|
+
try {
|
|
89
|
+
throw;
|
|
90
|
+
} catch (const GeneratorReturnException&) {
|
|
91
|
+
// Handled return
|
|
92
|
+
} catch (...) {
|
|
93
|
+
exception_ = std::current_exception();
|
|
94
|
+
}
|
|
72
95
|
}
|
|
73
96
|
};
|
|
74
97
|
|
|
@@ -76,7 +99,9 @@ namespace jspp
|
|
|
76
99
|
handle_type handle;
|
|
77
100
|
|
|
78
101
|
explicit JsIterator(handle_type h) : handle(h) {}
|
|
79
|
-
JsIterator(JsIterator &&other) noexcept
|
|
102
|
+
JsIterator(JsIterator &&other) noexcept
|
|
103
|
+
: handle(std::exchange(other.handle, nullptr)),
|
|
104
|
+
props(std::move(other.props)) {}
|
|
80
105
|
|
|
81
106
|
// Delete copy constructor/assignment to ensure unique ownership of the handle
|
|
82
107
|
JsIterator(const JsIterator &) = delete;
|
|
@@ -92,8 +117,10 @@ namespace jspp
|
|
|
92
117
|
|
|
93
118
|
std::string to_std_string() const;
|
|
94
119
|
NextResult next(const T &val = T());
|
|
120
|
+
NextResult return_(const T &val = T());
|
|
121
|
+
NextResult throw_(const AnyValue &err);
|
|
95
122
|
std::vector<T> to_vector();
|
|
96
|
-
AnyValue get_property(const std::string &key,
|
|
97
|
-
AnyValue set_property(const std::string &key,
|
|
123
|
+
AnyValue get_property(const std::string &key, AnyValue thisVal);
|
|
124
|
+
AnyValue set_property(const std::string &key, AnyValue value, AnyValue thisVal);
|
|
98
125
|
};
|
|
99
126
|
}
|