@ugo-studio/jspp 0.3.1 → 0.3.3
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 +136 -82
- 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 +71 -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 -36
- package/src/prelude/utils/assignment_operators.hpp +136 -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 +123 -88
- package/src/prelude/utils/operators_native.hpp +360 -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/iterator.cpp +262 -210
- 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
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "types.hpp"
|
|
4
|
+
#include "any_value.hpp"
|
|
5
|
+
#include "exception.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
|
+
// Private namespace for helper functions that implement JS type conversions.
|
|
15
|
+
namespace Operators_Private
|
|
16
|
+
{
|
|
17
|
+
// Implements the ToNumber abstract operation from ECMA-262.
|
|
18
|
+
inline double ToNumber(const AnyValue &val)
|
|
19
|
+
{
|
|
20
|
+
if (val.is_number())
|
|
21
|
+
return val.as_double();
|
|
22
|
+
if (val.is_null())
|
|
23
|
+
return 0.0;
|
|
24
|
+
if (val.is_undefined())
|
|
25
|
+
return std::numeric_limits<double>::quiet_NaN();
|
|
26
|
+
if (val.is_boolean())
|
|
27
|
+
return val.as_boolean() ? 1.0 : 0.0;
|
|
28
|
+
if (val.is_string())
|
|
29
|
+
{
|
|
30
|
+
const std::string &s = val.as_string()->value;
|
|
31
|
+
if (s.empty() || std::all_of(s.begin(), s.end(), [](unsigned char c)
|
|
32
|
+
{ return std::isspace(c); }))
|
|
33
|
+
return 0.0;
|
|
34
|
+
try
|
|
35
|
+
{
|
|
36
|
+
size_t pos;
|
|
37
|
+
double num = std::stod(s, &pos);
|
|
38
|
+
while (pos < s.length() && std::isspace(static_cast<unsigned char>(s[pos])))
|
|
39
|
+
pos++;
|
|
40
|
+
if (pos != s.length())
|
|
41
|
+
return std::numeric_limits<double>::quiet_NaN();
|
|
42
|
+
return num;
|
|
43
|
+
}
|
|
44
|
+
catch (...)
|
|
45
|
+
{
|
|
46
|
+
return std::numeric_limits<double>::quiet_NaN();
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (val.is_uninitialized())
|
|
50
|
+
{
|
|
51
|
+
// THROW
|
|
52
|
+
Exception::throw_uninitialized_reference("#<Object>");
|
|
53
|
+
}
|
|
54
|
+
// Default to NaN
|
|
55
|
+
return std::numeric_limits<double>::quiet_NaN();
|
|
56
|
+
}
|
|
57
|
+
// Implements the ToInt32 abstract operation from ECMA-262.
|
|
58
|
+
inline int32_t ToInt32(const AnyValue &val)
|
|
59
|
+
{
|
|
60
|
+
double num = ToNumber(val);
|
|
61
|
+
|
|
62
|
+
if (std::isnan(num) || std::isinf(num) || num == 0)
|
|
63
|
+
return 0;
|
|
64
|
+
|
|
65
|
+
double posInt = std::signbit(num) ? -std::floor(std::abs(num)) : std::floor(std::abs(num));
|
|
66
|
+
double int32bit = fmod(posInt, 4294967296.0); // 2^32
|
|
67
|
+
|
|
68
|
+
if (int32bit >= 2147483648.0) // 2^31
|
|
69
|
+
return static_cast<int32_t>(int32bit - 4294967296.0);
|
|
70
|
+
else
|
|
71
|
+
return static_cast<int32_t>(int32bit);
|
|
72
|
+
}
|
|
73
|
+
// Implements the ToUint32 abstract operation from ECMA-262.
|
|
74
|
+
inline uint32_t ToUint32(const AnyValue &val)
|
|
75
|
+
{
|
|
76
|
+
double num = ToNumber(val);
|
|
77
|
+
if (std::isnan(num) || std::isinf(num) || num == 0)
|
|
78
|
+
return 0;
|
|
79
|
+
double posInt = std::signbit(num) ? -std::floor(std::abs(num)) : std::floor(std::abs(num));
|
|
80
|
+
uint32_t uint32bit = static_cast<uint32_t>(fmod(posInt, 4294967296.0));
|
|
81
|
+
return uint32bit;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// --- TRUTHY CHECKER ---
|
|
86
|
+
inline const bool is_truthy(const double &val) noexcept
|
|
87
|
+
{
|
|
88
|
+
return val != 0.0 && !std::isnan(val);
|
|
89
|
+
}
|
|
90
|
+
inline const bool is_truthy(const std::string &val) noexcept
|
|
91
|
+
{
|
|
92
|
+
return !val.empty();
|
|
93
|
+
}
|
|
94
|
+
inline const bool is_truthy(const AnyValue &val) noexcept
|
|
95
|
+
{
|
|
96
|
+
switch (val.get_type())
|
|
97
|
+
{
|
|
98
|
+
case JsType::Number:
|
|
99
|
+
return is_truthy(val.as_double());
|
|
100
|
+
case JsType::String:
|
|
101
|
+
return is_truthy(val.as_string()->value);
|
|
102
|
+
case JsType::Boolean:
|
|
103
|
+
return val.as_boolean();
|
|
104
|
+
case JsType::Null:
|
|
105
|
+
case JsType::Undefined:
|
|
106
|
+
case JsType::Uninitialized:
|
|
107
|
+
return false;
|
|
108
|
+
default:
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// --- UNARY NATIVE ---
|
|
114
|
+
inline double plus_native(const AnyValue &val) { return Operators_Private::ToNumber(val); }
|
|
115
|
+
inline double plus_native(double val) { return val; }
|
|
116
|
+
inline double negate_native(const AnyValue &val) { return -Operators_Private::ToNumber(val); }
|
|
117
|
+
inline double negate_native(double val) { return -val; }
|
|
118
|
+
inline double bitwise_not_native(const AnyValue &val) { return static_cast<double>(~Operators_Private::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
|
+
|
|
123
|
+
// --- BASIC EQUALITY ---
|
|
124
|
+
|
|
125
|
+
// Operator === (returns primitive boolean)
|
|
126
|
+
inline const bool is_strictly_equal_to_native(const AnyValue &lhs, const double &rhs) noexcept
|
|
127
|
+
{
|
|
128
|
+
if (lhs.is_number())
|
|
129
|
+
return lhs.as_double() == rhs;
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
inline const bool is_strictly_equal_to_native(const double &lhs, const AnyValue &rhs) noexcept
|
|
133
|
+
{
|
|
134
|
+
if (rhs.is_number())
|
|
135
|
+
return lhs == rhs.as_double();
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
inline const bool is_strictly_equal_to_native(const double &lhs, const double &rhs) noexcept
|
|
139
|
+
{
|
|
140
|
+
return lhs == rhs;
|
|
141
|
+
}
|
|
142
|
+
inline const bool is_strictly_equal_to_native(const AnyValue &lhs, const AnyValue &rhs) noexcept
|
|
143
|
+
{
|
|
144
|
+
JsType type = lhs.get_type();
|
|
145
|
+
if (type != rhs.get_type())
|
|
146
|
+
return false;
|
|
147
|
+
switch (type)
|
|
148
|
+
{
|
|
149
|
+
case JsType::Boolean:
|
|
150
|
+
return lhs.as_boolean() == rhs.as_boolean();
|
|
151
|
+
case JsType::Number:
|
|
152
|
+
return lhs.as_double() == rhs.as_double();
|
|
153
|
+
case JsType::String:
|
|
154
|
+
return lhs.as_string()->value == rhs.as_string()->value;
|
|
155
|
+
case JsType::Array:
|
|
156
|
+
return lhs.as_array() == rhs.as_array();
|
|
157
|
+
case JsType::Object:
|
|
158
|
+
return lhs.as_object() == rhs.as_object();
|
|
159
|
+
case JsType::Function:
|
|
160
|
+
return lhs.as_function() == rhs.as_function();
|
|
161
|
+
case JsType::Iterator:
|
|
162
|
+
return lhs.as_iterator() == rhs.as_iterator();
|
|
163
|
+
case JsType::AsyncIterator:
|
|
164
|
+
return lhs.as_async_iterator() == rhs.as_async_iterator();
|
|
165
|
+
case JsType::Promise:
|
|
166
|
+
return lhs.as_promise() == rhs.as_promise();
|
|
167
|
+
case JsType::Symbol:
|
|
168
|
+
return lhs.as_symbol() == rhs.as_symbol();
|
|
169
|
+
case JsType::DataDescriptor:
|
|
170
|
+
return lhs.as_data_descriptor() == rhs.as_data_descriptor();
|
|
171
|
+
case JsType::AccessorDescriptor:
|
|
172
|
+
return lhs.as_accessor_descriptor() == rhs.as_accessor_descriptor();
|
|
173
|
+
default:
|
|
174
|
+
return true;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Operator == (returns primitive boolean)
|
|
179
|
+
inline const bool is_equal_to_native(const AnyValue &lhs, const double &rhs) noexcept
|
|
180
|
+
{
|
|
181
|
+
return is_equal_to_native(lhs, AnyValue::make_number(rhs));
|
|
182
|
+
}
|
|
183
|
+
inline const bool is_equal_to_native(const double &lhs, const AnyValue &rhs) noexcept
|
|
184
|
+
{
|
|
185
|
+
return is_equal_to_native(AnyValue::make_number(lhs), rhs);
|
|
186
|
+
}
|
|
187
|
+
inline const bool is_equal_to_native(const double &lhs, const double &rhs) noexcept
|
|
188
|
+
{
|
|
189
|
+
return lhs == rhs;
|
|
190
|
+
}
|
|
191
|
+
inline const bool is_equal_to_native(const AnyValue &lhs, const AnyValue &rhs) noexcept
|
|
192
|
+
{
|
|
193
|
+
JsType lhs_type = lhs.get_type();
|
|
194
|
+
JsType rhs_type = rhs.get_type();
|
|
195
|
+
if (lhs_type == rhs_type)
|
|
196
|
+
{
|
|
197
|
+
return is_strictly_equal_to_native(lhs, rhs);
|
|
198
|
+
}
|
|
199
|
+
if ((lhs_type == JsType::Null && rhs_type == JsType::Undefined) || (lhs_type == JsType::Undefined && rhs_type == JsType::Null))
|
|
200
|
+
{
|
|
201
|
+
return true;
|
|
202
|
+
}
|
|
203
|
+
if (lhs_type == JsType::Number && rhs_type == JsType::String)
|
|
204
|
+
{
|
|
205
|
+
return lhs.as_double() == Operators_Private::ToNumber(rhs);
|
|
206
|
+
}
|
|
207
|
+
if (lhs_type == JsType::String && rhs_type == JsType::Number)
|
|
208
|
+
{
|
|
209
|
+
return Operators_Private::ToNumber(lhs) == rhs.as_double();
|
|
210
|
+
}
|
|
211
|
+
if (lhs_type == JsType::Boolean)
|
|
212
|
+
{
|
|
213
|
+
return is_equal_to_native(lhs.as_boolean() ? 1.0 : 0.0, rhs);
|
|
214
|
+
}
|
|
215
|
+
if (rhs_type == JsType::Boolean)
|
|
216
|
+
{
|
|
217
|
+
return is_equal_to_native(lhs, AnyValue::make_number(rhs.as_boolean() ? 1.0 : 0.0));
|
|
218
|
+
}
|
|
219
|
+
if ((lhs_type == JsType::Object || lhs_type == JsType::Array || lhs_type == JsType::Function || lhs_type == JsType::Promise || lhs_type == JsType::Iterator) &&
|
|
220
|
+
(rhs_type == JsType::String || rhs_type == JsType::Number || rhs_type == JsType::Symbol))
|
|
221
|
+
{
|
|
222
|
+
return is_equal_to_native(AnyValue::make_string(lhs.to_std_string()), rhs);
|
|
223
|
+
}
|
|
224
|
+
if ((rhs_type == JsType::Object || rhs_type == JsType::Array || rhs_type == JsType::Function || rhs_type == JsType::Promise || rhs_type == JsType::Iterator) &&
|
|
225
|
+
(lhs_type == JsType::String || lhs_type == JsType::Number || lhs_type == JsType::Symbol))
|
|
226
|
+
{
|
|
227
|
+
return is_equal_to_native(lhs, AnyValue::make_string(rhs.to_std_string()));
|
|
228
|
+
}
|
|
229
|
+
if (lhs_type == JsType::DataDescriptor || lhs_type == JsType::AccessorDescriptor)
|
|
230
|
+
{
|
|
231
|
+
return is_strictly_equal_to_native(lhs, rhs);
|
|
232
|
+
}
|
|
233
|
+
return false;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// Operator !== (returns primitive boolean)
|
|
237
|
+
inline const bool not_strictly_equal_to_native(const AnyValue &lhs, const double &rhs) noexcept { return !is_strictly_equal_to_native(lhs, rhs); }
|
|
238
|
+
inline const bool not_strictly_equal_to_native(const double &lhs, const AnyValue &rhs) noexcept { return !is_strictly_equal_to_native(lhs, rhs); }
|
|
239
|
+
inline const bool not_strictly_equal_to_native(const double &lhs, const double &rhs) noexcept { return !is_strictly_equal_to_native(lhs, rhs); }
|
|
240
|
+
inline const bool not_strictly_equal_to_native(const AnyValue &lhs, const AnyValue &rhs) noexcept { return !is_strictly_equal_to_native(lhs, rhs); }
|
|
241
|
+
|
|
242
|
+
// Operator != (returns primitive boolean)
|
|
243
|
+
inline const bool not_equal_to_native(const AnyValue &lhs, const double &rhs) noexcept { return !is_equal_to_native(lhs, rhs); }
|
|
244
|
+
inline const bool not_equal_to_native(const double &lhs, const AnyValue &rhs) noexcept { return !is_equal_to_native(lhs, rhs); }
|
|
245
|
+
inline const bool not_equal_to_native(const double &lhs, const double &rhs) noexcept { return !is_equal_to_native(lhs, rhs); }
|
|
246
|
+
inline const bool not_equal_to_native(const AnyValue &lhs, const AnyValue &rhs) noexcept { return !is_equal_to_native(lhs, rhs); }
|
|
247
|
+
|
|
248
|
+
// --- PRIMITIVE ARITHMETIC OPERATORS ---
|
|
249
|
+
inline double add_native(const double &lhs, const double &rhs) { return lhs + rhs; }
|
|
250
|
+
inline double add_native(const AnyValue &lhs, const AnyValue &rhs) { return Operators_Private::ToNumber(lhs) + Operators_Private::ToNumber(rhs); }
|
|
251
|
+
inline double add_native(const AnyValue &lhs, const double &rhs) { return Operators_Private::ToNumber(lhs) + rhs; }
|
|
252
|
+
inline double add_native(const double &lhs, const AnyValue &rhs) { return lhs + Operators_Private::ToNumber(rhs); }
|
|
253
|
+
|
|
254
|
+
inline double sub_native(const double &lhs, const double &rhs) { return lhs - rhs; }
|
|
255
|
+
inline double sub_native(const AnyValue &lhs, const AnyValue &rhs) { return Operators_Private::ToNumber(lhs) - Operators_Private::ToNumber(rhs); }
|
|
256
|
+
inline double sub_native(const AnyValue &lhs, const double &rhs) { return Operators_Private::ToNumber(lhs) - rhs; }
|
|
257
|
+
inline double sub_native(const double &lhs, const AnyValue &rhs) { return lhs - Operators_Private::ToNumber(rhs); }
|
|
258
|
+
|
|
259
|
+
inline double mul_native(const double &lhs, const double &rhs) { return lhs * rhs; }
|
|
260
|
+
inline double mul_native(const AnyValue &lhs, const AnyValue &rhs) { return Operators_Private::ToNumber(lhs) * Operators_Private::ToNumber(rhs); }
|
|
261
|
+
inline double mul_native(const AnyValue &lhs, const double &rhs) { return Operators_Private::ToNumber(lhs) * rhs; }
|
|
262
|
+
inline double mul_native(const double &lhs, const AnyValue &rhs) { return lhs * Operators_Private::ToNumber(rhs); }
|
|
263
|
+
|
|
264
|
+
inline double div_native(const double &lhs, const double &rhs) { return lhs / rhs; }
|
|
265
|
+
inline double div_native(const AnyValue &lhs, const AnyValue &rhs) { return Operators_Private::ToNumber(lhs) / Operators_Private::ToNumber(rhs); }
|
|
266
|
+
inline double div_native(const AnyValue &lhs, const double &rhs) { return Operators_Private::ToNumber(lhs) / rhs; }
|
|
267
|
+
inline double div_native(const double &lhs, const AnyValue &rhs) { return lhs / Operators_Private::ToNumber(rhs); }
|
|
268
|
+
|
|
269
|
+
inline double mod_native(const double &lhs, const double &rhs) { return std::fmod(lhs, rhs); }
|
|
270
|
+
inline double mod_native(const AnyValue &lhs, const AnyValue &rhs) { return std::fmod(Operators_Private::ToNumber(lhs), Operators_Private::ToNumber(rhs)); }
|
|
271
|
+
inline double mod_native(const AnyValue &lhs, const double &rhs) { return std::fmod(Operators_Private::ToNumber(lhs), rhs); }
|
|
272
|
+
inline double mod_native(const double &lhs, const AnyValue &rhs) { return std::fmod(lhs, Operators_Private::ToNumber(rhs)); }
|
|
273
|
+
|
|
274
|
+
inline double pow_native(const double &lhs, const double &rhs) { return std::pow(lhs, rhs); }
|
|
275
|
+
inline double pow_native(const AnyValue &lhs, const AnyValue &rhs) { return std::pow(Operators_Private::ToNumber(lhs), Operators_Private::ToNumber(rhs)); }
|
|
276
|
+
inline double pow_native(const AnyValue &lhs, const double &rhs) { return std::pow(Operators_Private::ToNumber(lhs), rhs); }
|
|
277
|
+
inline double pow_native(const double &lhs, const AnyValue &rhs) { return std::pow(lhs, Operators_Private::ToNumber(rhs)); }
|
|
278
|
+
|
|
279
|
+
// --- PRIMITIVE COMPARISON OPERATORS ---
|
|
280
|
+
inline bool less_than_native(const double &lhs, const double &rhs) { return lhs < rhs; }
|
|
281
|
+
inline bool less_than_native(const AnyValue &lhs, const AnyValue &rhs) { return less_than_native(Operators_Private::ToNumber(lhs), Operators_Private::ToNumber(rhs)); }
|
|
282
|
+
inline bool less_than_native(const AnyValue &lhs, const double &rhs) { return less_than_native(Operators_Private::ToNumber(lhs), rhs); }
|
|
283
|
+
inline bool less_than_native(const double &lhs, const AnyValue &rhs) { return less_than_native(lhs, Operators_Private::ToNumber(rhs)); }
|
|
284
|
+
|
|
285
|
+
inline bool greater_than_native(const double &lhs, const double &rhs) { return lhs > rhs; }
|
|
286
|
+
inline bool greater_than_native(const AnyValue &lhs, const AnyValue &rhs) { return greater_than_native(Operators_Private::ToNumber(lhs), Operators_Private::ToNumber(rhs)); }
|
|
287
|
+
inline bool greater_than_native(const AnyValue &lhs, const double &rhs) { return greater_than_native(Operators_Private::ToNumber(lhs), rhs); }
|
|
288
|
+
inline bool greater_than_native(const double &lhs, const AnyValue &rhs) { return greater_than_native(lhs, Operators_Private::ToNumber(rhs)); }
|
|
289
|
+
|
|
290
|
+
inline bool less_than_or_equal_native(const double &lhs, const double &rhs) { return lhs <= rhs; }
|
|
291
|
+
inline bool less_than_or_equal_native(const AnyValue &lhs, const AnyValue &rhs) { return less_than_or_equal_native(Operators_Private::ToNumber(lhs), Operators_Private::ToNumber(rhs)); }
|
|
292
|
+
inline bool less_than_or_equal_native(const AnyValue &lhs, const double &rhs) { return less_than_or_equal_native(Operators_Private::ToNumber(lhs), rhs); }
|
|
293
|
+
inline bool less_than_or_equal_native(const double &lhs, const AnyValue &rhs) { return less_than_or_equal_native(lhs, Operators_Private::ToNumber(rhs)); }
|
|
294
|
+
|
|
295
|
+
inline bool greater_than_or_equal_native(const double &lhs, const double &rhs) { return lhs >= rhs; }
|
|
296
|
+
inline bool greater_than_or_equal_native(const AnyValue &lhs, const AnyValue &rhs) { return greater_than_or_equal_native(Operators_Private::ToNumber(lhs), Operators_Private::ToNumber(rhs)); }
|
|
297
|
+
inline bool greater_than_or_equal_native(const AnyValue &lhs, const double &rhs) { return greater_than_or_equal_native(Operators_Private::ToNumber(lhs), rhs); }
|
|
298
|
+
inline bool greater_than_or_equal_native(const double &lhs, const AnyValue &rhs) { return greater_than_or_equal_native(lhs, Operators_Private::ToNumber(rhs)); }
|
|
299
|
+
|
|
300
|
+
inline bool equal_native(const double &lhs, const double &rhs) { return lhs == rhs; }
|
|
301
|
+
inline bool equal_native(const AnyValue &lhs, const AnyValue &rhs) { return equal_native(Operators_Private::ToNumber(lhs), Operators_Private::ToNumber(rhs)); }
|
|
302
|
+
inline bool equal_native(const AnyValue &lhs, const double &rhs) { return equal_native(Operators_Private::ToNumber(lhs), rhs); }
|
|
303
|
+
inline bool equal_native(const double &lhs, const AnyValue &rhs) { return equal_native(lhs, Operators_Private::ToNumber(rhs)); }
|
|
304
|
+
|
|
305
|
+
inline bool not_equal_native(const double &lhs, const double &rhs) { return lhs != rhs; }
|
|
306
|
+
inline bool not_equal_native(const AnyValue &lhs, const AnyValue &rhs) { return not_equal_native(Operators_Private::ToNumber(lhs), Operators_Private::ToNumber(rhs)); }
|
|
307
|
+
inline bool not_equal_native(const AnyValue &lhs, const double &rhs) { return not_equal_native(Operators_Private::ToNumber(lhs), rhs); }
|
|
308
|
+
inline bool not_equal_native(const double &lhs, const AnyValue &rhs) { return not_equal_native(lhs, Operators_Private::ToNumber(rhs)); }
|
|
309
|
+
|
|
310
|
+
// --- PRIMITIVE BITWISE OPERATORS ---
|
|
311
|
+
inline double bitwise_and_native(const double &lhs, const double &rhs)
|
|
312
|
+
{
|
|
313
|
+
return static_cast<double>(static_cast<int32_t>(lhs) & static_cast<int32_t>(rhs));
|
|
314
|
+
}
|
|
315
|
+
inline double bitwise_and_native(const AnyValue &lhs, const AnyValue &rhs) { return bitwise_and_native(Operators_Private::ToInt32(lhs), Operators_Private::ToInt32(rhs)); }
|
|
316
|
+
inline double bitwise_and_native(const AnyValue &lhs, const double &rhs) { return bitwise_and_native(Operators_Private::ToInt32(lhs), rhs); }
|
|
317
|
+
inline double bitwise_and_native(const double &lhs, const AnyValue &rhs) { return bitwise_and_native(lhs, Operators_Private::ToInt32(rhs)); }
|
|
318
|
+
|
|
319
|
+
inline double bitwise_or_native(const double &lhs, const double &rhs)
|
|
320
|
+
{
|
|
321
|
+
return static_cast<double>(static_cast<int32_t>(lhs) | static_cast<int32_t>(rhs));
|
|
322
|
+
}
|
|
323
|
+
inline double bitwise_or_native(const AnyValue &lhs, const AnyValue &rhs) { return bitwise_or_native(Operators_Private::ToInt32(lhs), Operators_Private::ToInt32(rhs)); }
|
|
324
|
+
inline double bitwise_or_native(const AnyValue &lhs, const double &rhs) { return bitwise_or_native(Operators_Private::ToInt32(lhs), rhs); }
|
|
325
|
+
inline double bitwise_or_native(const double &lhs, const AnyValue &rhs) { return bitwise_or_native(lhs, Operators_Private::ToInt32(rhs)); }
|
|
326
|
+
|
|
327
|
+
inline double bitwise_xor_native(const double &lhs, const double &rhs)
|
|
328
|
+
{
|
|
329
|
+
return static_cast<double>(static_cast<int32_t>(lhs) ^ static_cast<int32_t>(rhs));
|
|
330
|
+
}
|
|
331
|
+
inline double bitwise_xor_native(const AnyValue &lhs, const AnyValue &rhs) { return bitwise_xor_native(Operators_Private::ToInt32(lhs), Operators_Private::ToInt32(rhs)); }
|
|
332
|
+
inline double bitwise_xor_native(const AnyValue &lhs, const double &rhs) { return bitwise_xor_native(Operators_Private::ToInt32(lhs), rhs); }
|
|
333
|
+
inline double bitwise_xor_native(const double &lhs, const AnyValue &rhs) { return bitwise_xor_native(lhs, Operators_Private::ToInt32(rhs)); }
|
|
334
|
+
|
|
335
|
+
inline double left_shift_native(const double &lhs, const double &rhs)
|
|
336
|
+
{
|
|
337
|
+
return static_cast<double>(static_cast<int32_t>(lhs) << (static_cast<int32_t>(rhs) & 0x1F));
|
|
338
|
+
}
|
|
339
|
+
inline double left_shift_native(const AnyValue &lhs, const AnyValue &rhs) { return left_shift_native(Operators_Private::ToInt32(lhs), Operators_Private::ToInt32(rhs)); }
|
|
340
|
+
inline double left_shift_native(const AnyValue &lhs, const double &rhs) { return left_shift_native(Operators_Private::ToInt32(lhs), rhs); }
|
|
341
|
+
inline double left_shift_native(const double &lhs, const AnyValue &rhs) { return left_shift_native(lhs, Operators_Private::ToInt32(rhs)); }
|
|
342
|
+
|
|
343
|
+
inline double right_shift_native(const double &lhs, const double &rhs)
|
|
344
|
+
{
|
|
345
|
+
return static_cast<double>(static_cast<int32_t>(lhs) >> (static_cast<int32_t>(rhs) & 0x1F));
|
|
346
|
+
}
|
|
347
|
+
inline double right_shift_native(const AnyValue &lhs, const AnyValue &rhs) { return right_shift_native(Operators_Private::ToInt32(lhs), Operators_Private::ToInt32(rhs)); }
|
|
348
|
+
inline double right_shift_native(const AnyValue &lhs, const double &rhs) { return right_shift_native(Operators_Private::ToInt32(lhs), rhs); }
|
|
349
|
+
inline double right_shift_native(const double &lhs, const AnyValue &rhs) { return right_shift_native(lhs, Operators_Private::ToInt32(rhs)); }
|
|
350
|
+
|
|
351
|
+
inline double unsigned_right_shift_native(const double &lhs, const double &rhs)
|
|
352
|
+
{
|
|
353
|
+
uint32_t l = static_cast<uint32_t>(fmod(lhs, 4294967296.0));
|
|
354
|
+
return static_cast<double>(l >> (static_cast<int32_t>(rhs) & 0x1F));
|
|
355
|
+
}
|
|
356
|
+
inline double unsigned_right_shift_native(const AnyValue &lhs, const AnyValue &rhs) { return unsigned_right_shift_native(Operators_Private::ToUint32(lhs), Operators_Private::ToInt32(rhs)); }
|
|
357
|
+
inline double unsigned_right_shift_native(const AnyValue &lhs, const double &rhs) { return unsigned_right_shift_native(Operators_Private::ToUint32(lhs), rhs); }
|
|
358
|
+
inline double unsigned_right_shift_native(const double &lhs, const AnyValue &rhs) { return unsigned_right_shift_native(lhs, Operators_Private::ToInt32(rhs)); }
|
|
359
|
+
|
|
360
|
+
}
|
|
@@ -7,18 +7,18 @@ namespace jspp
|
|
|
7
7
|
namespace WellKnownSymbols
|
|
8
8
|
{
|
|
9
9
|
// We use a specific prefix "@@" for well-known symbols to distinguish them from user symbols
|
|
10
|
-
inline JsSymbol*
|
|
11
|
-
inline JsSymbol*
|
|
12
|
-
inline JsSymbol*
|
|
13
|
-
inline JsSymbol*
|
|
14
|
-
inline JsSymbol*
|
|
15
|
-
inline JsSymbol*
|
|
16
|
-
inline JsSymbol*
|
|
17
|
-
inline JsSymbol*
|
|
18
|
-
inline JsSymbol*
|
|
19
|
-
inline JsSymbol*
|
|
20
|
-
inline JsSymbol*
|
|
21
|
-
inline JsSymbol*
|
|
22
|
-
inline JsSymbol*
|
|
10
|
+
inline JsSymbol *iterator = new JsSymbol("Symbol.iterator", "@@iterator");
|
|
11
|
+
inline JsSymbol *asyncIterator = new JsSymbol("Symbol.asyncIterator", "@@asyncIterator");
|
|
12
|
+
inline JsSymbol *hasInstance = new JsSymbol("Symbol.hasInstance", "@@hasInstance");
|
|
13
|
+
inline JsSymbol *isConcatSpreadable = new JsSymbol("Symbol.isConcatSpreadable", "@@isConcatSpreadable");
|
|
14
|
+
inline JsSymbol *match = new JsSymbol("Symbol.match", "@@match");
|
|
15
|
+
inline JsSymbol *matchAll = new JsSymbol("Symbol.matchAll", "@@matchAll");
|
|
16
|
+
inline JsSymbol *replace = new JsSymbol("Symbol.replace", "@@replace");
|
|
17
|
+
inline JsSymbol *search = new JsSymbol("Symbol.search", "@@search");
|
|
18
|
+
inline JsSymbol *species = new JsSymbol("Symbol.species", "@@species");
|
|
19
|
+
inline JsSymbol *split = new JsSymbol("Symbol.split", "@@split");
|
|
20
|
+
inline JsSymbol *toPrimitive = new JsSymbol("Symbol.toPrimitive", "@@toPrimitive");
|
|
21
|
+
inline JsSymbol *toStringTag = new JsSymbol("Symbol.toStringTag", "@@toStringTag");
|
|
22
|
+
inline JsSymbol *unscopables = new JsSymbol("Symbol.unscopables", "@@unscopables");
|
|
23
23
|
}
|
|
24
24
|
}
|
|
@@ -513,7 +513,7 @@ AnyValue &get_includes_fn()
|
|
|
513
513
|
AnyValue element = self->get_property(static_cast<uint32_t>(i));
|
|
514
514
|
// SameValueZero algorithm (includes handles NaN)
|
|
515
515
|
if (element.is_number() && searchElement.is_number() && std::isnan(element.as_double()) && std::isnan(searchElement.as_double())) return Constants::TRUE;
|
|
516
|
-
if (
|
|
516
|
+
if (is_strictly_equal_to_native(element, searchElement)) return Constants::TRUE;
|
|
517
517
|
}
|
|
518
518
|
return Constants::FALSE; },
|
|
519
519
|
"includes");
|
|
@@ -538,7 +538,7 @@ AnyValue &get_indexOf_fn()
|
|
|
538
538
|
{
|
|
539
539
|
if (self->has_property(std::to_string(i))) {
|
|
540
540
|
AnyValue element = self->get_property(static_cast<uint32_t>(i));
|
|
541
|
-
if (
|
|
541
|
+
if (is_strictly_equal_to_native(element, searchElement)) return AnyValue::make_number(i);
|
|
542
542
|
}
|
|
543
543
|
}
|
|
544
544
|
return AnyValue::make_number(-1); },
|
|
@@ -565,7 +565,7 @@ AnyValue &get_lastIndexOf_fn()
|
|
|
565
565
|
{
|
|
566
566
|
if (self->has_property(std::to_string(i))) {
|
|
567
567
|
AnyValue element = self->get_property(static_cast<uint32_t>(i));
|
|
568
|
-
if (
|
|
568
|
+
if (is_strictly_equal_to_native(element, searchElement)) return AnyValue::make_number(i);
|
|
569
569
|
}
|
|
570
570
|
}
|
|
571
571
|
return AnyValue::make_number(-1); },
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
#include "jspp.hpp"
|
|
2
|
+
#include "values/prototypes/boolean.hpp"
|
|
3
|
+
|
|
4
|
+
namespace jspp
|
|
5
|
+
{
|
|
6
|
+
|
|
7
|
+
// --- JsBoolean Implementation ---
|
|
8
|
+
|
|
9
|
+
namespace JsBoolean
|
|
10
|
+
{
|
|
11
|
+
std::string to_std_string(bool value)
|
|
12
|
+
{
|
|
13
|
+
return value ? "true" : "false";
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
std::string to_std_string(const AnyValue &value)
|
|
17
|
+
{
|
|
18
|
+
return to_std_string(value.as_boolean());
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// --- BooleanPrototypes Implementation ---
|
|
24
|
+
|
|
25
|
+
namespace BooleanPrototypes
|
|
26
|
+
{
|
|
27
|
+
|
|
28
|
+
AnyValue &get_toString_fn()
|
|
29
|
+
{
|
|
30
|
+
static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
|
|
31
|
+
{ return AnyValue::make_string(JsBoolean::to_std_string(thisVal.as_boolean())); },
|
|
32
|
+
"toString");
|
|
33
|
+
return fn;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
AnyValue &get_valueOf_fn()
|
|
37
|
+
{
|
|
38
|
+
static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue>) -> AnyValue
|
|
39
|
+
{ return AnyValue::make_boolean(thisVal.as_boolean()); },
|
|
40
|
+
"valueOf");
|
|
41
|
+
return fn;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
std::optional<AnyValue> get(const std::string &key)
|
|
45
|
+
{
|
|
46
|
+
if (key == "toString")
|
|
47
|
+
return get_toString_fn();
|
|
48
|
+
if (key == "valueOf")
|
|
49
|
+
return get_valueOf_fn();
|
|
50
|
+
return std::nullopt;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
std::optional<AnyValue> get(const AnyValue &key)
|
|
54
|
+
{
|
|
55
|
+
if (key == "toString")
|
|
56
|
+
return get_toString_fn();
|
|
57
|
+
if (key == "valueOf")
|
|
58
|
+
return get_valueOf_fn();
|
|
59
|
+
return std::nullopt;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
} // namespace BooleanPrototypes
|
|
63
|
+
|
|
64
|
+
} // namespace jspp
|