@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.
- package/LICENSE +25 -25
- package/README.md +20 -12
- package/dist/analysis/scope.js +5 -3
- package/dist/analysis/typeAnalyzer.js +21 -25
- package/dist/cli/index.js +14 -4
- package/dist/cli/utils.js +61 -0
- package/dist/core/codegen/class-handlers.js +6 -6
- package/dist/core/codegen/control-flow-handlers.js +10 -9
- package/dist/core/codegen/declaration-handlers.js +10 -3
- package/dist/core/codegen/destructuring-handlers.js +9 -4
- package/dist/core/codegen/expression-handlers.js +40 -29
- package/dist/core/codegen/function-handlers.js +78 -12
- package/dist/core/codegen/helpers.js +91 -14
- package/dist/core/codegen/index.js +4 -2
- package/dist/core/codegen/statement-handlers.js +9 -7
- package/package.json +2 -2
- package/scripts/precompile-headers.ts +249 -50
- package/scripts/setup-compiler.ts +63 -63
- package/src/prelude/any_value.cpp +636 -0
- package/src/prelude/any_value.hpp +369 -362
- package/src/prelude/{exception_helpers.hpp → exception.cpp} +53 -53
- package/src/prelude/exception.hpp +27 -27
- package/src/prelude/iterator_instantiations.hpp +10 -0
- package/src/prelude/{index.hpp → jspp.hpp} +10 -16
- package/src/prelude/library/array.cpp +191 -0
- package/src/prelude/library/array.hpp +13 -186
- package/src/prelude/library/console.cpp +125 -0
- package/src/prelude/library/console.hpp +24 -112
- package/src/prelude/library/error.cpp +100 -0
- package/src/prelude/library/error.hpp +13 -113
- package/src/prelude/library/function.cpp +69 -0
- package/src/prelude/library/function.hpp +11 -10
- package/src/prelude/library/global.cpp +96 -0
- package/src/prelude/library/global.hpp +12 -28
- package/src/prelude/library/global_usings.hpp +15 -0
- package/src/prelude/library/math.cpp +258 -0
- package/src/prelude/library/math.hpp +26 -308
- package/src/prelude/library/object.cpp +379 -0
- package/src/prelude/library/object.hpp +14 -276
- package/src/prelude/library/performance.cpp +21 -0
- package/src/prelude/library/performance.hpp +5 -20
- package/src/prelude/library/process.cpp +38 -0
- package/src/prelude/library/process.hpp +11 -39
- package/src/prelude/library/promise.cpp +131 -0
- package/src/prelude/library/promise.hpp +12 -123
- package/src/prelude/library/symbol.cpp +56 -0
- package/src/prelude/library/symbol.hpp +11 -52
- package/src/prelude/library/timer.cpp +88 -0
- package/src/prelude/library/timer.hpp +16 -92
- package/src/prelude/runtime.cpp +19 -0
- package/src/prelude/types.hpp +184 -179
- package/src/prelude/utils/access.hpp +502 -411
- package/src/prelude/utils/assignment_operators.hpp +99 -99
- package/src/prelude/utils/log_any_value/array.hpp +61 -40
- package/src/prelude/utils/log_any_value/function.hpp +39 -39
- package/src/prelude/utils/log_any_value/object.hpp +60 -3
- package/src/prelude/utils/operators.hpp +351 -336
- package/src/prelude/utils/operators_primitive.hpp +336 -336
- package/src/prelude/utils/well_known_symbols.hpp +24 -24
- package/src/prelude/values/array.cpp +1399 -0
- package/src/prelude/values/array.hpp +4 -1
- package/src/prelude/values/async_iterator.cpp +251 -0
- package/src/prelude/values/async_iterator.hpp +111 -83
- package/src/prelude/values/function.cpp +262 -0
- package/src/prelude/values/function.hpp +62 -82
- package/src/prelude/values/iterator.cpp +309 -0
- package/src/prelude/values/iterator.hpp +33 -64
- package/src/prelude/values/number.cpp +176 -0
- package/src/prelude/values/object.cpp +159 -0
- package/src/prelude/values/object.hpp +4 -0
- package/src/prelude/values/promise.cpp +479 -0
- package/src/prelude/values/promise.hpp +79 -72
- package/src/prelude/values/prototypes/array.hpp +46 -1336
- package/src/prelude/values/prototypes/async_iterator.hpp +19 -61
- package/src/prelude/values/prototypes/function.hpp +7 -46
- package/src/prelude/values/prototypes/iterator.hpp +25 -201
- package/src/prelude/values/prototypes/number.hpp +23 -210
- package/src/prelude/values/prototypes/object.hpp +7 -23
- package/src/prelude/values/prototypes/promise.hpp +18 -196
- package/src/prelude/values/prototypes/string.hpp +39 -542
- package/src/prelude/values/prototypes/symbol.hpp +9 -70
- package/src/prelude/values/shape.hpp +52 -52
- package/src/prelude/values/string.cpp +485 -0
- package/src/prelude/values/string.hpp +25 -26
- package/src/prelude/values/symbol.cpp +89 -0
- package/src/prelude/values/symbol.hpp +101 -101
- package/src/prelude/any_value_access.hpp +0 -170
- package/src/prelude/any_value_defines.hpp +0 -190
- package/src/prelude/any_value_helpers.hpp +0 -374
- package/src/prelude/values/helpers/array.hpp +0 -209
- package/src/prelude/values/helpers/async_iterator.hpp +0 -275
- package/src/prelude/values/helpers/function.hpp +0 -109
- package/src/prelude/values/helpers/iterator.hpp +0 -145
- package/src/prelude/values/helpers/object.hpp +0 -104
- package/src/prelude/values/helpers/promise.hpp +0 -254
- package/src/prelude/values/helpers/string.hpp +0 -61
- package/src/prelude/values/helpers/symbol.hpp +0 -21
|
@@ -1,61 +1,19 @@
|
|
|
1
|
-
#pragma once
|
|
2
|
-
|
|
3
|
-
#include "types.hpp"
|
|
4
|
-
#include
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
inline AnyValue &get_asyncIterator_fn()
|
|
22
|
-
{
|
|
23
|
-
static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue>) -> AnyValue
|
|
24
|
-
{ return thisVal; },
|
|
25
|
-
"Symbol.asyncIterator");
|
|
26
|
-
return fn;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
inline AnyValue &get_next_fn()
|
|
30
|
-
{
|
|
31
|
-
static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
|
|
32
|
-
{
|
|
33
|
-
AnyValue val = args.empty() ? Constants::UNDEFINED : args[0];
|
|
34
|
-
auto res = thisVal.as_async_iterator()->next(val);
|
|
35
|
-
return AnyValue::make_promise(res); },
|
|
36
|
-
"next");
|
|
37
|
-
return fn;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
inline std::optional<AnyValue> get(const std::string &key)
|
|
41
|
-
{
|
|
42
|
-
// --- toString() method ---
|
|
43
|
-
if (key == "toString" || key == WellKnownSymbols::toStringTag->key)
|
|
44
|
-
{
|
|
45
|
-
return get_toString_fn();
|
|
46
|
-
}
|
|
47
|
-
// --- [Symbol.asyncIterator]() method ---
|
|
48
|
-
if (key == WellKnownSymbols::asyncIterator->key)
|
|
49
|
-
{
|
|
50
|
-
return get_asyncIterator_fn();
|
|
51
|
-
}
|
|
52
|
-
// --- next() method ---
|
|
53
|
-
if (key == "next")
|
|
54
|
-
{
|
|
55
|
-
return get_next_fn();
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
return std::nullopt;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "types.hpp"
|
|
4
|
+
#include <optional>
|
|
5
|
+
|
|
6
|
+
namespace jspp
|
|
7
|
+
{
|
|
8
|
+
class AnyValue;
|
|
9
|
+
|
|
10
|
+
namespace AsyncIteratorPrototypes
|
|
11
|
+
{
|
|
12
|
+
AnyValue &get_toString_fn();
|
|
13
|
+
AnyValue &get_asyncIterator_fn();
|
|
14
|
+
AnyValue &get_next_fn();
|
|
15
|
+
|
|
16
|
+
std::optional<AnyValue> get(const std::string &key);
|
|
17
|
+
std::optional<AnyValue> get(const AnyValue &key);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -1,56 +1,17 @@
|
|
|
1
1
|
#pragma once
|
|
2
2
|
|
|
3
3
|
#include "types.hpp"
|
|
4
|
-
#include
|
|
5
|
-
#include "any_value.hpp"
|
|
6
|
-
#include "exception.hpp"
|
|
7
|
-
#include "utils/operators.hpp"
|
|
4
|
+
#include <optional>
|
|
8
5
|
|
|
9
6
|
namespace jspp
|
|
10
7
|
{
|
|
8
|
+
class AnyValue;
|
|
9
|
+
|
|
11
10
|
namespace FunctionPrototypes
|
|
12
11
|
{
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
"toString");
|
|
18
|
-
return fn;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
inline AnyValue &get_call_fn()
|
|
22
|
-
{
|
|
23
|
-
static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
|
|
24
|
-
{
|
|
25
|
-
AnyValue thisArg = Constants::UNDEFINED;
|
|
26
|
-
std::span<const AnyValue> fnArgs;
|
|
27
|
-
|
|
28
|
-
if (!args.empty())
|
|
29
|
-
{
|
|
30
|
-
thisArg = args[0];
|
|
31
|
-
fnArgs = args.subspan(1);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return thisVal.call(thisArg, fnArgs); },
|
|
35
|
-
"call");
|
|
36
|
-
return fn;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
inline std::optional<AnyValue> get(const std::string &key)
|
|
40
|
-
{
|
|
41
|
-
// --- toString() method ---
|
|
42
|
-
if (key == "toString" || key == WellKnownSymbols::toStringTag->key)
|
|
43
|
-
{
|
|
44
|
-
return get_toString_fn();
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// --- call() method ---
|
|
48
|
-
if (key == "call")
|
|
49
|
-
{
|
|
50
|
-
return get_call_fn();
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
return std::nullopt;
|
|
54
|
-
}
|
|
12
|
+
AnyValue &get_toString_fn();
|
|
13
|
+
AnyValue &get_call_fn();
|
|
14
|
+
std::optional<AnyValue> get(const std::string &key);
|
|
15
|
+
std::optional<AnyValue> get(const AnyValue &key);
|
|
55
16
|
}
|
|
56
17
|
}
|
|
@@ -1,201 +1,25 @@
|
|
|
1
|
-
#pragma once
|
|
2
|
-
|
|
3
|
-
#include "types.hpp"
|
|
4
|
-
#include
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
return fn;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
inline AnyValue &get_next_fn()
|
|
30
|
-
{
|
|
31
|
-
static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
|
|
32
|
-
{
|
|
33
|
-
AnyValue val = args.empty() ? Constants::UNDEFINED : args[0];
|
|
34
|
-
auto res = thisVal.as_iterator()->next(val);
|
|
35
|
-
return AnyValue::make_object({{"value", res.value.value_or(Constants::UNDEFINED)}, {"done", AnyValue::make_boolean(res.done)}}); },
|
|
36
|
-
"next");
|
|
37
|
-
return fn;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
inline AnyValue &get_return_fn()
|
|
41
|
-
{
|
|
42
|
-
static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
|
|
43
|
-
{
|
|
44
|
-
AnyValue val = args.empty() ? Constants::UNDEFINED : args[0];
|
|
45
|
-
auto res = thisVal.as_iterator()->return_(val);
|
|
46
|
-
return AnyValue::make_object({{"value", res.value.value_or(Constants::UNDEFINED)}, {"done", AnyValue::make_boolean(res.done)}}); },
|
|
47
|
-
"return");
|
|
48
|
-
return fn;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
inline AnyValue &get_throw_fn()
|
|
52
|
-
{
|
|
53
|
-
static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
|
|
54
|
-
{
|
|
55
|
-
AnyValue err = args.empty() ? Constants::UNDEFINED : args[0];
|
|
56
|
-
auto res = thisVal.as_iterator()->throw_(err);
|
|
57
|
-
return AnyValue::make_object({{"value", res.value.value_or(Constants::UNDEFINED)}, {"done", AnyValue::make_boolean(res.done)}}); },
|
|
58
|
-
"throw");
|
|
59
|
-
return fn;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
inline AnyValue &get_toArray_fn()
|
|
63
|
-
{
|
|
64
|
-
static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue>) -> AnyValue
|
|
65
|
-
{ return AnyValue::make_array(thisVal.as_iterator()->to_vector()); },
|
|
66
|
-
"drop");
|
|
67
|
-
return fn;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
inline AnyValue &get_drop_fn()
|
|
71
|
-
{
|
|
72
|
-
static AnyValue fn = AnyValue::make_generator([](const AnyValue &thisVal, std::span<const AnyValue> args) -> JsIterator<AnyValue>
|
|
73
|
-
{
|
|
74
|
-
auto self = thisVal.as_iterator();
|
|
75
|
-
size_t skip_count = 0;
|
|
76
|
-
if (!args.empty()) {
|
|
77
|
-
skip_count = static_cast<size_t>(args[0].as_double());
|
|
78
|
-
}
|
|
79
|
-
size_t skipped = 0;
|
|
80
|
-
while (true)
|
|
81
|
-
{
|
|
82
|
-
auto next_res = self->next();
|
|
83
|
-
if (next_res.done) { break; }
|
|
84
|
-
if (skipped < skip_count)
|
|
85
|
-
{
|
|
86
|
-
skipped++;
|
|
87
|
-
continue;
|
|
88
|
-
}
|
|
89
|
-
co_yield next_res.value.value_or(Constants::UNDEFINED);
|
|
90
|
-
}
|
|
91
|
-
co_return jspp::Constants::UNDEFINED; },
|
|
92
|
-
"drop");
|
|
93
|
-
return fn;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
inline AnyValue &get_take_fn()
|
|
97
|
-
{
|
|
98
|
-
static AnyValue fn = AnyValue::make_generator([](const AnyValue &thisVal, std::span<const AnyValue> args) -> JsIterator<AnyValue>
|
|
99
|
-
{
|
|
100
|
-
auto self = thisVal.as_iterator();
|
|
101
|
-
size_t take_count = 0;
|
|
102
|
-
if (!args.empty()) {
|
|
103
|
-
take_count = static_cast<size_t>(args[0].as_double());
|
|
104
|
-
}
|
|
105
|
-
size_t taken = 0;
|
|
106
|
-
while (true)
|
|
107
|
-
{
|
|
108
|
-
auto next_res = self->next();
|
|
109
|
-
if (next_res.done) { break; }
|
|
110
|
-
if (taken < take_count)
|
|
111
|
-
{
|
|
112
|
-
taken++;
|
|
113
|
-
co_yield next_res.value.value_or(Constants::UNDEFINED);
|
|
114
|
-
}
|
|
115
|
-
if (taken >= take_count)
|
|
116
|
-
{
|
|
117
|
-
// Call the iterator's return() method for early cleanup of resources
|
|
118
|
-
self->return_();
|
|
119
|
-
break;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
co_return jspp::Constants::UNDEFINED; },
|
|
123
|
-
"take");
|
|
124
|
-
return fn;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
inline AnyValue &get_some_fn()
|
|
128
|
-
{
|
|
129
|
-
static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
|
|
130
|
-
{
|
|
131
|
-
auto self = thisVal.as_iterator();
|
|
132
|
-
if (args.empty() || !args[0].is_function()) throw Exception::make_exception("callback is not a function", "TypeError");
|
|
133
|
-
auto callback = args[0].as_function();
|
|
134
|
-
while (true)
|
|
135
|
-
{
|
|
136
|
-
auto next_res = self->next();
|
|
137
|
-
if (next_res.done) { break; }
|
|
138
|
-
if (is_truthy(callback->call(thisVal, std::span<const AnyValue>((const jspp::AnyValue[]){next_res.value.value_or(Constants::UNDEFINED)}, 1))))
|
|
139
|
-
{
|
|
140
|
-
// Call the iterator's return() method for early cleanup of resources
|
|
141
|
-
self->return_();
|
|
142
|
-
return Constants::TRUE;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
return jspp::Constants::FALSE; },
|
|
146
|
-
"some");
|
|
147
|
-
return fn;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
inline std::optional<AnyValue> get(const std::string &key)
|
|
151
|
-
{
|
|
152
|
-
// --- toString() method ---
|
|
153
|
-
if (key == "toString" || key == WellKnownSymbols::toStringTag->key)
|
|
154
|
-
{
|
|
155
|
-
return get_toString_fn();
|
|
156
|
-
}
|
|
157
|
-
// --- [Symbol.iterator]() method ---
|
|
158
|
-
if (key == WellKnownSymbols::iterator->key)
|
|
159
|
-
{
|
|
160
|
-
return get_iterator_fn();
|
|
161
|
-
}
|
|
162
|
-
// --- next() method ---
|
|
163
|
-
if (key == "next")
|
|
164
|
-
{
|
|
165
|
-
return get_next_fn();
|
|
166
|
-
}
|
|
167
|
-
// --- return() method ---
|
|
168
|
-
if (key == "return")
|
|
169
|
-
{
|
|
170
|
-
return get_return_fn();
|
|
171
|
-
}
|
|
172
|
-
// --- throw() method ---
|
|
173
|
-
if (key == "throw")
|
|
174
|
-
{
|
|
175
|
-
return get_throw_fn();
|
|
176
|
-
}
|
|
177
|
-
// --- toArray() method ---
|
|
178
|
-
if (key == "toArray")
|
|
179
|
-
{
|
|
180
|
-
return get_toArray_fn();
|
|
181
|
-
}
|
|
182
|
-
// --- drop() method ---
|
|
183
|
-
if (key == "drop")
|
|
184
|
-
{
|
|
185
|
-
return get_drop_fn();
|
|
186
|
-
}
|
|
187
|
-
// --- take() method ---
|
|
188
|
-
if (key == "take")
|
|
189
|
-
{
|
|
190
|
-
return get_take_fn();
|
|
191
|
-
}
|
|
192
|
-
// --- some() method ---
|
|
193
|
-
if (key == "some")
|
|
194
|
-
{
|
|
195
|
-
return get_some_fn();
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
return std::nullopt;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
}
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "types.hpp"
|
|
4
|
+
#include <optional>
|
|
5
|
+
|
|
6
|
+
namespace jspp
|
|
7
|
+
{
|
|
8
|
+
class AnyValue;
|
|
9
|
+
|
|
10
|
+
namespace IteratorPrototypes
|
|
11
|
+
{
|
|
12
|
+
AnyValue &get_toString_fn();
|
|
13
|
+
AnyValue &get_iterator_fn();
|
|
14
|
+
AnyValue &get_next_fn();
|
|
15
|
+
AnyValue &get_return_fn();
|
|
16
|
+
AnyValue &get_throw_fn();
|
|
17
|
+
AnyValue &get_toArray_fn();
|
|
18
|
+
AnyValue &get_drop_fn();
|
|
19
|
+
AnyValue &get_take_fn();
|
|
20
|
+
AnyValue &get_some_fn();
|
|
21
|
+
|
|
22
|
+
std::optional<AnyValue> get(const std::string &key);
|
|
23
|
+
std::optional<AnyValue> get(const AnyValue &key);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -1,210 +1,23 @@
|
|
|
1
|
-
#pragma once
|
|
2
|
-
|
|
3
|
-
#include "types.hpp"
|
|
4
|
-
#include
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
return value > 0 ? "Infinity" : "-Infinity";
|
|
25
|
-
|
|
26
|
-
// For radix != 10, we only support integer part for now
|
|
27
|
-
// as implementing full float-to-radix is complex.
|
|
28
|
-
long long intPart = static_cast<long long>(value);
|
|
29
|
-
|
|
30
|
-
if (radix == 10)
|
|
31
|
-
{
|
|
32
|
-
return AnyValue::make_number(value).to_std_string();
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
bool negative = intPart < 0;
|
|
36
|
-
if (negative)
|
|
37
|
-
intPart = -intPart;
|
|
38
|
-
|
|
39
|
-
std::string chars = "0123456789abcdefghijklmnopqrstuvwxyz";
|
|
40
|
-
std::string res = "";
|
|
41
|
-
|
|
42
|
-
if (intPart == 0)
|
|
43
|
-
res = "0";
|
|
44
|
-
else
|
|
45
|
-
{
|
|
46
|
-
while (intPart > 0)
|
|
47
|
-
{
|
|
48
|
-
res += chars[intPart % radix];
|
|
49
|
-
intPart /= radix;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
if (negative)
|
|
53
|
-
res += "-";
|
|
54
|
-
std::reverse(res.begin(), res.end());
|
|
55
|
-
return res;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
inline AnyValue &get_toExponential_fn()
|
|
59
|
-
{
|
|
60
|
-
static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
|
|
61
|
-
{
|
|
62
|
-
double self = thisVal.as_double();
|
|
63
|
-
int digits = -1;
|
|
64
|
-
if (!args.empty() && !args[0].is_undefined())
|
|
65
|
-
{
|
|
66
|
-
digits = Operators_Private::ToInt32(args[0]);
|
|
67
|
-
if (digits < 0 || digits > 100)
|
|
68
|
-
{
|
|
69
|
-
throw Exception::make_exception("toExponential() digits argument must be between 0 and 100", "RangeError");
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
std::ostringstream ss;
|
|
74
|
-
if (digits >= 0)
|
|
75
|
-
{
|
|
76
|
-
ss << std::scientific << std::setprecision(digits) << self;
|
|
77
|
-
}
|
|
78
|
-
else
|
|
79
|
-
{
|
|
80
|
-
ss << std::scientific << self;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
std::string res = ss.str();
|
|
84
|
-
return AnyValue::make_string(res); },
|
|
85
|
-
"toExponential");
|
|
86
|
-
return fn;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
inline AnyValue &get_toFixed_fn()
|
|
90
|
-
{
|
|
91
|
-
static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
|
|
92
|
-
{
|
|
93
|
-
double self = thisVal.as_double();
|
|
94
|
-
int digits = 0;
|
|
95
|
-
if (!args.empty() && !args[0].is_undefined())
|
|
96
|
-
{
|
|
97
|
-
digits = Operators_Private::ToInt32(args[0]);
|
|
98
|
-
}
|
|
99
|
-
if (digits < 0 || digits > 100)
|
|
100
|
-
{
|
|
101
|
-
throw Exception::make_exception("toFixed() digits argument must be between 0 and 100", "RangeError");
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
std::ostringstream ss;
|
|
105
|
-
ss << std::fixed << std::setprecision(digits) << self;
|
|
106
|
-
return AnyValue::make_string(ss.str()); },
|
|
107
|
-
"toFixed");
|
|
108
|
-
return fn;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
inline AnyValue &get_toPrecision_fn()
|
|
112
|
-
{
|
|
113
|
-
static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
|
|
114
|
-
{
|
|
115
|
-
double self = thisVal.as_double();
|
|
116
|
-
if (args.empty() || args[0].is_undefined())
|
|
117
|
-
{
|
|
118
|
-
return AnyValue::make_number(self).get_own_property("toString").call(AnyValue::make_number(self), {}, "toString");
|
|
119
|
-
}
|
|
120
|
-
int precision = Operators_Private::ToInt32(args[0]);
|
|
121
|
-
if (precision < 1 || precision > 100)
|
|
122
|
-
{
|
|
123
|
-
throw Exception::make_exception("toPrecision() precision argument must be between 1 and 100", "RangeError");
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
std::ostringstream ss;
|
|
127
|
-
ss << std::setprecision(precision) << self;
|
|
128
|
-
return AnyValue::make_string(ss.str()); },
|
|
129
|
-
"toPrecision");
|
|
130
|
-
return fn;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
inline AnyValue &get_toString_fn()
|
|
134
|
-
{
|
|
135
|
-
static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
|
|
136
|
-
{
|
|
137
|
-
double self = thisVal.as_double();
|
|
138
|
-
int radix = 10;
|
|
139
|
-
if (!args.empty() && !args[0].is_undefined())
|
|
140
|
-
{
|
|
141
|
-
radix = Operators_Private::ToInt32(args[0]);
|
|
142
|
-
}
|
|
143
|
-
if (radix < 2 || radix > 36)
|
|
144
|
-
{
|
|
145
|
-
throw Exception::make_exception("toString() radix argument must be between 2 and 36", "RangeError");
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
return AnyValue::make_string(number_to_radix_string(self, radix)); },
|
|
149
|
-
"toString");
|
|
150
|
-
return fn;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
inline AnyValue &get_valueOf_fn()
|
|
154
|
-
{
|
|
155
|
-
static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue>) -> AnyValue
|
|
156
|
-
{ return AnyValue::make_number(thisVal.as_double()); },
|
|
157
|
-
"valueOf");
|
|
158
|
-
return fn;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
inline AnyValue &get_toLocaleString_fn()
|
|
162
|
-
{
|
|
163
|
-
static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue>) -> AnyValue
|
|
164
|
-
{ return AnyValue::make_string(AnyValue::make_number(thisVal.as_double()).to_std_string()); },
|
|
165
|
-
"toLocaleString");
|
|
166
|
-
return fn;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
inline std::optional<AnyValue> get(const std::string &key)
|
|
170
|
-
{
|
|
171
|
-
// --- toExponential(fractionDigits) ---
|
|
172
|
-
if (key == "toExponential")
|
|
173
|
-
{
|
|
174
|
-
return get_toExponential_fn();
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// --- toFixed(digits) ---
|
|
178
|
-
if (key == "toFixed")
|
|
179
|
-
{
|
|
180
|
-
return get_toFixed_fn();
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
// --- toPrecision(precision) ---
|
|
184
|
-
if (key == "toPrecision")
|
|
185
|
-
{
|
|
186
|
-
return get_toPrecision_fn();
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
// --- toString(radix) ---
|
|
190
|
-
if (key == "toString")
|
|
191
|
-
{
|
|
192
|
-
return get_toString_fn();
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
// --- valueOf() ---
|
|
196
|
-
if (key == "valueOf")
|
|
197
|
-
{
|
|
198
|
-
return get_valueOf_fn();
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
// --- toLocaleString() ---
|
|
202
|
-
if (key == "toLocaleString")
|
|
203
|
-
{
|
|
204
|
-
return get_toLocaleString_fn();
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
return std::nullopt;
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
}
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "types.hpp"
|
|
4
|
+
#include <optional>
|
|
5
|
+
|
|
6
|
+
namespace jspp
|
|
7
|
+
{
|
|
8
|
+
class AnyValue;
|
|
9
|
+
|
|
10
|
+
namespace NumberPrototypes
|
|
11
|
+
{
|
|
12
|
+
std::string number_to_radix_string(double value, int radix);
|
|
13
|
+
AnyValue &get_toExponential_fn();
|
|
14
|
+
AnyValue &get_toFixed_fn();
|
|
15
|
+
AnyValue &get_toPrecision_fn();
|
|
16
|
+
AnyValue &get_toString_fn();
|
|
17
|
+
AnyValue &get_valueOf_fn();
|
|
18
|
+
AnyValue &get_toLocaleString_fn();
|
|
19
|
+
|
|
20
|
+
std::optional<AnyValue> get(const std::string &key);
|
|
21
|
+
std::optional<AnyValue> get(const AnyValue &key);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -1,32 +1,16 @@
|
|
|
1
1
|
#pragma once
|
|
2
2
|
|
|
3
3
|
#include "types.hpp"
|
|
4
|
-
#include
|
|
5
|
-
#include "any_value.hpp"
|
|
6
|
-
#include "exception.hpp"
|
|
7
|
-
#include "utils/operators.hpp"
|
|
4
|
+
#include <optional>
|
|
8
5
|
|
|
9
6
|
namespace jspp
|
|
10
7
|
{
|
|
8
|
+
class AnyValue;
|
|
9
|
+
|
|
11
10
|
namespace ObjectPrototypes
|
|
12
11
|
{
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
{ return AnyValue::make_string(thisVal.to_std_string()); },
|
|
17
|
-
"toString");
|
|
18
|
-
return fn;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
inline std::optional<AnyValue> get(const std::string &key)
|
|
22
|
-
{
|
|
23
|
-
// --- toString() method ---
|
|
24
|
-
if (key == "toString" || key == WellKnownSymbols::toStringTag->key)
|
|
25
|
-
{
|
|
26
|
-
return get_toString_fn();
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return std::nullopt;
|
|
30
|
-
}
|
|
12
|
+
AnyValue& get_toString_fn();
|
|
13
|
+
std::optional<AnyValue> get(const std::string &key);
|
|
14
|
+
std::optional<AnyValue> get(const AnyValue &key);
|
|
31
15
|
}
|
|
32
|
-
}
|
|
16
|
+
}
|