@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,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,39 @@
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
+ std::string name = parent->name.value_or("");
24
+ if (!name.empty())
25
+ {
26
+ extends_part = " extends " + name;
27
+ }
28
+ }
29
+ }
30
+ std::string name = fn->name.value_or("");
31
+ return Color::CYAN + std::string("[class ") + (name.empty() ? "(anonymous)" : name) + extends_part + "]" + Color::RESET;
32
+ }
33
+
34
+ auto type_part = fn->is_generator ? "GeneratorFunction" : "Function";
35
+ auto name_part = fn->name.has_value() ? ": " + fn->name.value() : " (anonymous)";
36
+ return Color::CYAN + "[" + type_part + name_part + "]" + Color::RESET;
37
+ }
38
+ }
39
+ }
@@ -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
+ }
@@ -0,0 +1,136 @@
1
+ #pragma once
2
+ #include "types.hpp"
3
+ #include "any_value.hpp"
4
+ #include "library/error.hpp"
5
+ #include "utils/log_any_value/config.hpp"
6
+ #include "utils/log_any_value/helpers.hpp"
7
+ #include "utils/log_any_value/fwd.hpp" // Required for recursive to_log_string call
8
+ #include <string>
9
+ #include <sstream>
10
+ #include <unordered_set>
11
+
12
+ namespace jspp
13
+ {
14
+ namespace LogAnyValue
15
+ {
16
+ inline std::string format_object(const AnyValue &val, std::unordered_set<const void *> &visited, int depth)
17
+ {
18
+ auto obj = val.as_object();
19
+
20
+ size_t prop_count = obj->storage.size();
21
+ bool use_horizontal_layout = prop_count > 0 && prop_count <= HORIZONTAL_OBJECT_MAX_PROPS;
22
+
23
+ for (size_t i = 0; i < obj->storage.size(); ++i)
24
+ {
25
+ const auto& prop_val = obj->storage[i];
26
+ if (!is_enumerable_property(prop_val))
27
+ {
28
+ prop_count--;
29
+ continue;
30
+ }
31
+ if (use_horizontal_layout && !is_simple_value(prop_val))
32
+ {
33
+ use_horizontal_layout = false;
34
+ break;
35
+ }
36
+ }
37
+
38
+ std::string indent(depth * 2, ' ');
39
+ std::string next_indent((depth + 1) * 2, ' ');
40
+ std::stringstream ss;
41
+
42
+ // Special handling for Error objects
43
+ try
44
+ {
45
+ const AnyValue args[] = {val};
46
+ auto is_error = is_truthy(isErrorFn.call(isErrorFn, std::span<const AnyValue>(args, 1)));
47
+ if (is_error)
48
+ {
49
+ auto result = errorToStringFn.call(val, std::span<const AnyValue>{});
50
+ if (result.is_string())
51
+ {
52
+ ss << result.to_std_string();
53
+ if (prop_count == 0)
54
+ {
55
+ return ss.str();
56
+ }
57
+ ss << " ";
58
+ }
59
+ }
60
+ }
61
+ catch (...)
62
+ {
63
+ // ignore
64
+ }
65
+
66
+ if (use_horizontal_layout)
67
+ {
68
+ ss << "{ ";
69
+ size_t current_prop = 0;
70
+ for (size_t i = 0; i < obj->shape->property_names.size(); ++i)
71
+ {
72
+ const auto& key = obj->shape->property_names[i];
73
+ const auto& prop_val = obj->storage[i];
74
+
75
+ if (!is_enumerable_property(prop_val))
76
+ continue;
77
+
78
+ if (is_valid_js_identifier(key))
79
+ {
80
+ ss << key;
81
+ }
82
+ else
83
+ {
84
+ ss << "\"" << key << "\"";
85
+ }
86
+ ss << ": " << to_log_string(prop_val, visited, depth + 1);
87
+ if (++current_prop < prop_count)
88
+ ss << Color::BRIGHT_BLACK << ", " << Color::RESET;
89
+ }
90
+ ss << " }";
91
+ }
92
+ else
93
+ {
94
+ ss << "{";
95
+ if (prop_count > 0)
96
+ {
97
+ ss << "\n";
98
+ size_t props_shown = 0;
99
+ for (size_t i = 0; i < obj->shape->property_names.size(); ++i)
100
+ {
101
+ const auto& key = obj->shape->property_names[i];
102
+ const auto& prop_val = obj->storage[i];
103
+
104
+ if (props_shown >= MAX_OBJECT_PROPS)
105
+ break;
106
+
107
+ if (!is_enumerable_property(prop_val))
108
+ continue;
109
+
110
+ if (props_shown > 0)
111
+ ss << ",\n";
112
+
113
+ ss << next_indent;
114
+ if (is_valid_js_identifier(key))
115
+ {
116
+ ss << key;
117
+ }
118
+ else
119
+ {
120
+ ss << "\"" << key << "\"";
121
+ }
122
+ ss << ": " << to_log_string(prop_val, visited, depth + 1);
123
+ props_shown++;
124
+ }
125
+ if (prop_count > MAX_OBJECT_PROPS)
126
+ ss << ",\n"
127
+ << next_indent << Color::BRIGHT_BLACK << "... " << (prop_count - MAX_OBJECT_PROPS) << " more properties" << Color::RESET;
128
+ ss << "\n"
129
+ << indent;
130
+ }
131
+ ss << "}";
132
+ }
133
+ return ss.str();
134
+ }
135
+ }
136
+ }
@@ -0,0 +1,43 @@
1
+ #pragma once
2
+
3
+ #include "types.hpp"
4
+ #include "any_value.hpp"
5
+ #include "utils/log_any_value/config.hpp"
6
+ #include "utils/log_any_value/helpers.hpp"
7
+ #include <string>
8
+ #include <optional>
9
+
10
+ namespace jspp
11
+ {
12
+ namespace LogAnyValue
13
+ {
14
+ inline std::optional<std::string> format_primitive(const AnyValue &val, int depth)
15
+ {
16
+ if (val.is_uninitialized())
17
+ // THROW
18
+ Exception::throw_uninitialized_reference("#<Object>");
19
+ if (val.is_undefined())
20
+ return Color::BRIGHT_BLACK + std::string("undefined") + Color::RESET;
21
+ if (val.is_null())
22
+ return Color::MAGENTA + std::string("null") + Color::RESET;
23
+ if (val.is_boolean())
24
+ return Color::YELLOW + std::string(val.as_boolean() ? "true" : "false") + Color::RESET;
25
+ if (val.is_number())
26
+ return Color::YELLOW + val.to_std_string() + Color::RESET;
27
+ if (val.is_symbol())
28
+ return Color::BLUE + val.to_std_string() + Color::RESET;
29
+ if (val.is_accessor_descriptor())
30
+ return Color::BLUE + std::string("[Getter/Setter]") + Color::RESET;
31
+
32
+ if (val.is_string())
33
+ {
34
+ const std::string &s = val.as_string()->value;
35
+ if (depth == 0)
36
+ return truncate_string(s);
37
+ return Color::GREEN + std::string("\"") + truncate_string(s) + "\"" + Color::RESET;
38
+ }
39
+
40
+ return std::nullopt; // Not a primitive
41
+ }
42
+ }
43
+ }