@ugo-studio/jspp 0.3.0 → 0.3.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 (95) hide show
  1. package/LICENSE +25 -25
  2. package/README.md +20 -12
  3. package/dist/analysis/scope.js +5 -3
  4. package/dist/analysis/typeAnalyzer.js +21 -25
  5. package/dist/cli/index.js +14 -4
  6. package/dist/cli/utils.js +61 -0
  7. package/dist/core/codegen/control-flow-handlers.js +10 -9
  8. package/dist/core/codegen/declaration-handlers.js +10 -3
  9. package/dist/core/codegen/destructuring-handlers.js +9 -4
  10. package/dist/core/codegen/expression-handlers.js +40 -29
  11. package/dist/core/codegen/function-handlers.js +78 -12
  12. package/dist/core/codegen/helpers.js +91 -14
  13. package/dist/core/codegen/index.js +4 -2
  14. package/dist/core/codegen/statement-handlers.js +8 -6
  15. package/package.json +2 -2
  16. package/scripts/precompile-headers.ts +249 -50
  17. package/scripts/setup-compiler.ts +63 -63
  18. package/src/prelude/any_value.cpp +636 -0
  19. package/src/prelude/any_value.hpp +23 -16
  20. package/src/prelude/{exception_helpers.hpp → exception.cpp} +53 -53
  21. package/src/prelude/exception.hpp +27 -27
  22. package/src/prelude/iterator_instantiations.hpp +10 -0
  23. package/src/prelude/{index.hpp → jspp.hpp} +10 -16
  24. package/src/prelude/library/array.cpp +191 -0
  25. package/src/prelude/library/array.hpp +5 -178
  26. package/src/prelude/library/console.cpp +125 -0
  27. package/src/prelude/library/console.hpp +9 -97
  28. package/src/prelude/library/error.cpp +100 -0
  29. package/src/prelude/library/error.hpp +8 -108
  30. package/src/prelude/library/function.cpp +69 -0
  31. package/src/prelude/library/function.hpp +6 -5
  32. package/src/prelude/library/global.cpp +96 -0
  33. package/src/prelude/library/global.hpp +12 -28
  34. package/src/prelude/library/global_usings.hpp +15 -0
  35. package/src/prelude/library/math.cpp +258 -0
  36. package/src/prelude/library/math.hpp +6 -288
  37. package/src/prelude/library/object.cpp +379 -0
  38. package/src/prelude/library/object.hpp +5 -267
  39. package/src/prelude/library/performance.cpp +21 -0
  40. package/src/prelude/library/performance.hpp +5 -20
  41. package/src/prelude/library/process.cpp +38 -0
  42. package/src/prelude/library/process.hpp +3 -31
  43. package/src/prelude/library/promise.cpp +131 -0
  44. package/src/prelude/library/promise.hpp +5 -116
  45. package/src/prelude/library/symbol.cpp +56 -0
  46. package/src/prelude/library/symbol.hpp +5 -46
  47. package/src/prelude/library/timer.cpp +88 -0
  48. package/src/prelude/library/timer.hpp +11 -87
  49. package/src/prelude/runtime.cpp +19 -0
  50. package/src/prelude/types.hpp +17 -12
  51. package/src/prelude/utils/access.hpp +122 -31
  52. package/src/prelude/utils/assignment_operators.hpp +99 -99
  53. package/src/prelude/utils/log_any_value/array.hpp +61 -40
  54. package/src/prelude/utils/log_any_value/function.hpp +39 -39
  55. package/src/prelude/utils/log_any_value/object.hpp +60 -3
  56. package/src/prelude/utils/operators.hpp +25 -10
  57. package/src/prelude/utils/operators_primitive.hpp +336 -336
  58. package/src/prelude/utils/well_known_symbols.hpp +24 -24
  59. package/src/prelude/values/array.cpp +1399 -0
  60. package/src/prelude/values/array.hpp +4 -0
  61. package/src/prelude/values/async_iterator.cpp +251 -0
  62. package/src/prelude/values/async_iterator.hpp +60 -32
  63. package/src/prelude/values/function.cpp +262 -0
  64. package/src/prelude/values/function.hpp +10 -30
  65. package/src/prelude/values/iterator.cpp +309 -0
  66. package/src/prelude/values/iterator.hpp +33 -64
  67. package/src/prelude/values/number.cpp +176 -0
  68. package/src/prelude/values/object.cpp +159 -0
  69. package/src/prelude/values/object.hpp +4 -0
  70. package/src/prelude/values/promise.cpp +479 -0
  71. package/src/prelude/values/promise.hpp +9 -2
  72. package/src/prelude/values/prototypes/array.hpp +46 -1348
  73. package/src/prelude/values/prototypes/async_iterator.hpp +19 -61
  74. package/src/prelude/values/prototypes/function.hpp +7 -46
  75. package/src/prelude/values/prototypes/iterator.hpp +15 -191
  76. package/src/prelude/values/prototypes/number.hpp +23 -210
  77. package/src/prelude/values/prototypes/object.hpp +7 -23
  78. package/src/prelude/values/prototypes/promise.hpp +8 -186
  79. package/src/prelude/values/prototypes/string.hpp +28 -553
  80. package/src/prelude/values/prototypes/symbol.hpp +9 -70
  81. package/src/prelude/values/shape.hpp +52 -52
  82. package/src/prelude/values/string.cpp +485 -0
  83. package/src/prelude/values/symbol.cpp +89 -0
  84. package/src/prelude/values/symbol.hpp +101 -101
  85. package/src/prelude/any_value_access.hpp +0 -170
  86. package/src/prelude/any_value_defines.hpp +0 -190
  87. package/src/prelude/any_value_helpers.hpp +0 -374
  88. package/src/prelude/values/helpers/array.hpp +0 -199
  89. package/src/prelude/values/helpers/async_iterator.hpp +0 -275
  90. package/src/prelude/values/helpers/function.hpp +0 -109
  91. package/src/prelude/values/helpers/iterator.hpp +0 -145
  92. package/src/prelude/values/helpers/object.hpp +0 -104
  93. package/src/prelude/values/helpers/promise.hpp +0 -254
  94. package/src/prelude/values/helpers/string.hpp +0 -37
  95. package/src/prelude/values/helpers/symbol.hpp +0 -21
