@ugo-studio/jspp 0.2.6 → 0.2.8

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 (43) hide show
  1. package/dist/analysis/typeAnalyzer.js +56 -24
  2. package/dist/ast/symbols.js +28 -19
  3. package/dist/cli/index.js +1 -1
  4. package/dist/core/codegen/class-handlers.js +8 -8
  5. package/dist/core/codegen/control-flow-handlers.js +17 -7
  6. package/dist/core/codegen/declaration-handlers.js +21 -9
  7. package/dist/core/codegen/expression-handlers.js +558 -126
  8. package/dist/core/codegen/function-handlers.js +101 -108
  9. package/dist/core/codegen/helpers.js +28 -7
  10. package/dist/core/codegen/index.js +6 -4
  11. package/dist/core/codegen/literal-handlers.js +4 -2
  12. package/dist/core/codegen/statement-handlers.js +39 -19
  13. package/package.json +1 -1
  14. package/scripts/precompile-headers.ts +8 -1
  15. package/src/prelude/any_value.hpp +89 -59
  16. package/src/prelude/any_value_access.hpp +1 -1
  17. package/src/prelude/any_value_helpers.hpp +85 -43
  18. package/src/prelude/index.hpp +1 -0
  19. package/src/prelude/library/array.hpp +3 -2
  20. package/src/prelude/types.hpp +8 -8
  21. package/src/prelude/utils/access.hpp +62 -6
  22. package/src/prelude/utils/assignment_operators.hpp +14 -14
  23. package/src/prelude/utils/log_any_value/array.hpp +0 -15
  24. package/src/prelude/utils/log_any_value/primitives.hpp +2 -0
  25. package/src/prelude/utils/operators.hpp +117 -474
  26. package/src/prelude/utils/operators_primitive.hpp +337 -0
  27. package/src/prelude/values/helpers/array.hpp +4 -4
  28. package/src/prelude/values/helpers/async_iterator.hpp +2 -2
  29. package/src/prelude/values/helpers/function.hpp +3 -3
  30. package/src/prelude/values/helpers/iterator.hpp +2 -2
  31. package/src/prelude/values/helpers/object.hpp +3 -3
  32. package/src/prelude/values/helpers/promise.hpp +1 -1
  33. package/src/prelude/values/helpers/string.hpp +1 -1
  34. package/src/prelude/values/helpers/symbol.hpp +1 -1
  35. package/src/prelude/values/prototypes/array.hpp +1125 -853
  36. package/src/prelude/values/prototypes/async_iterator.hpp +32 -14
  37. package/src/prelude/values/prototypes/function.hpp +30 -18
  38. package/src/prelude/values/prototypes/iterator.hpp +40 -17
  39. package/src/prelude/values/prototypes/number.hpp +119 -62
  40. package/src/prelude/values/prototypes/object.hpp +10 -4
  41. package/src/prelude/values/prototypes/promise.hpp +167 -109
  42. package/src/prelude/values/prototypes/string.hpp +407 -231
  43. package/src/prelude/values/prototypes/symbol.hpp +45 -23
