@ugo-studio/jspp 0.1.2 → 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 (72) hide show
  1. package/README.md +5 -3
  2. package/dist/analysis/scope.js +38 -15
  3. package/dist/analysis/typeAnalyzer.js +257 -23
  4. package/dist/ast/types.js +6 -0
  5. package/dist/cli.js +3 -4
  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 +432 -116
  10. package/dist/core/codegen/function-handlers.js +110 -13
  11. package/dist/core/codegen/helpers.js +76 -8
  12. package/dist/core/codegen/index.js +18 -5
  13. package/dist/core/codegen/literal-handlers.js +3 -0
  14. package/dist/core/codegen/statement-handlers.js +152 -186
  15. package/dist/core/codegen/visitor.js +35 -3
  16. package/package.json +3 -3
  17. package/src/prelude/any_value.hpp +658 -734
  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 -0
  21. package/src/prelude/exception.hpp +31 -0
  22. package/src/prelude/exception_helpers.hpp +49 -0
  23. package/src/prelude/index.hpp +35 -12
  24. package/src/prelude/library/console.hpp +20 -20
  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 +25 -0
  28. package/src/prelude/library/promise.hpp +121 -0
  29. package/src/prelude/library/symbol.hpp +60 -4
  30. package/src/prelude/library/timer.hpp +92 -0
  31. package/src/prelude/scheduler.hpp +145 -0
  32. package/src/prelude/types.hpp +33 -6
  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} +31 -12
  43. package/src/prelude/utils/well_known_symbols.hpp +13 -0
  44. package/src/prelude/values/array.hpp +5 -2
  45. package/src/prelude/{descriptors.hpp → values/descriptors.hpp} +2 -2
  46. package/src/prelude/values/function.hpp +76 -19
  47. package/src/prelude/values/{operators → helpers}/array.hpp +30 -14
  48. package/src/prelude/values/helpers/function.hpp +125 -0
  49. package/src/prelude/values/helpers/iterator.hpp +107 -0
  50. package/src/prelude/values/helpers/object.hpp +64 -0
  51. package/src/prelude/values/helpers/promise.hpp +181 -0
  52. package/src/prelude/values/helpers/string.hpp +50 -0
  53. package/src/prelude/values/helpers/symbol.hpp +23 -0
  54. package/src/prelude/values/iterator.hpp +96 -0
  55. package/src/prelude/values/object.hpp +8 -3
  56. package/src/prelude/values/promise.hpp +73 -0
  57. package/src/prelude/values/prototypes/array.hpp +23 -16
  58. package/src/prelude/values/prototypes/function.hpp +26 -0
  59. package/src/prelude/values/prototypes/iterator.hpp +58 -0
  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 +366 -357
  63. package/src/prelude/values/prototypes/symbol.hpp +41 -0
  64. package/src/prelude/values/string.hpp +25 -0
  65. package/src/prelude/values/symbol.hpp +102 -0
  66. package/src/prelude/access.hpp +0 -86
  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 -403
  70. package/src/prelude/values/operators/function.hpp +0 -34
  71. package/src/prelude/values/operators/object.hpp +0 -34
  72. package/src/prelude/well_known_symbols.hpp +0 -10
