@ugo-studio/jspp 0.2.9 → 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 (97) 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/class-handlers.js +6 -6
  8. package/dist/core/codegen/control-flow-handlers.js +10 -9
  9. package/dist/core/codegen/declaration-handlers.js +10 -3
  10. package/dist/core/codegen/destructuring-handlers.js +9 -4
  11. package/dist/core/codegen/expression-handlers.js +40 -29
  12. package/dist/core/codegen/function-handlers.js +78 -12
  13. package/dist/core/codegen/helpers.js +91 -14
  14. package/dist/core/codegen/index.js +4 -2
  15. package/dist/core/codegen/statement-handlers.js +9 -7
  16. package/package.json +2 -2
  17. package/scripts/precompile-headers.ts +249 -50
  18. package/scripts/setup-compiler.ts +63 -63
  19. package/src/prelude/any_value.cpp +636 -0
  20. package/src/prelude/any_value.hpp +369 -362
  21. package/src/prelude/{exception_helpers.hpp → exception.cpp} +53 -53
  22. package/src/prelude/exception.hpp +27 -27
  23. package/src/prelude/iterator_instantiations.hpp +10 -0
  24. package/src/prelude/{index.hpp → jspp.hpp} +10 -16
  25. package/src/prelude/library/array.cpp +191 -0
  26. package/src/prelude/library/array.hpp +13 -186
  27. package/src/prelude/library/console.cpp +125 -0
  28. package/src/prelude/library/console.hpp +24 -112
  29. package/src/prelude/library/error.cpp +100 -0
  30. package/src/prelude/library/error.hpp +13 -113
  31. package/src/prelude/library/function.cpp +69 -0
  32. package/src/prelude/library/function.hpp +11 -10
  33. package/src/prelude/library/global.cpp +96 -0
  34. package/src/prelude/library/global.hpp +12 -28
  35. package/src/prelude/library/global_usings.hpp +15 -0
  36. package/src/prelude/library/math.cpp +258 -0
  37. package/src/prelude/library/math.hpp +26 -308
  38. package/src/prelude/library/object.cpp +379 -0
  39. package/src/prelude/library/object.hpp +14 -276
  40. package/src/prelude/library/performance.cpp +21 -0
  41. package/src/prelude/library/performance.hpp +5 -20
  42. package/src/prelude/library/process.cpp +38 -0
  43. package/src/prelude/library/process.hpp +11 -39
  44. package/src/prelude/library/promise.cpp +131 -0
  45. package/src/prelude/library/promise.hpp +12 -123
  46. package/src/prelude/library/symbol.cpp +56 -0
  47. package/src/prelude/library/symbol.hpp +11 -52
  48. package/src/prelude/library/timer.cpp +88 -0
  49. package/src/prelude/library/timer.hpp +16 -92
  50. package/src/prelude/runtime.cpp +19 -0
  51. package/src/prelude/types.hpp +184 -179
  52. package/src/prelude/utils/access.hpp +502 -411
  53. package/src/prelude/utils/assignment_operators.hpp +99 -99
  54. package/src/prelude/utils/log_any_value/array.hpp +61 -40
  55. package/src/prelude/utils/log_any_value/function.hpp +39 -39
  56. package/src/prelude/utils/log_any_value/object.hpp +60 -3
  57. package/src/prelude/utils/operators.hpp +351 -336
  58. package/src/prelude/utils/operators_primitive.hpp +336 -336
  59. package/src/prelude/utils/well_known_symbols.hpp +24 -24
  60. package/src/prelude/values/array.cpp +1399 -0
  61. package/src/prelude/values/array.hpp +4 -1
  62. package/src/prelude/values/async_iterator.cpp +251 -0
  63. package/src/prelude/values/async_iterator.hpp +111 -83
  64. package/src/prelude/values/function.cpp +262 -0
  65. package/src/prelude/values/function.hpp +62 -82
  66. package/src/prelude/values/iterator.cpp +309 -0
  67. package/src/prelude/values/iterator.hpp +33 -64
  68. package/src/prelude/values/number.cpp +176 -0
  69. package/src/prelude/values/object.cpp +159 -0
  70. package/src/prelude/values/object.hpp +4 -0
  71. package/src/prelude/values/promise.cpp +479 -0
  72. package/src/prelude/values/promise.hpp +79 -72
  73. package/src/prelude/values/prototypes/array.hpp +46 -1336
  74. package/src/prelude/values/prototypes/async_iterator.hpp +19 -61
  75. package/src/prelude/values/prototypes/function.hpp +7 -46
  76. package/src/prelude/values/prototypes/iterator.hpp +25 -201
  77. package/src/prelude/values/prototypes/number.hpp +23 -210
  78. package/src/prelude/values/prototypes/object.hpp +7 -23
  79. package/src/prelude/values/prototypes/promise.hpp +18 -196
  80. package/src/prelude/values/prototypes/string.hpp +39 -542
  81. package/src/prelude/values/prototypes/symbol.hpp +9 -70
  82. package/src/prelude/values/shape.hpp +52 -52
  83. package/src/prelude/values/string.cpp +485 -0
  84. package/src/prelude/values/string.hpp +25 -26
  85. package/src/prelude/values/symbol.cpp +89 -0
  86. package/src/prelude/values/symbol.hpp +101 -101
  87. package/src/prelude/any_value_access.hpp +0 -170
  88. package/src/prelude/any_value_defines.hpp +0 -190
  89. package/src/prelude/any_value_helpers.hpp +0 -374
  90. package/src/prelude/values/helpers/array.hpp +0 -209
  91. package/src/prelude/values/helpers/async_iterator.hpp +0 -275
  92. package/src/prelude/values/helpers/function.hpp +0 -109
  93. package/src/prelude/values/helpers/iterator.hpp +0 -145
  94. package/src/prelude/values/helpers/object.hpp +0 -104
  95. package/src/prelude/values/helpers/promise.hpp +0 -254
  96. package/src/prelude/values/helpers/string.hpp +0 -61
  97. package/src/prelude/values/helpers/symbol.hpp +0 -21
