@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
@@ -1,337 +1,352 @@
1
- #pragma once
2
-
3
- #include "types.hpp"
4
- #include "any_value.hpp"
5
- #include "operators_primitive.hpp"
6
- #include <cstdint> // For int32_t
7
- #include <cmath> // For fmod, isnan, isinf, floor, abs, pow
8
- #include <string> // For std::to_string, std::stod
9
- #include <algorithm> // For std::all_of
10
- #include <limits> // For numeric_limits
11
-
12
- namespace jspp
13
- {
14
- // Operator === (returns boolean wrapped in AnyValue)
15
- inline const AnyValue is_strictly_equal_to(const AnyValue &lhs, const double &rhs) noexcept
16
- {
17
- return AnyValue::make_boolean(is_strictly_equal_to_primitive(lhs, rhs));
18
- }
19
- inline const AnyValue is_strictly_equal_to(const double &lhs, const AnyValue &rhs) noexcept
20
- {
21
- return AnyValue::make_boolean(is_strictly_equal_to_primitive(lhs, rhs));
22
- }
23
- inline const AnyValue is_strictly_equal_to(const double &lhs, const double &rhs) noexcept
24
- {
25
- return AnyValue::make_boolean(is_strictly_equal_to_primitive(lhs, rhs));
26
- }
27
- inline const AnyValue is_strictly_equal_to(const AnyValue &lhs, const AnyValue &rhs) noexcept
28
- {
29
- return AnyValue::make_boolean(is_strictly_equal_to_primitive(lhs, rhs));
30
- }
31
-
32
- // Operator == (returns boolean wrapped in AnyValue)
33
- inline const AnyValue is_equal_to(const AnyValue &lhs, const double &rhs) noexcept
34
- {
35
- return AnyValue::make_boolean(is_equal_to_primitive(lhs, rhs));
36
- }
37
- inline const AnyValue is_equal_to(const double &lhs, const AnyValue &rhs) noexcept
38
- {
39
- return AnyValue::make_boolean(is_equal_to_primitive(lhs, rhs));
40
- }
41
- inline const AnyValue is_equal_to(const double &lhs, const double &rhs) noexcept
42
- {
43
- return AnyValue::make_boolean(is_equal_to_primitive(lhs, rhs));
44
- }
45
- inline const AnyValue is_equal_to(const AnyValue &lhs, const AnyValue &rhs) noexcept
46
- {
47
- return AnyValue::make_boolean(is_equal_to_primitive(lhs, rhs));
48
- }
49
-
50
- // Operator !== (returns boolean wrapped in AnyValue)
51
- inline const AnyValue not_strictly_equal_to(const AnyValue &lhs, const double &rhs) noexcept
52
- {
53
- return AnyValue::make_boolean(!is_strictly_equal_to_primitive(lhs, rhs));
54
- }
55
- inline const AnyValue not_strictly_equal_to(const double &lhs, const AnyValue &rhs) noexcept
56
- {
57
- return AnyValue::make_boolean(!is_strictly_equal_to_primitive(lhs, rhs));
58
- }
59
- inline const AnyValue not_strictly_equal_to(const double &lhs, const double &rhs) noexcept
60
- {
61
- return AnyValue::make_boolean(!is_strictly_equal_to_primitive(lhs, rhs));
62
- }
63
- inline const AnyValue not_strictly_equal_to(const AnyValue &lhs, const AnyValue &rhs) noexcept
64
- {
65
- return AnyValue::make_boolean(!is_strictly_equal_to_primitive(lhs, rhs));
66
- }
67
-
68
- // Operator != (returns boolean wrapped in AnyValue)
69
- inline const AnyValue not_equal_to(const AnyValue &lhs, const double &rhs) noexcept
70
- {
71
- return AnyValue::make_boolean(!is_equal_to_primitive(lhs, rhs));
72
- }
73
- inline const AnyValue not_equal_to(const double &lhs, const AnyValue &rhs) noexcept
74
- {
75
- return AnyValue::make_boolean(!is_equal_to_primitive(lhs, rhs));
76
- }
77
- inline const AnyValue not_equal_to(const double &lhs, const double &rhs) noexcept
78
- {
79
- return AnyValue::make_boolean(!is_equal_to_primitive(lhs, rhs));
80
- }
81
- inline const AnyValue not_equal_to(const AnyValue &lhs, const AnyValue &rhs) noexcept
82
- {
83
- return AnyValue::make_boolean(!is_equal_to_primitive(lhs, rhs));
84
- }
85
-
86
- // --- BASIC ARITHMETIC ---
87
-
88
- // Function add
89
- inline AnyValue add(const AnyValue &lhs, const AnyValue &rhs)
90
- {
91
- if (lhs.is_number() && rhs.is_number())
92
- return AnyValue::make_number(add_primitive(lhs.as_double(), rhs.as_double()));
93
- if (lhs.is_string() || rhs.is_string())
94
- return AnyValue::make_string(lhs.to_std_string() + rhs.to_std_string());
95
- return AnyValue::make_number(add_primitive(lhs, rhs));
96
- }
97
- inline AnyValue add(const AnyValue &lhs, const double &rhs)
98
- {
99
- if (lhs.is_number())
100
- return AnyValue::make_number(add_primitive(lhs.as_double(), rhs));
101
- if (lhs.is_string())
102
- return AnyValue::make_string(lhs.to_std_string() + std::to_string(rhs));
103
- return AnyValue::make_number(add_primitive(lhs, rhs));
104
- }
105
- inline AnyValue add(const double &lhs, const AnyValue &rhs)
106
- {
107
- if (rhs.is_number())
108
- return AnyValue::make_number(add_primitive(lhs, rhs.as_double()));
109
- if (rhs.is_string())
110
- return AnyValue::make_string(std::to_string(lhs) + rhs.to_std_string());
111
- return AnyValue::make_number(add_primitive(lhs, rhs));
112
- }
113
- inline AnyValue add(const double &lhs, const double &rhs)
114
- {
115
- return AnyValue::make_number(add_primitive(lhs, rhs));
116
- }
117
-
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)); }
123
-
124
- // Function mul
125
- inline AnyValue mul(const AnyValue &lhs, const AnyValue &rhs) { return AnyValue::make_number(mul_primitive(lhs, rhs)); }
126
- inline AnyValue mul(const AnyValue &lhs, const double &rhs) { return AnyValue::make_number(mul_primitive(lhs, rhs)); }
127
- inline AnyValue mul(const double &lhs, const AnyValue &rhs) { return AnyValue::make_number(mul_primitive(lhs, rhs)); }
128
- inline AnyValue mul(const double &lhs, const double &rhs) { return AnyValue::make_number(mul_primitive(lhs, rhs)); }
129
-
130
- // Function div
131
- inline AnyValue div(const AnyValue &lhs, const AnyValue &rhs) { return AnyValue::make_number(div_primitive(lhs, rhs)); }
132
- inline AnyValue div(const AnyValue &lhs, const double &rhs) { return AnyValue::make_number(div_primitive(lhs, rhs)); }
133
- inline AnyValue div(const double &lhs, const AnyValue &rhs) { return AnyValue::make_number(div_primitive(lhs, rhs)); }
134
- inline AnyValue div(const double &lhs, const double &rhs) { return AnyValue::make_number(div_primitive(lhs, rhs)); }
135
-
136
- // Function mod
137
- inline AnyValue mod(const AnyValue &lhs, const AnyValue &rhs) { return AnyValue::make_number(mod_primitive(lhs, rhs)); }
138
- inline AnyValue mod(const AnyValue &lhs, const double &rhs) { return AnyValue::make_number(mod_primitive(lhs, rhs)); }
139
- inline AnyValue mod(const double &lhs, const AnyValue &rhs) { return AnyValue::make_number(mod_primitive(lhs, rhs)); }
140
- inline AnyValue mod(const double &lhs, const double &rhs) { return AnyValue::make_number(mod_primitive(lhs, rhs)); }
141
-
142
- // --- UNARY OPERATORS ---
143
- inline AnyValue plus(const AnyValue &val)
144
- {
145
- return AnyValue::make_number(Operators_Private::ToNumber(val));
146
- }
147
- inline AnyValue negate(const AnyValue &val)
148
- {
149
- return AnyValue::make_number(-Operators_Private::ToNumber(val));
150
- }
151
- inline AnyValue bitwise_not(const AnyValue &val)
152
- {
153
- return AnyValue::make_number(~Operators_Private::ToInt32(val));
154
- }
155
- inline AnyValue logical_not(const AnyValue &val)
156
- {
157
- return AnyValue::make_boolean(!is_truthy(val));
158
- }
159
-
160
- // --- EXPONENTIATION ---
161
- inline AnyValue pow(const AnyValue &lhs, const AnyValue &rhs) { return AnyValue::make_number(pow_primitive(lhs, rhs)); }
162
- inline AnyValue pow(const AnyValue &lhs, const double &rhs) { return AnyValue::make_number(pow_primitive(lhs, rhs)); }
163
- inline AnyValue pow(const double &lhs, const AnyValue &rhs) { return AnyValue::make_number(pow_primitive(lhs, rhs)); }
164
- inline AnyValue pow(const double &lhs, const double &rhs) { return AnyValue::make_number(pow_primitive(lhs, rhs)); }
165
-
166
- // --- COMPARISON OPERATORS ---
167
-
168
- // Less than <
169
- inline AnyValue less_than(const AnyValue &lhs, const AnyValue &rhs)
170
- {
171
- if (lhs.is_string() && rhs.is_string())
172
- return AnyValue::make_boolean(lhs.as_string()->value < rhs.as_string()->value);
173
-
174
- return AnyValue::make_boolean(less_than_primitive(lhs, rhs));
175
- }
176
- inline AnyValue less_than(const AnyValue &lhs, const double &rhs)
177
- {
178
- return AnyValue::make_boolean(less_than_primitive(lhs, rhs));
179
- }
180
- inline AnyValue less_than(const double &lhs, const AnyValue &rhs)
181
- {
182
- return AnyValue::make_boolean(less_than_primitive(lhs, rhs));
183
- }
184
- inline AnyValue less_than(const double &lhs, const double &rhs)
185
- {
186
- return AnyValue::make_boolean(less_than_primitive(lhs, rhs));
187
- }
188
-
189
- inline AnyValue greater_than(const AnyValue &lhs, const AnyValue &rhs) { return less_than(rhs, lhs); }
190
- inline AnyValue greater_than(const AnyValue &lhs, const double &rhs) { return less_than(rhs, lhs); }
191
- inline AnyValue greater_than(const double &lhs, const AnyValue &rhs) { return less_than(rhs, lhs); }
192
- inline AnyValue greater_than(const double &lhs, const double &rhs) { return AnyValue::make_boolean(greater_than_primitive(lhs, rhs)); }
193
-
194
- inline AnyValue less_than_or_equal(const AnyValue &lhs, const AnyValue &rhs)
195
- {
196
- if (lhs.is_string() && rhs.is_string())
197
- return AnyValue::make_boolean(lhs.as_string()->value <= rhs.as_string()->value);
198
- return AnyValue::make_boolean(less_than_or_equal_primitive(lhs, rhs));
199
- }
200
- inline AnyValue less_than_or_equal(const AnyValue &lhs, const double &rhs)
201
- {
202
- return AnyValue::make_boolean(less_than_or_equal_primitive(lhs, rhs));
203
- }
204
- inline AnyValue less_than_or_equal(const double &lhs, const AnyValue &rhs)
205
- {
206
- return AnyValue::make_boolean(less_than_or_equal_primitive(lhs, rhs));
207
- }
208
- inline AnyValue less_than_or_equal(const double &lhs, const double &rhs)
209
- {
210
- return AnyValue::make_boolean(less_than_or_equal_primitive(lhs, rhs));
211
- }
212
-
213
- inline AnyValue greater_than_or_equal(const AnyValue &lhs, const AnyValue &rhs)
214
- {
215
- if (lhs.is_string() && rhs.is_string())
216
- return AnyValue::make_boolean(lhs.as_string()->value >= rhs.as_string()->value);
217
- return AnyValue::make_boolean(greater_than_or_equal_primitive(lhs, rhs));
218
- }
219
- inline AnyValue greater_than_or_equal(const AnyValue &lhs, const double &rhs)
220
- {
221
- return AnyValue::make_boolean(greater_than_or_equal_primitive(lhs, rhs));
222
- }
223
- inline AnyValue greater_than_or_equal(const double &lhs, const AnyValue &rhs)
224
- {
225
- return AnyValue::make_boolean(greater_than_or_equal_primitive(lhs, rhs));
226
- }
227
- inline AnyValue greater_than_or_equal(const double &lhs, const double &rhs)
228
- {
229
- return AnyValue::make_boolean(greater_than_or_equal_primitive(lhs, rhs));
230
- }
231
-
232
- // Equality ==
233
- inline AnyValue equal(const AnyValue &lhs, const AnyValue &rhs)
234
- {
235
- return AnyValue::make_boolean(is_equal_to_primitive(lhs, rhs));
236
- }
237
- inline AnyValue equal(const AnyValue &lhs, const double &rhs)
238
- {
239
- if (lhs.is_number())
240
- return AnyValue::make_boolean(equal_primitive(lhs.as_double(), rhs));
241
- return AnyValue::make_boolean(is_equal_to_primitive(lhs, AnyValue::make_number(rhs)));
242
- }
243
- inline AnyValue equal(const double &lhs, const AnyValue &rhs)
244
- {
245
- if (rhs.is_number())
246
- return AnyValue::make_boolean(equal_primitive(lhs, rhs.as_double()));
247
- return AnyValue::make_boolean(is_equal_to_primitive(rhs, AnyValue::make_number(lhs)));
248
- }
249
- inline AnyValue equal(const double &lhs, const double &rhs)
250
- {
251
- return AnyValue::make_boolean(equal_primitive(lhs, rhs));
252
- }
253
-
254
- inline AnyValue not_equal(const AnyValue &lhs, const AnyValue &rhs) { return AnyValue::make_boolean(!is_equal_to_primitive(lhs, rhs)); }
255
- inline AnyValue not_equal(const AnyValue &lhs, const double &rhs) { return AnyValue::make_boolean(!equal(lhs, rhs).as_boolean()); }
256
- inline AnyValue not_equal(const double &lhs, const AnyValue &rhs) { return AnyValue::make_boolean(!equal(lhs, rhs).as_boolean()); }
257
- inline AnyValue not_equal(const double &lhs, const double &rhs) { return AnyValue::make_boolean(not_equal_primitive(lhs, rhs)); }
258
-
259
- // --- BITWISE OPERATORS ---
260
- inline AnyValue bitwise_xor(const AnyValue &lhs, const AnyValue &rhs) { return AnyValue::make_number(bitwise_xor_primitive(lhs, rhs)); }
261
- inline AnyValue bitwise_xor(const AnyValue &lhs, const double &rhs) { return AnyValue::make_number(bitwise_xor_primitive(lhs, rhs)); }
262
- inline AnyValue bitwise_xor(const double &lhs, const AnyValue &rhs) { return AnyValue::make_number(bitwise_xor_primitive(lhs, rhs)); }
263
- inline AnyValue bitwise_xor(const double &lhs, const double &rhs) { return AnyValue::make_number(bitwise_xor_primitive(lhs, rhs)); }
264
-
265
- inline AnyValue bitwise_and(const AnyValue &lhs, const AnyValue &rhs) { return AnyValue::make_number(bitwise_and_primitive(lhs, rhs)); }
266
- inline AnyValue bitwise_and(const AnyValue &lhs, const double &rhs) { return AnyValue::make_number(bitwise_and_primitive(lhs, rhs)); }
267
- inline AnyValue bitwise_and(const double &lhs, const AnyValue &rhs) { return AnyValue::make_number(bitwise_and_primitive(lhs, rhs)); }
268
- inline AnyValue bitwise_and(const double &lhs, const double &rhs) { return AnyValue::make_number(bitwise_and_primitive(lhs, rhs)); }
269
-
270
- inline AnyValue bitwise_or(const AnyValue &lhs, const AnyValue &rhs) { return AnyValue::make_number(bitwise_or_primitive(lhs, rhs)); }
271
- inline AnyValue bitwise_or(const AnyValue &lhs, const double &rhs) { return AnyValue::make_number(bitwise_or_primitive(lhs, rhs)); }
272
- inline AnyValue bitwise_or(const double &lhs, const AnyValue &rhs) { return AnyValue::make_number(bitwise_or_primitive(lhs, rhs)); }
273
- inline AnyValue bitwise_or(const double &lhs, const double &rhs) { return AnyValue::make_number(bitwise_or_primitive(lhs, rhs)); }
274
-
275
- // --- SHIFT OPERATORS ---
276
- inline AnyValue left_shift(const AnyValue &lhs, const AnyValue &rhs) { return AnyValue::make_number(left_shift_primitive(lhs, rhs)); }
277
- inline AnyValue left_shift(const AnyValue &lhs, const double &rhs) { return AnyValue::make_number(left_shift_primitive(lhs, rhs)); }
278
- inline AnyValue left_shift(const double &lhs, const AnyValue &rhs) { return AnyValue::make_number(left_shift_primitive(lhs, rhs)); }
279
- inline AnyValue left_shift(const double &lhs, const double &rhs) { return AnyValue::make_number(left_shift_primitive(lhs, rhs)); }
280
-
281
- inline AnyValue right_shift(const AnyValue &lhs, const AnyValue &rhs) { return AnyValue::make_number(right_shift_primitive(lhs, rhs)); }
282
- inline AnyValue right_shift(const AnyValue &lhs, const double &rhs) { return AnyValue::make_number(right_shift_primitive(lhs, rhs)); }
283
- inline AnyValue right_shift(const double &lhs, const AnyValue &rhs) { return AnyValue::make_number(right_shift_primitive(lhs, rhs)); }
284
- inline AnyValue right_shift(const double &lhs, const double &rhs) { return AnyValue::make_number(right_shift_primitive(lhs, rhs)); }
285
-
286
- inline AnyValue unsigned_right_shift(const AnyValue &lhs, const AnyValue &rhs) { return AnyValue::make_number(unsigned_right_shift_primitive(lhs, rhs)); }
287
- inline AnyValue unsigned_right_shift(const AnyValue &lhs, const double &rhs) { return AnyValue::make_number(unsigned_right_shift_primitive(lhs, rhs)); }
288
- inline AnyValue unsigned_right_shift(const double &lhs, const AnyValue &rhs) { return AnyValue::make_number(unsigned_right_shift_primitive(lhs, rhs)); }
289
- inline AnyValue unsigned_right_shift(const double &lhs, const double &rhs) { return AnyValue::make_number(unsigned_right_shift_primitive(lhs, rhs)); }
290
-
291
- // --- LOGICAL SHORT-CIRCUITING HELPERS ---
292
- inline AnyValue logical_and(const AnyValue &lhs, const AnyValue &rhs)
293
- {
294
- if (!is_truthy(lhs))
295
- return lhs;
296
- return rhs;
297
- }
298
- inline AnyValue logical_or(const AnyValue &lhs, const AnyValue &rhs)
299
- {
300
- if (is_truthy(lhs))
301
- return lhs;
302
- return rhs;
303
- }
304
- inline AnyValue nullish_coalesce(const AnyValue &lhs, const AnyValue &rhs)
305
- {
306
- if (!lhs.is_null() && !lhs.is_undefined())
307
- return lhs;
308
- return rhs;
309
- }
310
-
311
- // --- LOGICAL ASSIGNMENT HELPERS ---
312
- inline AnyValue &logical_and_assign(AnyValue &lhs, const AnyValue &rhs)
313
- {
314
- if (is_truthy(lhs))
315
- lhs = rhs;
316
- return lhs;
317
- }
318
- inline AnyValue &logical_or_assign(AnyValue &lhs, const AnyValue &rhs)
319
- {
320
- if (!is_truthy(lhs))
321
- lhs = rhs;
322
- return lhs;
323
- }
324
- inline AnyValue &nullish_coalesce_assign(AnyValue &lhs, const AnyValue &rhs)
325
- {
326
- if (lhs.is_null() || lhs.is_undefined())
327
- lhs = rhs;
328
- return lhs;
329
- }
330
-
331
- inline AnyValue undefined_coalesce(const AnyValue &lhs, const AnyValue &rhs)
332
- {
333
- if (!lhs.is_undefined())
334
- return lhs;
335
- return rhs;
336
- }
1
+ #pragma once
2
+
3
+ #include "types.hpp"
4
+ #include "any_value.hpp"
5
+ #include "operators_primitive.hpp"
6
+ #include <cstdint> // For int32_t
7
+ #include <cmath> // For fmod, isnan, isinf, floor, abs, pow
8
+ #include <string> // For std::to_string, std::stod
9
+ #include <algorithm> // For std::all_of
10
+ #include <limits> // For numeric_limits
11
+
12
+ namespace jspp
13
+ {
14
+ // Operator === (returns boolean wrapped in AnyValue)
15
+ inline const AnyValue is_strictly_equal_to(const AnyValue &lhs, const double &rhs) noexcept
16
+ {
17
+ return AnyValue::make_boolean(is_strictly_equal_to_primitive(lhs, rhs));
18
+ }
19
+ inline const AnyValue is_strictly_equal_to(const double &lhs, const AnyValue &rhs) noexcept
20
+ {
21
+ return AnyValue::make_boolean(is_strictly_equal_to_primitive(lhs, rhs));
22
+ }
23
+ inline const AnyValue is_strictly_equal_to(const double &lhs, const double &rhs) noexcept
24
+ {
25
+ return AnyValue::make_boolean(is_strictly_equal_to_primitive(lhs, rhs));
26
+ }
27
+ inline const AnyValue is_strictly_equal_to(const AnyValue &lhs, const AnyValue &rhs) noexcept
28
+ {
29
+ return AnyValue::make_boolean(is_strictly_equal_to_primitive(lhs, rhs));
30
+ }
31
+
32
+ // Operator == (returns boolean wrapped in AnyValue)
33
+ inline const AnyValue is_equal_to(const AnyValue &lhs, const double &rhs) noexcept
34
+ {
35
+ return AnyValue::make_boolean(is_equal_to_primitive(lhs, rhs));
36
+ }
37
+ inline const AnyValue is_equal_to(const double &lhs, const AnyValue &rhs) noexcept
38
+ {
39
+ return AnyValue::make_boolean(is_equal_to_primitive(lhs, rhs));
40
+ }
41
+ inline const AnyValue is_equal_to(const double &lhs, const double &rhs) noexcept
42
+ {
43
+ return AnyValue::make_boolean(is_equal_to_primitive(lhs, rhs));
44
+ }
45
+ inline const AnyValue is_equal_to(const AnyValue &lhs, const AnyValue &rhs) noexcept
46
+ {
47
+ return AnyValue::make_boolean(is_equal_to_primitive(lhs, rhs));
48
+ }
49
+
50
+ // Operator !== (returns boolean wrapped in AnyValue)
51
+ inline const AnyValue not_strictly_equal_to(const AnyValue &lhs, const double &rhs) noexcept
52
+ {
53
+ return AnyValue::make_boolean(!is_strictly_equal_to_primitive(lhs, rhs));
54
+ }
55
+ inline const AnyValue not_strictly_equal_to(const double &lhs, const AnyValue &rhs) noexcept
56
+ {
57
+ return AnyValue::make_boolean(!is_strictly_equal_to_primitive(lhs, rhs));
58
+ }
59
+ inline const AnyValue not_strictly_equal_to(const double &lhs, const double &rhs) noexcept
60
+ {
61
+ return AnyValue::make_boolean(!is_strictly_equal_to_primitive(lhs, rhs));
62
+ }
63
+ inline const AnyValue not_strictly_equal_to(const AnyValue &lhs, const AnyValue &rhs) noexcept
64
+ {
65
+ return AnyValue::make_boolean(!is_strictly_equal_to_primitive(lhs, rhs));
66
+ }
67
+
68
+ // Operator != (returns boolean wrapped in AnyValue)
69
+ inline const AnyValue not_equal_to(const AnyValue &lhs, const double &rhs) noexcept
70
+ {
71
+ return AnyValue::make_boolean(!is_equal_to_primitive(lhs, rhs));
72
+ }
73
+ inline const AnyValue not_equal_to(const double &lhs, const AnyValue &rhs) noexcept
74
+ {
75
+ return AnyValue::make_boolean(!is_equal_to_primitive(lhs, rhs));
76
+ }
77
+ inline const AnyValue not_equal_to(const double &lhs, const double &rhs) noexcept
78
+ {
79
+ return AnyValue::make_boolean(!is_equal_to_primitive(lhs, rhs));
80
+ }
81
+ inline const AnyValue not_equal_to(const AnyValue &lhs, const AnyValue &rhs) noexcept
82
+ {
83
+ return AnyValue::make_boolean(!is_equal_to_primitive(lhs, rhs));
84
+ }
85
+
86
+ // --- BASIC ARITHMETIC ---
87
+
88
+ // Function add
89
+ inline AnyValue add(const AnyValue &lhs, const AnyValue &rhs)
90
+ {
91
+ if (lhs.is_number() && rhs.is_number())
92
+ return AnyValue::make_number(lhs.as_double() + rhs.as_double());
93
+ if (lhs.is_string() || rhs.is_string())
94
+ return AnyValue::make_string(lhs.to_std_string() + rhs.to_std_string());
95
+ return AnyValue::make_number(add_primitive(lhs, rhs));
96
+ }
97
+ inline AnyValue add(const AnyValue &lhs, const double &rhs)
98
+ {
99
+ if (lhs.is_number())
100
+ return AnyValue::make_number(lhs.as_double() + rhs);
101
+ if (lhs.is_string())
102
+ return AnyValue::make_string(lhs.to_std_string() + std::to_string(rhs));
103
+ return AnyValue::make_number(add_primitive(lhs, rhs));
104
+ }
105
+ inline AnyValue add(const double &lhs, const AnyValue &rhs)
106
+ {
107
+ if (rhs.is_number())
108
+ return AnyValue::make_number(lhs + rhs.as_double());
109
+ if (rhs.is_string())
110
+ return AnyValue::make_string(std::to_string(lhs) + rhs.to_std_string());
111
+ return AnyValue::make_number(add_primitive(lhs, rhs));
112
+ }
113
+ inline AnyValue add(const double &lhs, const double &rhs)
114
+ {
115
+ return AnyValue::make_number(lhs + rhs);
116
+ }
117
+
118
+ // Function sub
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); }
138
+
139
+ // Function mul
140
+ inline AnyValue mul(const AnyValue &lhs, const AnyValue &rhs) { return AnyValue::make_number(mul_primitive(lhs, rhs)); }
141
+ inline AnyValue mul(const AnyValue &lhs, const double &rhs) { return AnyValue::make_number(mul_primitive(lhs, rhs)); }
142
+ inline AnyValue mul(const double &lhs, const AnyValue &rhs) { return AnyValue::make_number(mul_primitive(lhs, rhs)); }
143
+ inline AnyValue mul(const double &lhs, const double &rhs) { return AnyValue::make_number(mul_primitive(lhs, rhs)); }
144
+
145
+ // Function div
146
+ inline AnyValue div(const AnyValue &lhs, const AnyValue &rhs) { return AnyValue::make_number(div_primitive(lhs, rhs)); }
147
+ inline AnyValue div(const AnyValue &lhs, const double &rhs) { return AnyValue::make_number(div_primitive(lhs, rhs)); }
148
+ inline AnyValue div(const double &lhs, const AnyValue &rhs) { return AnyValue::make_number(div_primitive(lhs, rhs)); }
149
+ inline AnyValue div(const double &lhs, const double &rhs) { return AnyValue::make_number(div_primitive(lhs, rhs)); }
150
+
151
+ // Function mod
152
+ inline AnyValue mod(const AnyValue &lhs, const AnyValue &rhs) { return AnyValue::make_number(mod_primitive(lhs, rhs)); }
153
+ inline AnyValue mod(const AnyValue &lhs, const double &rhs) { return AnyValue::make_number(mod_primitive(lhs, rhs)); }
154
+ inline AnyValue mod(const double &lhs, const AnyValue &rhs) { return AnyValue::make_number(mod_primitive(lhs, rhs)); }
155
+ inline AnyValue mod(const double &lhs, const double &rhs) { return AnyValue::make_number(mod_primitive(lhs, rhs)); }
156
+
157
+ // --- UNARY OPERATORS ---
158
+ inline AnyValue plus(const AnyValue &val)
159
+ {
160
+ return AnyValue::make_number(Operators_Private::ToNumber(val));
161
+ }
162
+ inline AnyValue negate(const AnyValue &val)
163
+ {
164
+ return AnyValue::make_number(-Operators_Private::ToNumber(val));
165
+ }
166
+ inline AnyValue bitwise_not(const AnyValue &val)
167
+ {
168
+ return AnyValue::make_number(~Operators_Private::ToInt32(val));
169
+ }
170
+ inline AnyValue logical_not(const AnyValue &val)
171
+ {
172
+ return AnyValue::make_boolean(!is_truthy(val));
173
+ }
174
+
175
+ // --- EXPONENTIATION ---
176
+ inline AnyValue pow(const AnyValue &lhs, const AnyValue &rhs) { return AnyValue::make_number(pow_primitive(lhs, rhs)); }
177
+ inline AnyValue pow(const AnyValue &lhs, const double &rhs) { return AnyValue::make_number(pow_primitive(lhs, rhs)); }
178
+ inline AnyValue pow(const double &lhs, const AnyValue &rhs) { return AnyValue::make_number(pow_primitive(lhs, rhs)); }
179
+ inline AnyValue pow(const double &lhs, const double &rhs) { return AnyValue::make_number(pow_primitive(lhs, rhs)); }
180
+
181
+ // --- COMPARISON OPERATORS ---
182
+
183
+ // Less than <
184
+ inline AnyValue less_than(const AnyValue &lhs, const AnyValue &rhs)
185
+ {
186
+ if (lhs.is_string() && rhs.is_string())
187
+ return AnyValue::make_boolean(lhs.as_string()->value < rhs.as_string()->value);
188
+
189
+ return AnyValue::make_boolean(less_than_primitive(lhs, rhs));
190
+ }
191
+ inline AnyValue less_than(const AnyValue &lhs, const double &rhs)
192
+ {
193
+ return AnyValue::make_boolean(less_than_primitive(lhs, rhs));
194
+ }
195
+ inline AnyValue less_than(const double &lhs, const AnyValue &rhs)
196
+ {
197
+ return AnyValue::make_boolean(less_than_primitive(lhs, rhs));
198
+ }
199
+ inline AnyValue less_than(const double &lhs, const double &rhs)
200
+ {
201
+ return AnyValue::make_boolean(less_than_primitive(lhs, rhs));
202
+ }
203
+
204
+ inline AnyValue greater_than(const AnyValue &lhs, const AnyValue &rhs) { return less_than(rhs, lhs); }
205
+ inline AnyValue greater_than(const AnyValue &lhs, const double &rhs) { return less_than(rhs, lhs); }
206
+ inline AnyValue greater_than(const double &lhs, const AnyValue &rhs) { return less_than(rhs, lhs); }
207
+ inline AnyValue greater_than(const double &lhs, const double &rhs) { return AnyValue::make_boolean(greater_than_primitive(lhs, rhs)); }
208
+
209
+ inline AnyValue less_than_or_equal(const AnyValue &lhs, const AnyValue &rhs)
210
+ {
211
+ if (lhs.is_string() && rhs.is_string())
212
+ return AnyValue::make_boolean(lhs.as_string()->value <= rhs.as_string()->value);
213
+ return AnyValue::make_boolean(less_than_or_equal_primitive(lhs, rhs));
214
+ }
215
+ inline AnyValue less_than_or_equal(const AnyValue &lhs, const double &rhs)
216
+ {
217
+ return AnyValue::make_boolean(less_than_or_equal_primitive(lhs, rhs));
218
+ }
219
+ inline AnyValue less_than_or_equal(const double &lhs, const AnyValue &rhs)
220
+ {
221
+ return AnyValue::make_boolean(less_than_or_equal_primitive(lhs, rhs));
222
+ }
223
+ inline AnyValue less_than_or_equal(const double &lhs, const double &rhs)
224
+ {
225
+ return AnyValue::make_boolean(less_than_or_equal_primitive(lhs, rhs));
226
+ }
227
+
228
+ inline AnyValue greater_than_or_equal(const AnyValue &lhs, const AnyValue &rhs)
229
+ {
230
+ if (lhs.is_string() && rhs.is_string())
231
+ return AnyValue::make_boolean(lhs.as_string()->value >= rhs.as_string()->value);
232
+ return AnyValue::make_boolean(greater_than_or_equal_primitive(lhs, rhs));
233
+ }
234
+ inline AnyValue greater_than_or_equal(const AnyValue &lhs, const double &rhs)
235
+ {
236
+ return AnyValue::make_boolean(greater_than_or_equal_primitive(lhs, rhs));
237
+ }
238
+ inline AnyValue greater_than_or_equal(const double &lhs, const AnyValue &rhs)
239
+ {
240
+ return AnyValue::make_boolean(greater_than_or_equal_primitive(lhs, rhs));
241
+ }
242
+ inline AnyValue greater_than_or_equal(const double &lhs, const double &rhs)
243
+ {
244
+ return AnyValue::make_boolean(greater_than_or_equal_primitive(lhs, rhs));
245
+ }
246
+
247
+ // Equality ==
248
+ inline AnyValue equal(const AnyValue &lhs, const AnyValue &rhs)
249
+ {
250
+ return AnyValue::make_boolean(is_equal_to_primitive(lhs, rhs));
251
+ }
252
+ inline AnyValue equal(const AnyValue &lhs, const double &rhs)
253
+ {
254
+ if (lhs.is_number())
255
+ return AnyValue::make_boolean(equal_primitive(lhs.as_double(), rhs));
256
+ return AnyValue::make_boolean(is_equal_to_primitive(lhs, AnyValue::make_number(rhs)));
257
+ }
258
+ inline AnyValue equal(const double &lhs, const AnyValue &rhs)
259
+ {
260
+ if (rhs.is_number())
261
+ return AnyValue::make_boolean(equal_primitive(lhs, rhs.as_double()));
262
+ return AnyValue::make_boolean(is_equal_to_primitive(rhs, AnyValue::make_number(lhs)));
263
+ }
264
+ inline AnyValue equal(const double &lhs, const double &rhs)
265
+ {
266
+ return AnyValue::make_boolean(equal_primitive(lhs, rhs));
267
+ }
268
+
269
+ inline AnyValue not_equal(const AnyValue &lhs, const AnyValue &rhs) { return AnyValue::make_boolean(!is_equal_to_primitive(lhs, rhs)); }
270
+ inline AnyValue not_equal(const AnyValue &lhs, const double &rhs) { return AnyValue::make_boolean(!equal(lhs, rhs).as_boolean()); }
271
+ inline AnyValue not_equal(const double &lhs, const AnyValue &rhs) { return AnyValue::make_boolean(!equal(lhs, rhs).as_boolean()); }
272
+ inline AnyValue not_equal(const double &lhs, const double &rhs) { return AnyValue::make_boolean(not_equal_primitive(lhs, rhs)); }
273
+
274
+ // --- BITWISE OPERATORS ---
275
+ inline AnyValue bitwise_xor(const AnyValue &lhs, const AnyValue &rhs) { return AnyValue::make_number(bitwise_xor_primitive(lhs, rhs)); }
276
+ inline AnyValue bitwise_xor(const AnyValue &lhs, const double &rhs) { return AnyValue::make_number(bitwise_xor_primitive(lhs, rhs)); }
277
+ inline AnyValue bitwise_xor(const double &lhs, const AnyValue &rhs) { return AnyValue::make_number(bitwise_xor_primitive(lhs, rhs)); }
278
+ inline AnyValue bitwise_xor(const double &lhs, const double &rhs) { return AnyValue::make_number(bitwise_xor_primitive(lhs, rhs)); }
279
+
280
+ inline AnyValue bitwise_and(const AnyValue &lhs, const AnyValue &rhs) { return AnyValue::make_number(bitwise_and_primitive(lhs, rhs)); }
281
+ inline AnyValue bitwise_and(const AnyValue &lhs, const double &rhs) { return AnyValue::make_number(bitwise_and_primitive(lhs, rhs)); }
282
+ inline AnyValue bitwise_and(const double &lhs, const AnyValue &rhs) { return AnyValue::make_number(bitwise_and_primitive(lhs, rhs)); }
283
+ inline AnyValue bitwise_and(const double &lhs, const double &rhs) { return AnyValue::make_number(bitwise_and_primitive(lhs, rhs)); }
284
+
285
+ inline AnyValue bitwise_or(const AnyValue &lhs, const AnyValue &rhs) { return AnyValue::make_number(bitwise_or_primitive(lhs, rhs)); }
286
+ inline AnyValue bitwise_or(const AnyValue &lhs, const double &rhs) { return AnyValue::make_number(bitwise_or_primitive(lhs, rhs)); }
287
+ inline AnyValue bitwise_or(const double &lhs, const AnyValue &rhs) { return AnyValue::make_number(bitwise_or_primitive(lhs, rhs)); }
288
+ inline AnyValue bitwise_or(const double &lhs, const double &rhs) { return AnyValue::make_number(bitwise_or_primitive(lhs, rhs)); }
289
+
290
+ // --- SHIFT OPERATORS ---
291
+ inline AnyValue left_shift(const AnyValue &lhs, const AnyValue &rhs) { return AnyValue::make_number(left_shift_primitive(lhs, rhs)); }
292
+ inline AnyValue left_shift(const AnyValue &lhs, const double &rhs) { return AnyValue::make_number(left_shift_primitive(lhs, rhs)); }
293
+ inline AnyValue left_shift(const double &lhs, const AnyValue &rhs) { return AnyValue::make_number(left_shift_primitive(lhs, rhs)); }
294
+ inline AnyValue left_shift(const double &lhs, const double &rhs) { return AnyValue::make_number(left_shift_primitive(lhs, rhs)); }
295
+
296
+ inline AnyValue right_shift(const AnyValue &lhs, const AnyValue &rhs) { return AnyValue::make_number(right_shift_primitive(lhs, rhs)); }
297
+ inline AnyValue right_shift(const AnyValue &lhs, const double &rhs) { return AnyValue::make_number(right_shift_primitive(lhs, rhs)); }
298
+ inline AnyValue right_shift(const double &lhs, const AnyValue &rhs) { return AnyValue::make_number(right_shift_primitive(lhs, rhs)); }
299
+ inline AnyValue right_shift(const double &lhs, const double &rhs) { return AnyValue::make_number(right_shift_primitive(lhs, rhs)); }
300
+
301
+ inline AnyValue unsigned_right_shift(const AnyValue &lhs, const AnyValue &rhs) { return AnyValue::make_number(unsigned_right_shift_primitive(lhs, rhs)); }
302
+ inline AnyValue unsigned_right_shift(const AnyValue &lhs, const double &rhs) { return AnyValue::make_number(unsigned_right_shift_primitive(lhs, rhs)); }
303
+ inline AnyValue unsigned_right_shift(const double &lhs, const AnyValue &rhs) { return AnyValue::make_number(unsigned_right_shift_primitive(lhs, rhs)); }
304
+ inline AnyValue unsigned_right_shift(const double &lhs, const double &rhs) { return AnyValue::make_number(unsigned_right_shift_primitive(lhs, rhs)); }
305
+
306
+ // --- LOGICAL SHORT-CIRCUITING HELPERS ---
307
+ inline AnyValue logical_and(const AnyValue &lhs, const AnyValue &rhs)
308
+ {
309
+ if (!is_truthy(lhs))
310
+ return lhs;
311
+ return rhs;
312
+ }
313
+ inline AnyValue logical_or(const AnyValue &lhs, const AnyValue &rhs)
314
+ {
315
+ if (is_truthy(lhs))
316
+ return lhs;
317
+ return rhs;
318
+ }
319
+ inline AnyValue nullish_coalesce(const AnyValue &lhs, const AnyValue &rhs)
320
+ {
321
+ if (!lhs.is_null() && !lhs.is_undefined())
322
+ return lhs;
323
+ return rhs;
324
+ }
325
+
326
+ // --- LOGICAL ASSIGNMENT HELPERS ---
327
+ inline AnyValue &logical_and_assign(AnyValue &lhs, const AnyValue &rhs)
328
+ {
329
+ if (is_truthy(lhs))
330
+ lhs = rhs;
331
+ return lhs;
332
+ }
333
+ inline AnyValue &logical_or_assign(AnyValue &lhs, const AnyValue &rhs)
334
+ {
335
+ if (!is_truthy(lhs))
336
+ lhs = rhs;
337
+ return lhs;
338
+ }
339
+ inline AnyValue &nullish_coalesce_assign(AnyValue &lhs, const AnyValue &rhs)
340
+ {
341
+ if (lhs.is_null() || lhs.is_undefined())
342
+ lhs = rhs;
343
+ return lhs;
344
+ }
345
+
346
+ inline AnyValue undefined_coalesce(const AnyValue &lhs, const AnyValue &rhs)
347
+ {
348
+ if (!lhs.is_undefined())
349
+ return lhs;
350
+ return rhs;
351
+ }
337
352
  }