@ugo-studio/jspp 0.1.8 → 0.2.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/README.md +1 -1
- package/dist/cli-utils/args.js +2 -0
- package/dist/cli.js +1 -1
- package/package.json +3 -2
- package/scripts/precompile-headers.ts +110 -0
- package/scripts/setup-compiler.ts +63 -0
- 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 +9 -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
|
@@ -1,378 +1,338 @@
|
|
|
1
|
-
#pragma once
|
|
2
|
-
|
|
3
|
-
#include "types.hpp"
|
|
4
|
-
#include "well_known_symbols.hpp"
|
|
5
|
-
#include "values/function.hpp"
|
|
6
|
-
#include "values/symbol.hpp"
|
|
7
|
-
#include "exception.hpp"
|
|
8
|
-
#include "any_value.hpp"
|
|
9
|
-
#include <ranges>
|
|
10
|
-
|
|
11
|
-
namespace jspp
|
|
12
|
-
{
|
|
13
|
-
namespace Access
|
|
14
|
-
{
|
|
15
|
-
// Helper function to check for TDZ and deref heap-allocated variables
|
|
16
|
-
inline const AnyValue &deref_ptr(const std::shared_ptr<AnyValue> &var, const std::string &name)
|
|
17
|
-
{
|
|
18
|
-
if (
|
|
19
|
-
{
|
|
20
|
-
Exception::throw_uninitialized_reference(name);
|
|
21
|
-
}
|
|
22
|
-
return *var;
|
|
23
|
-
}
|
|
24
|
-
inline AnyValue &deref_ptr(std::shared_ptr<AnyValue> &var, const std::string &name)
|
|
25
|
-
{
|
|
26
|
-
if (
|
|
27
|
-
{
|
|
28
|
-
Exception::throw_uninitialized_reference(name);
|
|
29
|
-
}
|
|
30
|
-
return *var;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
case JsType::
|
|
75
|
-
return AnyValue::make_string("
|
|
76
|
-
case JsType::
|
|
77
|
-
return AnyValue::make_string("object");
|
|
78
|
-
case JsType::
|
|
79
|
-
return AnyValue::make_string("
|
|
80
|
-
|
|
81
|
-
return AnyValue::make_string("
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
if (
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
if (
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
if (obj.
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
}
|
|
340
|
-
if (obj.is_function())
|
|
341
|
-
{
|
|
342
|
-
auto ptr = obj.as_function();
|
|
343
|
-
ptr->props.erase(key.to_std_string());
|
|
344
|
-
return Constants::TRUE;
|
|
345
|
-
}
|
|
346
|
-
return Constants::TRUE;
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
inline AnyValue optional_get_property(const AnyValue &obj, const std::string &key)
|
|
350
|
-
{
|
|
351
|
-
if (obj.is_null() || obj.is_undefined())
|
|
352
|
-
return Constants::UNDEFINED;
|
|
353
|
-
return obj.get_own_property(key);
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
inline AnyValue optional_get_element(const AnyValue &obj, const AnyValue &key)
|
|
357
|
-
{
|
|
358
|
-
if (obj.is_null() || obj.is_undefined())
|
|
359
|
-
return Constants::UNDEFINED;
|
|
360
|
-
return obj.get_own_property(key);
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
inline AnyValue optional_get_element(const AnyValue &obj, const double &key)
|
|
364
|
-
{
|
|
365
|
-
if (obj.is_null() || obj.is_undefined())
|
|
366
|
-
return Constants::UNDEFINED;
|
|
367
|
-
return obj.get_own_property(static_cast<uint32_t>(key));
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
inline AnyValue optional_call(const AnyValue &fn, const AnyValue &thisVal, std::span<const AnyValue> args, const std::optional<std::string> &name = std::nullopt)
|
|
371
|
-
{
|
|
372
|
-
if (fn.is_null() || fn.is_undefined())
|
|
373
|
-
return Constants::UNDEFINED;
|
|
374
|
-
return fn.call(thisVal, args, name);
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
}
|
|
378
|
-
}
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "types.hpp"
|
|
4
|
+
#include "well_known_symbols.hpp"
|
|
5
|
+
#include "values/function.hpp"
|
|
6
|
+
#include "values/symbol.hpp"
|
|
7
|
+
#include "exception.hpp"
|
|
8
|
+
#include "any_value.hpp"
|
|
9
|
+
#include <ranges>
|
|
10
|
+
|
|
11
|
+
namespace jspp
|
|
12
|
+
{
|
|
13
|
+
namespace Access
|
|
14
|
+
{
|
|
15
|
+
// Helper function to check for TDZ and deref heap-allocated variables
|
|
16
|
+
inline const AnyValue &deref_ptr(const std::shared_ptr<AnyValue> &var, const std::string &name)
|
|
17
|
+
{
|
|
18
|
+
if (var->is_uninitialized()) [[unlikely]]
|
|
19
|
+
{
|
|
20
|
+
Exception::throw_uninitialized_reference(name);
|
|
21
|
+
}
|
|
22
|
+
return *var;
|
|
23
|
+
}
|
|
24
|
+
inline AnyValue &deref_ptr(std::shared_ptr<AnyValue> &var, const std::string &name)
|
|
25
|
+
{
|
|
26
|
+
if (var->is_uninitialized()) [[unlikely]]
|
|
27
|
+
{
|
|
28
|
+
Exception::throw_uninitialized_reference(name);
|
|
29
|
+
}
|
|
30
|
+
return *var;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Helper function to check for TDZ on stack-allocated variables
|
|
34
|
+
inline const AnyValue &deref_stack(const AnyValue &var, const std::string &name)
|
|
35
|
+
{
|
|
36
|
+
if (var.is_uninitialized()) [[unlikely]]
|
|
37
|
+
{
|
|
38
|
+
Exception::throw_uninitialized_reference(name);
|
|
39
|
+
}
|
|
40
|
+
return var;
|
|
41
|
+
}
|
|
42
|
+
inline AnyValue &deref_stack(AnyValue &var, const std::string &name)
|
|
43
|
+
{
|
|
44
|
+
if (var.is_uninitialized()) [[unlikely]]
|
|
45
|
+
{
|
|
46
|
+
Exception::throw_uninitialized_reference(name);
|
|
47
|
+
}
|
|
48
|
+
return var;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
inline AnyValue type_of(const std::optional<AnyValue> &val = std::nullopt)
|
|
52
|
+
{
|
|
53
|
+
if (!val.has_value())
|
|
54
|
+
return AnyValue::make_string("undefined");
|
|
55
|
+
|
|
56
|
+
switch (val.value().get_type())
|
|
57
|
+
{
|
|
58
|
+
case JsType::Undefined:
|
|
59
|
+
return AnyValue::make_string("undefined");
|
|
60
|
+
case JsType::Null:
|
|
61
|
+
return AnyValue::make_string("object");
|
|
62
|
+
case JsType::Boolean:
|
|
63
|
+
return AnyValue::make_string("boolean");
|
|
64
|
+
case JsType::Number:
|
|
65
|
+
return AnyValue::make_string("number");
|
|
66
|
+
case JsType::String:
|
|
67
|
+
return AnyValue::make_string("string");
|
|
68
|
+
case JsType::Symbol:
|
|
69
|
+
return AnyValue::make_string("symbol");
|
|
70
|
+
case JsType::Function:
|
|
71
|
+
return AnyValue::make_string("function");
|
|
72
|
+
case JsType::Object:
|
|
73
|
+
return AnyValue::make_string("object");
|
|
74
|
+
case JsType::Array:
|
|
75
|
+
return AnyValue::make_string("object");
|
|
76
|
+
case JsType::Iterator:
|
|
77
|
+
return AnyValue::make_string("object");
|
|
78
|
+
case JsType::AsyncIterator:
|
|
79
|
+
return AnyValue::make_string("object");
|
|
80
|
+
default:
|
|
81
|
+
return AnyValue::make_string("undefined");
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Helper function to get enumerable own property keys/values of an object
|
|
86
|
+
inline std::vector<std::string> get_object_keys(const AnyValue &obj)
|
|
87
|
+
{
|
|
88
|
+
std::vector<std::string> keys;
|
|
89
|
+
|
|
90
|
+
if (obj.is_object())
|
|
91
|
+
{
|
|
92
|
+
auto ptr = obj.as_object();
|
|
93
|
+
for (const auto &key : ptr->shape->property_names)
|
|
94
|
+
{
|
|
95
|
+
if (ptr->deleted_keys.count(key))
|
|
96
|
+
continue;
|
|
97
|
+
|
|
98
|
+
if (JsSymbol::is_internal_key(key))
|
|
99
|
+
continue;
|
|
100
|
+
|
|
101
|
+
auto offset_opt = ptr->shape->get_offset(key);
|
|
102
|
+
if (!offset_opt.has_value())
|
|
103
|
+
continue;
|
|
104
|
+
|
|
105
|
+
const auto &val = ptr->storage[offset_opt.value()];
|
|
106
|
+
|
|
107
|
+
if (val.is_data_descriptor())
|
|
108
|
+
{
|
|
109
|
+
if (val.as_data_descriptor()->enumerable)
|
|
110
|
+
keys.push_back(key);
|
|
111
|
+
}
|
|
112
|
+
else if (val.is_accessor_descriptor())
|
|
113
|
+
{
|
|
114
|
+
if (val.as_accessor_descriptor()->enumerable)
|
|
115
|
+
keys.push_back(key);
|
|
116
|
+
}
|
|
117
|
+
else
|
|
118
|
+
{
|
|
119
|
+
keys.push_back(key);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if (obj.is_function())
|
|
124
|
+
{
|
|
125
|
+
auto ptr = obj.as_function();
|
|
126
|
+
for (const auto &pair : ptr->props)
|
|
127
|
+
{
|
|
128
|
+
if (!JsSymbol::is_internal_key(pair.first))
|
|
129
|
+
{
|
|
130
|
+
if (!pair.second.is_data_descriptor() && !pair.second.is_accessor_descriptor())
|
|
131
|
+
keys.push_back(pair.first);
|
|
132
|
+
else if ((pair.second.is_data_descriptor() && pair.second.as_data_descriptor()->enumerable) ||
|
|
133
|
+
(pair.second.is_accessor_descriptor() && pair.second.as_accessor_descriptor()->enumerable))
|
|
134
|
+
keys.push_back(pair.first);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (obj.is_array())
|
|
139
|
+
{
|
|
140
|
+
auto len = obj.as_array()->length;
|
|
141
|
+
for (uint64_t i = 0; i < len; ++i)
|
|
142
|
+
{
|
|
143
|
+
keys.push_back(std::to_string(i));
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
if (obj.is_string())
|
|
147
|
+
{
|
|
148
|
+
auto len = obj.as_string()->value.length();
|
|
149
|
+
for (size_t i = 0; i < len; ++i)
|
|
150
|
+
{
|
|
151
|
+
keys.push_back(std::to_string(i));
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return keys;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
inline AnyValue get_object_value_iterator(const AnyValue &obj, const std::string &name)
|
|
159
|
+
{
|
|
160
|
+
if (obj.is_iterator())
|
|
161
|
+
{
|
|
162
|
+
return obj;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
auto gen_fn = obj.get_own_property(WellKnownSymbols::iterator->key);
|
|
166
|
+
if (gen_fn.is_function())
|
|
167
|
+
{
|
|
168
|
+
auto iter = gen_fn.call(obj, {}, WellKnownSymbols::iterator->key);
|
|
169
|
+
if (iter.is_iterator())
|
|
170
|
+
{
|
|
171
|
+
return iter;
|
|
172
|
+
}
|
|
173
|
+
if (iter.is_object())
|
|
174
|
+
{
|
|
175
|
+
auto next_fn = iter.get_own_property("next");
|
|
176
|
+
if (next_fn.is_function())
|
|
177
|
+
{
|
|
178
|
+
return iter;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
throw jspp::Exception::make_exception(name + " is not iterable", "TypeError");
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
inline AnyValue get_async_object_value_iterator(const AnyValue &obj, const std::string &name)
|
|
187
|
+
{
|
|
188
|
+
if (obj.is_async_iterator())
|
|
189
|
+
return obj;
|
|
190
|
+
|
|
191
|
+
auto method = obj.get_own_property(WellKnownSymbols::asyncIterator->key);
|
|
192
|
+
if (method.is_function())
|
|
193
|
+
{
|
|
194
|
+
auto iter = method.call(obj, {}, WellKnownSymbols::asyncIterator->key);
|
|
195
|
+
if (iter.is_object() || iter.is_async_iterator() || iter.is_iterator())
|
|
196
|
+
return iter;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
auto syncMethod = obj.get_own_property(WellKnownSymbols::iterator->key);
|
|
200
|
+
if (syncMethod.is_function())
|
|
201
|
+
{
|
|
202
|
+
auto iter = syncMethod.call(obj, {}, WellKnownSymbols::iterator->key);
|
|
203
|
+
if (iter.is_object() || iter.is_iterator())
|
|
204
|
+
return iter;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
throw jspp::Exception::make_exception(name + " is not async iterable", "TypeError");
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
inline AnyValue in(const AnyValue &lhs, const AnyValue &rhs)
|
|
211
|
+
{
|
|
212
|
+
if (!rhs.is_object() && !rhs.is_array() && !rhs.is_function() && !rhs.is_promise() && !rhs.is_iterator())
|
|
213
|
+
{
|
|
214
|
+
throw jspp::Exception::make_exception("Cannot use 'in' operator to search for '" + lhs.to_std_string() + "' in " + rhs.to_std_string(), "TypeError");
|
|
215
|
+
}
|
|
216
|
+
return AnyValue::make_boolean(rhs.has_property(lhs.to_std_string()));
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
inline AnyValue instance_of(const AnyValue &lhs, const AnyValue &rhs)
|
|
220
|
+
{
|
|
221
|
+
if (!rhs.is_function())
|
|
222
|
+
{
|
|
223
|
+
throw jspp::Exception::make_exception("Right-hand side of 'instanceof' is not callable", "TypeError");
|
|
224
|
+
}
|
|
225
|
+
if (!lhs.is_object() && !lhs.is_array() && !lhs.is_function() && !lhs.is_promise() && !lhs.is_iterator() && !lhs.is_async_iterator())
|
|
226
|
+
{
|
|
227
|
+
return Constants::FALSE;
|
|
228
|
+
}
|
|
229
|
+
AnyValue targetProto = rhs.get_own_property("prototype");
|
|
230
|
+
if (!targetProto.is_object() && !targetProto.is_array() && !targetProto.is_function())
|
|
231
|
+
{
|
|
232
|
+
throw jspp::Exception::make_exception("Function has non-object prototype in instanceof check", "TypeError");
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
AnyValue current = lhs;
|
|
236
|
+
|
|
237
|
+
while (true)
|
|
238
|
+
{
|
|
239
|
+
AnyValue proto;
|
|
240
|
+
if (current.is_object())
|
|
241
|
+
{
|
|
242
|
+
proto = current.as_object()->proto;
|
|
243
|
+
}
|
|
244
|
+
else if (current.is_array())
|
|
245
|
+
{
|
|
246
|
+
proto = current.as_array()->proto;
|
|
247
|
+
}
|
|
248
|
+
else if (current.is_function())
|
|
249
|
+
{
|
|
250
|
+
proto = current.as_function()->proto;
|
|
251
|
+
}
|
|
252
|
+
else
|
|
253
|
+
{
|
|
254
|
+
break;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if (proto.is_null() || proto.is_undefined())
|
|
258
|
+
break;
|
|
259
|
+
if (is_strictly_equal_to_primitive(proto, targetProto))
|
|
260
|
+
return Constants::TRUE;
|
|
261
|
+
current = proto;
|
|
262
|
+
}
|
|
263
|
+
return Constants::FALSE;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
inline AnyValue delete_property(const AnyValue &obj, const AnyValue &key)
|
|
267
|
+
{
|
|
268
|
+
if (obj.is_object())
|
|
269
|
+
{
|
|
270
|
+
auto ptr = obj.as_object();
|
|
271
|
+
std::string key_str = key.to_std_string();
|
|
272
|
+
if (ptr->shape->get_offset(key_str).has_value())
|
|
273
|
+
{
|
|
274
|
+
ptr->deleted_keys.insert(key_str);
|
|
275
|
+
}
|
|
276
|
+
return Constants::TRUE;
|
|
277
|
+
}
|
|
278
|
+
if (obj.is_array())
|
|
279
|
+
{
|
|
280
|
+
auto ptr = obj.as_array();
|
|
281
|
+
std::string key_str = key.to_std_string();
|
|
282
|
+
if (JsArray::is_array_index(key_str))
|
|
283
|
+
{
|
|
284
|
+
uint32_t idx = static_cast<uint32_t>(std::stoull(key_str));
|
|
285
|
+
if (idx < ptr->dense.size())
|
|
286
|
+
{
|
|
287
|
+
ptr->dense[idx] = Constants::UNINITIALIZED;
|
|
288
|
+
}
|
|
289
|
+
else
|
|
290
|
+
{
|
|
291
|
+
ptr->sparse.erase(idx);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
else
|
|
295
|
+
{
|
|
296
|
+
ptr->props.erase(key_str);
|
|
297
|
+
}
|
|
298
|
+
return Constants::TRUE;
|
|
299
|
+
}
|
|
300
|
+
if (obj.is_function())
|
|
301
|
+
{
|
|
302
|
+
auto ptr = obj.as_function();
|
|
303
|
+
ptr->props.erase(key.to_std_string());
|
|
304
|
+
return Constants::TRUE;
|
|
305
|
+
}
|
|
306
|
+
return Constants::TRUE;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
inline AnyValue optional_get_property(const AnyValue &obj, const std::string &key)
|
|
310
|
+
{
|
|
311
|
+
if (obj.is_null() || obj.is_undefined())
|
|
312
|
+
return Constants::UNDEFINED;
|
|
313
|
+
return obj.get_own_property(key);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
inline AnyValue optional_get_element(const AnyValue &obj, const AnyValue &key)
|
|
317
|
+
{
|
|
318
|
+
if (obj.is_null() || obj.is_undefined())
|
|
319
|
+
return Constants::UNDEFINED;
|
|
320
|
+
return obj.get_own_property(key);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
inline AnyValue optional_get_element(const AnyValue &obj, const double &key)
|
|
324
|
+
{
|
|
325
|
+
if (obj.is_null() || obj.is_undefined())
|
|
326
|
+
return Constants::UNDEFINED;
|
|
327
|
+
return obj.get_own_property(static_cast<uint32_t>(key));
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
inline AnyValue optional_call(const AnyValue &fn, const AnyValue &thisVal, std::span<const AnyValue> args, const std::optional<std::string> &name = std::nullopt)
|
|
331
|
+
{
|
|
332
|
+
if (fn.is_null() || fn.is_undefined())
|
|
333
|
+
return Constants::UNDEFINED;
|
|
334
|
+
return fn.call(thisVal, args, name);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
}
|
|
338
|
+
}
|