@ugo-studio/jspp 0.2.9 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/LICENSE +25 -25
  2. package/README.md +20 -12
  3. package/dist/analysis/scope.js +5 -3
  4. package/dist/analysis/typeAnalyzer.js +21 -25
  5. package/dist/cli/index.js +14 -4
  6. package/dist/cli/utils.js +61 -0
  7. package/dist/core/codegen/class-handlers.js +6 -6
  8. package/dist/core/codegen/control-flow-handlers.js +10 -9
  9. package/dist/core/codegen/declaration-handlers.js +10 -3
  10. package/dist/core/codegen/destructuring-handlers.js +9 -4
  11. package/dist/core/codegen/expression-handlers.js +40 -29
  12. package/dist/core/codegen/function-handlers.js +78 -12
  13. package/dist/core/codegen/helpers.js +91 -14
  14. package/dist/core/codegen/index.js +4 -2
  15. package/dist/core/codegen/statement-handlers.js +9 -7
  16. package/package.json +2 -2
  17. package/scripts/precompile-headers.ts +249 -50
  18. package/scripts/setup-compiler.ts +63 -63
  19. package/src/prelude/any_value.cpp +636 -0
  20. package/src/prelude/any_value.hpp +369 -362
  21. package/src/prelude/{exception_helpers.hpp → exception.cpp} +53 -53
  22. package/src/prelude/exception.hpp +27 -27
  23. package/src/prelude/iterator_instantiations.hpp +10 -0
  24. package/src/prelude/{index.hpp → jspp.hpp} +10 -16
  25. package/src/prelude/library/array.cpp +191 -0
  26. package/src/prelude/library/array.hpp +13 -186
  27. package/src/prelude/library/console.cpp +125 -0
  28. package/src/prelude/library/console.hpp +24 -112
  29. package/src/prelude/library/error.cpp +100 -0
  30. package/src/prelude/library/error.hpp +13 -113
  31. package/src/prelude/library/function.cpp +69 -0
  32. package/src/prelude/library/function.hpp +11 -10
  33. package/src/prelude/library/global.cpp +96 -0
  34. package/src/prelude/library/global.hpp +12 -28
  35. package/src/prelude/library/global_usings.hpp +15 -0
  36. package/src/prelude/library/math.cpp +258 -0
  37. package/src/prelude/library/math.hpp +26 -308
  38. package/src/prelude/library/object.cpp +379 -0
  39. package/src/prelude/library/object.hpp +14 -276
  40. package/src/prelude/library/performance.cpp +21 -0
  41. package/src/prelude/library/performance.hpp +5 -20
  42. package/src/prelude/library/process.cpp +38 -0
  43. package/src/prelude/library/process.hpp +11 -39
  44. package/src/prelude/library/promise.cpp +131 -0
  45. package/src/prelude/library/promise.hpp +12 -123
  46. package/src/prelude/library/symbol.cpp +56 -0
  47. package/src/prelude/library/symbol.hpp +11 -52
  48. package/src/prelude/library/timer.cpp +88 -0
  49. package/src/prelude/library/timer.hpp +16 -92
  50. package/src/prelude/runtime.cpp +19 -0
  51. package/src/prelude/types.hpp +184 -179
  52. package/src/prelude/utils/access.hpp +502 -411
  53. package/src/prelude/utils/assignment_operators.hpp +99 -99
  54. package/src/prelude/utils/log_any_value/array.hpp +61 -40
  55. package/src/prelude/utils/log_any_value/function.hpp +39 -39
  56. package/src/prelude/utils/log_any_value/object.hpp +60 -3
  57. package/src/prelude/utils/operators.hpp +351 -336
  58. package/src/prelude/utils/operators_primitive.hpp +336 -336
  59. package/src/prelude/utils/well_known_symbols.hpp +24 -24
  60. package/src/prelude/values/array.cpp +1399 -0
  61. package/src/prelude/values/array.hpp +4 -1
  62. package/src/prelude/values/async_iterator.cpp +251 -0
  63. package/src/prelude/values/async_iterator.hpp +111 -83
  64. package/src/prelude/values/function.cpp +262 -0
  65. package/src/prelude/values/function.hpp +62 -82
  66. package/src/prelude/values/iterator.cpp +309 -0
  67. package/src/prelude/values/iterator.hpp +33 -64
  68. package/src/prelude/values/number.cpp +176 -0
  69. package/src/prelude/values/object.cpp +159 -0
  70. package/src/prelude/values/object.hpp +4 -0
  71. package/src/prelude/values/promise.cpp +479 -0
  72. package/src/prelude/values/promise.hpp +79 -72
  73. package/src/prelude/values/prototypes/array.hpp +46 -1336
  74. package/src/prelude/values/prototypes/async_iterator.hpp +19 -61
  75. package/src/prelude/values/prototypes/function.hpp +7 -46
  76. package/src/prelude/values/prototypes/iterator.hpp +25 -201
  77. package/src/prelude/values/prototypes/number.hpp +23 -210
  78. package/src/prelude/values/prototypes/object.hpp +7 -23
  79. package/src/prelude/values/prototypes/promise.hpp +18 -196
  80. package/src/prelude/values/prototypes/string.hpp +39 -542
  81. package/src/prelude/values/prototypes/symbol.hpp +9 -70
  82. package/src/prelude/values/shape.hpp +52 -52
  83. package/src/prelude/values/string.cpp +485 -0
  84. package/src/prelude/values/string.hpp +25 -26
  85. package/src/prelude/values/symbol.cpp +89 -0
  86. package/src/prelude/values/symbol.hpp +101 -101
  87. package/src/prelude/any_value_access.hpp +0 -170
  88. package/src/prelude/any_value_defines.hpp +0 -190
  89. package/src/prelude/any_value_helpers.hpp +0 -374
  90. package/src/prelude/values/helpers/array.hpp +0 -209
  91. package/src/prelude/values/helpers/async_iterator.hpp +0 -275
  92. package/src/prelude/values/helpers/function.hpp +0 -109
  93. package/src/prelude/values/helpers/iterator.hpp +0 -145
  94. package/src/prelude/values/helpers/object.hpp +0 -104
  95. package/src/prelude/values/helpers/promise.hpp +0 -254
  96. package/src/prelude/values/helpers/string.hpp +0 -61
  97. package/src/prelude/values/helpers/symbol.hpp +0 -21
