Coverage Report

Created: 2026-01-29 06:33

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