@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,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
- }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes