@ugo-studio/jspp 0.3.0 → 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.
Files changed (127) hide show
  1. package/LICENSE +25 -25
  2. package/README.md +20 -12
  3. package/dist/cli/args.js +22 -0
  4. package/dist/cli/compiler.js +53 -0
  5. package/dist/cli/index.js +43 -107
  6. package/dist/cli/pch.js +71 -0
  7. package/dist/cli/runner.js +23 -0
  8. package/dist/cli/spinner.js +27 -11
  9. package/dist/cli/transpiler.js +20 -0
  10. package/dist/cli/utils.js +59 -0
  11. package/dist/cli/wasm.js +70 -0
  12. package/dist/index.js +17 -6
  13. package/dist/{analysis → interpreter/analysis}/scope.js +38 -3
  14. package/dist/{analysis → interpreter/analysis}/typeAnalyzer.js +563 -28
  15. package/dist/{core → interpreter/core}/codegen/class-handlers.js +1 -1
  16. package/dist/{core → interpreter/core}/codegen/control-flow-handlers.js +12 -11
  17. package/dist/{core → interpreter/core}/codegen/declaration-handlers.js +28 -9
  18. package/dist/{core → interpreter/core}/codegen/destructuring-handlers.js +9 -4
  19. package/dist/{core → interpreter/core}/codegen/expression-handlers.js +82 -88
  20. package/dist/{core → interpreter/core}/codegen/function-handlers.js +159 -46
  21. package/dist/{core → interpreter/core}/codegen/helpers.js +170 -25
  22. package/dist/interpreter/core/codegen/index.js +156 -0
  23. package/dist/{core → interpreter/core}/codegen/literal-handlers.js +9 -0
  24. package/dist/{core → interpreter/core}/codegen/statement-handlers.js +47 -7
  25. package/package.json +6 -4
  26. package/scripts/precompile-headers.ts +293 -50
  27. package/scripts/setup-compiler.ts +63 -63
  28. package/scripts/setup-emsdk.ts +114 -0
  29. package/src/prelude/any_value.cpp +888 -0
  30. package/src/prelude/any_value.hpp +29 -24
  31. package/src/prelude/{exception_helpers.hpp → exception.cpp} +53 -53
  32. package/src/prelude/exception.hpp +27 -27
  33. package/src/prelude/iterator_instantiations.hpp +10 -0
  34. package/src/prelude/{index.hpp → jspp.hpp} +13 -17
  35. package/src/prelude/library/array.cpp +191 -0
  36. package/src/prelude/library/array.hpp +5 -178
  37. package/src/prelude/library/boolean.cpp +30 -0
  38. package/src/prelude/library/boolean.hpp +14 -0
  39. package/src/prelude/library/console.cpp +125 -0
  40. package/src/prelude/library/console.hpp +9 -97
  41. package/src/prelude/library/error.cpp +100 -0
  42. package/src/prelude/library/error.hpp +8 -108
  43. package/src/prelude/library/function.cpp +69 -0
  44. package/src/prelude/library/function.hpp +6 -5
  45. package/src/prelude/library/global.cpp +98 -0
  46. package/src/prelude/library/global.hpp +12 -28
  47. package/src/prelude/library/global_usings.hpp +15 -0
  48. package/src/prelude/library/math.cpp +261 -0
  49. package/src/prelude/library/math.hpp +8 -288
  50. package/src/prelude/library/object.cpp +379 -0
  51. package/src/prelude/library/object.hpp +5 -267
  52. package/src/prelude/library/performance.cpp +21 -0
  53. package/src/prelude/library/performance.hpp +5 -20
  54. package/src/prelude/library/process.cpp +38 -0
  55. package/src/prelude/library/process.hpp +3 -31
  56. package/src/prelude/library/promise.cpp +131 -0
  57. package/src/prelude/library/promise.hpp +5 -116
  58. package/src/prelude/library/symbol.cpp +56 -0
  59. package/src/prelude/library/symbol.hpp +5 -46
  60. package/src/prelude/library/timer.cpp +88 -0
  61. package/src/prelude/library/timer.hpp +11 -87
  62. package/src/prelude/runtime.cpp +19 -0
  63. package/src/prelude/types.hpp +26 -20
  64. package/src/prelude/utils/access.hpp +123 -32
  65. package/src/prelude/utils/assignment_operators.hpp +119 -99
  66. package/src/prelude/utils/log_any_value/array.hpp +61 -40
  67. package/src/prelude/utils/log_any_value/function.hpp +39 -39
  68. package/src/prelude/utils/log_any_value/log_any_value.hpp +1 -1
  69. package/src/prelude/utils/log_any_value/object.hpp +60 -3
  70. package/src/prelude/utils/log_any_value/primitives.hpp +1 -1
  71. package/src/prelude/utils/operators.hpp +109 -94
  72. package/src/prelude/utils/operators_native.hpp +349 -0
  73. package/src/prelude/utils/well_known_symbols.hpp +24 -24
  74. package/src/prelude/values/array.cpp +1399 -0
  75. package/src/prelude/values/array.hpp +4 -0
  76. package/src/prelude/values/async_iterator.cpp +251 -0
  77. package/src/prelude/values/async_iterator.hpp +60 -32
  78. package/src/prelude/values/boolean.cpp +64 -0
  79. package/src/prelude/values/function.cpp +262 -0
  80. package/src/prelude/values/function.hpp +10 -30
  81. package/src/prelude/values/iterator.cpp +309 -0
  82. package/src/prelude/values/iterator.hpp +33 -64
  83. package/src/prelude/values/number.cpp +221 -0
  84. package/src/prelude/values/object.cpp +200 -0
  85. package/src/prelude/values/object.hpp +4 -0
  86. package/src/prelude/values/promise.cpp +479 -0
  87. package/src/prelude/values/promise.hpp +9 -2
  88. package/src/prelude/values/prototypes/array.hpp +46 -1348
  89. package/src/prelude/values/prototypes/async_iterator.hpp +19 -61
  90. package/src/prelude/values/prototypes/boolean.hpp +24 -0
  91. package/src/prelude/values/prototypes/function.hpp +7 -46
  92. package/src/prelude/values/prototypes/iterator.hpp +15 -191
  93. package/src/prelude/values/prototypes/number.hpp +30 -210
  94. package/src/prelude/values/prototypes/object.hpp +7 -23
  95. package/src/prelude/values/prototypes/promise.hpp +8 -186
  96. package/src/prelude/values/prototypes/string.hpp +28 -553
  97. package/src/prelude/values/prototypes/symbol.hpp +9 -70
  98. package/src/prelude/values/shape.hpp +52 -52
  99. package/src/prelude/values/string.cpp +485 -0
  100. package/src/prelude/values/symbol.cpp +89 -0
  101. package/src/prelude/values/symbol.hpp +101 -101
  102. package/dist/cli/file-utils.js +0 -20
  103. package/dist/cli-utils/args.js +0 -59
  104. package/dist/cli-utils/colors.js +0 -9
  105. package/dist/cli-utils/file-utils.js +0 -20
  106. package/dist/cli-utils/spinner.js +0 -55
  107. package/dist/cli.js +0 -153
  108. package/dist/core/codegen/index.js +0 -86
  109. package/src/prelude/any_value_access.hpp +0 -170
  110. package/src/prelude/any_value_defines.hpp +0 -190
  111. package/src/prelude/any_value_helpers.hpp +0 -374
  112. package/src/prelude/utils/operators_primitive.hpp +0 -337
  113. package/src/prelude/values/helpers/array.hpp +0 -199
  114. package/src/prelude/values/helpers/async_iterator.hpp +0 -275
  115. package/src/prelude/values/helpers/function.hpp +0 -109
  116. package/src/prelude/values/helpers/iterator.hpp +0 -145
  117. package/src/prelude/values/helpers/object.hpp +0 -104
  118. package/src/prelude/values/helpers/promise.hpp +0 -254
  119. package/src/prelude/values/helpers/string.hpp +0 -37
  120. package/src/prelude/values/helpers/symbol.hpp +0 -21
  121. /package/dist/{ast → interpreter/ast}/symbols.js +0 -0
  122. /package/dist/{ast → interpreter/ast}/types.js +0 -0
  123. /package/dist/{core → interpreter/core}/codegen/visitor.js +0 -0
  124. /package/dist/{core → interpreter/core}/constants.js +0 -0
  125. /package/dist/{core → interpreter/core}/error.js +0 -0
  126. /package/dist/{core → interpreter/core}/parser.js +0 -0
  127. /package/dist/{core → interpreter/core}/traverser.js +0 -0
