@ugo-studio/jspp 0.1.3 → 0.1.4

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 (69) hide show
  1. package/README.md +2 -2
  2. package/dist/analysis/scope.js +16 -4
  3. package/dist/analysis/typeAnalyzer.js +253 -20
  4. package/dist/ast/types.js +6 -0
  5. package/dist/cli.js +1 -2
  6. package/dist/core/codegen/class-handlers.js +127 -0
  7. package/dist/core/codegen/control-flow-handlers.js +464 -0
  8. package/dist/core/codegen/declaration-handlers.js +31 -14
  9. package/dist/core/codegen/expression-handlers.js +429 -117
  10. package/dist/core/codegen/function-handlers.js +91 -15
  11. package/dist/core/codegen/helpers.js +66 -2
  12. package/dist/core/codegen/index.js +17 -6
  13. package/dist/core/codegen/literal-handlers.js +3 -0
  14. package/dist/core/codegen/statement-handlers.js +133 -204
  15. package/dist/core/codegen/visitor.js +29 -3
  16. package/package.json +3 -3
  17. package/src/prelude/any_value.hpp +658 -634
  18. package/src/prelude/any_value_access.hpp +103 -0
  19. package/src/prelude/any_value_defines.hpp +151 -0
  20. package/src/prelude/any_value_helpers.hpp +246 -225
  21. package/src/prelude/exception.hpp +31 -0
  22. package/src/prelude/exception_helpers.hpp +49 -0
  23. package/src/prelude/index.hpp +18 -9
  24. package/src/prelude/library/console.hpp +13 -13
  25. package/src/prelude/library/error.hpp +111 -0
  26. package/src/prelude/library/global.hpp +15 -4
  27. package/src/prelude/library/performance.hpp +2 -2
  28. package/src/prelude/library/promise.hpp +121 -0
  29. package/src/prelude/library/symbol.hpp +3 -4
  30. package/src/prelude/library/timer.hpp +92 -0
  31. package/src/prelude/scheduler.hpp +145 -0
  32. package/src/prelude/types.hpp +10 -1
  33. package/src/prelude/utils/access.hpp +174 -0
  34. package/src/prelude/utils/log_any_value/array.hpp +245 -0
  35. package/src/prelude/utils/log_any_value/config.hpp +32 -0
  36. package/src/prelude/utils/log_any_value/function.hpp +37 -0
  37. package/src/prelude/utils/log_any_value/fwd.hpp +15 -0
  38. package/src/prelude/utils/log_any_value/helpers.hpp +62 -0
  39. package/src/prelude/utils/log_any_value/log_any_value.hpp +94 -0
  40. package/src/prelude/utils/log_any_value/object.hpp +119 -0
  41. package/src/prelude/utils/log_any_value/primitives.hpp +41 -0
  42. package/src/prelude/{operators.hpp → utils/operators.hpp} +29 -10
  43. package/src/prelude/{well_known_symbols.hpp → utils/well_known_symbols.hpp} +0 -1
  44. package/src/prelude/values/array.hpp +3 -2
  45. package/src/prelude/{descriptors.hpp → values/descriptors.hpp} +2 -2
  46. package/src/prelude/values/function.hpp +76 -51
  47. package/src/prelude/values/helpers/array.hpp +20 -11
  48. package/src/prelude/values/helpers/function.hpp +125 -77
  49. package/src/prelude/values/helpers/iterator.hpp +13 -7
  50. package/src/prelude/values/helpers/object.hpp +36 -6
  51. package/src/prelude/values/helpers/promise.hpp +181 -0
  52. package/src/prelude/values/helpers/string.hpp +3 -3
  53. package/src/prelude/values/helpers/symbol.hpp +2 -2
  54. package/src/prelude/values/iterator.hpp +13 -5
  55. package/src/prelude/values/object.hpp +6 -2
  56. package/src/prelude/values/promise.hpp +73 -0
  57. package/src/prelude/values/prototypes/array.hpp +16 -16
  58. package/src/prelude/values/prototypes/function.hpp +4 -4
  59. package/src/prelude/values/prototypes/iterator.hpp +11 -10
  60. package/src/prelude/values/prototypes/object.hpp +26 -0
  61. package/src/prelude/values/prototypes/promise.hpp +124 -0
  62. package/src/prelude/values/prototypes/string.hpp +26 -26
  63. package/src/prelude/values/prototypes/symbol.hpp +5 -3
  64. package/src/prelude/values/string.hpp +1 -1
  65. package/src/prelude/values/symbol.hpp +1 -1
  66. package/src/prelude/access.hpp +0 -91
  67. package/src/prelude/error.hpp +0 -31
  68. package/src/prelude/error_helpers.hpp +0 -59
  69. package/src/prelude/log_string.hpp +0 -407
