@ugo-studio/jspp 0.2.9 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/LICENSE +25 -25
  2. package/README.md +20 -12
  3. package/dist/analysis/scope.js +5 -3
  4. package/dist/analysis/typeAnalyzer.js +21 -25
  5. package/dist/cli/index.js +14 -4
  6. package/dist/cli/utils.js +61 -0
  7. package/dist/core/codegen/class-handlers.js +6 -6
  8. package/dist/core/codegen/control-flow-handlers.js +10 -9
  9. package/dist/core/codegen/declaration-handlers.js +10 -3
  10. package/dist/core/codegen/destructuring-handlers.js +9 -4
  11. package/dist/core/codegen/expression-handlers.js +40 -29
  12. package/dist/core/codegen/function-handlers.js +78 -12
  13. package/dist/core/codegen/helpers.js +91 -14
  14. package/dist/core/codegen/index.js +4 -2
  15. package/dist/core/codegen/statement-handlers.js +9 -7
  16. package/package.json +2 -2
  17. package/scripts/precompile-headers.ts +249 -50
  18. package/scripts/setup-compiler.ts +63 -63
  19. package/src/prelude/any_value.cpp +636 -0
  20. package/src/prelude/any_value.hpp +369 -362
  21. package/src/prelude/{exception_helpers.hpp → exception.cpp} +53 -53
  22. package/src/prelude/exception.hpp +27 -27
  23. package/src/prelude/iterator_instantiations.hpp +10 -0
  24. package/src/prelude/{index.hpp → jspp.hpp} +10 -16
  25. package/src/prelude/library/array.cpp +191 -0
  26. package/src/prelude/library/array.hpp +13 -186
  27. package/src/prelude/library/console.cpp +125 -0
  28. package/src/prelude/library/console.hpp +24 -112
  29. package/src/prelude/library/error.cpp +100 -0
  30. package/src/prelude/library/error.hpp +13 -113
  31. package/src/prelude/library/function.cpp +69 -0
  32. package/src/prelude/library/function.hpp +11 -10
  33. package/src/prelude/library/global.cpp +96 -0
  34. package/src/prelude/library/global.hpp +12 -28
  35. package/src/prelude/library/global_usings.hpp +15 -0
  36. package/src/prelude/library/math.cpp +258 -0
  37. package/src/prelude/library/math.hpp +26 -308
  38. package/src/prelude/library/object.cpp +379 -0
  39. package/src/prelude/library/object.hpp +14 -276
  40. package/src/prelude/library/performance.cpp +21 -0
  41. package/src/prelude/library/performance.hpp +5 -20
  42. package/src/prelude/library/process.cpp +38 -0
  43. package/src/prelude/library/process.hpp +11 -39
  44. package/src/prelude/library/promise.cpp +131 -0
  45. package/src/prelude/library/promise.hpp +12 -123
  46. package/src/prelude/library/symbol.cpp +56 -0
  47. package/src/prelude/library/symbol.hpp +11 -52
  48. package/src/prelude/library/timer.cpp +88 -0
  49. package/src/prelude/library/timer.hpp +16 -92
  50. package/src/prelude/runtime.cpp +19 -0
  51. package/src/prelude/types.hpp +184 -179
  52. package/src/prelude/utils/access.hpp +502 -411
  53. package/src/prelude/utils/assignment_operators.hpp +99 -99
  54. package/src/prelude/utils/log_any_value/array.hpp +61 -40
  55. package/src/prelude/utils/log_any_value/function.hpp +39 -39
  56. package/src/prelude/utils/log_any_value/object.hpp +60 -3
  57. package/src/prelude/utils/operators.hpp +351 -336
  58. package/src/prelude/utils/operators_primitive.hpp +336 -336
  59. package/src/prelude/utils/well_known_symbols.hpp +24 -24
  60. package/src/prelude/values/array.cpp +1399 -0
  61. package/src/prelude/values/array.hpp +4 -1
  62. package/src/prelude/values/async_iterator.cpp +251 -0
  63. package/src/prelude/values/async_iterator.hpp +111 -83
  64. package/src/prelude/values/function.cpp +262 -0
  65. package/src/prelude/values/function.hpp +62 -82
  66. package/src/prelude/values/iterator.cpp +309 -0
  67. package/src/prelude/values/iterator.hpp +33 -64
  68. package/src/prelude/values/number.cpp +176 -0
  69. package/src/prelude/values/object.cpp +159 -0
  70. package/src/prelude/values/object.hpp +4 -0
  71. package/src/prelude/values/promise.cpp +479 -0
  72. package/src/prelude/values/promise.hpp +79 -72
  73. package/src/prelude/values/prototypes/array.hpp +46 -1336
  74. package/src/prelude/values/prototypes/async_iterator.hpp +19 -61
  75. package/src/prelude/values/prototypes/function.hpp +7 -46
  76. package/src/prelude/values/prototypes/iterator.hpp +25 -201
  77. package/src/prelude/values/prototypes/number.hpp +23 -210
  78. package/src/prelude/values/prototypes/object.hpp +7 -23
  79. package/src/prelude/values/prototypes/promise.hpp +18 -196
  80. package/src/prelude/values/prototypes/string.hpp +39 -542
  81. package/src/prelude/values/prototypes/symbol.hpp +9 -70
  82. package/src/prelude/values/shape.hpp +52 -52
  83. package/src/prelude/values/string.cpp +485 -0
  84. package/src/prelude/values/string.hpp +25 -26
  85. package/src/prelude/values/symbol.cpp +89 -0
  86. package/src/prelude/values/symbol.hpp +101 -101
  87. package/src/prelude/any_value_access.hpp +0 -170
  88. package/src/prelude/any_value_defines.hpp +0 -190
  89. package/src/prelude/any_value_helpers.hpp +0 -374
  90. package/src/prelude/values/helpers/array.hpp +0 -209
  91. package/src/prelude/values/helpers/async_iterator.hpp +0 -275
  92. package/src/prelude/values/helpers/function.hpp +0 -109
  93. package/src/prelude/values/helpers/iterator.hpp +0 -145
  94. package/src/prelude/values/helpers/object.hpp +0 -104
  95. package/src/prelude/values/helpers/promise.hpp +0 -254
  96. package/src/prelude/values/helpers/string.hpp +0 -61
  97. package/src/prelude/values/helpers/symbol.hpp +0 -21