@@ -0,0 +1,309 @@
1
+ #include "jspp.hpp"
2
+ #include "values/iterator.hpp"
3
+ #include "values/prototypes/iterator.hpp"
4
+
5
+ namespace jspp {
6
+
7
+ template <typename T>
8
+ JsIterator<T>::JsIterator(handle_type h) : handle(h) {}
9
+
10
+ template <typename T>
11
+ JsIterator<T>::JsIterator(JsIterator &&other) noexcept
12
+ : handle(std::exchange(other.handle, nullptr)),
13
+ props(std::move(other.props)),
14
+ symbol_props(std::move(other.symbol_props)) {}
15
+
16
+ template <typename T>
17
+ JsIterator<T>::~JsIterator() { if (handle) handle.destroy(); }
18
+
19
+ template <typename T>
20
+ JsIterator<T> JsIterator<T>::promise_type::get_return_object() {
21
+ return JsIterator{std::coroutine_handle<promise_type>::from_promise(*this)};
22
+ }
23
+
24
+ template <typename T>
25
+ std::suspend_always JsIterator<T>::promise_type::initial_suspend() noexcept { return {}; }
26
+
27
+ template <typename T>
28
+ std::suspend_always JsIterator<T>::promise_type::final_suspend() noexcept { return {}; }
29
+
30
+ template <typename T>
31
+ void JsIterator<T>::promise_type::unhandled_exception() {
32
+ try {
33
+ throw;
34
+ } catch (const GeneratorReturnException&) {
35
+ } catch (...) {
36
+ exception_ = std::current_exception();
37
+ }
38
+ }
39
+
40
+ template <typename T>
41
+ std::string JsIterator<T>::to_std_string() const { return "[object Generator]"; }
42
+
43
+ template <typename T>
44
+ typename JsIterator<T>::NextResult JsIterator<T>::next(const T &val)
45
+ {
46
+ if (!handle || handle.done()) return {std::nullopt, true};
47
+ handle.promise().input_value = val;
48
+ handle.resume();
49
+ if (handle.promise().exception_) std::rethrow_exception(handle.promise().exception_);
50
+ bool is_done = handle.done();
51
+ return {handle.promise().current_value, is_done};
52
+ }
53
+
54
+ template <typename T>
55
+ typename JsIterator<T>::NextResult JsIterator<T>::return_(const T &val)
56
+ {
57
+ if (!handle || handle.done()) return {val, true};
58
+ handle.promise().pending_return = true;
59
+ handle.promise().current_value = val;
60
+ handle.resume();
61
+ if (handle.promise().exception_) std::rethrow_exception(handle.promise().exception_);
62
+ return {handle.promise().current_value, true};
63
+ }
64
+
65
+ template <typename T>
66
+ typename JsIterator<T>::NextResult JsIterator<T>::throw_(const AnyValue &err)
67
+ {
68
+ if (!handle || handle.done()) throw Exception(err);
69
+ handle.promise().pending_exception = std::make_exception_ptr(Exception(err));
70
+ handle.resume();
71
+ if (handle.promise().exception_) std::rethrow_exception(handle.promise().exception_);
72
+ bool is_done = handle.done();
73
+ return {handle.promise().current_value, is_done};
74
+ }
75
+
76
+ template <typename T>
77
+ std::vector<T> JsIterator<T>::to_vector()
78
+ {
79
+ std::vector<T> result;
80
+ while (true) {
81
+ auto next_res = this->next();
82
+ if (next_res.done) break;
83
+ result.push_back(next_res.value.value_or(Constants::UNDEFINED));
84
+ }
85
+ return result;
86
+ }
87
+
88
+ template <typename T>
89
+ bool JsIterator<T>::has_symbol_property(const AnyValue &key) const { return symbol_props.count(key) > 0; }
90
+
91
+ template <typename T>
92
+ AnyValue JsIterator<T>::get_property(const std::string &key, AnyValue thisVal)
93
+ {
94
+ auto it = props.find(key);
95
+ if (it == props.end()) {
96
+ if constexpr (std::is_same_v<T, AnyValue>) {
97
+ auto proto_it = IteratorPrototypes::get(key);
98
+ if (proto_it.has_value()) return AnyValue::resolve_property_for_read(proto_it.value(), thisVal, key);
99
+ }
100
+ return Constants::UNDEFINED;
101
+ }
102
+ return AnyValue::resolve_property_for_read(it->second, thisVal, key);
103
+ }
104
+
105
+ template <typename T>
106
+ AnyValue JsIterator<T>::get_symbol_property(const AnyValue &key, AnyValue thisVal)
107
+ {
108
+ auto it = symbol_props.find(key);
109
+ if (it == symbol_props.end()) {
110
+ if constexpr (std::is_same_v<T, AnyValue>) {
111
+ auto proto_it = IteratorPrototypes::get(key);
112
+ if (proto_it.has_value()) return AnyValue::resolve_property_for_read(proto_it.value(), thisVal, key.to_std_string());
113
+ }
114
+ return Constants::UNDEFINED;
115
+ }
116
+ return AnyValue::resolve_property_for_read(it->second, thisVal, key.to_std_string());
117
+ }
118
+
119
+ template <typename T>
120
+ AnyValue JsIterator<T>::set_property(const std::string &key, AnyValue value, AnyValue thisVal)
121
+ {
122
+ if constexpr (std::is_same_v<T, AnyValue>) {
123
+ auto proto_it = IteratorPrototypes::get(key);
124
+ if (proto_it.has_value()) {
125
+ auto proto_value = proto_it.value();
126
+ if (proto_value.is_accessor_descriptor()) return AnyValue::resolve_property_for_write(proto_value, thisVal, value, key);
127
+ if (proto_value.is_data_descriptor() && !proto_value.as_data_descriptor()->writable) return AnyValue::resolve_property_for_write(proto_value, thisVal, value, key);
128
+ }
129
+ }
130
+ auto it = props.find(key);
131
+ if (it != props.end()) return jspp::AnyValue::resolve_property_for_write(it->second, thisVal, value, key);
132
+ else { props[key] = value; return value; }
133
+ }
134
+
135
+ template <typename T>
136
+ AnyValue JsIterator<T>::set_symbol_property(const AnyValue &key, AnyValue value, AnyValue thisVal)
137
+ {
138
+ auto it = symbol_props.find(key);
139
+ if (it != symbol_props.end()) return AnyValue::resolve_property_for_write(it->second, thisVal, value, key.to_std_string());
140
+ else { symbol_props[key] = value; return value; }
141
+ }
142
+
143
+ // Explicit template instantiation
144
+ template class JsIterator<AnyValue>;
145
+
146
+ namespace IteratorPrototypes {
147
+
148
+ AnyValue &get_toString_fn()
149
+ {
150
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue>) -> AnyValue
151
+ { return AnyValue::make_string(thisVal.as_iterator()->to_std_string()); },
152
+ "toString");
153
+ return fn;
154
+ }
155
+
156
+ AnyValue &get_iterator_fn()
157
+ {
158
+ static AnyValue fn = AnyValue::make_generator([](const AnyValue &thisVal, std::span<const AnyValue>) -> AnyValue
159
+ { return thisVal; },
160
+ "Symbol.iterator");
161
+ return fn;
162
+ }
163
+
164
+ AnyValue &get_next_fn()
165
+ {
166
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
167
+ {
168
+ AnyValue val = args.empty() ? Constants::UNDEFINED : args[0];
169
+ auto res = thisVal.as_iterator()->next(val);
170
+ return AnyValue::make_object({{"value", res.value.value_or(Constants::UNDEFINED)}, {"done", AnyValue::make_boolean(res.done)}}); },
171
+ "next");
172
+ return fn;
173
+ }
174
+
175
+ AnyValue &get_return_fn()
176
+ {
177
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
178
+ {
179
+ AnyValue val = args.empty() ? Constants::UNDEFINED : args[0];
180
+ auto res = thisVal.as_iterator()->return_(val);
181
+ return AnyValue::make_object({{"value", res.value.value_or(Constants::UNDEFINED)}, {"done", AnyValue::make_boolean(res.done)}}); },
182
+ "return");
183
+ return fn;
184
+ }
185
+
186
+ AnyValue &get_throw_fn()
187
+ {
188
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
189
+ {
190
+ AnyValue err = args.empty() ? Constants::UNDEFINED : args[0];
191
+ auto res = thisVal.as_iterator()->throw_(err);
192
+ return AnyValue::make_object({{"value", res.value.value_or(Constants::UNDEFINED)}, {"done", AnyValue::make_boolean(res.done)}}); },
193
+ "throw");
194
+ return fn;
195
+ }
196
+
197
+ AnyValue &get_toArray_fn()
198
+ {
199
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue>) -> AnyValue
200
+ { return AnyValue::make_array(thisVal.as_iterator()->to_vector()); },
201
+ "toArray");
202
+ return fn;
203
+ }
204
+
205
+ AnyValue &get_drop_fn()
206
+ {
207
+ static AnyValue fn = AnyValue::make_generator([](AnyValue thisVal, std::vector<AnyValue> args) -> JsIterator<AnyValue>
208
+ {
209
+ auto self = thisVal.as_iterator();
210
+ size_t skip_count = 0;
211
+ if (!args.empty()) {
212
+ skip_count = static_cast<size_t>(args[0].as_double());
213
+ }
214
+ size_t skipped = 0;
215
+ while (true)
216
+ {
217
+ auto next_res = self->next();
218
+ if (next_res.done) { break; }
219
+ if (skipped < skip_count)
220
+ {
221
+ skipped++;
222
+ continue;
223
+ }
224
+ co_yield next_res.value.value_or(Constants::UNDEFINED);
225
+ }
226
+ co_return jspp::Constants::UNDEFINED; },
227
+ "drop");
228
+ return fn;
229
+ }
230
+
231
+ AnyValue &get_take_fn()
232
+ {
233
+ static AnyValue fn = AnyValue::make_generator([](AnyValue thisVal, std::vector<AnyValue> args) -> JsIterator<AnyValue>
234
+ {
235
+ auto self = thisVal.as_iterator();
236
+ size_t take_count = 0;
237
+ if (!args.empty()) {
238
+ take_count = static_cast<size_t>(args[0].as_double());
239
+ }
240
+ size_t taken = 0;
241
+ while (true)
242
+ {
243
+ auto next_res = self->next();
244
+ if (next_res.done) { break; }
245
+ if (taken < take_count)
246
+ {
247
+ taken++;
248
+ co_yield next_res.value.value_or(Constants::UNDEFINED);
249
+ }
250
+ if (taken >= take_count)
251
+ {
252
+ self->return_();
253
+ break;
254
+ }
255
+ }
256
+ co_return jspp::Constants::UNDEFINED; },
257
+ "take");
258
+ return fn;
259
+ }
260
+
261
+ AnyValue &get_some_fn()
262
+ {
263
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
264
+ {
265
+ auto self = thisVal.as_iterator();
266
+ if (args.empty() || !args[0].is_function()) throw Exception::make_exception("callback is not a function", "TypeError");
267
+ auto callback = args[0].as_function();
268
+ while (true)
269
+ {
270
+ auto next_res = self->next();
271
+ if (next_res.done) { break; }
272
+ if (is_truthy(callback->call(thisVal, std::span<const AnyValue>((const jspp::AnyValue[]){next_res.value.value_or(Constants::UNDEFINED)}, 1))))
273
+ {
274
+ self->return_();
275
+ return Constants::TRUE;
276
+ }
277
+ }
278
+ return jspp::Constants::FALSE; },
279
+ "some");
280
+ return fn;
281
+ }
282
+
283
+ std::optional<AnyValue> get(const std::string &key)
284
+ {
285
+ if (key == "toString") return get_toString_fn();
286
+ if (key == "next") return get_next_fn();
287
+ if (key == "return") return get_return_fn();
288
+ if (key == "throw") return get_throw_fn();
289
+ if (key == "toArray") return get_toArray_fn();
290
+ if (key == "drop") return get_drop_fn();
291
+ if (key == "take") return get_take_fn();
292
+ if (key == "some") return get_some_fn();
293
+ return std::nullopt;
294
+ }
295
+
296
+ std::optional<AnyValue> get(const AnyValue &key)
297
+ {
298
+ if (key.is_string())
299
+ return get(key.as_string()->value);
300
+
301
+ if (key == AnyValue::from_symbol(WellKnownSymbols::toStringTag)) return get_toString_fn();
302
+ if (key == AnyValue::from_symbol(WellKnownSymbols::iterator)) return get_iterator_fn();
303
+
304
+ return std::nullopt;
305
+ }
306
+
307
+ } // namespace IteratorPrototypes
308
+
309
+ } // namespace jspp
@@ -3,16 +3,12 @@
3
3
  #include "types.hpp"
