@ugo-studio/jspp 0.3.0 → 0.3.2
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/cli/args.js +22 -0
- package/dist/cli/compiler.js +53 -0
- package/dist/cli/index.js +43 -107
- package/dist/cli/pch.js +71 -0
- package/dist/cli/runner.js +23 -0
- package/dist/cli/spinner.js +27 -11
- package/dist/cli/transpiler.js +20 -0
- package/dist/cli/utils.js +59 -0
- package/dist/cli/wasm.js +70 -0
- package/dist/index.js +17 -6
- package/dist/{analysis → interpreter/analysis}/scope.js +38 -3
- package/dist/{analysis → interpreter/analysis}/typeAnalyzer.js +563 -28
- package/dist/{core → interpreter/core}/codegen/class-handlers.js +1 -1
- package/dist/{core → interpreter/core}/codegen/control-flow-handlers.js +12 -11
- package/dist/{core → interpreter/core}/codegen/declaration-handlers.js +28 -9
- package/dist/{core → interpreter/core}/codegen/destructuring-handlers.js +9 -4
- package/dist/{core → interpreter/core}/codegen/expression-handlers.js +82 -88
- package/dist/{core → interpreter/core}/codegen/function-handlers.js +159 -46
- package/dist/{core → interpreter/core}/codegen/helpers.js +170 -25
- package/dist/interpreter/core/codegen/index.js +156 -0
- package/dist/{core → interpreter/core}/codegen/literal-handlers.js +9 -0
- package/dist/{core → interpreter/core}/codegen/statement-handlers.js +47 -7
- package/package.json +6 -4
- package/scripts/precompile-headers.ts +293 -50
- package/scripts/setup-compiler.ts +63 -63
- package/scripts/setup-emsdk.ts +114 -0
- package/src/prelude/any_value.cpp +888 -0
- package/src/prelude/any_value.hpp +29 -24
- 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} +13 -17
- package/src/prelude/library/array.cpp +191 -0
- package/src/prelude/library/array.hpp +5 -178
- package/src/prelude/library/boolean.cpp +30 -0
- package/src/prelude/library/boolean.hpp +14 -0
- 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 +98 -0
- package/src/prelude/library/global.hpp +12 -28
- package/src/prelude/library/global_usings.hpp +15 -0
- package/src/prelude/library/math.cpp +261 -0
- package/src/prelude/library/math.hpp +8 -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 +26 -20
- package/src/prelude/utils/access.hpp +123 -32
- package/src/prelude/utils/assignment_operators.hpp +119 -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/log_any_value.hpp +1 -1
- package/src/prelude/utils/log_any_value/object.hpp +60 -3
- package/src/prelude/utils/log_any_value/primitives.hpp +1 -1
- package/src/prelude/utils/operators.hpp +109 -94
- package/src/prelude/utils/operators_native.hpp +349 -0
- 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/boolean.cpp +64 -0
- 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 +221 -0
- package/src/prelude/values/object.cpp +200 -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/boolean.hpp +24 -0
- 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 +30 -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/dist/cli/file-utils.js +0 -20
- package/dist/cli-utils/args.js +0 -59
- package/dist/cli-utils/colors.js +0 -9
- package/dist/cli-utils/file-utils.js +0 -20
- package/dist/cli-utils/spinner.js +0 -55
- package/dist/cli.js +0 -153
- package/dist/core/codegen/index.js +0 -86
- 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/utils/operators_primitive.hpp +0 -337
- 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
- /package/dist/{ast → interpreter/ast}/symbols.js +0 -0
- /package/dist/{ast → interpreter/ast}/types.js +0 -0
- /package/dist/{core → interpreter/core}/codegen/visitor.js +0 -0
- /package/dist/{core → interpreter/core}/constants.js +0 -0
- /package/dist/{core → interpreter/core}/error.js +0 -0
- /package/dist/{core → interpreter/core}/parser.js +0 -0
- /package/dist/{core → interpreter/core}/traverser.js +0 -0
|
@@ -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
|
|
@@ -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
|