@ugo-studio/jspp 0.1.3 → 0.1.5

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 (83) hide show
  1. package/README.md +2 -2
  2. package/dist/analysis/scope.js +33 -4
  3. package/dist/analysis/typeAnalyzer.js +260 -21
  4. package/dist/ast/symbols.js +29 -0
  5. package/dist/cli-utils/args.js +57 -0
  6. package/dist/cli-utils/colors.js +9 -0
  7. package/dist/cli-utils/file-utils.js +20 -0
  8. package/dist/cli-utils/spinner.js +55 -0
  9. package/dist/cli.js +105 -31
  10. package/dist/core/codegen/class-handlers.js +131 -0
  11. package/dist/core/codegen/control-flow-handlers.js +474 -0
  12. package/dist/core/codegen/declaration-handlers.js +36 -15
  13. package/dist/core/codegen/expression-handlers.js +579 -125
  14. package/dist/core/codegen/function-handlers.js +222 -37
  15. package/dist/core/codegen/helpers.js +158 -4
  16. package/dist/core/codegen/index.js +20 -8
  17. package/dist/core/codegen/literal-handlers.js +18 -6
  18. package/dist/core/codegen/statement-handlers.js +171 -228
  19. package/dist/core/codegen/visitor.js +31 -3
  20. package/package.json +3 -3
  21. package/src/prelude/any_value.hpp +510 -633
  22. package/src/prelude/any_value_access.hpp +151 -0
  23. package/src/prelude/any_value_defines.hpp +190 -0
  24. package/src/prelude/any_value_helpers.hpp +139 -225
  25. package/src/prelude/exception.hpp +32 -0
  26. package/src/prelude/exception_helpers.hpp +49 -0
  27. package/src/prelude/index.hpp +25 -9
  28. package/src/prelude/library/array.hpp +190 -0
  29. package/src/prelude/library/console.hpp +14 -13
  30. package/src/prelude/library/error.hpp +113 -0
  31. package/src/prelude/library/function.hpp +10 -0
  32. package/src/prelude/library/global.hpp +35 -4
  33. package/src/prelude/library/math.hpp +308 -0
  34. package/src/prelude/library/object.hpp +288 -0
  35. package/src/prelude/library/performance.hpp +2 -2
  36. package/src/prelude/library/process.hpp +39 -0
  37. package/src/prelude/library/promise.hpp +131 -0
  38. package/src/prelude/library/symbol.hpp +46 -59
  39. package/src/prelude/library/timer.hpp +92 -0
  40. package/src/prelude/scheduler.hpp +145 -0
  41. package/src/prelude/types.hpp +58 -1
  42. package/src/prelude/utils/access.hpp +345 -0
  43. package/src/prelude/utils/assignment_operators.hpp +99 -0
  44. package/src/prelude/utils/log_any_value/array.hpp +245 -0
  45. package/src/prelude/utils/log_any_value/config.hpp +32 -0
  46. package/src/prelude/utils/log_any_value/function.hpp +39 -0
  47. package/src/prelude/utils/log_any_value/fwd.hpp +15 -0
  48. package/src/prelude/utils/log_any_value/helpers.hpp +62 -0
  49. package/src/prelude/utils/log_any_value/log_any_value.hpp +94 -0
  50. package/src/prelude/utils/log_any_value/object.hpp +136 -0
  51. package/src/prelude/utils/log_any_value/primitives.hpp +43 -0
  52. package/src/prelude/utils/operators.hpp +751 -0
  53. package/src/prelude/utils/well_known_symbols.hpp +25 -0
  54. package/src/prelude/values/array.hpp +10 -7
  55. package/src/prelude/{descriptors.hpp → values/descriptors.hpp} +2 -2
  56. package/src/prelude/values/function.hpp +85 -51
  57. package/src/prelude/values/helpers/array.hpp +80 -35
  58. package/src/prelude/values/helpers/function.hpp +110 -77
  59. package/src/prelude/values/helpers/iterator.hpp +16 -10
  60. package/src/prelude/values/helpers/object.hpp +85 -10
  61. package/src/prelude/values/helpers/promise.hpp +181 -0
  62. package/src/prelude/values/helpers/string.hpp +3 -3
  63. package/src/prelude/values/helpers/symbol.hpp +2 -2
  64. package/src/prelude/values/iterator.hpp +14 -6
  65. package/src/prelude/values/object.hpp +14 -3
  66. package/src/prelude/values/promise.hpp +73 -0
  67. package/src/prelude/values/prototypes/array.hpp +855 -16
  68. package/src/prelude/values/prototypes/function.hpp +4 -4
  69. package/src/prelude/values/prototypes/iterator.hpp +11 -10
  70. package/src/prelude/values/prototypes/number.hpp +153 -0
  71. package/src/prelude/values/prototypes/object.hpp +26 -0
  72. package/src/prelude/values/prototypes/promise.hpp +134 -0
  73. package/src/prelude/values/prototypes/string.hpp +29 -29
  74. package/src/prelude/values/prototypes/symbol.hpp +22 -3
  75. package/src/prelude/values/shape.hpp +52 -0
  76. package/src/prelude/values/string.hpp +1 -1
  77. package/src/prelude/values/symbol.hpp +1 -1
  78. package/src/prelude/access.hpp +0 -91
  79. package/src/prelude/error.hpp +0 -31
  80. package/src/prelude/error_helpers.hpp +0 -59
  81. package/src/prelude/log_string.hpp +0 -407
  82. package/src/prelude/operators.hpp +0 -256
  83. package/src/prelude/well_known_symbols.hpp +0 -14