@@ -0,0 +1,174 @@
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 AnyValue typeof(const AnyValue &val)
68
+ {
69
+ switch (val.get_type())
70
+ {
71
+ case JsType::Undefined:
72
+ return AnyValue::make_string("undefined");
73
+ case JsType::Null:
74
+ return AnyValue::make_string("object");
75
+ case JsType::Boolean:
76
+ return AnyValue::make_string("boolean");
77
+ case JsType::Number:
78
+ return AnyValue::make_string("number");
79
+ case JsType::String:
80
+ return AnyValue::make_string("string");
81
+ case JsType::Symbol:
82
+ return AnyValue::make_string("symbol");
83
+ case JsType::Function:
84
+ return AnyValue::make_string("function");
85
+ case JsType::Object:
86
+ case JsType::Array:
87
+ case JsType::Iterator:
88
+ return AnyValue::make_string("object");
89
+ default:
90
+ return AnyValue::make_string("undefined");
91
+ }
92
+ }
93
+ inline AnyValue typeof() // for undeclared variables
94
+ {
95
+ return AnyValue::make_string("undefined");
96
+ }
97
+
98
+ // Helper function to get enumerable own property keys/values of an object
99
+ inline std::vector<std::string> get_object_keys(const AnyValue &obj)
100
+ {
101
+ std::vector<std::string> keys;
102
+
103
+ if (obj.is_object())
104
+ {
105
+ auto ptr = obj.as_object();
106
+ for (const auto &pair : ptr->props)
107
+ {
108
+ if (!JsSymbol::is_internal_key(pair.first))
109
+ keys.push_back(pair.first);
110
+ }
111
+ }
112
+ if (obj.is_function())
113
+ {
114
+ auto ptr = obj.as_function();
115
+ for (const auto &pair : ptr->props)
116
+ {
117
+ if (!JsSymbol::is_internal_key(pair.first))
118
+ {
119
+ if (!pair.second.is_data_descriptor() && !pair.second.is_accessor_descriptor())
120
+ keys.push_back(pair.first);
121
+ else if ((pair.second.is_data_descriptor() && pair.second.as_data_descriptor()->enumerable) ||
122
+ (pair.second.is_accessor_descriptor() && pair.second.as_accessor_descriptor()->enumerable))
123
+ keys.push_back(pair.first);
124
+ }
125
+ }
126
+ }
127
+ if (obj.is_array())
128
+ {
129
+ auto len = obj.as_array()->length;
130
+ for (auto i = 0; i < len; ++i)
131
+ {
132
+ keys.push_back(std::to_string(i));
133
+ }
134
+ }
135
+ if (obj.is_string())
136
+ {
137
+ auto len = obj.as_string()->value.length();
138
+ for (auto i = 0; i < len; ++i)
139
+ {
140
+ keys.push_back(std::to_string(i));
141
+ }
142
+ }
143
+
144
+ return keys;
145
+ }
146
+ inline AnyValue get_object_value_iterator(const AnyValue &obj, const std::string &name)
147
+ {
148
+ if (obj.is_iterator())
149
+ {
150
+ return obj;
151
+ }
152
+
153
+ auto gen_fn = obj.get_own_property(WellKnownSymbols::iterator->key);
154
+ if (gen_fn.is_function())
155
+ {
156
+ auto iter = gen_fn.as_function()->call(gen_fn, {});
157
+ if (iter.is_iterator())
158
+ {
159
+ return iter;
160
+ }
161
+ if (iter.is_object())
162
+ {
163
+ auto next_fn = iter.get_own_property("next");
164
+ if (next_fn.is_function())
165
+ {
166
+ return iter;
167
+ }
168
+ }
169
+ }
170
+
171
+ throw jspp::Exception::make_exception(name + " is not iterable", "TypeError");
172
+ }
173
+ }
174
+ }
@@ -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
+ std::optional<AnyValue> itemVal;
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.has_value() && !is_simple_value(itemVal.value()))
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
+ std::optional<AnyValue> itemVal;
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.has_value())
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.value(), 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
+ std::optional<AnyValue> itemVal;
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.has_value())
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.value(), 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
+ }
@@ -0,0 +1,32 @@
1
+ #pragma once
2
+
3
+ #include <string>
4
+
5
+ namespace jspp
6
+ {
7
+ namespace LogAnyValue
8
+ {
9
+ // --- Configuration for Logging Verbosity ---
10
+ const int MAX_DEPTH = 5;
11
+ const size_t MAX_STRING_LENGTH = 100;
12
+ const size_t MAX_ARRAY_ITEMS = 50;
13
+ const size_t MAX_OBJECT_PROPS = 30;
14
+
15
+ // --- Configuration for Horizontal Layout ---
16
+ const size_t HORIZONTAL_ARRAY_MAX_ITEMS = 10;
17
+ const size_t HORIZONTAL_OBJECT_MAX_PROPS = 5;
18
+
19
+ // ANSI Color Codes for terminal output
20
+ namespace Color
21
+ {
22
+ const std::string RESET = "\033[0m";
23
+ const std::string RED = "\033[31m";
24
+ const std::string GREEN = "\033[32m";
25
+ const std::string YELLOW = "\033[33m";
26
+ const std::string BLUE = "\033[94m";
27
+ const std::string CYAN = "\033[36m";
28
+ const std::string MAGENTA = "\033[35m";
29
+ const std::string BRIGHT_BLACK = "\033[90m"; // Grey
30
+ }
31
+ }
32
+ }
@@ -0,0 +1,37 @@
1
+ #pragma once
2
+ #include "types.hpp"
3
+ #include "any_value.hpp"
4
+ #include "utils/log_any_value/config.hpp"
5
+ #include <string>
6
+
7
+ namespace jspp
8
+ {
9
+ namespace LogAnyValue
10
+ {
11
+ inline std::string format_function(const AnyValue &val)
12
+ {
13
+ auto fn = val.as_function();
14
+
15
+ if (fn->is_class)
16
+ {
17
+ std::string extends_part = "";
18
+ if (fn->proto && !fn->proto->is_uninitialized() && !fn->proto->is_undefined() && !fn->proto->is_null())
19
+ {
20
+ if (fn->proto->is_function())
21
+ {
22
+ auto parent = fn->proto->as_function();
23
+ if (!parent->name.empty())
24
+ {
25
+ extends_part = " extends " + parent->name;
26
+ }
27
+ }
28
+ }
29
+ return Color::CYAN + std::string("[class ") + (fn->name.empty() ? "(anonymous)" : fn->name) + extends_part + "]" + Color::RESET;
30
+ }
31
+
32
+ auto type_part = fn->is_generator ? "GeneratorFunction" : "Function";
33
+ auto name_part = fn->name.size() > 0 ? ": " + fn->name : " (anonymous)";
34
+ return Color::CYAN + "[" + type_part + name_part + "]" + Color::RESET;
35
+ }
36
+ }
37
+ }
@@ -0,0 +1,15 @@
1
+ #pragma once
2
+
3
+ #include <string>
4
+ #include <unordered_set>
5
+ #include "any_value.hpp"
6
+
7
+ namespace jspp
8
+ {
9
+ namespace LogAnyValue
10
+ {
11
+ // Forward declarations
12
+ inline std::string to_log_string(const AnyValue &val);
13
+ inline std::string to_log_string(const AnyValue &val, std::unordered_set<const void *> &visited, int depth);
14
+ }
15
+ }
@@ -0,0 +1,62 @@
1
+ #pragma once
2
+
3
+ #include "types.hpp"
4
+ #include "any_value.hpp"
5
+ #include "utils/log_any_value/config.hpp"
6
+ #include <string>
7
+ #include <cctype>
8
+
9
+ namespace jspp
10
+ {
11
+ namespace LogAnyValue
12
+ {
13
+ inline bool is_valid_js_identifier(const std::string &s)
14
+ {
15
+ if (s.empty())
16
+ {
17
+ return false;
18
+ }
19
+ if (!std::isalpha(s[0]) && s[0] != '_' && s[0] != '$')
20
+ {
21
+ return false;
22
+ }
23
+ for (size_t i = 1; i < s.length(); ++i)
24
+ {
25
+ if (!std::isalnum(s[i]) && s[i] != '_' && s[i] != '$')
26
+ {
27
+ return false;
28
+ }
29
+ }
30
+ return true;
31
+ }
32
+
33
+ inline bool is_simple_value(const AnyValue &val)
34
+ {
35
+ return val.is_undefined() || val.is_null() || val.is_uninitialized() ||
36
+ val.is_boolean() || val.is_number() || val.is_string();
37
+ }
38
+
39
+ inline bool is_enumerable_property(const AnyValue &val)
40
+ {
41
+ if (val.is_data_descriptor())
42
+ {
43
+ return val.as_data_descriptor()->enumerable;
44
+ }
45
+ else if (
46
+ val.is_accessor_descriptor())
47
+ {
48
+ return val.as_accessor_descriptor()->enumerable;
49
+ }
50
+ return true;
51
+ }
52
+
53
+ inline std::string truncate_string(const std::string &str)
54
+ {
55
+ if (str.length() > MAX_STRING_LENGTH)
56
+ {
57
+ return str.substr(0, MAX_STRING_LENGTH) + "...";
58
+ }
59
+ return str;
60
+ }
61
+ }
62
+ }
@@ -0,0 +1,94 @@
1
+ #pragma once
2
+
3
+ #include "types.hpp"
4
+ #include "utils/well_known_symbols.hpp"
5
+ #include "any_value.hpp"
6
+ #include "utils/log_any_value/config.hpp"
7
+ #include "utils/log_any_value/fwd.hpp"
8
+ #include "utils/log_any_value/helpers.hpp"
9
+ #include "utils/log_any_value/primitives.hpp"
10
+ #include "utils/log_any_value/function.hpp"
11
+ #include "utils/log_any_value/object.hpp"
12
+ #include "utils/log_any_value/array.hpp"
13
+
14
+ #include <unordered_set>
15
+
16
+ namespace jspp
17
+ {
18
+ namespace LogAnyValue
19
+ {
20
+ inline std::string to_log_string(const AnyValue &val)
21
+ {
22
+ std::unordered_set<const void *> visited;
23
+ return to_log_string(val, visited, 0);
24
+ }
25
+
26
+ inline std::string to_log_string(const AnyValue &val, std::unordered_set<const void *> &visited, int depth)
27
+ {
28
+ // 1. Try Primitives
29
+ auto primitiveStr = format_primitive(val, depth);
30
+ if (primitiveStr.has_value())
31
+ {
32
+ return primitiveStr.value();
33
+ }
34
+
35
+ // 2. Functions
36
+ if (val.is_function())
37
+ {
38
+ return format_function(val);
39
+ }
40
+
41
+ // 3. Depth limit
42
+ if (depth > MAX_DEPTH)
43
+ {
44
+ if (val.is_object())
45
+ return Color::CYAN + std::string("[Object]") + Color::RESET;
46
+ if (val.is_array())
47
+ return Color::CYAN + std::string("[Array]") + Color::RESET;
48
+ }
49
+
50
+ // 4. Circular reference detection
51
+ const void *ptr_address = nullptr;
52
+ if (val.is_object())
53
+ ptr_address = val.as_object();
54
+ else if (val.is_array())
55
+ ptr_address = val.as_array();
56
+
57
+ if (ptr_address)
58
+ {
59
+ if (visited.count(ptr_address))
60
+ return Color::CYAN + std::string("[Circular]") + Color::RESET;
61
+ visited.insert(ptr_address);
62
+ }
63
+
64
+ // 5. Complex Types (Objects & Arrays)
65
+ if (val.is_object())
66
+ {
67
+ return format_object(val, visited, depth);
68
+ }
69
+
70
+ if (val.is_array())
71
+ {
72
+ return format_array(val, visited, depth);
73
+ }
74
+
75
+ // 6. DataDescriptor
76
+ if (val.is_data_descriptor())
77
+ {
78
+ auto desc = val.as_data_descriptor();
79
+ if (desc->enumerable)
80
+ {
81
+ return to_log_string((*desc->value), visited, depth);
82
+ }
83
+ else
84
+ {
85
+ // Will not be printed if the method works well
86
+ return Color::BRIGHT_BLACK + "<non-enumerable>" + Color::RESET;
87
+ }
88
+ }
89
+
90
+ // Fallback
91
+ return val.to_std_string();
92
+ }
93
+ }
94
+ }