@@ -1,61 +1,19 @@
1
- #pragma once
2
-
3
- #include "types.hpp"
4
- #include "values/async_iterator.hpp"
5
- #include "any_value.hpp"
6
- #include "exception.hpp"
7
- #include "utils/operators.hpp"
8
-
9
- namespace jspp
10
- {
11
- namespace AsyncIteratorPrototypes
12
- {
13
- inline AnyValue &get_toString_fn()
14
- {
15
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue>) -> AnyValue
16
- { return AnyValue::make_string(thisVal.as_async_iterator()->to_std_string()); },
17
- "toString");
18
- return fn;
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
+ }
@@ -0,0 +1,24 @@
1
+ #pragma once
2
+
3
+ #include "types.hpp"
4
+ #include <optional>
5
+
6
+ namespace jspp
7
+ {
8
+ class AnyValue;
9
+
10
+ namespace JsBoolean
11
+ {
12
+ std::string to_std_string(bool value);
13
+ std::string to_std_string(const AnyValue &value);
14
+ }
15
+
16
+ namespace BooleanPrototypes
17
+ {
18
+ AnyValue &get_toString_fn();
19
+ AnyValue &get_valueOf_fn();
20
+
21
+ std::optional<AnyValue> get(const std::string &key);
22
+ std::optional<AnyValue> get(const AnyValue &key);
23
+ }
24
+ }
@@ -1,56 +1,17 @@
1
1
  #pragma once
