@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
|
JsFunctionCallable callable;
|
|
14
14
|
std::optional<std::string> name;
|
|
15
15
|
std::unordered_map<std::string, AnyValue> props;
|
|
16
|
+
std::map<AnyValue, AnyValue> symbol_props;
|
|
16
17
|
AnyValue proto;
|
|
17
18
|
bool is_generator;
|
|
18
19
|
bool is_async;
|
|
@@ -23,34 +24,18 @@ namespace jspp
|
|
|
23
24
|
JsFunction(const JsFunctionCallable &c,
|
|
24
25
|
std::optional<std::string> n = std::nullopt,
|
|
25
26
|
std::unordered_map<std::string, AnyValue> p = {},
|
|
27
|
+
std::map<AnyValue, AnyValue> sp = {},
|
|
26
28
|
bool is_cls = false,
|
|
27
|
-
bool is_ctor = true)
|
|
28
|
-
: callable(c),
|
|
29
|
-
name(std::move(n)),
|
|
30
|
-
props(std::move(p)),
|
|
31
|
-
is_generator(callable.index() == 1),
|
|
32
|
-
is_async(callable.index() == 2),
|
|
33
|
-
is_class(is_cls),
|
|
34
|
-
is_constructor(is_ctor && !is_generator && !is_async) // Generators and asyncs are never constructors
|
|
35
|
-
{
|
|
36
|
-
}
|
|
29
|
+
bool is_ctor = true);
|
|
37
30
|
|
|
38
31
|
// ---- Constructor B: explicit generator flag (backward compat) ----
|
|
39
32
|
JsFunction(const JsFunctionCallable &c,
|
|
40
33
|
bool is_gen,
|
|
41
34
|
std::optional<std::string> n = std::nullopt,
|
|
42
35
|
std::unordered_map<std::string, AnyValue> p = {},
|
|
36
|
+
std::map<AnyValue, AnyValue> sp = {},
|
|
43
37
|
bool is_cls = false,
|
|
44
|
-
bool is_ctor = true)
|
|
45
|
-
: callable(c),
|
|
46
|
-
name(std::move(n)),
|
|
47
|
-
props(std::move(p)),
|
|
48
|
-
is_generator(is_gen),
|
|
49
|
-
is_async(callable.index() == 2),
|
|
50
|
-
is_class(is_cls),
|
|
51
|
-
is_constructor(is_ctor && !is_gen && !is_async)
|
|
52
|
-
{
|
|
53
|
-
}
|
|
38
|
+
bool is_ctor = true);
|
|
54
39
|
|
|
55
40
|
// ---- Constructor C: explicit async flag ----
|
|
56
41
|
JsFunction(const JsFunctionCallable &c,
|
|
@@ -58,17 +43,9 @@ namespace jspp
|
|
|
58
43
|
bool is_async_func,
|
|
59
44
|
std::optional<std::string> n = std::nullopt,
|
|
60
45
|
std::unordered_map<std::string, AnyValue> p = {},
|
|
46
|
+
std::map<AnyValue, AnyValue> sp = {},
|
|
61
47
|
bool is_cls = false,
|
|
62
|
-
bool is_ctor = true)
|
|
63
|
-
: callable(c),
|
|
64
|
-
name(std::move(n)),
|
|
65
|
-
props(std::move(p)),
|
|
66
|
-
is_generator(is_gen),
|
|
67
|
-
is_async(is_async_func),
|
|
68
|
-
is_class(is_cls),
|
|
69
|
-
is_constructor(is_ctor && !is_gen && !is_async_func)
|
|
70
|
-
{
|
|
71
|
-
}
|
|
48
|
+
bool is_ctor = true);
|
|
72
49
|
|
|
73
50
|
JsType get_heap_type() const override { return JsType::Function; }
|
|
74
51
|
|
|
@@ -76,7 +53,10 @@ namespace jspp
|
|
|
76
53
|
AnyValue call(AnyValue thisVal, std::span<const AnyValue> args);
|
|
77
54
|
|
|
78
55
|
bool has_property(const std::string &key) const;
|
|
56
|
+
bool has_symbol_property(const AnyValue &key) const;
|
|
79
57
|
AnyValue get_property(const std::string &key, AnyValue thisVal);
|
|
58
|
+
AnyValue get_symbol_property(const AnyValue &key, AnyValue thisVal);
|
|
80
59
|
AnyValue set_property(const std::string &key, AnyValue value, AnyValue thisVal);
|
|
60
|
+
AnyValue set_symbol_property(const AnyValue &key, AnyValue value, AnyValue thisVal);
|
|
81
61
|
};
|
|
82
62
|
}
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
#include "jspp.hpp"
|
|
2
|
+
#include "values/iterator.hpp"
|
|
3
|
+
#include "values/prototypes/iterator.hpp"
|
|
4
|
+
|
|
5
|
+
namespace jspp {
|
|
6
|
+
|
|
7
|
+
template <typename T>
|
|
8
|
+
JsIterator<T>::JsIterator(handle_type h) : handle(h) {}
|
|
9
|
+
|
|
10
|
+
template <typename T>
|
|
11
|
+
JsIterator<T>::JsIterator(JsIterator &&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
|
+
JsIterator<T>::~JsIterator() { if (handle) handle.destroy(); }
|
|
18
|
+
|
|
19
|
+
template <typename T>
|
|
20
|
+
JsIterator<T> JsIterator<T>::promise_type::get_return_object() {
|
|
21
|
+
return JsIterator{std::coroutine_handle<promise_type>::from_promise(*this)};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
template <typename T>
|
|
25
|
+
std::suspend_always JsIterator<T>::promise_type::initial_suspend() noexcept { return {}; }
|
|
26
|
+
|
|
27
|
+
template <typename T>
|
|
28
|
+
std::suspend_always JsIterator<T>::promise_type::final_suspend() noexcept { return {}; }
|
|
29
|
+
|
|
30
|
+
template <typename T>
|
|
31
|
+
void JsIterator<T>::promise_type::unhandled_exception() {
|
|
32
|
+
try {
|
|
33
|
+
throw;
|
|
34
|
+
} catch (const GeneratorReturnException&) {
|
|
35
|
+
} catch (...) {
|
|
36
|
+
exception_ = std::current_exception();
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
template <typename T>
|
|
41
|
+
std::string JsIterator<T>::to_std_string() const { return "[object Generator]"; }
|
|
42
|
+
|
|
43
|
+
template <typename T>
|
|
44
|
+
typename JsIterator<T>::NextResult JsIterator<T>::next(const T &val)
|
|
45
|
+
{
|
|
46
|
+
if (!handle || handle.done()) return {std::nullopt, true};
|
|
47
|
+
handle.promise().input_value = val;
|
|
48
|
+
handle.resume();
|
|
49
|
+
if (handle.promise().exception_) std::rethrow_exception(handle.promise().exception_);
|
|
50
|
+
bool is_done = handle.done();
|
|
51
|
+
return {handle.promise().current_value, is_done};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
template <typename T>
|
|
55
|
+
typename JsIterator<T>::NextResult JsIterator<T>::return_(const T &val)
|
|
56
|
+
{
|
|
57
|
+
if (!handle || handle.done()) return {val, true};
|
|
58
|
+
handle.promise().pending_return = true;
|
|
59
|
+
handle.promise().current_value = val;
|
|
60
|
+
handle.resume();
|
|
61
|
+
if (handle.promise().exception_) std::rethrow_exception(handle.promise().exception_);
|
|
62
|
+
return {handle.promise().current_value, true};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
template <typename T>
|
|
66
|
+
typename JsIterator<T>::NextResult JsIterator<T>::throw_(const AnyValue &err)
|
|
67
|
+
{
|
|
68
|
+
if (!handle || handle.done()) throw Exception(err);
|
|
69
|
+
handle.promise().pending_exception = std::make_exception_ptr(Exception(err));
|
|
70
|
+
handle.resume();
|
|
71
|
+
if (handle.promise().exception_) std::rethrow_exception(handle.promise().exception_);
|
|
72
|
+
bool is_done = handle.done();
|
|
73
|
+
return {handle.promise().current_value, is_done};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
template <typename T>
|
|
77
|
+
std::vector<T> JsIterator<T>::to_vector()
|
|
78
|
+
{
|
|
79
|
+
std::vector<T> result;
|
|
80
|
+
while (true) {
|
|
81
|
+
auto next_res = this->next();
|
|
82
|
+
if (next_res.done) break;
|
|
83
|
+
result.push_back(next_res.value.value_or(Constants::UNDEFINED));
|
|
84
|
+
}
|
|
85
|
+
return result;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
template <typename T>
|
|
89
|
+
bool JsIterator<T>::has_symbol_property(const AnyValue &key) const { return symbol_props.count(key) > 0; }
|
|
90
|
+
|
|
91
|
+
template <typename T>
|
|
92
|
+
AnyValue JsIterator<T>::get_property(const std::string &key, AnyValue thisVal)
|
|
93
|
+
{
|
|
94
|
+
auto it = props.find(key);
|
|
95
|
+
if (it == props.end()) {
|
|
96
|
+
if constexpr (std::is_same_v<T, AnyValue>) {
|
|
97
|
+
auto proto_it = IteratorPrototypes::get(key);
|
|
98
|
+
if (proto_it.has_value()) return AnyValue::resolve_property_for_read(proto_it.value(), thisVal, key);
|
|
99
|
+
}
|
|
100
|
+
return Constants::UNDEFINED;
|
|
101
|
+
}
|
|
102
|
+
return AnyValue::resolve_property_for_read(it->second, thisVal, key);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
template <typename T>
|
|
106
|
+
AnyValue JsIterator<T>::get_symbol_property(const AnyValue &key, AnyValue thisVal)
|
|
107
|
+
{
|
|
108
|
+
auto it = symbol_props.find(key);
|
|
109
|
+
if (it == symbol_props.end()) {
|
|
110
|
+
if constexpr (std::is_same_v<T, AnyValue>) {
|
|
111
|
+
auto proto_it = IteratorPrototypes::get(key);
|
|
112
|
+
if (proto_it.has_value()) return AnyValue::resolve_property_for_read(proto_it.value(), thisVal, key.to_std_string());
|
|
113
|
+
}
|
|
114
|
+
return Constants::UNDEFINED;
|
|
115
|
+
}
|
|
116
|
+
return AnyValue::resolve_property_for_read(it->second, thisVal, key.to_std_string());
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
template <typename T>
|
|
120
|
+
AnyValue JsIterator<T>::set_property(const std::string &key, AnyValue value, AnyValue thisVal)
|
|
121
|
+
{
|
|
122
|
+
if constexpr (std::is_same_v<T, AnyValue>) {
|
|
123
|
+
auto proto_it = IteratorPrototypes::get(key);
|
|
124
|
+
if (proto_it.has_value()) {
|
|
125
|
+
auto proto_value = proto_it.value();
|
|
126
|
+
if (proto_value.is_accessor_descriptor()) return AnyValue::resolve_property_for_write(proto_value, thisVal, value, key);
|
|
127
|
+
if (proto_value.is_data_descriptor() && !proto_value.as_data_descriptor()->writable) return AnyValue::resolve_property_for_write(proto_value, thisVal, value, key);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
auto it = props.find(key);
|
|
131
|
+
if (it != props.end()) return jspp::AnyValue::resolve_property_for_write(it->second, thisVal, value, key);
|
|
132
|
+
else { props[key] = value; return value; }
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
template <typename T>
|
|
136
|
+
AnyValue JsIterator<T>::set_symbol_property(const AnyValue &key, AnyValue value, AnyValue thisVal)
|
|
137
|
+
{
|
|
138
|
+
auto it = symbol_props.find(key);
|
|
139
|
+
if (it != symbol_props.end()) return AnyValue::resolve_property_for_write(it->second, thisVal, value, key.to_std_string());
|
|
140
|
+
else { symbol_props[key] = value; return value; }
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Explicit template instantiation
|
|
144
|
+
template class JsIterator<AnyValue>;
|
|
145
|
+
|
|
146
|
+
namespace IteratorPrototypes {
|
|
147
|
+
|
|
148
|
+
AnyValue &get_toString_fn()
|
|
149
|
+
{
|
|
150
|
+
static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue>) -> AnyValue
|
|
151
|
+
{ return AnyValue::make_string(thisVal.as_iterator()->to_std_string()); },
|
|
152
|
+
"toString");
|
|
153
|
+
return fn;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
AnyValue &get_iterator_fn()
|
|
157
|
+
{
|
|
158
|
+
static AnyValue fn = AnyValue::make_generator([](const AnyValue &thisVal, std::span<const AnyValue>) -> AnyValue
|
|
159
|
+
{ return thisVal; },
|
|
160
|
+
"Symbol.iterator");
|
|
161
|
+
return fn;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
AnyValue &get_next_fn()
|
|
165
|
+
{
|
|
166
|
+
static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
|
|
167
|
+
{
|
|
168
|
+
AnyValue val = args.empty() ? Constants::UNDEFINED : args[0];
|
|
169
|
+
auto res = thisVal.as_iterator()->next(val);
|
|
170
|
+
return AnyValue::make_object({{"value", res.value.value_or(Constants::UNDEFINED)}, {"done", AnyValue::make_boolean(res.done)}}); },
|
|
171
|
+
"next");
|
|
172
|
+
return fn;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
AnyValue &get_return_fn()
|
|
176
|
+
{
|
|
177
|
+
static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
|
|
178
|
+
{
|
|
179
|
+
AnyValue val = args.empty() ? Constants::UNDEFINED : args[0];
|
|
180
|
+
auto res = thisVal.as_iterator()->return_(val);
|
|
181
|
+
return AnyValue::make_object({{"value", res.value.value_or(Constants::UNDEFINED)}, {"done", AnyValue::make_boolean(res.done)}}); },
|
|
182
|
+
"return");
|
|
183
|
+
return fn;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
AnyValue &get_throw_fn()
|
|
187
|
+
{
|
|
188
|
+
static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
|
|
189
|
+
{
|
|
190
|
+
AnyValue err = args.empty() ? Constants::UNDEFINED : args[0];
|
|
191
|
+
auto res = thisVal.as_iterator()->throw_(err);
|
|
192
|
+
return AnyValue::make_object({{"value", res.value.value_or(Constants::UNDEFINED)}, {"done", AnyValue::make_boolean(res.done)}}); },
|
|
193
|
+
"throw");
|
|
194
|
+
return fn;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
AnyValue &get_toArray_fn()
|
|
198
|
+
{
|
|
199
|
+
static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue>) -> AnyValue
|
|
200
|
+
{ return AnyValue::make_array(thisVal.as_iterator()->to_vector()); },
|
|
201
|
+
"toArray");
|
|
202
|
+
return fn;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
AnyValue &get_drop_fn()
|
|
206
|
+
{
|
|
207
|
+
static AnyValue fn = AnyValue::make_generator([](AnyValue thisVal, std::vector<AnyValue> args) -> JsIterator<AnyValue>
|
|
208
|
+
{
|
|
209
|
+
auto self = thisVal.as_iterator();
|
|
210
|
+
size_t skip_count = 0;
|
|
211
|
+
if (!args.empty()) {
|
|
212
|
+
skip_count = static_cast<size_t>(args[0].as_double());
|
|
213
|
+
}
|
|
214
|
+
size_t skipped = 0;
|
|
215
|
+
while (true)
|
|
216
|
+
{
|
|
217
|
+
auto next_res = self->next();
|
|
218
|
+
if (next_res.done) { break; }
|
|
219
|
+
if (skipped < skip_count)
|
|
220
|
+
{
|
|
221
|
+
skipped++;
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
co_yield next_res.value.value_or(Constants::UNDEFINED);
|
|
225
|
+
}
|
|
226
|
+
co_return jspp::Constants::UNDEFINED; },
|
|
227
|
+
"drop");
|
|
228
|
+
return fn;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
AnyValue &get_take_fn()
|
|
232
|
+
{
|
|
233
|
+
static AnyValue fn = AnyValue::make_generator([](AnyValue thisVal, std::vector<AnyValue> args) -> JsIterator<AnyValue>
|
|
234
|
+
{
|
|
235
|
+
auto self = thisVal.as_iterator();
|
|
236
|
+
size_t take_count = 0;
|
|
237
|
+
if (!args.empty()) {
|
|
238
|
+
take_count = static_cast<size_t>(args[0].as_double());
|
|
239
|
+
}
|
|
240
|
+
size_t taken = 0;
|
|
241
|
+
while (true)
|
|
242
|
+
{
|
|
243
|
+
auto next_res = self->next();
|
|
244
|
+
if (next_res.done) { break; }
|
|
245
|
+
if (taken < take_count)
|
|
246
|
+
{
|
|
247
|
+
taken++;
|
|
248
|
+
co_yield next_res.value.value_or(Constants::UNDEFINED);
|
|
249
|
+
}
|
|
250
|
+
if (taken >= take_count)
|
|
251
|
+
{
|
|
252
|
+
self->return_();
|
|
253
|
+
break;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
co_return jspp::Constants::UNDEFINED; },
|
|
257
|
+
"take");
|
|
258
|
+
return fn;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
AnyValue &get_some_fn()
|
|
262
|
+
{
|
|
263
|
+
static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
|
|
264
|
+
{
|
|
265
|
+
auto self = thisVal.as_iterator();
|
|
266
|
+
if (args.empty() || !args[0].is_function()) throw Exception::make_exception("callback is not a function", "TypeError");
|
|
267
|
+
auto callback = args[0].as_function();
|
|
268
|
+
while (true)
|
|
269
|
+
{
|
|
270
|
+
auto next_res = self->next();
|
|
271
|
+
if (next_res.done) { break; }
|
|
272
|
+
if (is_truthy(callback->call(thisVal, std::span<const AnyValue>((const jspp::AnyValue[]){next_res.value.value_or(Constants::UNDEFINED)}, 1))))
|
|
273
|
+
{
|
|
274
|
+
self->return_();
|
|
275
|
+
return Constants::TRUE;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
return jspp::Constants::FALSE; },
|
|
279
|
+
"some");
|
|
280
|
+
return fn;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
std::optional<AnyValue> get(const std::string &key)
|
|
284
|
+
{
|
|
285
|
+
if (key == "toString") return get_toString_fn();
|
|
286
|
+
if (key == "next") return get_next_fn();
|
|
287
|
+
if (key == "return") return get_return_fn();
|
|
288
|
+
if (key == "throw") return get_throw_fn();
|
|
289
|
+
if (key == "toArray") return get_toArray_fn();
|
|
290
|
+
if (key == "drop") return get_drop_fn();
|
|
291
|
+
if (key == "take") return get_take_fn();
|
|
292
|
+
if (key == "some") return get_some_fn();
|
|
293
|
+
return std::nullopt;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
std::optional<AnyValue> get(const AnyValue &key)
|
|
297
|
+
{
|
|
298
|
+
if (key.is_string())
|
|
299
|
+
return get(key.as_string()->value);
|
|
300
|
+
|
|
301
|
+
if (key == AnyValue::from_symbol(WellKnownSymbols::toStringTag)) return get_toString_fn();
|
|
302
|
+
if (key == AnyValue::from_symbol(WellKnownSymbols::iterator)) return get_iterator_fn();
|
|
303
|
+
|
|
304
|
+
return std::nullopt;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
} // namespace IteratorPrototypes
|
|
308
|
+
|
|
309
|
+
} // namespace jspp
|
|
@@ -3,16 +3,12 @@
|
|
|
3
3
|
#include "types.hpp"
|
|
4
4
|
#include <coroutine>
|
|
5
5
|
#include <optional>
|
|
6
|
-
#include <
|
|
7
|
-
#include <utility>
|
|
8
|
-
#include <exception>
|
|
6
|
+
#include <vector>
|
|
9
7
|
|
|
10
8
|
namespace jspp
|
|
11
9
|
{
|
|
12
|
-
// Forward declaration of AnyValue
|
|
13
10
|
class AnyValue;
|
|
14
11
|
|
|
15
|
-
// Special exception to signal a return from a generator
|
|
16
12
|
struct GeneratorReturnException {};
|
|
17
13
|
|
|
18
14
|
template <typename T>
|
|
@@ -36,91 +32,64 @@ namespace jspp
|
|
|
36
32
|
std::exception_ptr pending_exception = nullptr;
|
|
37
33
|
bool pending_return = false;
|
|
38
34
|
|
|
39
|
-
JsIterator get_return_object()
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
std::coroutine_handle<promise_type>::from_promise(*this)};
|
|
43
|
-
}
|
|
35
|
+
JsIterator get_return_object();
|
|
36
|
+
std::suspend_always initial_suspend() noexcept;
|
|
37
|
+
std::suspend_always final_suspend() noexcept;
|
|
44
38
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
39
|
+
struct Awaiter
|
|
40
|
+
{
|
|
41
|
+
promise_type &p;
|
|
42
|
+
bool await_ready() { return false; }
|
|
43
|
+
void await_suspend(std::coroutine_handle<promise_type>) {}
|
|
44
|
+
T await_resume() {
|
|
45
|
+
if (p.pending_exception) {
|
|
46
|
+
auto ex = p.pending_exception;
|
|
47
|
+
p.pending_exception = nullptr;
|
|
48
|
+
std::rethrow_exception(ex);
|
|
49
|
+
}
|
|
50
|
+
if (p.pending_return) {
|
|
51
|
+
p.pending_return = false;
|
|
52
|
+
throw GeneratorReturnException{};
|
|
53
|
+
}
|
|
54
|
+
return p.input_value;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
50
57
|
|
|
51
|
-
// Handle co_yield
|
|
52
58
|
template <typename From>
|
|
53
|
-
|
|
54
|
-
{
|
|
59
|
+
Awaiter yield_value(From &&from) {
|
|
55
60
|
current_value = std::forward<From>(from);
|
|
56
|
-
struct Awaiter
|
|
57
|
-
{
|
|
58
|
-
promise_type &p;
|
|
59
|
-
bool await_ready() { return false; }
|
|
60
|
-
void await_suspend(std::coroutine_handle<promise_type>) {}
|
|
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
|
-
}
|
|
73
|
-
};
|
|
74
61
|
return Awaiter{*this};
|
|
75
62
|
}
|
|
76
63
|
|
|
77
|
-
// Handle co_return
|
|
78
|
-
// This replaces return_void.
|
|
79
|
-
// It captures the final value and moves to final_suspend (implicit).
|
|
80
64
|
template <typename From>
|
|
81
|
-
void return_value(From &&from)
|
|
82
|
-
{
|
|
65
|
+
void return_value(From &&from) {
|
|
83
66
|
current_value = std::forward<From>(from);
|
|
84
67
|
}
|
|
85
68
|
|
|
86
|
-
void unhandled_exception()
|
|
87
|
-
{
|
|
88
|
-
try {
|
|
89
|
-
throw;
|
|
90
|
-
} catch (const GeneratorReturnException&) {
|
|
91
|
-
// Handled return
|
|
92
|
-
} catch (...) {
|
|
93
|
-
exception_ = std::current_exception();
|
|
94
|
-
}
|
|
95
|
-
}
|
|
69
|
+
void unhandled_exception();
|
|
96
70
|
};
|
|
97
71
|
|
|
98
72
|
using handle_type = std::coroutine_handle<promise_type>;
|
|
99
73
|
handle_type handle;
|
|
100
74
|
|
|
101
|
-
explicit JsIterator(handle_type h)
|
|
102
|
-
JsIterator(JsIterator &&other) noexcept
|
|
103
|
-
: handle(std::exchange(other.handle, nullptr)),
|
|
104
|
-
props(std::move(other.props)) {}
|
|
105
|
-
|
|
106
|
-
// Delete copy constructor/assignment to ensure unique ownership of the handle
|
|
75
|
+
explicit JsIterator(handle_type h);
|
|
76
|
+
JsIterator(JsIterator &&other) noexcept;
|
|
107
77
|
JsIterator(const JsIterator &) = delete;
|
|
108
78
|
JsIterator &operator=(const JsIterator &) = delete;
|
|
109
|
-
|
|
110
|
-
~JsIterator()
|
|
111
|
-
{
|
|
112
|
-
if (handle)
|
|
113
|
-
handle.destroy();
|
|
114
|
-
}
|
|
79
|
+
~JsIterator();
|
|
115
80
|
|
|
116
81
|
std::unordered_map<std::string, AnyValue> props;
|
|
82
|
+
std::map<AnyValue, AnyValue> symbol_props;
|
|
117
83
|
|
|
118
84
|
std::string to_std_string() const;
|
|
119
85
|
NextResult next(const T &val = T());
|
|
120
86
|
NextResult return_(const T &val = T());
|
|
121
87
|
NextResult throw_(const AnyValue &err);
|
|
122
88
|
std::vector<T> to_vector();
|
|
89
|
+
bool has_symbol_property(const AnyValue &key) const;
|
|
123
90
|
AnyValue get_property(const std::string &key, AnyValue thisVal);
|
|
91
|
+
AnyValue get_symbol_property(const AnyValue &key, AnyValue thisVal);
|
|
124
92
|
AnyValue set_property(const std::string &key, AnyValue value, AnyValue thisVal);
|
|
93
|
+
AnyValue set_symbol_property(const AnyValue &key, AnyValue value, AnyValue thisVal);
|
|
125
94
|
};
|
|
126
|
-
}
|
|
95
|
+
}
|