@@ -1,412 +1,503 @@
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<AnyValue> get_object_keys(const AnyValue &obj, bool include_symbols = false)
87
+ {
88
+ std::vector<AnyValue> 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
+ auto offset_opt = ptr->shape->get_offset(key);
99
+ if (!offset_opt.has_value())
100
+ continue;
101
+
102
+ const auto &val = ptr->storage[offset_opt.value()];
103
+
104
+ if (val.is_data_descriptor())
105
+ {
106
+ if (val.as_data_descriptor()->enumerable)
107
+ keys.push_back(AnyValue::make_string(key));
108
+ }
109
+ else if (val.is_accessor_descriptor())
110
+ {
111
+ if (val.as_accessor_descriptor()->enumerable)
112
+ keys.push_back(AnyValue::make_string(key));
113
+ }
114
+ else
115
+ {
116
+ keys.push_back(AnyValue::make_string(key));
117
+ }
118
+ }
119
+ if (include_symbols)
120
+ {
121
+ for (const auto &pair : ptr->symbol_props)
122
+ {
123
+ const auto &val = pair.second;
124
+ if (val.is_data_descriptor())
125
+ {
126
+ if (val.as_data_descriptor()->enumerable)
127
+ keys.push_back(pair.first);
128
+ }
129
+ else if (val.is_accessor_descriptor())
130
+ {
131
+ if (val.as_accessor_descriptor()->enumerable)
132
+ keys.push_back(pair.first);
133
+ }
134
+ else
135
+ {
136
+ keys.push_back(pair.first);
137
+ }
138
+ }
139
+ }
140
+ }
141
+ if (obj.is_function())
142
+ {
143
+ auto ptr = obj.as_function();
144
+ for (const auto &pair : ptr->props)
145
+ {
146
+ if (!pair.second.is_data_descriptor() && !pair.second.is_accessor_descriptor())
147
+ keys.push_back(AnyValue::make_string(pair.first));
148
+ else if ((pair.second.is_data_descriptor() && pair.second.as_data_descriptor()->enumerable) ||
149
+ (pair.second.is_accessor_descriptor() && pair.second.as_accessor_descriptor()->enumerable))
150
+ keys.push_back(AnyValue::make_string(pair.first));
151
+ }
152
+ if (include_symbols)
153
+ {
154
+ for (const auto &pair : ptr->symbol_props)
155
+ {
156
+ const auto &val = pair.second;
157
+ if (val.is_data_descriptor())
158
+ {
159
+ if (val.as_data_descriptor()->enumerable)
160
+ keys.push_back(pair.first);
161
+ }
162
+ else if (val.is_accessor_descriptor())
163
+ {
164
+ if (val.as_accessor_descriptor()->enumerable)
165
+ keys.push_back(pair.first);
166
+ }
167
+ else
168
+ {
169
+ keys.push_back(pair.first);
170
+ }
171
+ }
172
+ }
173
+ }
174
+ if (obj.is_array())
175
+ {
176
+ auto ptr = obj.as_array();
177
+ auto len = ptr->length;
178
+ for (uint64_t i = 0; i < len; ++i)
179
+ {
180
+ keys.push_back(AnyValue::make_string(std::to_string(i)));
181
+ }
182
+ if (include_symbols)
183
+ {
184
+ for (const auto &pair : ptr->symbol_props)
185
+ {
186
+ const auto &val = pair.second;
187
+ if (val.is_data_descriptor())
188
+ {
189
+ if (val.as_data_descriptor()->enumerable)
190
+ keys.push_back(pair.first);
191
+ }
192
+ else if (val.is_accessor_descriptor())
193
+ {
194
+ if (val.as_accessor_descriptor()->enumerable)
195
+ keys.push_back(pair.first);
196
+ }
197
+ else
198
+ {
199
+ keys.push_back(pair.first);
200
+ }
201
+ }
202
+ }
203
+ }
204
+ if (obj.is_string())
205
+ {
206
+ auto len = obj.as_string()->value.length();
207
+ for (size_t i = 0; i < len; ++i)
208
+ {
209
+ keys.push_back(AnyValue::make_string(std::to_string(i)));
210
+ }
211
+ }
212
+
213
+ return keys;
214
+ }
215
+
216
+ inline AnyValue get_object_iterator(const AnyValue &obj, const std::optional<std::string> &name = std::nullopt)
217
+ {
218
+ if (obj.is_null() || obj.is_undefined())
219
+ {
220
+ throw jspp::Exception::make_exception("Cannot read properties of " + obj.to_std_string() + " (reading 'Symbol.iterator')", "TypeError");
221
+ }
222
+
223
+ if (obj.is_iterator())
224
+ {
225
+ return obj;
226
+ }
227
+
228
+ auto iterSym = AnyValue::from_symbol(WellKnownSymbols::iterator);
229
+ auto gen_fn = obj.get_own_property(iterSym);
230
+ if (gen_fn.is_function())
231
+ {
232
+ auto iter = gen_fn.call(obj, {}, iterSym.to_std_string());
233
+ if (iter.is_iterator())
234
+ {
235
+ return iter;
236
+ }
237
+ if (iter.is_object())
238
+ {
239
+ auto next_fn = iter.get_own_property("next");
240
+ if (next_fn.is_function())
241
+ {
242
+ return iter;
243
+ }
244
+ }
245
+ }
246
+
247
+ throw jspp::Exception::make_exception(name.value_or(obj.to_std_string()) + " is not iterable", "TypeError");
248
+ }
249
+
250
+ inline AnyValue get_object_async_iterator(const AnyValue &obj, const std::optional<std::string> &name = std::nullopt)
251
+ {
252
+ if (obj.is_null() || obj.is_undefined())
253
+ {
254
+ throw jspp::Exception::make_exception("Cannot read properties of " + obj.to_std_string() + " (reading 'Symbol.asyncIterator')", "TypeError");
255
+ }
256
+
257
+ if (obj.is_async_iterator())
258
+ return obj;
259
+
260
+ auto asyncIterSym = AnyValue::from_symbol(WellKnownSymbols::asyncIterator);
261
+ auto method = obj.get_own_property(asyncIterSym);
262
+ if (method.is_function())
263
+ {
264
+ auto iter = method.call(obj, {}, asyncIterSym.to_std_string());
265
+ if (iter.is_object() || iter.is_async_iterator() || iter.is_iterator())
266
+ return iter;
267
+ }
268
+
269
+ auto iterSym = AnyValue::from_symbol(WellKnownSymbols::iterator);
270
+ auto syncMethod = obj.get_own_property(iterSym);
271
+ if (syncMethod.is_function())
272
+ {
273
+ auto iter = syncMethod.call(obj, {}, iterSym.to_std_string());
274
+ if (iter.is_object() || iter.is_iterator())
275
+ return iter;
276
+ }
277
+
278
+ throw jspp::Exception::make_exception(name.value_or(obj.to_std_string()) + " is not async iterable", "TypeError");
279
+ }
280
+
281
+ inline AnyValue in(const AnyValue &lhs, const AnyValue &rhs)
282
+ {
283
+ if (!rhs.is_object() && !rhs.is_array() && !rhs.is_function() && !rhs.is_promise() && !rhs.is_iterator())
284
+ {
285
+ throw jspp::Exception::make_exception("Cannot use 'in' operator to search for '" + lhs.to_std_string() + "' in " + rhs.to_std_string(), "TypeError");
286
+ }
287
+ return AnyValue::make_boolean(rhs.has_property(lhs.to_std_string()));
288
+ }
289
+
290
+ inline AnyValue instance_of(const AnyValue &lhs, const AnyValue &rhs)
291
+ {
292
+ if (!rhs.is_function())
293
+ {
294
+ throw jspp::Exception::make_exception("Right-hand side of 'instanceof' is not callable", "TypeError");
295
+ }
296
+ if (!lhs.is_object() && !lhs.is_array() && !lhs.is_function() && !lhs.is_promise() && !lhs.is_iterator() && !lhs.is_async_iterator())
297
+ {
298
+ return Constants::FALSE;
299
+ }
300
+ AnyValue targetProto = rhs.get_own_property("prototype");
301
+ if (!targetProto.is_object() && !targetProto.is_array() && !targetProto.is_function())
302
+ {
303
+ throw jspp::Exception::make_exception("Function has non-object prototype in instanceof check", "TypeError");
304
+ }
305
+
306
+ AnyValue current = lhs;
307
+
308
+ while (true)
309
+ {
310
+ AnyValue proto;
311
+ if (current.is_object())
312
+ {
313
+ proto = current.as_object()->proto;
314
+ }
315
+ else if (current.is_array())
316
+ {
317
+ proto = current.as_array()->proto;
318
+ }
319
+ else if (current.is_function())
320
+ {
321
+ proto = current.as_function()->proto;
322
+ }
323
+ else if (current.is_promise())
324
+ {
325
+ proto = current.as_promise()->get_property("__proto__", current); // Fallback for promise if not fully modularized
326
+ }
327
+ else
328
+ {
329
+ break;
330
+ }
331
+
332
+ if (proto.is_null() || proto.is_undefined())
333
+ break;
334
+ if (is_strictly_equal_to_primitive(proto, targetProto))
335
+ return Constants::TRUE;
336
+ current = proto;
337
+ }
338
+ return Constants::FALSE;
339
+ }
340
+
341
+ inline AnyValue delete_property(const AnyValue &obj, const AnyValue &key)
342
+ {
343
+ if (obj.is_object())
344
+ {
345
+ auto ptr = obj.as_object();
346
+ std::string key_str = key.to_std_string();
347
+ if (ptr->shape->get_offset(key_str).has_value())
348
+ {
349
+ ptr->deleted_keys.insert(key_str);
350
+ }
351
+ return Constants::TRUE;
352
+ }
353
+ if (obj.is_array())
354
+ {
355
+ auto ptr = obj.as_array();
356
+ std::string key_str = key.to_std_string();
357
+ if (JsArray::is_array_index(key_str))
358
+ {
359
+ uint32_t idx = static_cast<uint32_t>(std::stoull(key_str));
360
+ if (idx < ptr->dense.size())
361
+ {
362
+ ptr->dense[idx] = Constants::UNINITIALIZED;
363
+ }
364
+ else
365
+ {
366
+ ptr->sparse.erase(idx);
367
+ }
368
+ }
369
+ else
370
+ {
371
+ ptr->props.erase(key_str);
372
+ }
373
+ return Constants::TRUE;
374
+ }
375
+ if (obj.is_function())
376
+ {
377
+ auto ptr = obj.as_function();
378
+ ptr->props.erase(key.to_std_string());
379
+ return Constants::TRUE;
380
+ }
381
+ return Constants::TRUE;
382
+ }
383
+
384
+ inline AnyValue get_optional_property(const AnyValue &obj, const std::string &key)
385
+ {
386
+ if (obj.is_null() || obj.is_undefined())
387
+ return Constants::UNDEFINED;
388
+ return obj.get_own_property(key);
389
+ }
390
+
391
+ inline AnyValue get_optional_element(const AnyValue &obj, const AnyValue &key)
392
+ {
393
+ if (obj.is_null() || obj.is_undefined())
394
+ return Constants::UNDEFINED;
395
+ return obj.get_own_property(key);
396
+ }
397
+
398
+ inline AnyValue get_optional_element(const AnyValue &obj, const double &key)
399
+ {
400
+ if (obj.is_null() || obj.is_undefined())
401
+ return Constants::UNDEFINED;
402
+ return obj.get_own_property(static_cast<uint32_t>(key));
403
+ }
404
+
405
+ 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)
406
+ {
407
+ if (obj.is_null() || obj.is_undefined())
408
+ return Constants::UNDEFINED;
409
+ return obj.get_own_property(key).call(obj, args, expr);
410
+ }
411
+ 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)
412
+ {
413
+ if (obj.is_null() || obj.is_undefined())
414
+ return Constants::UNDEFINED;
415
+ return obj.get_own_property(key).optional_call(obj, args, expr);
416
+ }
417
+
418
+ inline void spread_array(std::vector<AnyValue> &target, const AnyValue &source)
419
+ {
420
+ if (source.is_array())
421
+ {
422
+ auto arr = source.as_array();
423
+ target.reserve(target.size() + arr->length);
424
+ for (uint64_t i = 0; i < arr->length; ++i)
425
+ {
426
+ target.push_back(arr->get_property(static_cast<uint32_t>(i)));
427
+ }
428
+ }
429
+ else if (source.is_string())
430
+ {
431
+ auto s = source.as_string();
432
+ target.reserve(target.size() + s->value.length());
433
+ for (char c : s->value)
434
+ {
435
+ target.push_back(AnyValue::make_string(std::string(1, c)));
436
+ }
437
+ }
438
+ else if (source.is_object() || source.is_function() || source.is_iterator())
439
+ {
440
+ auto iter = get_object_iterator(source, "spread target");
441
+ auto next_fn = iter.get_own_property("next");
442
+ while (true)
443
+ {
444
+ auto next_res = next_fn.call(iter, {});
445
+ if (is_truthy(next_res.get_own_property("done")))
446
+ break;
447
+ target.push_back(next_res.get_own_property("value"));
448
+ }
449
+ }
450
+ else
451
+ {
452
+ throw jspp::Exception::make_exception("Spread syntax requires an iterable object", "TypeError");
453
+ }
454
+ }
455
+
456
+ inline void spread_object(AnyValue &target, const AnyValue &source)
457
+ {
458
+ if (source.is_null() || source.is_undefined())
459
+ return;
460
+
461
+ auto keys = get_object_keys(source);
462
+ for (const auto &key : keys)
463
+ {
464
+ target.set_own_property(key, source.get_property_with_receiver(key.to_std_string(), source));
465
+ }
466
+ }
467
+
468
+ inline AnyValue get_rest_object(const AnyValue &source, const std::vector<AnyValue> &excluded_keys)
469
+ {
470
+ if (source.is_null() || source.is_undefined())
471
+ return AnyValue::make_object({});
472
+
473
+ auto result = AnyValue::make_object({});
474
+ auto keys = get_object_keys(source, true);
475
+
476
+ auto is_excluded = [&](const AnyValue &key)
477
+ {
478
+ for (const auto &ex : excluded_keys)
479
+ {
480
+ if (is_strictly_equal_to_primitive(key, ex))
481
+ return true;
482
+ }
483
+ return false;
484
+ };
485
+
486
+ for (const auto &key : keys)
487
+ {
488
+ if (!is_excluded(key))
489
+ {
490
+ if (key.is_symbol())
491
+ {
492
+ result.set_own_symbol_property(key, source.get_symbol_property_with_receiver(key, source));
493
+ }
494
+ else
495
+ {
496
+ result.set_own_property(key.to_std_string(), source.get_property_with_receiver(key.to_std_string(), source));
497
+ }
498
+ }
499
+ }
500
+ return result;
501
+ }
502
+ }
412
503
  }