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