@ugo-studio/jspp 0.2.9 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/dist/core/codegen/class-handlers.js +6 -6
  2. package/dist/core/codegen/statement-handlers.js +1 -1
  3. package/package.json +1 -1
  4. package/src/prelude/any_value.hpp +362 -362
  5. package/src/prelude/any_value_access.hpp +170 -170
  6. package/src/prelude/any_value_defines.hpp +189 -189
  7. package/src/prelude/any_value_helpers.hpp +374 -374
  8. package/src/prelude/library/array.hpp +185 -185
  9. package/src/prelude/library/console.hpp +111 -111
  10. package/src/prelude/library/error.hpp +112 -112
  11. package/src/prelude/library/function.hpp +10 -10
  12. package/src/prelude/library/math.hpp +307 -307
  13. package/src/prelude/library/object.hpp +275 -275
  14. package/src/prelude/library/process.hpp +39 -39
  15. package/src/prelude/library/promise.hpp +123 -123
  16. package/src/prelude/library/symbol.hpp +52 -52
  17. package/src/prelude/library/timer.hpp +91 -91
  18. package/src/prelude/types.hpp +178 -178
  19. package/src/prelude/utils/access.hpp +411 -411
  20. package/src/prelude/utils/operators.hpp +336 -336
  21. package/src/prelude/values/array.hpp +0 -1
  22. package/src/prelude/values/async_iterator.hpp +83 -83
  23. package/src/prelude/values/function.hpp +82 -82
  24. package/src/prelude/values/helpers/array.hpp +198 -208
  25. package/src/prelude/values/helpers/async_iterator.hpp +275 -275
  26. package/src/prelude/values/helpers/function.hpp +108 -108
  27. package/src/prelude/values/helpers/iterator.hpp +144 -144
  28. package/src/prelude/values/helpers/promise.hpp +253 -253
  29. package/src/prelude/values/helpers/string.hpp +37 -61
  30. package/src/prelude/values/promise.hpp +72 -72
  31. package/src/prelude/values/prototypes/array.hpp +14 -2
  32. package/src/prelude/values/prototypes/iterator.hpp +201 -201
  33. package/src/prelude/values/prototypes/promise.hpp +196 -196
  34. package/src/prelude/values/prototypes/string.hpp +564 -542
  35. package/src/prelude/values/string.hpp +25 -26
