@ugo-studio/jspp 0.1.9 → 0.2.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/README.md +1 -1
- package/dist/cli-utils/args.js +2 -0
- package/dist/cli.js +4 -4
- package/package.json +1 -1
- package/src/prelude/any_value.hpp +185 -391
- package/src/prelude/any_value_access.hpp +170 -190
- package/src/prelude/any_value_defines.hpp +12 -12
- package/src/prelude/any_value_helpers.hpp +208 -26
- package/src/prelude/exception.hpp +27 -31
- package/src/prelude/exception_helpers.hpp +53 -49
- package/src/prelude/index.hpp +4 -4
- package/src/prelude/library/array.hpp +4 -9
- package/src/prelude/library/console.hpp +112 -112
- package/src/prelude/library/error.hpp +8 -8
- package/src/prelude/library/math.hpp +3 -3
- package/src/prelude/library/object.hpp +12 -24
- package/src/prelude/library/promise.hpp +1 -1
- package/src/prelude/library/symbol.hpp +1 -1
- package/src/prelude/library/timer.hpp +3 -3
- package/src/prelude/types.hpp +178 -130
- package/src/prelude/utils/access.hpp +338 -378
- 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/operators.hpp +20 -82
- package/src/prelude/utils/well_known_symbols.hpp +14 -15
- package/src/prelude/values/array.hpp +5 -3
- package/src/prelude/values/async_iterator.hpp +3 -1
- package/src/prelude/values/descriptors.hpp +15 -3
- package/src/prelude/values/function.hpp +5 -9
- package/src/prelude/values/helpers/array.hpp +208 -219
- package/src/prelude/values/helpers/async_iterator.hpp +7 -11
- package/src/prelude/values/helpers/function.hpp +12 -17
- package/src/prelude/values/helpers/iterator.hpp +108 -107
- package/src/prelude/values/helpers/object.hpp +104 -109
- package/src/prelude/values/helpers/promise.hpp +185 -119
- package/src/prelude/values/helpers/string.hpp +7 -10
- package/src/prelude/values/helpers/symbol.hpp +21 -23
- package/src/prelude/values/iterator.hpp +4 -1
- package/src/prelude/values/object.hpp +6 -4
- package/src/prelude/values/promise.hpp +5 -2
- package/src/prelude/values/prototypes/array.hpp +22 -22
- package/src/prelude/values/prototypes/async_iterator.hpp +3 -10
- package/src/prelude/values/prototypes/iterator.hpp +51 -58
- package/src/prelude/values/prototypes/promise.hpp +32 -28
- package/src/prelude/values/prototypes/string.hpp +5 -5
- package/src/prelude/values/prototypes/symbol.hpp +1 -1
- package/src/prelude/values/string.hpp +3 -1
- package/src/prelude/values/symbol.hpp +101 -102
|
@@ -3,11 +3,197 @@
|
|
|
3
3
|
#include "types.hpp"
|
|
4
4
|
#include "any_value.hpp"
|
|
5
5
|
#include "values/string.hpp"
|
|
6
|
+
#include "values/object.hpp"
|
|
7
|
+
#include "values/array.hpp"
|
|
8
|
+
#include "values/function.hpp"
|
|
9
|
+
#include "values/iterator.hpp"
|
|
10
|
+
#include "values/async_iterator.hpp"
|
|
11
|
+
#include "values/promise.hpp"
|
|
12
|
+
#include "values/symbol.hpp"
|
|
13
|
+
#include "values/descriptors.hpp"
|
|
6
14
|
#include "exception.hpp"
|
|
7
15
|
|
|
8
16
|
namespace jspp
|
|
9
17
|
{
|
|
10
|
-
|
|
18
|
+
// --- AnyValue GETTERS ---
|
|
19
|
+
inline JsString* AnyValue::as_string() const noexcept { return static_cast<JsString*>(get_ptr()); }
|
|
20
|
+
inline JsObject* AnyValue::as_object() const noexcept { return static_cast<JsObject*>(get_ptr()); }
|
|
21
|
+
inline JsArray* AnyValue::as_array() const noexcept { return static_cast<JsArray*>(get_ptr()); }
|
|
22
|
+
inline JsFunction* AnyValue::as_function() const noexcept { return static_cast<JsFunction*>(get_ptr()); }
|
|
23
|
+
inline JsSymbol* AnyValue::as_symbol() const noexcept { return static_cast<JsSymbol*>(get_ptr()); }
|
|
24
|
+
inline JsPromise* AnyValue::as_promise() const noexcept { return static_cast<JsPromise*>(get_ptr()); }
|
|
25
|
+
inline JsIterator<AnyValue>* AnyValue::as_iterator() const noexcept { return static_cast<JsIterator<AnyValue>*>(get_ptr()); }
|
|
26
|
+
inline JsAsyncIterator<AnyValue>* AnyValue::as_async_iterator() const noexcept { return static_cast<JsAsyncIterator<AnyValue>*>(get_ptr()); }
|
|
27
|
+
inline DataDescriptor* AnyValue::as_data_descriptor() const noexcept { return static_cast<DataDescriptor*>(get_ptr()); }
|
|
28
|
+
inline AccessorDescriptor* AnyValue::as_accessor_descriptor() const noexcept { return static_cast<AccessorDescriptor*>(get_ptr()); }
|
|
29
|
+
|
|
30
|
+
inline bool AnyValue::is_generator() const noexcept { return is_function() && as_function()->is_generator; }
|
|
31
|
+
|
|
32
|
+
// --- AnyValue FACTORIES ---
|
|
33
|
+
inline AnyValue AnyValue::make_string(const std::string &raw_s) noexcept {
|
|
34
|
+
return from_ptr(new JsString(raw_s));
|
|
35
|
+
}
|
|
36
|
+
inline AnyValue AnyValue::make_object(std::initializer_list<std::pair<std::string, AnyValue>> props) noexcept {
|
|
37
|
+
return from_ptr(new JsObject(props, make_null()));
|
|
38
|
+
}
|
|
39
|
+
inline AnyValue AnyValue::make_object(const std::map<std::string, AnyValue> &props) noexcept {
|
|
40
|
+
return from_ptr(new JsObject(props, make_null()));
|
|
41
|
+
}
|
|
42
|
+
inline AnyValue AnyValue::make_object_with_proto(std::initializer_list<std::pair<std::string, AnyValue>> props, AnyValue proto) noexcept {
|
|
43
|
+
return from_ptr(new JsObject(props, proto));
|
|
44
|
+
}
|
|
45
|
+
inline AnyValue AnyValue::make_object_with_proto(const std::map<std::string, AnyValue> &props, AnyValue proto) noexcept {
|
|
46
|
+
return from_ptr(new JsObject(props, proto));
|
|
47
|
+
}
|
|
48
|
+
inline AnyValue AnyValue::make_array(std::span<const AnyValue> dense) noexcept {
|
|
49
|
+
std::vector<AnyValue> vec;
|
|
50
|
+
vec.reserve(dense.size());
|
|
51
|
+
for (const auto &item : dense) vec.push_back(item);
|
|
52
|
+
return from_ptr(new JsArray(std::move(vec)));
|
|
53
|
+
}
|
|
54
|
+
inline AnyValue AnyValue::make_array(const std::vector<AnyValue> &dense) noexcept {
|
|
55
|
+
return from_ptr(new JsArray(dense));
|
|
56
|
+
}
|
|
57
|
+
inline AnyValue AnyValue::make_array(std::vector<AnyValue> &&dense) noexcept {
|
|
58
|
+
return from_ptr(new JsArray(std::move(dense)));
|
|
59
|
+
}
|
|
60
|
+
inline AnyValue AnyValue::make_array_with_proto(std::span<const AnyValue> dense, AnyValue proto) noexcept {
|
|
61
|
+
auto arr = new JsArray();
|
|
62
|
+
arr->dense.reserve(dense.size());
|
|
63
|
+
for (const auto &item : dense) arr->dense.push_back(item);
|
|
64
|
+
arr->length = dense.size();
|
|
65
|
+
arr->proto = proto;
|
|
66
|
+
return from_ptr(arr);
|
|
67
|
+
}
|
|
68
|
+
inline AnyValue AnyValue::make_array_with_proto(const std::vector<AnyValue> &dense, AnyValue proto) noexcept {
|
|
69
|
+
auto arr = new JsArray(dense);
|
|
70
|
+
arr->proto = proto;
|
|
71
|
+
return from_ptr(arr);
|
|
72
|
+
}
|
|
73
|
+
inline AnyValue AnyValue::make_function(const JsFunctionCallable &call, const std::optional<std::string> &name, bool is_constructor) noexcept {
|
|
74
|
+
auto v = from_ptr(new JsFunction(call, name, {}, false, is_constructor));
|
|
75
|
+
auto proto = make_object({});
|
|
76
|
+
proto.define_data_property("constructor", AnyValue::make_data_descriptor(v, true, false, false));
|
|
77
|
+
v.define_data_property("prototype", AnyValue::make_data_descriptor(proto, false, false, false));
|
|
78
|
+
return v;
|
|
79
|
+
}
|
|
80
|
+
inline AnyValue AnyValue::make_class(const JsFunctionCallable &call, const std::optional<std::string> &name) noexcept {
|
|
81
|
+
auto v = from_ptr(new JsFunction(call, name, {}, true));
|
|
82
|
+
auto proto = make_object({});
|
|
83
|
+
proto.define_data_property("constructor", AnyValue::make_data_descriptor(v, true, false, false));
|
|
84
|
+
v.define_data_property("prototype", AnyValue::make_data_descriptor(proto, false, false, false));
|
|
85
|
+
return v;
|
|
86
|
+
}
|
|
87
|
+
inline AnyValue AnyValue::make_generator(const JsFunctionCallable &call, const std::optional<std::string> &name) noexcept {
|
|
88
|
+
auto v = from_ptr(new JsFunction(call, true, name));
|
|
89
|
+
auto proto = make_object({});
|
|
90
|
+
proto.define_data_property("constructor", AnyValue::make_data_descriptor(v, true, false, false));
|
|
91
|
+
v.define_data_property("prototype", AnyValue::make_data_descriptor(proto, false, false, false));
|
|
92
|
+
return v;
|
|
93
|
+
}
|
|
94
|
+
inline AnyValue AnyValue::make_async_function(const JsFunctionCallable &call, const std::optional<std::string> &name) noexcept {
|
|
95
|
+
auto v = from_ptr(new JsFunction(call, false, true, name));
|
|
96
|
+
auto proto = make_object({});
|
|
97
|
+
proto.define_data_property("constructor", AnyValue::make_data_descriptor(v, true, false, false));
|
|
98
|
+
v.define_data_property("prototype", AnyValue::make_data_descriptor(proto, false, false, false));
|
|
99
|
+
return v;
|
|
100
|
+
}
|
|
101
|
+
inline AnyValue AnyValue::make_async_generator(const JsFunctionCallable &call, const std::optional<std::string> &name) noexcept {
|
|
102
|
+
auto v = from_ptr(new JsFunction(call, true, true, name));
|
|
103
|
+
auto proto = make_object({});
|
|
104
|
+
proto.define_data_property("constructor", AnyValue::make_data_descriptor(v, true, false, false));
|
|
105
|
+
v.define_data_property("prototype", AnyValue::make_data_descriptor(proto, false, false, false));
|
|
106
|
+
return v;
|
|
107
|
+
}
|
|
108
|
+
inline AnyValue AnyValue::make_symbol(const std::string &description) noexcept {
|
|
109
|
+
return from_ptr(new JsSymbol(description));
|
|
110
|
+
}
|
|
111
|
+
inline AnyValue AnyValue::make_promise(const JsPromise &promise) noexcept {
|
|
112
|
+
auto p = new JsPromise();
|
|
113
|
+
*p = promise;
|
|
114
|
+
return from_ptr(p);
|
|
115
|
+
}
|
|
116
|
+
inline AnyValue AnyValue::make_data_descriptor(AnyValue value, bool writable, bool enumerable, bool configurable) noexcept {
|
|
117
|
+
return from_ptr(new DataDescriptor(value, writable, enumerable, configurable));
|
|
118
|
+
}
|
|
119
|
+
inline AnyValue AnyValue::make_accessor_descriptor(const std::optional<std::function<AnyValue(const AnyValue &, std::span<const AnyValue>)>> &get,
|
|
120
|
+
const std::optional<std::function<AnyValue(const AnyValue &, std::span<const AnyValue>)>> &set,
|
|
121
|
+
bool enumerable,
|
|
122
|
+
bool configurable) noexcept {
|
|
123
|
+
return from_ptr(new AccessorDescriptor(get, set, enumerable, configurable));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
inline AnyValue AnyValue::from_symbol(JsSymbol *sym) noexcept {
|
|
127
|
+
return from_ptr(sym);
|
|
128
|
+
}
|
|
129
|
+
inline AnyValue AnyValue::from_string(JsString *str) noexcept {
|
|
130
|
+
return from_ptr(str);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
inline AnyValue AnyValue::from_iterator(JsIterator<AnyValue> &&iterator) noexcept {
|
|
134
|
+
auto it = new JsIterator<AnyValue>(std::move(iterator));
|
|
135
|
+
return from_ptr(it);
|
|
136
|
+
}
|
|
137
|
+
inline AnyValue AnyValue::from_iterator_ref(JsIterator<AnyValue> *iterator) noexcept {
|
|
138
|
+
return from_ptr(iterator);
|
|
139
|
+
}
|
|
140
|
+
inline AnyValue AnyValue::from_async_iterator(JsAsyncIterator<AnyValue> &&iterator) noexcept {
|
|
141
|
+
auto it = new JsAsyncIterator<AnyValue>(std::move(iterator));
|
|
142
|
+
return from_ptr(it);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
inline AnyValue AnyValue::resolve_property_for_read(const AnyValue &val, const AnyValue &thisVal, const std::string &propName) noexcept
|
|
146
|
+
{
|
|
147
|
+
if (val.is_data_descriptor())
|
|
148
|
+
{
|
|
149
|
+
return val.as_data_descriptor()->value;
|
|
150
|
+
}
|
|
151
|
+
if (val.is_accessor_descriptor())
|
|
152
|
+
{
|
|
153
|
+
auto accessor = val.as_accessor_descriptor();
|
|
154
|
+
if (accessor->get.has_value())
|
|
155
|
+
return accessor->get.value()(thisVal, std::span<const AnyValue>{});
|
|
156
|
+
else
|
|
157
|
+
{
|
|
158
|
+
return Constants::UNDEFINED;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return val;
|
|
162
|
+
}
|
|
163
|
+
inline AnyValue AnyValue::resolve_property_for_write(AnyValue &val, const AnyValue &thisVal, const AnyValue &new_val, const std::string &propName)
|
|
164
|
+
{
|
|
165
|
+
if (val.is_data_descriptor())
|
|
166
|
+
{
|
|
167
|
+
auto data = val.as_data_descriptor();
|
|
168
|
+
if (data->writable)
|
|
169
|
+
{
|
|
170
|
+
data->value = new_val;
|
|
171
|
+
return new_val;
|
|
172
|
+
}
|
|
173
|
+
else
|
|
174
|
+
{
|
|
175
|
+
throw Exception::make_exception("Cannot assign to read only property '" + propName + "' of object '#<Object>'", "TypeError");
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
if (val.is_accessor_descriptor())
|
|
179
|
+
{
|
|
180
|
+
auto accessor = val.as_accessor_descriptor();
|
|
181
|
+
if (accessor->set.has_value())
|
|
182
|
+
{
|
|
183
|
+
const AnyValue args[] = {new_val};
|
|
184
|
+
accessor->set.value()(thisVal, std::span<const AnyValue>(args, 1));
|
|
185
|
+
return new_val;
|
|
186
|
+
}
|
|
187
|
+
else
|
|
188
|
+
{
|
|
189
|
+
throw Exception::make_exception("Cannot set property of #<Object> which has only a getter", "TypeError");
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
val = new_val;
|
|
193
|
+
return new_val;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
inline std::string AnyValue::to_std_string() const
|
|
11
197
|
{
|
|
12
198
|
switch (get_type())
|
|
13
199
|
{
|
|
@@ -16,35 +202,35 @@ namespace jspp
|
|
|
16
202
|
case JsType::Null:
|
|
17
203
|
return "null";
|
|
18
204
|
case JsType::Boolean:
|
|
19
|
-
return
|
|
205
|
+
return as_boolean() ? "true" : "false";
|
|
20
206
|
case JsType::String:
|
|
21
|
-
return
|
|
207
|
+
return as_string()->to_std_string();
|
|
22
208
|
case JsType::Object:
|
|
23
|
-
return
|
|
209
|
+
return as_object()->to_std_string();
|
|
24
210
|
case JsType::Array:
|
|
25
|
-
return
|
|
211
|
+
return as_array()->to_std_string();
|
|
26
212
|
case JsType::Function:
|
|
27
|
-
return
|
|
213
|
+
return as_function()->to_std_string();
|
|
28
214
|
case JsType::Iterator:
|
|
29
|
-
return
|
|
215
|
+
return as_iterator()->to_std_string();
|
|
30
216
|
case JsType::AsyncIterator:
|
|
31
|
-
return
|
|
217
|
+
return as_async_iterator()->to_std_string();
|
|
32
218
|
case JsType::Promise:
|
|
33
|
-
return
|
|
219
|
+
return as_promise()->to_std_string();
|
|
34
220
|
case JsType::Symbol:
|
|
35
|
-
return
|
|
221
|
+
return as_symbol()->to_std_string();
|
|
36
222
|
case JsType::DataDescriptor:
|
|
37
|
-
return
|
|
223
|
+
return as_data_descriptor()->value.to_std_string();
|
|
38
224
|
case JsType::AccessorDescriptor:
|
|
39
225
|
{
|
|
40
|
-
if (
|
|
41
|
-
return
|
|
226
|
+
if (as_accessor_descriptor()->get.has_value())
|
|
227
|
+
return as_accessor_descriptor()->get.value()(*this, {}).to_std_string();
|
|
42
228
|
else
|
|
43
229
|
return "undefined";
|
|
44
230
|
}
|
|
45
231
|
case JsType::Number:
|
|
46
232
|
{
|
|
47
|
-
double num =
|
|
233
|
+
double num = as_double();
|
|
48
234
|
if (std::isnan(num))
|
|
49
235
|
{
|
|
50
236
|
return "NaN";
|
|
@@ -75,19 +261,19 @@ namespace jspp
|
|
|
75
261
|
}
|
|
76
262
|
}
|
|
77
263
|
|
|
78
|
-
void AnyValue::set_prototype(const AnyValue &proto)
|
|
264
|
+
inline void AnyValue::set_prototype(const AnyValue &proto)
|
|
79
265
|
{
|
|
80
266
|
if (is_object())
|
|
81
267
|
{
|
|
82
|
-
|
|
268
|
+
as_object()->proto = proto;
|
|
83
269
|
}
|
|
84
270
|
else if (is_array())
|
|
85
271
|
{
|
|
86
|
-
|
|
272
|
+
as_array()->proto = proto;
|
|
87
273
|
}
|
|
88
274
|
else if (is_function())
|
|
89
275
|
{
|
|
90
|
-
|
|
276
|
+
as_function()->proto = proto;
|
|
91
277
|
}
|
|
92
278
|
else if (is_uninitialized())
|
|
93
279
|
{
|
|
@@ -96,28 +282,25 @@ namespace jspp
|
|
|
96
282
|
}
|
|
97
283
|
|
|
98
284
|
// AnyValue::call implementation
|
|
99
|
-
|
|
285
|
+
inline AnyValue AnyValue::call(const AnyValue &thisVal, std::span<const AnyValue> args, const std::optional<std::string> &expr) const
|
|
100
286
|
{
|
|
101
287
|
if (!is_function())
|
|
102
288
|
{
|
|
103
289
|
throw Exception::make_exception(expr.value_or(to_std_string()) + " is not a function", "TypeError");
|
|
104
290
|
}
|
|
105
|
-
return as_function()->call(thisVal, args);
|
|
291
|
+
return as_function()->call(thisVal, args);
|
|
106
292
|
}
|
|
107
293
|
|
|
108
294
|
// AnyValue::construct implementation
|
|
109
|
-
|
|
295
|
+
inline AnyValue AnyValue::construct(std::span<const AnyValue> args, const std::optional<std::string> &name) const
|
|
110
296
|
{
|
|
111
297
|
if (!is_function() || !as_function()->is_constructor)
|
|
112
298
|
{
|
|
113
|
-
// std::cerr << "Construct fail: " << name.value_or(to_std_string()) << " is_function=" << is_function() << " is_constructor=" << (is_function() ? as_function()->is_constructor : false) << std::endl;
|
|
114
299
|
throw Exception::make_exception(name.value_or(to_std_string()) + " is not a constructor", "TypeError");
|
|
115
300
|
}
|
|
116
301
|
|
|
117
302
|
// 1. Get prototype
|
|
118
303
|
AnyValue proto = get_own_property("prototype");
|
|
119
|
-
// If prototype is not an object, default to a plain object (which ideally inherits from Object.prototype)
|
|
120
|
-
// Here we just make a plain object.
|
|
121
304
|
if (!proto.is_object())
|
|
122
305
|
{
|
|
123
306
|
proto = AnyValue::make_object({});
|
|
@@ -127,7 +310,6 @@ namespace jspp
|
|
|
127
310
|
AnyValue instance = AnyValue::make_object_with_proto({}, proto);
|
|
128
311
|
|
|
129
312
|
// 3. Call function
|
|
130
|
-
// We pass 'instance' as 'this'
|
|
131
313
|
AnyValue result = call(instance, args);
|
|
132
314
|
|
|
133
315
|
// 4. Return result if object, else instance
|
|
@@ -138,4 +320,4 @@ namespace jspp
|
|
|
138
320
|
return instance;
|
|
139
321
|
}
|
|
140
322
|
|
|
141
|
-
}
|
|
323
|
+
}
|
|
@@ -1,32 +1,28 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
#include
|
|
5
|
-
#include "
|
|
6
|
-
|
|
7
|
-
namespace jspp
|
|
8
|
-
{
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
explicit Exception(
|
|
16
|
-
: data(std::move(
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
static
|
|
24
|
-
static AnyValue
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
static AnyValue throw_uninitialized_reference(const std::string &var_name);
|
|
29
|
-
static AnyValue throw_immutable_assignment();
|
|
30
|
-
static AnyValue throw_invalid_return_statement();
|
|
31
|
-
};
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <exception>
|
|
4
|
+
#include "types.hpp"
|
|
5
|
+
#include "any_value.hpp"
|
|
6
|
+
|
|
7
|
+
namespace jspp
|
|
8
|
+
{
|
|
9
|
+
struct Exception : std::exception
|
|
10
|
+
{
|
|
11
|
+
AnyValue data;
|
|
12
|
+
|
|
13
|
+
explicit Exception(const AnyValue &value)
|
|
14
|
+
: data(value) {}
|
|
15
|
+
explicit Exception(AnyValue &&value)
|
|
16
|
+
: data(std::move(value)) {}
|
|
17
|
+
|
|
18
|
+
const char *what() const noexcept override;
|
|
19
|
+
static Exception make_exception(const std::string &message, const std::string &name);
|
|
20
|
+
static AnyValue exception_to_any_value(const std::exception &ex);
|
|
21
|
+
|
|
22
|
+
// --- THROWERS
|
|
23
|
+
static AnyValue throw_unresolved_reference(const std::string &var_name);
|
|
24
|
+
static AnyValue throw_uninitialized_reference(const std::string &var_name);
|
|
25
|
+
static AnyValue throw_immutable_assignment();
|
|
26
|
+
static AnyValue throw_invalid_return_statement();
|
|
27
|
+
};
|
|
32
28
|
}
|
|
@@ -1,49 +1,53 @@
|
|
|
1
|
-
#pragma once
|
|
2
|
-
|
|
3
|
-
#include "types.hpp"
|
|
4
|
-
#include "exception.hpp"
|
|
5
|
-
#include "any_value.hpp"
|
|
6
|
-
|
|
7
|
-
const char *jspp::Exception::what() const noexcept
|
|
8
|
-
{
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
{
|
|
29
|
-
return
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
jspp::AnyValue jspp::Exception::
|
|
39
|
-
{
|
|
40
|
-
throw Exception::make_exception(
|
|
41
|
-
}
|
|
42
|
-
jspp::AnyValue jspp::Exception::
|
|
43
|
-
{
|
|
44
|
-
throw Exception::make_exception("
|
|
45
|
-
}
|
|
46
|
-
jspp::AnyValue jspp::Exception::
|
|
47
|
-
{
|
|
48
|
-
throw Exception::make_exception("
|
|
49
|
-
}
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "types.hpp"
|
|
4
|
+
#include "exception.hpp"
|
|
5
|
+
#include "any_value.hpp"
|
|
6
|
+
|
|
7
|
+
const char *jspp::Exception::what() const noexcept
|
|
8
|
+
{
|
|
9
|
+
// Note: this returns a pointer to a temporary string if we are not careful.
|
|
10
|
+
// In a real implementation, we might need to store the message string in the Exception object.
|
|
11
|
+
static thread_local std::string last_msg;
|
|
12
|
+
last_msg = data.to_std_string();
|
|
13
|
+
return last_msg.c_str();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
jspp::Exception jspp::Exception::make_exception(const std::string &message, const std::string &name)
|
|
17
|
+
{
|
|
18
|
+
// Use the global Error object to construct the exception
|
|
19
|
+
std::vector<AnyValue> args = {AnyValue::make_string(message)};
|
|
20
|
+
AnyValue errorObj = ::Error.construct(args, name);
|
|
21
|
+
errorObj.define_data_property("name", AnyValue::make_string(name), true, false, true);
|
|
22
|
+
|
|
23
|
+
return Exception(errorObj);
|
|
24
|
+
}
|
|
25
|
+
jspp::AnyValue jspp::Exception::exception_to_any_value(const std::exception &ex)
|
|
26
|
+
{
|
|
27
|
+
if (const jspp::Exception *err = dynamic_cast<const jspp::Exception *>(&ex))
|
|
28
|
+
{
|
|
29
|
+
return err->data;
|
|
30
|
+
}
|
|
31
|
+
else
|
|
32
|
+
{
|
|
33
|
+
return AnyValue::make_string(ex.what());
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// --- THROWERS
|
|
38
|
+
jspp::AnyValue jspp::Exception::throw_unresolved_reference(const std::string &var_name)
|
|
39
|
+
{
|
|
40
|
+
throw Exception::make_exception(var_name + " is not defined", "ReferenceError");
|
|
41
|
+
}
|
|
42
|
+
jspp::AnyValue jspp::Exception::throw_uninitialized_reference(const std::string &var_name)
|
|
43
|
+
{
|
|
44
|
+
throw Exception::make_exception("Cannot access '" + var_name + "' before initialization", "ReferenceError");
|
|
45
|
+
}
|
|
46
|
+
jspp::AnyValue jspp::Exception::throw_immutable_assignment()
|
|
47
|
+
{
|
|
48
|
+
throw Exception::make_exception("Assignment to constant variable.", "TypeError");
|
|
49
|
+
}
|
|
50
|
+
jspp::AnyValue jspp::Exception::throw_invalid_return_statement()
|
|
51
|
+
{
|
|
52
|
+
throw Exception::make_exception("Return statements are only valid inside functions.", "SyntaxError");
|
|
53
|
+
}
|
package/src/prelude/index.hpp
CHANGED
|
@@ -7,15 +7,15 @@
|
|
|
7
7
|
#include "values/shape.hpp"
|
|
8
8
|
#include "values/symbol.hpp"
|
|
9
9
|
#include "values/non_values.hpp"
|
|
10
|
-
#include "values/object.hpp"
|
|
11
|
-
#include "values/array.hpp"
|
|
12
|
-
#include "values/function.hpp"
|
|
13
10
|
#include "values/iterator.hpp"
|
|
14
11
|
#include "values/async_iterator.hpp"
|
|
15
|
-
#include "values/promise.hpp"
|
|
16
12
|
#include "values/string.hpp"
|
|
17
13
|
|
|
18
14
|
#include "any_value.hpp"
|
|
15
|
+
#include "values/object.hpp"
|
|
16
|
+
#include "values/array.hpp"
|
|
17
|
+
#include "values/function.hpp"
|
|
18
|
+
#include "values/promise.hpp"
|
|
19
19
|
#include "values/descriptors.hpp"
|
|
20
20
|
#include "any_value_helpers.hpp"
|
|
21
21
|
#include "any_value_access.hpp"
|
|
@@ -14,7 +14,7 @@ inline auto Array = jspp::AnyValue::make_class([](const jspp::AnyValue &thisVal,
|
|
|
14
14
|
}
|
|
15
15
|
auto arr = jspp::AnyValue::make_array(std::vector<jspp::AnyValue>());
|
|
16
16
|
arr.as_array()->length = static_cast<uint64_t>(len);
|
|
17
|
-
arr.as_array()->dense.resize(static_cast<size_t>(len), jspp::
|
|
17
|
+
arr.as_array()->dense.resize(static_cast<size_t>(len), jspp::Constants::UNINITIALIZED);
|
|
18
18
|
return arr;
|
|
19
19
|
}
|
|
20
20
|
std::vector<jspp::AnyValue> elements;
|
|
@@ -27,9 +27,6 @@ struct ArrayInit
|
|
|
27
27
|
{
|
|
28
28
|
ArrayInit()
|
|
29
29
|
{
|
|
30
|
-
// Set Array.prototype.proto to Object.prototype
|
|
31
|
-
// Array.get_own_property("prototype").set_prototype(::Object.get_own_property("prototype"));
|
|
32
|
-
|
|
33
30
|
// Array.isArray(value)
|
|
34
31
|
Array.define_data_property("isArray", jspp::AnyValue::make_function([](const jspp::AnyValue &, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
|
|
35
32
|
{
|
|
@@ -58,8 +55,6 @@ struct ArrayInit
|
|
|
58
55
|
|
|
59
56
|
std::vector<jspp::AnyValue> result;
|
|
60
57
|
|
|
61
|
-
// Check if iterable
|
|
62
|
-
// Simple check: does it have [Symbol.iterator]?
|
|
63
58
|
auto iteratorSym = jspp::WellKnownSymbols::iterator;
|
|
64
59
|
if (items.has_property(iteratorSym->key)) {
|
|
65
60
|
auto iter = jspp::Access::get_object_value_iterator(items, "Array.from source");
|
|
@@ -111,8 +106,8 @@ struct ArrayInit
|
|
|
111
106
|
std::vector<jspp::AnyValue> result;
|
|
112
107
|
|
|
113
108
|
bool isAsync = false;
|
|
114
|
-
jspp::AnyValue iter;
|
|
115
|
-
jspp::AnyValue nextFn;
|
|
109
|
+
jspp::AnyValue iter = jspp::Constants::UNDEFINED;
|
|
110
|
+
jspp::AnyValue nextFn = jspp::Constants::UNDEFINED;
|
|
116
111
|
|
|
117
112
|
if (items.has_property(jspp::WellKnownSymbols::asyncIterator->key)) {
|
|
118
113
|
auto method = items.get_property_with_receiver(jspp::WellKnownSymbols::asyncIterator->key, items);
|
|
@@ -187,4 +182,4 @@ struct ArrayInit
|
|
|
187
182
|
Array.define_getter(jspp::AnyValue::from_symbol(jspp::WellKnownSymbols::species), jspp::AnyValue::make_function([](const jspp::AnyValue &thisVal, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
|
|
188
183
|
{ return thisVal; }, "get [Symbol.species]"));
|
|
189
184
|
}
|
|
190
|
-
} arrayInit;
|
|
185
|
+
} arrayInit;
|