@ugo-studio/jspp 0.2.9 → 0.3.0

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 (35) hide show
  1. package/dist/core/codegen/class-handlers.js +6 -6
  2. package/dist/core/codegen/statement-handlers.js +1 -1
  3. package/package.json +1 -1
  4. package/src/prelude/any_value.hpp +362 -362
  5. package/src/prelude/any_value_access.hpp +170 -170
  6. package/src/prelude/any_value_defines.hpp +189 -189
  7. package/src/prelude/any_value_helpers.hpp +374 -374
  8. package/src/prelude/library/array.hpp +185 -185
  9. package/src/prelude/library/console.hpp +111 -111
  10. package/src/prelude/library/error.hpp +112 -112
  11. package/src/prelude/library/function.hpp +10 -10
  12. package/src/prelude/library/math.hpp +307 -307
  13. package/src/prelude/library/object.hpp +275 -275
  14. package/src/prelude/library/process.hpp +39 -39
  15. package/src/prelude/library/promise.hpp +123 -123
  16. package/src/prelude/library/symbol.hpp +52 -52
  17. package/src/prelude/library/timer.hpp +91 -91
  18. package/src/prelude/types.hpp +178 -178
  19. package/src/prelude/utils/access.hpp +411 -411
  20. package/src/prelude/utils/operators.hpp +336 -336
  21. package/src/prelude/values/array.hpp +0 -1
  22. package/src/prelude/values/async_iterator.hpp +83 -83
  23. package/src/prelude/values/function.hpp +82 -82
  24. package/src/prelude/values/helpers/array.hpp +198 -208
  25. package/src/prelude/values/helpers/async_iterator.hpp +275 -275
  26. package/src/prelude/values/helpers/function.hpp +108 -108
  27. package/src/prelude/values/helpers/iterator.hpp +144 -144
  28. package/src/prelude/values/helpers/promise.hpp +253 -253
  29. package/src/prelude/values/helpers/string.hpp +37 -61
  30. package/src/prelude/values/promise.hpp +72 -72
  31. package/src/prelude/values/prototypes/array.hpp +14 -2
  32. package/src/prelude/values/prototypes/iterator.hpp +201 -201
  33. package/src/prelude/values/prototypes/promise.hpp +196 -196
  34. package/src/prelude/values/prototypes/string.hpp +564 -542
  35. package/src/prelude/values/string.hpp +25 -26
