/src/moddable/xs/sources/xsPromise.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2016-2023 Moddable Tech, Inc. |
3 | | * |
4 | | * This file is part of the Moddable SDK Runtime. |
5 | | * |
6 | | * The Moddable SDK Runtime is free software: you can redistribute it and/or modify |
7 | | * it under the terms of the GNU Lesser General Public License as published by |
8 | | * the Free Software Foundation, either version 3 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * The Moddable SDK Runtime is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public License |
17 | | * along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>. |
18 | | * |
19 | | * This file incorporates work covered by the following copyright and |
20 | | * permission notice: |
21 | | * |
22 | | * Copyright (C) 2010-2016 Marvell International Ltd. |
23 | | * Copyright (C) 2002-2010 Kinoma, Inc. |
24 | | * |
25 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
26 | | * you may not use this file except in compliance with the License. |
27 | | * You may obtain a copy of the License at |
28 | | * |
29 | | * http://www.apache.org/licenses/LICENSE-2.0 |
30 | | * |
31 | | * Unless required by applicable law or agreed to in writing, software |
32 | | * distributed under the License is distributed on an "AS IS" BASIS, |
33 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
34 | | * See the License for the specific language governing permissions and |
35 | | * limitations under the License. |
36 | | */ |
37 | | |
38 | | #include "xsAll.h" |
39 | | |
40 | | //#define mxPromisePrint 1 |
41 | | #ifndef mxReportUnhandledRejections |
42 | | #define mxReportUnhandledRejections 0 |
43 | | #endif |
44 | | |
45 | | static void fxAddUnhandledRejection(txMachine* the, txSlot* promise); |
46 | | static void fxCombinePromises(txMachine* the, txInteger which); |
47 | | static txSlot* fxNewCombinePromisesFunction(txMachine* the, txInteger which, txSlot* already, txSlot* object); |
48 | | |
49 | | enum { |
50 | | XS_PROMISE_COMBINE_NONE = 0, |
51 | | XS_PROMISE_COMBINE_FULFILLED = 1, |
52 | | XS_PROMISE_COMBINE_REJECTED = 2, |
53 | | XS_PROMISE_COMBINE_SETTLED = 4, |
54 | | }; |
55 | | |
56 | | void fxBuildPromise(txMachine* the) |
57 | 18.3k | { |
58 | 18.3k | txSlot* slot; |
59 | 18.3k | mxPush(mxObjectPrototype); |
60 | 18.3k | slot = fxLastProperty(the, fxNewObjectInstance(the)); |
61 | 18.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Promise_prototype_catch), 1, mxID(_catch), XS_DONT_ENUM_FLAG); |
62 | 18.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Promise_prototype_finally), 1, mxID(_finally_), XS_DONT_ENUM_FLAG); |
63 | 18.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Promise_prototype_then), 2, mxID(_then), XS_DONT_ENUM_FLAG); |
64 | 18.3k | slot = fxNextStringXProperty(the, slot, "Promise", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); |
65 | 18.3k | mxPromisePrototype = *the->stack; |
66 | 18.3k | slot = fxBuildHostConstructor(the, mxCallback(fx_Promise), 1, mxID(_Promise)); |
67 | 18.3k | mxPromiseConstructor = *the->stack; |
68 | 18.3k | slot = fxLastProperty(the, slot); |
69 | 18.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Promise_all), 1, mxID(_all), XS_DONT_ENUM_FLAG); |
70 | 18.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Promise_allSettled), 1, mxID(_allSettled), XS_DONT_ENUM_FLAG); |
71 | 18.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Promise_any), 1, mxID(_any), XS_DONT_ENUM_FLAG); |
72 | 18.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Promise_race), 1, mxID(_race), XS_DONT_ENUM_FLAG); |
73 | 18.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Promise_reject), 1, mxID(_reject), XS_DONT_ENUM_FLAG); |
74 | 18.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Promise_resolve), 1, mxID(_resolve), XS_DONT_ENUM_FLAG); |
75 | 18.3k | #if mxECMAScript2025 |
76 | 18.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Promise_try), 1, mxID(_try_), XS_DONT_ENUM_FLAG); |
77 | 18.3k | #endif |
78 | 18.3k | #if mxECMAScript2024 |
79 | 18.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Promise_withResolvers), 0, mxID(_withResolvers), XS_DONT_ENUM_FLAG); |
80 | 18.3k | #endif |
81 | 18.3k | slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_species_get), C_NULL, mxID(_Symbol_species), XS_DONT_ENUM_FLAG); |
82 | 18.3k | mxPop(); |
83 | 18.3k | fxNewHostFunction(the, mxCallback(fxOnRejectedPromise), 1, XS_NO_ID, XS_NO_ID); |
84 | 18.3k | mxOnRejectedPromiseFunction = *the->stack; |
85 | 18.3k | mxPop(); |
86 | 18.3k | fxNewHostFunction(the, mxCallback(fxOnResolvedPromise), 1, XS_NO_ID, XS_NO_ID); |
87 | 18.3k | mxOnResolvedPromiseFunction = *the->stack; |
88 | 18.3k | mxPop(); |
89 | 18.3k | fxNewHostFunction(the, mxCallback(fxOnThenable), 1, XS_NO_ID, XS_NO_ID); |
90 | 18.3k | mxOnThenableFunction = *the->stack; |
91 | 18.3k | mxPop(); |
92 | 18.3k | } |
93 | | |
94 | | txSlot* fxNewPromiseInstance(txMachine* the) |
95 | 846k | { |
96 | | #ifdef mxPromisePrint |
97 | | static txID gID = 0; |
98 | | #endif |
99 | 846k | txSlot* promise; |
100 | 846k | txSlot* slot; |
101 | 846k | txSlot* instance; |
102 | 846k | promise = fxNewSlot(the); |
103 | 846k | promise->kind = XS_INSTANCE_KIND; |
104 | 846k | promise->value.instance.garbage = C_NULL; |
105 | 846k | promise->value.instance.prototype = the->stack->value.reference; |
106 | 846k | the->stack->kind = XS_REFERENCE_KIND; |
107 | 846k | the->stack->value.reference = promise; |
108 | | /* STATUS */ |
109 | 846k | slot = promise->next = fxNewSlot(the); |
110 | 846k | slot->flag = XS_INTERNAL_FLAG; |
111 | | #ifdef mxPromisePrint |
112 | | slot->ID = gID++; |
113 | | #endif |
114 | 846k | slot->kind = XS_PROMISE_KIND; |
115 | 846k | slot->value.integer = mxUndefinedStatus; |
116 | | /* THENS */ |
117 | 846k | slot = slot->next = fxNewSlot(the); |
118 | 846k | slot->flag = XS_INTERNAL_FLAG; |
119 | 846k | slot->value.reference = instance = fxNewSlot(the); |
120 | 846k | slot->kind = XS_REFERENCE_KIND; |
121 | 846k | instance->kind = XS_INSTANCE_KIND; |
122 | 846k | instance->value.instance.garbage = C_NULL; |
123 | 846k | instance->value.instance.prototype = C_NULL; |
124 | | /* RESULT */ |
125 | 846k | slot = slot->next = fxNewSlot(the); |
126 | 846k | slot->flag = XS_INTERNAL_FLAG; |
127 | | #if mxReportUnhandledRejections |
128 | | /* ENVIRONMENT */ |
129 | | slot = slot->next = fxNewSlot(the); |
130 | | slot->flag = XS_INTERNAL_FLAG; |
131 | | instance = the->frame; |
132 | | while (instance) { |
133 | | txSlot* environment = mxFrameToEnvironment(instance); |
134 | | if (environment->ID != XS_NO_ID) { |
135 | | slot->ID = environment->ID; |
136 | | slot->value.environment.line = environment->value.environment.line; |
137 | | break; |
138 | | } |
139 | | instance = instance->next; |
140 | | } |
141 | | #endif |
142 | 846k | return promise; |
143 | 846k | } |
144 | | |
145 | | txSlot* fxNewPromiseCapability(txMachine* the, txSlot* resolveFunction, txSlot* rejectFunction) |
146 | 414k | { |
147 | 414k | txSlot* capability; |
148 | 414k | txSlot* slot; |
149 | 414k | txSlot* function; |
150 | 414k | mxNew(); |
151 | 414k | resolveFunction->value.reference = fxNewHostFunction(the, fxNewPromiseCapabilityCallback, 2, XS_NO_ID, mxNewPromiseCapabilityCallbackProfileID); |
152 | 414k | resolveFunction->kind = XS_REFERENCE_KIND; |
153 | 414k | mxRunCount(1); |
154 | 414k | capability = resolveFunction->value.reference; |
155 | 414k | resolveFunction->kind = XS_UNDEFINED_KIND; |
156 | 414k | slot = mxFunctionInstanceHome(capability)->value.home.object; |
157 | 414k | if (!slot) |
158 | 5 | mxTypeError("executor not called"); |
159 | 414k | slot = slot->next; |
160 | 414k | if (!mxIsReference(slot)) |
161 | 12 | mxTypeError("resolve: not an object"); |
162 | 414k | function = slot->value.reference; |
163 | 414k | if (!mxIsFunction(function)) |
164 | 0 | mxTypeError("resolve: not a function"); |
165 | 414k | resolveFunction->kind = XS_REFERENCE_KIND; |
166 | 414k | resolveFunction->value.reference = function; |
167 | 414k | slot = slot->next; |
168 | 414k | if (!mxIsReference(slot)) |
169 | 3 | mxTypeError("reject: not an object"); |
170 | 414k | function = slot->value.reference; |
171 | 414k | if (!mxIsFunction(function)) |
172 | 0 | mxTypeError("reject: not a function"); |
173 | 414k | rejectFunction->kind = XS_REFERENCE_KIND; |
174 | 414k | rejectFunction->value.reference = function; |
175 | 414k | return the->stack->value.reference; |
176 | 414k | } |
177 | | |
178 | | void fxNewPromiseCapabilityCallback(txMachine* the) |
179 | 414k | { |
180 | 414k | txSlot* slot = mxFunctionInstanceHome(mxFunction->value.reference); |
181 | 414k | txSlot* object = slot->value.home.object; |
182 | 414k | txSlot* resolveFunction; |
183 | 414k | txSlot* rejectFunction; |
184 | 414k | if (object) { |
185 | 57 | resolveFunction = object->next; |
186 | 57 | rejectFunction = resolveFunction->next; |
187 | 57 | if (!mxIsUndefined(resolveFunction) || !mxIsUndefined(rejectFunction)) |
188 | 25 | mxTypeError("executor already called"); |
189 | 57 | } |
190 | 414k | else { |
191 | 414k | object = fxNewInstance(the); |
192 | 414k | resolveFunction = object->next = fxNewSlot(the); |
193 | 414k | rejectFunction = resolveFunction->next = fxNewSlot(the); |
194 | 414k | slot->value.home.object = object; |
195 | 414k | mxPop(); |
196 | 414k | } |
197 | 414k | if (mxArgc > 0) { |
198 | 414k | resolveFunction->kind = mxArgv(0)->kind; |
199 | 414k | resolveFunction->value = mxArgv(0)->value; |
200 | 414k | } |
201 | 414k | if (mxArgc > 1) { |
202 | 414k | rejectFunction->kind = mxArgv(1)->kind; |
203 | 414k | rejectFunction->value = mxArgv(1)->value; |
204 | 414k | } |
205 | 414k | } |
206 | | |
207 | | void fxAddUnhandledRejection(txMachine* the, txSlot* promise) |
208 | 82.4k | { |
209 | 82.4k | txSlot* reason = mxPromiseResult(promise); |
210 | 82.4k | txSlot* list = &mxUnhandledPromises; |
211 | 82.4k | txSlot** address = &list->value.reference->next; |
212 | 82.4k | txSlot* slot; |
213 | 89.3M | while ((slot = *address)) { |
214 | 89.2M | if (slot->value.weakRef.target == promise) |
215 | 0 | break; |
216 | 89.2M | mxMeterSome(1); |
217 | 89.2M | slot = slot->next; |
218 | 89.2M | address = &slot->next; |
219 | 89.2M | } |
220 | 82.4k | if (!slot) { |
221 | | #ifdef mxPromisePrint |
222 | | fprintf(stderr, "fxAddUnhandledRejection %d\n", promise->next->ID); |
223 | | #endif |
224 | 82.4k | slot = *address = fxNewSlot(the); |
225 | 82.4k | slot->kind = XS_WEAK_REF_KIND; |
226 | 82.4k | slot->value.weakRef.target = promise; |
227 | 82.4k | slot = slot->next = fxNewSlot(the); |
228 | 82.4k | slot->kind = reason->kind; |
229 | 82.4k | slot->value = reason->value; |
230 | 82.4k | } |
231 | 82.4k | } |
232 | | |
233 | | void fxCheckUnhandledRejections(txMachine* the, txBoolean atExit) |
234 | 189k | { |
235 | 189k | txSlot* list = &mxUnhandledPromises; |
236 | 189k | txSlot** address = &list->value.reference->next; |
237 | 189k | txSlot* slot; |
238 | 189k | if (atExit) { |
239 | 0 | txIndex count = 0; |
240 | 0 | while ((slot = *address)) { |
241 | | #if mxReportUnhandledRejections |
242 | | if (slot->value.weakRef.target == C_NULL) { |
243 | | fprintf(stderr, "# garbage collected promise\n"); |
244 | | } |
245 | | else { |
246 | | txSlot* property = mxPromiseEnvironment(slot->value.weakRef.target); |
247 | | if (property && property->ID) { |
248 | | fprintf(stderr, "%s:%d\n", fxGetKeyName(the, property->ID), property->value.environment.line); |
249 | | } |
250 | | } |
251 | | #endif |
252 | 0 | slot = slot->next; |
253 | 0 | *address = slot->next; |
254 | 0 | mxException.value = slot->value; |
255 | 0 | mxException.kind = slot->kind; |
256 | 0 | count++; |
257 | 0 | } |
258 | 0 | if (count > 0) |
259 | 0 | fxAbort(the, XS_UNHANDLED_REJECTION_EXIT); |
260 | 0 | } |
261 | 189k | else { |
262 | 326k | while ((slot = *address)) { |
263 | 136k | if (slot->value.weakRef.target == C_NULL) { |
264 | 12 | slot = slot->next; |
265 | 12 | *address = slot->next; |
266 | 12 | mxException.value = slot->value; |
267 | 12 | mxException.kind = slot->kind; |
268 | 12 | fxAbort(the, XS_UNHANDLED_REJECTION_EXIT); |
269 | 12 | } |
270 | 136k | else { |
271 | 136k | slot = slot->next; |
272 | 136k | address = &slot->next; |
273 | 136k | } |
274 | 136k | } |
275 | 189k | } |
276 | 189k | } |
277 | | |
278 | | void fxCombinePromises(txMachine* the, txInteger which) |
279 | 2.49k | { |
280 | 2.49k | txSlot* stack = the->stack; |
281 | 2.49k | txSlot* resolveFunction; |
282 | 2.49k | txSlot* rejectFunction; |
283 | 2.49k | txSlot* promise; |
284 | 2.49k | txSlot* object; |
285 | 2.49k | txSlot* property; |
286 | 2.49k | txSlot* array; |
287 | 2.49k | txSlot* already; |
288 | 2.49k | txSlot* iterator; |
289 | 2.49k | txSlot* next; |
290 | 2.49k | txSlot* value; |
291 | 2.49k | txInteger index; |
292 | | |
293 | 2.49k | if (!mxIsReference(mxThis)) |
294 | 33 | mxTypeError("this: not an object"); |
295 | 2.46k | mxTemporary(resolveFunction); |
296 | 2.46k | mxTemporary(rejectFunction); |
297 | 2.46k | mxPushSlot(mxThis); |
298 | 2.46k | promise = fxNewPromiseCapability(the, resolveFunction, rejectFunction); |
299 | 2.46k | mxPullSlot(mxResult); |
300 | 2.46k | { |
301 | 2.46k | mxTry(the) { |
302 | 2.43k | txSlot* resolve = C_NULL; |
303 | 2.43k | if (which) { |
304 | 2.40k | object = fxNewInstance(the); |
305 | 2.40k | property = fxNextIntegerProperty(the, object, 0, XS_NO_ID, XS_NO_FLAG); |
306 | 2.40k | property = fxNextReferenceProperty(the, property, promise, XS_NO_ID, XS_NO_FLAG); |
307 | 2.40k | if (which == XS_PROMISE_COMBINE_REJECTED) |
308 | 308 | property = fxNextSlotProperty(the, property, rejectFunction, XS_NO_ID, XS_NO_FLAG); |
309 | 2.09k | else |
310 | 2.09k | property = fxNextSlotProperty(the, property, resolveFunction, XS_NO_ID, XS_NO_FLAG); |
311 | 2.40k | mxPush(mxArrayPrototype); |
312 | 2.40k | array = fxNewArrayInstance(the); |
313 | 2.40k | already = array->next; |
314 | 2.40k | property = fxNextReferenceProperty(the, property, array, XS_NO_ID, XS_NO_FLAG); |
315 | 2.40k | mxPop(); |
316 | 2.40k | } |
317 | 2.43k | mxPushSlot(mxThis); |
318 | 2.43k | mxGetID(mxID(_resolve)); |
319 | 2.43k | resolve = the->stack; |
320 | 2.43k | if (!fxIsCallable(the, resolve)) |
321 | 7 | mxTypeError("resolve: not a function"); |
322 | 2.42k | mxTemporary(iterator); |
323 | 2.42k | mxTemporary(next); |
324 | 2.42k | fxGetIterator(the, mxArgv(0), iterator, next, 0); |
325 | 2.42k | index = 0; |
326 | 2.42k | mxTemporary(value); |
327 | 74.0k | while (fxIteratorNext(the, iterator, next, value)) { |
328 | 71.6k | mxTry(the) { |
329 | 71.6k | mxPushSlot(mxThis); |
330 | 71.6k | mxPushSlot(resolve); |
331 | 71.6k | mxCall(); |
332 | 71.6k | mxPushSlot(value); |
333 | 71.6k | mxRunCount(1); |
334 | 71.6k | mxDub(); |
335 | 71.6k | mxGetID(mxID(_then)); |
336 | 71.6k | mxCall(); |
337 | 71.6k | if (which) { |
338 | 71.6k | already = already->next = fxNewSlot(the); |
339 | 71.6k | already->kind = XS_UNINITIALIZED_KIND; |
340 | 71.6k | array->next->value.array.length++; |
341 | 71.6k | } |
342 | 71.6k | if (which & XS_PROMISE_COMBINE_SETTLED) { |
343 | 36 | fxNewCombinePromisesFunction(the, which | XS_PROMISE_COMBINE_FULFILLED, already, object); |
344 | 36 | fxNewCombinePromisesFunction(the, which | XS_PROMISE_COMBINE_REJECTED, already, object); |
345 | 36 | } |
346 | 71.6k | else if (which & XS_PROMISE_COMBINE_FULFILLED) { |
347 | 71.3k | fxNewCombinePromisesFunction(the, which, already, object); |
348 | 71.3k | mxPushSlot(rejectFunction); |
349 | 71.3k | } |
350 | 322 | else if (which & XS_PROMISE_COMBINE_REJECTED) { |
351 | 273 | mxPushSlot(resolveFunction); |
352 | 273 | fxNewCombinePromisesFunction(the, which, already, object); |
353 | 273 | } |
354 | 49 | else { |
355 | 49 | mxPushSlot(resolveFunction); |
356 | 49 | mxPushSlot(rejectFunction); |
357 | 49 | } |
358 | 71.6k | mxRunCount(2); |
359 | 71.6k | mxPop(); |
360 | 71.6k | index++; |
361 | 71.6k | } |
362 | 71.6k | mxCatch(the) { |
363 | 23 | fxIteratorReturn(the, iterator, 1); |
364 | 23 | fxJump(the); |
365 | 23 | } |
366 | 71.6k | } |
367 | 2.40k | if (which) { |
368 | 1.94k | property = object->next; |
369 | 1.94k | property->value.integer += index; |
370 | 1.94k | index = property->value.integer; |
371 | 1.94k | } |
372 | 2.40k | if ((index == 0) && (which != XS_PROMISE_COMBINE_NONE)) { |
373 | 223 | mxPushUndefined(); |
374 | 223 | if (which == XS_PROMISE_COMBINE_REJECTED) |
375 | 49 | mxPushSlot(rejectFunction); |
376 | 174 | else |
377 | 174 | mxPushSlot(resolveFunction); |
378 | 223 | mxCall(); |
379 | 223 | if ((which == XS_PROMISE_COMBINE_SETTLED) || (which == XS_PROMISE_COMBINE_FULFILLED)) { |
380 | 174 | fxCacheArray(the, array); |
381 | 174 | mxPushReference(array); |
382 | 174 | } |
383 | 49 | else if (which == XS_PROMISE_COMBINE_REJECTED) { |
384 | 49 | mxPush(mxAggregateErrorConstructor); |
385 | 49 | mxNew(); |
386 | 49 | fxCacheArray(the, array); |
387 | 49 | mxPushReference(array); |
388 | 49 | mxRunCount(1); |
389 | 49 | } |
390 | 0 | else { |
391 | 0 | mxPushUndefined(); |
392 | 0 | } |
393 | 223 | mxRunCount(1); |
394 | 223 | } |
395 | 2.40k | } |
396 | 2.40k | mxCatch(the) { |
397 | 470 | fxRejectException(the, rejectFunction); |
398 | 470 | } |
399 | 2.46k | } |
400 | 2.43k | the->stack = stack; |
401 | 2.43k | } |
402 | | |
403 | | void fxCombinePromisesCallback(txMachine* the) |
404 | 71.1k | { |
405 | 71.1k | txSlot* slot = mxFunctionInstanceHome(mxFunction->value.reference)->value.home.object->next; |
406 | 71.1k | txInteger which = slot->value.integer; |
407 | 71.1k | txSlot* instance; |
408 | 71.1k | txSlot* property; |
409 | 71.1k | slot = slot->next; |
410 | 71.1k | if (slot->value.closure->kind != XS_UNINITIALIZED_KIND) |
411 | 3 | return; |
412 | 71.1k | if (which & XS_PROMISE_COMBINE_SETTLED) { |
413 | 21 | mxPush(mxObjectPrototype); |
414 | 21 | instance = fxNewObjectInstance(the); |
415 | 21 | } |
416 | 71.1k | if (mxArgc > 0) |
417 | 71.1k | mxPushSlot(mxArgv(0)); |
418 | 0 | else |
419 | 0 | mxPushUndefined(); |
420 | 71.1k | if (which & XS_PROMISE_COMBINE_SETTLED) { |
421 | 21 | property = fxLastProperty(the, instance); |
422 | 21 | if (which & XS_PROMISE_COMBINE_FULFILLED) { |
423 | 10 | property = fxNextStringXProperty(the, property, "fulfilled", mxID(_status), XS_NO_FLAG); |
424 | 10 | property = fxNextSlotProperty(the, property, the->stack, mxID(_value), XS_NO_FLAG); |
425 | 10 | } |
426 | 11 | else { |
427 | 11 | property = fxNextStringXProperty(the, property, "rejected", mxID(_status), XS_NO_FLAG); |
428 | 11 | property = fxNextSlotProperty(the, property, the->stack, mxID(_reason), XS_NO_FLAG); |
429 | 11 | } |
430 | 21 | mxPop(); |
431 | 21 | } |
432 | 71.1k | mxPullSlot(slot->value.closure); |
433 | 71.1k | slot = slot->next->value.reference->next; |
434 | 71.1k | slot->value.integer--; |
435 | 71.1k | if (slot->value.integer == 0) { |
436 | | /* THIS */ |
437 | 1.41k | slot = slot->next; |
438 | 1.41k | mxPushSlot(slot); |
439 | | /* FUNCTION */ |
440 | 1.41k | slot = slot->next; |
441 | 1.41k | mxPushSlot(slot); |
442 | 1.41k | mxCall(); |
443 | | /* ARGUMENTS */ |
444 | 1.41k | slot = slot->next; |
445 | 1.41k | if (which == XS_PROMISE_COMBINE_REJECTED) { |
446 | 3 | mxPush(mxAggregateErrorConstructor); |
447 | 3 | mxNew(); |
448 | 3 | } |
449 | 1.41k | fxCacheArray(the, slot->value.reference); |
450 | 1.41k | mxPushSlot(slot); |
451 | 1.41k | if (which == XS_PROMISE_COMBINE_REJECTED) { |
452 | 3 | mxRunCount(1); |
453 | 3 | } |
454 | | /* COUNT */ |
455 | 1.41k | mxRunCount(1); |
456 | 1.41k | mxPullSlot(mxResult); |
457 | 1.41k | } |
458 | 71.1k | } |
459 | | |
460 | | txSlot* fxNewCombinePromisesFunction(txMachine* the, txInteger which, txSlot* already, txSlot* object) |
461 | 71.6k | { |
462 | 71.6k | txSlot* result; |
463 | 71.6k | txSlot* instance; |
464 | 71.6k | txSlot* property; |
465 | 71.6k | result = fxNewHostFunction(the, fxCombinePromisesCallback, 1, XS_NO_ID, mxCombinePromisesCallbackProfileID); |
466 | 71.6k | instance = fxNewInstance(the); |
467 | 71.6k | property = fxNextIntegerProperty(the, instance, which, XS_NO_ID, XS_NO_FLAG); |
468 | 71.6k | property = property->next = fxNewSlot(the); |
469 | 71.6k | property->kind = XS_CLOSURE_KIND; |
470 | 71.6k | property->value.closure = already; |
471 | 71.6k | property = fxNextReferenceProperty(the, property, object, XS_NO_ID, XS_NO_FLAG); |
472 | 71.6k | property = mxFunctionInstanceHome(result); |
473 | 71.6k | property->value.home.object = instance; |
474 | 71.6k | mxPop(); |
475 | 71.6k | return result; |
476 | 71.6k | } |
477 | | |
478 | | void fxOnRejectedPromise(txMachine* the) |
479 | 1.93k | { |
480 | 1.93k | txSlot* reaction = mxThis->value.reference; |
481 | 1.93k | txSlot* resolveFunction = reaction->next; |
482 | 1.93k | txSlot* rejectFunction = resolveFunction->next; |
483 | 1.93k | txSlot* resolveHandler = rejectFunction->next; |
484 | 1.93k | txSlot* rejectHandler = resolveHandler->next; |
485 | 1.93k | txSlot* argument = mxArgv(0); |
486 | 1.93k | txSlot* function = rejectFunction; |
487 | 1.93k | if (rejectHandler->kind == XS_REFERENCE_KIND) { |
488 | 1.91k | mxTry(the) { |
489 | | /* THIS */ |
490 | 1.91k | mxPushUndefined(); |
491 | | /* FUNCTION */ |
492 | 1.91k | mxPushSlot(rejectHandler); |
493 | 1.91k | mxCall(); |
494 | | /* ARGUMENTS */ |
495 | 1.91k | mxPushSlot(argument); |
496 | 1.91k | mxRunCount(1); |
497 | 1.91k | mxPullSlot(argument); |
498 | 1.91k | function = resolveFunction; |
499 | 1.91k | } |
500 | 1.91k | mxCatch(the) { |
501 | 15 | *argument = mxException; |
502 | 15 | mxException = mxUndefined; |
503 | 15 | function = rejectFunction; |
504 | 15 | } |
505 | 1.91k | } |
506 | 1.93k | if (function->kind == XS_REFERENCE_KIND) { |
507 | | /* THIS */ |
508 | 1.91k | mxPushUndefined(); |
509 | | /* FUNCTION */ |
510 | 1.91k | mxPushSlot(function); |
511 | 1.91k | mxCall(); |
512 | | /* ARGUMENTS */ |
513 | 1.91k | mxPushSlot(argument); |
514 | 1.91k | mxRunCount(1); |
515 | 1.91k | mxPop(); |
516 | 1.91k | } |
517 | 1.93k | } |
518 | | |
519 | | void fxOnResolvedPromise(txMachine* the) |
520 | 225k | { |
521 | 225k | txSlot* reaction = mxThis->value.reference; |
522 | 225k | txSlot* resolveFunction = reaction->next; |
523 | 225k | txSlot* rejectFunction = resolveFunction->next; |
524 | 225k | txSlot* resolveHandler = rejectFunction->next; |
525 | 225k | txSlot* argument = mxArgv(0); |
526 | 225k | txSlot* function = resolveFunction; |
527 | 225k | if (resolveHandler->kind == XS_REFERENCE_KIND) { |
528 | 225k | mxTry(the) { |
529 | | /* THIS */ |
530 | 225k | mxPushUndefined(); |
531 | | /* FUNCTION */ |
532 | 225k | mxPushSlot(resolveHandler); |
533 | 225k | mxCall(); |
534 | | /* ARGUMENTS */ |
535 | 225k | mxPushSlot(argument); |
536 | 225k | mxRunCount(1); |
537 | 225k | mxPullSlot(argument); |
538 | 225k | } |
539 | 225k | mxCatch(the) { |
540 | 36 | *argument = mxException; |
541 | 36 | mxException = mxUndefined; |
542 | 36 | function = rejectFunction; |
543 | 36 | } |
544 | 225k | } |
545 | 225k | if (function->kind == XS_REFERENCE_KIND) { |
546 | | /* THIS */ |
547 | 104k | mxPushUndefined(); |
548 | | /* FUNCTION */ |
549 | 104k | mxPushSlot(function); |
550 | 104k | mxCall(); |
551 | | /* ARGUMENTS */ |
552 | 104k | mxPushSlot(argument); |
553 | 104k | mxRunCount(1); |
554 | 104k | mxPop(); |
555 | 104k | } |
556 | 225k | } |
557 | | |
558 | | void fxOnThenable(txMachine* the) |
559 | 35.3k | { |
560 | 35.3k | txSlot* resolveFunction = mxArgv(0); |
561 | 35.3k | txSlot* rejectFunction = mxArgv(1); |
562 | 35.3k | txSlot* thenFunction = mxArgv(2); |
563 | 35.3k | mxTry(the) { |
564 | | /* THIS */ |
565 | 35.3k | mxPushSlot(mxThis); |
566 | | /* FUNCTION */ |
567 | 35.3k | mxPushSlot(thenFunction); |
568 | 35.3k | mxCall(); |
569 | | /* ARGUMENTS */ |
570 | 35.3k | mxPushSlot(resolveFunction); |
571 | 35.3k | mxPushSlot(rejectFunction); |
572 | 35.3k | mxRunCount(2); |
573 | 35.3k | mxPop(); |
574 | 35.3k | } |
575 | 35.3k | mxCatch(the) { |
576 | 10 | fxRejectException(the, rejectFunction); |
577 | 10 | } |
578 | 35.3k | } |
579 | | |
580 | | void fxPromiseThen(txMachine* the, txSlot* promise, txSlot* onFullfilled, txSlot* onRejected, txSlot* resolveFunction, txSlot* rejectFunction) |
581 | 315k | { |
582 | 315k | txSlot* reaction; |
583 | 315k | txSlot* slot; |
584 | 315k | txSlot* status; |
585 | | |
586 | 315k | reaction = fxNewInstance(the); |
587 | 315k | slot = reaction->next = fxNewSlot(the); |
588 | 315k | slot->flag = XS_INTERNAL_FLAG; |
589 | 315k | if (resolveFunction) { |
590 | 194k | slot->kind = resolveFunction->kind; |
591 | 194k | slot->value = resolveFunction->value; |
592 | 194k | } |
593 | 315k | slot = slot->next = fxNewSlot(the); |
594 | 315k | slot->flag = XS_INTERNAL_FLAG; |
595 | 315k | if (rejectFunction) { |
596 | 194k | slot->kind = rejectFunction->kind; |
597 | 194k | slot->value = rejectFunction->value; |
598 | 194k | } |
599 | 315k | slot = slot->next = fxNewSlot(the); |
600 | 315k | slot->ID = mxID(__onFullfilled_); |
601 | 315k | if (onFullfilled) { |
602 | 315k | slot->kind = onFullfilled->kind; |
603 | 315k | slot->value = onFullfilled->value; |
604 | 315k | } |
605 | 315k | slot = slot->next = fxNewSlot(the); |
606 | 315k | slot->ID = mxID(__onRejected_); |
607 | 315k | if (onRejected) { |
608 | 315k | slot->kind = onRejected->kind; |
609 | 315k | slot->value = onRejected->value; |
610 | 315k | } |
611 | 315k | if (resolveFunction) { |
612 | 194k | slot = slot->next = fxNewSlot(the); |
613 | 194k | slot->ID = mxID(__result_); |
614 | 194k | slot->kind = mxResult->kind; |
615 | 194k | slot->value = mxResult->value; |
616 | 194k | } |
617 | | |
618 | 315k | status = mxPromiseStatus(promise); |
619 | 315k | if (status->value.integer == mxPendingStatus) { |
620 | 208k | txSlot** address = &(mxPromiseThens(promise)->value.reference->next); |
621 | 208k | while ((slot = *address)) |
622 | 3 | address = &(slot->next); |
623 | 208k | slot = *address = fxNewSlot(the); |
624 | 208k | slot->kind = XS_REFERENCE_KIND; |
625 | 208k | slot->value.reference = reaction; |
626 | 208k | } |
627 | 107k | else { |
628 | 107k | mxPushReference(reaction); |
629 | 107k | if (status->value.integer == mxFulfilledStatus) |
630 | 105k | mxPush(mxOnResolvedPromiseFunction); |
631 | 2.03k | else |
632 | 2.03k | mxPush(mxOnRejectedPromiseFunction); |
633 | 107k | mxCall(); |
634 | 107k | slot = mxPromiseResult(promise); |
635 | 107k | mxPushSlot(slot); |
636 | 107k | fxQueueJob(the, 1, promise); |
637 | 107k | } |
638 | 315k | mxPop(); // reaction |
639 | 315k | } |
640 | | |
641 | | void fxPushPromiseFunctions(txMachine* the, txSlot* promise) |
642 | 896k | { |
643 | 896k | txSlot* resolve; |
644 | 896k | txSlot* reject; |
645 | 896k | txSlot* object; |
646 | 896k | txSlot* slot; |
647 | 896k | resolve = fxNewHostFunction(the, fxResolvePromise, 1, XS_NO_ID, mxResolvePromiseProfileID); |
648 | 896k | reject = fxNewHostFunction(the, fxRejectPromise, 1, XS_NO_ID, mxRejectPromiseProfileID); |
649 | 896k | slot = object = fxNewInstance(the); |
650 | 896k | slot = object->next = fxNewSlot(the); |
651 | 896k | slot->kind = XS_BOOLEAN_KIND; |
652 | 896k | slot->value.boolean = 0; |
653 | 896k | slot = slot->next = fxNewSlot(the); |
654 | 896k | slot->kind = XS_REFERENCE_KIND; |
655 | 896k | slot->value.reference = promise; |
656 | 896k | slot = mxFunctionInstanceHome(resolve); |
657 | 896k | slot->value.home.object = object; |
658 | 896k | slot = mxFunctionInstanceHome(reject); |
659 | 896k | slot->value.home.object = object; |
660 | 896k | mxPop(); |
661 | 896k | } |
662 | | |
663 | | void fxRejectException(txMachine* the, txSlot* rejectFunction) |
664 | 63.1k | { |
665 | | /* THIS */ |
666 | 63.1k | mxPushUndefined(); |
667 | | /* FUNCTION */ |
668 | 63.1k | mxPushSlot(rejectFunction); |
669 | 63.1k | mxCall(); |
670 | | /* ARGUMENTS */ |
671 | 63.1k | mxPush(mxException); |
672 | 63.1k | mxException = mxUndefined; |
673 | 63.1k | mxRunCount(1); |
674 | 63.1k | mxPop(); |
675 | 63.1k | } |
676 | | |
677 | | void fxRejectPromise(txMachine* the) |
678 | 82.5k | { |
679 | 82.5k | txSlot* slot; |
680 | 82.5k | txSlot* promise; |
681 | 82.5k | txSlot* argument; |
682 | 82.5k | txSlot* result; |
683 | 82.5k | slot = mxFunctionInstanceHome(mxFunction->value.reference)->value.home.object->next; |
684 | 82.5k | if (slot->value.boolean) |
685 | 13 | return; |
686 | 82.5k | slot->value.boolean = 1; |
687 | 82.5k | mxPushSlot(slot->next); |
688 | 82.5k | promise = the->stack->value.reference; |
689 | 82.5k | slot->next = C_NULL; |
690 | 82.5k | if (mxArgc > 0) |
691 | 82.5k | mxPushSlot(mxArgv(0)); |
692 | 5 | else |
693 | 5 | mxPushUndefined(); |
694 | 82.5k | argument = the->stack; |
695 | | #ifdef mxPromisePrint |
696 | | fprintf(stderr, "fxRejectPromise %d\n", promise->next->ID); |
697 | | #endif |
698 | 82.5k | result = mxPromiseResult(promise); |
699 | 82.5k | result->kind = argument->kind; |
700 | 82.5k | result->value = argument->value; |
701 | 82.5k | slot = mxPromiseThens(promise)->value.reference->next; |
702 | 82.5k | if (slot) { |
703 | 98 | while (slot) { |
704 | 50 | mxPushReference(slot->value.reference); |
705 | 50 | mxPush(mxOnRejectedPromiseFunction); |
706 | 50 | mxCall(); |
707 | 50 | mxPushSlot(argument); |
708 | 50 | fxQueueJob(the, 1, promise); |
709 | 50 | slot = slot->next; |
710 | 50 | } |
711 | 48 | mxPromiseThens(promise)->value.reference->next = C_NULL; |
712 | 48 | } |
713 | 82.4k | else { |
714 | 82.4k | fxAddUnhandledRejection(the, promise); |
715 | 82.4k | } |
716 | 82.5k | slot = mxPromiseStatus(promise); |
717 | 82.5k | slot->value.integer = mxRejectedStatus; |
718 | 82.5k | } |
719 | | |
720 | | void fxResolvePromise(txMachine* the) |
721 | 579k | { |
722 | 579k | txSlot* slot; |
723 | 579k | txSlot* promise; |
724 | 579k | txSlot* argument; |
725 | 579k | txSlot* result; |
726 | 579k | slot = mxFunctionInstanceHome(mxFunction->value.reference)->value.home.object->next; |
727 | 579k | if (slot->value.boolean) |
728 | 7 | return; |
729 | 579k | slot->value.boolean = 1; |
730 | 579k | mxPushSlot(slot->next); |
731 | 579k | promise = the->stack->value.reference; |
732 | 579k | slot->next = C_NULL; |
733 | 579k | if (mxArgc > 0) |
734 | 577k | mxPushSlot(mxArgv(0)); |
735 | 1.70k | else |
736 | 1.70k | mxPushUndefined(); |
737 | 579k | argument = the->stack; |
738 | | #ifdef mxPromisePrint |
739 | | fprintf(stderr, "fxResolvePromise %d\n", promise->next->ID); |
740 | | #endif |
741 | 579k | mxTry(the) { |
742 | 579k | if (mxIsReference(argument)) { |
743 | 81.8k | if (argument->value.reference == promise) |
744 | 2 | mxTypeError("promise resolves itself"); |
745 | 81.8k | mxPushSlot(argument); |
746 | 81.8k | mxGetID(mxID(_then)); |
747 | 81.8k | slot = the->stack; |
748 | 81.8k | if (fxIsCallable(the, slot)) { |
749 | | #ifdef mxPromisePrint |
750 | | fprintf(stderr, "fxResolvePromise then %d\n", promise->next->ID); |
751 | | #endif |
752 | 50.5k | mxPushSlot(argument); |
753 | 50.5k | mxPush(mxOnThenableFunction); |
754 | 50.5k | mxCall(); |
755 | 50.5k | fxPushPromiseFunctions(the, promise); |
756 | 50.5k | mxPushSlot(slot); |
757 | 50.5k | fxQueueJob(the, 3, promise); |
758 | 50.5k | goto bail; |
759 | 50.5k | } |
760 | 31.3k | mxPop(); |
761 | 31.3k | } |
762 | 529k | result = mxPromiseResult(promise); |
763 | 529k | result->kind = argument->kind; |
764 | 529k | result->value = argument->value; |
765 | 529k | slot = mxPromiseThens(promise)->value.reference->next; |
766 | 650k | while (slot) { |
767 | 121k | mxPushReference(slot->value.reference); |
768 | 121k | mxPush(mxOnResolvedPromiseFunction); |
769 | 121k | mxCall(); |
770 | 121k | mxPushSlot(result); |
771 | 121k | fxQueueJob(the, 1, promise); |
772 | 121k | slot = slot->next; |
773 | 121k | } |
774 | 529k | mxPromiseThens(promise)->value.reference->next = C_NULL; |
775 | 529k | slot = mxPromiseStatus(promise); |
776 | 529k | slot->value.integer = mxFulfilledStatus; |
777 | 529k | } |
778 | 579k | bail: |
779 | 579k | mxCatch(the) { |
780 | 2 | result = mxPromiseResult(promise); |
781 | 2 | result->kind = mxException.kind; |
782 | 2 | result->value = mxException.value; |
783 | 2 | mxException = mxUndefined; |
784 | 2 | slot = mxPromiseThens(promise)->value.reference->next; |
785 | 2 | if (slot) { |
786 | 2 | while (slot) { |
787 | 1 | mxPushReference(slot->value.reference); |
788 | 1 | mxPush(mxOnRejectedPromiseFunction); |
789 | 1 | mxCall(); |
790 | 1 | mxPushSlot(result); |
791 | 1 | fxQueueJob(the, 1, promise); |
792 | 1 | slot = slot->next; |
793 | 1 | } |
794 | 1 | mxPromiseThens(promise)->value.reference->next = C_NULL; |
795 | 1 | } |
796 | 1 | else { |
797 | 1 | fxAddUnhandledRejection(the, promise); |
798 | 1 | } |
799 | 2 | slot = mxPromiseStatus(promise); |
800 | 2 | slot->value.integer = mxRejectedStatus; |
801 | 2 | } |
802 | 579k | } |
803 | | |
804 | | void fx_Promise(txMachine* the) |
805 | 508k | { |
806 | 508k | txSlot* stack = the->stack; |
807 | 508k | txSlot* promise; |
808 | 508k | txSlot* argument; |
809 | 508k | txSlot* status; |
810 | 508k | txSlot* resolveFunction; |
811 | 508k | txSlot* rejectFunction; |
812 | 508k | if (mxIsUndefined(mxTarget)) |
813 | 6 | mxTypeError("call: Promise"); |
814 | 508k | if (mxArgc < 1) |
815 | 1 | mxTypeError("no executor"); |
816 | 508k | argument = mxArgv(0); |
817 | 508k | if (!fxIsCallable(the, argument)) |
818 | 10 | mxTypeError("executor: not a function"); |
819 | 508k | mxPushSlot(mxTarget); |
820 | 508k | fxGetPrototypeFromConstructor(the, &mxPromisePrototype); |
821 | 508k | promise = fxNewPromiseInstance(the); |
822 | | #ifdef mxPromisePrint |
823 | | fprintf(stderr, "fx_Promise %d\n", promise->next->ID); |
824 | | #endif |
825 | 508k | mxPullSlot(mxResult); |
826 | 508k | status = mxPromiseStatus(promise); |
827 | 508k | status->value.integer = mxPendingStatus; |
828 | 508k | fxPushPromiseFunctions(the, promise); |
829 | 508k | resolveFunction = the->stack + 1; |
830 | 508k | rejectFunction = the->stack; |
831 | 508k | { |
832 | 508k | mxTry(the) { |
833 | | /* THIS */ |
834 | 508k | mxPushUndefined(); |
835 | | /* FUNCTION */ |
836 | 508k | mxPushSlot(argument); |
837 | 508k | mxCall(); |
838 | | /* ARGUMENTS */ |
839 | 508k | mxPushSlot(resolveFunction); |
840 | 508k | mxPushSlot(rejectFunction); |
841 | 508k | mxRunCount(2); |
842 | 508k | } |
843 | 508k | mxCatch(the) { |
844 | 10 | fxRejectException(the, rejectFunction); |
845 | 10 | } |
846 | 508k | } |
847 | 508k | the->stack = stack; |
848 | 508k | } |
849 | | |
850 | | void fx_Promise_all(txMachine* the) |
851 | 2.09k | { |
852 | 2.09k | fxCombinePromises(the, XS_PROMISE_COMBINE_FULFILLED); |
853 | 2.09k | } |
854 | | |
855 | | void fx_Promise_allSettled(txMachine* the) |
856 | 36 | { |
857 | 36 | fxCombinePromises(the, XS_PROMISE_COMBINE_SETTLED); |
858 | 36 | } |
859 | | |
860 | | void fx_Promise_any(txMachine* the) |
861 | 315 | { |
862 | 315 | fxCombinePromises(the, XS_PROMISE_COMBINE_REJECTED); |
863 | 315 | } |
864 | | |
865 | | void fx_Promise_race(txMachine* the) |
866 | 55 | { |
867 | 55 | fxCombinePromises(the, XS_PROMISE_COMBINE_NONE); |
868 | 55 | } |
869 | | |
870 | | void fx_Promise_reject(txMachine* the) |
871 | 6.25k | { |
872 | 6.25k | txSlot* resolveFunction; |
873 | 6.25k | txSlot* rejectFunction; |
874 | | |
875 | 6.25k | if (!mxIsReference(mxThis)) |
876 | 9 | mxTypeError("this: not an object"); |
877 | 6.24k | mxTemporary(resolveFunction); |
878 | 6.24k | mxTemporary(rejectFunction); |
879 | 6.24k | mxPushSlot(mxThis); |
880 | 6.24k | fxNewPromiseCapability(the, resolveFunction, rejectFunction); |
881 | 6.24k | mxPullSlot(mxResult); |
882 | | /* THIS */ |
883 | 6.24k | mxPushUndefined(); |
884 | | /* FUNCTION */ |
885 | 6.24k | mxPushSlot(rejectFunction); |
886 | 6.24k | mxCall(); |
887 | | /* ARGUMENTS */ |
888 | 6.24k | if (mxArgc > 0) |
889 | 6.23k | mxPushSlot(mxArgv(0)); |
890 | 5 | else |
891 | 5 | mxPushUndefined(); |
892 | 6.24k | mxRunCount(1); |
893 | 6.24k | mxPop(); |
894 | 6.24k | } |
895 | | |
896 | | void fx_Promise_resolve(txMachine* the) |
897 | 90.2k | { |
898 | 90.2k | if (!mxIsReference(mxThis)) |
899 | 9 | mxTypeError("this: not an object"); |
900 | 90.2k | mxPushUndefined(); |
901 | 90.2k | mxPushSlot(mxThis); |
902 | 90.2k | if (mxArgc > 0) |
903 | 90.2k | mxPushSlot(mxArgv(0)); |
904 | 13 | else |
905 | 13 | mxPushUndefined(); |
906 | 90.2k | fx_Promise_resolveAux(the); |
907 | 90.2k | mxPop(); |
908 | 90.2k | mxPop(); |
909 | 90.2k | mxPullSlot(mxResult); |
910 | 90.2k | } |
911 | | |
912 | | void fx_Promise_resolveAux(txMachine* the) |
913 | 90.3k | { |
914 | 90.3k | txSlot* argument = the->stack; |
915 | 90.3k | txSlot* constructor = the->stack + 1; |
916 | 90.3k | txSlot* result = the->stack + 2; |
917 | 90.3k | txSlot* resolveFunction; |
918 | 90.3k | txSlot* rejectFunction; |
919 | 90.3k | if (mxIsReference(argument)) { |
920 | 509 | txSlot* promise = argument->value.reference; |
921 | 509 | if (mxIsPromise(promise)) { |
922 | 238 | mxPushReference(promise); |
923 | 238 | mxGetID(mxID(_constructor)); |
924 | 238 | if (fxIsSameValue(the, constructor, the->stack, 0)) { |
925 | 230 | *result = *argument; |
926 | 230 | mxPop(); |
927 | 230 | return; |
928 | 230 | } |
929 | 8 | mxPop(); |
930 | 8 | } |
931 | 509 | } |
932 | 90.1k | mxTemporary(resolveFunction); |
933 | 90.1k | mxTemporary(rejectFunction); |
934 | 90.1k | mxPushSlot(constructor); |
935 | 90.1k | fxNewPromiseCapability(the, resolveFunction, rejectFunction); |
936 | 90.1k | *result = *the->stack; |
937 | 90.1k | mxPop(); |
938 | | /* THIS */ |
939 | 90.1k | mxPushUndefined(); |
940 | | /* FUNCTION */ |
941 | 90.1k | mxPushSlot(resolveFunction); |
942 | 90.1k | mxCall(); |
943 | | /* ARGUMENTS */ |
944 | 90.1k | mxPushSlot(argument); |
945 | | /* COUNT */ |
946 | 90.1k | mxRunCount(1); |
947 | 90.1k | mxPop(); |
948 | 90.1k | mxPop(); // rejectFunction |
949 | 90.1k | mxPop(); // resolveFunction |
950 | 90.1k | } |
951 | | |
952 | | #if mxECMAScript2025 |
953 | | void fx_Promise_try(txMachine* the) |
954 | 38 | { |
955 | 38 | txSlot* stack = the->stack; |
956 | 38 | txSlot* resolveFunction; |
957 | 38 | txSlot* rejectFunction; |
958 | 38 | if (!mxIsReference(mxThis)) |
959 | 7 | mxTypeError("this: not an object"); |
960 | 31 | mxTemporary(resolveFunction); |
961 | 31 | mxTemporary(rejectFunction); |
962 | 31 | mxPushSlot(mxThis); |
963 | 31 | fxNewPromiseCapability(the, resolveFunction, rejectFunction); |
964 | 31 | mxPullSlot(mxResult); |
965 | 31 | { |
966 | 31 | mxTry(the) { |
967 | 31 | txInteger c = mxArgc, i; |
968 | 31 | txSlot* value; |
969 | | /* THIS */ |
970 | 31 | mxPushUndefined(); |
971 | | /* FUNCTION */ |
972 | 31 | if (c > 0) |
973 | 31 | mxPushSlot(mxArgv(0)); |
974 | 0 | else |
975 | 0 | mxPushUndefined(); |
976 | 31 | mxCall(); |
977 | | /* ARGUMENTS */ |
978 | 31 | for (i = 1; i < c; i++) |
979 | 0 | mxPushSlot(mxArgv(i)); |
980 | 31 | if (c > 0) |
981 | 31 | mxRunCount(c - 1); |
982 | 0 | else |
983 | 0 | mxRunCount(0); |
984 | 31 | value = the->stack; |
985 | | /* THIS */ |
986 | 31 | mxPushUndefined(); |
987 | | /* FUNCTION */ |
988 | 31 | mxPushSlot(resolveFunction); |
989 | 31 | mxCall(); |
990 | | /* ARGUMENTS */ |
991 | 31 | mxPushSlot(value); |
992 | | /* COUNT */ |
993 | 31 | mxRunCount(1); |
994 | 31 | } |
995 | 31 | mxCatch(the) { |
996 | 30 | fxRejectException(the, rejectFunction); |
997 | 30 | } |
998 | 31 | } |
999 | 31 | the->stack = stack; |
1000 | 31 | } |
1001 | | #endif |
1002 | | |
1003 | | #if mxECMAScript2024 |
1004 | | void fx_Promise_withResolvers(txMachine* the) |
1005 | 12 | { |
1006 | 12 | txSlot* resolveFunction; |
1007 | 12 | txSlot* rejectFunction; |
1008 | 12 | txSlot* promise; |
1009 | 12 | txSlot* slot; |
1010 | 12 | mxTemporary(resolveFunction); |
1011 | 12 | mxTemporary(rejectFunction); |
1012 | 12 | mxPushSlot(mxThis); |
1013 | 12 | fxNewPromiseCapability(the, resolveFunction, rejectFunction); |
1014 | 12 | promise = the->stack; |
1015 | 12 | mxPush(mxObjectPrototype); |
1016 | 12 | slot = fxNewObjectInstance(the); |
1017 | 12 | mxPullSlot(mxResult); |
1018 | 12 | slot = fxLastProperty(the, slot); |
1019 | 12 | slot = fxNextSlotProperty(the, slot, promise, mxID(_promise), XS_NO_FLAG); |
1020 | 12 | slot = fxNextSlotProperty(the, slot, resolveFunction, mxID(_resolve), XS_NO_FLAG); |
1021 | 12 | slot = fxNextSlotProperty(the, slot, rejectFunction, mxID(_reject), XS_NO_FLAG); |
1022 | 12 | mxPop(); |
1023 | 12 | mxPop(); |
1024 | 12 | mxPop(); |
1025 | 12 | } |
1026 | | #endif |
1027 | | |
1028 | | void fx_Promise_prototype_catch(txMachine* the) |
1029 | 32 | { |
1030 | 32 | mxPushSlot(mxThis); |
1031 | 32 | mxDub(); |
1032 | 32 | mxGetID(mxID(_then)); |
1033 | 32 | mxCall(); |
1034 | 32 | mxPushUndefined(); |
1035 | 32 | if (mxArgc > 0) |
1036 | 20 | mxPushSlot(mxArgv(0)); |
1037 | 12 | else |
1038 | 12 | mxPushUndefined(); |
1039 | 32 | mxRunCount(2); |
1040 | 32 | mxPullSlot(mxResult); |
1041 | 32 | } |
1042 | | |
1043 | | #if 0 |
1044 | | void fx_Promise_prototype_dumpAux(txMachine* the, txSlot* promise, txInteger c) |
1045 | | { |
1046 | | txInteger i; |
1047 | | txSlot* reference; |
1048 | | for (i = 0; i < c; i++) |
1049 | | fprintf(stderr, "\t"); |
1050 | | fprintf(stderr, "promise %d\n", promise->next->ID); |
1051 | | reference = mxPromiseThens(promise)->value.reference->next; |
1052 | | c++; |
1053 | | while (reference) { |
1054 | | fx_Promise_prototype_dumpAux(the, reference->value.reference, c); |
1055 | | reference = reference->next; |
1056 | | } |
1057 | | } |
1058 | | #endif |
1059 | | |
1060 | | void fx_Promise_prototype_finally(txMachine* the) |
1061 | 87.1k | { |
1062 | 87.1k | txSlot* constructor; |
1063 | 87.1k | if (!mxIsReference(mxThis)) |
1064 | 3 | mxTypeError("this: not an object"); |
1065 | 87.1k | mxPushSlot(mxThis); |
1066 | 87.1k | mxGetID(mxID(_constructor)); |
1067 | 87.1k | fxToSpeciesConstructor(the, &mxPromiseConstructor); |
1068 | 87.1k | constructor = the->stack; |
1069 | | |
1070 | 87.1k | mxPushSlot(mxThis); |
1071 | 87.1k | mxDub(); |
1072 | 87.1k | mxGetID(mxID(_then)); |
1073 | 87.1k | mxCall(); |
1074 | 87.1k | if (mxArgc > 0) { |
1075 | 87.1k | if (mxIsReference(mxArgv(0)) && mxIsCallable(mxArgv(0)->value.reference)) { |
1076 | 87.1k | txSlot* function = fxNewHostFunction(the, fx_Promise_prototype_finallyAux, 1, XS_NO_ID, mx_Promise_prototype_finallyAuxProfileID); |
1077 | 87.1k | txSlot* object = fxNewInstance(the); |
1078 | 87.1k | txSlot* slot = object->next = fxNewSlot(the); |
1079 | 87.1k | slot->kind = XS_REFERENCE_KIND; |
1080 | 87.1k | slot->value.reference = constructor->value.reference; |
1081 | 87.1k | slot = slot->next = fxNewSlot(the); |
1082 | 87.1k | slot->kind = XS_REFERENCE_KIND; |
1083 | 87.1k | slot->value.reference = mxArgv(0)->value.reference; |
1084 | 87.1k | slot = slot->next = fxNewSlot(the); |
1085 | 87.1k | slot->kind = XS_BOOLEAN_KIND; |
1086 | 87.1k | slot->value.boolean = 1; |
1087 | 87.1k | slot = mxFunctionInstanceHome(function); |
1088 | 87.1k | slot->value.home.object = object; |
1089 | 87.1k | mxPop(); |
1090 | | |
1091 | 87.1k | function = fxNewHostFunction(the, fx_Promise_prototype_finallyAux, 1, XS_NO_ID, mx_Promise_prototype_finallyAuxProfileID); |
1092 | 87.1k | object = fxNewInstance(the); |
1093 | 87.1k | slot = object->next = fxNewSlot(the); |
1094 | 87.1k | slot->kind = XS_REFERENCE_KIND; |
1095 | 87.1k | slot->value.reference = constructor->value.reference; |
1096 | 87.1k | slot = slot->next = fxNewSlot(the); |
1097 | 87.1k | slot->kind = XS_REFERENCE_KIND; |
1098 | 87.1k | slot->value.reference = mxArgv(0)->value.reference; |
1099 | 87.1k | slot = slot->next = fxNewSlot(the); |
1100 | 87.1k | slot->kind = XS_BOOLEAN_KIND; |
1101 | 87.1k | slot->value.boolean = 0; |
1102 | 87.1k | slot = mxFunctionInstanceHome(function); |
1103 | 87.1k | slot->value.home.object = object; |
1104 | 87.1k | mxPop(); |
1105 | 87.1k | } |
1106 | 2 | else { |
1107 | 2 | mxPushSlot(mxArgv(0)); |
1108 | 2 | mxPushSlot(mxArgv(0)); |
1109 | 2 | } |
1110 | 87.1k | } |
1111 | 2 | else { |
1112 | 2 | mxPushUndefined(); |
1113 | 2 | mxPushUndefined(); |
1114 | 2 | } |
1115 | 87.1k | mxRunCount(2); |
1116 | 87.1k | mxPullSlot(mxResult); |
1117 | 87.1k | } |
1118 | | |
1119 | | void fx_Promise_prototype_finallyAux(txMachine* the) |
1120 | 25 | { |
1121 | 25 | txSlot* object = mxFunctionInstanceHome(mxFunction->value.reference)->value.home.object; |
1122 | 25 | txSlot* constructor = object->next; |
1123 | 25 | txSlot* onFinally = constructor->next; |
1124 | 25 | txSlot* success = onFinally->next; |
1125 | 25 | txSlot* argument; |
1126 | 25 | txSlot* function; |
1127 | 25 | txSlot* slot; |
1128 | 25 | txSlot* home; |
1129 | | |
1130 | 25 | { |
1131 | 25 | mxTry(the) { |
1132 | 25 | mxPushUndefined(); |
1133 | 25 | mxPushSlot(onFinally); |
1134 | 25 | mxCall(); |
1135 | 25 | mxRunCount(0); |
1136 | 25 | } |
1137 | 25 | mxCatch(the) { |
1138 | 9 | mxArgv(0)->kind = mxException.kind; |
1139 | 9 | mxArgv(0)->value = mxException.value; |
1140 | 9 | success->value.boolean = 0; |
1141 | 9 | mxPush(mxException); |
1142 | 9 | mxException = mxUndefined; |
1143 | 9 | } |
1144 | 25 | } |
1145 | 25 | argument = the->stack; |
1146 | | |
1147 | 25 | mxPushUndefined(); |
1148 | 25 | mxPushSlot(constructor); |
1149 | 25 | mxPushSlot(argument); |
1150 | 25 | fx_Promise_resolveAux(the); |
1151 | 25 | mxPop(); |
1152 | 25 | mxPop(); |
1153 | 25 | mxDub(); |
1154 | 25 | mxGetID(mxID(_then)); |
1155 | 25 | mxCall(); |
1156 | | |
1157 | 25 | if (success->value.boolean) |
1158 | 8 | function = fxNewHostFunction(the, fx_Promise_prototype_finallyReturn, 0, XS_NO_ID, mx_Promise_prototype_finallyReturnProfileID); |
1159 | 17 | else |
1160 | 17 | function = fxNewHostFunction(the, fx_Promise_prototype_finallyThrow, 0, XS_NO_ID, mx_Promise_prototype_finallyThrowProfileID); |
1161 | 25 | object = fxNewInstance(the); |
1162 | 25 | slot = object->next = fxNewSlot(the); |
1163 | 25 | slot->kind = mxArgv(0)->kind; |
1164 | 25 | slot->value = mxArgv(0)->value; |
1165 | 25 | home = mxFunctionInstanceHome(function); |
1166 | 25 | home->value.home.object = object; |
1167 | 25 | mxPop(); |
1168 | 25 | mxRunCount(1); |
1169 | | |
1170 | 25 | mxPullSlot(mxResult); |
1171 | 25 | } |
1172 | | |
1173 | | void fx_Promise_prototype_finallyReturn(txMachine* the) |
1174 | 8 | { |
1175 | 8 | txSlot* object = mxFunctionInstanceHome(mxFunction->value.reference)->value.home.object; |
1176 | 8 | txSlot* slot = object->next; |
1177 | 8 | mxResult->kind = slot->kind; |
1178 | 8 | mxResult->value = slot->value; |
1179 | 8 | } |
1180 | | |
1181 | | void fx_Promise_prototype_finallyThrow(txMachine* the) |
1182 | 16 | { |
1183 | 16 | txSlot* object = mxFunctionInstanceHome(mxFunction->value.reference)->value.home.object; |
1184 | 16 | txSlot* slot = object->next; |
1185 | 16 | mxException.kind = slot->kind; |
1186 | 16 | mxException.value = slot->value; |
1187 | 16 | fxThrow(the, NULL, 0); |
1188 | 16 | } |
1189 | | |
1190 | | void fx_Promise_prototype_then(txMachine* the) |
1191 | 194k | { |
1192 | 194k | txSlot* promise; |
1193 | 194k | txSlot* onFullfilled = C_NULL; |
1194 | 194k | txSlot* onRejected = C_NULL; |
1195 | 194k | txSlot* resolveFunction; |
1196 | 194k | txSlot* rejectFunction; |
1197 | | |
1198 | 194k | if (!mxIsReference(mxThis)) |
1199 | 1 | mxTypeError("this: not an object"); |
1200 | 194k | promise = mxThis->value.reference; |
1201 | 194k | if (!mxIsPromise(promise)) |
1202 | 1 | mxTypeError("this: not a Promise instance"); |
1203 | | #ifdef mxPromisePrint |
1204 | | fprintf(stderr, "fx_Promise_prototype_then %d\n", promise->next->ID); |
1205 | | #endif |
1206 | | |
1207 | 194k | if ((mxArgc > 0) && mxIsReference(mxArgv(0))) { |
1208 | 194k | onFullfilled = mxArgv(0); |
1209 | 194k | } |
1210 | 194k | if ((mxArgc > 1) && mxIsReference(mxArgv(1))) { |
1211 | 194k | onRejected = mxArgv(1); |
1212 | 194k | } |
1213 | | |
1214 | 194k | mxTemporary(resolveFunction); |
1215 | 194k | mxTemporary(rejectFunction); |
1216 | 194k | mxPushSlot(mxThis); |
1217 | 194k | mxGetID(mxID(_constructor)); |
1218 | 194k | fxToSpeciesConstructor(the, &mxPromiseConstructor); |
1219 | 194k | fxNewPromiseCapability(the, resolveFunction, rejectFunction); |
1220 | 194k | mxPullSlot(mxResult); |
1221 | | |
1222 | 194k | fxPromiseThen(the, promise, onFullfilled, onRejected, resolveFunction, rejectFunction); |
1223 | 194k | } |
1224 | | |
1225 | | void fxQueueJob(txMachine* the, txInteger count, txSlot* promise) |
1226 | 278k | { |
1227 | 278k | txSlot* slot; |
1228 | 278k | txSlot* job; |
1229 | 278k | txSlot* item; |
1230 | 278k | txSlot* stack; |
1231 | 278k | txSlot** address; |
1232 | | |
1233 | 278k | if (promise) { |
1234 | 278k | txSlot* list = &mxUnhandledPromises; |
1235 | 278k | txSlot** address = &list->value.reference->next; |
1236 | 106M | while ((slot = *address)) { |
1237 | 106M | if (slot->value.weakRef.target == promise) { |
1238 | 2.03k | slot = slot->next; |
1239 | 2.03k | *address = slot->next; |
1240 | 2.03k | break; |
1241 | 2.03k | } |
1242 | 106M | slot = slot->next; |
1243 | 106M | address = &slot->next; |
1244 | 106M | } |
1245 | | #ifdef mxPromisePrint |
1246 | | fprintf(stderr, "fxQueueJob %d\n", promise->next->ID); |
1247 | | #endif |
1248 | | #ifdef mxInstrument |
1249 | | the->promisesSettledCount += 1; |
1250 | | #endif |
1251 | 278k | } |
1252 | 278k | if (mxPendingJobs.value.reference->next == NULL) { |
1253 | 91.8k | fxQueuePromiseJobs(the); |
1254 | 91.8k | } |
1255 | 278k | count += 6; |
1256 | 278k | item = stack = the->stack + count; |
1257 | 278k | slot = job = fxNewInstance(the); |
1258 | 2.33M | while (count > 0) { |
1259 | 2.05M | item--; |
1260 | 2.05M | slot = slot->next = fxNewSlot(the); |
1261 | 2.05M | slot->kind = item->kind; |
1262 | 2.05M | slot->value = item->value; |
1263 | 2.05M | count--; |
1264 | 2.05M | } |
1265 | 278k | address = &(mxPendingJobs.value.reference->next); |
1266 | 561M | while ((slot = *address)) |
1267 | 561M | address = &(slot->next); |
1268 | 278k | slot = *address = fxNewSlot(the); |
1269 | 278k | slot->kind = XS_REFERENCE_KIND; |
1270 | 278k | slot->value.reference = job; |
1271 | 278k | the->stack = stack; |
1272 | 278k | } |
1273 | | |
1274 | | void fxRunPromiseJobs(txMachine* the) |
1275 | 91.6k | { |
1276 | 91.6k | txSlot* job; |
1277 | 91.6k | txSlot* slot; |
1278 | 91.6k | txInteger count; |
1279 | | |
1280 | | #ifdef mxPromisePrint |
1281 | | fprintf(stderr, "\n# fxRunPromiseJobs\n"); |
1282 | | #endif |
1283 | 91.6k | job = mxRunningJobs.value.reference->next = mxPendingJobs.value.reference->next; |
1284 | 91.6k | mxPendingJobs.value.reference->next = C_NULL; |
1285 | 354k | while (job) { |
1286 | 263k | mxTry(the) { |
1287 | 263k | count = 0; |
1288 | 263k | slot = job->value.reference->next; |
1289 | 2.17M | while (slot) { |
1290 | 1.91M | mxPushSlot(slot); |
1291 | 1.91M | count++; |
1292 | 1.91M | slot = slot->next; |
1293 | 1.91M | } |
1294 | 263k | mxRunCount(count - 6); |
1295 | 263k | mxPop(); |
1296 | 263k | if (mxDuringJobs.kind == XS_REFERENCE_KIND) |
1297 | 263k | mxDuringJobs.value.reference->next = C_NULL; |
1298 | 263k | } |
1299 | 263k | mxCatch(the) { |
1300 | 1 | fxAbort(the, XS_UNHANDLED_EXCEPTION_EXIT); |
1301 | 1 | } |
1302 | 263k | job = job->next; |
1303 | 263k | } |
1304 | 91.6k | mxRunningJobs.value.reference->next = C_NULL; |
1305 | 91.6k | } |
1306 | | |
1307 | | |
1308 | | |
1309 | | |
1310 | | |