@@ -1,403 +0,0 @@
1
- #pragma once
2
-
3
- #include "types.hpp"
4
- #include "well_known_symbols.hpp"
5
- #include "any_value.hpp"
6
- #include <sstream>
7
- #include <unordered_set>
8
- #include <algorithm>
9
-
10
- namespace jspp
11
- {
12
- namespace LogString
13
- {
14
- // --- Configuration for Logging Verbosity ---
15
- const int MAX_DEPTH = 5;
16
- const size_t MAX_STRING_LENGTH = 100;
17
- const size_t MAX_ARRAY_ITEMS = 50;
18
- const size_t MAX_OBJECT_PROPS = 30;
19
- // --- Configuration for Horizontal Layout ---
20
- const size_t HORIZONTAL_ARRAY_MAX_ITEMS = 10;
21
- const size_t HORIZONTAL_OBJECT_MAX_PROPS = 5;
22
-
23
- // ANSI Color Codes for terminal output
24
- namespace Color
25
- {
26
- const std::string RESET = "\033[0m";
27
- const std::string GREEN = "\033[32m";
28
- const std::string YELLOW = "\033[33m";
29
- const std::string CYAN = "\033[36m";
30
- const std::string MAGENTA = "\033[35m";
31
- const std::string BRIGHT_BLACK = "\033[90m"; // Grey
32
- }
33
-
34
- // Forward declarations
35
- inline std::string to_log_string(const AnyValue &val);
36
- inline std::string to_log_string(const AnyValue &val, std::unordered_set<const void *> &visited, int depth);
37
- inline bool is_simple_value(const AnyValue &val);
38
-
39
- inline bool is_valid_js_identifier(const std::string &s)
40
- {
41
- if (s.empty())
42
- {
43
- return false;
44
- }
45
- if (!std::isalpha(s[0]) && s[0] != '_' && s[0] != '$')
46
- {
47
- return false;
48
- }
49
- for (size_t i = 1; i < s.length(); ++i)
50
- {
51
- if (!std::isalnum(s[i]) && s[i] != '_' && s[i] != '$')
52
- {
53
- return false;
54
- }
55
- }
56
- return true;
57
- }
58
-
59
- inline bool is_simple_value(const AnyValue &val)
60
- {
61
- return val.is_undefined() || val.is_null() || val.is_uninitialized() ||
62
- val.is_boolean() || val.is_number() || val.is_string();
63
- }
64
-
65
- inline std::string truncate_string(const std::string &str)
66
- {
67
- if (str.length() > MAX_STRING_LENGTH)
68
- {
69
- return str.substr(0, MAX_STRING_LENGTH) + "...";
70
- }
71
- return str;
72
- }
73
-
74
- inline std::string to_log_string(const AnyValue &val)
75
- {
76
- std::unordered_set<const void *> visited;
77
- return to_log_string(val, visited, 0);
78
- }
79
-
80
- inline std::string to_log_string(const AnyValue &val, std::unordered_set<const void *> &visited, int depth)
81
- {
82
- // Primitives and simple wrapped values
83
- if (val.is_uninitialized())
84
- return Color::BRIGHT_BLACK + std::string("<uninitialized>") + Color::RESET;
85
- if (val.is_undefined())
86
- return Color::BRIGHT_BLACK + std::string("undefined") + Color::RESET;
87
- if (val.is_null())
88
- return Color::MAGENTA + std::string("null") + Color::RESET;
89
- if (val.is_boolean())
90
- return Color::YELLOW + std::string(val.as_boolean() ? "true" : "false") + Color::RESET;
91
- if (val.is_number())
92
- return Color::YELLOW + val.to_std_string() + Color::RESET;
93
- if (val.is_string())
94
- {
95
- const std::string &s = *val.as_string();
96
- if (depth == 0)
97
- return truncate_string(s);
98
- return Color::GREEN + std::string("\"") + truncate_string(s) + "\"" + Color::RESET;
99
- }
100
- if (val.is_function())
101
- {
102
- auto fn = val.as_function();
103
- auto name_part = fn->name.size() > 0 ? ": " + fn->name : "";
104
- return Color::CYAN + std::string("[Function") + name_part + "]" + Color::RESET;
105
- }
106
-
107
- // Depth limit
108
- if (depth > MAX_DEPTH)
109
- {
110
- if (val.is_object())
111
- return Color::CYAN + std::string("[Object]") + Color::RESET;
112
- if (val.is_array())
113
- return Color::CYAN + std::string("[Array]") + Color::RESET;
114
- }
115
-
116
- // Circular reference detection
117
- const void *ptr_address = nullptr;
118
- if (val.is_object())
119
- ptr_address = val.as_object();
120
- else if (val.is_array())
121
- ptr_address = val.as_array();
122
-
123
- if (ptr_address)
124
- {
125
- if (visited.count(ptr_address))
126
- return Color::CYAN + std::string("[Circular]") + Color::RESET;
127
- visited.insert(ptr_address);
128
- }
129
-
130
- std::string indent(depth * 2, ' ');
131
- std::string next_indent((depth + 1) * 2, ' ');
132
- std::stringstream ss;
133
-
134
- // Objects
135
- if (val.is_object())
136
- {
137
- auto obj = val.as_object();
138
-
139
- // If custom toString exists on the object, prefer it
140
- auto itToString = obj->props.find(jspp::WellKnownSymbols::toString);
141
- if (itToString != obj->props.end() && itToString->second.is_function())
142
- {
143
- try
144
- {
145
- auto result = itToString->second.as_function("toString")->call({});
146
- return to_log_string(result, visited, depth);
147
- }
148
- catch (...)
149
- {
150
- // ignore and fallback to manual formatting
151
- }
152
- }
153
-
154
- size_t prop_count = obj->props.size();
155
-
156
- bool use_horizontal_layout = prop_count > 0 && prop_count <= HORIZONTAL_OBJECT_MAX_PROPS;
157
- if (use_horizontal_layout)
158
- {
159
- for (const auto &pair : obj->props)
160
- {
161
- if (!is_simple_value(pair.second))
162
- {
163
- use_horizontal_layout = false;
164
- break;
165
- }
166
- }
167
- }
168
-
169
- if (use_horizontal_layout)
170
- {
171
- ss << "{ ";
172
- size_t current_prop = 0;
173
- for (const auto &pair : obj->props)
174
- {
175
- if (is_valid_js_identifier(pair.first))
176
- {
177
- ss << pair.first;
178
- }
179
- else
180
- {
181
- ss << "\"" << pair.first << "\"";
182
- }
183
- ss << ": " << to_log_string(pair.second, visited, depth + 1);
184
- if (++current_prop < prop_count)
185
- ss << Color::BRIGHT_BLACK << ", " << Color::RESET;
186
- }
187
- ss << " }";
188
- }
189
- else
190
- {
191
- ss << "{";
192
- if (prop_count > 0)
193
- {
194
- ss << "\n";
195
- size_t props_shown = 0;
196
- for (const auto &pair : obj->props)
197
- {
198
- if (props_shown >= MAX_OBJECT_PROPS)
199
- break;
200
- if (props_shown > 0)
201
- ss << ",\n";
202
-
203
- ss << next_indent;
204
- if (is_valid_js_identifier(pair.first))
205
- {
206
- ss << pair.first;
207
- }
208
- else
209
- {
210
- ss << "\"" << pair.first << "\"";
211
- }
212
- ss << ": " << to_log_string(pair.second, visited, depth + 1);
213
- props_shown++;
214
- }
215
- if (prop_count > MAX_OBJECT_PROPS)
216
- ss << ",\n"
217
- << next_indent << Color::BRIGHT_BLACK << "... " << (prop_count - MAX_OBJECT_PROPS) << " more properties" << Color::RESET;
218
- ss << "\n"
219
- << indent;
220
- }
221
- ss << "}";
222
- }
223
- return ss.str();
224
- }
225
-
226
- // Arrays
227
- if (val.is_array())
228
- {
229
- auto arr = val.as_array();
230
- size_t item_count = static_cast<size_t>(arr->length);
231
-
232
- // If custom toString exists on the object, prefer it
233
- auto itToString = arr->props.find(jspp::WellKnownSymbols::toString);
234
- if (depth > 0 && itToString != arr->props.end() && itToString->second.is_function())
235
- {
236
- try
237
- {
238
- auto result = itToString->second.as_function("toString")->call({});
239
- return to_log_string(result, visited, depth);
240
- }
241
- catch (...)
242
- {
243
- // ignore and fallback to manual formatting
244
- }
245
- }
246
-
247
- std::string indent(depth * 2, ' ');
248
- std::string next_indent((depth + 1) * 2, ' ');
249
- std::stringstream ss;
250
-
251
- // Horizontal layout for small and simple arrays
252
- bool use_horizontal_layout = item_count <= HORIZONTAL_ARRAY_MAX_ITEMS;
253
- if (use_horizontal_layout)
254
- {
255
- for (size_t i = 0; i < item_count; ++i)
256
- {
257
- std::optional<AnyValue> itemVal;
258
- if (i < arr->dense.size())
259
- {
260
- itemVal = arr->dense[i];
261
- }
262
- else
263
- {
264
- auto it = arr->sparse.find(static_cast<uint32_t>(i));
265
- if (it != arr->sparse.end())
266
- {
267
- itemVal = it->second;
268
- }
269
- }
270
- if (itemVal.has_value() && !is_simple_value(itemVal.value()))
271
- {
272
- use_horizontal_layout = false;
273
- break;
274
- }
275
- }
276
- }
277
-
278
- if (use_horizontal_layout)
279
- {
280
- ss << "[ ";
281
- size_t empty_count = 0;
282
- bool needs_comma = false;
283
-
284
- for (size_t i = 0; i < item_count; ++i)
285
- {
286
- std::optional<AnyValue> itemVal;
287
- if (i < arr->dense.size())
288
- {
289
- itemVal = arr->dense[i];
290
- }
291
- else
292
- {
293
- auto it = arr->sparse.find(static_cast<uint32_t>(i));
294
- if (it != arr->sparse.end())
295
- {
296
- itemVal = it->second;
297
- }
298
- }
299
-
300
- if (itemVal.has_value())
301
- {
302
- if (empty_count > 0)
303
- {
304
- if (needs_comma)
305
- ss << Color::BRIGHT_BLACK << ", " << Color::RESET;
306
- ss << Color::BRIGHT_BLACK << empty_count << " x empty item" << (empty_count > 1 ? "s" : "") << Color::RESET;
307
- needs_comma = true;
308
- empty_count = 0;
309
- }
310
- if (needs_comma)
311
- ss << Color::BRIGHT_BLACK << ", " << Color::RESET;
312
- ss << to_log_string(itemVal.value(), visited, depth + 1);
313
- needs_comma = true;
314
- }
315
- else
316
- {
317
- empty_count++;
318
- }
319
- }
320
-
321
- if (empty_count > 0)
322
- {
323
- if (needs_comma)
324
- ss << Color::BRIGHT_BLACK << ", " << Color::RESET;
325
- ss << Color::BRIGHT_BLACK << empty_count << " x empty item" << (empty_count > 1 ? "s" : "") << Color::RESET;
326
- }
327
-
328
- ss << " ]";
329
- return ss.str();
330
- }
331
-
332
- // Bun-like multi-line layout
333
- ss << "[\n";
334
-
335
- const size_t items_to_show = std::min(item_count, MAX_ARRAY_ITEMS);
336
- size_t empty_count = 0;
337
- bool first_item_printed = false;
338
-
339
- for (size_t i = 0; i < items_to_show; ++i)
340
- {
341
- std::optional<AnyValue> itemVal;
342
- if (i < arr->dense.size())
343
- {
344
- itemVal = arr->dense[i];
345
- }
346
- else
347
- {
348
- auto it = arr->sparse.find(static_cast<uint32_t>(i));
349
- if (it != arr->sparse.end())
350
- {
351
- itemVal = it->second;
352
- }
353
- }
354
-
355
- if (itemVal.has_value())
356
- {
357
- if (empty_count > 0)
358
- {
359
- if (first_item_printed)
360
- ss << Color::BRIGHT_BLACK << ",\n"
361
- << Color::RESET;
362
- ss << next_indent << Color::BRIGHT_BLACK << empty_count << " x empty item" << (empty_count > 1 ? "s" : "") << Color::RESET;
363
- first_item_printed = true;
364
- empty_count = 0;
365
- }
366
- if (first_item_printed)
367
- ss << Color::BRIGHT_BLACK << ",\n"
368
- << Color::RESET;
369
- ss << next_indent << to_log_string(itemVal.value(), visited, depth + 1);
370
- first_item_printed = true;
371
- }
372
- else
373
- {
374
- empty_count++;
375
- }
376
- }
377
-
378
- if (empty_count > 0)
379
- {
380
- if (first_item_printed)
381
- ss << Color::BRIGHT_BLACK << ",\n"
382
- << Color::RESET;
383
- ss << next_indent << Color::BRIGHT_BLACK << empty_count << " x empty item" << (empty_count > 1 ? "s" : "") << Color::RESET;
384
- first_item_printed = true;
385
- }
386
-
387
- if (item_count > items_to_show)
388
- {
389
- if (first_item_printed)
390
- ss << Color::BRIGHT_BLACK << ",\n"
391
- << Color::RESET;
392
- ss << next_indent << Color::BRIGHT_BLACK << "... " << (item_count - items_to_show) << " more items" << Color::RESET;
393
- }
394
- ss << "\n";
395
- ss << indent << "]";
396
- return ss.str();
397
- }
398
-
399
- // Fallback
400
- return val.to_std_string();
401
- }
402
- }
403
- }
@@ -1,34 +0,0 @@
1
- #pragma once
2
-
3
- #include "types.hpp"
4
- #include "values/function.hpp"
5
- #include "any_value.hpp"
6
-
7
- std::string jspp::JsFunction::to_std_string() const
8
- {
9
- return "function " + name + "() { [native code] }";
10
- }
11
-
12
- jspp::AnyValue jspp::JsFunction::get_property(const std::string &key)
13
- {
14
- auto it = props.find(key);
15
- if (it != props.end())
16
- {
17
- return jspp::AnyValue::resolve_property_for_read(it->second);
18
- }
19
- return jspp::AnyValue::make_undefined();
20
- }
21
-
22
- jspp::AnyValue jspp::JsFunction::set_property(const std::string &key, const AnyValue &value)
23
- {
24
- auto it = props.find(key);
25
- if (it != props.end())
26
- {
27
- return jspp::AnyValue::resolve_property_for_write(it->second, value);
28
- }
29
- else
30
- {
31
- props[key] = value;
32
- return value;
33
- }
34
- }
@@ -1,34 +0,0 @@
1
- #pragma once
2
-
3
- #include "types.hpp"
4
- #include "values/object.hpp"
5
- #include "any_value.hpp"
6
-
7
- std::string jspp::JsObject::to_std_string() const
8
- {
9
- return "[Object Object]";
10
- }
11
-
12
- jspp::AnyValue jspp::JsObject::get_property(const std::string &key)
13
- {
14
- auto it = props.find(key);
15
- if (it != props.end())
16
- {
17
- return jspp::AnyValue::resolve_property_for_read(it->second);
18
- }
19
- return jspp::AnyValue::make_undefined();
20
- }
21
-
22
- jspp::AnyValue jspp::JsObject::set_property(const std::string &key, const AnyValue &value)
23
- {
24
- auto it = props.find(key);
25
- if (it != props.end())
26
- {
27
- return jspp::AnyValue::resolve_property_for_write(it->second, value);
28
- }
29
- else
30
- {
31
- props[key] = value;
32
- return value;
33
- }
34
- }
@@ -1,10 +0,0 @@
1
- #pragma once
2
-
3
- namespace jspp
4
- {
5
- namespace WellKnownSymbols
6
- {
7
- inline std::string iterator = std::string("Symbol(Symbol.iterator)");
8
- inline std::string toString = std::string("Symbol(Symbol.toString)");
9
- }
10
- }