@@ -18,7 +18,7 @@ namespace jspp
18
18
  {
19
19
  auto arr = val.as_array();
20
20
  size_t item_count = static_cast<size_t>(arr->length);
21
- size_t prop_count = arr->props.size();
21
+ size_t prop_count = arr->props.size() + arr->symbol_props.size();
22
22
 
23
23
  std::string indent(depth * 2, ' ');
24
24
  std::string next_indent((depth + 1) * 2, ' ');
@@ -102,28 +102,36 @@ namespace jspp
102
102
  }
103
103
 
104
104
  // Print properties
105
- if (prop_count > 0)
105
+ for (const auto &pair : arr->props)
106
106
  {
107
- ss << Color::BRIGHT_BLACK << ", " << Color::RESET;
107
+ if (!is_enumerable_property(pair.second))
108
+ continue;
108
109
 
109
- size_t current_prop = 0;
110
- for (const auto &pair : arr->props)
111
- {
112
- if (!is_enumerable_property(pair.second))
113
- continue;
110
+ if (needs_comma)
111
+ ss << Color::BRIGHT_BLACK << ", " << Color::RESET;
114
112
 
115
- if (is_valid_js_identifier(pair.first))
116
- {
117
- ss << pair.first;
118
- }
119
- else
120
- {
121
- ss << "\"" << pair.first << "\"";
122
- }
123
- ss << ": " << to_log_string(pair.second, visited, depth + 1);
124
- if (++current_prop < prop_count)
125
- ss << Color::BRIGHT_BLACK << ", " << Color::RESET;
113
+ if (is_valid_js_identifier(pair.first))
114
+ {
115
+ ss << pair.first;
126
116
  }
117
+ else
118
+ {
119
+ ss << "\"" << pair.first << "\"";
120
+ }
121
+ ss << ": " << to_log_string(pair.second, visited, depth + 1);
122
+ needs_comma = true;
123
+ }
124
+ for (const auto &pair : arr->symbol_props)
125
+ {
126
+ if (!is_enumerable_property(pair.second))
127
+ continue;
128
+
129
+ if (needs_comma)
130
+ ss << Color::BRIGHT_BLACK << ", " << Color::RESET;
131
+
132
+ ss << Color::BLUE << pair.first.to_std_string() << Color::RESET;
133
+ ss << ": " << to_log_string(pair.second, visited, depth + 1);
134
+ needs_comma = true;
127
135
  }
128
136
 
129
137
  ss << " ]";
@@ -193,34 +201,47 @@ namespace jspp
193
201
  ss << next_indent << Color::BRIGHT_BLACK << "... " << (item_count - items_to_show) << " more items" << Color::RESET;
194
202
  }