@@ -0,0 +1,96 @@
1
+ #include "jspp.hpp"
2
+ #include "library/global.hpp"
3
+
4
+ namespace jspp {
5
+ jspp::AnyValue GeneratorFunction;
6
+ jspp::AnyValue AsyncFunction;
7
+ jspp::AnyValue AsyncGeneratorFunction;
8
+ jspp::AnyValue global;
9
+
10
+ void initialize_runtime() {
11
+ if (!global.is_undefined()) return;
12
+
13
+ // 1. Initialize core built-ins
14
+ init_symbol();
15
+ init_function_lib();
16
+ init_object();
17
+ init_array();
18
+ init_error();
19
+ init_promise();
20
+ init_math();
21
+ init_console();
22
+
23
+ // 2. Initialize functions and linking
24
+ GeneratorFunction = jspp::AnyValue::make_generator([](jspp::AnyValue, std::vector<jspp::AnyValue>) -> jspp::JsIterator<jspp::AnyValue>
25
+ { co_return jspp::Constants::UNDEFINED; })
26
+ .get_own_property("prototype")
27
+ .get_own_property("constructor");
28
+ AsyncFunction = jspp::AnyValue::make_async_function([](jspp::AnyValue, std::vector<jspp::AnyValue>) -> jspp::JsPromise
29
+ { co_return jspp::Constants::UNDEFINED; })
30
+ .get_own_property("prototype")
31
+ .get_own_property("constructor");
32
+ AsyncGeneratorFunction = jspp::AnyValue::make_async_generator([](jspp::AnyValue, std::vector<jspp::AnyValue>) -> jspp::JsAsyncIterator<jspp::AnyValue>
33
+ { co_return jspp::Constants::UNDEFINED; })
34
+ .get_own_property("prototype")
35
+ .get_own_property("constructor");
36
+
37
+ global = jspp::AnyValue::make_object({
38
+ {"Symbol", Symbol},
39
+ {"process", process},
40
+ {"Function", Function},
41
+ {"GeneratorFunction", GeneratorFunction},
42
+ {"AsyncFunction", AsyncFunction},
43
+ {"AsyncGeneratorFunction", AsyncGeneratorFunction},
44
+ {"console", jspp::console},
45
+ {"performance", performance},
46
+ {"Error", Error},
47
+ {"Promise", Promise},
48
+ {"setTimeout", setTimeout},
49
+ {"clearTimeout", clearTimeout},
50
+ {"setInterval", setInterval},
51
+ {"clearInterval", clearInterval},
52
+ {"Math", jspp::Math},
53
+ {"Object", jspp::Object},
54
+ {"Array", jspp::Array},
55
+ });
56
+
57
+ auto objectProto = ::Object.get_own_property("prototype");
58
+ auto functionProto = ::Function.get_own_property("prototype");
59
+ auto arrayProto = ::Array.get_own_property("prototype");
60
+
61
+ auto toStringTagSym = jspp::AnyValue::from_symbol(jspp::WellKnownSymbols::toStringTag);
62
+
63
+ objectProto.define_data_property(toStringTagSym, jspp::AnyValue::make_string("Object"), true, false, true);
64
+ functionProto.define_data_property(toStringTagSym, jspp::AnyValue::make_string("Function"), true, false, true);
65
+ arrayProto.define_data_property(toStringTagSym, jspp::AnyValue::make_string("Array"), true, false, true);
66
+
67
+ // Important: Link prototypes to Object.prototype
68
+ arrayProto.set_prototype(objectProto);
69
+ functionProto.set_prototype(objectProto);
70
+ ::Error.get_own_property("prototype").set_prototype(objectProto);
71
+ ::Promise.get_own_property("prototype").set_prototype(objectProto);
72
+ ::Symbol.get_own_property("prototype").set_prototype(objectProto);
73
+
74
+ auto generatorFunctionProto = GeneratorFunction.get_own_property("prototype");
75
+ generatorFunctionProto.set_prototype(functionProto);
76
+ generatorFunctionProto.define_data_property(toStringTagSym, jspp::AnyValue::make_string("GeneratorFunction"), true, false, true);
77
+
78
+ auto asyncFunctionProto = AsyncFunction.get_own_property("prototype");
79
+ asyncFunctionProto.set_prototype(functionProto);
80
+ asyncFunctionProto.define_data_property(toStringTagSym, jspp::AnyValue::make_string("AsyncFunction"), true, false, true);
81
+
82
+ auto asyncGeneratorFunctionProto = AsyncGeneratorFunction.get_own_property("prototype");
83
+ asyncGeneratorFunctionProto.set_prototype(functionProto);
84
+ asyncGeneratorFunctionProto.define_data_property(toStringTagSym, jspp::AnyValue::make_string("AsyncGeneratorFunction"), true, false, true);
85
+
86
+ ::Object.set_prototype(functionProto);
87
+ ::Array.set_prototype(functionProto);
88
+ ::Function.set_prototype(functionProto);
89
+ GeneratorFunction.set_prototype(functionProto);
90
+ AsyncFunction.set_prototype(functionProto);
91
+ AsyncGeneratorFunction.set_prototype(functionProto);
92
+ ::Error.set_prototype(functionProto);
93
+ ::Promise.set_prototype(functionProto);
94
+ ::Symbol.set_prototype(functionProto);
95
+ }
96
+ }
@@ -10,32 +10,16 @@
10
10
  #include "library/timer.hpp"
