@ugo-studio/jspp 0.3.0 → 0.3.2

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 (127) hide show
  1. package/LICENSE +25 -25
  2. package/README.md +20 -12
  3. package/dist/cli/args.js +22 -0
  4. package/dist/cli/compiler.js +53 -0
  5. package/dist/cli/index.js +43 -107
  6. package/dist/cli/pch.js +71 -0
  7. package/dist/cli/runner.js +23 -0
  8. package/dist/cli/spinner.js +27 -11
  9. package/dist/cli/transpiler.js +20 -0
  10. package/dist/cli/utils.js +59 -0
  11. package/dist/cli/wasm.js +70 -0
  12. package/dist/index.js +17 -6
  13. package/dist/{analysis → interpreter/analysis}/scope.js +38 -3
  14. package/dist/{analysis → interpreter/analysis}/typeAnalyzer.js +563 -28
  15. package/dist/{core → interpreter/core}/codegen/class-handlers.js +1 -1
  16. package/dist/{core → interpreter/core}/codegen/control-flow-handlers.js +12 -11
  17. package/dist/{core → interpreter/core}/codegen/declaration-handlers.js +28 -9
  18. package/dist/{core → interpreter/core}/codegen/destructuring-handlers.js +9 -4
  19. package/dist/{core → interpreter/core}/codegen/expression-handlers.js +82 -88
  20. package/dist/{core → interpreter/core}/codegen/function-handlers.js +159 -46
  21. package/dist/{core → interpreter/core}/codegen/helpers.js +170 -25
  22. package/dist/interpreter/core/codegen/index.js +156 -0
  23. package/dist/{core → interpreter/core}/codegen/literal-handlers.js +9 -0
  24. package/dist/{core → interpreter/core}/codegen/statement-handlers.js +47 -7
  25. package/package.json +6 -4
  26. package/scripts/precompile-headers.ts +293 -50
  27. package/scripts/setup-compiler.ts +63 -63
  28. package/scripts/setup-emsdk.ts +114 -0
  29. package/src/prelude/any_value.cpp +888 -0
  30. package/src/prelude/any_value.hpp +29 -24
  31. package/src/prelude/{exception_helpers.hpp → exception.cpp} +53 -53
  32. package/src/prelude/exception.hpp +27 -27
  33. package/src/prelude/iterator_instantiations.hpp +10 -0
  34. package/src/prelude/{index.hpp → jspp.hpp} +13 -17
  35. package/src/prelude/library/array.cpp +191 -0
  36. package/src/prelude/library/array.hpp +5 -178
  37. package/src/prelude/library/boolean.cpp +30 -0
  38. package/src/prelude/library/boolean.hpp +14 -0
  39. package/src/prelude/library/console.cpp +125 -0
  40. package/src/prelude/library/console.hpp +9 -97
  41. package/src/prelude/library/error.cpp +100 -0
  42. package/src/prelude/library/error.hpp +8 -108
  43. package/src/prelude/library/function.cpp +69 -0
  44. package/src/prelude/library/function.hpp +6 -5
  45. package/src/prelude/library/global.cpp +98 -0
  46. package/src/prelude/library/global.hpp +12 -28
  47. package/src/prelude/library/global_usings.hpp +15 -0
  48. package/src/prelude/library/math.cpp +261 -0
  49. package/src/prelude/library/math.hpp +8 -288
  50. package/src/prelude/library/object.cpp +379 -0
  51. package/src/prelude/library/object.hpp +5 -267
  52. package/src/prelude/library/performance.cpp +21 -0
  53. package/src/prelude/library/performance.hpp +5 -20
  54. package/src/prelude/library/process.cpp +38 -0
  55. package/src/prelude/library/process.hpp +3 -31
  56. package/src/prelude/library/promise.cpp +131 -0
  57. package/src/prelude/library/promise.hpp +5 -116
  58. package/src/prelude/library/symbol.cpp +56 -0
  59. package/src/prelude/library/symbol.hpp +5 -46
  60. package/src/prelude/library/timer.cpp +88 -0
  61. package/src/prelude/library/timer.hpp +11 -87
  62. package/src/prelude/runtime.cpp +19 -0
  63. package/src/prelude/types.hpp +26 -20
  64. package/src/prelude/utils/access.hpp +123 -32
  65. package/src/prelude/utils/assignment_operators.hpp +119 -99
  66. package/src/prelude/utils/log_any_value/array.hpp +61 -40
  67. package/src/prelude/utils/log_any_value/function.hpp +39 -39
  68. package/src/prelude/utils/log_any_value/log_any_value.hpp +1 -1
  69. package/src/prelude/utils/log_any_value/object.hpp +60 -3
  70. package/src/prelude/utils/log_any_value/primitives.hpp +1 -1
  71. package/src/prelude/utils/operators.hpp +109 -94
  72. package/src/prelude/utils/operators_native.hpp +349 -0
  73. package/src/prelude/utils/well_known_symbols.hpp +24 -24
  74. package/src/prelude/values/array.cpp +1399 -0
  75. package/src/prelude/values/array.hpp +4 -0
  76. package/src/prelude/values/async_iterator.cpp +251 -0
  77. package/src/prelude/values/async_iterator.hpp +60 -32
  78. package/src/prelude/values/boolean.cpp +64 -0
  79. package/src/prelude/values/function.cpp +262 -0
  80. package/src/prelude/values/function.hpp +10 -30
  81. package/src/prelude/values/iterator.cpp +309 -0
  82. package/src/prelude/values/iterator.hpp +33 -64
  83. package/src/prelude/values/number.cpp +221 -0
  84. package/src/prelude/values/object.cpp +200 -0
  85. package/src/prelude/values/object.hpp +4 -0
  86. package/src/prelude/values/promise.cpp +479 -0
  87. package/src/prelude/values/promise.hpp +9 -2
  88. package/src/prelude/values/prototypes/array.hpp +46 -1348
  89. package/src/prelude/values/prototypes/async_iterator.hpp +19 -61
  90. package/src/prelude/values/prototypes/boolean.hpp +24 -0
  91. package/src/prelude/values/prototypes/function.hpp +7 -46
  92. package/src/prelude/values/prototypes/iterator.hpp +15 -191
  93. package/src/prelude/values/prototypes/number.hpp +30 -210
  94. package/src/prelude/values/prototypes/object.hpp +7 -23
  95. package/src/prelude/values/prototypes/promise.hpp +8 -186
  96. package/src/prelude/values/prototypes/string.hpp +28 -553
  97. package/src/prelude/values/prototypes/symbol.hpp +9 -70
  98. package/src/prelude/values/shape.hpp +52 -52
  99. package/src/prelude/values/string.cpp +485 -0
  100. package/src/prelude/values/symbol.cpp +89 -0
  101. package/src/prelude/values/symbol.hpp +101 -101
  102. package/dist/cli/file-utils.js +0 -20
  103. package/dist/cli-utils/args.js +0 -59
  104. package/dist/cli-utils/colors.js +0 -9
  105. package/dist/cli-utils/file-utils.js +0 -20
  106. package/dist/cli-utils/spinner.js +0 -55
  107. package/dist/cli.js +0 -153
  108. package/dist/core/codegen/index.js +0 -86
  109. package/src/prelude/any_value_access.hpp +0 -170
  110. package/src/prelude/any_value_defines.hpp +0 -190
  111. package/src/prelude/any_value_helpers.hpp +0 -374
  112. package/src/prelude/utils/operators_primitive.hpp +0 -337
  113. package/src/prelude/values/helpers/array.hpp +0 -199
  114. package/src/prelude/values/helpers/async_iterator.hpp +0 -275
  115. package/src/prelude/values/helpers/function.hpp +0 -109
  116. package/src/prelude/values/helpers/iterator.hpp +0 -145
  117. package/src/prelude/values/helpers/object.hpp +0 -104
  118. package/src/prelude/values/helpers/promise.hpp +0 -254
  119. package/src/prelude/values/helpers/string.hpp +0 -37
  120. package/src/prelude/values/helpers/symbol.hpp +0 -21
  121. /package/dist/{ast → interpreter/ast}/symbols.js +0 -0
  122. /package/dist/{ast → interpreter/ast}/types.js +0 -0
  123. /package/dist/{core → interpreter/core}/codegen/visitor.js +0 -0
  124. /package/dist/{core → interpreter/core}/constants.js +0 -0
  125. /package/dist/{core → interpreter/core}/error.js +0 -0
  126. /package/dist/{core → interpreter/core}/parser.js +0 -0
  127. /package/dist/{core → interpreter/core}/traverser.js +0 -0