195
203
  // Print properties
196
- else if (prop_count > 0)
204
+ size_t current_prop = 0;
205
+ for (const auto &pair : arr->props)
197
206
  {
207
+ if (current_prop >= MAX_OBJECT_PROPS)
208
+ break;
209
+ if (!is_enumerable_property(pair.second))
210
+ continue;
211
+
198
212
  if (first_item_printed)
199
213
  ss << Color::BRIGHT_BLACK << ",\n"
200
214
  << Color::RESET;
201
215
 
202
- size_t current_prop = 0;
203
- for (const auto &pair : arr->props)
216
+ ss << next_indent;
217
+ if (is_valid_js_identifier(pair.first))
204
218
  {
205
- if (current_prop >= MAX_OBJECT_PROPS)
206
- break;
207
- if (!is_enumerable_property(pair.second))
208
- continue;
209
-
210
- ss << next_indent;
211
- if (is_valid_js_identifier(pair.first))
212
- {
213
- ss << pair.first;
214
- }
215
- else
216
- {
217
- ss << "\"" << pair.first << "\"";
218
- }
219
- ss << ": " << to_log_string(pair.second, visited, depth + 1);
220
- if (++current_prop < prop_count)
221
- ss << Color::BRIGHT_BLACK << ",\n"
222
- << Color::RESET;
219
+ ss << pair.first;
223
220
  }
221
+ else
222
+ {
223
+ ss << "\"" << pair.first << "\"";
224
+ }
225
+ ss << ": " << to_log_string(pair.second, visited, depth + 1);
226
+ first_item_printed = true;
227
+ current_prop++;
228
+ }
229
+ for (const auto &pair : arr->symbol_props)
230
+ {
231
+ if (current_prop >= MAX_OBJECT_PROPS)
232
+ break;
233
+ if (!is_enumerable_property(pair.second))
234
+ continue;
235
+
236
+ if (first_item_printed)
237
+ ss << Color::BRIGHT_BLACK << ",\n"
238
+ << Color::RESET;
239
+
240
+ ss << next_indent;
241
+ ss << Color::BLUE << pair.first.to_std_string() << Color::RESET;
242
+ ss << ": " << to_log_string(pair.second, visited, depth + 1);
243
+ first_item_printed = true;
244
+ current_prop++;
224
245
  }
225
246
  ss << "\n";
226
247
  ss << indent << "]";
@@ -1,39 +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.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
- }
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.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
+ }
@@ -17,7 +17,7 @@ namespace jspp
17
17
  {
18
18
  auto obj = val.as_object();
19
19
 
20
- size_t prop_count = obj->storage.size();
20
+ size_t prop_count = obj->storage.size() + obj->symbol_props.size();
21
21
  bool use_horizontal_layout = prop_count > 0 && prop_count <= HORIZONTAL_OBJECT_MAX_PROPS;
22
22
 
23
23
  for (size_t i = 0; i < obj->storage.size(); ++i)
@@ -34,19 +34,47 @@ namespace jspp
34
34
  break;
35
35
  }
36
36
  }
37
+ for (const auto &pair : obj->symbol_props)
38
+ {
39
+ if (!is_enumerable_property(pair.second))
40
+ {
41
+ prop_count--;
42
+ continue;
43
+ }
44
+ if (use_horizontal_layout && !is_simple_value(pair.second))
45
+ {
46
+ use_horizontal_layout = false;
47
+ break;
48
+ }
49
+ }
37
50
 
38
51
  std::string indent(depth * 2, ' ');
39
52
  std::string next_indent((depth + 1) * 2, ' ');
40
53
  std::stringstream ss;
