Coverage Report

Created: 2026-12-01 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/moddable/xs/sources/xsError.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2016-2026  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
static txSlot* fx_Error_aux(txMachine* the, txError error, txInteger i);
41
42
const int gxErrorWhichPrototypeStackIndex[XS_ERROR_COUNT] ICACHE_FLASH_ATTR = {
43
  mxErrorPrototypeStackIndex,
44
  mxErrorPrototypeStackIndex,
45
  mxEvalErrorPrototypeStackIndex,
46
  mxRangeErrorPrototypeStackIndex,
47
  mxReferenceErrorPrototypeStackIndex,
48
  mxSyntaxErrorPrototypeStackIndex,
49
  mxTypeErrorPrototypeStackIndex,
50
  mxURIErrorPrototypeStackIndex,
51
  mxAggregateErrorPrototypeStackIndex,
52
#if mxExplicitResourceManagement
53
  mxSuppressedErrorPrototypeStackIndex,
54
#else
55
  mxErrorPrototypeStackIndex,
56
#endif
57
};
58
59
const char *const gxErrorNames[XS_ERROR_COUNT] ICACHE_FLASH_ATTR = {
60
  "no error",
61
  "Error",
62
  "EvalError",
63
  "RangeError",
64
  "ReferenceError",
65
  "SyntaxError",
66
  "TypeError",
67
  "URIError",
68
  "AggregateError",
69
  "SuppressedError"
70
};
71
72
void fxBuildError(txMachine* the)
73
33.9k
{
74
33.9k
  txSlot* slot;
75
33.9k
  txSlot* prototype;
76
33.9k
  txSlot* instance;
77
33.9k
#if mxExplicitResourceManagement
78
33.9k
  txSlot* property;
79
33.9k
#endif
80
  
81
33.9k
  mxPush(mxObjectPrototype);
82
33.9k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
83
33.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Error_toString), 0, mxID(_toString), XS_DONT_ENUM_FLAG);
84
33.9k
  slot = fxNextStringXProperty(the, slot, (txString)gxErrorNames[XS_UNKNOWN_ERROR], mxID(_name), XS_DONT_ENUM_FLAG);
85
33.9k
  slot = fxNextStringXProperty(the, slot, "", mxID(_message), XS_DONT_ENUM_FLAG);
86
33.9k
  slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_Error_prototype_get_stack), mxCallback(fx_Error_prototype_set_stack), mxID(_stack), XS_DONT_ENUM_FLAG);
87
33.9k
  mxErrorPrototype = *the->stack;
88
33.9k
  prototype = fxBuildHostConstructor(the, mxCallback(fx_Error), 1, mxID(_Error));
89
33.9k
  mxErrorConstructor = *the->stack;
90
33.9k
#if mxErrorIsError
91
33.9k
  slot = fxLastProperty(the, prototype);
92
33.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Error_isError), 1, mxID(_isError), XS_DONT_ENUM_FLAG);
93
33.9k
#endif
94
33.9k
  mxPop();
95
33.9k
  mxPush(mxErrorPrototype);
96
33.9k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
97
33.9k
  slot = fxNextStringXProperty(the, slot, (txString)gxErrorNames[XS_AGGREGATE_ERROR], mxID(_name), XS_DONT_ENUM_FLAG);
98
33.9k
  slot = fxNextStringXProperty(the, slot, "", mxID(_message), XS_DONT_ENUM_FLAG);
99
33.9k
  mxAggregateErrorPrototype = *the->stack;
100
33.9k
  instance = fxBuildHostConstructor(the, mxCallback(fx_AggregateError), 2, mxID(_AggregateError));
101
33.9k
  instance->value.instance.prototype = prototype;
102
33.9k
  mxAggregateErrorConstructor = *the->stack;
103
33.9k
  mxPop();
104
33.9k
  mxPush(mxErrorPrototype);
105
33.9k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
106
33.9k
  slot = fxNextStringXProperty(the, slot, (txString)gxErrorNames[XS_EVAL_ERROR], mxID(_name), XS_DONT_ENUM_FLAG);
107
33.9k
  slot = fxNextStringXProperty(the, slot, "", mxID(_message), XS_DONT_ENUM_FLAG);
108
33.9k
  mxEvalErrorPrototype = *the->stack;
109
33.9k
  instance = fxBuildHostConstructor(the, mxCallback(fx_EvalError), 1, mxID(_EvalError));
110
33.9k
  instance->value.instance.prototype = prototype;
111
33.9k
  mxEvalErrorConstructor = *the->stack;
112
33.9k
  mxPop();
113
33.9k
  mxPush(mxErrorPrototype);
114
33.9k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
115
33.9k
  slot = fxNextStringXProperty(the, slot, (txString)gxErrorNames[XS_RANGE_ERROR], mxID(_name), XS_DONT_ENUM_FLAG);
116
33.9k
  slot = fxNextStringXProperty(the, slot, "", mxID(_message), XS_DONT_ENUM_FLAG);
117
33.9k
  mxRangeErrorPrototype = *the->stack;
118
33.9k
  instance = fxBuildHostConstructor(the, mxCallback(fx_RangeError), 1, mxID(_RangeError));
119
33.9k
  instance->value.instance.prototype = prototype;
120
33.9k
  mxRangeErrorConstructor = *the->stack;
121
33.9k
  mxPop();
122
33.9k
  mxPush(mxErrorPrototype);
123
33.9k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
124
33.9k
  slot = fxNextStringXProperty(the, slot, (txString)gxErrorNames[XS_REFERENCE_ERROR], mxID(_name), XS_DONT_ENUM_FLAG);
125
33.9k
  slot = fxNextStringXProperty(the, slot, "", mxID(_message), XS_DONT_ENUM_FLAG);
126
33.9k
  mxReferenceErrorPrototype = *the->stack;
127
33.9k
  instance = fxBuildHostConstructor(the, mxCallback(fx_ReferenceError), 1, mxID(_ReferenceError));
128
33.9k
  instance->value.instance.prototype = prototype;
129
33.9k
  mxReferenceErrorConstructor = *the->stack;
130
33.9k
  mxPop();
131
33.9k
  mxPush(mxErrorPrototype);
132
33.9k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
133
33.9k
  slot = fxNextStringXProperty(the, slot, (txString)gxErrorNames[XS_SYNTAX_ERROR], mxID(_name), XS_DONT_ENUM_FLAG);
134
33.9k
  slot = fxNextStringXProperty(the, slot, "", mxID(_message), XS_DONT_ENUM_FLAG);
135
33.9k
  mxSyntaxErrorPrototype = *the->stack;
