@ugo-studio/jspp 0.2.9 → 0.3.0

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 (35) hide show
  1. package/dist/core/codegen/class-handlers.js +6 -6
  2. package/dist/core/codegen/statement-handlers.js +1 -1
  3. package/package.json +1 -1
  4. package/src/prelude/any_value.hpp +362 -362
  5. package/src/prelude/any_value_access.hpp +170 -170
  6. package/src/prelude/any_value_defines.hpp +189 -189
  7. package/src/prelude/any_value_helpers.hpp +374 -374
  8. package/src/prelude/library/array.hpp +185 -185
  9. package/src/prelude/library/console.hpp +111 -111
  10. package/src/prelude/library/error.hpp +112 -112
  11. package/src/prelude/library/function.hpp +10 -10
  12. package/src/prelude/library/math.hpp +307 -307
  13. package/src/prelude/library/object.hpp +275 -275
  14. package/src/prelude/library/process.hpp +39 -39
  15. package/src/prelude/library/promise.hpp +123 -123
  16. package/src/prelude/library/symbol.hpp +52 -52
  17. package/src/prelude/library/timer.hpp +91 -91
  18. package/src/prelude/types.hpp +178 -178
  19. package/src/prelude/utils/access.hpp +411 -411
  20. package/src/prelude/utils/operators.hpp +336 -336
  21. package/src/prelude/values/array.hpp +0 -1
  22. package/src/prelude/values/async_iterator.hpp +83 -83
  23. package/src/prelude/values/function.hpp +82 -82
  24. package/src/prelude/values/helpers/array.hpp +198 -208
  25. package/src/prelude/values/helpers/async_iterator.hpp +275 -275
  26. package/src/prelude/values/helpers/function.hpp +108 -108
  27. package/src/prelude/values/helpers/iterator.hpp +144 -144
  28. package/src/prelude/values/helpers/promise.hpp +253 -253
  29. package/src/prelude/values/helpers/string.hpp +37 -61
  30. package/src/prelude/values/promise.hpp +72 -72
  31. package/src/prelude/values/prototypes/array.hpp +14 -2
  32. package/src/prelude/values/prototypes/iterator.hpp +201 -201
  33. package/src/prelude/values/prototypes/promise.hpp +196 -196
  34. package/src/prelude/values/prototypes/string.hpp +564 -542
  35. package/src/prelude/values/string.hpp +25 -26
@@ -1,308 +1,308 @@
1
- #pragma once
2
-
3
- #include <cmath>
4
- #include <limits>
5
- #include <random>
6
- #include <algorithm>
7
- #include <bit>
8
- #include <numbers>
9
- #include <stdfloat>
10
-
11
- #include "types.hpp"
12
- #include "any_value.hpp"
13
- #include "utils/operators.hpp"
14
- #include "utils/access.hpp"
15
-
16
- namespace jspp {
17
- 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
- }
36
- }
37
- }
38
-
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
- }
1
+ #pragma once
2
+
3
+ #include <cmath>
4
+ #include <limits>
5
+ #include <random>
6
+ #include <algorithm>
7
+ #include <bit>
8
+ #include <numbers>
9
+ #include <stdfloat>
10
+
11
+ #include "types.hpp"
12
+ #include "any_value.hpp"
13
+ #include "utils/operators.hpp"
14
+ #include "utils/access.hpp"
15
+
16
+ namespace jspp {
17
+ 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
+ }
36
+ }
37
+ }
38
+
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
308
  } mathInit;