54
+ // ... (omitting intermediate parts for brevity in explanation, but including in replacement)
55
+ // Use Symbol.toStringTag for object prefix
56
+ bool has_tag = false;
57
+ try
58
+ {
59
+ auto tag_val = val.get_own_property(AnyValue::from_symbol(WellKnownSymbols::toStringTag));
60
+ if (tag_val.is_string())
61
+ {
62
+ ss << tag_val.to_std_string() << " ";
63
+ has_tag = true;
64
+ }
65
+ }
66
+ catch (...)
67
+ {
68
+ }
41
69
 
42
70
  // Special handling for Error objects
43
71
  try
44
72
  {
45
73
  const AnyValue args[] = {val};
46
- auto is_error = is_truthy(isErrorFn.call(isErrorFn, std::span<const AnyValue>(args, 1)));
74
+ auto is_error = is_truthy(isErrorFn.call(isErrorFn, std::span<const jspp::AnyValue>(args, 1)));
47
75
  if (is_error)
48
76
  {
49
- auto result = errorToStringFn.call(val, std::span<const AnyValue>{});
77
+ auto result = errorToStringFn.call(val, std::span<const jspp::AnyValue>{});
50
78
  if (result.is_string())
51
79
  {
52
80
  ss << result.to_std_string();
@@ -88,6 +116,18 @@ namespace jspp
88
116
  if (++current_prop < prop_count)
89
117
  ss << Color::BRIGHT_BLACK << ", " << Color::RESET;
90
118
  }
119
+ for (const auto &pair : obj->symbol_props)
120
+ {
121
+ if (!is_enumerable_property(pair.second))
122
+ continue;
123
+
124
+ ss << Color::BRIGHT_BLACK << "[" << Color::RESET;
125
+ ss << Color::BLUE << pair.first.to_std_string() << Color::RESET;
126
+ ss << Color::BRIGHT_BLACK << "]: " << Color::RESET;
127
+ ss << to_log_string(pair.second, visited, depth + 1);
128
+ if (++current_prop < prop_count)
129
+ ss << Color::BRIGHT_BLACK << ", " << Color::RESET;
130
+ }
91
131
  ss << " }";
92
132
  }
93
133
  else
@@ -124,6 +164,23 @@ namespace jspp
124
164
  ss << to_log_string(prop_val, visited, depth + 1);
125
165
  props_shown++;
126
166
  }
167
+ for (const auto &pair : obj->symbol_props)
168
+ {
169
+ if (props_shown >= MAX_OBJECT_PROPS)
170
+ break;
171
+
172
+ if (!is_enumerable_property(pair.second))
173
+ continue;
174
+
175
+ if (props_shown > 0)
176
+ ss << ",\n";
177
+
178
+ ss << next_indent;
179
+ ss << Color::BLUE << pair.first.to_std_string() << Color::RESET;
180
+ ss << Color::BRIGHT_BLACK << ": " << Color::RESET;
181
+ ss << to_log_string(pair.second, visited, depth + 1);
182
+ props_shown++;
183
+ }
127
184
  if (prop_count > MAX_OBJECT_PROPS)
128
185
  ss << ",\n"
129
186
  << next_indent << Color::BRIGHT_BLACK << "... " << (prop_count - MAX_OBJECT_PROPS) << " more properties" << Color::RESET;
@@ -89,7 +89,7 @@ namespace jspp
89
89
  inline AnyValue add(const AnyValue &lhs, const AnyValue &rhs)