11
11
  #include "library/math.hpp"
12
12
 
13
- inline auto global = jspp::AnyValue::make_object({
14
- {"Symbol", Symbol},
15
- {"process", process},
16
- {"Function", Function},
17
- {"console", console},
18
- {"performance", performance},
19
- {"Error", Error},
20
- {"Promise", Promise},
21
- {"setTimeout", setTimeout},
22
- {"clearTimeout", clearTimeout},
23
- {"setInterval", setInterval},
24
- {"clearInterval", clearInterval},
25
- {"Math", Math},
26
- {"Object", Object},
27
- {"Array", Array},
28
- });
13
+ namespace jspp {
14
+ extern AnyValue GeneratorFunction;
15
+ extern AnyValue AsyncFunction;
16
+ extern AnyValue AsyncGeneratorFunction;
17
+ extern AnyValue global;
29
18
 
30
- struct GlobalInit {
31
- GlobalInit() {
32
- auto objectProto = ::Object.get_own_property("prototype");
33
-
34
- // Tie built-in prototypes to Object.prototype
35
- ::Array.get_own_property("prototype").set_prototype(objectProto);
36
- ::Function.get_own_property("prototype").set_prototype(objectProto);
37
- ::Error.get_own_property("prototype").set_prototype(objectProto);
38
- ::Promise.get_own_property("prototype").set_prototype(objectProto);
39
- ::Symbol.get_own_property("prototype").set_prototype(objectProto);
40
- }
41
- } globalInit;
19
+ void initialize_runtime();
20
+ }
21
+
22
+ using jspp::global;
23
+ using jspp::GeneratorFunction;
24
+ using jspp::AsyncFunction;
25
+ using jspp::AsyncGeneratorFunction;
@@ -0,0 +1,15 @@
1
+ #pragma once
2
+ #include "library/console.hpp"
3
+ #include "library/math.hpp"
4
+ #include "library/object.hpp"
5
+ #include "library/array.hpp"
6
+ #include "library/error.hpp"
7
+ #include "library/promise.hpp"
8
+
9
+ // Global usings for the transpiled code
10
+ using jspp::console;
11
+ using jspp::Math;
12
+ using jspp::Object;
13
+ using jspp::Array;
14
+ using jspp::Error;
15
+ using jspp::Promise;
@@ -0,0 +1,258 @@
1
+ #include "jspp.hpp"
2
+ #include "library/math.hpp"
3
+ #include <numbers>
4
+ #include <random>
5
+
6
+ namespace jspp {
7
+ namespace Library {
8
+ static std::random_device rd;
9
+ static std::mt19937 gen(rd());
10
+ static std::uniform_real_distribution<> dis(0.0, 1.0);
11
+
12
+ double GetArgAsDouble(std::span<const jspp::AnyValue> args, size_t index) {
13
+ if (index >= args.size()) return std::numeric_limits<double>::quiet_NaN();
14
+ return Operators_Private::ToNumber(args[index]);
15
+ }
16
+
17
+ jspp::AnyValue MathFunc1(std::span<const jspp::AnyValue> args, double (*func)(double)) {
18
+ return jspp::AnyValue::make_number(func(GetArgAsDouble(args, 0)));
19
+ }
20
+
21
+ jspp::AnyValue MathFunc2(std::span<const jspp::AnyValue> args, double (*func)(double, double)) {
22
+ return jspp::AnyValue::make_number(func(GetArgAsDouble(args, 0), GetArgAsDouble(args, 1)));
23
+ }
24
+ }
25
+
26
+ jspp::AnyValue Math = jspp::AnyValue::make_object({});
27
+
28
+ struct MathInit {
29
+ MathInit() {
30
+ using namespace jspp::Library;
31
+
32
+ auto defConst = [](const std::string& key, double val) {
33
+ jspp::Math.define_data_property(key, AnyValue::make_number(val), false, false, false);
34
+ };
35
+
36
+ defConst("E", std::numbers::e);
37
+ defConst("LN10", std::numbers::ln10);
38
+ defConst("LN2", std::numbers::ln2);
39
+ defConst("LOG10E", std::numbers::log10e);
40
+ defConst("LOG2E", std::numbers::log2e);
41
+ defConst("PI", std::numbers::pi);
42
+ defConst("SQRT1_2", std::numbers::sqrt2 / 2.0);
43
+ defConst("SQRT2", std::numbers::sqrt2);
44
+
45
+ jspp::Math.define_data_property("abs", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
46
+ return AnyValue::make_number(std::abs(GetArgAsDouble(args, 0)));
47
+ }, "abs"));
48
+
49
+ jspp::Math.define_data_property("acos", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
50
+ return MathFunc1(args, std::acos);
51
+ }, "acos"));
52
+
53
+ jspp::Math.define_data_property("acosh", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
54
+ return MathFunc1(args, std::acosh);
55
+ }, "acosh"));
56
+
57
+ jspp::Math.define_data_property("asin", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
58
+ return MathFunc1(args, std::asin);
59
+ }, "asin"));
60
+
61
+ jspp::Math.define_data_property("asinh", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
62
+ return MathFunc1(args, std::asinh);
63
+ }, "asinh"));
64
+
65
+ jspp::Math.define_data_property("atan", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
66
+ return MathFunc1(args, std::atan);
67
+ }, "atan"));
68
+
69
+ jspp::Math.define_data_property("atan2", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
70
+ return MathFunc2(args, std::atan2);
71
+ }, "atan2"));
72
+
73
+ jspp::Math.define_data_property("atanh", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
74
+ return MathFunc1(args, std::atanh);
75
+ }, "atanh"));
76
+
77
+ jspp::Math.define_data_property("cbrt", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
78
+ return MathFunc1(args, std::cbrt);
79
+ }, "cbrt"));
80
+
81
+ jspp::Math.define_data_property("ceil", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
82
+ return MathFunc1(args, std::ceil);
83
+ }, "ceil"));
84
+
85
+ jspp::Math.define_data_property("clz32", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
86
+ uint32_t val = Operators_Private::ToInt32(args.empty() ? Constants::UNDEFINED : args[0]);
87
+ if (val == 0) return AnyValue::make_number(32);
88
+ return AnyValue::make_number(std::countl_zero(val));
89
+ }, "clz32"));
90
+
91
+ jspp::Math.define_data_property("cos", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
92
+ return MathFunc1(args, std::cos);
93
+ }, "cos"));
94
+
95
+ jspp::Math.define_data_property("cosh", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
96
+ return MathFunc1(args, std::cosh);
97
+ }, "cosh"));
98
+
99
+ jspp::Math.define_data_property("exp", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
100
+ return MathFunc1(args, std::exp);
101
+ }, "exp"));
102
+
103
+ jspp::Math.define_data_property("expm1", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
104
+ return MathFunc1(args, std::expm1);
105
+ }, "expm1"));
106
+
107
+ jspp::Math.define_data_property("floor", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
108
+ return MathFunc1(args, std::floor);
109
+ }, "floor"));
110
+
111
+ jspp::Math.define_data_property("fround", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
112
+ double d = GetArgAsDouble(args, 0);
113
+ return AnyValue::make_number(static_cast<double>(static_cast<float>(d)));
114
+ }, "fround"));
115
+
116
+ jspp::Math.define_data_property("hypot", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
117
+ double result = 0;
118
+ for (const auto& arg : args) {
119
+ double val = Operators_Private::ToNumber(arg);
120
+ if (std::isinf(val)) return AnyValue::make_number(std::numeric_limits<double>::infinity());
121
+ result = std::hypot(result, val);
122
+ }
123
+ return AnyValue::make_number(result);
124
+ }, "hypot"));
125
+
126
+ jspp::Math.define_data_property("imul", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
127
+ int32_t a = Operators_Private::ToInt32(args.empty() ? Constants::UNDEFINED : args[0]);
128
+ int32_t b = Operators_Private::ToInt32(args.size() < 2 ? Constants::UNDEFINED : args[1]);
129
+ return AnyValue::make_number(a * b);
130
+ }, "imul"));
131
+
132
+ jspp::Math.define_data_property("log", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
133
+ return MathFunc1(args, std::log);
134
+ }, "log"));
135
+
136
+ jspp::Math.define_data_property("log10", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
137
+ return MathFunc1(args, std::log10);
138
+ }, "log10"));
139
+
140
+ jspp::Math.define_data_property("log1p", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
141
+ return MathFunc1(args, std::log1p);
142
+ }, "log1p"));
143
+
144
+ jspp::Math.define_data_property("log2", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
145
+ return MathFunc1(args, std::log2);
146
+ }, "log2"));
147
+
148
+ jspp::Math.define_data_property("max", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
149
+ double maxVal = -std::numeric_limits<double>::infinity();
150
+ for (const auto& arg : args) {
151
+ double val = Operators_Private::ToNumber(arg);
152
+ if (std::isnan(val)) return AnyValue::make_nan();
153
+ if (val > maxVal) maxVal = val;
154
+ }
155
+ return AnyValue::make_number(maxVal);
156
+ }, "max"));
157
+
158
+ jspp::Math.define_data_property("min", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
159
+ double minVal = std::numeric_limits<double>::infinity();
160
+ for (const auto& arg : args) {
161
+ double val = Operators_Private::ToNumber(arg);
162
+ if (std::isnan(val)) return AnyValue::make_nan();
163
+ if (val < minVal) minVal = val;
164
+ }
165
+ return AnyValue::make_number(minVal);
166
+ }, "min"));
167
+
168
+ jspp::Math.define_data_property("pow", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
169
+ return MathFunc2(args, std::pow);
170
+ }, "pow"));
171
+
172
+ jspp::Math.define_data_property("random", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
173
+ return AnyValue::make_number(dis(gen));
174
+ }, "random"));
175
+
176
+ jspp::Math.define_data_property("round", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
177
+ double d = GetArgAsDouble(args, 0);
178
+ if (std::isnan(d)) return AnyValue::make_nan();
179
+ return AnyValue::make_number(std::floor(d + 0.5));
180
+ }, "round"));
181
+
182
+ jspp::Math.define_data_property("sign", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
183
+ double d = GetArgAsDouble(args, 0);
184
+ if (std::isnan(d)) return AnyValue::make_nan();
185
+ if (d == 0) return AnyValue::make_number(d);
186
+ return AnyValue::make_number(d > 0 ? 1.0 : -1.0);
187
+ }, "sign"));
188
+
189
+ jspp::Math.define_data_property("sin", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
190
+ return MathFunc1(args, std::sin);
191
+ }, "sin"));
192
+
193
+ jspp::Math.define_data_property("sinh", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
194
+ return MathFunc1(args, std::sinh);
195
+ }, "sinh"));
196
+
197
+ jspp::Math.define_data_property("sqrt", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
198
+ return MathFunc1(args, std::sqrt);
199
+ }, "sqrt"));
200
+
201
+ jspp::Math.define_data_property("tan", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
202
+ return MathFunc1(args, std::tan);
203
+ }, "tan"));
204
+
205
+ jspp::Math.define_data_property("tanh", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
206
+ return MathFunc1(args, std::tanh);
207
+ }, "tanh"));
208
+
209
+ jspp::Math.define_data_property("trunc", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
210
+ return MathFunc1(args, std::trunc);
211
+ }, "trunc"));
212
+
213
+ // Math.f16round(x)
214
+ jspp::Math.define_data_property("f16round", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
215
+ double d = GetArgAsDouble(args, 0);
216
+ #if defined(__STDCPP_FLOAT16_T__)
217
+ return AnyValue::make_number(static_cast<double>(static_cast<std::float16_t>(d)));
218
+ #else
219
+ float f = static_cast<float>(d);
220
+ return AnyValue::make_number(static_cast<double>(f));
221
+ #endif
222
+ }, "f16round"));
223
+ // Math.sumPrecise(iterable)
224
+ jspp::Math.define_data_property("sumPrecise", AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue {
225
+ if (args.empty()) throw Exception::make_exception("Math.sumPrecise requires an iterable", "TypeError");
226
+
227
+ auto iterObj = jspp::Access::get_object_iterator(args[0], "iterable");
228
+ auto nextFunc = iterObj.get_own_property("next");
229
+
230
+ double sum = 0;
231
+ // Kahan summation algorithm for better precision
232
+ double c = 0;
233
+
234
+ while (true) {
235
+ auto nextRes = nextFunc.call(iterObj, std::span<const jspp::AnyValue>{}, "next");
236
+ if (is_truthy(nextRes.get_own_property("done"))) break;
237
+
238
+ double val = Operators_Private::ToNumber(nextRes.get_own_property("value"));
239
+ if (std::isnan(val)) {
240
+ sum = std::numeric_limits<double>::quiet_NaN();
241
+ break;
242
+ }
243
+
244
+ double y = val - c;
245
+ double t = sum + y;
246
+ c = (t - sum) - y;
247
+ sum = t;
248
+ }
249
+
250
+ return AnyValue::make_number(sum);
251
+ }, "sumPrecise"));
252
+ }
253
+ };
254
+ void init_math()
255
+ {
256
+ static MathInit mathInit;
257
+ }
258
+ }