@@ -6,7 +6,9 @@
6
6
  #include <algorithm>
7
7
  #include <bit>
8
8
  #include <numbers>
9
+ #if __has_include(<stdfloat>)
9
10
  #include <stdfloat>
11
+ #endif
10
12
 
11
13
  #include "types.hpp"
12
14
  #include "any_value.hpp"
@@ -15,294 +17,12 @@
15
17
 
16
18
  namespace jspp {
17
19
  namespace Library {
18
- // --- Random Number Generator ---
19
- static std::random_device rd;
20
- static std::mt19937 gen(rd());
21
- static std::uniform_real_distribution<> dis(0.0, 1.0);
22
-
23
- // --- Helpers ---
24
- inline double GetArgAsDouble(std::span<const jspp::AnyValue> args, size_t index) {
25
- if (index >= args.size()) return std::numeric_limits<double>::quiet_NaN();
26
- return Operators_Private::ToNumber(args[index]);
27
- }
28
-
29
- inline jspp::AnyValue MathFunc1(std::span<const jspp::AnyValue> args, double (*func)(double)) {
30
- return jspp::AnyValue::make_number(func(GetArgAsDouble(args, 0)));
31
- }
32
-
33
- inline jspp::AnyValue MathFunc2(std::span<const jspp::AnyValue> args, double (*func)(double, double)) {
34
- return jspp::AnyValue::make_number(func(GetArgAsDouble(args, 0), GetArgAsDouble(args, 1)));
35
- }
20
+ double GetArgAsDouble(std::span<const jspp::AnyValue> args, size_t index);
21
+ jspp::AnyValue MathFunc1(std::span<const jspp::AnyValue> args, double (*func)(double));
22
+ jspp::AnyValue MathFunc2(std::span<const jspp::AnyValue> args, double (*func)(double, double));
36
23
  }
37
24
  }
