@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
@@ -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,221 @@
1
+ #include "jspp.hpp"
2
+ #include "values/prototypes/number.hpp"
3
+
4
+ namespace jspp
5
+ {
6
+
7
+ // --- JsNumber Implementation ---
8
+
9
+ namespace JsNumber
10
+ {
11
+ std::string to_std_string(double num)
12
+ {
13
+ if (std::isnan(num))
14
+ return "NaN";
15
+ if (std::abs(num) >= 1e21 || (std::abs(num) > 0 && std::abs(num) < 1e-6))
16
+ {
17
+ std::ostringstream oss;
18
+ oss << std::scientific << std::setprecision(4) << num;
19
+ return oss.str();
20
+ }
21
+ else
22
+ {
23
+ std::ostringstream oss;
24
+ oss << std::setprecision(6) << std::fixed << num;
25
+ std::string s = oss.str();
26
+ s.erase(s.find_last_not_of('0') + 1, std::string::npos);
27
+ if (!s.empty() && s.back() == '.')
28
+ s.pop_back();
29
+ return s;
30
+ }
31
+ }
32
+
33
+ std::string to_std_string(const AnyValue &value)
34
+ {
35
+ return to_std_string(value.as_double());
36
+ }
37
+
38
+ std::string to_radix_string(double value, int radix)
39
+ {
40
+ if (std::isnan(value))
41
+ return "NaN";
42
+ if (!std::isfinite(value))
43
+ return value > 0 ? "Infinity" : "-Infinity";
44
+
45
+ long long intPart = static_cast<long long>(value);
46
+
47
+ if (radix == 10)
48
+ {
49
+ return AnyValue::make_number(value).to_std_string();
50
+ }
51
+
52
+ bool negative = intPart < 0;
53
+ if (negative)
54
+ intPart = -intPart;
55
+
56
+ std::string chars = "0123456789abcdefghijklmnopqrstuvwxyz";
57
+ std::string res = "";
58
+
59
+ if (intPart == 0)
60
+ res = "0";
61
+ else
62
+ {
63
+ while (intPart > 0)
64
+ {
65
+ res += chars[intPart % radix];
66
+ intPart /= radix;
67
+ }
68
+ }
69
+ if (negative)
70
+ res += "-";
71
+ std::reverse(res.begin(), res.end());
72
+ return res;
73
+ }
74
+
75
+ }
76
+
77
+ // --- NumberPrototypes Implementation ---
78
+
79
+ namespace NumberPrototypes
80
+ {
81
+
82
+ AnyValue &get_toExponential_fn()
83
+ {
84
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
85
+ {
86
+ double self = thisVal.as_double();
87
+ int digits = -1;
88
+ if (!args.empty() && !args[0].is_undefined())
89
+ {
90
+ digits = Operators_Private::ToInt32(args[0]);
91
+ if (digits < 0 || digits > 100)
92
+ {
93
+ throw Exception::make_exception("toExponential() digits argument must be between 0 and 100", "RangeError");
94
+ }
95
+ }
96
+
97
+ std::ostringstream ss;
98
+ if (digits >= 0)
99
+ {
100
+ ss << std::scientific << std::setprecision(digits) << self;
101
+ }
102
+ else
103
+ {
104
+ ss << std::scientific << self;
105
+ }
106
+
107
+ std::string res = ss.str();
108
+ return AnyValue::make_string(res); },
109
+ "toExponential");
110
+ return fn;
111
+ }
112
+
113
+ AnyValue &get_toFixed_fn()
114
+ {
115
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
116
+ {
117
+ double self = thisVal.as_double();
118
+ int digits = 0;
119
+ if (!args.empty() && !args[0].is_undefined())
120
+ {
121
+ digits = Operators_Private::ToInt32(args[0]);
122
+ }
123
+ if (digits < 0 || digits > 100)
124
+ {
125
+ throw Exception::make_exception("toFixed() digits argument must be between 0 and 100", "RangeError");
126
+ }
127
+
128
+ std::ostringstream ss;
129
+ ss << std::fixed << std::setprecision(digits) << self;
130
+ return AnyValue::make_string(ss.str()); },
131
+ "toFixed");
132
+ return fn;
133
+ }
134
+
135
+ AnyValue &get_toPrecision_fn()
136
+ {
137
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
138
+ {
139
+ double self = thisVal.as_double();
140
+ if (args.empty() || args[0].is_undefined())
141
+ {
142
+ return AnyValue::make_number(self).get_own_property("toString").call(AnyValue::make_number(self), {}, "toString");
143
+ }
144
+ int precision = Operators_Private::ToInt32(args[0]);
145
+ if (precision < 1 || precision > 100)
146
+ {
147
+ throw Exception::make_exception("toPrecision() precision argument must be between 1 and 100", "RangeError");
148
+ }
149
+
150
+ std::ostringstream ss;
151
+ ss << std::setprecision(precision) << self;
152
+ return AnyValue::make_string(ss.str()); },
153
+ "toPrecision");
154
+ return fn;
155
+ }
156
+
157
+ AnyValue &get_toString_fn()
158
+ {
159
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
160
+ {
161
+ double self = thisVal.as_double();
162
+ int radix = 10;
163
+ if (!args.empty() && !args[0].is_undefined())
164
+ {
165
+ radix = Operators_Private::ToInt32(args[0]);
166
+ }
167
+ if (radix < 2 || radix > 36)
168
+ {
169
+ throw Exception::make_exception("toString() radix argument must be between 2 and 36", "RangeError");
170
+ }
171
+
172
+ return AnyValue::make_string(JsNumber::to_radix_string(self, radix)); },
173
+ "toString");
174
+ return fn;
175
+ }
176
+
177
+ AnyValue &get_valueOf_fn()
178
+ {
179
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue>) -> AnyValue
180
+ { return AnyValue::make_number(thisVal.as_double()); },
181
+ "valueOf");
182
+ return fn;
183
+ }
184
+
185
+ AnyValue &get_toLocaleString_fn()
186
+ {
187
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue>) -> AnyValue
188
+ { return AnyValue::make_string(AnyValue::make_number(thisVal.as_double()).to_std_string()); },
189
+ "toLocaleString");
190
+ return fn;
191
+ }
192
+
193
+ std::optional<AnyValue> get(const std::string &key)
194
+ {
195
+ if (key == "toExponential")
196
+ return get_toExponential_fn();
197
+ if (key == "toFixed")
198
+ return get_toFixed_fn();
199
+ if (key == "toPrecision")
200
+ return get_toPrecision_fn();
201
+ if (key == "toString")
202
+ return get_toString_fn();
203
+ if (key == "valueOf")
204
+ return get_valueOf_fn();
205
+ if (key == "toLocaleString")
206
+ return get_toLocaleString_fn();
207
+ return std::nullopt;
208
+ }
209
+
210
+ std::optional<AnyValue> get(const AnyValue &key)
211
+ {
212
+ if (key == "toString" || key == AnyValue::from_symbol(WellKnownSymbols::toStringTag))
213
+ return get_toString_fn();
214
+ if (key == "valueOf")
215
+ return get_valueOf_fn();
216
+ return std::nullopt;
217
+ }
218
+
219
+ } // namespace NumberPrototypes
220
+
221
+ } // namespace jspp
@@ -0,0 +1,200 @@
1
+ #include "jspp.hpp"
2
+ #include "values/object.hpp"
3
+ #include "values/prototypes/object.hpp"
4
+
5
+ namespace jspp
6
+ {
7
+
8
+ // --- JsObject Implementation ---
9
+
10
+ JsObject::JsObject() : shape(Shape::empty_shape()), proto(Constants::Null) {}
11
+
12
+ JsObject::JsObject(std::initializer_list<std::pair<std::string, AnyValue>> p, AnyValue pr) : proto(pr)
13
+ {
14
+ shape = Shape::empty_shape();
15
+ storage.reserve(p.size());
16
+ for (const auto &pair : p)
17
+ {
18
+ shape = shape->transition(pair.first);
19
+ storage.push_back(pair.second);
20
+ }
21
+ }
22
+
23
+ JsObject::JsObject(const std::map<std::string, AnyValue> &p, AnyValue pr) : proto(pr)
24
+ {
25
+ shape = Shape::empty_shape();
26
+ storage.reserve(p.size());
27
+ for (const auto &pair : p)
28
+ {
29
+ shape = shape->transition(pair.first);
30
+ storage.push_back(pair.second);
31
+ }
32
+ }
33
+
34
+ std::string JsObject::to_std_string() const
35
+ {
36
+ return "[Object Object]";
37
+ }
38
+
39
+ bool JsObject::has_property(const std::string &key) const
40
+ {
41
+ if (deleted_keys.count(key))
42
+ return false;
43
+
44
+ if (shape->get_offset(key).has_value())
45
+ return true;
46
+ if (!proto.is_null() && !proto.is_undefined())
47
+ {
48
+ if (proto.has_property(key))
49
+ return true;
50
+ }
51
+ if (ObjectPrototypes::get(key).has_value())
52
+ return true;
53
+ return false;
54
+ }
55
+
56
+ bool JsObject::has_symbol_property(const AnyValue &key) const
57
+ {
58
+ if (symbol_props.count(key))
59
+ return true;
60
+ if (!proto.is_null() && !proto.is_undefined())
61
+ {
62
+ if (proto.has_property(key))
63
+ return true;
64
+ }
65
+ if (ObjectPrototypes::get(key).has_value())
66
+ return true;
67
+ return false;
68
+ }
69
+
70
+ AnyValue JsObject::get_property(const std::string &key, const AnyValue &thisVal)
71
+ {
72
+ if (deleted_keys.count(key))
73
+ return Constants::UNDEFINED;
74
+
75
+ auto offset = shape->get_offset(key);
76
+ if (!offset.has_value())
77
+ {
78
+ if (!proto.is_null() && !proto.is_undefined())
79
+ {
80
+ if (proto.has_property(key))
81
+ {
82
+ return proto.get_property_with_receiver(key, thisVal);
83
+ }
84
+ }
85
+
86
+ auto proto_it = ObjectPrototypes::get(key);
87
+ if (proto_it.has_value())
88
+ {
89
+ return AnyValue::resolve_property_for_read(proto_it.value(), thisVal, key);
90
+ }
91
+ return Constants::UNDEFINED;
92
+ }
93
+ return AnyValue::resolve_property_for_read(storage[offset.value()], thisVal, key);
94
+ }
95
+
96
+ AnyValue JsObject::get_symbol_property(const AnyValue &key, const AnyValue &thisVal)
97
+ {
98
+ auto it = symbol_props.find(key);
99
+ if (it == symbol_props.end())
100
+ {
101
+ if (!proto.is_null() && !proto.is_undefined())
102
+ {
103
+ auto res = proto.get_symbol_property_with_receiver(key, thisVal);
104
+ if (!res.is_undefined())
105
+ return res;
106
+ }
107
+
108
+ auto proto_it = ObjectPrototypes::get(key);
109
+ if (proto_it.has_value())
110
+ {
111
+ return AnyValue::resolve_property_for_read(proto_it.value(), thisVal, key.to_std_string());
112
+ }
113
+ return Constants::UNDEFINED;
114
+ }
115
+ return AnyValue::resolve_property_for_read(it->second, thisVal, key.to_std_string());
116
+ }
117
+
118
+ AnyValue JsObject::set_property(const std::string &key, const AnyValue &value, const AnyValue &thisVal)
119
+ {
120
+ auto proto_it = ObjectPrototypes::get(key);
121
+ if (proto_it.has_value())
122
+ {
123
+ auto proto_value = proto_it.value();
124
+ if (proto_value.is_accessor_descriptor())
125
+ {
126
+ return AnyValue::resolve_property_for_write(proto_value, thisVal, value, key);
127
+ }
128
+ if (proto_value.is_data_descriptor() && !proto_value.as_data_descriptor()->writable)
129
+ {
130
+ return AnyValue::resolve_property_for_write(proto_value, thisVal, value, key);
131
+ }
132
+ }
133
+
134
+ if (deleted_keys.count(key))
135
+ deleted_keys.erase(key);
136
+
137
+ auto offset = shape->get_offset(key);
138
+ if (offset.has_value())
139
+ {
140
+ return AnyValue::resolve_property_for_write(storage[offset.value()], thisVal, value, key);
141
+ }
142
+ else
143
+ {
144
+ shape = shape->transition(key);
145
+ storage.push_back(value);
146
+ return value;
147
+ }
148
+ }
149
+
150
+ AnyValue JsObject::set_symbol_property(const AnyValue &key, const AnyValue &value, const AnyValue &thisVal)
151
+ {
152
+ auto it = symbol_props.find(key);
153
+ if (it != symbol_props.end())
154
+ {
155
+ return AnyValue::resolve_property_for_write(it->second, thisVal, value, key.to_std_string());
156
+ }
157
+ else
158
+ {
159
+ symbol_props[key] = value;
160
+ return value;
161
+ }
162
+ }
163
+
164
+ // --- ObjectPrototypes Implementation ---
165
+
166
+ namespace ObjectPrototypes
167
+ {
168
+
169
+ AnyValue &get_toString_fn()
170
+ {
171
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> _) -> AnyValue
172
+ { return AnyValue::make_string(thisVal.to_std_string()); },
173
+ "toString");
174
+ return fn;
175
+ }
176
+
177
+ std::optional<AnyValue> get(const std::string &key)
178
+ {
179
+ if (key == "toString")
180
+ {
181
+ return get_toString_fn();
182
+ }
183
+ return std::nullopt;
184
+ }
185
+
186
+ std::optional<AnyValue> get(const AnyValue &key)
187
+ {
188
+ if (key.is_string())
189
+ return get(key.as_string()->value);
190
+
191
+ if (key == AnyValue::from_symbol(WellKnownSymbols::toStringTag))
192
+ {
193
+ return get_toString_fn();
194
+ }
195
+ return std::nullopt;
196
+ }
197
+
198
+ } // namespace ObjectPrototypes
199
+
200
+ } // namespace jspp
@@ -16,6 +16,7 @@ namespace jspp
16
16
  std::vector<AnyValue> storage;
17
17
  AnyValue proto;
18
18
  std::unordered_set<std::string> deleted_keys;
19
+ std::map<AnyValue, AnyValue> symbol_props;
19
20
 
20
21
  JsObject();
21
22
  JsObject(std::initializer_list<std::pair<std::string, AnyValue>> p, AnyValue pr);
@@ -25,7 +26,10 @@ namespace jspp
25
26
 
26
27
  std::string to_std_string() const;
27
28
  bool has_property(const std::string &key) const;
29
+ bool has_symbol_property(const AnyValue &key) const;
28
30
  AnyValue get_property(const std::string &key, const AnyValue &thisVal);
31
+ AnyValue get_symbol_property(const AnyValue &key, const AnyValue &thisVal);
29
32
  AnyValue set_property(const std::string &key, const AnyValue &value, const AnyValue &thisVal);
33
+ AnyValue set_symbol_property(const AnyValue &key, const AnyValue &value, const AnyValue &thisVal);
30
34
  };
31
35
  }