90
90
  {
91
91
  if (lhs.is_number() && rhs.is_number())
92
- return AnyValue::make_number(add_primitive(lhs.as_double(), rhs.as_double()));
92
+ return AnyValue::make_number(lhs.as_double() + rhs.as_double());
93
93
  if (lhs.is_string() || rhs.is_string())
94
94
  return AnyValue::make_string(lhs.to_std_string() + rhs.to_std_string());
95
95
  return AnyValue::make_number(add_primitive(lhs, rhs));
@@ -97,7 +97,7 @@ namespace jspp
97
97
  inline AnyValue add(const AnyValue &lhs, const double &rhs)
98
98
  {
99
99
  if (lhs.is_number())
100
- return AnyValue::make_number(add_primitive(lhs.as_double(), rhs));
100
+ return AnyValue::make_number(lhs.as_double() + rhs);
101
101
  if (lhs.is_string())
102
102
  return AnyValue::make_string(lhs.to_std_string() + std::to_string(rhs));
103
103
  return AnyValue::make_number(add_primitive(lhs, rhs));
@@ -105,21 +105,36 @@ namespace jspp
105
105
  inline AnyValue add(const double &lhs, const AnyValue &rhs)
106
106
  {
107
107
  if (rhs.is_number())
108
- return AnyValue::make_number(add_primitive(lhs, rhs.as_double()));
108
+ return AnyValue::make_number(lhs + rhs.as_double());
109
109
  if (rhs.is_string())
110
110
  return AnyValue::make_string(std::to_string(lhs) + rhs.to_std_string());
111
111
  return AnyValue::make_number(add_primitive(lhs, rhs));
112
112
  }
113
113
  inline AnyValue add(const double &lhs, const double &rhs)
114
114
  {
115
- return AnyValue::make_number(add_primitive(lhs, rhs));
115
+ return AnyValue::make_number(lhs + rhs);
116
116
  }
117
117
 
118
118
  // Function sub
119
- inline AnyValue sub(const AnyValue &lhs, const AnyValue &rhs) { return AnyValue::make_number(sub_primitive(lhs, rhs)); }
120
- inline AnyValue sub(const AnyValue &lhs, const double &rhs) { return AnyValue::make_number(sub_primitive(lhs, rhs)); }
121
- inline AnyValue sub(const double &lhs, const AnyValue &rhs) { return AnyValue::make_number(sub_primitive(lhs, rhs)); }
122
- inline AnyValue sub(const double &lhs, const double &rhs) { return AnyValue::make_number(sub_primitive(lhs, rhs)); }
119
+ inline AnyValue sub(const AnyValue &lhs, const AnyValue &rhs)
120
+ {
121
+ if (lhs.is_number() && rhs.is_number())
122
+ return AnyValue::make_number(lhs.as_double() - rhs.as_double());
123
+ return AnyValue::make_number(sub_primitive(lhs, rhs));
124
+ }
125
+ inline AnyValue sub(const AnyValue &lhs, const double &rhs)
126
+ {
127
+ if (lhs.is_number())
128
+ return AnyValue::make_number(lhs.as_double() - rhs);
129
+ return AnyValue::make_number(sub_primitive(lhs, rhs));
130
+ }
131
+ inline AnyValue sub(const double &lhs, const AnyValue &rhs)
132
+ {
133
+ if (rhs.is_number())
134
+ return AnyValue::make_number(lhs - rhs.as_double());
135
+ return AnyValue::make_number(sub_primitive(lhs, rhs));
136
+ }
137
+ inline AnyValue sub(const double &lhs, const double &rhs) { return AnyValue::make_number(lhs - rhs); }
123
138
 
124
139
  // Function mul
125
140
  inline AnyValue mul(const AnyValue &lhs, const AnyValue &rhs) { return AnyValue::make_number(mul_primitive(lhs, rhs)); }
@@ -194,7 +209,7 @@ namespace jspp
194
209
  inline AnyValue less_than_or_equal(const AnyValue &lhs, const AnyValue &rhs)
195
210
  {
196
211
  if (lhs.is_string() && rhs.is_string())
197
- return AnyValue::make_boolean(lhs.as_string()->value <= rhs.as_string()->value);
212
+ return AnyValue::make_boolean(lhs.as_string()->value <= rhs.as_string()->value);
198
213
  return AnyValue::make_boolean(less_than_or_equal_primitive(lhs, rhs));
199
214
  }
200
215
  inline AnyValue less_than_or_equal(const AnyValue &lhs, const double &rhs)
@@ -213,7 +228,7 @@ namespace jspp
213
228
  inline AnyValue greater_than_or_equal(const AnyValue &lhs, const AnyValue &rhs)
214
229
  {
215
230
  if (lhs.is_string() && rhs.is_string())
216
- return AnyValue::make_boolean(lhs.as_string()->value >= rhs.as_string()->value);
231
+ return AnyValue::make_boolean(lhs.as_string()->value >= rhs.as_string()->value);
217
232
  return AnyValue::make_boolean(greater_than_or_equal_primitive(lhs, rhs));
218
233
  }
219
234
  inline AnyValue greater_than_or_equal(const AnyValue &lhs, const double &rhs)