@@ -8,131 +8,189 @@ namespace jspp
8
8
  {
9
9
  namespace PromisePrototypes
10
10
  {
11
- inline std::optional<AnyValue> get(const std::string &key, JsPromise *self)
11
+ inline AnyValue &get_then_fn()
12
12
  {
13
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
14
+ {
15
+ auto self = thisVal.as_promise();
16
+ AnyValue onFulfilled = (args.size() > 0 && args[0].is_function()) ? args[0] : Constants::UNDEFINED;
17
+ AnyValue onRejected = (args.size() > 1 && args[1].is_function()) ? args[1] : Constants::UNDEFINED;
13
18
 
14
- if (key == "then")
15
- {
16
- return AnyValue::make_function([self](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
17
- {
18
- AnyValue onFulfilled = (args.size() > 0 && args[0].is_function()) ? args[0] : Constants::UNDEFINED;
19
- AnyValue onRejected = (args.size() > 1 && args[1].is_function()) ? args[1] : Constants::UNDEFINED;
20
-
21
- // "then" returns a new Promise
22
- JsPromise newPromise;
23
- AnyValue newPromiseVal = AnyValue::make_promise(newPromise);
24
-
25
- auto newPromiseState = newPromise.state;
26
- auto resolveNew = [newPromiseState](const AnyValue& v) {
27
- // Manual state resolution to avoid HeapObject management issues here
28
- if (newPromiseState->status != PromiseStatus::Pending) return;
29
- newPromiseState->status = PromiseStatus::Fulfilled;
30
- newPromiseState->result = v;
31
- auto callbacks = newPromiseState->onFulfilled;
32
- newPromiseState->onFulfilled.clear();
33
- newPromiseState->onRejected.clear();
34
- for (auto& cb : callbacks) jspp::Scheduler::instance().enqueue([cb, v]() { cb(v); });
35
- };
36
- auto rejectNew = [newPromiseState](const AnyValue& r) {
37
- if (newPromiseState->status != PromiseStatus::Pending) return;
38
- newPromiseState->status = PromiseStatus::Rejected;
39
- newPromiseState->result = r;
40
- auto callbacks = newPromiseState->onRejected;
41
- newPromiseState->onFulfilled.clear();
42
- newPromiseState->onRejected.clear();
43
- for (auto& cb : callbacks) jspp::Scheduler::instance().enqueue([cb, r]() { cb(r); });
44
- };
19
+ // "then" returns a new Promise
20
+ JsPromise newPromise;
21
+ AnyValue newPromiseVal = AnyValue::make_promise(newPromise);
22
+
23
+ auto newPromiseState = newPromise.state;
24
+ auto resolveNew = [newPromiseState](const AnyValue &v)
25
+ {
26
+ // Manual state resolution to avoid HeapObject management issues here
27
+ if (newPromiseState->status != PromiseStatus::Pending)
28
+ return;
29
+ newPromiseState->status = PromiseStatus::Fulfilled;
30
+ newPromiseState->result = v;
31
+ auto callbacks = newPromiseState->onFulfilled;
32
+ newPromiseState->onFulfilled.clear();
33
+ newPromiseState->onRejected.clear();
34
+ for (auto &cb : callbacks)
35
+ jspp::Scheduler::instance().enqueue([cb, v]()
36
+ { cb(v); });
37
+ };
38
+ auto rejectNew = [newPromiseState](const AnyValue &r)
39
+ {
40
+ if (newPromiseState->status != PromiseStatus::Pending)
41
+ return;
42
+ newPromiseState->status = PromiseStatus::Rejected;
43
+ newPromiseState->result = r;
44
+ auto callbacks = newPromiseState->onRejected;
45
+ newPromiseState->onFulfilled.clear();
46
+ newPromiseState->onRejected.clear();
47
+ for (auto &cb : callbacks)
48
+ jspp::Scheduler::instance().enqueue([cb, r]()
49
+ { cb(r); });
50
+ };
51
+
52
+ // Resolve handler
53
+ auto resolveHandler = [resolveNew, rejectNew, onFulfilled](const AnyValue &val) mutable
54
+ {
55
+ if (onFulfilled.is_function())
56
+ {
57
+ try
58
+ {
59
+ const AnyValue cbArgs[] = {val};
60
+ auto res = onFulfilled.call(Constants::UNDEFINED, cbArgs, "onFulfilled");
61
+ if (res.is_promise())
62
+ {
63
+ auto chained = res.as_promise();
64
+ chained->then(
65
+ [resolveNew](const AnyValue &v)
66
+ { resolveNew(v); },
67
+ [rejectNew](const AnyValue &e)
68
+ { rejectNew(e); });
69
+ }
70
+ else
71
+ {
72
+ resolveNew(res);
73
+ }
74
+ }
75
+ catch (const Exception &e)
76
+ {
77
+ rejectNew(e.data);
78
+ }
79
+ catch (...)
80
+ {
81
+ rejectNew(AnyValue::make_string("Unknown error"));
82
+ }
83
+ }
84
+ else
85
+ {
86
+ resolveNew(val);
87
+ }
88
+ };
45
89
 
90
+ // Reject handler
91
+ auto rejectHandler = [resolveNew, rejectNew, onRejected](const AnyValue &reason) mutable
92
+ {
93
+ if (onRejected.is_function())
94
+ {
95
+ try
96
+ {
97
+ const AnyValue cbArgs[] = {reason};
98
+ auto res = onRejected.call(Constants::UNDEFINED, cbArgs, "onRejected");
99
+ if (res.is_promise())
100
+ {
101
+ auto chained = res.as_promise();
102
+ chained->then(
103
+ [resolveNew](const AnyValue &v)
104
+ { resolveNew(v); },
105
+ [rejectNew](const AnyValue &e)
106
+ { rejectNew(e); });
107
+ }
108
+ else
109
+ {
110
+ resolveNew(res);
111
+ }
112
+ }
113
+ catch (const Exception &e)
114
+ {
115
+ rejectNew(e.data);
116
+ }
117
+ catch (...)
118
+ {
119
+ rejectNew(AnyValue::make_string("Unknown error"));
120
+ }
121
+ }
122
+ else
123
+ {
124
+ rejectNew(reason);
125
+ }
126
+ };
46
127
 
47
- // Resolve handler
48
- auto resolveHandler = [resolveNew, rejectNew, onFulfilled](const AnyValue& val) mutable {
49
- if (onFulfilled.is_function()) {
50
- try {
51
- const AnyValue cbArgs[] = {val};
52
- auto res = onFulfilled.call(Constants::UNDEFINED, cbArgs, "onFulfilled");
53
- if (res.is_promise()) {
54
- auto chained = res.as_promise();
55
- chained->then(
56
- [resolveNew](const AnyValue& v) { resolveNew(v); },
57
- [rejectNew](const AnyValue& e) { rejectNew(e); }
58
- );
59
- } else {
60
- resolveNew(res);
61
- }
62
- } catch (const Exception& e) {
63
- rejectNew(e.data);
64
- } catch (...) {
65
- rejectNew(AnyValue::make_string("Unknown error"));
66
- }
67
- } else {
68
- resolveNew(val);
69
- }
70
- };
128
+ self->then(resolveHandler, rejectHandler);
129
+ return newPromiseVal; },
130
+ "then");
131
+ return fn;
132
+ }
71
133
 
72
- // Reject handler
73
- auto rejectHandler = [resolveNew, rejectNew, onRejected](const AnyValue& reason) mutable {
74
- if (onRejected.is_function()) {
75
- try {
76
- const AnyValue cbArgs[] = {reason};
77
- auto res = onRejected.call(Constants::UNDEFINED, cbArgs,"onRejected");
78
- if (res.is_promise()) {
79
- auto chained = res.as_promise();
80
- chained->then(
81
- [resolveNew](const AnyValue& v) { resolveNew(v); },
82
- [rejectNew](const AnyValue& e) { rejectNew(e); }
83
- );
84
- } else {
85
- resolveNew(res);
86
- }
87
- } catch (const Exception& e) {
88
- rejectNew(e.data);
89
- } catch (...) {
90
- rejectNew(AnyValue::make_string("Unknown error"));
91
- }
92
- } else {
93
- rejectNew(reason);
94
- }
95
- };
134
+ inline AnyValue &get_catch_fn()
135
+ {
136
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
137
+ {
138
+ AnyValue onRejected = (args.size() > 0 && args[0].is_function()) ? args[0] : Constants::UNDEFINED;
139
+ const AnyValue thenArgs[] = {Constants::UNDEFINED, onRejected};
140
+ return thisVal.get_own_property("then").call(thisVal, thenArgs, "then"); },
141
+ "catch");
142
+ return fn;
143
+ }
96
144
 
97
- self->then(resolveHandler, rejectHandler);
98
- return newPromiseVal; }, "then");
145
+ inline AnyValue &get_finally_fn()
146
+ {
147
+ static AnyValue fn = AnyValue::make_function([](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
148
+ {
149
+ AnyValue onFinally = (args.size() > 0 && args[0].is_function()) ? args[0] : Constants::UNDEFINED;
150
+
151
+ const AnyValue thenArgs[] = {
152
+ AnyValue::make_function([onFinally](const AnyValue &, std::span<const AnyValue> args) -> AnyValue
153
+ {
154
+ AnyValue val = args.empty() ? Constants::UNDEFINED : args[0];
155
+ if (onFinally.is_function())
156
+ {
157
+ onFinally.call(Constants::UNDEFINED, {}, "onFinally");
158
+ }
159
+ return val; },
160
+ ""),
161
+ AnyValue::make_function([onFinally](const AnyValue &, std::span<const AnyValue> args) -> AnyValue
162
+ {
163
+ AnyValue reason = args.empty() ? Constants::UNDEFINED : args[0];
164
+ if (onFinally.is_function())
165
+ {
166
+ onFinally.call(Constants::UNDEFINED, {}, "onFinally");
167
+ }
168
+ throw Exception(reason); },
169
+ "")};
170
+ return thisVal.get_own_property("then").call(thisVal, thenArgs, "then"); },
171
+ "finally");
172
+ return fn;
173
+ }
174
+
175
+ inline std::optional<AnyValue> get(const std::string &key)
176
+ {
177
+
178
+ if (key == "then")
179
+ {
180
+ return get_then_fn();
99
181
  }
100
182
 
101
183
  if (key == "catch")
102
184
  {
103
- return AnyValue::make_function([self](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
104
- {
105
- AnyValue onRejected = (args.size() > 0 && args[0].is_function()) ? args[0] : Constants::UNDEFINED;
106
- const AnyValue thenArgs[] = {Constants::UNDEFINED, onRejected};
107
- return thisVal.get_own_property("then").call(thisVal, thenArgs,"then"); }, "catch");
185
+ return get_catch_fn();
108
186
  }
109
187
 
110
188
  if (key == "finally")
111
189
  {
112
- return AnyValue::make_function([self](const AnyValue &thisVal, std::span<const AnyValue> args) -> AnyValue
113
- {
114
- AnyValue onFinally = (args.size() > 0 && args[0].is_function()) ? args[0] : Constants::UNDEFINED;
115
-
116
- const AnyValue thenArgs[] = {
117
- AnyValue::make_function([onFinally](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
118
- AnyValue val = args.empty() ? Constants::UNDEFINED : args[0];
119
- if (onFinally.is_function()) {
120
- onFinally.call(Constants::UNDEFINED, {}, "onFinally");
121
- }
122
- return val;
123
- }, ""),
124
- AnyValue::make_function([onFinally](const AnyValue&, std::span<const AnyValue> args) -> AnyValue {
125
- AnyValue reason = args.empty() ? Constants::UNDEFINED : args[0];
126
- if (onFinally.is_function()) {
127
- onFinally.call(Constants::UNDEFINED, {}, "onFinally");
128
- }
129
- throw Exception(reason);
130
- }, "")
131
- };
132
- return thisVal.get_own_property("then").call(thisVal, thenArgs,"then"); }, "finally");
190
+ return get_finally_fn();
133
191
  }
134
192
 
135
193
  return std::nullopt;
136
194
  }
137
195
  }
138
- }
196
+ }