Coverage Report

Created: 2025-11-24 06:44

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