@ugo-studio/jspp 0.2.8 → 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.
- package/dist/analysis/typeAnalyzer.js +42 -27
- package/dist/core/codegen/class-handlers.js +6 -6
- package/dist/core/codegen/control-flow-handlers.js +4 -4
- package/dist/core/codegen/declaration-handlers.js +21 -3
- package/dist/core/codegen/destructuring-handlers.js +187 -0
- package/dist/core/codegen/expression-handlers.js +7 -0
- package/dist/core/codegen/function-handlers.js +58 -36
- package/dist/core/codegen/helpers.js +288 -52
- package/dist/core/codegen/index.js +7 -4
- package/dist/core/codegen/statement-handlers.js +43 -23
- package/package.json +1 -1
- package/scripts/precompile-headers.ts +13 -5
- package/src/prelude/any_value.hpp +362 -361
- package/src/prelude/any_value_access.hpp +170 -170
- package/src/prelude/any_value_defines.hpp +189 -189
- package/src/prelude/any_value_helpers.hpp +374 -365
- package/src/prelude/library/array.hpp +185 -185
- package/src/prelude/library/console.hpp +111 -111
- package/src/prelude/library/error.hpp +112 -112
- package/src/prelude/library/function.hpp +10 -10
- package/src/prelude/library/math.hpp +307 -307
- package/src/prelude/library/object.hpp +275 -275
- package/src/prelude/library/performance.hpp +1 -1
- package/src/prelude/library/process.hpp +39 -39
- package/src/prelude/library/promise.hpp +123 -123
- package/src/prelude/library/symbol.hpp +52 -52
- package/src/prelude/library/timer.hpp +91 -91
- package/src/prelude/types.hpp +178 -178
- package/src/prelude/utils/access.hpp +411 -393
- package/src/prelude/utils/operators.hpp +336 -329
- package/src/prelude/values/array.hpp +0 -1
- package/src/prelude/values/async_iterator.hpp +83 -81
- package/src/prelude/values/function.hpp +82 -82
- package/src/prelude/values/helpers/array.hpp +198 -208
- package/src/prelude/values/helpers/async_iterator.hpp +275 -271
- package/src/prelude/values/helpers/function.hpp +108 -108
- package/src/prelude/values/helpers/iterator.hpp +144 -107
- package/src/prelude/values/helpers/promise.hpp +253 -253
- package/src/prelude/values/helpers/string.hpp +37 -47
- package/src/prelude/values/iterator.hpp +32 -5
- package/src/prelude/values/promise.hpp +72 -72
- package/src/prelude/values/prototypes/array.hpp +54 -42
- package/src/prelude/values/prototypes/iterator.hpp +201 -74
- package/src/prelude/values/prototypes/promise.hpp +196 -196
- package/src/prelude/values/prototypes/string.hpp +564 -542
- package/src/prelude/values/string.hpp +25 -26
|
@@ -1,196 +1,196 @@
|
|
|
1
|
-
#pragma once
|
|
2
|
-
|
|
3
|
-
#include "types.hpp"
|
|
4
|
-
#include "values/promise.hpp"
|
|
5
|
-
#include "any_value.hpp"
|
|
6
|
-
|
|
7
|
-
namespace jspp
|
|
8
|
-
{
|
|
9
|
-
namespace PromisePrototypes
|
|
10
|
-
{
|
|
11
|
-
inline AnyValue &get_then_fn()
|
|
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;
|
|
18
|
-
|
|
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
|
-
};
|
|
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
|
-
};
|
|
127
|
-
|
|
128
|
-
self->then(resolveHandler, rejectHandler);
|
|
129
|
-
return newPromiseVal; },
|
|
130
|
-
"then");
|
|
131
|
-
return fn;
|
|
132
|
-
}
|
|
133
|
-
|
|
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
|
-
}
|
|
144
|
-
|
|
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();
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
if (key == "catch")
|
|
184
|
-
{
|
|
185
|
-
return get_catch_fn();
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
if (key == "finally")
|
|
189
|
-
{
|
|
190
|
-
return get_finally_fn();
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
return std::nullopt;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
}
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "types.hpp"
|
|
4
|
+
#include "values/promise.hpp"
|
|
5
|
+
#include "any_value.hpp"
|
|
6
|
+
|
|
7
|
+
namespace jspp
|
|
8
|
+
{
|
|
9
|
+
namespace PromisePrototypes
|
|
10
|
+
{
|
|
11
|
+
inline AnyValue &get_then_fn()
|
|
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;
|
|
18
|
+
|
|
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
|
+
};
|
|
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
|
+
};
|
|
127
|
+
|
|
128
|
+
self->then(resolveHandler, rejectHandler);
|
|
129
|
+
return newPromiseVal; },
|
|
130
|
+
"then");
|
|
131
|
+
return fn;
|
|
132
|
+
}
|
|
133
|
+
|
|
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
|
+
}
|
|
144
|
+
|
|
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();
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (key == "catch")
|
|
184
|
+
{
|
|
185
|
+
return get_catch_fn();
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (key == "finally")
|
|
189
|
+
{
|
|
190
|
+
return get_finally_fn();
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return std::nullopt;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|