4
4
  #include <coroutine>
5
5
  #include <optional>
6
- #include <iostream>
7
- #include <utility>
8
- #include <exception>
6
+ #include <vector>
9
7
 
10
8
  namespace jspp
11
9
  {
12
- // Forward declaration of AnyValue
13
10
  class AnyValue;
14
11
 
15
- // Special exception to signal a return from a generator
16
12
  struct GeneratorReturnException {};
17
13
 
18
14
  template <typename T>
@@ -36,91 +32,64 @@ namespace jspp
36
32
  std::exception_ptr pending_exception = nullptr;
37
33
  bool pending_return = false;
38
34
 
39
- JsIterator get_return_object()
40
- {
41
- return JsIterator{
42
- std::coroutine_handle<promise_type>::from_promise(*this)};
43
- }
35
+ JsIterator get_return_object();
36
+ std::suspend_always initial_suspend() noexcept;
37
+ std::suspend_always final_suspend() noexcept;
44
38
 
45
- std::suspend_always initial_suspend() noexcept { return {}; }
46
-
47
- // valid js generators allow access to the return value after completion,
48
- // so we must suspend at the end to keep the promise (and value) alive.
49
- std::suspend_always final_suspend() noexcept { return {}; }
39
+ struct Awaiter
40
+ {
41
+ promise_type &p;
42
+ bool await_ready() { return false; }
43
+ void await_suspend(std::coroutine_handle<promise_type>) {}
44
+ T await_resume() {
45
+ if (p.pending_exception) {
46
+ auto ex = p.pending_exception;
47
+ p.pending_exception = nullptr;
48
+ std::rethrow_exception(ex);
49
+ }
50
+ if (p.pending_return) {
51
+ p.pending_return = false;
52
+ throw GeneratorReturnException{};
53
+ }
54
+ return p.input_value;
55
+ }
56
+ };
50
57
 
51
- // Handle co_yield
52
58
  template <typename From>
53
- auto yield_value(From &&from)
54
- {
59
+ Awaiter yield_value(From &&from) {
55
60
  current_value = std::forward<From>(from);
56
- struct Awaiter
57
- {
58
- promise_type &p;
59
- bool await_ready() { return false; }
60
- void await_suspend(std::coroutine_handle<promise_type>) {}
61
- T await_resume() {
62
- if (p.pending_exception) {
63
- auto ex = p.pending_exception;
64
- p.pending_exception = nullptr;
65
- std::rethrow_exception(ex);
66
- }
67
- if (p.pending_return) {
68
- p.pending_return = false;
69
- throw GeneratorReturnException{};
70
- }
71
- return p.input_value;
72
- }
73
- };
74
61
  return Awaiter{*this};
75
62
  }
76
63
 
77
- // Handle co_return
78
- // This replaces return_void.
79
- // It captures the final value and moves to final_suspend (implicit).
80
64
  template <typename From>
81
- void return_value(From &&from)
82
- {
65
+ void return_value(From &&from) {
83
66
  current_value = std::forward<From>(from);
84
67
  }
85
68
 
86
- void unhandled_exception()
87
- {
88
- try {
89
- throw;
90
- } catch (const GeneratorReturnException&) {
91
- // Handled return
92
- } catch (...) {
93
- exception_ = std::current_exception();
94
- }
95
- }
69
+ void unhandled_exception();
96
70
  };
97
71
 
98
72
  using handle_type = std::coroutine_handle<promise_type>;
99
73
  handle_type handle;
100
74
 
101
- explicit JsIterator(handle_type h) : handle(h) {}
102
- JsIterator(JsIterator &&other) noexcept
103
- : handle(std::exchange(other.handle, nullptr)),
104
- props(std::move(other.props)) {}
105
-
106
- // Delete copy constructor/assignment to ensure unique ownership of the handle
75
+ explicit JsIterator(handle_type h);
76
+ JsIterator(JsIterator &&other) noexcept;
107
77
  JsIterator(const JsIterator &) = delete;
108
78
  JsIterator &operator=(const JsIterator &) = delete;
109
-
110
- ~JsIterator()
111
- {
112
- if (handle)
113
- handle.destroy();
114
- }
79
+ ~JsIterator();
115
80
 
116
81
  std::unordered_map<std::string, AnyValue> props;
82
+ std::map<AnyValue, AnyValue> symbol_props;
117
83
 
118
84
  std::string to_std_string() const;
119
85
  NextResult next(const T &val = T());
120
86
  NextResult return_(const T &val = T());
121
87
  NextResult throw_(const AnyValue &err);
122
88
  std::vector<T> to_vector();
89
+ bool has_symbol_property(const AnyValue &key) const;
123
90
  AnyValue get_property(const std::string &key, AnyValue thisVal);
91
+ AnyValue get_symbol_property(const AnyValue &key, AnyValue thisVal);
124
92
  AnyValue set_property(const std::string &key, AnyValue value, AnyValue thisVal);
93
+ AnyValue set_symbol_property(const AnyValue &key, AnyValue value, AnyValue thisVal);
125
94
  };
126
- }
95
+ }
@@ -0,0 +1,176 @@
1
+ #include "jspp.hpp"
2
+ #include "values/prototypes/number.hpp"
3
+
4
+ namespace jspp {
5
+
6
+ namespace NumberPrototypes {
7
+
8
+ std::string number_to_radix_string(double value, int radix)
9
+ {
10
+ if (std::isnan(value))
11
+ return "NaN";
12
+ if (!std::isfinite(value))
13
+ return value > 0 ? "Infinity" : "-Infinity";
14
+
15
+ long long intPart = static_cast<long long>(value);
16
+
17
+ if (radix == 10)
18
+ {
19
+ return AnyValue::make_number(value).to_std_string();
20
+ }
21
+
22
+ bool negative = intPart < 0;
23
+ if (negative)
24
+ intPart = -intPart;
25
+
26
+ std::string chars = "0123456789abcdefghijklmnopqrstuvwxyz";
27
+ std::string res = "";
28
+
29
+ if (intPart == 0)
30
+ res = "0";
31
+ else
32
+ {
33
+ while (intPart > 0)
34
+ {
35
+ res += chars[intPart % radix];
36
+ intPart /= radix;
37
+ }
38
+ }
39
+ if (negative)
40
+ res += "-";
41
+ std::reverse(res.begin(), res.end());
42
+ return res;
43
+ }
44
+
45
+ AnyValue &get_toExponential_fn()
46
+ {
47
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
48
+ {
49
+ double self = thisVal.as_double();
50
+ int digits = -1;
51
+ if (!args.empty() && !args[0].is_undefined())
52
+ {
53
+ digits = Operators_Private::ToInt32(args[0]);
54
+ if (digits < 0 || digits > 100)
55
+ {
56
+ throw Exception::make_exception("toExponential() digits argument must be between 0 and 100", "RangeError");
57
+ }
58
+ }
59
+
60
+ std::ostringstream ss;
61
+ if (digits >= 0)
62
+ {
63
+ ss << std::scientific << std::setprecision(digits) << self;
64
+ }
65
+ else
66
+ {
67
+ ss << std::scientific << self;
68
+ }
69
+
70
+ std::string res = ss.str();
71
+ return AnyValue::make_string(res); },
72
+ "toExponential");
73
+ return fn;
74
+ }
75
+
76
+ AnyValue &get_toFixed_fn()
77
+ {
78
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
79
+ {
80
+ double self = thisVal.as_double();
81
+ int digits = 0;
82
+ if (!args.empty() && !args[0].is_undefined())
83
+ {
84
+ digits = Operators_Private::ToInt32(args[0]);
85
+ }
86
+ if (digits < 0 || digits > 100)
87
+ {
88
+ throw Exception::make_exception("toFixed() digits argument must be between 0 and 100", "RangeError");
89
+ }
90
+
91
+ std::ostringstream ss;
92
+ ss << std::fixed << std::setprecision(digits) << self;
93
+ return AnyValue::make_string(ss.str()); },
94
+ "toFixed");
95
+ return fn;
96
+ }
97
+
98
+ AnyValue &get_toPrecision_fn()
99
+ {
100
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
101
+ {
102
+ double self = thisVal.as_double();
103
+ if (args.empty() || args[0].is_undefined())
104
+ {
105
+ return AnyValue::make_number(self).get_own_property("toString").call(AnyValue::make_number(self), {}, "toString");
106
+ }
107
+ int precision = Operators_Private::ToInt32(args[0]);
108
+ if (precision < 1 || precision > 100)
109
+ {
110
+ throw Exception::make_exception("toPrecision() precision argument must be between 1 and 100", "RangeError");
111
+ }
112
+
113
+ std::ostringstream ss;
114
+ ss << std::setprecision(precision) << self;
115
+ return AnyValue::make_string(ss.str()); },
116
+ "toPrecision");
117
+ return fn;
118
+ }
119
+
120
+ AnyValue &get_toString_fn()
121
+ {
122
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
123
+ {
124
+ double self = thisVal.as_double();
125
+ int radix = 10;
126
+ if (!args.empty() && !args[0].is_undefined())
127
+ {
128
+ radix = Operators_Private::ToInt32(args[0]);
129
+ }
130
+ if (radix < 2 || radix > 36)
131
+ {
132
+ throw Exception::make_exception("toString() radix argument must be between 2 and 36", "RangeError");
133
+ }
134
+
135
+ return AnyValue::make_string(number_to_radix_string(self, radix)); },
136
+ "toString");
137
+ return fn;
138
+ }
139
+
140
+ AnyValue &get_valueOf_fn()
141
+ {
142
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue>) -> AnyValue
143
+ { return AnyValue::make_number(thisVal.as_double()); },
144
+ "valueOf");
145
+ return fn;
146
+ }
147
+
148
+ AnyValue &get_toLocaleString_fn()
149
+ {
150
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue>) -> AnyValue
151
+ { return AnyValue::make_string(AnyValue::make_number(thisVal.as_double()).to_std_string()); },
152
+ "toLocaleString");
153
+ return fn;
154
+ }
155
+
156
+ std::optional<AnyValue> get(const std::string &key)
157
+ {
158
+ if (key == "toExponential") return get_toExponential_fn();
159
+ if (key == "toFixed") return get_toFixed_fn();
160
+ if (key == "toPrecision") return get_toPrecision_fn();
161
+ if (key == "toString") return get_toString_fn();
162
+ if (key == "valueOf") return get_valueOf_fn();
163
+ if (key == "toLocaleString") return get_toLocaleString_fn();
164
+ return std::nullopt;
165
+ }
166
+
167
+ std::optional<AnyValue> get(const AnyValue &key)
168
+ {
169
+ if (key == "toString" || key == AnyValue::from_symbol(WellKnownSymbols::toStringTag)) return get_toString_fn();
170
+ if (key == "valueOf") return get_valueOf_fn();
171
+ return std::nullopt;
172
+ }
173
+
174
+ } // namespace NumberPrototypes
175
+
176
+ } // namespace jspp