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