@@ -1,412 +1,412 @@
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, bool include_symbols = false)
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 (!include_symbols && 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 (include_symbols || !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_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_object_async_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 get_optional_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 get_optional_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 get_optional_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 call_optional_property(const AnyValue &obj, const std::string &key, std::span<const AnyValue> args, const std::optional<std::string> &expr = std::nullopt)
331
- {
332
- if (obj.is_null() || obj.is_undefined())
333
- return Constants::UNDEFINED;
334
- return obj.get_own_property(key).call(obj, args, expr);
335
- }
336
- inline AnyValue call_optional_property_with_optional_call(const AnyValue &obj, const std::string &key, std::span<const AnyValue> args, const std::optional<std::string> &expr = std::nullopt)
337
- {
338
- if (obj.is_null() || obj.is_undefined())
339
- return Constants::UNDEFINED;
340
- return obj.get_own_property(key).optional_call(obj, args, expr);
341
- }
342
-
343
- inline void spread_array(std::vector<AnyValue> &target, const AnyValue &source)
344
- {
345
- if (source.is_array())
346
- {
347
- auto arr = source.as_array();
348
- target.reserve(target.size() + arr->length);
349
- for (uint64_t i = 0; i < arr->length; ++i)
350
- {
351
- target.push_back(arr->get_property(static_cast<uint32_t>(i)));
352
- }
353
- }
354
- else if (source.is_string())
355
- {
356
- auto s = source.as_string();
357
- target.reserve(target.size() + s->value.length());
358
- for (char c : s->value)
359
- {
360
- target.push_back(AnyValue::make_string(std::string(1, c)));
361
- }
362
- }
363
- else if (source.is_object() || source.is_function() || source.is_iterator())
364
- {
365
- auto iter = get_object_iterator(source, "spread target");
366
- auto next_fn = iter.get_own_property("next");
367
- while (true)
368
- {
369
- auto next_res = next_fn.call(iter, {});
370
- if (is_truthy(next_res.get_own_property("done")))
371
- break;
372
- target.push_back(next_res.get_own_property("value"));
373
- }
374
- }
375
- else
376
- {
377
- throw jspp::Exception::make_exception("Spread syntax requires an iterable object", "TypeError");
378
- }
379
- }
380
-
381
- inline void spread_object(AnyValue &target, const AnyValue &source)
382
- {
383
- if (source.is_null() || source.is_undefined())
384
- return;
385
-
386
- auto keys = get_object_keys(source);
387
- for (const auto &key : keys)
388
- {
389
- target.set_own_property(key, source.get_property_with_receiver(key, source));
390
- }
391
- }
392
-
393
- inline AnyValue get_rest_object(const AnyValue &source, const std::vector<std::string> &excluded_keys)
394
- {
395
- if (source.is_null() || source.is_undefined())
396
- return AnyValue::make_object({});
397
-
398
- auto result = AnyValue::make_object({});
399
- auto keys = get_object_keys(source, true);
400
- std::unordered_set<std::string> excluded(excluded_keys.begin(), excluded_keys.end());
401
-
402
- for (const auto &key : keys)
403
- {
404
- if (excluded.find(key) == excluded.end())
405
- {
406
- result.set_own_property(key, source.get_property_with_receiver(key, source));
407
- }
408
- }
409
- return result;
410
- }
411
- }
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, bool include_symbols = false)
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 (!include_symbols && 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 (include_symbols || !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_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_object_async_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 get_optional_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 get_optional_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 get_optional_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 call_optional_property(const AnyValue &obj, const std::string &key, std::span<const AnyValue> args, const std::optional<std::string> &expr = std::nullopt)
331
+ {
332
+ if (obj.is_null() || obj.is_undefined())
333
+ return Constants::UNDEFINED;
334
+ return obj.get_own_property(key).call(obj, args, expr);
335
+ }
336
+ inline AnyValue call_optional_property_with_optional_call(const AnyValue &obj, const std::string &key, std::span<const AnyValue> args, const std::optional<std::string> &expr = std::nullopt)
337
+ {
338
+ if (obj.is_null() || obj.is_undefined())
339
+ return Constants::UNDEFINED;
340
+ return obj.get_own_property(key).optional_call(obj, args, expr);
341
+ }
342
+
343
+ inline void spread_array(std::vector<AnyValue> &target, const AnyValue &source)
344
+ {
345
+ if (source.is_array())
346
+ {
347
+ auto arr = source.as_array();
348
+ target.reserve(target.size() + arr->length);
349
+ for (uint64_t i = 0; i < arr->length; ++i)
350
+ {
351
+ target.push_back(arr->get_property(static_cast<uint32_t>(i)));
352
+ }
353
+ }
354
+ else if (source.is_string())
355
+ {
356
+ auto s = source.as_string();
357
+ target.reserve(target.size() + s->value.length());
358
+ for (char c : s->value)
359
+ {
360
+ target.push_back(AnyValue::make_string(std::string(1, c)));
361
+ }
362
+ }
363
+ else if (source.is_object() || source.is_function() || source.is_iterator())
364
+ {
365
+ auto iter = get_object_iterator(source, "spread target");
366
+ auto next_fn = iter.get_own_property("next");
367
+ while (true)
368
+ {
369
+ auto next_res = next_fn.call(iter, {});
370
+ if (is_truthy(next_res.get_own_property("done")))
371
+ break;
372
+ target.push_back(next_res.get_own_property("value"));
373
+ }
374
+ }
375
+ else
376
+ {
377
+ throw jspp::Exception::make_exception("Spread syntax requires an iterable object", "TypeError");
378
+ }
379
+ }
380
+
381
+ inline void spread_object(AnyValue &target, const AnyValue &source)
382
+ {
383
+ if (source.is_null() || source.is_undefined())
384
+ return;
385
+
386
+ auto keys = get_object_keys(source);
387
+ for (const auto &key : keys)
388
+ {
389
+ target.set_own_property(key, source.get_property_with_receiver(key, source));
390
+ }
391
+ }
392
+
393
+ inline AnyValue get_rest_object(const AnyValue &source, const std::vector<std::string> &excluded_keys)
394
+ {
395
+ if (source.is_null() || source.is_undefined())
396
+ return AnyValue::make_object({});
397
+
398
+ auto result = AnyValue::make_object({});
399
+ auto keys = get_object_keys(source, true);
400
+ std::unordered_set<std::string> excluded(excluded_keys.begin(), excluded_keys.end());
401
+
402
+ for (const auto &key : keys)
403
+ {
404
+ if (excluded.find(key) == excluded.end())
405
+ {
406
+ result.set_own_property(key, source.get_property_with_receiver(key, source));
407
+ }
408
+ }
409
+ return result;
410
+ }
411
+ }
412
412
  }