@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
@@ -1,254 +0,0 @@
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
- }
@@ -1,61 +0,0 @@
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,21 +0,0 @@
1
- #pragma once
2
-
3
- #include "types.hpp"
4
- #include "values/symbol.hpp"
5
- #include "any_value.hpp"
6
- #include "values/prototypes/symbol.hpp"
7
-
8
- inline std::string jspp::JsSymbol::to_std_string() const
9
- {
10
- return "Symbol(" + description + ")";
11
- }
12
-
13
- inline jspp::AnyValue jspp::JsSymbol::get_property(const std::string &key, const AnyValue &thisVal)
14
- {
15
- auto proto_it = SymbolPrototypes::get(key);
16
- if (proto_it.has_value())
17
- {
18
- return AnyValue::resolve_property_for_read(proto_it.value(), thisVal, key);
19
- }
20
- return Constants::UNDEFINED;
21
- }