@@ -0,0 +1,345 @@
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
+ default:
95
+ return AnyValue::make_string("undefined");
96
+ }
97
+ }
98
+
99
+ // Helper function to get enumerable own property keys/values of an object
100
+ inline std::vector<std::string> get_object_keys(const AnyValue &obj)
101
+ {
102
+ std::vector<std::string> keys;
103
+
104
+ if (obj.is_object())
105
+ {
106
+ auto ptr = obj.as_object();
107
+ // Use shape's property_names for stable iteration order
108
+ for (const auto &key : ptr->shape->property_names)
109
+ {
110
+ if (ptr->deleted_keys.count(key))
111
+ continue;
112
+
113
+ if (JsSymbol::is_internal_key(key))
114
+ continue;
115
+
116
+ auto offset_opt = ptr->shape->get_offset(key);
117
+ if (!offset_opt.has_value())
118
+ continue;
119
+
120
+ const auto &val = ptr->storage[offset_opt.value()];
121
+
122
+ if (val.is_data_descriptor())
123
+ {
124
+ if (val.as_data_descriptor()->enumerable)
125
+ keys.push_back(key);
126
+ }
127
+ else if (val.is_accessor_descriptor())
128
+ {
129
+ if (val.as_accessor_descriptor()->enumerable)
130
+ keys.push_back(key);
131
+ }
132
+ else
133
+ {
134
+ keys.push_back(key);
135
+ }
136
+ }
137
+ }
138
+ if (obj.is_function())
139
+ {
140
+ auto ptr = obj.as_function();
141
+ for (const auto &pair : ptr->props)
142
+ {
143
+ if (!JsSymbol::is_internal_key(pair.first))
144
+ {
145
+ if (!pair.second.is_data_descriptor() && !pair.second.is_accessor_descriptor())
146
+ keys.push_back(pair.first);
147
+ else if ((pair.second.is_data_descriptor() && pair.second.as_data_descriptor()->enumerable) ||
148
+ (pair.second.is_accessor_descriptor() && pair.second.as_accessor_descriptor()->enumerable))
149
+ keys.push_back(pair.first);
150
+ }
151
+ }
152
+ }
153
+ if (obj.is_array())
154
+ {
155
+ auto len = obj.as_array()->length;
156
+ for (auto i = 0; i < len; ++i)
157
+ {
158
+ keys.push_back(std::to_string(i));
159
+ }
160
+ }
161
+ if (obj.is_string())
162
+ {
163
+ auto len = obj.as_string()->value.length();
164
+ for (auto i = 0; i < len; ++i)
165
+ {
166
+ keys.push_back(std::to_string(i));
167
+ }
168
+ }
169
+
170
+ return keys;
171
+ }
172
+ inline AnyValue get_object_value_iterator(const AnyValue &obj, const std::string &name)
173
+ {
174
+ if (obj.is_iterator())
175
+ {
176
+ return obj;
177
+ }
178
+
179
+ auto gen_fn = obj.get_own_property(WellKnownSymbols::iterator->key);
180
+ if (gen_fn.is_function())
181
+ {
182
+ auto iter = gen_fn.call(obj, {}, WellKnownSymbols::iterator->key);
183
+ if (iter.is_iterator())
184
+ {
185
+ return iter;
186
+ }
187
+ if (iter.is_object())
188
+ {
189
+ auto next_fn = iter.get_own_property("next");
190
+ if (next_fn.is_function())
191
+ {
192
+ return iter;
193
+ }
194
+ }
195
+ }
196
+
197
+ throw jspp::Exception::make_exception(name + " is not iterable", "TypeError");
198
+ }
199
+
200
+ inline AnyValue in(const AnyValue &lhs, const AnyValue &rhs)
201
+ {
202
+ if (!rhs.is_object() && !rhs.is_array() && !rhs.is_function() && !rhs.is_promise() && !rhs.is_iterator())
203
+ {
204
+ throw jspp::Exception::make_exception("Cannot use 'in' operator to search for '" + lhs.to_std_string() + "' in " + rhs.to_std_string(), "TypeError");
205
+ }
206
+ return AnyValue::make_boolean(rhs.has_property(lhs.to_std_string()));
207
+ }
208
+
209
+ inline AnyValue instance_of(const AnyValue &lhs, const AnyValue &rhs)
210
+ {
211
+ if (!rhs.is_function())
212
+ {
213
+ throw jspp::Exception::make_exception("Right-hand side of 'instanceof' is not callable", "TypeError");
214
+ }
215
+ if (!lhs.is_object() && !lhs.is_array() && !lhs.is_function() && !lhs.is_promise() && !lhs.is_iterator())
216
+ {
217
+ return Constants::FALSE;
218
+ }
219
+ AnyValue targetProto = rhs.get_own_property("prototype");
220
+ if (!targetProto.is_object() && !targetProto.is_array() && !targetProto.is_function())
221
+ {
222
+ throw jspp::Exception::make_exception("Function has non-object prototype in instanceof check", "TypeError");
223
+ }
224
+ // Walk prototype chain of lhs
225
+ AnyValue current = lhs;
226
+
227
+ while (true)
228
+ {
229
+ AnyValue proto;
230
+ if (current.is_object())
231
+ {
232
+ auto p = current.as_object()->proto;
233
+ if (p)
234
+ proto = *p;
235
+ else
236
+ break;
237
+ }
238
+ else if (current.is_array())
239
+ {
240
+ auto p = current.as_array()->proto;
241
+ if (p)
242
+ proto = *p;
243
+ else
244
+ break;
245
+ }
246
+ else if (current.is_function())
247
+ {
248
+ auto p = current.as_function()->proto;
249
+ if (p)
250
+ proto = *p;
251
+ else
252
+ break;
253
+ }
254
+ else if (current.is_promise())
255
+ {
256
+ // Promises don't store explicit proto yet in our impl
257
+ break;
258
+ }
259
+ else
260
+ {
261
+ break;
262
+ }
263
+ if (proto.is_null() || proto.is_undefined())
264
+ break;
265
+ if (is_strictly_equal_to_primitive(proto, targetProto))
266
+ return Constants::TRUE;
267
+ current = proto;
268
+ }
269
+ return Constants::FALSE;
270
+ }
271
+
272
+ inline AnyValue delete_property(const AnyValue &obj, const AnyValue &key)
273
+ {
274
+ if (obj.is_object())
275
+ {
276
+ auto ptr = obj.as_object();
277
+ std::string key_str = key.to_std_string();
278
+ if (ptr->shape->get_offset(key_str).has_value())
279
+ {
280
+ ptr->deleted_keys.insert(key_str);
281
+ }
282
+ return Constants::TRUE;
283
+ }
284
+ if (obj.is_array())
285
+ {
286
+ auto ptr = obj.as_array();
287
+ std::string key_str = key.to_std_string();
288
+ if (JsArray::is_array_index(key_str))
289
+ {
290
+ uint32_t idx = static_cast<uint32_t>(std::stoull(key_str));
291
+ if (idx < ptr->dense.size())
292
+ {
293
+ ptr->dense[idx] = Constants::UNINITIALIZED;
294
+ }
295
+ else
296
+ {
297
+ ptr->sparse.erase(idx);
298
+ }
299
+ }
300
+ else
301
+ {
302
+ ptr->props.erase(key_str);
303
+ }
304
+ return Constants::TRUE;
305
+ }
306
+ if (obj.is_function())
307
+ {
308
+ auto ptr = obj.as_function();
309
+ ptr->props.erase(key.to_std_string());
310
+ return Constants::TRUE;
311
+ }
312
+ return Constants::TRUE;
313
+ }
314
+
315
+ inline AnyValue optional_get_property(const AnyValue &obj, const std::string &key)
316
+ {
317
+ if (obj.is_null() || obj.is_undefined())
318
+ return Constants::UNDEFINED;
319
+ return obj.get_own_property(key);
320
+ }
321
+
322
+ inline AnyValue optional_get_element(const AnyValue &obj, const AnyValue &key)
323
+ {
324
+ if (obj.is_null() || obj.is_undefined())
325
+ return Constants::UNDEFINED;
326
+ return obj.get_own_property(key);
327
+ }
328
+
329
+ inline AnyValue optional_get_element(const AnyValue &obj, const double &key)
330
+ {
331
+ if (obj.is_null() || obj.is_undefined())
332
+ return Constants::UNDEFINED;
333
+ return obj.get_own_property(static_cast<uint32_t>(key));
334
+ }
335
+
336
+ inline AnyValue optional_call(const AnyValue &fn, const AnyValue &thisVal, std::span<const AnyValue> args, const std::optional<std::string> &name = std::nullopt)
337
+ {
338
+ if (fn.is_null() || fn.is_undefined())
339
+ return Constants::UNDEFINED;
340
+ return fn.call(thisVal, args, name);
341
+ }
342
+
343
+ }
344
+
345
+ }
@@ -0,0 +1,99 @@
1
+ #pragma once
2
+
3
+ #include "types.hpp"
4
+ #include "any_value.hpp"
5
+ #include "utils/operators.hpp"
6
+
7
+ namespace jspp {
8
+
9
+ // --- FRIEND IMPLEMENTATIONS ---
10
+
11
+ inline AnyValue &operator+=(AnyValue &lhs, const AnyValue &rhs) {
12
+ lhs = lhs + rhs;
13
+ return lhs;
14
+ }
15
+
16
+ inline AnyValue &operator-=(AnyValue &lhs, const AnyValue &rhs) {
17
+ lhs = lhs - rhs;
18
+ return lhs;
19
+ }
20
+
21
+ inline AnyValue &operator*=(AnyValue &lhs, const AnyValue &rhs) {
22
+ lhs = lhs * rhs;
23
+ return lhs;
24
+ }
25
+
26
+ inline AnyValue &operator/=(AnyValue &lhs, const AnyValue &rhs) {
27
+ lhs = lhs / rhs;
28
+ return lhs;
29
+ }
30
+
31
+ inline AnyValue &operator%=(AnyValue &lhs, const AnyValue &rhs) {
32
+ lhs = lhs % rhs;
33
+ return lhs;
34
+ }
35
+
36
+ inline AnyValue &operator++(AnyValue &val) {
37
+ val = val + 1.0;
38
+ return val;
39
+ }
40
+
41
+ inline AnyValue operator++(AnyValue &val, int) {
42
+ AnyValue temp = val;
43
+ val = val + 1.0;
44
+ return temp;
45
+ }
46
+
47
+ inline AnyValue &operator--(AnyValue &val) {
48
+ val = val - 1.0;
49
+ return val;
50
+ }
51
+
52
+ inline AnyValue operator--(AnyValue &val, int) {
53
+ AnyValue temp = val;
54
+ val = val - 1.0;
55
+ return temp;
56
+ }
57
+
58
+ // --- OVERLOADS FOR PRIMITIVES ---
59
+
60
+ inline AnyValue &operator+=(AnyValue &lhs, const double &rhs) {
61
+ lhs = lhs + rhs;
62
+ return lhs;
63
+ }
64
+ inline AnyValue &operator+=(AnyValue &lhs, const int &rhs) {
65
+ return lhs += static_cast<double>(rhs);
66
+ }
67
+
68
+ inline AnyValue &operator-=(AnyValue &lhs, const double &rhs) {
69
+ lhs = lhs - rhs;
70
+ return lhs;
71
+ }
72
+ inline AnyValue &operator-=(AnyValue &lhs, const int &rhs) {
73
+ return lhs -= static_cast<double>(rhs);
74
+ }
75
+
76
+ inline AnyValue &operator*=(AnyValue &lhs, const double &rhs) {
77
+ lhs = lhs * rhs;
78
+ return lhs;
79
+ }
80
+ inline AnyValue &operator*=(AnyValue &lhs, const int &rhs) {
81
+ return lhs *= static_cast<double>(rhs);
82
+ }
83
+
84
+ inline AnyValue &operator/=(AnyValue &lhs, const double &rhs) {
85
+ lhs = lhs / rhs;
86
+ return lhs;
87
+ }
88
+ inline AnyValue &operator/=(AnyValue &lhs, const int &rhs) {
89
+ return lhs /= static_cast<double>(rhs);
90
+ }
91
+
92
+ inline AnyValue &operator%=(AnyValue &lhs, const double &rhs) {
93
+ lhs = lhs % rhs;
94
+ return lhs;
95
+ }
96
+ inline AnyValue &operator%=(AnyValue &lhs, const int &rhs) {
97
+ return lhs %= static_cast<double>(rhs);
98
+ }
99
+ }
@@ -0,0 +1,245 @@
1
+ #pragma once
2
+ #include "types.hpp"
3
+ #include "any_value.hpp"
4
+ #include "utils/log_any_value/config.hpp"
5
+ #include "utils/log_any_value/helpers.hpp"
6
+ #include "utils/log_any_value/fwd.hpp"
7
+ #include <string>
8
+ #include <sstream>
9
+ #include <unordered_set>
10
+ #include <algorithm>
11
+ #include <optional>
12
+
13
+ namespace jspp
14
+ {
15
+ namespace LogAnyValue
16
+ {
17
+ inline std::string format_array(const AnyValue &val, std::unordered_set<const void *> &visited, int depth)
18
+ {
19
+ auto arr = val.as_array();
20
+ size_t item_count = static_cast<size_t>(arr->length);
21
+ size_t prop_count = arr->props.size();
22
+
23
+ // // If custom toString exists on the object, prefer it
24
+ // auto itToString = arr->props.find("toString");
25
+ // if (depth > 0 && itToString != arr->props.end() && itToString->second.is_function())
26
+ // {
27
+ // try
28
+ // {
29
+ // auto result = itToString->second.as_function()->call(itToString->second, {});
30
+ // return to_log_string(result, visited, depth);
31
+ // }
32
+ // catch (...)
33
+ // {
34
+ // // ignore and fallback to manual formatting
35
+ // }
36
+ // }
37
+
38
+ std::string indent(depth * 2, ' ');
39
+ std::string next_indent((depth + 1) * 2, ' ');
40
+ std::stringstream ss;
41
+
42
+ // Horizontal layout for small and simple arrays
43
+ bool use_horizontal_layout = item_count <= HORIZONTAL_ARRAY_MAX_ITEMS;
44
+ if (use_horizontal_layout)
45
+ {
46
+ for (size_t i = 0; i < item_count; ++i)
47
+ {
48
+ AnyValue itemVal = AnyValue::make_uninitialized();
49
+ if (i < arr->dense.size())
50
+ {
51
+ itemVal = arr->dense[i];
52
+ }
53
+ else
54
+ {
55
+ auto it = arr->sparse.find(static_cast<uint32_t>(i));
56
+ if (it != arr->sparse.end())
57
+ {
58
+ itemVal = it->second;
59
+ }
60
+ }
61
+ if (!itemVal.is_uninitialized() && !is_simple_value(itemVal))
62
+ {
63
+ use_horizontal_layout = false;
64
+ break;
65
+ }
66
+ }
67
+ }
68
+
69
+ if (use_horizontal_layout)
70
+ {
71
+ ss << "[ ";
72
+ size_t empty_count = 0;
73
+ bool needs_comma = false;
74
+
75
+ for (size_t i = 0; i < item_count; ++i)
76
+ {
77
+ AnyValue itemVal = AnyValue::make_uninitialized();
78
+ if (i < arr->dense.size())
79
+ {
80
+ itemVal = arr->dense[i];
81
+ }
82
+ else
83
+ {
84
+ auto it = arr->sparse.find(static_cast<uint32_t>(i));
85
+ if (it != arr->sparse.end())
86
+ {
87
+ itemVal = it->second;
88
+ }
89
+ }
90
+
91
+ if (!itemVal.is_uninitialized())
92
+ {
93
+ if (empty_count > 0)
94
+ {
95
+ if (needs_comma)
96
+ ss << Color::BRIGHT_BLACK << ", " << Color::RESET;
97
+ ss << Color::BRIGHT_BLACK << empty_count << " x empty item" << (empty_count > 1 ? "s" : "") << Color::RESET;
98
+ needs_comma = true;
99
+ empty_count = 0;
100
+ }
101
+ if (needs_comma)
102
+ ss << Color::BRIGHT_BLACK << ", " << Color::RESET;
103
+ ss << to_log_string(itemVal, visited, depth + 1);
104
+ needs_comma = true;
105
+ }
106
+ else
107
+ {
108
+ empty_count++;
109
+ }
110
+ }
111
+
112
+ if (empty_count > 0)
113
+ {
114
+ if (needs_comma)
115
+ ss << Color::BRIGHT_BLACK << ", " << Color::RESET;
116
+ ss << Color::BRIGHT_BLACK << empty_count << " x empty item" << (empty_count > 1 ? "s" : "") << Color::RESET;
117
+ }
118
+
119
+ // Print properties
120
+ if (prop_count > 0)
121
+ {
122
+ ss << Color::BRIGHT_BLACK << ", " << Color::RESET;
123
+
124
+ size_t current_prop = 0;
125
+ for (const auto &pair : arr->props)
126
+ {
127
+ if (!is_enumerable_property(pair.second))
128
+ continue;
129
+
130
+ if (is_valid_js_identifier(pair.first))
131
+ {
132
+ ss << pair.first;
133
+ }
134
+ else
135
+ {
136
+ ss << "\"" << pair.first << "\"";
137
+ }
138
+ ss << ": " << to_log_string(pair.second, visited, depth + 1);
139
+ if (++current_prop < prop_count)
140
+ ss << Color::BRIGHT_BLACK << ", " << Color::RESET;
141
+ }
142
+ }
143
+
144
+ ss << " ]";
145
+ return ss.str();
146
+ }
147
+
148
+ // Bun-like multi-line layout
149
+ ss << "[\n";
150
+
151
+ const size_t items_to_show = std::min(item_count, MAX_ARRAY_ITEMS);
152
+ size_t empty_count = 0;
153
+ bool first_item_printed = false;
154
+
155
+ for (size_t i = 0; i < items_to_show; ++i)
156
+ {
157
+ AnyValue itemVal = AnyValue::make_uninitialized();
158
+ if (i < arr->dense.size())
159
+ {
160
+ itemVal = arr->dense[i];
161
+ }
162
+ else
163
+ {
164
+ auto it = arr->sparse.find(static_cast<uint32_t>(i));
165
+ if (it != arr->sparse.end())
166
+ {
167
+ itemVal = it->second;
168
+ }
169
+ }
170
+
171
+ if (!itemVal.is_uninitialized())
172
+ {
173
+ if (empty_count > 0)
174
+ {
175
+ if (first_item_printed)
176
+ ss << Color::BRIGHT_BLACK << ",\n"
177
+ << Color::RESET;
178
+ ss << next_indent << Color::BRIGHT_BLACK << empty_count << " x empty item" << (empty_count > 1 ? "s" : "") << Color::RESET;
179
+ first_item_printed = true;
180
+ empty_count = 0;
181
+ }
182
+ if (first_item_printed)
183
+ ss << Color::BRIGHT_BLACK << ",\n"
184
+ << Color::RESET;
185
+ ss << next_indent << to_log_string(itemVal, visited, depth + 1);
186
+ first_item_printed = true;
187
+ }
188
+ else
189
+ {
190
+ empty_count++;
191
+ }
192
+ }
193
+
194
+ if (empty_count > 0)
195
+ {
196
+ if (first_item_printed)
197
+ ss << Color::BRIGHT_BLACK << ",\n"
198
+ << Color::RESET;
199
+ ss << next_indent << Color::BRIGHT_BLACK << empty_count << " x empty item" << (empty_count > 1 ? "s" : "") << Color::RESET;
200
+ first_item_printed = true;
201
+ }
202
+
203
+ if (item_count > items_to_show)
204
+ {
205
+ if (first_item_printed)
206
+ ss << Color::BRIGHT_BLACK << ",\n"
207
+ << Color::RESET;
208
+ ss << next_indent << Color::BRIGHT_BLACK << "... " << (item_count - items_to_show) << " more items" << Color::RESET;
209
+ }
210
+ // Print properties
211
+ else if (prop_count > 0)
212
+ {
213
+ if (first_item_printed)
214
+ ss << Color::BRIGHT_BLACK << ",\n"
215
+ << Color::RESET;
216
+
217
+ size_t current_prop = 0;
218
+ for (const auto &pair : arr->props)
219
+ {
220
+ if (current_prop >= MAX_OBJECT_PROPS)
221
+ break;
222
+ if (!is_enumerable_property(pair.second))
223
+ continue;
224
+
225
+ ss << next_indent;
226
+ if (is_valid_js_identifier(pair.first))
227
+ {
228
+ ss << pair.first;
229
+ }
230
+ else
231
+ {
232
+ ss << "\"" << pair.first << "\"";
233
+ }
234
+ ss << ": " << to_log_string(pair.second, visited, depth + 1);
235
+ if (++current_prop < prop_count)
236
+ ss << Color::BRIGHT_BLACK << ",\n"
237
+ << Color::RESET;
238
+ }
239
+ }
240
+ ss << "\n";
241
+ ss << indent << "]";
242
+ return ss.str();
243
+ }
244
+ }
245
+ }