136
33.9k
  instance = fxBuildHostConstructor(the, mxCallback(fx_SyntaxError), 1, mxID(_SyntaxError));
137
33.9k
  instance->value.instance.prototype = prototype;
138
33.9k
  mxSyntaxErrorConstructor = *the->stack;
139
33.9k
  mxPop();
140
33.9k
  mxPush(mxErrorPrototype);
141
33.9k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
142
33.9k
  slot = fxNextStringXProperty(the, slot, (txString)gxErrorNames[XS_TYPE_ERROR], mxID(_name), XS_DONT_ENUM_FLAG);
143
33.9k
  slot = fxNextStringXProperty(the, slot, "", mxID(_message), XS_DONT_ENUM_FLAG);
144
33.9k
  mxTypeErrorPrototype = *the->stack;
145
33.9k
  instance = fxBuildHostConstructor(the, mxCallback(fx_TypeError), 1, mxID(_TypeError));
146
33.9k
  instance->value.instance.prototype = prototype;
147
33.9k
  mxTypeErrorConstructor = *the->stack;
148
33.9k
  mxPop();
149
33.9k
  mxPush(mxErrorPrototype);
150
33.9k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
151
33.9k
  slot = fxNextStringXProperty(the, slot, (txString)gxErrorNames[XS_URI_ERROR], mxID(_name), XS_DONT_ENUM_FLAG);
152
33.9k
  slot = fxNextStringXProperty(the, slot, "", mxID(_message), XS_DONT_ENUM_FLAG);
153
33.9k
  mxURIErrorPrototype = *the->stack;
154
33.9k
  instance = fxBuildHostConstructor(the, mxCallback(fx_URIError), 1, mxID(_URIError));
155
33.9k
  instance->value.instance.prototype = prototype;
156
33.9k
  mxURIErrorConstructor = *the->stack;
157
33.9k
  mxPop();
158
159
33.9k
#if mxExplicitResourceManagement
160
33.9k
  mxPush(mxErrorPrototype);
161
33.9k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
162
33.9k
  slot = fxNextStringXProperty(the, slot, (txString)gxErrorNames[XS_SUPPRESSED_ERROR], mxID(_name), XS_DONT_ENUM_FLAG);
163
33.9k
  slot = fxNextStringXProperty(the, slot, "", mxID(_message), XS_DONT_ENUM_FLAG);
164
33.9k
  mxSuppressedErrorPrototype = *the->stack;
165
33.9k
  instance = fxBuildHostConstructor(the, mxCallback(fx_SuppressedError), 3, mxID(_SuppressedError));
166
33.9k
  instance->value.instance.prototype = prototype;
167
33.9k
  mxSuppressedErrorConstructor = *the->stack;
168
33.9k
  mxPop();
169
170
33.9k
  mxPush(mxObjectPrototype);
171
33.9k
    slot = fxLastProperty(the, fxNewObjectInstance(the));
172
33.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Error_toString), 0, mxID(_toString), XS_DONT_ENUM_FLAG);
173
33.9k
  slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_DisposableStack_prototype_get_disposed), C_NULL, mxID(_disposed), XS_DONT_ENUM_FLAG);
174
33.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_DisposableStack_prototype_adopt), 2, mxID(_adopt), XS_DONT_ENUM_FLAG);
175
33.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_DisposableStack_prototype_defer), 1, mxID(_defer), XS_DONT_ENUM_FLAG);
176
33.9k
  property = slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_DisposableStack_prototype_dispose), 0, mxID(_dispose), XS_DONT_ENUM_FLAG);
177
33.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_DisposableStack_prototype_move), 0, mxID(_move), XS_DONT_ENUM_FLAG);
178
33.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_DisposableStack_prototype_use), 1, mxID(_use), XS_DONT_ENUM_FLAG);
179
33.9k
  slot = fxNextSlotProperty(the, slot, property, mxID(_Symbol_dispose), XS_DONT_ENUM_FLAG);
180
33.9k
  slot = fxNextStringXProperty(the, slot, "DisposableStack", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
181
33.9k
  mxDisposableStackPrototype = *the->stack;
182
33.9k
  slot = fxBuildHostConstructor(the, mxCallback(fx_DisposableStack), 0, mxID(_DisposableStack));
183
33.9k
  mxDisposableStackConstructor = *the->stack;
184
33.9k
  mxPop();
185
  
186
33.9k
  mxPush(mxObjectPrototype);
187
33.9k
    slot = fxLastProperty(the, fxNewObjectInstance(the));
188
33.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Error_toString), 0, mxID(_toString), XS_DONT_ENUM_FLAG);
189
33.9k
  slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_AsyncDisposableStack_prototype_get_disposed), C_NULL, mxID(_disposed), XS_DONT_ENUM_FLAG);
190
33.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_AsyncDisposableStack_prototype_adopt), 2, mxID(_adopt), XS_DONT_ENUM_FLAG);
191
33.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_AsyncDisposableStack_prototype_defer), 1, mxID(_defer), XS_DONT_ENUM_FLAG);
192
33.9k
  property = slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_AsyncDisposableStack_prototype_disposeAsync), 0, mxID(_disposeAsync), XS_DONT_ENUM_FLAG);
193
33.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_AsyncDisposableStack_prototype_move), 0, mxID(_move), XS_DONT_ENUM_FLAG);
194
33.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_AsyncDisposableStack_prototype_use), 1, mxID(_use), XS_DONT_ENUM_FLAG);
195
33.9k
  slot = fxNextSlotProperty(the, slot, property, mxID(_Symbol_asyncDispose), XS_DONT_ENUM_FLAG);
