Coverage Report

Created: 2026-09-03 07:12

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