@ugo-studio/jspp 0.3.0 → 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 (95) 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/control-flow-handlers.js +10 -9
  8. package/dist/core/codegen/declaration-handlers.js +10 -3
  9. package/dist/core/codegen/destructuring-handlers.js +9 -4
  10. package/dist/core/codegen/expression-handlers.js +40 -29
  11. package/dist/core/codegen/function-handlers.js +78 -12
  12. package/dist/core/codegen/helpers.js +91 -14
  13. package/dist/core/codegen/index.js +4 -2
  14. package/dist/core/codegen/statement-handlers.js +8 -6
  15. package/package.json +2 -2
  16. package/scripts/precompile-headers.ts +249 -50
  17. package/scripts/setup-compiler.ts +63 -63
  18. package/src/prelude/any_value.cpp +636 -0
  19. package/src/prelude/any_value.hpp +23 -16
  20. package/src/prelude/{exception_helpers.hpp → exception.cpp} +53 -53
  21. package/src/prelude/exception.hpp +27 -27
  22. package/src/prelude/iterator_instantiations.hpp +10 -0
  23. package/src/prelude/{index.hpp → jspp.hpp} +10 -16
  24. package/src/prelude/library/array.cpp +191 -0
  25. package/src/prelude/library/array.hpp +5 -178
  26. package/src/prelude/library/console.cpp +125 -0
  27. package/src/prelude/library/console.hpp +9 -97
  28. package/src/prelude/library/error.cpp +100 -0
  29. package/src/prelude/library/error.hpp +8 -108
  30. package/src/prelude/library/function.cpp +69 -0
  31. package/src/prelude/library/function.hpp +6 -5
  32. package/src/prelude/library/global.cpp +96 -0
  33. package/src/prelude/library/global.hpp +12 -28
  34. package/src/prelude/library/global_usings.hpp +15 -0
  35. package/src/prelude/library/math.cpp +258 -0
  36. package/src/prelude/library/math.hpp +6 -288
  37. package/src/prelude/library/object.cpp +379 -0
  38. package/src/prelude/library/object.hpp +5 -267
  39. package/src/prelude/library/performance.cpp +21 -0
  40. package/src/prelude/library/performance.hpp +5 -20
  41. package/src/prelude/library/process.cpp +38 -0
  42. package/src/prelude/library/process.hpp +3 -31
  43. package/src/prelude/library/promise.cpp +131 -0
  44. package/src/prelude/library/promise.hpp +5 -116
  45. package/src/prelude/library/symbol.cpp +56 -0
  46. package/src/prelude/library/symbol.hpp +5 -46
  47. package/src/prelude/library/timer.cpp +88 -0
  48. package/src/prelude/library/timer.hpp +11 -87
  49. package/src/prelude/runtime.cpp +19 -0
  50. package/src/prelude/types.hpp +17 -12
  51. package/src/prelude/utils/access.hpp +122 -31
  52. package/src/prelude/utils/assignment_operators.hpp +99 -99
  53. package/src/prelude/utils/log_any_value/array.hpp +61 -40
  54. package/src/prelude/utils/log_any_value/function.hpp +39 -39
  55. package/src/prelude/utils/log_any_value/object.hpp +60 -3
  56. package/src/prelude/utils/operators.hpp +25 -10
  57. package/src/prelude/utils/operators_primitive.hpp +336 -336
  58. package/src/prelude/utils/well_known_symbols.hpp +24 -24
  59. package/src/prelude/values/array.cpp +1399 -0
  60. package/src/prelude/values/array.hpp +4 -0
  61. package/src/prelude/values/async_iterator.cpp +251 -0
  62. package/src/prelude/values/async_iterator.hpp +60 -32
  63. package/src/prelude/values/function.cpp +262 -0
  64. package/src/prelude/values/function.hpp +10 -30
  65. package/src/prelude/values/iterator.cpp +309 -0
  66. package/src/prelude/values/iterator.hpp +33 -64
  67. package/src/prelude/values/number.cpp +176 -0
  68. package/src/prelude/values/object.cpp +159 -0
  69. package/src/prelude/values/object.hpp +4 -0
  70. package/src/prelude/values/promise.cpp +479 -0
  71. package/src/prelude/values/promise.hpp +9 -2
  72. package/src/prelude/values/prototypes/array.hpp +46 -1348
  73. package/src/prelude/values/prototypes/async_iterator.hpp +19 -61
  74. package/src/prelude/values/prototypes/function.hpp +7 -46
  75. package/src/prelude/values/prototypes/iterator.hpp +15 -191
  76. package/src/prelude/values/prototypes/number.hpp +23 -210
  77. package/src/prelude/values/prototypes/object.hpp +7 -23
  78. package/src/prelude/values/prototypes/promise.hpp +8 -186
  79. package/src/prelude/values/prototypes/string.hpp +28 -553
  80. package/src/prelude/values/prototypes/symbol.hpp +9 -70
  81. package/src/prelude/values/shape.hpp +52 -52
  82. package/src/prelude/values/string.cpp +485 -0
  83. package/src/prelude/values/symbol.cpp +89 -0
  84. package/src/prelude/values/symbol.hpp +101 -101
  85. package/src/prelude/any_value_access.hpp +0 -170
  86. package/src/prelude/any_value_defines.hpp +0 -190
  87. package/src/prelude/any_value_helpers.hpp +0 -374
  88. package/src/prelude/values/helpers/array.hpp +0 -199
  89. package/src/prelude/values/helpers/async_iterator.hpp +0 -275
  90. package/src/prelude/values/helpers/function.hpp +0 -109
  91. package/src/prelude/values/helpers/iterator.hpp +0 -145
  92. package/src/prelude/values/helpers/object.hpp +0 -104
  93. package/src/prelude/values/helpers/promise.hpp +0 -254
  94. package/src/prelude/values/helpers/string.hpp +0 -37
  95. 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,37 +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::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
- }
@@ -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
- }