196
33.9k
  slot = fxNextStringXProperty(the, slot, "AsyncDisposableStack", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
197
33.9k
  mxAsyncDisposableStackPrototype = *the->stack;
198
33.9k
  slot = fxBuildHostConstructor(the, mxCallback(fx_AsyncDisposableStack), 0, mxID(_AsyncDisposableStack));
199
33.9k
  mxAsyncDisposableStackConstructor = *the->stack;
200
33.9k
  mxPop();
201
33.9k
#endif
202
33.9k
}
203
204
void fxCaptureErrorStack(txMachine* the, txSlot* internal, txSlot* frame)
205
3.91M
{
206
3.91M
  txSlot* slot = internal->value.reference = fxNewSlot(the);
207
3.91M
  slot->kind = XS_INSTANCE_KIND;
208
3.91M
  slot->value.instance.garbage = C_NULL;
209
3.91M
  slot->value.instance.prototype = C_NULL;
210
19.5M
  while (frame->next) {
211
15.6M
    txSlot* environment = mxFrameToEnvironment(frame);
212
15.6M
    txSlot* function = frame + 2;
213
15.6M
    if (function->kind == XS_REFERENCE_KIND) {
214
15.5M
      function = function->value.reference;
215
15.5M
      if (mxIsFunction(function)) {
216
15.5M
        txSlot* name = mxBehaviorGetProperty(the, function, mxID(_name), 0, XS_OWN);
217
15.5M
        slot = slot->next = fxNewSlot(the);
218
15.5M
        slot->flag = XS_GET_ONLY;
219
15.5M
        slot->kind = XS_KEY_KIND;
220
15.5M
        slot->ID = environment->ID;
221
15.5M
        slot->value.key.string = C_NULL;
222
15.5M
        slot->value.key.sum = environment->value.environment.line;
223
15.5M
        if (name && ((name->kind == XS_STRING_KIND) || (name->kind == XS_STRING_X_KIND))) {
224
15.5M
          if (name->kind == XS_STRING_X_KIND)
225
0
            slot->kind = XS_KEY_X_KIND;
226
15.5M
          slot->value.key.string = name->value.string;
227
15.5M
        }
228
0
        else {
229
0
          txSlot* code = mxFunctionInstanceCode(function);
230
0
          if (code->ID != XS_NO_ID) {
231
0
            txSlot* key = fxGetKey(the, code->ID);
232
0
            if ((key->kind == XS_KEY_KIND) || (key->kind == XS_KEY_X_KIND)) {
233
0
              slot->value.key.string = key->value.key.string;
234
0
              slot->kind = key->kind;
235
0
            }
236
0
            else if (key->kind == XS_REFERENCE_KIND) {
237
0
              name = key->value.reference->next->next;
238
0
              if (name && ((name->kind == XS_STRING_KIND) || (name->kind == XS_STRING_X_KIND))) {
239
0
                if (name->kind == XS_STRING_X_KIND)
240
0
                  slot->kind = XS_KEY_X_KIND;
241
0
                slot->value.key.string = name->value.string;
242
0
              }
243
0
            }
244
0
          }
245
0
        }
246
15.5M
      }
247
15.5M
    }
248
15.6M
    frame = frame->next;
249
15.6M
  }
250
3.91M
}
251
252
void fx_Error(txMachine* the)
253
98.1k
{
254
98.1k
  fx_Error_aux(the, XS_UNKNOWN_ERROR, 0);
255
98.1k
}
256
257
void fx_Error_isError(txMachine* the)
258
1.39k
{
259
1.39k
  mxResult->kind = XS_BOOLEAN_KIND;
260
1.39k
  mxResult->value.boolean = 0;
261
1.39k
  if (mxArgc > 0) {
262
388
    txSlot* instance = fxGetInstance(the, mxArgv(0));
263
388
    if (instance) {
264
51
      txSlot* internal = instance->next;
265
51
      if (internal && (internal->kind == XS_ERROR_KIND)) {
266
25
        mxResult->value.boolean = 1;
267
25
      }
268
51
    }
269
388
  }
270
1.39k
}
271
272
txSlot* fx_Error_aux(txMachine* the, txError error, txInteger i)
273
960k
{
274
960k
  txSlot* instance;
275
960k
  txSlot* slot;
276
960k
  if (!mxHasTarget)
277
467
    mxPushSlot(mxFunction);
278
959k
  else
279
959k
    mxPushSlot(mxTarget);
280
960k
  fxGetPrototypeFromConstructor(the, &mxErrorPrototypes(error));
281
960k
  instance = fxNewObjectInstance(the);
282
960k
  mxPullSlot(mxResult);
283
960k
  slot = instance->next = fxNewSlot(the);
284
960k
  slot->flag = XS_INTERNAL_FLAG;
285
960k
  slot->kind = XS_ERROR_KIND;
286
960k
  slot->value.error.info = C_NULL;
287
960k
  slot->value.error.which = error;
288
960k
  if (gxDefaults.captureErrorStack)
289
960k
    gxDefaults.captureErrorStack(the, slot, the->frame->next);
290
960k
  if ((mxArgc > i) && (mxArgv(i)->kind != XS_UNDEFINED_KIND)) {
291
958k
    fxToString(the, mxArgv(i));
292
958k
    slot = fxNextSlotProperty(the, slot, mxArgv(i), mxID(_message), XS_DONT_ENUM_FLAG);
293
958k
  }
294
960k
  i++;
295
960k
  if ((mxArgc > i) && (mxArgv(i)->kind == XS_REFERENCE_KIND)) {
296
97.3k
    if (mxBehaviorHasProperty(the, mxArgv(i)->value.reference, mxID(_cause), 0)) {
297
1
      mxPushSlot(mxArgv(i));
298
1
      mxGetID(mxID(_cause));
299
1
      slot = fxNextSlotProperty(the, slot, the->stack, mxID(_cause), XS_DONT_ENUM_FLAG);
300
1
      mxPop();
301
1
    }
302
97.3k
  }
303
960k
  return slot;
304
960k
}
305
306
void fx_Error_toString(txMachine* the)
307
162k
{
308
162k
  txSlot* name;
309
162k
  txSlot* message;
310
162k
  if (mxThis->kind != XS_REFERENCE_KIND)
311
9
    mxTypeError("this: not an object");
312
162k
  mxPushSlot(mxThis);
313
162k
  mxGetID(mxID(_name));
314
162k
  if (the->stack->kind == XS_UNDEFINED_KIND) 
315
74.9k
    fxStringX(the, the->stack, "Error");
316
87.7k
  else  
317
87.7k
    fxToString(the, the->stack);
318
162k
  name = the->stack;
319
162k
  mxPushSlot(mxThis);
320
162k
  mxGetID(mxID(_message));
321
162k
  if (the->stack->kind == XS_UNDEFINED_KIND) 
322
74.9k
    *the->stack = mxEmptyString;
323
87.7k
  else  
324
87.7k
    fxToString(the, the->stack);
325
162k
  message = the->stack;
326
162k
  if (c_isEmpty(name->value.string))
327
1
    *mxResult = *message;
328
162k
  else if (c_isEmpty(message->value.string))
329
75.2k
    *mxResult = *name;
330
87.4k
  else {
331
87.4k
    fxStringX(the, mxResult, "");
332
87.4k
    fxConcatString(the, mxResult, name);
333
87.4k
    fxConcatStringC(the, mxResult, ": ");
334
87.4k
    fxConcatString(the, mxResult, message);
335
87.4k
  }
336
162k
  the->stack += 2;
337
162k
}
338
339
void fx_AggregateError(txMachine* the)
340
111
{
341
111
  txSlot* stack = the->stack;
342
111
  txSlot* property = fx_Error_aux(the, XS_AGGREGATE_ERROR, 1);
343
111
  txSlot* array;
344
111
  txSlot** address;
345
111
  txIndex length = 0;
346
111
  txSlot* iterator;
347
111
  txSlot* next;
348
111
  txSlot* value;
349
111
  txSlot* slot;
350
  
351
111
  mxPush(mxArrayPrototype);
352
111
  array = fxNewArrayInstance(the);
353
111
  fxNextSlotProperty(the, property, the->stack, mxID(_errors), XS_DONT_ENUM_FLAG);
354
111
  address = &array->next->next;
355
111
  mxTemporary(iterator);
356
111
  mxTemporary(next);
357
111
  fxGetIterator(the, mxArgv(0), iterator, next, 0);
358
111
  mxTemporary(value);
359
39.7k
  while (fxIteratorNext(the, iterator, next, value)) {
360
39.6k
    mxTry(the) {
361
39.6k
      *address = slot = fxNewSlot(the);
362
39.6k
      slot->kind = value->kind;
363
39.6k
      slot->value = value->value;
364
39.6k
      address = &slot->next;
365
39.6k
      length++;
366
39.6k
    }
367
39.6k
    mxCatch(the) {
368
0
      fxIteratorReturn(the, iterator, 1);
369
0
      fxJump(the);
370
0
    }
371
39.6k
  }
372
111
  array->next->value.array.length = length;
373
111
  fxCacheArray(the, array);
374
  
375
111
  the->stack = stack;
376
111
}
377
378
void fx_EvalError(txMachine* the)
379
9
{
380
9
  fx_Error_aux(the, XS_EVAL_ERROR, 0);
381
9
}
382
383
void fx_RangeError(txMachine* the)
384
147
{
385
147
  fx_Error_aux(the, XS_RANGE_ERROR, 0);
386
147
}
387
388
void fx_ReferenceError(txMachine* the)
389
7
{
390
7
  fx_Error_aux(the, XS_REFERENCE_ERROR, 0);
391
7
}
392
393
void fx_SyntaxError(txMachine* the)
394
861k
{
395
861k
  fx_Error_aux(the, XS_SYNTAX_ERROR, 0);
396
861k
}
397
398
void fx_TypeError(txMachine* the)
399
15
{
400
15
  fx_Error_aux(the, XS_TYPE_ERROR, 0);
401
15
}
402
403
void fx_URIError(txMachine* the)
404
9
{
405
9
  fx_Error_aux(the, XS_URI_ERROR, 0);
406
9
}
407
408
void fx_Error_prototype_get_stack(txMachine* the)
409
36.1k
{
410
36.1k
  txSlot* slot;
411
36.1k
  if (mxThis->kind != XS_REFERENCE_KIND)
412
17
    mxTypeError("this: not an object");
413
36.1k
  slot = mxThis->value.reference->next;
414
36.1k
  if (slot && (slot->kind == XS_ERROR_KIND)) {
415
1
    fxStringX(the, mxResult, "");
416
1
    mxPushSlot(mxThis);
417
1
    mxGetID(mxID(_name));
418
1
    if (the->stack->kind != XS_UNDEFINED_KIND)  {
419
1
      fxToString(the, the->stack);
420
1
      fxConcatString(the, mxResult, the->stack);
421
1
    }
422
0
    else
423
0
      fxConcatStringC(the, mxResult, "Error");
424
1
    mxPop();
425
1
    mxPushSlot(mxThis);
426
1
    mxGetID(mxID(_message));
427
1
    if (the->stack->kind != XS_UNDEFINED_KIND) {
428
1
      fxToString(the, the->stack);
429
1
      if (!c_isEmpty(the->stack->value.string)) {
430
0
        fxConcatStringC(the, mxResult, ": ");
431
0
        fxConcatString(the, mxResult, the->stack);
432
0
      }
433
1
    }
434
1
    mxPop();
435
1
    slot = slot->value.reference;
436
1
    if (slot) {
437
1
      slot = slot->next;
438
2
      while (slot) {
439
1
        fxConcatStringC(the, mxResult, "\n at");
440
1
        if ((slot->value.key.string != C_NULL) && (mxStringLength(slot->value.key.string))) {
441
0
          fxConcatStringC(the, mxResult, " ");
442
0
          fxConcatString(the, mxResult, slot);
443
0
        }
444
1
        fxConcatStringC(the, mxResult, " (");
445
1
        if (slot->ID != XS_NO_ID) {
446
1
          fxIDToString(the, slot->ID, the->nameBuffer, sizeof(the->nameBuffer));
447
1
          fxConcatStringC(the, mxResult, the->nameBuffer);
448
1
          fxConcatStringC(the, mxResult, ":");
449
1
          fxIntegerToString(the, (txInteger)slot->value.key.sum, the->nameBuffer, sizeof(the->nameBuffer));
450
1
          fxConcatStringC(the, mxResult, the->nameBuffer);
451
1
        }
452
1
        fxConcatStringC(the, mxResult, ")");
453
1
        slot = slot->next;
454
1
      }
455
1
    }
456
1
  }
457
36.1k
}
458
459
void fx_Error_prototype_set_stack(txMachine* the)
460
20
{
461
20
  if (mxThis->kind != XS_REFERENCE_KIND)
462
13
    mxTypeError("this: not an object");
463
7
  if (mxArgc < 1)
464
1
    mxTypeError("no value");
465
6
  mxPushSlot(mxArgv(0));
466
6
  mxPushSlot(mxThis);
467
6
  mxDefineID(mxID(_stack), XS_NO_FLAG, XS_GET_ONLY);
468
6
  mxPop();
469
6
}
470
471
static const txString gXSAbortStrings[] = {
472
  "debugger",
473
  "memory full",
474
  "JavaScript stack overflow",
475
  "fatal",
476
  "dead strip",
477
  "unhandled exception",
478
  "not enough keys",
479
  "too much computation",
480
  "unhandled rejection",
481
  "native stack overflow",
482
  "incompatible mod",
483
};
484
485
txString fxAbortString(int status)
486
0
{
487
0
  if ((status < 0) || (status >= (int)(sizeof(gXSAbortStrings) / sizeof(txString))))
488
0
    return "unknown";
489
490
0
  return gXSAbortStrings[status];
491
0
}
492
493
#if mxExplicitResourceManagement
494
495
void fx_SuppressedError(txMachine* the)
496
416
{
497
416
  txSlot* property = fx_Error_aux(the, XS_SUPPRESSED_ERROR, 2);
498
416
  property = fxNextSlotProperty(the, property, mxArgv(0), mxID(_error), XS_DONT_ENUM_FLAG);
499
416
  property = fxNextSlotProperty(the, property, mxArgv(1), mxID(_suppressed), XS_DONT_ENUM_FLAG);
500
416
}
501
502
static txSlot* fxCheckDisposableStackInstance(txMachine* the, txSlot* slot, txBoolean mutable, txBoolean disposable);
503
static void fxDisposableStackPush(txMachine* the, txSlot* property);
504
505
txSlot* fxCheckDisposableStackInstance(txMachine* the, txSlot* slot, txBoolean mutable, txBoolean disposable)
506
299
{
507
299
  if (slot->kind == XS_REFERENCE_KIND) {
508
209
    txSlot* instance = slot->value.reference;
509
209
    if (((slot = instance->next)) && (slot->flag & XS_INTERNAL_FLAG) && (slot->kind == XS_DISPOSABLE_STACK_KIND)) {
510
143
      if (mutable && (slot->flag & XS_DONT_SET_FLAG))
511
0
        mxTypeError("this: read-only DisposableStack instance");
512
143
      if (disposable && slot->value.disposableStack.disposed)
513
18
        mxReferenceError("this: disposed DisposableStack instance");
514
125
      return instance;
515
143
    }
516
209
  }
517
299
  mxTypeError("this: not a DisposableStack instance");
518
0
  return C_NULL;
519
299
}
520
521
void fx_DisposableStack(txMachine* the)
522
30.7k
{
523
30.7k
  txSlot* instance;
524
30.7k
  txSlot* property;
525
30.7k
  if (!mxHasTarget)
526
1
    mxTypeError("call: DisposableStack");
527
30.7k
  mxPushSlot(mxTarget);
528
30.7k
  fxGetPrototypeFromConstructor(the, &mxDisposableStackPrototype);
529
30.7k
  instance = fxNewSlot(the);
530
30.7k
  instance->kind = XS_INSTANCE_KIND;
531
30.7k
  instance->value.instance.garbage = C_NULL;
532
30.7k
  instance->value.instance.prototype = the->stack->value.reference;
533
30.7k
  the->stack->kind = XS_REFERENCE_KIND;
534
30.7k
  the->stack->value.reference = instance;
535
30.7k
  mxPullSlot(mxResult);
536
30.7k
  property = instance->next = fxNewSlot(the);
537
30.7k
  property->flag = XS_INTERNAL_FLAG;
538
30.7k
  property->kind = XS_DISPOSABLE_STACK_KIND;
539
30.7k
  property->value.disposableStack.stack = C_NULL;
540
30.7k
  property->value.disposableStack.disposed = 0;
541
30.7k
}
542
543
void fx_DisposableStack_prototype_get_disposed(txMachine* the)
544
19
{
545
19
  txSlot* instance = fxCheckDisposableStackInstance(the, mxThis, 0, 0);
546
19
  txSlot* property = instance->next;
547
19
  mxResult->value.boolean = property->value.disposableStack.disposed;
548
19
  mxResult->kind = XS_BOOLEAN_KIND;
549
19
}
550
551
void fx_DisposableStack_prototype_adopt(txMachine* the)
552
59
{
553
59
  txSlot* instance = fxCheckDisposableStackInstance(the, mxThis, 1, 1);
554
59
  txSlot* property = instance->next;
555
59
  if (mxArgc > 0) 
556
38
    mxPushSlot(mxArgv(0));
557
21
  else
558
21
    mxPushUndefined();
559
59
  if (mxArgc > 1) 
560
37
    mxPushSlot(mxArgv(1));
561
22
  else
562
22
    mxPushUndefined();
563
59
  fxDisposableStackPush(the, property);
564
59
  property->value.disposableStack.stack->flag |= XS_BASE_FLAG;
565
59
  mxPop();
566
59
  mxPullSlot(mxResult);
567
59
}
568
569
void fx_DisposableStack_prototype_defer(txMachine* the)
570
57
{
571
57
  txSlot* instance = fxCheckDisposableStackInstance(the, mxThis, 1, 1);
572
57
  txSlot* property = instance->next;
573
57
  mxPushUndefined();
574
57
  if (mxArgc > 0) 
575
21
    mxPushSlot(mxArgv(0));
576
36
  else
577
36
    mxPushUndefined();
578
57
  fxDisposableStackPush(the, property);
579
57
  mxPop();
580
57
  mxPop();
581
57
}  
582
583
void fx_DisposableStack_prototype_dispose(txMachine* the)
584
45
{
585
45
  txSlot* instance = fxCheckDisposableStackInstance(the, mxThis, 1, 0);
586
45
  txSlot* property = instance->next;
587
45
  txSlot* exception;
588
45
  txBoolean selector = 1;
589
45
  txSlot* slot;
590
45
  if (property->value.disposableStack.disposed)
591
6
    return;
592
39
  property->value.disposableStack.disposed = 1;
593
39
  mxTemporary(exception);
594
39
  slot = property->value.disposableStack.stack;
595
55
  while (slot) {
596
16
    txSlot* dispose = slot;
597
16
    txSlot* resource = slot->next;
598
16
    mxTry(the) {
599
16
      if (dispose->flag & XS_BASE_FLAG) {
600
6
        mxPushUndefined();
601
6
        mxPushSlot(dispose);
602
6
        mxCall();
603
6
        mxPushSlot(resource);
604
6
        mxRunCount(1);
605
6
        mxPop();
606
6
      }
607
10
      else {
608
10
        mxPushSlot(resource);
609
10
        mxPushSlot(dispose);
610
10
        mxCall();
611
10
        mxRunCount(0);
612
10
        mxPop();
613
10
      }
614
16
    }
615
16
    mxCatch(the) {
616
6
      if (selector == 0) {
617
3
        mxPush(mxSuppressedErrorConstructor);
618
3
        mxNew();
619
3
        mxPush(mxException);
620
3
        mxPushSlot(exception);
621
3
        mxRunCount(2);
622
3
        mxPullSlot(exception);
623
3
      }
624
3
      else {
625
3
        *exception = mxException;
626
3
        selector = 0;
627
3
      }
628
6
      mxException = mxUndefined;
629
6
    }
630
16
    slot = resource->next;
631
16
  }
632
39
  if (selector == 0) {
633
3
    mxException.kind = exception->kind;
634
3
    mxException.value = exception->value;
635
3
    fxJump(the);
636
3
  }
637
39
}
638
639
void fx_DisposableStack_prototype_move(txMachine* the)
640
49
{
641
49
  txSlot* instance = fxCheckDisposableStackInstance(the, mxThis, 1, 1);
642
49
  txSlot* property = instance->next;
643
49
  txSlot* resultInstance;
644
49
  txSlot* resultProperty;
645
49
  mxPush(mxDisposableStackConstructor);
646
49
  fxGetPrototypeFromConstructor(the, &mxDisposableStackPrototype);
647
49
  resultInstance = fxNewSlot(the);
648
49
  resultInstance->kind = XS_INSTANCE_KIND;
649
49
  resultInstance->value.instance.garbage = C_NULL;
650
49
  resultInstance->value.instance.prototype = the->stack->value.reference;
651
49
  the->stack->kind = XS_REFERENCE_KIND;
652
49
  the->stack->value.reference = resultInstance;
653
49
  mxPullSlot(mxResult);
654
49
  resultProperty = resultInstance->next = fxNewSlot(the);
655
49
  resultProperty->flag = XS_INTERNAL_FLAG;
656
49
  resultProperty->kind = XS_DISPOSABLE_STACK_KIND;
657
49
  resultProperty->value.disposableStack.stack = property->value.disposableStack.stack;
658
49
  resultProperty->value.disposableStack.disposed = 0;
659
49
  property->value.disposableStack.stack = C_NULL;
660
49
  property->value.disposableStack.disposed = 1;
661
49
}
662
663
void fx_DisposableStack_prototype_use(txMachine* the)
664
70
{
665
70
  txSlot* instance = fxCheckDisposableStackInstance(the, mxThis, 1, 1);
666
70
  txSlot* property = instance->next;
667
70
  txSlot* resource;
668
70
  if (mxArgc > 0)
669
28
    mxPushSlot(mxArgv(0));
670
42
  else
671
42
    mxPushUndefined();
672
70
  resource = the->stack;
673
70
  if (!mxIsNull(resource) && !mxIsUndefined(resource)) {
674
23
    mxPushSlot(resource);
675
23
    mxGetID(mxID(_Symbol_dispose));
676
23
    fxDisposableStackPush(the, property);
677
23
    mxPop();
678
23
  }
679
70
  mxPullSlot(mxResult);
680
70
}
681
682
void fxDisposableStackPush(txMachine* the, txSlot* property)
683
83
{
684
83
  txSlot* dispose = the->stack;
685
83
  txSlot* resource = dispose + 1;
686
83
  txSlot** address = &property->value.disposableStack.stack;
687
83
  txSlot* slot;
688
83
  if (!fxIsCallable(the, dispose))
689
42
    mxTypeError("dispose: not a function");
690
    
691
41
  slot = fxNewSlot(the);
692
41
  slot->next = *address;
693
41
  slot->kind = resource->kind;
694
41
  slot->value = resource->value;
695
41
  *address = slot;
696
  
697
41
  slot = fxNewSlot(the);
698
41
  slot->next = *address;
699
41
  slot->kind = dispose->kind;
700
41
  slot->value = dispose->value;
701
41
  *address = slot;
702
41
}
703
704
static txSlot* fxCheckAsyncDisposableStackInstance(txMachine* the, txSlot* slot, txBoolean mutable, txBoolean disposable);
705
static void fxAsyncDisposableStackPush(txMachine* the, txSlot* property);
706
static void fxAsyncDisposableStackReject(txMachine* the);
707
static void fxAsyncDisposableStackResolve(txMachine* the);
708
709
txSlot* fxCheckAsyncDisposableStackInstance(txMachine* the, txSlot* slot, txBoolean mutable, txBoolean disposable)
710
252
{
711
252
  if (slot->kind == XS_REFERENCE_KIND) {
712
194
    txSlot* instance = slot->value.reference;
713
194
    if (((slot = instance->next)) && (slot->flag & XS_INTERNAL_FLAG) && (slot->kind == XS_ASYNC_DISPOSABLE_STACK_KIND)) {
714
134
      if (mutable && (slot->flag & XS_DONT_SET_FLAG))
715
0
        mxTypeError("this: read-only AsyncDisposableStack instance");
716
134
      if (disposable && slot->value.disposableStack.disposed)
717
19
        mxReferenceError("this: disposed AsyncDisposableStack instance");
718
115
      return instance;
719
134
    }
720
194
  }
721
252
  mxTypeError("this: not a AsyncDisposableStack instance");
722
0
  return C_NULL;
723
252
}
724
void fx_AsyncDisposableStack(txMachine* the)
725
74.9k
{
726
74.9k
  txSlot* instance;
727
74.9k
  txSlot* property;
728
74.9k
  txSlot* function;
729
74.9k
  txSlot* home;
730
74.9k
  if (!mxHasTarget)
731
1
    mxTypeError("call: AsyncDisposableStack");
732
74.9k
  mxPushSlot(mxTarget);
733
74.9k
  fxGetPrototypeFromConstructor(the, &mxAsyncDisposableStackPrototype);
734
74.9k
  instance = fxNewSlot(the);
735
74.9k
  instance->kind = XS_INSTANCE_KIND;
736
74.9k
  instance->value.instance.garbage = C_NULL;
737
74.9k
  instance->value.instance.prototype = the->stack->value.reference;
738
74.9k
  the->stack->kind = XS_REFERENCE_KIND;
739
74.9k
  the->stack->value.reference = instance;
740
74.9k
  mxPullSlot(mxResult);
741
74.9k
  property = instance->next = fxNewSlot(the);
742
74.9k
  property->flag = XS_INTERNAL_FLAG;
743
74.9k
  property->kind = XS_ASYNC_DISPOSABLE_STACK_KIND;
744
74.9k
  property->value.disposableStack.stack = C_NULL;
745
74.9k
  property->value.disposableStack.disposed = 0;
746
  
747
74.9k
    property = fxNextUndefinedProperty(the, property, XS_NO_ID, XS_INTERNAL_FLAG);
748
74.9k
  function = fxNewHostFunction(the, fxAsyncDisposableStackResolve, 1, XS_NO_ID, mxAsyncGeneratorResolveYieldProfileID);
749
74.9k
  home = mxFunctionInstanceHome(function);
750
74.9k
  home->value.home.object = instance;
751
74.9k
    property = fxNextSlotProperty(the, property, the->stack, XS_NO_ID, XS_INTERNAL_FLAG);
752
74.9k
  mxPop();
753
74.9k
  function = fxNewHostFunction(the, fxAsyncDisposableStackReject, 1, XS_NO_ID, mxAsyncGeneratorRejectYieldProfileID);
754
74.9k
  home = mxFunctionInstanceHome(function);
755
74.9k
  home->value.home.object = instance;
756
74.9k
    property = fxNextSlotProperty(the, property, the->stack, XS_NO_ID, XS_INTERNAL_FLAG);
757
74.9k
  mxPop();
758
74.9k
    property = fxNextUndefinedProperty(the, property, XS_NO_ID, XS_INTERNAL_FLAG);
759
74.9k
    property = fxNextUndefinedProperty(the, property, XS_NO_ID, XS_INTERNAL_FLAG);
760
74.9k
}
761
762
void fx_AsyncDisposableStack_prototype_get_disposed(txMachine* the)
763
30
{
764
30
  txSlot* instance = fxCheckAsyncDisposableStackInstance(the, mxThis, 0, 0);
765
30
  txSlot* property = instance->next;
766
30
  mxResult->value.boolean = property->value.disposableStack.disposed;
767
30
  mxResult->kind = XS_BOOLEAN_KIND;
768
30
}
769
770
void fx_AsyncDisposableStack_prototype_adopt(txMachine* the)
771
63
{
772
63
  txSlot* instance = fxCheckAsyncDisposableStackInstance(the, mxThis, 1, 1);
773
63
  txSlot* property = instance->next;
774
63
  if (mxArgc > 0) 
775
42
    mxPushSlot(mxArgv(0));
776
21
  else
777
21
    mxPushUndefined();
778
63
  if (mxArgc > 1) 
779
41
    mxPushSlot(mxArgv(1));
780
22
  else
781
22
    mxPushUndefined();
782
63
  fxAsyncDisposableStackPush(the, property);
783
63
  property->value.disposableStack.stack->flag |= XS_BASE_FLAG;
784
63
  mxPop();
785
63
  mxPullSlot(mxResult);
786
63
}
787
788
void fx_AsyncDisposableStack_prototype_defer(txMachine* the)
789
45
{
790
45
  txSlot* instance = fxCheckAsyncDisposableStackInstance(the, mxThis, 1, 1);
791
45
  txSlot* property = instance->next;
792
45
  mxPushUndefined();
793
45
  if (mxArgc > 0) 
794
21
    mxPushSlot(mxArgv(0));
795
24
  else
796
24
    mxPushUndefined();
797
45
  fxAsyncDisposableStackPush(the, property);
798
45
  mxPop();
799
45
  mxPop();
800
45
}  
801
802
void fx_AsyncDisposableStack_prototype_disposeAsync(txMachine* the)
803
8
{
804
8
  txSlot* promise;
805
8
  txSlot* resolveFunction;
806
8
  txSlot* rejectFunction;
807
808
8
  mxPush(mxPromisePrototype);
809
8
  promise = fxNewPromiseInstance(the);
810
8
  mxPullSlot(mxResult);
811
8
  mxPromiseStatus(promise)->value.integer = mxPendingStatus;
812
8
  fxPushPromiseFunctions(the, promise);
813
8
  resolveFunction = the->stack + 1;
814
8
  rejectFunction = the->stack;
815
8
  mxTry(the) {
816
8
    txSlot* instance = fxCheckAsyncDisposableStackInstance(the, mxThis, 1, 0);
817
8
    txSlot* property = instance->next;
818
8
    if (property->value.disposableStack.disposed) {
819
0
      mxPushUndefined();
820
0
      mxPushSlot(resolveFunction);
821
0
      mxCall();
822
0
      mxPushUndefined();
823
0
      mxRunCount(1);
824
0
      mxPop();
825
0
    }
826
8
    else {
827
8
      txSlot* exception = property->next;
828
8
      txSlot* resolveStepFunction = exception->next;
829
8
      txSlot* rejectStepFunction = resolveStepFunction->next;
830
8
      property->value.disposableStack.disposed = 1;
831
8
      property = rejectStepFunction->next;
832
8
      property->kind = resolveFunction->kind;
833
8
      property->value = resolveFunction->value;
834
8
      property = property->next;
835
8
      property->kind = rejectFunction->kind;
836
8
      property->value = rejectFunction->value;
837
8
      mxPushUndefined();
838
8
      mxPushSlot(resolveStepFunction);
839
8
      mxCall();
840
8
      mxPushUndefined();
841
8
      mxRunCount(1);
842
8
      mxPop();
843
8
    }
844
8
  }
845
8
  mxCatch(the) {
846
0
    fxRejectException(the, rejectFunction);
847
0
  }
848
8
}
849
850
void fx_AsyncDisposableStack_prototype_move(txMachine* the)
851
25
{
852
25
  txSlot* instance = fxCheckAsyncDisposableStackInstance(the, mxThis, 1, 1);
853
25
  txSlot* property = instance->next;
854
25
  txSlot* resultInstance;
855
25
  txSlot* resultProperty;
856
25
  txSlot* function;
857
25
  txSlot* home;
858
25
  mxPush(mxAsyncDisposableStackConstructor);
859
25
  fxGetPrototypeFromConstructor(the, &mxAsyncDisposableStackPrototype);
860
25
  resultInstance = fxNewSlot(the);
861
25
  resultInstance->kind = XS_INSTANCE_KIND;
862
25
  resultInstance->value.instance.garbage = C_NULL;
863
25
  resultInstance->value.instance.prototype = the->stack->value.reference;
864
25
  the->stack->kind = XS_REFERENCE_KIND;
865
25
  the->stack->value.reference = resultInstance;
866
25
  mxPullSlot(mxResult);
867
25
  resultProperty = resultInstance->next = fxNewSlot(the);
868
25
  resultProperty->flag = XS_INTERNAL_FLAG;
869
25
  resultProperty->kind = XS_ASYNC_DISPOSABLE_STACK_KIND;
870
25
  resultProperty->value.disposableStack.stack = property->value.disposableStack.stack;
871
25
  resultProperty->value.disposableStack.disposed = 0;
872
  
873
25
    resultProperty = fxNextUndefinedProperty(the, resultProperty, XS_NO_ID, XS_INTERNAL_FLAG);
874
25
  function = fxNewHostFunction(the, fxAsyncDisposableStackResolve, 1, XS_NO_ID, mxAsyncGeneratorResolveYieldProfileID);
875
25
  home = mxFunctionInstanceHome(function);
876
25
  home->value.home.object = resultInstance;
877
25
    resultProperty = fxNextSlotProperty(the, resultProperty, the->stack, XS_NO_ID, XS_INTERNAL_FLAG);
878
25
  mxPop();
879
25
  function = fxNewHostFunction(the, fxAsyncDisposableStackReject, 1, XS_NO_ID, mxAsyncGeneratorRejectYieldProfileID);
880
25
  home = mxFunctionInstanceHome(function);
881
25
  home->value.home.object = resultInstance;
882
25
    resultProperty = fxNextSlotProperty(the, resultProperty, the->stack, XS_NO_ID, XS_INTERNAL_FLAG);
883
25
  mxPop();
884
25
    resultProperty = fxNextUndefinedProperty(the, resultProperty, XS_NO_ID, XS_INTERNAL_FLAG);
885
25
    resultProperty = fxNextUndefinedProperty(the, resultProperty, XS_NO_ID, XS_INTERNAL_FLAG);
886
  
887
25
  property->value.disposableStack.stack = C_NULL;
888
25
  property->value.disposableStack.disposed = 1;
889
25
}
890
891
void fx_AsyncDisposableStack_prototype_use(txMachine* the)
892
81
{
893
81
  txSlot* instance = fxCheckAsyncDisposableStackInstance(the, mxThis, 1, 1);
894
81
  txSlot* property = instance->next;
895
81
  txSlot* resource;
896
81
  if (mxArgc > 0)
897
26
    mxPushSlot(mxArgv(0));
898
55
  else
899
55
    mxPushUndefined();
900
81
  resource = the->stack;
901
81
  if (!mxIsNull(resource) && !mxIsUndefined(resource)) {
902
24
    mxPushSlot(resource);
903
24
    mxGetID(mxID(_Symbol_asyncDispose));
904
24
    if (!fxIsCallable(the, the->stack)) {
905
20
      mxPop();
906
20
      mxPushSlot(resource);
907
20
      mxGetID(mxID(_Symbol_dispose));
908
20
    }
909
24
    fxAsyncDisposableStackPush(the, property);
910
24
    mxPop();
911
24
  }
912
57
  else {
913
57
    txSlot** address = &property->value.disposableStack.stack;
914
57
    txSlot* slot;
915
    
916
57
    slot = fxNewSlot(the);
917
57
    slot->next = *address;
918
57
    *address = slot;
919
    
920
57
    slot = fxNewSlot(the);
921
57
    slot->next = *address;
922
57
    *address = slot;
923
57
  }
924
81
  mxPullSlot(mxResult);
925
81
}
926
927
void fxAsyncDisposableStackPush(txMachine* the, txSlot* property)
928
87
{
929
87
  txSlot* dispose = the->stack;
930
87
  txSlot* resource = dispose + 1;
931
87
  txSlot** address = &property->value.disposableStack.stack;
932
87
  txSlot* slot;
933
87
  if (!fxIsCallable(the, dispose))
934
60
    mxTypeError("dispose: no a function");
935
    
936
27
  slot = fxNewSlot(the);
937
27
  slot->next = *address;
938
27
  slot->kind = resource->kind;
939
27
  slot->value = resource->value;
940
27
  *address = slot;
941
  
942
27
  slot = fxNewSlot(the);
943
27
  slot->next = *address;
944
27
  slot->kind = dispose->kind;
945
27
  slot->value = dispose->value;
946
27
  *address = slot;
947
27
}
948
949
void fxAsyncDisposableStackReject(txMachine* the)
950
0
{
951
0
  txSlot* slot = mxFunctionInstanceHome(mxFunction->value.reference);
952
0
  txSlot* instance = slot->value.home.object;
953
0
  txSlot* property = instance->next;
954
0
  txSlot* exception = property->next;
955
  
956
0
  if (mxIsUndefined(exception)) {
957
0
    mxPushSlot(mxArgv(0));
958
0
    mxPullSlot(exception);
959
0
  }
960
0
  else {
961
0
    mxPush(mxSuppressedErrorConstructor);
962
0
    mxNew();
963
0
    mxPushSlot(mxArgv(0));
964
0
    mxPushSlot(exception);
965
0
    mxRunCount(2);
966
0
    mxPullSlot(exception);
967
0
  }
968
0
  fxAsyncDisposableStackResolve(the);
969
0
}
970
971
void fxAsyncDisposableStackResolve(txMachine* the)
972
8
{
973
8
  txSlot* slot = mxFunctionInstanceHome(mxFunction->value.reference);
974
8
  txSlot* instance = slot->value.home.object;
975
8
  txSlot* property = instance->next;
976
8
  txSlot* exception = property->next;
977
8
  txSlot* resolveStepFunction = exception->next;
978
8
  txSlot* rejectStepFunction = resolveStepFunction->next;
979
8
  txSlot* resolveFunction = rejectStepFunction->next;
980
8
  txSlot* rejectFunction = resolveFunction->next;
981
982
8
  slot = property->value.disposableStack.stack;
983
8
  if (slot) {
984
0
    txSlot* dispose = slot;
985
0
    txSlot* resource = slot->next;
986
0
    property->value.disposableStack.stack = resource->next;
987
0
    mxTry(the) {
988
0
      mxPushUndefined();
989
0
      mxPush(mxPromiseConstructor);
990
0
      if (!mxIsUndefined(dispose)) {
991
0
        if (dispose->flag & XS_BASE_FLAG) {
992
0
          mxPushUndefined();
993
0
          mxPushSlot(dispose);
994
0
          mxCall();
995
0
          mxPushSlot(resource);
996
0
          mxRunCount(1);
997
0
        }
998
0
        else {
999
0
          mxPushSlot(resource);
1000
0
          mxPushSlot(dispose);
1001
0
          mxCall();
1002
0
          mxRunCount(0);
1003
0
        }
1004
0
      }
1005
0
      else
1006
0
        mxPushUndefined();
1007
0
      fx_Promise_resolveAux(the);
1008
0
      mxPop();
1009
0
      mxPop();
1010
0
      fxPromiseThen(the, the->stack->value.reference, resolveStepFunction, rejectStepFunction, C_NULL, C_NULL);
1011
0
      mxPop();
1012
0
    }
1013
0
    mxCatch(the) {
1014
0
      mxPushUndefined();
1015
0
      mxPushSlot(rejectStepFunction);
1016
0
      mxCall();
1017
0
      mxPush(mxException);
1018
0
      mxException = mxUndefined;
1019
0
      mxRunCount(1);
1020
0
      mxPop();
1021
0
    }
1022
0
  }
1023
8
  else {
1024
8
    if (mxIsUndefined(exception)) {
1025
8
      mxPushUndefined();
1026
8
      mxPushSlot(resolveFunction);
1027
8
      mxCall();
1028
8
      mxPushUndefined();
1029
8
      mxRunCount(1);
1030
8
      mxPop();
1031
8
    }
1032
0
    else {
1033
0
      mxPushUndefined();
1034
0
      mxPushSlot(rejectFunction);
1035
0
      mxCall();
1036
0
      mxPushSlot(exception);
1037
0
      mxRunCount(1);
1038
0
      mxPop();
1039
0
    }
1040
8
  }
1041
8
}
1042
1043
#endif
1044
1045
1046
1047