@@ -1,254 +1,254 @@
1
- #pragma once
2
-
3
- #include "types.hpp"
4
- #include "values/promise.hpp"
5
- #include "any_value.hpp"
6
- #include "values/prototypes/promise.hpp"
7
-
8
- namespace jspp
9
- {
10
-
11
- inline PromiseState::PromiseState() : result(Constants::UNDEFINED) {}
12
-
13
- inline JsPromise::JsPromise() : state(std::make_shared<PromiseState>()) {}
14
-
15
- inline void JsPromise::resolve(AnyValue value)
16
- {
17
- if (state->status != PromiseStatus::Pending)
18
- return;
19
-
20
- if (value.is_promise())
21
- {
22
- auto p = value.as_promise();
23
- if (p->state == state)
24
- {
25
- reject(AnyValue::make_string("TypeError: Chaining cycle detected for promise"));
26
- return;
27
- }
28
-
29
- auto weak_state = std::weak_ptr<PromiseState>(state);
30
-
31
- p->then(
32
- [weak_state](AnyValue v)
33
- {
34
- if (auto s = weak_state.lock())
35
- {
36
- // We can't easily use JsPromise here because it's a HeapObject now.
37
- // But we can manually resolve the state if we have a way.
38
- // Actually, we can create a temporary AnyValue from the state.
39
- // But AnyValue expects a JsPromise* which was allocated with new.
40
- // This is tricky.
41
- // Let's assume we can just modify the status and result of the state directly.
42
- s->status = PromiseStatus::Fulfilled;
43
- s->result = v;
44
- auto callbacks = s->onFulfilled;
45
- s->onFulfilled.clear();
46
- s->onRejected.clear();
47
- for (auto &cb : callbacks)
48
- jspp::Scheduler::instance().enqueue([cb, v]()
49
- { cb(v); });
50
- }
51
- },
52
- [weak_state](AnyValue r)
53
- {
54
- if (auto s = weak_state.lock())
55
- {
56
- s->status = PromiseStatus::Rejected;
57
- s->result = r;
58
- auto callbacks = s->onRejected;
59
- s->onFulfilled.clear();
60
- s->onRejected.clear();
61
- for (auto &cb : callbacks)
62
- jspp::Scheduler::instance().enqueue([cb, r]()
63
- { cb(r); });
64
- }
65
- });
66
- return;
67
- }
68
-
69
- state->status = PromiseStatus::Fulfilled;
70
- state->result = value;
71
-
72
- // Schedule callbacks
73
- auto callbacks = state->onFulfilled;
74
- state->onFulfilled.clear();
75
- state->onRejected.clear();
76
-
77
- for (auto &cb : callbacks)
78
- {
79
- jspp::Scheduler::instance().enqueue([cb, value]()
80
- { cb(value); });
81
- }
82
- }
83
-
84
- inline void JsPromise::reject(AnyValue reason)
85
- {
86
- if (state->status != PromiseStatus::Pending)
87
- return;
88
- state->status = PromiseStatus::Rejected;
89
- state->result = reason;
90
-
91
- auto callbacks = state->onRejected;
92
- state->onFulfilled.clear();
93
- state->onRejected.clear();
94
-
95
- for (auto &cb : callbacks)
96
- {
97
- jspp::Scheduler::instance().enqueue([cb, reason]()
98
- { cb(reason); });
99
- }
100
- }
101
-
102
- inline void JsPromise::then(std::function<void(AnyValue)> onFulfilled, std::function<void(AnyValue)> onRejected)
103
- {
104
- if (state->status == PromiseStatus::Fulfilled)
105
- {
106
- if (onFulfilled)
107
- {
108
- AnyValue val = state->result;
109
- jspp::Scheduler::instance().enqueue([onFulfilled, val]()
110
- { onFulfilled(val); });
111
- }
112
- }
113
- else if (state->status == PromiseStatus::Rejected)
114
- {
115
- if (onRejected)
116
- {
117
- AnyValue val = state->result;
118
- jspp::Scheduler::instance().enqueue([onRejected, val]()
119
- { onRejected(val); });
120
- }
121
- }
122
- else
123
- {
124
- if (onFulfilled)
125
- state->onFulfilled.push_back(onFulfilled);
126
- if (onRejected)
127
- state->onRejected.push_back(onRejected);
128
- }
129
- }
130
-
131
- inline auto JsPromise::operator co_await() const
132
- {
133
- // This is safe because AnyValue::make_promise copies the JsPromise (which is a HeapObject)
134
- // Actually, AnyValue::make_promise(const JsPromise&) does from_ptr(new JsPromise(promise)).
135
- return AnyValueAwaiter{AnyValue::make_promise(*this)};
136
- }
137
-
138
- inline std::string JsPromise::to_std_string() const
139
- {
140
- return "[object Promise]";
141
- }
142
-
143
- inline AnyValue JsPromise::get_property(const std::string &key, AnyValue thisVal)
144
- {
145
- // Prototype lookup
146
- auto proto_it = PromisePrototypes::get(key);
147
- if (proto_it.has_value())
148
- {
149
- return AnyValue::resolve_property_for_read(proto_it.value(), thisVal, key);
150
- }
151
-
152
- auto it = props.find(key);
153
- if (it != props.end())
154
- {
155
- return AnyValue::resolve_property_for_read(it->second, thisVal, key);
156
- }
157
- return Constants::UNDEFINED;
158
- }
159
-
160
- inline AnyValue JsPromise::set_property(const std::string &key, AnyValue value, AnyValue thisVal)
161
- {
162
- auto it = props.find(key);
163
- if (it != props.end())
164
- {
165
- return AnyValue::resolve_property_for_write(it->second, thisVal, value, key);
166
- }
167
- else
168
- {
169
- props[key] = value;
170
- return value;
171
- }
172
- }
173
-
174
- // --- Coroutine Methods ---
175
-
176
- inline void JsPromisePromiseType::return_value(AnyValue val)
177
- {
178
- promise.resolve(val);
179
- }
180
-
181
- inline void JsPromisePromiseType::unhandled_exception()
182
- {
183
- try
184
- {
185
- throw;
186
- }
187
- catch (const Exception &e)
188
- {
189
- promise.reject(e.data);
190
- }
191
- catch (const std::exception &e)
192
- {
193
- promise.reject(AnyValue::make_string(e.what()));
194
- }
195
- catch (...)
196
- {
197
- promise.reject(AnyValue::make_string("Unknown exception"));
198
- }
199
- }
200
-
201
- inline auto JsPromisePromiseType::await_transform(AnyValue value)
202
- {
203
- return AnyValueAwaiter{value};
204
- }
205
-
206
- inline auto JsPromisePromiseType::await_transform(const JsPromise &value)
207
- {
208
- // value is a JsPromise& which is a HeapObject.
209
- // We wrap it in a temporary AnyValue for Awaiter.
210
- // Wait, AnyValue::make_promise(value) will allocate a new JsPromise on heap.
211
- // This is fine for now.
212
- return AnyValueAwaiter{AnyValue::make_promise(value)};
213
- }
214
-
215
- // --- AnyValueAwaiter ---
216
-
217
- inline bool AnyValueAwaiter::await_ready()
218
- {
219
- return false;
220
- }
221
-
222
- inline void AnyValueAwaiter::await_suspend(std::coroutine_handle<> h)
223
- {
224
- if (!value.is_promise())
225
- {
226
- jspp::Scheduler::instance().enqueue([h]() mutable
227
- { h.resume(); });
228
- return;
229
- }
230
- auto p = value.as_promise();
231
-
232
- p->then(
233
- [h](AnyValue v) mutable
234
- { h.resume(); },
235
- [h](AnyValue e) mutable
236
- { h.resume(); });
237
- }
238
-
239
- inline AnyValue AnyValueAwaiter::await_resume()
240
- {
241
- if (!value.is_promise())
242
- return value;
243
- auto p = value.as_promise();
244
- if (p->state->status == PromiseStatus::Fulfilled)
245
- {
246
- return p->state->result;
247
- }
248
- else
249
- {
250
- throw Exception(p->state->result);
251
- }
252
- }
253
-
1
+ #pragma once
2
+
3
+ #include "types.hpp"
4
+ #include "values/promise.hpp"
5
+ #include "any_value.hpp"
6
+ #include "values/prototypes/promise.hpp"
7
+
8
+ namespace jspp
9
+ {
10
+
11
+ inline PromiseState::PromiseState() : result(Constants::UNDEFINED) {}
12
+
13
+ inline JsPromise::JsPromise() : state(std::make_shared<PromiseState>()) {}
14
+
15
+ inline void JsPromise::resolve(AnyValue value)
16
+ {
17
+ if (state->status != PromiseStatus::Pending)
18
+ return;
19
+
20
+ if (value.is_promise())
21
+ {
22
+ auto p = value.as_promise();
23
+ if (p->state == state)
24
+ {
25
+ reject(AnyValue::make_string("TypeError: Chaining cycle detected for promise"));
26
+ return;
27
+ }
28
+
29
+ auto weak_state = std::weak_ptr<PromiseState>(state);
30
+
31
+ p->then(
32
+ [weak_state](AnyValue v)
33
+ {
34
+ if (auto s = weak_state.lock())
35
+ {
36
+ // We can't easily use JsPromise here because it's a HeapObject now.
37
+ // But we can manually resolve the state if we have a way.
38
+ // Actually, we can create a temporary AnyValue from the state.
39
+ // But AnyValue expects a JsPromise* which was allocated with new.
40
+ // This is tricky.
41
+ // Let's assume we can just modify the status and result of the state directly.
42
+ s->status = PromiseStatus::Fulfilled;
43
+ s->result = v;
44
+ auto callbacks = s->onFulfilled;
45
+ s->onFulfilled.clear();
46
+ s->onRejected.clear();
47
+ for (auto &cb : callbacks)
48
+ jspp::Scheduler::instance().enqueue([cb, v]()
49
+ { cb(v); });
50
+ }
51
+ },
52
+ [weak_state](AnyValue r)
53
+ {
54
+ if (auto s = weak_state.lock())
55
+ {
56
+ s->status = PromiseStatus::Rejected;
57
+ s->result = r;
58
+ auto callbacks = s->onRejected;
59
+ s->onFulfilled.clear();
60
+ s->onRejected.clear();
61
+ for (auto &cb : callbacks)
62
+ jspp::Scheduler::instance().enqueue([cb, r]()
63
+ { cb(r); });
64
+ }
65
+ });
66
+ return;
67
+ }
68
+
69
+ state->status = PromiseStatus::Fulfilled;
70
+ state->result = value;
71
+
72
+ // Schedule callbacks
73
+ auto callbacks = state->onFulfilled;
74
+ state->onFulfilled.clear();
75
+ state->onRejected.clear();
76
+
77
+ for (auto &cb : callbacks)
78
+ {
79
+ jspp::Scheduler::instance().enqueue([cb, value]()
80
+ { cb(value); });
81
+ }
82
+ }
83
+
84
+ inline void JsPromise::reject(AnyValue reason)
85
+ {
86
+ if (state->status != PromiseStatus::Pending)
87
+ return;
88
+ state->status = PromiseStatus::Rejected;
89
+ state->result = reason;
90
+
91
+ auto callbacks = state->onRejected;
92
+ state->onFulfilled.clear();
93
+ state->onRejected.clear();
94
+
95
+ for (auto &cb : callbacks)
96
+ {
97
+ jspp::Scheduler::instance().enqueue([cb, reason]()
98
+ { cb(reason); });
99
+ }
100
+ }
101
+
102
+ inline void JsPromise::then(std::function<void(AnyValue)> onFulfilled, std::function<void(AnyValue)> onRejected)
103
+ {
104
+ if (state->status == PromiseStatus::Fulfilled)
105
+ {
106
+ if (onFulfilled)
107
+ {
108
+ AnyValue val = state->result;
109
+ jspp::Scheduler::instance().enqueue([onFulfilled, val]()
110
+ { onFulfilled(val); });
111
+ }
112
+ }
113
+ else if (state->status == PromiseStatus::Rejected)
114
+ {
115
+ if (onRejected)
116
+ {
117
+ AnyValue val = state->result;
118
+ jspp::Scheduler::instance().enqueue([onRejected, val]()
119
+ { onRejected(val); });
120
+ }
121
+ }
122
+ else
123
+ {
124
+ if (onFulfilled)
125
+ state->onFulfilled.push_back(onFulfilled);
126
+ if (onRejected)
127
+ state->onRejected.push_back(onRejected);
128
+ }
129
+ }
130
+
131
+ inline auto JsPromise::operator co_await() const
132
+ {
133
+ // This is safe because AnyValue::make_promise copies the JsPromise (which is a HeapObject)
134
+ // Actually, AnyValue::make_promise(const JsPromise&) does from_ptr(new JsPromise(promise)).
135
+ return AnyValueAwaiter{AnyValue::make_promise(*this)};
136
+ }
137
+
138
+ inline std::string JsPromise::to_std_string() const
139
+ {
140
+ return "[object Promise]";
141
+ }
142
+
143
+ inline AnyValue JsPromise::get_property(const std::string &key, AnyValue thisVal)
144
+ {
145
+ // Prototype lookup
146
+ auto proto_it = PromisePrototypes::get(key);
147
+ if (proto_it.has_value())
148
+ {
149
+ return AnyValue::resolve_property_for_read(proto_it.value(), thisVal, key);
150
+ }
151
+
152
+ auto it = props.find(key);
153
+ if (it != props.end())
154
+ {
155
+ return AnyValue::resolve_property_for_read(it->second, thisVal, key);
156
+ }
157
+ return Constants::UNDEFINED;
158
+ }
159
+
160
+ inline AnyValue JsPromise::set_property(const std::string &key, AnyValue value, AnyValue thisVal)
161
+ {
162
+ auto it = props.find(key);
163
+ if (it != props.end())
164
+ {
165
+ return AnyValue::resolve_property_for_write(it->second, thisVal, value, key);
166
+ }
167
+ else
168
+ {
169
+ props[key] = value;
170
+ return value;
171
+ }
172
+ }
173
+
174
+ // --- Coroutine Methods ---
175
+
176
+ inline void JsPromisePromiseType::return_value(AnyValue val)
177
+ {
178
+ promise.resolve(val);
179
+ }
180
+
181
+ inline void JsPromisePromiseType::unhandled_exception()
182
+ {
183
+ try
184
+ {
185
+ throw;
186
+ }
187
+ catch (const Exception &e)
188
+ {
189
+ promise.reject(e.data);
190
+ }
191
+ catch (const std::exception &e)
192
+ {
193
+ promise.reject(AnyValue::make_string(e.what()));
194
+ }
195
+ catch (...)
196
+ {
197
+ promise.reject(AnyValue::make_string("Unknown exception"));
198
+ }
199
+ }
200
+
201
+ inline auto JsPromisePromiseType::await_transform(AnyValue value)
202
+ {
203
+ return AnyValueAwaiter{value};
204
+ }
205
+
206
+ inline auto JsPromisePromiseType::await_transform(const JsPromise &value)
207
+ {
208
+ // value is a JsPromise& which is a HeapObject.
209
+ // We wrap it in a temporary AnyValue for Awaiter.
210
+ // Wait, AnyValue::make_promise(value) will allocate a new JsPromise on heap.
211
+ // This is fine for now.
212
+ return AnyValueAwaiter{AnyValue::make_promise(value)};
213
+ }
214
+
215
+ // --- AnyValueAwaiter ---
216
+
217
+ inline bool AnyValueAwaiter::await_ready()
218
+ {
219
+ return false;
220
+ }
221
+
222
+ inline void AnyValueAwaiter::await_suspend(std::coroutine_handle<> h)
223
+ {
224
+ if (!value.is_promise())
225
+ {
226
+ jspp::Scheduler::instance().enqueue([h]() mutable
227
+ { h.resume(); });
228
+ return;
229
+ }
230
+ auto p = value.as_promise();
231
+
232
+ p->then(
233
+ [h](AnyValue v) mutable
234
+ { h.resume(); },
235
+ [h](AnyValue e) mutable
236
+ { h.resume(); });
237
+ }
238
+
239
+ inline AnyValue AnyValueAwaiter::await_resume()
240
+ {
241
+ if (!value.is_promise())
242
+ return value;
243
+ auto p = value.as_promise();
244
+ if (p->state->status == PromiseStatus::Fulfilled)
245
+ {
246
+ return p->state->result;
247
+ }
248
+ else
249
+ {
250
+ throw Exception(p->state->result);
251
+ }
252
+ }
253
+
254
254
  }
@@ -1,61 +1,37 @@
1
- #pragma once
2
-
3
- #include "types.hpp"
4
- #include "values/array.hpp"
5
- #include "values/string.hpp"
6
- #include "exception.hpp"
7
- #include "any_value.hpp"
8
- #include "values/prototypes/string.hpp"
9
-
10
- inline std::string jspp::JsString::to_std_string() const
11
- {
12
- return value;
13
- }
14
-
15
- inline jspp::JsIterator<jspp::AnyValue> jspp::JsString::get_iterator()
16
- {
17
- for (size_t i = 0; i < value.length();)
18
- {
19
- unsigned char c = static_cast<unsigned char>(value[i]);
20
- size_t len = 1;
21
- if ((c & 0x80) == 0)
22
- len = 1;
23
- else if ((c & 0xE0) == 0xC0)
24
- len = 2;
25
- else if ((c & 0xF0) == 0xE0)
26
- len = 3;
27
- else if ((c & 0xF8) == 0xF0)
28
- len = 4;
29
-
30
- if (i + len > value.length())
31
- len = value.length() - i;
32
-
33
- co_yield AnyValue::make_string(value.substr(i, len));
34
- i += len;
35
- }
36
- co_return AnyValue::make_undefined();
37
- }
38
-
39
- inline jspp::AnyValue jspp::JsString::get_property(const std::string &key, const AnyValue &thisVal)
40
- {
41
- auto proto_fn = StringPrototypes::get(key);
42
- if (proto_fn.has_value())
43
- {
44
- return AnyValue::resolve_property_for_read(proto_fn.value(), thisVal, key);
45
- }
46
- if (JsArray::is_array_index(key))
47
- {
48
- uint32_t idx = static_cast<uint32_t>(std::stoull(key));
49
- return get_property(idx);
50
- }
51
- return Constants::UNDEFINED;
52
- }
53
-
54
- inline jspp::AnyValue jspp::JsString::get_property(uint32_t idx)
55
- {
56
- if (idx < value.length())
57
- {
58
- return AnyValue::make_string(std::string(1, value[idx]));
59
- }
60
- return Constants::UNDEFINED;
61
- }
1
+ #pragma once
2
+
3
+ #include "types.hpp"
4
+ #include "values/array.hpp"
5
+ #include "values/string.hpp"
6
+ #include "exception.hpp"
7
+ #include "any_value.hpp"
8
+ #include "values/prototypes/string.hpp"
9
+
10
+ inline std::string jspp::JsString::to_std_string() const
11
+ {
12
+ return value;
13
+ }
14
+
15
+ inline jspp::AnyValue jspp::JsString::get_property(const std::string &key, const AnyValue &thisVal)
16
+ {
17
+ auto proto_fn = StringPrototypes::get(key);
18
+ if (proto_fn.has_value())
19
+ {
20
+ return AnyValue::resolve_property_for_read(proto_fn.value(), thisVal, key);
21
+ }
22
+ if (JsArray::is_array_index(key))
23
+ {
24
+ uint32_t idx = static_cast<uint32_t>(std::stoull(key));
25
+ return get_property(idx);
26
+ }
27
+ return Constants::UNDEFINED;
28
+ }
29
+
30
+ inline jspp::AnyValue jspp::JsString::get_property(uint32_t idx)
31
+ {
32
+ if (idx < value.length())
33
+ {
34
+ return AnyValue::make_string(std::string(1, value[idx]));
35
+ }
36
+ return Constants::UNDEFINED;
37
+ }