@ugo-studio/jspp 0.3.1 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli/args.js +22 -0
- package/dist/cli/compiler.js +53 -0
- package/dist/cli/index.js +43 -117
- package/dist/cli/pch.js +71 -0
- package/dist/cli/runner.js +23 -0
- package/dist/cli/spinner.js +27 -11
- package/dist/cli/transpiler.js +20 -0
- package/dist/cli/utils.js +25 -27
- package/dist/cli/wasm.js +70 -0
- package/dist/index.js +17 -6
- package/dist/{analysis → interpreter/analysis}/scope.js +34 -1
- package/dist/{analysis → interpreter/analysis}/typeAnalyzer.js +542 -3
- package/dist/{core → interpreter/core}/codegen/class-handlers.js +1 -1
- package/dist/{core → interpreter/core}/codegen/control-flow-handlers.js +2 -2
- package/dist/{core → interpreter/core}/codegen/declaration-handlers.js +21 -9
- package/dist/{core → interpreter/core}/codegen/destructuring-handlers.js +3 -3
- package/dist/{core → interpreter/core}/codegen/expression-handlers.js +44 -61
- package/dist/{core → interpreter/core}/codegen/function-handlers.js +108 -61
- package/dist/{core → interpreter/core}/codegen/helpers.js +79 -11
- package/dist/interpreter/core/codegen/index.js +156 -0
- package/dist/{core → interpreter/core}/codegen/literal-handlers.js +9 -0
- package/dist/{core → interpreter/core}/codegen/statement-handlers.js +39 -1
- package/package.json +6 -4
- package/scripts/precompile-headers.ts +63 -19
- package/scripts/setup-emsdk.ts +114 -0
- package/src/prelude/any_value.cpp +851 -599
- package/src/prelude/any_value.hpp +28 -30
- package/src/prelude/jspp.hpp +3 -1
- package/src/prelude/library/boolean.cpp +30 -0
- package/src/prelude/library/boolean.hpp +14 -0
- package/src/prelude/library/error.cpp +2 -2
- package/src/prelude/library/global.cpp +2 -0
- package/src/prelude/library/math.cpp +46 -43
- package/src/prelude/library/math.hpp +2 -0
- package/src/prelude/library/object.cpp +1 -1
- package/src/prelude/types.hpp +10 -9
- package/src/prelude/utils/access.hpp +2 -2
- package/src/prelude/utils/assignment_operators.hpp +40 -20
- package/src/prelude/utils/log_any_value/log_any_value.hpp +1 -1
- package/src/prelude/utils/log_any_value/primitives.hpp +1 -1
- package/src/prelude/utils/operators.hpp +87 -87
- package/src/prelude/utils/operators_native.hpp +349 -0
- package/src/prelude/utils/well_known_symbols.hpp +13 -13
- package/src/prelude/values/array.cpp +3 -3
- package/src/prelude/values/boolean.cpp +64 -0
- package/src/prelude/values/number.cpp +137 -92
- package/src/prelude/values/object.cpp +163 -122
- package/src/prelude/values/prototypes/boolean.hpp +24 -0
- package/src/prelude/values/prototypes/number.hpp +8 -1
- package/dist/cli/file-utils.js +0 -20
- package/dist/cli-utils/args.js +0 -59
- package/dist/cli-utils/colors.js +0 -9
- package/dist/cli-utils/file-utils.js +0 -20
- package/dist/cli-utils/spinner.js +0 -55
- package/dist/cli.js +0 -153
- package/dist/core/codegen/index.js +0 -88
- package/src/prelude/utils/operators_primitive.hpp +0 -337
- /package/dist/{ast → interpreter/ast}/symbols.js +0 -0
- /package/dist/{ast → interpreter/ast}/types.js +0 -0
- /package/dist/{core → interpreter/core}/codegen/visitor.js +0 -0
- /package/dist/{core → interpreter/core}/constants.js +0 -0
- /package/dist/{core → interpreter/core}/error.js +0 -0
- /package/dist/{core → interpreter/core}/parser.js +0 -0
- /package/dist/{core → interpreter/core}/traverser.js +0 -0
|
@@ -65,8 +65,7 @@ namespace jspp
|
|
|
65
65
|
|
|
66
66
|
// default ctor (Undefined)
|
|
67
67
|
AnyValue() noexcept : storage(TAG_SPECIAL | VAL_UNDEFINED) {}
|
|
68
|
-
|
|
69
|
-
explicit AnyValue(double d) noexcept
|
|
68
|
+
AnyValue(double d) noexcept
|
|
70
69
|
{
|
|
71
70
|
std::memcpy(&storage, &d, 8);
|
|
72
71
|
if (storage >= TAG_BASE) [[unlikely]]
|
|
@@ -74,20 +73,28 @@ namespace jspp
|
|
|
74
73
|
storage = 0x7FF8000000000000ULL;
|
|
75
74
|
}
|
|
76
75
|
}
|
|
77
|
-
|
|
76
|
+
AnyValue(int i) noexcept
|
|
77
|
+
{
|
|
78
|
+
double d = static_cast<double>(i);
|
|
79
|
+
std::memcpy(&storage, &d, 8);
|
|
80
|
+
}
|
|
78
81
|
explicit AnyValue(bool b) noexcept : storage(TAG_BOOLEAN | (b ? 1 : 0)) {}
|
|
79
|
-
|
|
80
82
|
AnyValue(const AnyValue &other) noexcept : storage(other.storage)
|
|
81
83
|
{
|
|
82
84
|
if (is_heap_object())
|
|
83
85
|
get_ptr()->ref();
|
|
84
86
|
}
|
|
85
|
-
|
|
86
87
|
AnyValue(AnyValue &&other) noexcept : storage(other.storage)
|
|
87
88
|
{
|
|
88
89
|
other.storage = TAG_SPECIAL | VAL_UNDEFINED;
|
|
89
90
|
}
|
|
91
|
+
~AnyValue()
|
|
92
|
+
{
|
|
93
|
+
if (is_heap_object())
|
|
94
|
+
get_ptr()->deref();
|
|
95
|
+
}
|
|
90
96
|
|
|
97
|
+
// Assignments
|
|
91
98
|
AnyValue &operator=(const AnyValue &other) noexcept
|
|
92
99
|
{
|
|
93
100
|
if (this != &other)
|
|
@@ -100,7 +107,6 @@ namespace jspp
|
|
|
100
107
|
}
|
|
101
108
|
return *this;
|
|
102
109
|
}
|
|
103
|
-
|
|
104
110
|
AnyValue &operator=(AnyValue &&other) noexcept
|
|
105
111
|
{
|
|
106
112
|
if (this != &other)
|
|
@@ -112,14 +118,6 @@ namespace jspp
|
|
|
112
118
|
}
|
|
113
119
|
return *this;
|
|
114
120
|
}
|
|
115
|
-
|
|
116
|
-
~AnyValue()
|
|
117
|
-
{
|
|
118
|
-
if (is_heap_object())
|
|
119
|
-
get_ptr()->deref();
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
// Assignments
|
|
123
121
|
AnyValue &operator=(double val) noexcept
|
|
124
122
|
{
|
|
125
123
|
if (is_heap_object())
|
|
@@ -268,13 +266,13 @@ namespace jspp
|
|
|
268
266
|
|
|
269
267
|
bool is_generator() const noexcept;
|
|
270
268
|
|
|
271
|
-
double as_double() const noexcept
|
|
269
|
+
inline double as_double() const noexcept
|
|
272
270
|
{
|
|
273
271
|
double d;
|
|
274
272
|
std::memcpy(&d, &storage, 8);
|
|
275
273
|
return d;
|
|
276
274
|
}
|
|
277
|
-
bool as_boolean() const noexcept
|
|
275
|
+
inline bool as_boolean() const noexcept
|
|
278
276
|
{
|
|
279
277
|
return static_cast<bool>(storage & 1);
|
|
280
278
|
}
|
|
@@ -330,7 +328,6 @@ namespace jspp
|
|
|
330
328
|
AnyValue construct(std::span<const AnyValue> args, const std::optional<std::string> &name = std::nullopt) const;
|
|
331
329
|
AnyValue &set_prototype(AnyValue proto);
|
|
332
330
|
std::string to_std_string() const;
|
|
333
|
-
std::string to_property_key() const;
|
|
334
331
|
|
|
335
332
|
inline uint64_t get_storage() const noexcept { return storage; }
|
|
336
333
|
inline void *get_raw_ptr() const noexcept { return reinterpret_cast<void *>(storage & PAYLOAD_MASK); }
|
|
@@ -338,26 +335,27 @@ namespace jspp
|
|
|
338
335
|
bool operator==(const AnyValue &other) const noexcept;
|
|
339
336
|
bool operator==(const std::string &other) const noexcept;
|
|
340
337
|
bool operator<(const AnyValue &other) const noexcept;
|
|
341
|
-
|
|
338
|
+
};
|
|
342
339
|
|
|
343
|
-
|
|
344
|
-
|
|
340
|
+
struct AnyValueAwaiter
|
|
341
|
+
{
|
|
345
342
|
AnyValue value;
|
|
346
343
|
bool await_ready();
|
|
347
344
|
void await_suspend(std::coroutine_handle<> h);
|
|
348
345
|
AnyValue await_resume();
|
|
349
|
-
|
|
346
|
+
};
|
|
350
347
|
|
|
351
|
-
|
|
352
|
-
|
|
348
|
+
inline auto AnyValue::operator co_await() const
|
|
349
|
+
{
|
|
353
350
|
return AnyValueAwaiter{*this};
|
|
354
|
-
|
|
355
|
-
|
|
351
|
+
}
|
|
352
|
+
}
|
|
356
353
|
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
354
|
+
namespace jspp
|
|
355
|
+
{
|
|
356
|
+
namespace Constants
|
|
357
|
+
{
|
|
358
|
+
inline const AnyValue UNINITIALIZED = AnyValue::make_uninitialized();
|
|
361
359
|
inline const AnyValue UNDEFINED = AnyValue::make_undefined();
|
|
362
360
|
inline const AnyValue Null = AnyValue::make_null();
|
|
363
361
|
inline const AnyValue NaN = AnyValue::make_nan();
|
|
@@ -366,4 +364,4 @@ namespace jspp
|
|
|
366
364
|
inline const AnyValue ZERO = AnyValue::make_number(0.0);
|
|
367
365
|
inline const AnyValue ONE = AnyValue::make_number(1.0);
|
|
368
366
|
}
|
|
369
|
-
}
|
|
367
|
+
}
|
package/src/prelude/jspp.hpp
CHANGED
|
@@ -32,9 +32,10 @@
|
|
|
32
32
|
#include "values/prototypes/promise.hpp"
|
|
33
33
|
#include "values/prototypes/string.hpp"
|
|
34
34
|
#include "values/prototypes/number.hpp"
|
|
35
|
+
#include "values/prototypes/boolean.hpp"
|
|
35
36
|
|
|
36
37
|
// utilities
|
|
37
|
-
#include "utils/
|
|
38
|
+
#include "utils/operators_native.hpp"
|
|
38
39
|
#include "utils/operators.hpp"
|
|
39
40
|
#include "utils/assignment_operators.hpp"
|
|
40
41
|
#include "utils/access.hpp"
|
|
@@ -50,6 +51,7 @@
|
|
|
50
51
|
#include "library/math.hpp"
|
|
51
52
|
#include "library/object.hpp"
|
|
52
53
|
#include "library/array.hpp"
|
|
54
|
+
#include "library/boolean.hpp"
|
|
53
55
|
#include "library/global.hpp"
|
|
54
56
|
|
|
55
57
|
#include "iterator_instantiations.hpp"
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
#include "jspp.hpp"
|
|
2
|
+
#include "library/boolean.hpp"
|
|
3
|
+
|
|
4
|
+
namespace jspp
|
|
5
|
+
{
|
|
6
|
+
// TODO: implement boolean constructor
|
|
7
|
+
jspp::AnyValue Boolean = jspp::AnyValue::make_function(
|
|
8
|
+
std::function<AnyValue(AnyValue, std::span<const AnyValue>)>([](jspp::AnyValue thisVal, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
|
|
9
|
+
{
|
|
10
|
+
if (args.empty())
|
|
11
|
+
return jspp::Constants::FALSE;
|
|
12
|
+
return jspp::AnyValue::make_boolean(jspp::is_truthy(args[0])); }),
|
|
13
|
+
"Boolean");
|
|
14
|
+
|
|
15
|
+
struct BooleanInit
|
|
16
|
+
{
|
|
17
|
+
BooleanInit()
|
|
18
|
+
{ // TODO: implement boolean prototypes
|
|
19
|
+
// auto proto = Boolean.get_own_property("prototype");
|
|
20
|
+
// proto.define_data_property("valueOf", jspp::BooleanPrototypes::get("valueOf").value(), true, false, true);
|
|
21
|
+
// proto.define_data_property("toString", jspp::BooleanPrototypes::get("toString").value(), true, false, true);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
void init_boolean()
|
|
26
|
+
{
|
|
27
|
+
static BooleanInit booleanInit;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
}
|
|
@@ -14,7 +14,7 @@ namespace jspp
|
|
|
14
14
|
if (target.is_object())
|
|
15
15
|
{
|
|
16
16
|
auto obj = target.as_object();
|
|
17
|
-
if (!obj->proto.is_null() && !obj->proto.is_undefined() &&
|
|
17
|
+
if (!obj->proto.is_null() && !obj->proto.is_undefined() && is_strictly_equal_to_native(obj->proto, proto))
|
|
18
18
|
{
|
|
19
19
|
is_construct_call = true;
|
|
20
20
|
}
|
|
@@ -58,7 +58,7 @@ namespace jspp
|
|
|
58
58
|
if (val.is_object()) {
|
|
59
59
|
auto current = val.as_object()->proto;
|
|
60
60
|
while (!current.is_null()) {
|
|
61
|
-
if (
|
|
61
|
+
if (is_strictly_equal_to_native(current, proto)) return jspp::Constants::TRUE;
|
|
62
62
|
if (current.is_object()) current = current.as_object()->proto;
|
|
63
63
|
else break;
|
|
64
64
|
}
|
|
@@ -19,6 +19,7 @@ namespace jspp {
|
|
|
19
19
|
init_promise();
|
|
20
20
|
init_math();
|
|
21
21
|
init_console();
|
|
22
|
+
init_boolean();
|
|
22
23
|
|
|
23
24
|
// 2. Initialize functions and linking
|
|
24
25
|
GeneratorFunction = jspp::AnyValue::make_generator([](jspp::AnyValue, std::vector<jspp::AnyValue>) -> jspp::JsIterator<jspp::AnyValue>
|
|
@@ -52,6 +53,7 @@ namespace jspp {
|
|
|
52
53
|
{"Math", jspp::Math},
|
|
53
54
|
{"Object", jspp::Object},
|
|
54
55
|
{"Array", jspp::Array},
|
|
56
|
+
{"Boolean", jspp::Boolean},
|
|
55
57
|
});
|
|
56
58
|
|
|
57
59
|
auto objectProto = ::Object.get_own_property("prototype");
|
|
@@ -32,6 +32,9 @@ namespace jspp {
|
|
|
32
32
|
auto defConst = [](const std::string& key, double val) {
|
|
33
33
|
jspp::Math.define_data_property(key, AnyValue::make_number(val), false, false, false);
|
|
34
34
|
};
|
|
35
|
+
auto defMutable = [](const std::string& key, AnyValue val) {
|
|
36
|
+
jspp::Math.define_data_property(key, val, true, false, true);
|
|
37
|
+
};
|
|
35
38
|
|
|
36
39
|
defConst("E", std::numbers::e);
|
|
37
40
|
defConst("LN10", std::numbers::ln10);
|
|
@@ -42,78 +45,78 @@ namespace jspp {
|
|
|
42
45
|
defConst("SQRT1_2", std::numbers::sqrt2 / 2.0);
|
|
43
46
|
defConst("SQRT2", std::numbers::sqrt2);
|
|
44
47
|
|
|
45
|
-
|
|
48
|
+
defMutable("abs", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
46
49
|
return AnyValue::make_number(std::abs(GetArgAsDouble(args, 0)));
|
|
47
50
|
}, "abs"));
|
|
48
51
|
|
|
49
|
-
|
|
52
|
+
defMutable("acos", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
50
53
|
return MathFunc1(args, std::acos);
|
|
51
54
|
}, "acos"));
|
|
52
55
|
|
|
53
|
-
|
|
56
|
+
defMutable("acosh", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
54
57
|
return MathFunc1(args, std::acosh);
|
|
55
58
|
}, "acosh"));
|
|
56
59
|
|
|
57
|
-
|
|
60
|
+
defMutable("asin", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
58
61
|
return MathFunc1(args, std::asin);
|
|
59
62
|
}, "asin"));
|
|
60
63
|
|
|
61
|
-
|
|
64
|
+
defMutable("asinh", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
62
65
|
return MathFunc1(args, std::asinh);
|
|
63
66
|
}, "asinh"));
|
|
64
67
|
|
|
65
|
-
|
|
68
|
+
defMutable("atan", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
66
69
|
return MathFunc1(args, std::atan);
|
|
67
70
|
}, "atan"));
|
|
68
71
|
|
|
69
|
-
|
|
72
|
+
defMutable("atan2", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
70
73
|
return MathFunc2(args, std::atan2);
|
|
71
74
|
}, "atan2"));
|
|
72
75
|
|
|
73
|
-
|
|
76
|
+
defMutable("atanh", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
74
77
|
return MathFunc1(args, std::atanh);
|
|
75
78
|
}, "atanh"));
|
|
76
79
|
|
|
77
|
-
|
|
80
|
+
defMutable("cbrt", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
78
81
|
return MathFunc1(args, std::cbrt);
|
|
79
82
|
}, "cbrt"));
|
|
80
83
|
|
|
81
|
-
|
|
84
|
+
defMutable("ceil", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
82
85
|
return MathFunc1(args, std::ceil);
|
|
83
86
|
}, "ceil"));
|
|
84
87
|
|
|
85
|
-
|
|
88
|
+
defMutable("clz32", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
86
89
|
uint32_t val = Operators_Private::ToInt32(args.empty() ? Constants::UNDEFINED : args[0]);
|
|
87
90
|
if (val == 0) return AnyValue::make_number(32);
|
|
88
91
|
return AnyValue::make_number(std::countl_zero(val));
|
|
89
92
|
}, "clz32"));
|
|
90
93
|
|
|
91
|
-
|
|
94
|
+
defMutable("cos", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
92
95
|
return MathFunc1(args, std::cos);
|
|
93
96
|
}, "cos"));
|
|
94
97
|
|
|
95
|
-
|
|
98
|
+
defMutable("cosh", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
96
99
|
return MathFunc1(args, std::cosh);
|
|
97
100
|
}, "cosh"));
|
|
98
101
|
|
|
99
|
-
|
|
102
|
+
defMutable("exp", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
100
103
|
return MathFunc1(args, std::exp);
|
|
101
104
|
}, "exp"));
|
|
102
105
|
|
|
103
|
-
|
|
106
|
+
defMutable("expm1", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
104
107
|
return MathFunc1(args, std::expm1);
|
|
105
108
|
}, "expm1"));
|
|
106
109
|
|
|
107
|
-
|
|
110
|
+
defMutable("floor", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
108
111
|
return MathFunc1(args, std::floor);
|
|
109
112
|
}, "floor"));
|
|
110
113
|
|
|
111
|
-
|
|
114
|
+
defMutable("fround", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
112
115
|
double d = GetArgAsDouble(args, 0);
|
|
113
116
|
return AnyValue::make_number(static_cast<double>(static_cast<float>(d)));
|
|
114
117
|
}, "fround"));
|
|
115
118
|
|
|
116
|
-
|
|
119
|
+
defMutable("hypot", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
117
120
|
double result = 0;
|
|
118
121
|
for (const auto& arg : args) {
|
|
119
122
|
double val = Operators_Private::ToNumber(arg);
|
|
@@ -123,29 +126,29 @@ namespace jspp {
|
|
|
123
126
|
return AnyValue::make_number(result);
|
|
124
127
|
}, "hypot"));
|
|
125
128
|
|
|
126
|
-
|
|
129
|
+
defMutable("imul", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
127
130
|
int32_t a = Operators_Private::ToInt32(args.empty() ? Constants::UNDEFINED : args[0]);
|
|
128
131
|
int32_t b = Operators_Private::ToInt32(args.size() < 2 ? Constants::UNDEFINED : args[1]);
|
|
129
132
|
return AnyValue::make_number(a * b);
|
|
130
133
|
}, "imul"));
|
|
131
134
|
|
|
132
|
-
|
|
135
|
+
defMutable("log", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
133
136
|
return MathFunc1(args, std::log);
|
|
134
137
|
}, "log"));
|
|
135
138
|
|
|
136
|
-
|
|
139
|
+
defMutable("log10", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
137
140
|
return MathFunc1(args, std::log10);
|
|
138
141
|
}, "log10"));
|
|
139
142
|
|
|
140
|
-
|
|
143
|
+
defMutable("log1p", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
141
144
|
return MathFunc1(args, std::log1p);
|
|
142
145
|
}, "log1p"));
|
|
143
146
|
|
|
144
|
-
|
|
147
|
+
defMutable("log2", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
145
148
|
return MathFunc1(args, std::log2);
|
|
146
149
|
}, "log2"));
|
|
147
150
|
|
|
148
|
-
|
|
151
|
+
defMutable("max", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
149
152
|
double maxVal = -std::numeric_limits<double>::infinity();
|
|
150
153
|
for (const auto& arg : args) {
|
|
151
154
|
double val = Operators_Private::ToNumber(arg);
|
|
@@ -155,7 +158,7 @@ namespace jspp {
|
|
|
155
158
|
return AnyValue::make_number(maxVal);
|
|
156
159
|
}, "max"));
|
|
157
160
|
|
|
158
|
-
|
|
161
|
+
defMutable("min", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
159
162
|
double minVal = std::numeric_limits<double>::infinity();
|
|
160
163
|
for (const auto& arg : args) {
|
|
161
164
|
double val = Operators_Private::ToNumber(arg);
|
|
@@ -165,63 +168,63 @@ namespace jspp {
|
|
|
165
168
|
return AnyValue::make_number(minVal);
|
|
166
169
|
}, "min"));
|
|
167
170
|
|
|
168
|
-
|
|
171
|
+
defMutable("pow", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
169
172
|
return MathFunc2(args, std::pow);
|
|
170
173
|
}, "pow"));
|
|
171
174
|
|
|
172
|
-
|
|
175
|
+
defMutable("random", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
173
176
|
return AnyValue::make_number(dis(gen));
|
|
174
177
|
}, "random"));
|
|
175
178
|
|
|
176
|
-
|
|
179
|
+
defMutable("round", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
177
180
|
double d = GetArgAsDouble(args, 0);
|
|
178
181
|
if (std::isnan(d)) return AnyValue::make_nan();
|
|
179
182
|
return AnyValue::make_number(std::floor(d + 0.5));
|
|
180
183
|
}, "round"));
|
|
181
184
|
|
|
182
|
-
|
|
185
|
+
defMutable("sign", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
183
186
|
double d = GetArgAsDouble(args, 0);
|
|
184
187
|
if (std::isnan(d)) return AnyValue::make_nan();
|
|
185
188
|
if (d == 0) return AnyValue::make_number(d);
|
|
186
189
|
return AnyValue::make_number(d > 0 ? 1.0 : -1.0);
|
|
187
190
|
}, "sign"));
|
|
188
191
|
|
|
189
|
-
|
|
192
|
+
defMutable("sin", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
190
193
|
return MathFunc1(args, std::sin);
|
|
191
194
|
}, "sin"));
|
|
192
195
|
|
|
193
|
-
|
|
196
|
+
defMutable("sinh", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
194
197
|
return MathFunc1(args, std::sinh);
|
|
195
198
|
}, "sinh"));
|
|
196
199
|
|
|
197
|
-
|
|
200
|
+
defMutable("sqrt", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
198
201
|
return MathFunc1(args, std::sqrt);
|
|
199
202
|
}, "sqrt"));
|
|
200
203
|
|
|
201
|
-
|
|
204
|
+
defMutable("tan", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
202
205
|
return MathFunc1(args, std::tan);
|
|
203
206
|
}, "tan"));
|
|
204
207
|
|
|
205
|
-
|
|
208
|
+
defMutable("tanh", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
206
209
|
return MathFunc1(args, std::tanh);
|
|
207
210
|
}, "tanh"));
|
|
208
211
|
|
|
209
|
-
|
|
212
|
+
defMutable("trunc", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
210
213
|
return MathFunc1(args, std::trunc);
|
|
211
214
|
}, "trunc"));
|
|
212
215
|
|
|
213
216
|
// Math.f16round(x)
|
|
214
|
-
|
|
217
|
+
defMutable("f16round", AnyValue::make_function([](AnyValue, std::span<const AnyValue> args) -> AnyValue {
|
|
215
218
|
double d = GetArgAsDouble(args, 0);
|
|
216
|
-
#if defined(__STDCPP_FLOAT16_T__)
|
|
217
|
-
|
|
218
|
-
#else
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
#endif
|
|
219
|
+
#if defined(__STDCPP_FLOAT16_T__)
|
|
220
|
+
return AnyValue::make_number(static_cast<double>(static_cast<std::float16_t>(d)));
|
|
221
|
+
#else
|
|
222
|
+
float f = static_cast<float>(d);
|
|
223
|
+
return AnyValue::make_number(static_cast<double>(f));
|
|
224
|
+
#endif
|
|
222
225
|
}, "f16round"));
|
|
223
226
|
// Math.sumPrecise(iterable)
|
|
224
|
-
|
|
227
|
+
defMutable("sumPrecise", AnyValue::make_function([](AnyValue thisVal, std::span<const AnyValue> args) -> AnyValue {
|
|
225
228
|
if (args.empty()) throw Exception::make_exception("Math.sumPrecise requires an iterable", "TypeError");
|
|
226
229
|
|
|
227
230
|
auto iterObj = jspp::Access::get_object_iterator(args[0], "iterable");
|
|
@@ -362,7 +362,7 @@ namespace jspp {
|
|
|
362
362
|
}
|
|
363
363
|
|
|
364
364
|
while (!current.is_null()) {
|
|
365
|
-
if (jspp::
|
|
365
|
+
if (jspp::is_strictly_equal_to_native(current, thisVal)) return jspp::Constants::TRUE;
|
|
366
366
|
if (current.is_object()) current = current.as_object()->proto;
|
|
367
367
|
else if (current.is_array()) current = current.as_array()->proto;
|
|
368
368
|
else if (current.is_function()) current = current.as_function()->proto;
|
package/src/prelude/types.hpp
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
#pragma once
|
|
2
2
|
|
|
3
3
|
#include <iostream>
|
|
4
|
+
#include <exception>
|
|
4
5
|
#include <string>
|
|
5
6
|
#include <vector>
|
|
6
7
|
#include <variant>
|
|
@@ -100,15 +101,15 @@ namespace jspp
|
|
|
100
101
|
const bool is_truthy(const AnyValue &val) noexcept;
|
|
101
102
|
|
|
102
103
|
// Basic equality operators
|
|
103
|
-
inline const bool
|
|
104
|
-
inline const bool
|
|
105
|
-
inline const bool
|
|
106
|
-
inline const bool
|
|
107
|
-
|
|
108
|
-
inline const bool
|
|
109
|
-
inline const bool
|
|
110
|
-
inline const bool
|
|
111
|
-
inline const bool
|
|
104
|
+
inline const bool is_strictly_equal_to_native(const AnyValue &lhs, const double &rhs) noexcept;
|
|
105
|
+
inline const bool is_strictly_equal_to_native(const double &lhs, const AnyValue &rhs) noexcept;
|
|
106
|
+
inline const bool is_strictly_equal_to_native(const double &lhs, const double &rhs) noexcept;
|
|
107
|
+
inline const bool is_strictly_equal_to_native(const AnyValue &lhs, const AnyValue &rhs) noexcept;
|
|
108
|
+
|
|
109
|
+
inline const bool is_equal_to_native(const AnyValue &lhs, const double &rhs) noexcept;
|
|
110
|
+
inline const bool is_equal_to_native(const double &lhs, const AnyValue &rhs) noexcept;
|
|
111
|
+
inline const bool is_equal_to_native(const double &lhs, const double &rhs) noexcept;
|
|
112
|
+
inline const bool is_equal_to_native(const AnyValue &lhs, const AnyValue &rhs) noexcept;
|
|
112
113
|
|
|
113
114
|
inline const AnyValue is_strictly_equal_to(const AnyValue &lhs, const double &rhs) noexcept;
|
|
114
115
|
inline const AnyValue is_strictly_equal_to(const double &lhs, const AnyValue &rhs) noexcept;
|
|
@@ -331,7 +331,7 @@ namespace jspp
|
|
|
331
331
|
|
|
332
332
|
if (proto.is_null() || proto.is_undefined())
|
|
333
333
|
break;
|
|
334
|
-
if (
|
|
334
|
+
if (is_strictly_equal_to_native(proto, targetProto))
|
|
335
335
|
return Constants::TRUE;
|
|
336
336
|
current = proto;
|
|
337
337
|
}
|
|
@@ -477,7 +477,7 @@ namespace jspp
|
|
|
477
477
|
{
|
|
478
478
|
for (const auto &ex : excluded_keys)
|
|
479
479
|
{
|
|
480
|
-
if (
|
|
480
|
+
if (is_strictly_equal_to_native(key, ex))
|
|
481
481
|
return true;
|
|
482
482
|
}
|
|
483
483
|
return false;
|