38
25
 
39
- inline auto Math = jspp::AnyValue::make_object({});
40
-
41
- struct MathInit {
42
- MathInit() {
43
- using namespace jspp;
44
- using namespace jspp::Library;
45
-
46
- // --- Constants ---
47
- auto defConst = [](const std::string& key, double val) {
48
- Math.define_data_property(key, AnyValue::make_number(val), false, false, false);
49
- };
50
-
51
- defConst("E", std::numbers::e);
52
- defConst("LN10", std::numbers::ln10);
53
- defConst("LN2", std::numbers::ln2);
54
- defConst("LOG10E", std::numbers::log10e);
55
- defConst("LOG2E", std::numbers::log2e);
56
- defConst("PI", std::numbers::pi);
57
- defConst("SQRT1_2", std::numbers::sqrt2 / 2.0);
58
- defConst("SQRT2", std::numbers::sqrt2);
59
-
60
- // --- Methods ---
61
-
62
- // Math.abs(x)
63
- Math.define_data_property("abs", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
64
- return AnyValue::make_number(std::abs(GetArgAsDouble(args, 0)));
65
- }, "abs"));
66
-
67
- // Math.acos(x)
68
- Math.define_data_property("acos", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
69
- return MathFunc1(args, std::acos);
70
- }, "acos"));
71
-
72
- // Math.acosh(x)
73
- Math.define_data_property("acosh", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
74
- return MathFunc1(args, std::acosh);
75
- }, "acosh"));
76
-
77
- // Math.asin(x)
78
- Math.define_data_property("asin", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
79
- return MathFunc1(args, std::asin);
80
- }, "asin"));
81
-
82
- // Math.asinh(x)
83
- Math.define_data_property("asinh", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
84
- return MathFunc1(args, std::asinh);
85
- }, "asinh"));
86
-
87
- // Math.atan(x)
88
- Math.define_data_property("atan", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
89
- return MathFunc1(args, std::atan);
90
- }, "atan"));
91
-
92
- // Math.atan2(y, x)
93
- Math.define_data_property("atan2", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
94
- return MathFunc2(args, std::atan2);
95
- }, "atan2"));
96
-
97
- // Math.atanh(x)
98
- Math.define_data_property("atanh", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
99
- return MathFunc1(args, std::atanh);
100
- }, "atanh"));
101
-
102
- // Math.cbrt(x)
103
- Math.define_data_property("cbrt", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
104
- return MathFunc1(args, std::cbrt);
105
- }, "cbrt"));
106
-
107
- // Math.ceil(x)
108
- Math.define_data_property("ceil", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
109
- return MathFunc1(args, std::ceil);
110
- }, "ceil"));
111
-
112
- // Math.clz32(x)
113
- Math.define_data_property("clz32", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
114
- uint32_t val = Operators_Private::ToInt32(args.empty() ? Constants::UNDEFINED : args[0]);
115
- if (val == 0) return AnyValue::make_number(32);
116
- return AnyValue::make_number(std::countl_zero(val));
117
- }, "clz32"));
118
-
119
- // Math.cos(x)
120
- Math.define_data_property("cos", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
121
- return MathFunc1(args, std::cos);
122
- }, "cos"));
123
-
124
- // Math.cosh(x)
125
- Math.define_data_property("cosh", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
126
- return MathFunc1(args, std::cosh);
127
- }, "cosh"));
128
-
129
- // Math.exp(x)
130
- Math.define_data_property("exp", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
131
- return MathFunc1(args, std::exp);
132
- }, "exp"));
133
-
134
- // Math.expm1(x)
135
- Math.define_data_property("expm1", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
136
- return MathFunc1(args, std::expm1);
137
- }, "expm1"));
138
-
139
- // Math.f16round(x)
140
- Math.define_data_property("f16round", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
141
- double d = GetArgAsDouble(args, 0);
142
- #if defined(__STDCPP_FLOAT16_T__)
143
- return AnyValue::make_number(static_cast<double>(static_cast<std::float16_t>(d)));
144
- #else
145
- // Manual fallback if float16_t is not available
146
- // This is a very rough approximation, actual f16round is more complex
147
- float f = static_cast<float>(d);
148
- return AnyValue::make_number(static_cast<double>(f));
149
- #endif
150
- }, "f16round"));
151
-
152
- // Math.floor(x)
153
- Math.define_data_property("floor", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
154
- return MathFunc1(args, std::floor);
155
- }, "floor"));
156
-
157
- // Math.fround(x)
158
- Math.define_data_property("fround", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
159
- double d = GetArgAsDouble(args, 0);
160
- return AnyValue::make_number(static_cast<double>(static_cast<float>(d)));
161
- }, "fround"));
162
-
163
- // Math.hypot(...args)
164
- Math.define_data_property("hypot", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
165
- double result = 0;
166
- for (const auto& arg : args) {
167
- double val = Operators_Private::ToNumber(arg);
168
- if (std::isinf(val)) return AnyValue::make_number(std::numeric_limits<double>::infinity());
169
- result = std::hypot(result, val);
170
- }
171
- return AnyValue::make_number(result);
172
- }, "hypot"));
173
-
174
- // Math.imul(x, y)
175
- Math.define_data_property("imul", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
176
- int32_t a = Operators_Private::ToInt32(args.empty() ? Constants::UNDEFINED : args[0]);
177
- int32_t b = Operators_Private::ToInt32(args.size() < 2 ? Constants::UNDEFINED : args[1]);
178
- return AnyValue::make_number(a * b);
179
- }, "imul"));
180
-
181
- // Math.log(x)
182
- Math.define_data_property("log", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
183
- return MathFunc1(args, std::log);
184
- }, "log"));
185
-
186
- // Math.log10(x)
187
- Math.define_data_property("log10", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
188
- return MathFunc1(args, std::log10);
189
- }, "log10"));
190
-
191
- // Math.log1p(x)
192
- Math.define_data_property("log1p", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
193
- return MathFunc1(args, std::log1p);
194
- }, "log1p"));
195
-
196
- // Math.log2(x)
197
- Math.define_data_property("log2", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
198
- return MathFunc1(args, std::log2);
199
- }, "log2"));
200
-
201
- // Math.max(...args)
202
- Math.define_data_property("max", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
203
- double maxVal = -std::numeric_limits<double>::infinity();
204
- for (const auto& arg : args) {
205
- double val = Operators_Private::ToNumber(arg);
206
- if (std::isnan(val)) return AnyValue::make_nan();
207
- if (val > maxVal) maxVal = val;
208
- }
209
- return AnyValue::make_number(maxVal);
210
- }, "max"));
211
-
212
- // Math.min(...args)
213
- Math.define_data_property("min", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
214
- double minVal = std::numeric_limits<double>::infinity();
215
- for (const auto& arg : args) {
216
- double val = Operators_Private::ToNumber(arg);
217
- if (std::isnan(val)) return AnyValue::make_nan();
218
- if (val < minVal) minVal = val;
219
- }
220
- return AnyValue::make_number(minVal);
221
- }, "min"));
222
-
223
- // Math.pow(base, exponent)
224
- Math.define_data_property("pow", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
225
- return MathFunc2(args, std::pow);
226
- }, "pow"));
227
-
228
- // Math.random()
229
- Math.define_data_property("random", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
230
- return AnyValue::make_number(dis(gen));
231
- }, "random"));
232
-
233
- // Math.round(x)
234
- Math.define_data_property("round", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
235
- double d = GetArgAsDouble(args, 0);
236
- if (std::isnan(d)) return AnyValue::make_nan();
237
- return AnyValue::make_number(std::floor(d + 0.5));
238
- }, "round"));
239
-
240
- // Math.sign(x)
241
- Math.define_data_property("sign", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
242
- double d = GetArgAsDouble(args, 0);
243
- if (std::isnan(d)) return AnyValue::make_nan();
244
- if (d == 0) return AnyValue::make_number(d);
245
- return AnyValue::make_number(d > 0 ? 1.0 : -1.0);
246
- }, "sign"));
247
-
248
- // Math.sin(x)
249
- Math.define_data_property("sin", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
250
- return MathFunc1(args, std::sin);
251
- }, "sin"));
252
-
253
- // Math.sinh(x)
254
- Math.define_data_property("sinh", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
255
- return MathFunc1(args, std::sinh);
256
- }, "sinh"));
257
-
258
- // Math.sqrt(x)
259
- Math.define_data_property("sqrt", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
260
- return MathFunc1(args, std::sqrt);
261
- }, "sqrt"));
262
-
263
- // Math.sumPrecise(iterable)
264
- Math.define_data_property("sumPrecise", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
265
- if (args.empty()) throw Exception::make_exception("Math.sumPrecise requires an iterable", "TypeError");
266
-
267
- auto iterObj = jspp::Access::get_object_iterator(args[0], "iterable");
268
- auto nextFunc = iterObj.get_own_property("next");
269
-
270
- double sum = 0;
271
- // Kahan summation algorithm for better precision
272
- double c = 0;
273
-
274
- while (true) {
275
- auto nextRes = nextFunc.call(iterObj, std::span<const jspp::AnyValue>{}, "next");
276
- if (is_truthy(nextRes.get_own_property("done"))) break;
277
-
278
- double val = Operators_Private::ToNumber(nextRes.get_own_property("value"));
279
- if (std::isnan(val)) {
280
- sum = std::numeric_limits<double>::quiet_NaN();
281
- break;
282
- }
283
-
284
- double y = val - c;
285
- double t = sum + y;
286
- c = (t - sum) - y;
287
- sum = t;
288
- }
289
-
290
- return AnyValue::make_number(sum);
291
- }, "sumPrecise"));
292
-
293
- // Math.tan(x)
294
- Math.define_data_property("tan", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
295
- return MathFunc1(args, std::tan);
296
- }, "tan"));
297
-
298
- // Math.tanh(x)
299
- Math.define_data_property("tanh", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
300
- return MathFunc1(args, std::tanh);
301
- }, "tanh"));
302
-
303
- // Math.trunc(x)
304
- Math.define_data_property("trunc", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
305
- return MathFunc1(args, std::trunc);
306
- }, "trunc"));
307
- }
308
- } mathInit;
26
+ namespace jspp {
27
+ extern AnyValue Math; void init_math();
28
+ }