@ugo-studio/jspp 0.3.2 → 0.3.4
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.
- package/README.md +2 -6
- package/dist/cli/args.js +5 -0
- package/dist/cli/compiler.js +1 -1
- package/dist/cli/index.js +4 -2
- package/dist/interpreter/analysis/typeAnalyzer.js +62 -25
- package/dist/interpreter/core/codegen/control-flow-handlers.js +34 -10
- package/dist/interpreter/core/codegen/expression-handlers.js +92 -21
- package/dist/interpreter/core/codegen/function-handlers.js +16 -11
- package/dist/interpreter/core/codegen/helpers.js +5 -1
- package/dist/interpreter/core/codegen/index.js +1 -1
- package/dist/interpreter/core/codegen/statement-handlers.js +15 -11
- package/dist/interpreter/core/traverser.js +1 -2
- package/package.json +2 -2
- package/scripts/precompile-headers.ts +9 -1
- package/src/prelude/library/array.cpp +2 -2
- package/src/prelude/library/math.cpp +8 -8
- package/src/prelude/utils/access.hpp +0 -34
- package/src/prelude/utils/assignment_operators.hpp +96 -0
- package/src/prelude/utils/operators.hpp +39 -4
- package/src/prelude/utils/operators_native.hpp +73 -62
- package/src/prelude/values/array.cpp +17 -17
- package/src/prelude/values/iterator.cpp +262 -210
- package/src/prelude/values/number.cpp +4 -4
- package/src/prelude/values/string.cpp +13 -13
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
#include "types.hpp"
|
|
4
4
|
#include "any_value.hpp"
|
|
5
|
+
#include "exception.hpp"
|
|
5
6
|
#include <cstdint> // For int32_t
|
|
6
7
|
#include <cmath> // For fmod, isnan, isinf, floor, abs, pow
|
|
7
8
|
#include <string> // For std::to_string, std::stod
|
|
@@ -11,10 +12,10 @@
|
|
|
11
12
|
namespace jspp
|
|
12
13
|
{
|
|
13
14
|
// Private namespace for helper functions that implement JS type conversions.
|
|
14
|
-
namespace
|
|
15
|
+
namespace NumberOperators
|
|
15
16
|
{
|
|
16
|
-
// Implements the
|
|
17
|
-
inline double
|
|
17
|
+
// Implements the ToDouble abstract operation from ECMA-262.
|
|
18
|
+
inline double ToDouble(const AnyValue &val)
|
|
18
19
|
{
|
|
19
20
|
if (val.is_number())
|
|
20
21
|
return val.as_double();
|
|
@@ -56,7 +57,7 @@ namespace jspp
|
|
|
56
57
|
// Implements the ToInt32 abstract operation from ECMA-262.
|
|
57
58
|
inline int32_t ToInt32(const AnyValue &val)
|
|
58
59
|
{
|
|
59
|
-
double num =
|
|
60
|
+
double num = ToDouble(val);
|
|
60
61
|
|
|
61
62
|
if (std::isnan(num) || std::isinf(num) || num == 0)
|
|
62
63
|
return 0;
|
|
@@ -72,7 +73,7 @@ namespace jspp
|
|
|
72
73
|
// Implements the ToUint32 abstract operation from ECMA-262.
|
|
73
74
|
inline uint32_t ToUint32(const AnyValue &val)
|
|
74
75
|
{
|
|
75
|
-
double num =
|
|
76
|
+
double num = ToDouble(val);
|
|
76
77
|
if (std::isnan(num) || std::isinf(num) || num == 0)
|
|
77
78
|
return 0;
|
|
78
79
|
double posInt = std::signbit(num) ? -std::floor(std::abs(num)) : std::floor(std::abs(num));
|
|
@@ -109,6 +110,16 @@ namespace jspp
|
|
|
109
110
|
}
|
|
110
111
|
}
|
|
111
112
|
|
|
113
|
+
// --- UNARY NATIVE ---
|
|
114
|
+
inline double plus_native(const AnyValue &val) { return NumberOperators::ToDouble(val); }
|
|
115
|
+
inline double plus_native(double val) { return val; }
|
|
116
|
+
inline double negate_native(const AnyValue &val) { return -NumberOperators::ToDouble(val); }
|
|
117
|
+
inline double negate_native(double val) { return -val; }
|
|
118
|
+
inline double bitwise_not_native(const AnyValue &val) { return static_cast<double>(~NumberOperators::ToInt32(val)); }
|
|
119
|
+
inline double bitwise_not_native(double val) { return static_cast<double>(~static_cast<int32_t>(val)); }
|
|
120
|
+
inline bool logical_not_native(const AnyValue &val) { return !is_truthy(val); }
|
|
121
|
+
inline bool logical_not_native(double val) { return !is_truthy(val); }
|
|
122
|
+
|
|
112
123
|
// --- BASIC EQUALITY ---
|
|
113
124
|
|
|
114
125
|
// Operator === (returns primitive boolean)
|
|
@@ -191,11 +202,11 @@ namespace jspp
|
|
|
191
202
|
}
|
|
192
203
|
if (lhs_type == JsType::Number && rhs_type == JsType::String)
|
|
193
204
|
{
|
|
194
|
-
return lhs.as_double() ==
|
|
205
|
+
return lhs.as_double() == NumberOperators::ToDouble(rhs);
|
|
195
206
|
}
|
|
196
207
|
if (lhs_type == JsType::String && rhs_type == JsType::Number)
|
|
197
208
|
{
|
|
198
|
-
return
|
|
209
|
+
return NumberOperators::ToDouble(lhs) == rhs.as_double();
|
|
199
210
|
}
|
|
200
211
|
if (lhs_type == JsType::Boolean)
|
|
201
212
|
{
|
|
@@ -236,114 +247,114 @@ namespace jspp
|
|
|
236
247
|
|
|
237
248
|
// --- PRIMITIVE ARITHMETIC OPERATORS ---
|
|
238
249
|
inline double add_native(const double &lhs, const double &rhs) { return lhs + rhs; }
|
|
239
|
-
inline double add_native(const AnyValue &lhs, const AnyValue &rhs) { return
|
|
240
|
-
inline double add_native(const AnyValue &lhs, const double &rhs) { return
|
|
241
|
-
inline double add_native(const double &lhs, const AnyValue &rhs) { return lhs +
|
|
250
|
+
inline double add_native(const AnyValue &lhs, const AnyValue &rhs) { return NumberOperators::ToDouble(lhs) + NumberOperators::ToDouble(rhs); }
|
|
251
|
+
inline double add_native(const AnyValue &lhs, const double &rhs) { return NumberOperators::ToDouble(lhs) + rhs; }
|
|
252
|
+
inline double add_native(const double &lhs, const AnyValue &rhs) { return lhs + NumberOperators::ToDouble(rhs); }
|
|
242
253
|
|
|
243
254
|
inline double sub_native(const double &lhs, const double &rhs) { return lhs - rhs; }
|
|
244
|
-
inline double sub_native(const AnyValue &lhs, const AnyValue &rhs) { return
|
|
245
|
-
inline double sub_native(const AnyValue &lhs, const double &rhs) { return
|
|
246
|
-
inline double sub_native(const double &lhs, const AnyValue &rhs) { return lhs -
|
|
255
|
+
inline double sub_native(const AnyValue &lhs, const AnyValue &rhs) { return NumberOperators::ToDouble(lhs) - NumberOperators::ToDouble(rhs); }
|
|
256
|
+
inline double sub_native(const AnyValue &lhs, const double &rhs) { return NumberOperators::ToDouble(lhs) - rhs; }
|
|
257
|
+
inline double sub_native(const double &lhs, const AnyValue &rhs) { return lhs - NumberOperators::ToDouble(rhs); }
|
|
247
258
|
|
|
248
259
|
inline double mul_native(const double &lhs, const double &rhs) { return lhs * rhs; }
|
|
249
|
-
inline double mul_native(const AnyValue &lhs, const AnyValue &rhs) { return
|
|
250
|
-
inline double mul_native(const AnyValue &lhs, const double &rhs) { return
|
|
251
|
-
inline double mul_native(const double &lhs, const AnyValue &rhs) { return lhs *
|
|
260
|
+
inline double mul_native(const AnyValue &lhs, const AnyValue &rhs) { return NumberOperators::ToDouble(lhs) * NumberOperators::ToDouble(rhs); }
|
|
261
|
+
inline double mul_native(const AnyValue &lhs, const double &rhs) { return NumberOperators::ToDouble(lhs) * rhs; }
|
|
262
|
+
inline double mul_native(const double &lhs, const AnyValue &rhs) { return lhs * NumberOperators::ToDouble(rhs); }
|
|
252
263
|
|
|
253
264
|
inline double div_native(const double &lhs, const double &rhs) { return lhs / rhs; }
|
|
254
|
-
inline double div_native(const AnyValue &lhs, const AnyValue &rhs) { return
|
|
255
|
-
inline double div_native(const AnyValue &lhs, const double &rhs) { return
|
|
256
|
-
inline double div_native(const double &lhs, const AnyValue &rhs) { return lhs /
|
|
265
|
+
inline double div_native(const AnyValue &lhs, const AnyValue &rhs) { return NumberOperators::ToDouble(lhs) / NumberOperators::ToDouble(rhs); }
|
|
266
|
+
inline double div_native(const AnyValue &lhs, const double &rhs) { return NumberOperators::ToDouble(lhs) / rhs; }
|
|
267
|
+
inline double div_native(const double &lhs, const AnyValue &rhs) { return lhs / NumberOperators::ToDouble(rhs); }
|
|
257
268
|
|
|
258
269
|
inline double mod_native(const double &lhs, const double &rhs) { return std::fmod(lhs, rhs); }
|
|
259
|
-
inline double mod_native(const AnyValue &lhs, const AnyValue &rhs) { return std::fmod(
|
|
260
|
-
inline double mod_native(const AnyValue &lhs, const double &rhs) { return std::fmod(
|
|
261
|
-
inline double mod_native(const double &lhs, const AnyValue &rhs) { return std::fmod(lhs,
|
|
270
|
+
inline double mod_native(const AnyValue &lhs, const AnyValue &rhs) { return std::fmod(NumberOperators::ToDouble(lhs), NumberOperators::ToDouble(rhs)); }
|
|
271
|
+
inline double mod_native(const AnyValue &lhs, const double &rhs) { return std::fmod(NumberOperators::ToDouble(lhs), rhs); }
|
|
272
|
+
inline double mod_native(const double &lhs, const AnyValue &rhs) { return std::fmod(lhs, NumberOperators::ToDouble(rhs)); }
|
|
262
273
|
|
|
263
274
|
inline double pow_native(const double &lhs, const double &rhs) { return std::pow(lhs, rhs); }
|
|
264
|
-
inline double pow_native(const AnyValue &lhs, const AnyValue &rhs) { return std::pow(
|
|
265
|
-
inline double pow_native(const AnyValue &lhs, const double &rhs) { return std::pow(
|
|
266
|
-
inline double pow_native(const double &lhs, const AnyValue &rhs) { return std::pow(lhs,
|
|
275
|
+
inline double pow_native(const AnyValue &lhs, const AnyValue &rhs) { return std::pow(NumberOperators::ToDouble(lhs), NumberOperators::ToDouble(rhs)); }
|
|
276
|
+
inline double pow_native(const AnyValue &lhs, const double &rhs) { return std::pow(NumberOperators::ToDouble(lhs), rhs); }
|
|
277
|
+
inline double pow_native(const double &lhs, const AnyValue &rhs) { return std::pow(lhs, NumberOperators::ToDouble(rhs)); }
|
|
267
278
|
|
|
268
279
|
// --- PRIMITIVE COMPARISON OPERATORS ---
|
|
269
280
|
inline bool less_than_native(const double &lhs, const double &rhs) { return lhs < rhs; }
|
|
270
|
-
inline bool less_than_native(const AnyValue &lhs, const AnyValue &rhs) { return less_than_native(
|
|
271
|
-
inline bool less_than_native(const AnyValue &lhs, const double &rhs) { return less_than_native(
|
|
272
|
-
inline bool less_than_native(const double &lhs, const AnyValue &rhs) { return less_than_native(lhs,
|
|
281
|
+
inline bool less_than_native(const AnyValue &lhs, const AnyValue &rhs) { return less_than_native(NumberOperators::ToDouble(lhs), NumberOperators::ToDouble(rhs)); }
|
|
282
|
+
inline bool less_than_native(const AnyValue &lhs, const double &rhs) { return less_than_native(NumberOperators::ToDouble(lhs), rhs); }
|
|
283
|
+
inline bool less_than_native(const double &lhs, const AnyValue &rhs) { return less_than_native(lhs, NumberOperators::ToDouble(rhs)); }
|
|
273
284
|
|
|
274
285
|
inline bool greater_than_native(const double &lhs, const double &rhs) { return lhs > rhs; }
|
|
275
|
-
inline bool greater_than_native(const AnyValue &lhs, const AnyValue &rhs) { return greater_than_native(
|
|
276
|
-
inline bool greater_than_native(const AnyValue &lhs, const double &rhs) { return greater_than_native(
|
|
277
|
-
inline bool greater_than_native(const double &lhs, const AnyValue &rhs) { return greater_than_native(lhs,
|
|
286
|
+
inline bool greater_than_native(const AnyValue &lhs, const AnyValue &rhs) { return greater_than_native(NumberOperators::ToDouble(lhs), NumberOperators::ToDouble(rhs)); }
|
|
287
|
+
inline bool greater_than_native(const AnyValue &lhs, const double &rhs) { return greater_than_native(NumberOperators::ToDouble(lhs), rhs); }
|
|
288
|
+
inline bool greater_than_native(const double &lhs, const AnyValue &rhs) { return greater_than_native(lhs, NumberOperators::ToDouble(rhs)); }
|
|
278
289
|
|
|
279
290
|
inline bool less_than_or_equal_native(const double &lhs, const double &rhs) { return lhs <= rhs; }
|
|
280
|
-
inline bool less_than_or_equal_native(const AnyValue &lhs, const AnyValue &rhs) { return less_than_or_equal_native(
|
|
281
|
-
inline bool less_than_or_equal_native(const AnyValue &lhs, const double &rhs) { return less_than_or_equal_native(
|
|
282
|
-
inline bool less_than_or_equal_native(const double &lhs, const AnyValue &rhs) { return less_than_or_equal_native(lhs,
|
|
291
|
+
inline bool less_than_or_equal_native(const AnyValue &lhs, const AnyValue &rhs) { return less_than_or_equal_native(NumberOperators::ToDouble(lhs), NumberOperators::ToDouble(rhs)); }
|
|
292
|
+
inline bool less_than_or_equal_native(const AnyValue &lhs, const double &rhs) { return less_than_or_equal_native(NumberOperators::ToDouble(lhs), rhs); }
|
|
293
|
+
inline bool less_than_or_equal_native(const double &lhs, const AnyValue &rhs) { return less_than_or_equal_native(lhs, NumberOperators::ToDouble(rhs)); }
|
|
283
294
|
|
|
284
295
|
inline bool greater_than_or_equal_native(const double &lhs, const double &rhs) { return lhs >= rhs; }
|
|
285
|
-
inline bool greater_than_or_equal_native(const AnyValue &lhs, const AnyValue &rhs) { return greater_than_or_equal_native(
|
|
286
|
-
inline bool greater_than_or_equal_native(const AnyValue &lhs, const double &rhs) { return greater_than_or_equal_native(
|
|
287
|
-
inline bool greater_than_or_equal_native(const double &lhs, const AnyValue &rhs) { return greater_than_or_equal_native(lhs,
|
|
296
|
+
inline bool greater_than_or_equal_native(const AnyValue &lhs, const AnyValue &rhs) { return greater_than_or_equal_native(NumberOperators::ToDouble(lhs), NumberOperators::ToDouble(rhs)); }
|
|
297
|
+
inline bool greater_than_or_equal_native(const AnyValue &lhs, const double &rhs) { return greater_than_or_equal_native(NumberOperators::ToDouble(lhs), rhs); }
|
|
298
|
+
inline bool greater_than_or_equal_native(const double &lhs, const AnyValue &rhs) { return greater_than_or_equal_native(lhs, NumberOperators::ToDouble(rhs)); }
|
|
288
299
|
|
|
289
300
|
inline bool equal_native(const double &lhs, const double &rhs) { return lhs == rhs; }
|
|
290
|
-
inline bool equal_native(const AnyValue &lhs, const AnyValue &rhs) { return equal_native(
|
|
291
|
-
inline bool equal_native(const AnyValue &lhs, const double &rhs) { return equal_native(
|
|
292
|
-
inline bool equal_native(const double &lhs, const AnyValue &rhs) { return equal_native(lhs,
|
|
301
|
+
inline bool equal_native(const AnyValue &lhs, const AnyValue &rhs) { return equal_native(NumberOperators::ToDouble(lhs), NumberOperators::ToDouble(rhs)); }
|
|
302
|
+
inline bool equal_native(const AnyValue &lhs, const double &rhs) { return equal_native(NumberOperators::ToDouble(lhs), rhs); }
|
|
303
|
+
inline bool equal_native(const double &lhs, const AnyValue &rhs) { return equal_native(lhs, NumberOperators::ToDouble(rhs)); }
|
|
293
304
|
|
|
294
305
|
inline bool not_equal_native(const double &lhs, const double &rhs) { return lhs != rhs; }
|
|
295
|
-
inline bool not_equal_native(const AnyValue &lhs, const AnyValue &rhs) { return not_equal_native(
|
|
296
|
-
inline bool not_equal_native(const AnyValue &lhs, const double &rhs) { return not_equal_native(
|
|
297
|
-
inline bool not_equal_native(const double &lhs, const AnyValue &rhs) { return not_equal_native(lhs,
|
|
306
|
+
inline bool not_equal_native(const AnyValue &lhs, const AnyValue &rhs) { return not_equal_native(NumberOperators::ToDouble(lhs), NumberOperators::ToDouble(rhs)); }
|
|
307
|
+
inline bool not_equal_native(const AnyValue &lhs, const double &rhs) { return not_equal_native(NumberOperators::ToDouble(lhs), rhs); }
|
|
308
|
+
inline bool not_equal_native(const double &lhs, const AnyValue &rhs) { return not_equal_native(lhs, NumberOperators::ToDouble(rhs)); }
|
|
298
309
|
|
|
299
310
|
// --- PRIMITIVE BITWISE OPERATORS ---
|
|
300
311
|
inline double bitwise_and_native(const double &lhs, const double &rhs)
|
|
301
312
|
{
|
|
302
313
|
return static_cast<double>(static_cast<int32_t>(lhs) & static_cast<int32_t>(rhs));
|
|
303
314
|
}
|
|
304
|
-
inline double bitwise_and_native(const AnyValue &lhs, const AnyValue &rhs) { return bitwise_and_native(
|
|
305
|
-
inline double bitwise_and_native(const AnyValue &lhs, const double &rhs) { return bitwise_and_native(
|
|
306
|
-
inline double bitwise_and_native(const double &lhs, const AnyValue &rhs) { return bitwise_and_native(lhs,
|
|
315
|
+
inline double bitwise_and_native(const AnyValue &lhs, const AnyValue &rhs) { return bitwise_and_native(NumberOperators::ToInt32(lhs), NumberOperators::ToInt32(rhs)); }
|
|
316
|
+
inline double bitwise_and_native(const AnyValue &lhs, const double &rhs) { return bitwise_and_native(NumberOperators::ToInt32(lhs), rhs); }
|
|
317
|
+
inline double bitwise_and_native(const double &lhs, const AnyValue &rhs) { return bitwise_and_native(lhs, NumberOperators::ToInt32(rhs)); }
|
|
307
318
|
|
|
308
319
|
inline double bitwise_or_native(const double &lhs, const double &rhs)
|
|
309
320
|
{
|
|
310
321
|
return static_cast<double>(static_cast<int32_t>(lhs) | static_cast<int32_t>(rhs));
|
|
311
322
|
}
|
|
312
|
-
inline double bitwise_or_native(const AnyValue &lhs, const AnyValue &rhs) { return bitwise_or_native(
|
|
313
|
-
inline double bitwise_or_native(const AnyValue &lhs, const double &rhs) { return bitwise_or_native(
|
|
314
|
-
inline double bitwise_or_native(const double &lhs, const AnyValue &rhs) { return bitwise_or_native(lhs,
|
|
323
|
+
inline double bitwise_or_native(const AnyValue &lhs, const AnyValue &rhs) { return bitwise_or_native(NumberOperators::ToInt32(lhs), NumberOperators::ToInt32(rhs)); }
|
|
324
|
+
inline double bitwise_or_native(const AnyValue &lhs, const double &rhs) { return bitwise_or_native(NumberOperators::ToInt32(lhs), rhs); }
|
|
325
|
+
inline double bitwise_or_native(const double &lhs, const AnyValue &rhs) { return bitwise_or_native(lhs, NumberOperators::ToInt32(rhs)); }
|
|
315
326
|
|
|
316
327
|
inline double bitwise_xor_native(const double &lhs, const double &rhs)
|
|
317
328
|
{
|
|
318
329
|
return static_cast<double>(static_cast<int32_t>(lhs) ^ static_cast<int32_t>(rhs));
|
|
319
330
|
}
|
|
320
|
-
inline double bitwise_xor_native(const AnyValue &lhs, const AnyValue &rhs) { return bitwise_xor_native(
|
|
321
|
-
inline double bitwise_xor_native(const AnyValue &lhs, const double &rhs) { return bitwise_xor_native(
|
|
322
|
-
inline double bitwise_xor_native(const double &lhs, const AnyValue &rhs) { return bitwise_xor_native(lhs,
|
|
331
|
+
inline double bitwise_xor_native(const AnyValue &lhs, const AnyValue &rhs) { return bitwise_xor_native(NumberOperators::ToInt32(lhs), NumberOperators::ToInt32(rhs)); }
|
|
332
|
+
inline double bitwise_xor_native(const AnyValue &lhs, const double &rhs) { return bitwise_xor_native(NumberOperators::ToInt32(lhs), rhs); }
|
|
333
|
+
inline double bitwise_xor_native(const double &lhs, const AnyValue &rhs) { return bitwise_xor_native(lhs, NumberOperators::ToInt32(rhs)); }
|
|
323
334
|
|
|
324
335
|
inline double left_shift_native(const double &lhs, const double &rhs)
|
|
325
336
|
{
|
|
326
337
|
return static_cast<double>(static_cast<int32_t>(lhs) << (static_cast<int32_t>(rhs) & 0x1F));
|
|
327
338
|
}
|
|
328
|
-
inline double left_shift_native(const AnyValue &lhs, const AnyValue &rhs) { return left_shift_native(
|
|
329
|
-
inline double left_shift_native(const AnyValue &lhs, const double &rhs) { return left_shift_native(
|
|
330
|
-
inline double left_shift_native(const double &lhs, const AnyValue &rhs) { return left_shift_native(lhs,
|
|
339
|
+
inline double left_shift_native(const AnyValue &lhs, const AnyValue &rhs) { return left_shift_native(NumberOperators::ToInt32(lhs), NumberOperators::ToInt32(rhs)); }
|
|
340
|
+
inline double left_shift_native(const AnyValue &lhs, const double &rhs) { return left_shift_native(NumberOperators::ToInt32(lhs), rhs); }
|
|
341
|
+
inline double left_shift_native(const double &lhs, const AnyValue &rhs) { return left_shift_native(lhs, NumberOperators::ToInt32(rhs)); }
|
|
331
342
|
|
|
332
343
|
inline double right_shift_native(const double &lhs, const double &rhs)
|
|
333
344
|
{
|
|
334
345
|
return static_cast<double>(static_cast<int32_t>(lhs) >> (static_cast<int32_t>(rhs) & 0x1F));
|
|
335
346
|
}
|
|
336
|
-
inline double right_shift_native(const AnyValue &lhs, const AnyValue &rhs) { return right_shift_native(
|
|
337
|
-
inline double right_shift_native(const AnyValue &lhs, const double &rhs) { return right_shift_native(
|
|
338
|
-
inline double right_shift_native(const double &lhs, const AnyValue &rhs) { return right_shift_native(lhs,
|
|
347
|
+
inline double right_shift_native(const AnyValue &lhs, const AnyValue &rhs) { return right_shift_native(NumberOperators::ToInt32(lhs), NumberOperators::ToInt32(rhs)); }
|
|
348
|
+
inline double right_shift_native(const AnyValue &lhs, const double &rhs) { return right_shift_native(NumberOperators::ToInt32(lhs), rhs); }
|
|
349
|
+
inline double right_shift_native(const double &lhs, const AnyValue &rhs) { return right_shift_native(lhs, NumberOperators::ToInt32(rhs)); }
|
|
339
350
|
|
|
340
351
|
inline double unsigned_right_shift_native(const double &lhs, const double &rhs)
|
|
341
352
|
{
|
|
342
353
|
uint32_t l = static_cast<uint32_t>(fmod(lhs, 4294967296.0));
|
|
343
354
|
return static_cast<double>(l >> (static_cast<int32_t>(rhs) & 0x1F));
|
|
344
355
|
}
|
|
345
|
-
inline double unsigned_right_shift_native(const AnyValue &lhs, const AnyValue &rhs) { return unsigned_right_shift_native(
|
|
346
|
-
inline double unsigned_right_shift_native(const AnyValue &lhs, const double &rhs) { return unsigned_right_shift_native(
|
|
347
|
-
inline double unsigned_right_shift_native(const double &lhs, const AnyValue &rhs) { return unsigned_right_shift_native(lhs,
|
|
356
|
+
inline double unsigned_right_shift_native(const AnyValue &lhs, const AnyValue &rhs) { return unsigned_right_shift_native(NumberOperators::ToUint32(lhs), NumberOperators::ToInt32(rhs)); }
|
|
357
|
+
inline double unsigned_right_shift_native(const AnyValue &lhs, const double &rhs) { return unsigned_right_shift_native(NumberOperators::ToUint32(lhs), rhs); }
|
|
358
|
+
inline double unsigned_right_shift_native(const double &lhs, const AnyValue &rhs) { return unsigned_right_shift_native(lhs, NumberOperators::ToInt32(rhs)); }
|
|
348
359
|
|
|
349
|
-
}
|
|
360
|
+
}
|
|
@@ -286,7 +286,7 @@ AnyValue &get_length_desc()
|
|
|
286
286
|
|
|
287
287
|
auto self = thisVal.as_array();
|
|
288
288
|
const auto &new_len_val = args[0];
|
|
289
|
-
double new_len_double =
|
|
289
|
+
double new_len_double = NumberOperators::ToDouble(new_len_val);
|
|
290
290
|
|
|
291
291
|
if (new_len_double < 0 || std::isnan(new_len_double) || std::isinf(new_len_double) || new_len_double != static_cast<uint64_t>(new_len_double))
|
|
292
292
|
{
|
|
@@ -484,7 +484,7 @@ AnyValue &get_at_fn()
|
|
|
484
484
|
{
|
|
485
485
|
auto self = thisVal.as_array();
|
|
486
486
|
double len = static_cast<double>(self->length);
|
|
487
|
-
double relativeIndex = args.empty() ? 0 :
|
|
487
|
+
double relativeIndex = args.empty() ? 0 : NumberOperators::ToDouble(args[0]);
|
|
488
488
|
double k;
|
|
489
489
|
if (relativeIndex >= 0) k = relativeIndex;
|
|
490
490
|
else k = len + relativeIndex;
|
|
@@ -502,7 +502,7 @@ AnyValue &get_includes_fn()
|
|
|
502
502
|
AnyValue searchElement = args.empty() ? Constants::UNDEFINED : args[0];
|
|
503
503
|
double len = static_cast<double>(self->length);
|
|
504
504
|
if (len == 0) return Constants::FALSE;
|
|
505
|
-
double n = (args.size() > 1) ?
|
|
505
|
+
double n = (args.size() > 1) ? NumberOperators::ToDouble(args[1]) : 0;
|
|
506
506
|
double k;
|
|
507
507
|
if (n >= 0) k = n;
|
|
508
508
|
else k = len + n;
|
|
@@ -528,7 +528,7 @@ AnyValue &get_indexOf_fn()
|
|
|
528
528
|
AnyValue searchElement = args.empty() ? Constants::UNDEFINED : args[0];
|
|
529
529
|
double len = static_cast<double>(self->length);
|
|
530
530
|
if (len == 0) return AnyValue::make_number(-1);
|
|
531
|
-
double n = (args.size() > 1) ?
|
|
531
|
+
double n = (args.size() > 1) ? NumberOperators::ToDouble(args[1]) : 0;
|
|
532
532
|
double k;
|
|
533
533
|
if (n >= 0) k = n;
|
|
534
534
|
else k = len + n;
|
|
@@ -554,7 +554,7 @@ AnyValue &get_lastIndexOf_fn()
|
|
|
554
554
|
AnyValue searchElement = args.empty() ? Constants::UNDEFINED : args[0];
|
|
555
555
|
double len = static_cast<double>(self->length);
|
|
556
556
|
if (len == 0) return AnyValue::make_number(-1);
|
|
557
|
-
double n = (args.size() > 1) ?
|
|
557
|
+
double n = (args.size() > 1) ? NumberOperators::ToDouble(args[1]) : len - 1;
|
|
558
558
|
double k;
|
|
559
559
|
if (n >= 0) k = std::min(n, len - 1);
|
|
560
560
|
else k = len + n;
|
|
@@ -897,7 +897,7 @@ AnyValue &get_flat_fn()
|
|
|
897
897
|
static AnyValue fn = AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue
|
|
898
898
|
{
|
|
899
899
|
auto self = thisVal.as_array();
|
|
900
|
-
double depthVal = (args.size() > 0 && !args[0].is_undefined()) ?
|
|
900
|
+
double depthVal = (args.size() > 0 && !args[0].is_undefined()) ? NumberOperators::ToDouble(args[0]) : 1;
|
|
901
901
|
int depth = static_cast<int>(depthVal);
|
|
902
902
|
if (depth < 0) depth = 0;
|
|
903
903
|
|
|
@@ -968,8 +968,8 @@ AnyValue &get_fill_fn()
|
|
|
968
968
|
auto self = thisVal.as_array();
|
|
969
969
|
AnyValue value = args.empty() ? Constants::UNDEFINED : args[0];
|
|
970
970
|
double len = static_cast<double>(self->length);
|
|
971
|
-
double start = (args.size() > 1) ?
|
|
972
|
-
double end = (args.size() > 2 && !args[2].is_undefined()) ?
|
|
971
|
+
double start = (args.size() > 1) ? NumberOperators::ToDouble(args[1]) : 0;
|
|
972
|
+
double end = (args.size() > 2 && !args[2].is_undefined()) ? NumberOperators::ToDouble(args[2]) : len;
|
|
973
973
|
|
|
974
974
|
double k;
|
|
975
975
|
if (start >= 0) k = start; else k = len + start;
|
|
@@ -1041,7 +1041,7 @@ AnyValue &get_sort_fn()
|
|
|
1041
1041
|
|
|
1042
1042
|
if (compareFn.is_function()) {
|
|
1043
1043
|
const AnyValue cmpArgs[] = {a, b};
|
|
1044
|
-
double res =
|
|
1044
|
+
double res = NumberOperators::ToDouble(compareFn.call(Constants::UNDEFINED, std::span<const AnyValue>(cmpArgs, 2)));
|
|
1045
1045
|
return res < 0;
|
|
1046
1046
|
} else {
|
|
1047
1047
|
std::string sA = a.to_std_string();
|
|
@@ -1069,13 +1069,13 @@ AnyValue &get_splice_fn()
|
|
|
1069
1069
|
{
|
|
1070
1070
|
auto self = thisVal.as_array();
|
|
1071
1071
|
double len = static_cast<double>(self->length);
|
|
1072
|
-
double start = args.empty() ? 0 :
|
|
1072
|
+
double start = args.empty() ? 0 : NumberOperators::ToDouble(args[0]);
|
|
1073
1073
|
double actualStart = (start < 0) ? std::max(len + start, 0.0) : std::min(start, len);
|
|
1074
1074
|
|
|
1075
1075
|
uint64_t startIdx = static_cast<uint64_t>(actualStart);
|
|
1076
1076
|
uint64_t deleteCount = 0;
|
|
1077
1077
|
if (args.size() >= 2) {
|
|
1078
|
-
double dc =
|
|
1078
|
+
double dc = NumberOperators::ToDouble(args[1]);
|
|
1079
1079
|
deleteCount = static_cast<uint64_t>(std::max(0.0, std::min(dc, len - startIdx)));
|
|
1080
1080
|
} else if (args.size() == 1) {
|
|
1081
1081
|
deleteCount = len - startIdx;
|
|
@@ -1141,9 +1141,9 @@ AnyValue &get_copyWithin_fn()
|
|
|
1141
1141
|
{
|
|
1142
1142
|
auto self = thisVal.as_array();
|
|
1143
1143
|
double len = static_cast<double>(self->length);
|
|
1144
|
-
double target = args.empty() ? 0 :
|
|
1145
|
-
double start = (args.size() > 1) ?
|
|
1146
|
-
double end = (args.size() > 2 && !args[2].is_undefined()) ?
|
|
1144
|
+
double target = args.empty() ? 0 : NumberOperators::ToDouble(args[0]);
|
|
1145
|
+
double start = (args.size() > 1) ? NumberOperators::ToDouble(args[1]) : 0;
|
|
1146
|
+
double end = (args.size() > 2 && !args[2].is_undefined()) ? NumberOperators::ToDouble(args[2]) : len;
|
|
1147
1147
|
|
|
1148
1148
|
double to;
|
|
1149
1149
|
if (target >= 0) to = target; else to = len + target;
|
|
@@ -1239,9 +1239,9 @@ AnyValue &get_slice_fn()
|
|
|
1239
1239
|
{
|
|
1240
1240
|
auto self = thisVal.as_array();
|
|
1241
1241
|
double len = static_cast<double>(self->length);
|
|
1242
|
-
double start = args.empty() ? 0 :
|
|
1242
|
+
double start = args.empty() ? 0 : NumberOperators::ToDouble(args[0]);
|
|
1243
1243
|
double actualStart = (start < 0) ? std::max(len + start, 0.0) : std::min(start, len);
|
|
1244
|
-
double end = (args.size() < 2 || args[1].is_undefined()) ? len :
|
|
1244
|
+
double end = (args.size() < 2 || args[1].is_undefined()) ? len : NumberOperators::ToDouble(args[1]);
|
|
1245
1245
|
double actualEnd = (end < 0) ? std::max(len + end, 0.0) : std::min(end, len);
|
|
1246
1246
|
|
|
1247
1247
|
std::vector<AnyValue> result;
|
|
@@ -1298,7 +1298,7 @@ AnyValue &get_with_fn()
|
|
|
1298
1298
|
auto copy = thisVal.get_property_with_receiver("slice", thisVal).call(thisVal, {});
|
|
1299
1299
|
|
|
1300
1300
|
double len = static_cast<double>(self->length);
|
|
1301
|
-
double idx = args.empty() ? 0 :
|
|
1301
|
+
double idx = args.empty() ? 0 : NumberOperators::ToDouble(args[0]);
|
|
1302
1302
|
double k;
|
|
1303
1303
|
if (idx >= 0) k = idx; else k = len + idx;
|
|
1304
1304
|
|