2
2
 
3
3
  #include "types.hpp"
4
- #include "values/function.hpp"
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
- inline AnyValue &get_toString_fn()
14
- {
15
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> _) -> AnyValue
16
- { return AnyValue::make_string(thisVal.as_function()->to_std_string()); },
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
1
  #pragma once
2
2
 
3
3
  #include "types.hpp"
4
- #include "values/iterator.hpp"
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 IteratorPrototypes
12
11
  {
13
- inline AnyValue &get_toString_fn()
14
- {
15
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue>) -> AnyValue
16
- { return AnyValue::make_string(thisVal.as_iterator()->to_std_string()); },
17
- "toString");
18
- return fn;
19
- }
20
-
21
- inline AnyValue &get_iterator_fn()
22
- {
23
- static AnyValue fn = AnyValue::make_generator([](const AnyValue &thisVal, std::span<const AnyValue>) -> AnyValue
24
- { return thisVal; },
25
- "Symbol.iterator");
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([](AnyValue thisVal, std::vector<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([](AnyValue thisVal, std::vector<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
- }
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);
200
24
  }
201
25
  }
@@ -1,210 +1,30 @@
1
- #pragma once
2
-
3
- #include "types.hpp"
4
- #include "any_value.hpp"
5
- #include "exception.hpp"
6
- #include "utils/operators.hpp"
7
- #include <string>
8
- #include <vector>
9
- #include <iomanip>
10
- #include <sstream>
11
- #include <cmath>
12
- #include <algorithm>
13
-
14
- namespace jspp
15
- {
16
- namespace NumberPrototypes
17
- {
18
- // Helper for radix conversion
19
- inline std::string number_to_radix_string(double value, int radix)
20
- {
21
- if (std::isnan(value))
22
- return "NaN";
23
- if (!std::isfinite(value))
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 JsNumber
11
+ {
12
+ std::string to_std_string(double num);
13
+ std::string to_std_string(const AnyValue &value);
14
+ std::string to_radix_string(double value, int radix);
15
+
16
+ }
17
+
18
+ namespace NumberPrototypes
19
+ {
20
+ AnyValue &get_toExponential_fn();
21
+ AnyValue &get_toFixed_fn();
22
+ AnyValue &get_toPrecision_fn();
23
+ AnyValue &get_toString_fn();
24
+ AnyValue &get_valueOf_fn();
25
+ AnyValue &get_toLocaleString_fn();
26
+
27
+ std::optional<AnyValue> get(const std::string &key);
28
+ std::optional<AnyValue> get(const AnyValue &key);
29
+ }
30
+ }
@@ -1,32 +1,16 @@
1
1
  #pragma once
2
2
 
3
3
  #include "types.hpp"
4
- #include "values/object.hpp"
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
- inline AnyValue& get_toString_fn()
14
- {
15
- static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> _) -> AnyValue
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
+ }