Coverage Report

Created: 2026-10-31 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/moddable/xs/sources/xsGenerator.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
//#define mxPromisePrint 1
41
42
43
static txSlot* fxCheckIteratorStep(txMachine* the, txSlot* slot);
44
static txBoolean fxGetIteratorFlattenable(txMachine* the, txSlot* iterable, txSlot* iterator, txSlot* next, txBoolean optional);
45
static txSlot* fxNewIteratorHelperInstance(txMachine* the, txSlot* iterator, txInteger step);
46
47
static void fxNewGeneratorResult(txMachine* the, txBoolean done);
48
49
static txSlot* fxCheckGeneratorInstance(txMachine* the, txSlot* slot);
50
static void fx_Generator_prototype_aux(txMachine* the, txFlag status);
51
52
static void fxAsyncGeneratorStep(txMachine* the, txSlot* generator, txFlag status);
53
static txSlot* fxCheckAsyncGeneratorInstance(txMachine* the, txSlot* slot);
54
static void fx_AsyncGenerator_prototype_aux(txMachine* the, txFlag status);
55
56
static txSlot* fxCheckAsyncFromSyncIteratorInstance(txMachine* the, txSlot* slot);
57
static void fx_AsyncFromSyncIterator_prototype_aux(txMachine* the, txFlag status, txSlot* iterator, txBoolean* flag);
58
59
void fxBuildGenerator(txMachine* the)
60
6.39k
{
61
6.39k
  txSlot* slot;
62
6.39k
  txSlot* property;
63
64
6.39k
  mxPush(mxIteratorPrototype);
65
6.39k
  slot = fxLastProperty(the, the->stack->value.reference);
66
6.39k
#if mxECMAScript2025
67
6.39k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Iterator_prototype_drop), 1, mxID(_drop), XS_DONT_ENUM_FLAG);
68
6.39k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Iterator_prototype_every), 1, mxID(_every), XS_DONT_ENUM_FLAG);
69
6.39k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Iterator_prototype_filter), 1, mxID(_filter), XS_DONT_ENUM_FLAG);
70
6.39k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Iterator_prototype_find), 1, mxID(_find), XS_DONT_ENUM_FLAG);
71
6.39k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Iterator_prototype_flatMap), 1, mxID(_flatMap), XS_DONT_ENUM_FLAG);
72
6.39k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Iterator_prototype_forEach), 1, mxID(_forEach), XS_DONT_ENUM_FLAG);
73
6.39k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Iterator_prototype_map), 1, mxID(_map), XS_DONT_ENUM_FLAG);
74
6.39k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Iterator_prototype_reduce), 1, mxID(_reduce), XS_DONT_ENUM_FLAG);
75
6.39k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Iterator_prototype_some), 1, mxID(_some), XS_DONT_ENUM_FLAG);
76
6.39k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Iterator_prototype_take), 1, mxID(_take), XS_DONT_ENUM_FLAG);
77
6.39k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Iterator_prototype_toArray), 0, mxID(_toArray), XS_DONT_ENUM_FLAG);
78
6.39k
  slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_Iterator_prototype_toStringTag_get), mxCallback(fx_Iterator_prototype_toStringTag_set), mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG);
79
6.39k
#endif
80
6.39k
#if mxExplicitResourceManagement
81
6.39k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Iterator_prototype_dispose), 0, mxID(_Symbol_dispose), XS_DONT_ENUM_FLAG);
82
6.39k
#endif
83
6.39k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Iterator_prototype_iterator), 0, mxID(_Symbol_iterator), XS_DONT_ENUM_FLAG);
84
6.39k
#if mxECMAScript2025
85
6.39k
  property = slot;
86
6.39k
  slot = fxBuildHostConstructor(the, mxCallback(fx_Iterator), 0, mxID(_Iterator));
87
6.39k
  mxIteratorConstructor = *the->stack;
88
6.39k
  slot = fxLastProperty(the, slot);
89
6.39k
#if mxECMAScript2026
90
6.39k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Iterator_concat), 0, mxID(_concat), XS_DONT_ENUM_FLAG);
91
6.39k
#endif
92
6.39k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Iterator_from), 1, mxID(_from), XS_DONT_ENUM_FLAG);
93
  
94
6.39k
  mxPush(mxIteratorPrototype);
95
6.39k
  property = fxNextHostAccessorProperty(the, property, mxCallback(fx_Iterator_prototype_constructor_get), mxCallback(fx_Iterator_prototype_constructor_set), mxID(_constructor), XS_DONT_ENUM_FLAG);
96
6.39k
  mxPop();
97
  
98
6.39k
  mxPush(mxIteratorPrototype);
99
6.39k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
100
6.39k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_IteratorHelper_prototype_next), 1, mxID(_next), XS_DONT_ENUM_FLAG);
101
6.39k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_IteratorHelper_prototype_return), 1, mxID(_return), XS_DONT_ENUM_FLAG);
102
6.39k
  slot = fxNextStringXProperty(the, slot, "Iterator Helper", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
103
6.39k
  mxPull(mxIteratorHelperPrototype);
104
  
105
6.39k
  mxPush(mxIteratorPrototype);
106
6.39k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
107
6.39k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_IteratorWrapper_prototype_next), 1, mxID(_next), XS_DONT_ENUM_FLAG);
108
6.39k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_IteratorWrapper_prototype_return), 1, mxID(_return), XS_DONT_ENUM_FLAG);
109
6.39k
  mxPull(mxIteratorWrapperPrototype);
110
111
6.39k
#endif
112
113
6.39k
  mxPush(mxIteratorPrototype);
114
6.39k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
115
6.39k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Generator_prototype_next), 1, mxID(_next), XS_DONT_ENUM_FLAG);
116
6.39k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Generator_prototype_return), 1, mxID(_return), XS_DONT_ENUM_FLAG);
117
6.39k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Generator_prototype_throw), 1, mxID(_throw), XS_DONT_ENUM_FLAG);
118
6.39k
  slot = fxNextStringXProperty(the, slot, "Generator", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
119
6.39k
  mxGeneratorPrototype = *the->stack;
120
6.39k
  mxPop();
121
  
122
6.39k
  mxPush(mxFunctionPrototype);
123
6.39k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
124
6.39k
  slot = fxNextSlotProperty(the, slot, &mxGeneratorPrototype, mxID(_prototype), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
125
6.39k
  property = mxBehaviorSetProperty(the, mxGeneratorPrototype.value.reference, mxID(_constructor), 0, XS_OWN);
126
6.39k
  property->flag = XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG;
127
6.39k
  property->kind = the->stack->kind;
128
6.39k
  property->value = the->stack->value;
129
6.39k
  slot = fxNextStringXProperty(the, slot, "GeneratorFunction", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
130
6.39k
  mxGeneratorFunctionPrototype = *the->stack;
131
6.39k
  slot = fxBuildHostConstructor(the, mxCallback(fx_GeneratorFunction), 1, mxID(_GeneratorFunction));
132
6.39k
  slot = mxBehaviorGetProperty(the, mxGeneratorFunctionPrototype.value.reference, mxID(_constructor), 0, XS_OWN);
133
6.39k
  slot->flag |= XS_DONT_SET_FLAG;
134
6.39k
  mxPop();
135
  
136
6.39k
  mxPush(mxIteratorPrototype);
137
6.39k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
138
6.39k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Enumerator_prototype_next), 0, mxID(_next), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG);
139
6.39k
  fxNewHostConstructor(the, mxCallback(fx_Enumerator), 0, XS_NO_ID);
140
6.39k
  mxPull(mxEnumeratorFunction);
141
142
6.39k
  mxPush(mxObjectPrototype);
143
6.39k
  slot = fxNewObjectInstance(the);
144
6.39k
#if mxExplicitResourceManagement
145
6.39k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_AsyncIterator_prototype_asyncDispose), 0, mxID(_Symbol_asyncDispose), XS_DONT_ENUM_FLAG);
146
6.39k
#endif
147
6.39k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_AsyncIterator_prototype_asyncIterator), 0, mxID(_Symbol_asyncIterator), XS_DONT_ENUM_FLAG);
148
6.39k
  mxPull(mxAsyncIteratorPrototype);
149
  
150
6.39k
  mxPush(mxAsyncIteratorPrototype);
151
6.39k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
152
6.39k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_AsyncGenerator_prototype_next), 1, mxID(_next), XS_DONT_ENUM_FLAG);
153
6.39k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_AsyncGenerator_prototype_return), 1, mxID(_return), XS_DONT_ENUM_FLAG);
154
6.39k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_AsyncGenerator_prototype_throw), 1, mxID(_throw), XS_DONT_ENUM_FLAG);
155
6.39k
  slot = fxNextStringXProperty(the, slot, "AsyncGenerator", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
156
6.39k
  mxAsyncGeneratorPrototype = *the->stack;
157
6.39k
  mxPop();
158
159
6.39k
  mxPush(mxFunctionPrototype);
160
6.39k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
161
6.39k
  slot = fxNextSlotProperty(the, slot, &mxAsyncGeneratorPrototype, mxID(_prototype), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
162
6.39k
  property = mxBehaviorSetProperty(the, mxAsyncGeneratorPrototype.value.reference, mxID(_constructor), 0, XS_OWN);
163
6.39k
  property->flag = XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG;
164
6.39k
  property->kind = the->stack->kind;
165
6.39k
  property->value = the->stack->value;
166
6.39k
  slot = fxNextStringXProperty(the, slot, "AsyncGeneratorFunction", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
167
6.39k
  mxAsyncGeneratorFunctionPrototype = *the->stack;
168
6.39k
  slot = fxBuildHostConstructor(the, mxCallback(fx_AsyncGeneratorFunction), 1, mxID(_AsyncGeneratorFunction));
169
6.39k
  slot = mxBehaviorGetProperty(the, mxAsyncGeneratorFunctionPrototype.value.reference, mxID(_constructor), 0, XS_OWN);
170
6.39k
  slot->flag |= XS_DONT_SET_FLAG;
171
6.39k
  mxPop();
172
  
173
6.39k
  mxPush(mxAsyncIteratorPrototype);
174
6.39k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
175
6.39k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_AsyncFromSyncIterator_prototype_next), 1, mxID(_next), XS_DONT_ENUM_FLAG);
176
6.39k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_AsyncFromSyncIterator_prototype_return), 1, mxID(_return), XS_DONT_ENUM_FLAG);
177
6.39k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_AsyncFromSyncIterator_prototype_throw), 1, mxID(_throw), XS_DONT_ENUM_FLAG);
178
6.39k
  slot = fxNextStringXProperty(the, slot, "Async-from-Sync Iterator", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
179
6.39k
  mxAsyncFromSyncIteratorPrototype = *the->stack;
180
6.39k
  mxPop();
181
6.39k
}
182
183
typedef txBoolean (*txIteratorStep)(txMachine* the, txSlot* iterator, txSlot* next, txSlot* extra, txSlot* value, txFlag status);
184
185
enum {
186
  mx_Iterator_prototype_drop = 0,
187
  mx_Iterator_prototype_filter,
188
  mx_Iterator_prototype_flatMap,
189
  mx_Iterator_prototype_map,
190
  mx_Iterator_prototype_take,
191
  mx_Iterator_concat,
192
  mxIteratorStepCount
193
};
194
195
static txBoolean fx_Iterator_prototype_drop_step(txMachine* the, txSlot* iterator, txSlot* next, txSlot* extra, txSlot* value, txFlag status);
196
static txBoolean fx_Iterator_prototype_filter_step(txMachine* the, txSlot* iterator, txSlot* next, txSlot* extra, txSlot* value, txFlag status);
197
static txBoolean fx_Iterator_prototype_flatMap_step(txMachine* the, txSlot* iterator, txSlot* next, txSlot* extra, txSlot* value, txFlag status);
198
static txBoolean fx_Iterator_prototype_map_step(txMachine* the, txSlot* iterator, txSlot* next, txSlot* extra, txSlot* value, txFlag status);
199
static txBoolean fx_Iterator_prototype_take_step(txMachine* the, txSlot* iterator, txSlot* next, txSlot* extra, txSlot* value, txFlag status);
200
static txBoolean fx_Iterator_concat_step(txMachine* the, txSlot* iterator, txSlot* next, txSlot* extra, txSlot* value, txFlag status);
201
202
const txIteratorStep ICACHE_FLASH_ATTR gxIteratorSteps[mxIteratorStepCount]  = {
203
  fx_Iterator_prototype_drop_step,
204
  fx_Iterator_prototype_filter_step,
205
  fx_Iterator_prototype_flatMap_step,
206
  fx_Iterator_prototype_map_step,
207
  fx_Iterator_prototype_take_step,
208
  fx_Iterator_concat_step,
209
};
210
211
const txID ICACHE_FLASH_ATTR gxIteratorStepIDs[mxIteratorStepCount]  = {
212
  mxID(_drop),
213
  mxID(_filter),
214
  mxID(_flatMap),
215
  mxID(_map),
216
  mxID(_take),
217
};
218
219
txSlot* fxCheckIteratorInstance(txMachine* the, txSlot* slot, txID id)
220
0
{
221
0
  txSlot* instance;
222
0
  if (slot->kind == XS_REFERENCE_KIND) {
223
0
    instance = slot->value.reference;
224
0
    slot = instance->next;
225
0
    if (slot && (slot->flag & XS_INTERNAL_FLAG) && (slot->ID == id) && (slot->kind == XS_REFERENCE_KIND)) {
226
0
      return instance;
227
0
    }
228
0
  }
229
0
  mxTypeError("this: not an iterator");
230
0
  return C_NULL;
231
0
}
232
233
txSlot* fxCheckIteratorResult(txMachine* the, txSlot* result) 
234
0
{
235
0
  txSlot* value = result->value.reference->next;
236
0
  while (value && (value->flag & XS_INTERNAL_FLAG))
237
0
    value = value->next;
238
0
  mxCheck(the, (value != C_NULL) && (value->ID == mxID(_value)));
239
0
  mxCheck(the, (value->next != C_NULL) && (value->next->ID == mxID(_done)));
240
0
  return value;
241
0
}
242
243
txSlot* fxCheckIteratorStep(txMachine* the, txSlot* slot) 
244
0
{
245
0
  if (slot && (slot->kind == XS_INTEGER_KIND)) {
246
0
    txInteger step = slot->value.integer;
247
0
    if ((0 <= step) && (step < mxIteratorStepCount)) {
248
0
      if (slot->ID == gxIteratorStepIDs[step]) {
249
0
        return slot;
250
0
      }
251
0
    }
252
0
  }
253
0
  mxTypeError("this: not an iterator helper");
254
0
  return C_NULL;
255
0
}
256
257
txBoolean fxIteratorNext(txMachine* the, txSlot* iterator, txSlot* next, txSlot* value)
258
0
{
259
0
  mxPushSlot(iterator);
260
0
  mxPushSlot(next);
261
0
  mxCall();
262
0
  mxRunCount(0);
263
0
  if (!mxIsReference(the->stack))
264
0
    mxTypeError("iterator result: not an object");
265
0
  mxDub();
266
0
  mxGetID(mxID(_done));
267
0
  if (fxToBoolean(the, the->stack)) {
268
0
    mxPop();
269
0
    mxPop();
270
0
    return 0;
271
0
  }
272
0
  mxPop();
273
0
  mxGetID(mxID(_value));
274
0
  mxPullSlot(value);
275
0
  return 1;
276
0
}
277
278
void fxIteratorReturn(txMachine* the, txSlot* iterator, txBoolean abrupt)
279
0
{
280
0
  if (abrupt)
281
0
    mxPush(mxException);
282
0
  mxTry(the) {
283
0
    mxPushSlot(iterator);
284
0
    mxDub();
285
0
    mxGetID(mxID(_return));
286
0
    if (mxIsUndefined(the->stack) || mxIsNull(the->stack)) 
287
0
      mxPop();
288
0
    else {
289
0
      mxCall();
290
0
      mxRunCount(0);
291
0
      if (!mxIsReference(the->stack))
292
0
        mxTypeError("iterator result: not an object");
293
0
    }
294
0
    mxPop();
295
0
  }
296
0
  mxCatch(the) {
297
0
    if (!abrupt)
298
0
      fxJump(the);
299
0
  }
300
0
  if (abrupt)
301
0
    mxPull(mxException);
302
0
}
303
304
txBoolean fxGetIterator(txMachine* the, txSlot* iterable, txSlot* iterator, txSlot* next, txBoolean optional)
305
0
{
306
0
  mxPushSlot(iterable);
307
0
  mxDub();
308
0
  mxGetID(mxID(_Symbol_iterator));
309
0
  if (optional && (mxIsUndefined(the->stack) || mxIsNull(the->stack))) {
310
0
    mxPop();
311
0
    mxPop();
312
0
    return 0;
313
0
  }
314
0
  mxCall();
315
0
  mxRunCount(0);
316
0
  if (!mxIsReference(the->stack))
317
0
    mxTypeError("iterator: not an object");
318
0
  if (next) {
319
0
    mxDub();
320
0
    mxGetID(mxID(_next));
321
0
    mxPullSlot(next);
322
0
  }
323
0
  mxPullSlot(iterator);
324
0
  return 1;
325
0
}
326
327
txBoolean fxGetIteratorFlattenable(txMachine* the, txSlot* iterable, txSlot* iterator, txSlot* next, txBoolean optional)
328
0
{
329
0
  if (!mxIsReference(iterable)) {
330
0
    if (optional) {
331
0
      if ((iterable->kind != XS_STRING_KIND) && (iterable->kind != XS_STRING_X_KIND))
332
0
        mxTypeError("iterator: not a string");
333
0
    }
334
0
    else
335
0
      mxTypeError("iterator: not an object");
336
0
  }
337
0
  mxPushSlot(iterable);
338
0
  mxDub();
339
0
  mxGetID(mxID(_Symbol_iterator));
340
0
  if ((mxIsUndefined(the->stack) || mxIsNull(the->stack)))
341
0
    mxPop();
342
0
  else {
343
0
    mxCall();
344
0
    mxRunCount(0);
345
0
  }
346
0
  if (!mxIsReference(the->stack))
347
0
    mxTypeError("iterator: not an object");
348
0
  mxDub();
349
0
  mxGetID(mxID(_next));
350
0
  mxPullSlot(next);
351
0
  mxPullSlot(iterator);
352
0
  return 1;
353
0
}
354
355
txSlot* fxNewIteratorInstance(txMachine* the, txSlot* iterable, txID id) 
356
0
{
357
0
  txSlot* instance;
358
0
  txSlot* result;
359
0
  txSlot* property;
360
0
  instance = fxNewObjectInstance(the);
361
0
  mxPush(mxObjectPrototype);
362
0
  result = fxNewObjectInstance(the);
363
0
  property = fxNextUndefinedProperty(the, result, mxID(_value), XS_DONT_DELETE_FLAG | XS_DONT_SET_FLAG);
364
0
  property = fxNextBooleanProperty(the, property, 0, mxID(_done), XS_DONT_DELETE_FLAG | XS_DONT_SET_FLAG);
365
0
  property = fxNextSlotProperty(the, instance, the->stack, id, XS_INTERNAL_FLAG);
366
0
  property = fxNextSlotProperty(the, property, iterable, XS_NO_ID, XS_INTERNAL_FLAG);
367
0
  property = fxNextIntegerProperty(the, property, 0, XS_NO_ID, XS_INTERNAL_FLAG);
368
0
  mxPop();
369
0
  return instance;
370
0
}
371
372
void fxSetterThatIgnoresPrototypeProperties(txMachine* the, txSlot* reference, txID id, txString name, txSlot* value)
373
0
{
374
0
  txSlot* instance = fxGetInstance(the, reference);
375
0
  txSlot* home = mxFunctionInstanceHome(mxFunction->value.reference);
376
0
  txSlot* property;
377
0
  if (!instance)
378
0
    mxTypeError("set %s: not an object", name);
379
0
  if (home->value.home.object == instance)
380
0
    mxTypeError("set %s: not writable", name);
381
0
  mxTemporary(property);
382
0
  if (!mxBehaviorGetOwnProperty(the, instance, id, 0, property)) {
383
0
    if (!mxBehaviorDefineOwnProperty(the, instance, id, 0, value, XS_GET_ONLY))
384
0
      mxTypeError("set %s: not extensible", name);
385
0
  }
386
0
  else /*if (!mxIsProxy(instance))*/ {
387
0
    property = mxBehaviorSetProperty(the, instance, id, 0, XS_OWN);
388
0
    if (!property)
389
0
      mxTypeError("set %s: not writable", name);
390
0
        if (property->kind == XS_ACCESSOR_KIND) {
391
0
            txSlot* function = property->value.accessor.setter;
392
0
            if (!mxIsFunction(function))
393
0
                mxTypeError("set %s: not writable", name);
394
0
            mxPushSlot(reference);
395
0
            mxPushReference(function);
396
0
            mxCall();
397
0
            mxPushSlot(value);
398
0
            mxRunCount(1);
399
0
            mxPop();
400
0
       }
401
0
        else {
402
0
            property->kind = value->kind;
403
0
            property->value = value->value;
404
0
        }
405
0
  }
406
0
  mxPop();
407
0
}
408
409
void fx_Iterator(txMachine* the)
410
0
{
411
0
  if (!mxHasTarget)
412
0
    mxTypeError("call: Iterator");
413
0
  if (fxIsSameSlot(the, mxTarget, mxFunction))
414
0
    mxTypeError("new: Iterator");
415
0
  mxPushSlot(mxTarget);
416
0
  fxGetPrototypeFromConstructor(the, &mxIteratorPrototype);
417
0
  fxNewObjectInstance(the);
418
0
  mxPullSlot(mxResult);
419
0
}
420
421
void fx_Iterator_concat(txMachine* the)
422
0
{
423
0
  txSlot* list = fxNewInstance(the);
424
0
  txSlot* slot = list;
425
0
  txSlot* property;
426
0
  txInteger c = mxArgc, i;
427
0
  for (i = 0; i < c; i++) {
428
0
    txSlot* item = mxArgv(i);
429
0
    if (!mxIsReference(item))
430
0
      mxTypeError("items[%d]: not an object", i);
431
0
    mxPushSlot(item);
432
0
    slot = fxNextSlotProperty(the, slot, the->stack, XS_NO_ID, XS_NO_FLAG);
433
0
    mxGetID(mxID(_Symbol_iterator));
434
0
    if (!fxIsCallable(the, the->stack))
435
0
      mxTypeError("items[%d]: not iterable", i);
436
0
    slot = fxNextSlotProperty(the, slot, the->stack, XS_NO_ID, XS_NO_FLAG);
437
0
    mxPop();
438
0
  }
439
0
  mxPushNull();
440
0
  property = fxLastProperty(the, fxNewIteratorHelperInstance(the, the->stack, mx_Iterator_concat));
441
0
  property = fxNextReferenceProperty(the, property, list, XS_NO_ID, XS_INTERNAL_FLAG);
442
0
  mxPullSlot(mxResult);
443
0
  mxPop();
444
0
  mxPop();
445
0
}
446
447
txBoolean fx_Iterator_concat_step(txMachine* the, txSlot* iterator, txSlot* next, txSlot* extra, txSlot* value, txFlag status)
448
0
{
449
0
  if (status == XS_NO_STATUS) {
450
0
    for (;;) {
451
0
      if (mxIsNull(iterator)) {
452
0
        txSlot* iterable = extra->value.reference->next;
453
0
        if (!iterable)
454
0
          break;
455
0
        mxPushSlot(iterable);
456
0
        mxPushSlot(iterable->next);
457
0
        extra->value.reference->next = iterable->next->next;
458
0
        mxCall();
459
0
        mxRunCount(0);
460
0
        if (!mxIsReference(the->stack))
461
0
          mxTypeError("iterator: not an object");
462
0
        mxDub();
463
0
        mxGetID(mxID(_next));
464
0
        mxPullSlot(next);
465
0
        mxPullSlot(iterator);
466
0
      }
467
0
      if (fxIteratorNext(the, iterator, next, value))
468
0
        return 1;
469
0
      next->kind = XS_NULL_KIND;
470
0
      iterator->kind = XS_NULL_KIND;
471
0
    }
472
0
  }
473
0
  else if (status == XS_RETURN_STATUS) {
474
0
    if (!mxIsNull(iterator))
475
0
      fxIteratorReturn(the, iterator, 0);
476
0
  }
477
0
  else if (status == XS_THROW_STATUS) {
478
0
    if (!mxIsNull(iterator))
479
0
      fxIteratorReturn(the, iterator, 1);
480
0
  }
481
0
  return 0;
482
0
}
483
484
void fx_Iterator_from(txMachine* the)
485
0
{
486
0
  txSlot *iterator, *next, *property;
487
0
  mxTemporary(iterator);
488
0
  mxTemporary(next);
489
0
  fxGetIteratorFlattenable(the, mxArgv(0), iterator, next, 1);
490
0
  mxPush(mxIteratorConstructor);
491
0
  mxDub();
492
0
  mxGetID(mxID(_Symbol_hasInstance));
493
0
  mxCall();
494
0
  mxPushSlot(iterator);
495
0
  mxRunCount(1);
496
0
  if (fxToBoolean(the, the->stack)) {
497
0
    mxResult->kind = iterator->kind;
498
0
    mxResult->value = iterator->value;
499
0
  }
500
0
  else {
501
0
    mxPush(mxIteratorWrapperPrototype);
502
0
    property = fxLastProperty(the, fxNewIteratorInstance(the, iterator, mxID(_Iterator)));
503
0
    property = fxNextSlotProperty(the, property, next, XS_NO_ID, XS_INTERNAL_FLAG);
504
0
    mxPullSlot(mxResult);
505
0
  }
506
0
  mxPop();
507
0
  mxPop();
508
0
  mxPop();
509
0
}
510
511
void fx_Iterator_prototype_constructor_get(txMachine* the)
512
0
{
513
0
  mxPush(mxIteratorConstructor);
514
0
  mxPullSlot(mxResult);
515
0
}
516
517
void fx_Iterator_prototype_constructor_set(txMachine* the)
518
0
{
519
0
  fxSetterThatIgnoresPrototypeProperties(the, mxThis, mxID(_constructor), "constructor", mxArgv(0));
520
0
}
521
522
void fx_Iterator_prototype_dispose(txMachine* the)
523
0
{ 
524
0
  fxIteratorReturn(the, mxThis, 1);
525
0
}
526
527
void fx_Iterator_prototype_drop(txMachine* the)
528
0
{
529
0
  txSlot *iterator, *property;
530
0
  txNumber limit;
531
0
  if (!fxGetInstance(the, mxThis))
532
0
    mxTypeError("this: not an object");
533
0
  iterator = mxThis;
534
0
  {
535
0
    mxTry(the) {
536
0
      if (mxArgc > 0)
537
0
        mxPushSlot(mxArgv(0));
538
0
      else
539
0
        mxPushUndefined();
540
0
      limit = fxToNumber(the, the->stack);
541
0
      if (c_isnan(limit))
542
0
        mxRangeError("limit: NaN");
543
0
      if (c_isfinite(limit))
544
0
        limit = c_trunc(limit);
545
0
      if (limit < 0)
546
0
        mxRangeError("limit: < 0");
547
0
    }
548
0
    mxCatch(the) {
549
0
      fxIteratorReturn(the, iterator, 1);
550
0
      fxJump(the);
551
0
    }
552
0
  }
553
0
  property = fxLastProperty(the, fxNewIteratorHelperInstance(the, iterator, mx_Iterator_prototype_drop));
554
0
  property = fxNextNumberProperty(the, property, limit, XS_NO_ID, XS_INTERNAL_FLAG);
555
0
  mxPullSlot(mxResult);
556
0
  mxPop();
557
0
}
558
559
txBoolean fx_Iterator_prototype_drop_step(txMachine* the, txSlot* iterator, txSlot* next, txSlot* extra, txSlot* value, txFlag status)
560
0
{
561
0
  txNumber remaining = extra->value.number;
562
0
  if (status == XS_NO_STATUS) {
563
0
    while (remaining > 0) {
564
0
      if (fxIteratorNext(the, iterator, next, value)) {
565
0
        if (c_isfinite(remaining)) {
566
0
          remaining--;
567
0
          extra->value.number = remaining;
568
0
        }
569
0
      }
570
0
      else
571
0
        return 0;
572
0
    }
573
0
    if (fxIteratorNext(the, iterator, next, value))
574
0
      return 1;
575
0
  }
576
0
  else if (status == XS_RETURN_STATUS)
577
0
    fxIteratorReturn(the, iterator, 0);
578
0
  else if (status == XS_THROW_STATUS)
579
0
    fxIteratorReturn(the, iterator, 1);
580
0
  return 0;
581
0
}
582
583
void fx_Iterator_prototype_every(txMachine* the)
584
0
{ 
585
0
  txSlot *iterator, *predicate, *next, *value;
586
0
  txInteger counter;
587
0
  if (!fxGetInstance(the, mxThis))
588
0
    mxTypeError("this: not an object");
589
0
  iterator = mxThis;
590
0
  {
591
0
    mxTry(the) {
592
0
      if (mxArgc > 0)
593
0
        mxPushSlot(mxArgv(0));
594
0
      else
595
0
        mxPushUndefined();
596
0
      predicate = the->stack;
597
0
      if (!fxIsCallable(the, predicate))
598
0
        mxTypeError("predicate: not a function");
599
0
    }
600
0
    mxCatch(the) {
601
0
      fxIteratorReturn(the, iterator, 1);
602
0
      fxJump(the);
603
0
    }
604
0
  }
605
0
  mxPushSlot(iterator);
606
0
  mxGetID(mxID(_next));
607
0
  next = the->stack;
608
0
  mxTemporary(value);
609
0
  counter = 0;
610
0
  mxResult->kind = XS_BOOLEAN_KIND;
611
0
  mxResult->value.boolean = 1;
612
0
  {
613
0
    while (fxIteratorNext(the, iterator, next, value)) {
614
0
      mxTry(the) {
615
0
        mxPushUndefined();
616
0
        mxPushSlot(predicate);
617
0
        mxCall();
618
0
        mxPushSlot(value);
619
0
        mxPushInteger(counter);
620
0
        mxRunCount(2);
621
0
        mxResult->value.boolean = fxToBoolean(the, the->stack);
622
0
        mxPop();
623
0
      }
624
0
      mxCatch(the) {
625
0
        fxIteratorReturn(the, iterator, 1);
626
0
        fxJump(the);
627
0
      }
628
0
      if (!mxResult->value.boolean) {
629
0
        fxIteratorReturn(the, iterator, 0);
630
0
        break;
631
0
      }
632
0
      counter++;
633
0
    }
634
0
  }
635
0
  mxPop();
636
0
  mxPop();
637
0
  mxPop();
638
0
}
639
640
void fx_Iterator_prototype_filter(txMachine* the)
641
0
{ 
642
0
  txSlot *iterator, *predicate, *property;
643
0
  if (!fxGetInstance(the, mxThis))
644
0
    mxTypeError("this: not an object");
645
0
  iterator = mxThis;
646
0
  {
647
0
    mxTry(the) {
648
0
      if (mxArgc > 0)
649
0
        mxPushSlot(mxArgv(0));
650
0
      else
651
0
        mxPushUndefined();
652
0
      predicate = the->stack;
653
0
      if (!fxIsCallable(the, predicate))
654
0
        mxTypeError("predicate: not a function");
655
0
    }
656
0
    mxCatch(the) {
657
0
      fxIteratorReturn(the, iterator, 1);
658
0
      fxJump(the);
659
0
    }
660
0
  }
661
0
  property = fxLastProperty(the, fxNewIteratorHelperInstance(the, iterator, mx_Iterator_prototype_filter));
662
0
  property = fxNextSlotProperty(the, property, predicate, XS_NO_ID, XS_INTERNAL_FLAG);
663
0
  property = fxNextIntegerProperty(the, property, 0, XS_NO_ID, XS_INTERNAL_FLAG);
664
0
  mxPullSlot(mxResult);
665
0
  mxPop();
666
0
}
667
668
txBoolean fx_Iterator_prototype_filter_step(txMachine* the, txSlot* iterator, txSlot* next, txSlot* extra, txSlot* value, txFlag status)
669
0
{
670
0
  txSlot* counter = extra->next;
671
0
  txBoolean selected = 0;
672
0
  if (status == XS_NO_STATUS) {
673
0
    while (fxIteratorNext(the, iterator, next, value)) {
674
0
      mxTry(the) {
675
0
        mxPushUndefined();
676
0
        mxPushSlot(extra);
677
0
        mxCall();
678
0
        mxPushSlot(value);
679
0
        mxPushSlot(counter);
680
0
        mxRunCount(2);
681
0
        selected = fxToBoolean(the, the->stack);
682
0
        mxPop();
683
0
        counter->value.integer++;
684
0
      }
685
0
      mxCatch(the) {
686
0
        fxIteratorReturn(the, iterator, 1);
687
0
        fxJump(the);
688
0
      }
689
0
      if (selected)
690
0
        return 1;
691
0
    }
692
0
  }
693
0
  else if (status == XS_RETURN_STATUS)
694
0
    fxIteratorReturn(the, iterator, 0);
695
0
  else if (status == XS_THROW_STATUS)
696
0
    fxIteratorReturn(the, iterator, 1);
697
0
  return 0;
698
0
}
699
700
void fx_Iterator_prototype_find(txMachine* the)
701
0
{ 
702
0
  txSlot *iterator, *predicate, *next, *value;
703
0
  txInteger counter;
704
0
  txBoolean result;
705
0
  if (!fxGetInstance(the, mxThis))
706
0
    mxTypeError("this: not an object");
707
0
  iterator = mxThis;
708
0
  {
709
0
    mxTry(the) {
710
0
      if (mxArgc > 0)
711
0
        mxPushSlot(mxArgv(0));
712
0
      else
713
0
        mxPushUndefined();
714
0
      predicate = the->stack;
715
0
      if (!fxIsCallable(the, predicate))
716
0
        mxTypeError("predicate: not a function");
717
0
    }
718
0
    mxCatch(the) {
719
0
      fxIteratorReturn(the, iterator, 1);
720
0
      fxJump(the);
721
0
    }
722
0
  }
723
0
  mxPushSlot(iterator);
724
0
  mxGetID(mxID(_next));
725
0
  next = the->stack;
726
0
  mxTemporary(value);
727
0
  counter = 0;
728
0
  {
729
0
    while (fxIteratorNext(the, iterator, next, value)) {
730
0
      mxTry(the) {
731
0
        mxPushUndefined();
732
0
        mxPushSlot(predicate);
733
0
        mxCall();
734
0
        mxPushSlot(value);
735
0
        mxPushInteger(counter);
736
0
        mxRunCount(2);
737
0
        result = fxToBoolean(the, the->stack);
738
0
        mxPop();
739
0
      }
740
0
      mxCatch(the) {
741
0
        fxIteratorReturn(the, iterator, 1);
742
0
        fxJump(the);
743
0
      }
744
0
      if (result) {
745
0
        mxResult->kind = value->kind;
746
0
        mxResult->value = value->value;
747
0
        fxIteratorReturn(the, iterator, 0);
748
0
        break;
749
0
      }
750
0
      counter++;
751
0
    }
752
0
  }
753
0
  mxPop();
754
0
  mxPop();
755
0
  mxPop();
756
0
}
757
758
void fx_Iterator_prototype_flatMap(txMachine* the)
759
0
{ 
760
0
  txSlot *iterator, *mapper, *property;
761
0
  if (!fxGetInstance(the, mxThis))
762
0
    mxTypeError("this: not an object");
763
0
  iterator = mxThis;
764
0
  {
765
0
    mxTry(the) {
766
0
      if (mxArgc > 0)
767
0
        mxPushSlot(mxArgv(0));
768
0
      else
769
0
        mxPushUndefined();
770
0
      mapper = the->stack;
771
0
      if (!fxIsCallable(the, mapper))
772
0
        mxTypeError("mapper: not a function");
773
0
    }
774
0
    mxCatch(the) {
775
0
      fxIteratorReturn(the, iterator, 1);
776
0
      fxJump(the);
777
0
    }
778
0
  }
779
0
  property = fxLastProperty(the, fxNewIteratorHelperInstance(the, iterator, mx_Iterator_prototype_flatMap));
780
0
  property = fxNextSlotProperty(the, property, mapper, XS_NO_ID, XS_INTERNAL_FLAG);
781
0
  property = fxNextIntegerProperty(the, property, 0, XS_NO_ID, XS_INTERNAL_FLAG);
782
0
  property = fxNextNullProperty(the, property, XS_NO_ID, XS_INTERNAL_FLAG);
783
0
  property = fxNextNullProperty(the, property, XS_NO_ID, XS_INTERNAL_FLAG);
784
0
  mxPullSlot(mxResult);
785
0
  mxPop();
786
0
}
787
788
txBoolean fx_Iterator_prototype_flatMap_step(txMachine* the, txSlot* iterator, txSlot* next, txSlot* extra, txSlot* value, txFlag status)
789
0
{
790
0
  txSlot* counter = extra->next;
791
0
  txSlot* innerIterator = counter->next;
792
0
  txSlot* innerNext = innerIterator->next;
793
0
  if (status == XS_NO_STATUS) {
794
0
  again:
795
0
    if (mxIsNull(innerIterator)) {
796
0
      if (fxIteratorNext(the, iterator, next, value)) {
797
0
        mxTry(the) {
798
0
          mxPushUndefined();
799
0
          mxPushSlot(extra);
800
0
          mxCall();
801
0
          mxPushSlot(value);
802
0
          mxPushSlot(counter);
803
0
          mxRunCount(2);
804
0
          fxGetIteratorFlattenable(the, the->stack, innerIterator, innerNext, 0);
805
0
          mxPop();
806
0
        }
807
0
        mxCatch(the) {
808
0
          fxIteratorReturn(the, iterator, 1);
809
0
          fxJump(the);
810
0
        }
811
0
        counter->value.integer++;
812
0
      }
813
0
      else
814
0
        return 0;
815
0
    }
816
0
    if (fxIteratorNext(the, innerIterator, innerNext, value))
817
0
      return 1;
818
0
    innerIterator->kind = XS_NULL_KIND;
819
0
    innerNext->kind = XS_NULL_KIND;
820
0
    goto again;
821
0
  }
822
0
  else if (status == XS_RETURN_STATUS) {
823
0
    if (!mxIsNull(innerIterator))
824
0
      fxIteratorReturn(the, innerIterator, 0);
825
0
    fxIteratorReturn(the, iterator, 0);
826
0
  }
827
0
  else if (status == XS_THROW_STATUS) {
828
0
    if (!mxIsNull(innerIterator))
829
0
      fxIteratorReturn(the, innerIterator, 1);
830
0
    fxIteratorReturn(the, iterator, 1);
831
0
  }
832
0
  return 0;
833
0
}
834
835
void fx_Iterator_prototype_forEach(txMachine* the)
836
0
{ 
837
0
  txSlot *iterator, *procedure, *next, *value;
838
0
  txInteger counter;
839
0
  if (!fxGetInstance(the, mxThis))
840
0
    mxTypeError("this: not an object");
841
0
  iterator = mxThis;
842
0
  {
843
0
    mxTry(the) {
844
0
      if (mxArgc > 0)
845
0
        mxPushSlot(mxArgv(0));
846
0
      else
847
0
        mxPushUndefined();
848
0
      procedure = the->stack;
849
0
      if (!fxIsCallable(the, procedure))
850
0
        mxTypeError("procedure: not a function");
851
0
    }
852
0
    mxCatch(the) {
853
0
      fxIteratorReturn(the, iterator, 1);
854
0
      fxJump(the);
855
0
    }
856
0
  }
857
0
  mxPushSlot(iterator);
858
0
  mxGetID(mxID(_next));
859
0
  next = the->stack;
860
0
  mxTemporary(value);
861
0
  counter = 0;
862
0
  {
863
0
    while (fxIteratorNext(the, iterator, next, value)) {
864
0
      mxTry(the) {
865
0
        mxPushUndefined();
866
0
        mxPushSlot(procedure);
867
0
        mxCall();
868
0
        mxPushSlot(value);
869
0
        mxPushInteger(counter);
870
0
        mxRunCount(2);
871
0
        mxPop();
872
0
      }
873
0
      mxCatch(the) {
874
0
        fxIteratorReturn(the, iterator, 1);
875
0
        fxJump(the);
876
0
      }
877
0
      counter++;
878
0
    }
879
0
  }
880
0
  mxPop();
881
0
  mxPop();
882
0
  mxPop();
883
0
}
884
885
void fx_Iterator_prototype_iterator(txMachine* the)
886
0
{
887
0
  *mxResult = *mxThis;
888
0
}
889
890
void fx_Iterator_prototype_map(txMachine* the)
891
0
{ 
892
0
  txSlot *iterator, *mapper, *property;
893
0
  if (!fxGetInstance(the, mxThis))
894
0
    mxTypeError("this: not an object");
895
0
  iterator = mxThis;
896
0
  {
897
0
    mxTry(the) {
898
0
      if (mxArgc > 0)
899
0
        mxPushSlot(mxArgv(0));
900
0
      else
901
0
        mxPushUndefined();
902
0
      mapper = the->stack;
903
0
      if (!fxIsCallable(the, mapper))
904
0
        mxTypeError("mapper: not a function");
905
0
    }
906
0
    mxCatch(the) {
907
0
      fxIteratorReturn(the, iterator, 1);
908
0
      fxJump(the);
909
0
    }
910
0
  }
911
0
  property = fxLastProperty(the, fxNewIteratorHelperInstance(the, iterator, mx_Iterator_prototype_map));
912
0
  property = fxNextSlotProperty(the, property, mapper, XS_NO_ID, XS_INTERNAL_FLAG);
913
0
  property = fxNextIntegerProperty(the, property, 0, XS_NO_ID, XS_INTERNAL_FLAG);
914
0
  mxPullSlot(mxResult);
915
0
  mxPop();
916
0
}
917
918
txBoolean fx_Iterator_prototype_map_step(txMachine* the, txSlot* iterator, txSlot* next, txSlot* extra, txSlot* value, txFlag status)
919
0
{
920
0
  txSlot* counter = extra->next;
921
0
  if (status == XS_NO_STATUS) {
922
0
    if (fxIteratorNext(the, iterator, next, value)) {
923
0
      mxTry(the) {
924
0
        mxPushUndefined();
925
0
        mxPushSlot(extra);
926
0
        mxCall();
927
0
        mxPushSlot(value);
928
0
        mxPushSlot(counter);
929
0
        mxRunCount(2);
930
0
        mxPullSlot(value);
931
0
        counter->value.integer++;
932
0
      }
933
0
      mxCatch(the) {
934
0
        fxIteratorReturn(the, iterator, 1);
935
0
        fxJump(the);
936
0
      }
937
0
      return 1;
938
0
    }
939
0
  }
940
0
  else if (status == XS_RETURN_STATUS)
941
0
    fxIteratorReturn(the, iterator, 0);
942
0
  else if (status == XS_THROW_STATUS)
943
0
    fxIteratorReturn(the, iterator, 1);
944
0
  return 0;
945
0
}
946
947
void fx_Iterator_prototype_reduce(txMachine* the)
948
0
{ 
949
0
  txSlot *iterator, *reducer, *next, *value;
950
0
  txInteger counter;
951
0
  if (!fxGetInstance(the, mxThis))
952
0
    mxTypeError("this: not an object");
953
0
  iterator = mxThis;
954
0
  {
955
0
    mxTry(the) {
956
0
      if (mxArgc > 0)
957
0
        mxPushSlot(mxArgv(0));
958
0
      else
959
0
        mxPushUndefined();
960
0
      reducer = the->stack;
961
0
      if (!fxIsCallable(the, reducer))
962
0
        mxTypeError("reducer: not a function");
963
0
    }
964
0
    mxCatch(the) {
965
0
      fxIteratorReturn(the, iterator, 1);
966
0
      fxJump(the);
967
0
    }
968
0
  }
969
0
  mxPushSlot(iterator);
970
0
  mxGetID(mxID(_next));
971
0
  next = the->stack;
972
0
  mxTemporary(value);
973
0
  if (mxArgc > 1) {
974
0
    mxPushSlot(mxArgv(1));
975
0
    counter = 0;
976
0
  }
977
0
  else {
978
0
    mxPushUndefined();
979
0
    if (!fxIteratorNext(the, iterator, next, the->stack))
980
0
      mxTypeError("no initial value");
981
0
    counter = 1;
982
0
  }
983
0
  mxPullSlot(mxResult);
984
0
  {
985
0
    while (fxIteratorNext(the, iterator, next, value)) {
986
0
      mxTry(the) {
987
0
        mxPushUndefined();
988
0
        mxPushSlot(reducer);
989
0
        mxCall();
990
0
        mxPushSlot(mxResult);
991
0
        mxPushSlot(value);
992
0
        mxPushInteger(counter);
993
0
        mxRunCount(3);
994
0
        mxPullSlot(mxResult);
995
0
        counter++;
996
0
      }
997
0
      mxCatch(the) {
998
0
        fxIteratorReturn(the, iterator, 1);
999
0
        fxJump(the);
1000
0
      }
1001
0
    }
1002
0
  }
1003
0
  mxPop();
1004
0
  mxPop();
1005
0
  mxPop();
1006
0
}
1007
1008
void fx_Iterator_prototype_some(txMachine* the)
1009
0
{ 
1010
0
  txSlot *iterator, *predicate, *next, *value;
1011
0
  txInteger counter;
1012
0
  if (!fxGetInstance(the, mxThis))
1013
0
    mxTypeError("this: not an object");
1014
0
  iterator = mxThis;
1015
0
  {
1016
0
    mxTry(the) {
1017
0
      if (mxArgc > 0)
1018
0
        mxPushSlot(mxArgv(0));
1019
0
      else
1020
0
        mxPushUndefined();
1021
0
      predicate = the->stack;
1022
0
      if (!fxIsCallable(the, predicate))
1023
0
        mxTypeError("predicate: not a function");
1024
0
    }
1025
0
    mxCatch(the) {
1026
0
      fxIteratorReturn(the, iterator, 1);
1027
0
      fxJump(the);
1028
0
    }
1029
0
  }
1030
0
  mxPushSlot(iterator);
1031
0
  mxGetID(mxID(_next));
1032
0
  next = the->stack;
1033
0
  mxTemporary(value);
1034
0
  counter = 0;
1035
0
  mxResult->kind = XS_BOOLEAN_KIND;
1036
0
  mxResult->value.boolean = 0;
1037
0
  {
1038
0
    while (fxIteratorNext(the, iterator, next, value)) {
1039
0
      mxTry(the) {
1040
0
        mxPushUndefined();
1041
0
        mxPushSlot(predicate);
1042
0
        mxCall();
1043
0
        mxPushSlot(value);
1044
0
        mxPushInteger(counter);
1045
0
        mxRunCount(2);
1046
0
        mxResult->value.boolean = fxToBoolean(the, the->stack);
1047
0
        mxPop();
1048
0
      }
1049
0
      mxCatch(the) {
1050
0
        fxIteratorReturn(the, iterator, 1);
1051
0
        fxJump(the);
1052
0
      }
1053
0
      if (mxResult->value.boolean) {
1054
0
        fxIteratorReturn(the, iterator, 0);
1055
0
        break;
1056
0
      }
1057
0
      counter++;
1058
0
    }
1059
0
  }
1060
0
  mxPop();
1061
0
  mxPop();
1062
0
  mxPop();
1063
0
}
1064
1065
void fx_Iterator_prototype_take(txMachine* the)
1066
0
{ 
1067
0
  txSlot *iterator, *property;
1068
0
  txNumber limit;
1069
0
  if (!fxGetInstance(the, mxThis))
1070
0
    mxTypeError("this: not an object");
1071
0
  iterator = mxThis;
1072
0
  {
1073
0
    mxTry(the) {
1074
0
      if (mxArgc > 0)
1075
0
        mxPushSlot(mxArgv(0));
1076
0
      else
1077
0
        mxPushUndefined();
1078
0
      limit = fxToNumber(the, the->stack);
1079
0
      if (c_isnan(limit))
1080
0
        mxRangeError("limit: NaN");
1081
0
      if (c_isfinite(limit))
1082
0
        limit = c_trunc(limit);
1083
0
      if (limit < 0)
1084
0
        mxRangeError("limit: < 0");
1085
0
    }
1086
0
    mxCatch(the) {
1087
0
      fxIteratorReturn(the, iterator, 1);
1088
0
      fxJump(the);
1089
0
    }
1090
0
  }
1091
0
  property = fxLastProperty(the, fxNewIteratorHelperInstance(the, iterator, mx_Iterator_prototype_take));
1092
0
  property = fxNextNumberProperty(the, property, limit, XS_NO_ID, XS_INTERNAL_FLAG);
1093
0
  mxPullSlot(mxResult);
1094
0
  mxPop();
1095
0
}
1096
1097
txBoolean fx_Iterator_prototype_take_step(txMachine* the, txSlot* iterator, txSlot* next, txSlot* extra, txSlot* value, txFlag status)
1098
0
{
1099
0
  txNumber remaining = extra->value.number;
1100
0
  if (status == XS_NO_STATUS) {
1101
0
    if (remaining == 0) {
1102
0
      fxIteratorReturn(the, iterator, 0);
1103
0
      return 0;
1104
0
    }
1105
0
    if (c_isfinite(remaining)) {
1106
0
      remaining--;
1107
0
      extra->value.number = remaining;
1108
0
    }
1109
0
    if (fxIteratorNext(the, iterator, next, value))
1110
0
      return 1;
1111
0
  }
1112
0
  else if (status == XS_RETURN_STATUS)
1113
0
    fxIteratorReturn(the, iterator, 0);
1114
0
  else if (status == XS_THROW_STATUS)
1115
0
    fxIteratorReturn(the, iterator, 1);
1116
0
  return 0;
1117
0
}
1118
1119
void fx_Iterator_prototype_toArray(txMachine* the)
1120
0
{ 
1121
0
  txSlot *iterator, *next, *value, *instance, *internal, *item;
1122
0
  if (!fxGetInstance(the, mxThis))
1123
0
    mxTypeError("this: not an object");
1124
0
  iterator = mxThis;
1125
0
  mxPushSlot(iterator);
1126
0
  mxGetID(mxID(_next));
1127
0
  next = the->stack;
1128
0
  mxTemporary(value);
1129
0
  mxPush(mxArrayPrototype);
1130
0
  instance = fxNewArrayInstance(the);
1131
0
  mxPullSlot(mxResult);
1132
0
  internal = instance->next;
1133
0
  item = internal;
1134
0
  while (fxIteratorNext(the, iterator, next, value)) {
1135
0
    internal->value.array.length++;
1136
0
    item->next = fxNewSlot(the);
1137
0
    item = item->next;
1138
0
    item->kind = value->kind;
1139
0
    item->value = value->value;
1140
0
  }
1141
0
  fxCacheArray(the, instance);
1142
0
  mxPop();
1143
0
  mxPop();
1144
0
}
1145
1146
void fx_Iterator_prototype_toStringTag_get(txMachine* the)
1147
0
{ 
1148
0
  mxPushStringC("Iterator");
1149
0
  mxPullSlot(mxResult);
1150
0
}
1151
1152
void fx_Iterator_prototype_toStringTag_set(txMachine* the)
1153
0
{ 
1154
0
  fxSetterThatIgnoresPrototypeProperties(the, mxThis, mxID(_Symbol_toStringTag), "Symbol(toStringTag)", mxArgv(0));
1155
0
}
1156
1157
txSlot* fxNewIteratorHelperInstance(txMachine* the, txSlot* iterator, txInteger step) 
1158
0
{
1159
0
  txSlot* instance;
1160
0
  txSlot* property;
1161
0
  mxPush(mxIteratorHelperPrototype);
1162
0
  instance = fxNewIteratorInstance(the, iterator, mxID(_Iterator));
1163
0
  property = fxLastProperty(the, instance);
1164
0
  property->ID = gxIteratorStepIDs[step];
1165
0
  property->value.integer = step;
1166
0
  if (mxIsNull(iterator))
1167
0
    property = fxNextNullProperty(the, property, XS_NO_ID, XS_INTERNAL_FLAG);
1168
0
  else {
1169
0
    mxPushSlot(iterator);
1170
0
    mxGetID(mxID(_next));
1171
0
    property = fxNextSlotProperty(the, property, the->stack, XS_NO_ID, XS_INTERNAL_FLAG);
1172
0
    mxPop();
1173
0
  }
1174
0
  return instance;
1175
0
}
1176
1177
void fx_IteratorHelper_prototype_next(txMachine* the)
1178
0
{
1179
0
  txSlot* instance = fxCheckIteratorInstance(the, mxThis, mxID(_Iterator));
1180
0
  txSlot* result = instance->next;
1181
0
  txSlot* iterator = result->next;
1182
0
  txSlot* step = fxCheckIteratorStep(the, iterator->next);
1183
0
  txSlot* next = step->next;
1184
0
  txSlot* extra = next->next;
1185
0
  txSlot* value = fxCheckIteratorResult(the, result);
1186
0
  txSlot* done = value->next;
1187
0
  if (step->flag & XS_DONT_SET_FLAG)
1188
0
    mxTypeError("recursive iterator");
1189
0
  {
1190
0
    mxTry(the) {
1191
0
      step->flag |= XS_DONT_SET_FLAG;
1192
0
      if (!done->value.boolean) {
1193
0
        if (!(*gxIteratorSteps[step->value.integer])(the, iterator, next, extra, value, XS_NO_STATUS)) {
1194
0
          value->kind = XS_UNDEFINED_KIND;
1195
0
          done->value.boolean = 1;
1196
0
        }
1197
0
      }
1198
0
      mxResult->kind = result->kind;
1199
0
      mxResult->value = result->value;
1200
0
      step->flag &= ~XS_DONT_SET_FLAG;
1201
0
    }
1202
0
    mxCatch(the) {
1203
0
      step->flag &= ~XS_DONT_SET_FLAG;
1204
0
      value->kind = XS_UNDEFINED_KIND;
1205
0
      done->value.boolean = 1;
1206
0
      fxJump(the);
1207
0
    }
1208
0
  }
1209
0
}
1210
1211
void fx_IteratorHelper_prototype_return(txMachine* the)
1212
0
{
1213
0
  txSlot* instance = fxCheckIteratorInstance(the, mxThis, mxID(_Iterator));
1214
0
  txSlot* result = instance->next;
1215
0
  txSlot* iterator = result->next;
1216
0
  txSlot* step = fxCheckIteratorStep(the, iterator->next);
1217
0
  txSlot* next = step->next;
1218
0
  txSlot* extra = next->next;
1219
0
  txSlot* value = fxCheckIteratorResult(the, result);
1220
0
  txSlot* done = value->next;
1221
0
  if (step->flag & XS_DONT_SET_FLAG)
1222
0
    mxTypeError("recursive iterator");
1223
0
  {
1224
0
    mxTry(the) {
1225
0
      step->flag |= XS_DONT_SET_FLAG;
1226
0
      if (!done->value.boolean) {
1227
0
        value->kind = XS_UNDEFINED_KIND;
1228
0
        done->value.boolean = 1;
1229
0
        (*gxIteratorSteps[step->value.integer])(the, iterator, next, extra, value, XS_RETURN_STATUS);
1230
0
      }
1231
0
      mxResult->kind = result->kind;
1232
0
      mxResult->value = result->value;
1233
0
      step->flag &= ~XS_DONT_SET_FLAG;
1234
0
    }
1235
0
    mxCatch(the) {
1236
0
      step->flag &= ~XS_DONT_SET_FLAG;
1237
0
      fxJump(the);
1238
0
    }
1239
0
  }
1240
0
}
1241
1242
void fx_IteratorWrapper_prototype_next(txMachine* the)
1243
0
{
1244
0
  txSlot* instance = fxCheckIteratorInstance(the, mxThis, mxID(_Iterator));
1245
0
  txSlot* result = instance->next;
1246
0
  txSlot* iterator = result->next;
1247
0
  txSlot* step = iterator->next;
1248
0
  txSlot* next = step->next;
1249
0
  mxPushSlot(iterator);
1250
0
  mxPushSlot(next);
1251
0
  mxCall();
1252
0
  mxRunCount(0);
1253
0
  mxPullSlot(mxResult);
1254
0
}
1255
1256
void fx_IteratorWrapper_prototype_return(txMachine* the)
1257
0
{
1258
0
  txSlot* instance = fxCheckIteratorInstance(the, mxThis, mxID(_Iterator));
1259
0
  txSlot* result = instance->next;
1260
0
  txSlot* iterator = result->next;
1261
0
  txSlot* value = fxCheckIteratorResult(the, result);
1262
0
  txSlot* done = value->next;
1263
0
  mxPushSlot(iterator);
1264
0
  mxDub();
1265
0
  mxGetID(mxID(_return));
1266
0
  if (mxIsUndefined(the->stack) || mxIsNull(the->stack)) {
1267
0
    mxPop();
1268
0
    value->kind = XS_UNDEFINED_KIND;
1269
0
    done->value.boolean = 1;
1270
0
    mxResult->kind = result->kind;
1271
0
    mxResult->value = result->value;
1272
0
  }
1273
0
  else {
1274
0
    mxCall();
1275
0
    mxRunCount(0);
1276
0
    mxPullSlot(mxResult);
1277
0
  }
1278
0
}
1279
1280
txSlot* fxCheckGeneratorInstance(txMachine* the, txSlot* slot)
1281
0
{
1282
0
  if (slot->kind == XS_REFERENCE_KIND) {
1283
0
    txSlot* instance = slot->value.reference;
1284
0
    slot = instance->next;
1285
0
    if (slot && (slot->flag & XS_INTERNAL_FLAG) && (slot->kind == XS_STACK_KIND))
1286
0
      return instance;
1287
0
  }
1288
0
  mxTypeError("this: not a Generator instance");
1289
0
  return C_NULL;
1290
0
}
1291
1292
txSlot* fxNewGeneratorInstance(txMachine* the)
1293
0
{
1294
0
  txSlot* prototype;
1295
0
  txSlot* instance;
1296
0
  txSlot* property;
1297
1298
0
  prototype = (the->stack->kind == XS_REFERENCE_KIND) ? the->stack->value.reference : mxGeneratorPrototype.value.reference;
1299
1300
0
  instance = fxNewSlot(the);
1301
0
  instance->kind = XS_INSTANCE_KIND;
1302
0
  instance->value.instance.garbage = C_NULL;
1303
0
  instance->value.instance.prototype = prototype;
1304
0
  the->stack->value.reference = instance;
1305
0
  the->stack->kind = XS_REFERENCE_KIND;
1306
1307
0
  property = instance->next = fxNewSlot(the);
1308
0
  property->flag = XS_INTERNAL_FLAG;
1309
0
  property->kind = XS_STACK_KIND;
1310
0
  property->ID = XS_NO_ID;
1311
0
    property->value.stack.length = 0;
1312
0
    property->value.stack.address = C_NULL;
1313
  
1314
0
    property = property->next = fxNewSlot(the);
1315
0
  property->flag = XS_INTERNAL_FLAG;
1316
0
  property->kind = XS_INTEGER_KIND;
1317
0
  property->value.integer = XS_CODE_START_GENERATOR;
1318
  
1319
0
  return instance;
1320
0
}
1321
1322
void fxNewGeneratorResult(txMachine* the, txBoolean done)
1323
0
{
1324
0
  txSlot* value = the->stack;
1325
0
  txSlot* slot;
1326
0
  mxPush(mxObjectPrototype);
1327
0
  slot = fxLastProperty(the, fxNewObjectInstance(the));
1328
0
  slot = fxNextSlotProperty(the, slot, value, mxID(_value), XS_DONT_DELETE_FLAG | XS_DONT_SET_FLAG);
1329
0
  slot = fxNextBooleanProperty(the, slot, done, mxID(_done), XS_DONT_DELETE_FLAG | XS_DONT_SET_FLAG);
1330
0
  mxPullSlot(value);
1331
0
}
1332
1333
void fx_Generator(txMachine* the)
1334
0
{
1335
0
  if (mxHasTarget)
1336
0
    mxTypeError("new: Generator");
1337
0
}
1338
1339
void fx_Generator_prototype_aux(txMachine* the, txFlag status)
1340
0
{
1341
0
  txSlot* generator = fxCheckGeneratorInstance(the, mxThis);
1342
0
  txSlot* stack = generator->next;
1343
0
  txSlot* state = stack->next;
1344
1345
0
  if (state->value.integer == XS_NO_CODE)
1346
0
    mxTypeError("generator is running");
1347
0
  if ((state->value.integer == XS_CODE_START_GENERATOR) && (status != XS_NO_STATUS))
1348
0
    state->value.integer = XS_CODE_END;
1349
0
  if (mxArgc > 0)
1350
0
    mxPushSlot(mxArgv(0));
1351
0
  else
1352
0
    mxPushUndefined();
1353
0
  if (state->value.integer == XS_CODE_END) {
1354
0
    if (status == XS_THROW_STATUS) {
1355
0
      mxException.kind = the->stack->kind;
1356
0
      mxException.value = the->stack->value;
1357
0
      fxJump(the);
1358
0
    }
1359
0
    if (status == XS_NO_STATUS)
1360
0
      the->stack->kind = XS_UNDEFINED_KIND;
1361
0
    fxNewGeneratorResult(the, 1);
1362
0
    mxPullSlot(mxResult);
1363
0
  }
1364
0
  else {
1365
0
    mxTry(the) {
1366
0
      the->scratch.kind = the->stack->kind;
1367
0
      the->scratch.value = the->stack->value;
1368
0
      the->status = status;
1369
0
      state->value.integer = XS_NO_CODE;
1370
0
      fxRunID(the, generator, XS_NO_ID);
1371
0
      if (state->value.integer == XS_NO_CODE) {
1372
0
        state->value.integer = XS_CODE_END;
1373
0
        fxNewGeneratorResult(the, 1);
1374
0
      }
1375
0
      mxPullSlot(mxResult);
1376
0
    }
1377
0
    mxCatch(the) {
1378
0
      state->value.integer = XS_CODE_END;
1379
0
      fxJump(the);
1380
0
    }
1381
0
  }
1382
0
}
1383
1384
void fx_Generator_prototype_next(txMachine* the)
1385
0
{
1386
0
  fx_Generator_prototype_aux(the, XS_NO_STATUS);
1387
0
}
1388
1389
void fx_Generator_prototype_return(txMachine* the)
1390
0
{
1391
0
  fx_Generator_prototype_aux(the, XS_RETURN_STATUS);
1392
0
}
1393
1394
void fx_Generator_prototype_throw(txMachine* the)
1395
0
{
1396
0
  fx_Generator_prototype_aux(the, XS_THROW_STATUS);
1397
0
}
1398
1399
txSlot* fxNewGeneratorFunctionInstance(txMachine* the, txID name)
1400
0
{
1401
0
  txSlot* instance;
1402
0
  txSlot* property;
1403
1404
0
  instance = fxNewFunctionInstance(the, name);
1405
0
  property = fxLastProperty(the, instance);
1406
0
  mxPush(mxGeneratorPrototype);
1407
0
  fxNewObjectInstance(the);
1408
0
  fxNextSlotProperty(the, property, the->stack, mxID(_prototype), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG);
1409
0
  mxPop();
1410
  
1411
0
  return instance;
1412
0
}
1413
1414
void fx_GeneratorFunction(txMachine* the)
1415
0
{ 
1416
0
  txInteger c, i;
1417
0
  txStringStream stream;
1418
0
  txSlot* module = mxFunctionInstanceHome(mxFunction->value.reference)->value.home.module;
1419
0
  if (!module) module = mxProgram.value.reference;
1420
  
1421
0
  c = mxArgc;
1422
0
  i = 0;
1423
0
  mxPushStringX("(function* anonymous(");
1424
0
  while (c > 1) {
1425
0
    fxToString(the, mxArgv(i));
1426
0
    fxConcatString(the, the->stack, mxArgv(i));
1427
0
    if (c > 2)
1428
0
      fxConcatStringC(the, the->stack, ", ");
1429
0
    c--;
1430
0
    i++;
1431
0
  }
1432
0
  fxConcatStringC(the, the->stack, "\n){");
1433
0
  if (c > 0) {
1434
0
    fxToString(the, mxArgv(i));
1435
0
    fxConcatString(the, the->stack, mxArgv(i));
1436
0
  }
1437
0
  fxConcatStringC(the, the->stack, "\n})");
1438
0
  stream.slot = the->stack;
1439
0
  stream.offset = 0;
1440
0
  stream.size = mxStringLength(the->stack->value.string);
1441
0
  fxRunScript(the, fxParseScript(the, &stream, fxStringGetter, mxProgramFlag | mxGeneratorFlag), C_NULL, C_NULL, C_NULL, C_NULL, module);
1442
0
  mxPullSlot(mxResult);
1443
0
  if (mxHasTarget && !fxIsSameSlot(the, mxTarget, mxFunction)) {
1444
0
    mxPushSlot(mxTarget);
1445
0
    fxGetPrototypeFromConstructor(the, &mxGeneratorFunctionPrototype);
1446
0
    mxResult->value.reference->value.instance.prototype = the->stack->value.reference;
1447
0
    mxPop();
1448
0
  }
1449
0
}
1450
1451
void fx_Enumerator(txMachine* the)
1452
0
{
1453
0
  txSlot* iterator;
1454
0
  txSlot* result;
1455
0
  txSlot* slot;
1456
0
  txSlot* keys;
1457
0
  txSlot* visited;
1458
  
1459
0
  mxPush(mxEnumeratorFunction);
1460
0
  mxGetID(mxID(_prototype));
1461
0
  iterator = fxNewObjectInstance(the);
1462
0
  mxPullSlot(mxResult);
1463
0
  mxPush(mxObjectPrototype);
1464
0
  result = fxNewObjectInstance(the);
1465
0
  slot = fxNextUndefinedProperty(the, result, mxID(_value), XS_DONT_DELETE_FLAG | XS_DONT_SET_FLAG);
1466
0
  slot = fxNextBooleanProperty(the, slot, 0, mxID(_done), XS_DONT_DELETE_FLAG | XS_DONT_SET_FLAG);
1467
0
  slot = fxNextSlotProperty(the, iterator, the->stack, mxID(_result), XS_GET_ONLY);
1468
0
  mxPop();
1469
  
1470
0
  slot = slot->next = fxNewSlot(the);
1471
0
  slot->flag = XS_GET_ONLY;
1472
0
  slot->ID = mxID(_iterable);
1473
0
  slot->kind = mxThis->kind;
1474
0
  slot->value = mxThis->value;
1475
0
  if (mxIsUndefined(slot) || mxIsNull(slot))
1476
0
    return;
1477
0
  fxToInstance(the, slot);
1478
    
1479
0
  keys = fxNewInstance(the);
1480
0
  mxBehaviorOwnKeys(the, slot->value.reference, XS_EACH_NAME_FLAG, keys);
1481
0
  slot = slot->next = fxNewSlot(the);
1482
0
  slot->flag = XS_GET_ONLY;
1483
0
  slot->kind = XS_REFERENCE_KIND;
1484
0
  slot->value.reference = keys;
1485
0
  mxPop();
1486
  
1487
0
  visited = fxNewInstance(the);
1488
0
  slot = slot->next = fxNewSlot(the);
1489
0
  slot->flag = XS_GET_ONLY;
1490
0
  slot->kind = XS_REFERENCE_KIND;
1491
0
  slot->value.reference = visited;
1492
0
  mxPop();
1493
0
}
1494
1495
void fx_Enumerator_prototype_next(txMachine* the)
1496
0
{
1497
0
  txSlot* iterator = fxGetInstance(the, mxThis);
1498
0
  txSlot* result = iterator->next;
1499
0
  txSlot* iterable = result->next;
1500
0
  txSlot* at = C_NULL;
1501
0
  if (mxIsReference(iterable)) {
1502
0
    txSlot* instance = iterable->value.reference;
1503
0
    txSlot* keys = iterable->next;
1504
0
    txSlot* visited = keys->next;
1505
0
    txSlot* slot;
1506
0
    txSlot* former;
1507
  
1508
0
    keys = keys->value.reference;
1509
0
    visited = visited->value.reference;
1510
  
1511
0
    mxPushUndefined();
1512
0
    slot = the->stack;
1513
0
  again:  
1514
0
    while ((at = keys->next)) {
1515
0
      if (mxBehaviorGetOwnProperty(the, instance, at->value.at.id, at->value.at.index, slot)) {
1516
0
        txSlot** address = &(visited->next);
1517
0
        while ((former = *address)) {
1518
0
          if ((at->value.at.id == former->value.at.id) && (at->value.at.index == former->value.at.index))
1519
0
            break;
1520
0
          address = &(former->next);
1521
0
        }
1522
0
        if (!former) {
1523
0
          *address = at;
1524
0
          keys->next = at->next;
1525
0
          at->next = NULL;
1526
0
          if (!(slot->flag & XS_DONT_ENUM_FLAG)) {
1527
0
            fxKeyAt(the, at->value.at.id, at->value.at.index, result->value.reference->next);
1528
0
            break;
1529
0
          }
1530
0
        }
1531
0
        else
1532
0
          keys->next = at->next;
1533
0
      }
1534
0
      else
1535
0
        keys->next = at->next;
1536
0
    }
1537
0
    if (!at) {
1538
0
      if (mxBehaviorGetPrototype(the, instance, slot)) {
1539
0
        iterable->value.reference = instance = slot->value.reference;
1540
0
        mxBehaviorOwnKeys(the, instance, XS_EACH_NAME_FLAG, keys);
1541
0
        goto again;
1542
0
      }
1543
0
    }
1544
0
    mxPop();
1545
0
  }
1546
0
  if (!at) {
1547
0
    result->value.reference->next->kind = XS_UNDEFINED_KIND;
1548
0
    result->value.reference->next->next->value.boolean = 1;
1549
0
  }
1550
0
  mxResult->kind = result->kind;
1551
0
  mxResult->value = result->value;
1552
0
}
1553
1554
void fx_AsyncIterator_prototype_asyncDispose(txMachine* the)
1555
0
{ 
1556
0
  mxTry(the) {
1557
0
    mxPushUndefined();
1558
0
    mxPush(mxPromiseConstructor);
1559
0
    mxPushSlot(mxThis);
1560
0
    mxDub();
1561
0
    mxGetID(mxID(_return));
1562
0
    if (mxIsUndefined(the->stack)) {
1563
0
      mxPop();
1564
0
      the->stack->kind = XS_UNDEFINED_KIND;
1565
0
    }
1566
0
    else {
1567
0
      mxCall();
1568
0
      mxRunCount(0);    
1569
0
    }
1570
0
    fx_Promise_resolveAux(the);
1571
0
    mxPop();
1572
0
    mxPop();
1573
0
    mxPullSlot(mxResult);
1574
0
  }
1575
0
  mxCatch(the) {
1576
0
    txSlot* resolveFunction;
1577
0
    txSlot* rejectFunction;
1578
0
    mxTemporary(resolveFunction);
1579
0
    mxTemporary(rejectFunction);
1580
0
    mxPush(mxPromiseConstructor);
1581
0
    fxNewPromiseCapability(the, resolveFunction, rejectFunction);
1582
0
    mxPullSlot(mxResult);
1583
0
    fxRejectException(the, rejectFunction);
1584
0
  }
1585
0
}
1586
1587
void fx_AsyncIterator_prototype_asyncIterator(txMachine* the)
1588
0
{
1589
0
  *mxResult = *mxThis;
1590
0
}
1591
1592
void fxAsyncGeneratorRejectAwait(txMachine* the)
1593
0
{
1594
0
  txSlot* slot = mxFunctionInstanceHome(mxFunction->value.reference);
1595
0
  txSlot* generator = slot->value.home.object;
1596
0
  the->scratch.kind = mxArgv(0)->kind;
1597
0
  the->scratch.value = mxArgv(0)->value;
1598
0
  fxAsyncGeneratorStep(the, generator, XS_THROW_STATUS);
1599
0
}
1600
1601
void fxAsyncGeneratorRejectYield(txMachine* the)
1602
0
{
1603
0
  txSlot* home = mxFunctionInstanceHome(mxFunction->value.reference);
1604
0
  txSlot* generator = home->value.home.object;
1605
0
  txSlot* stack = generator->next;
1606
0
  txSlot* state = stack->next;
1607
0
  if (state->value.integer == XS_CODE_END) {
1608
0
    txSlot* queue = state->next;
1609
0
    txSlot* current = queue->value.list.first;
1610
0
    txSlot* resolveFunction = current->value.reference->next;
1611
0
    txSlot* rejectFunction = resolveFunction->next;
1612
1613
0
    mxPushUndefined();
1614
0
    mxPushSlot(rejectFunction);
1615
0
    mxCall();
1616
0
    mxPushSlot(mxArgv(0));
1617
  
1618
0
    queue->value.list.first = current = current->next;
1619
0
    if (current == C_NULL)
1620
0
      queue->value.list.last = C_NULL;
1621
  
1622
0
    mxRunCount(1);
1623
0
    mxPop();
1624
  
1625
0
    if (current) {
1626
0
      txSlot* resolveFunction = current->value.reference->next;
1627
0
      txSlot* rejectFunction = resolveFunction->next;
1628
0
      txSlot* status = rejectFunction->next;
1629
0
      txSlot* value = status->next;
1630
0
      the->scratch.kind = value->kind;
1631
0
      the->scratch.value = value->value;
1632
0
      fxAsyncGeneratorStep(the, generator, status->value.integer);
1633
0
    }
1634
0
  }
1635
0
  else {
1636
0
    mxPushSlot(mxArgv(0));
1637
0
    mxPull(the->scratch);
1638
0
    fxAsyncGeneratorStep(the, generator, XS_THROW_STATUS);
1639
0
  }
1640
0
}
1641
1642
void fxAsyncGeneratorResolveAwait(txMachine* the)
1643
0
{
1644
0
  txSlot* slot = mxFunctionInstanceHome(mxFunction->value.reference);
1645
0
  txSlot* generator = slot->value.home.object;
1646
0
  the->scratch.kind = mxArgv(0)->kind;
1647
0
  the->scratch.value = mxArgv(0)->value;
1648
0
  fxAsyncGeneratorStep(the, generator, XS_NO_STATUS);
1649
0
}
1650
1651
void fxAsyncGeneratorResolveYield(txMachine* the)
1652
0
{
1653
0
  txSlot* home = mxFunctionInstanceHome(mxFunction->value.reference);
1654
0
  txSlot* generator = home->value.home.object;
1655
0
  txSlot* stack = generator->next;
1656
0
  txSlot* state = stack->next;
1657
0
  txSlot* queue = state->next;
1658
0
  txSlot* current = queue->value.list.first;
1659
0
  if (current) {
1660
0
    txSlot* resolveFunction = current->value.reference->next;
1661
  
1662
0
    mxPushUndefined();
1663
0
    mxPushSlot(resolveFunction);
1664
0
    mxCall();
1665
0
    mxPushSlot(mxArgv(0));
1666
0
    fxNewGeneratorResult(the, (state->value.integer == XS_CODE_END) ? 1 : 0);
1667
  
1668
0
    queue->value.list.first = current = current->next;
1669
0
    if (current == C_NULL)
1670
0
      queue->value.list.last = C_NULL;
1671
  
1672
0
    mxRunCount(1);
1673
0
    mxPop();
1674
0
  }
1675
  
1676
0
  if (current) {
1677
0
    txSlot* resolveFunction = current->value.reference->next;
1678
0
    txSlot* rejectFunction = resolveFunction->next;
1679
0
    txSlot* status = rejectFunction->next;
1680
0
    txSlot* value = status->next;
1681
0
    the->scratch.kind = value->kind;
1682
0
    the->scratch.value = value->value;
1683
0
    fxAsyncGeneratorStep(the, generator, status->value.integer);
1684
0
  }
1685
0
}
1686
1687
void fxAsyncGeneratorStep(txMachine* the, txSlot* generator, txFlag status)
1688
0
{
1689
0
  txSlot* stack = generator->next;
1690
0
  txSlot* state = stack->next;
1691
0
  txSlot* queue = state->next;
1692
0
  txSlot* resolveAwaitFunction = queue->next;
1693
0
  txSlot* rejectAwaitFunction = resolveAwaitFunction->next;
1694
0
  txSlot* resolveYieldFunction = rejectAwaitFunction->next;
1695
0
  txSlot* rejectYieldFunction = resolveYieldFunction->next;
1696
0
  txSlot* resolveFunction;
1697
0
  txSlot* rejectFunction;
1698
0
  txSlot* value;
1699
  
1700
0
  mxTry(the) {
1701
0
    if (state->value.integer == XS_CODE_END) {
1702
0
      mxPush(the->scratch);
1703
0
      value = the->stack;
1704
0
      mxTemporary(resolveFunction);
1705
0
      mxTemporary(rejectFunction);
1706
0
      mxPush(mxPromiseConstructor);
1707
0
      fxNewPromiseCapability(the, resolveFunction, rejectFunction);
1708
0
            fxPromiseThen(the, the->stack->value.reference, resolveYieldFunction, rejectYieldFunction, C_NULL, C_NULL);
1709
      /* THIS */
1710
0
      mxPushUndefined();
1711
      /* FUNCTION */
1712
0
      if (status == XS_THROW_STATUS)
1713
0
        mxPushSlot(rejectFunction);
1714
0
      else
1715
0
        mxPushSlot(resolveFunction);
1716
0
      mxCall();
1717
      /* ARGUMENTS */
1718
0
      mxPushSlot(value);
1719
0
      mxRunCount(1);
1720
0
      mxPop();
1721
0
    }
1722
0
    else {
1723
0
      the->status = status;
1724
0
      status = XS_NO_STATUS;
1725
0
      state->value.integer = XS_NO_CODE;
1726
0
      fxRunID(the, generator, XS_NO_ID);
1727
0
      if (state->value.integer == XS_NO_CODE)
1728
0
        state->value.integer = XS_CODE_END;
1729
0
      value = the->stack;
1730
0
      if (state->value.integer == XS_CODE_END) {
1731
0
        txSlot* current = queue->value.list.first;
1732
0
        if (current) {
1733
0
          if (value->kind == XS_UNINITIALIZED_KIND)
1734
0
            value->kind = XS_UNDEFINED_KIND;
1735
0
          mxPushUndefined();
1736
0
          if (status == XS_THROW_STATUS)
1737
0
            mxPushSlot(rejectYieldFunction);
1738
0
          else
1739
0
            mxPushSlot(resolveYieldFunction);
1740
0
          mxCall();
1741
0
          mxPushSlot(value);
1742
0
          mxRunCount(1);
1743
0
          mxPop();
1744
0
        }
1745
0
      }
1746
0
      else if (state->value.integer == XS_CODE_AWAIT) {
1747
0
        mxPushUndefined();
1748
0
        mxPush(mxPromiseConstructor);
1749
0
        mxPushSlot(value);
1750
0
        fx_Promise_resolveAux(the);
1751
0
        mxPop();
1752
0
        mxPop();
1753
0
        fxPromiseThen(the, the->stack->value.reference, resolveAwaitFunction, rejectAwaitFunction, C_NULL, C_NULL);
1754
0
      }
1755
0
      else if (state->value.integer == XS_CODE_YIELD) {
1756
0
        mxPushUndefined();
1757
0
        mxPush(mxPromiseConstructor);
1758
0
        mxPushSlot(value);
1759
0
        fx_Promise_resolveAux(the);
1760
0
        mxPop();
1761
0
        mxPop();
1762
0
        fxPromiseThen(the, the->stack->value.reference, resolveYieldFunction, rejectYieldFunction, C_NULL, C_NULL);
1763
0
      }
1764
0
      else if (state->value.integer == XS_CODE_YIELD_STAR) {
1765
0
        txSlot* current = queue->value.list.first;
1766
0
        if (current) {
1767
0
          mxPushUndefined();
1768
0
          mxPushSlot(resolveYieldFunction);
1769
0
          mxCall();
1770
0
          mxPushSlot(value);
1771
0
          mxRunCount(1);
1772
0
          mxPop();
1773
0
        }
1774
0
      }
1775
0
    }
1776
0
    mxPop();
1777
0
  }
1778
0
  mxCatch(the) {
1779
0
    mxTemporary(resolveFunction);
1780
0
    mxTemporary(rejectFunction);
1781
0
    mxPush(mxPromiseConstructor);
1782
0
    fxNewPromiseCapability(the, resolveFunction, rejectFunction);
1783
0
        if (state->value.integer == XS_CODE_AWAIT) {
1784
0
            fxPromiseThen(the, the->stack->value.reference, resolveAwaitFunction, rejectAwaitFunction, C_NULL, C_NULL);
1785
0
        }
1786
0
        else {
1787
0
      state->value.integer = XS_CODE_END;
1788
0
            fxPromiseThen(the, the->stack->value.reference, resolveYieldFunction, rejectYieldFunction, C_NULL, C_NULL);
1789
0
        }
1790
0
        fxRejectException(the, rejectFunction);
1791
0
  }
1792
0
}
1793
1794
txSlot* fxCheckAsyncGeneratorInstance(txMachine* the, txSlot* slot)
1795
0
{
1796
0
  if (slot->kind == XS_REFERENCE_KIND) {
1797
0
    txSlot* instance = slot->value.reference;
1798
0
    slot = instance->next;
1799
0
    if (slot && (slot->flag & XS_INTERNAL_FLAG) && (slot->kind == XS_STACK_KIND)) {
1800
0
      slot = slot->next;
1801
0
      if (slot && (slot->flag & XS_INTERNAL_FLAG) && (slot->kind == XS_INTEGER_KIND)) {
1802
0
        slot = slot->next;
1803
0
        if (slot && (slot->flag & XS_INTERNAL_FLAG) && (slot->kind == XS_LIST_KIND)) {
1804
0
          return instance;
1805
0
        }
1806
0
      }
1807
0
    }
1808
0
  }
1809
0
  mxTypeError("this: not an AsyncGenerator instance");
1810
0
  return C_NULL;
1811
0
}
1812
1813
txSlot* fxNewAsyncGeneratorInstance(txMachine* the)
1814
0
{
1815
0
  txSlot* prototype;
1816
0
  txSlot* instance;
1817
0
  txSlot* property;
1818
0
  txSlot* function;
1819
0
  txSlot* home;
1820
1821
0
  prototype = (the->stack->kind == XS_REFERENCE_KIND) ? the->stack->value.reference : mxAsyncGeneratorPrototype.value.reference;
1822
1823
0
  instance = fxNewSlot(the);
1824
0
  instance->kind = XS_INSTANCE_KIND;
1825
0
  instance->value.instance.garbage = C_NULL;
1826
0
  instance->value.instance.prototype = prototype;
1827
0
  the->stack->value.reference = instance;
1828
0
  the->stack->kind = XS_REFERENCE_KIND;
1829
1830
0
  property = instance->next = fxNewSlot(the);
1831
0
  property->flag = XS_INTERNAL_FLAG;
1832
0
  property->kind = XS_STACK_KIND;
1833
0
  property->ID = XS_NO_ID;
1834
0
    property->value.stack.length = 0;
1835
0
    property->value.stack.address = C_NULL;
1836
  
1837
0
    property = property->next = fxNewSlot(the);
1838
0
  property->flag = XS_INTERNAL_FLAG;
1839
0
  property->kind = XS_INTEGER_KIND;
1840
0
  property->value.integer = XS_CODE_START_ASYNC_GENERATOR;
1841
  
1842
0
    property = property->next = fxNewSlot(the);
1843
0
  property->flag = XS_INTERNAL_FLAG;
1844
0
  property->kind = XS_LIST_KIND;
1845
0
  property->value.list.first = C_NULL;
1846
0
  property->value.list.last = C_NULL;
1847
  
1848
0
  function = fxNewHostFunction(the, fxAsyncGeneratorResolveAwait, 1, XS_NO_ID, mxAsyncGeneratorResolveAwaitProfileID);
1849
0
  home = mxFunctionInstanceHome(function);
1850
0
  home->value.home.object = instance;
1851
0
    property = fxNextSlotProperty(the, property, the->stack, XS_NO_ID, XS_INTERNAL_FLAG);
1852
0
  mxPop();
1853
  
1854
0
  function = fxNewHostFunction(the, fxAsyncGeneratorRejectAwait, 1, XS_NO_ID, mxAsyncGeneratorRejectAwaitProfileID);
1855
0
  home = mxFunctionInstanceHome(function);
1856
0
  home->value.home.object = instance;
1857
0
    property = fxNextSlotProperty(the, property, the->stack, XS_NO_ID, XS_INTERNAL_FLAG);
1858
0
  mxPop();
1859
  
1860
0
  function = fxNewHostFunction(the, fxAsyncGeneratorResolveYield, 1, XS_NO_ID, mxAsyncGeneratorResolveYieldProfileID);
1861
0
  home = mxFunctionInstanceHome(function);
1862
0
  home->value.home.object = instance;
1863
0
    property = fxNextSlotProperty(the, property, the->stack, XS_NO_ID, XS_INTERNAL_FLAG);
1864
0
  mxPop();
1865
  
1866
0
  function = fxNewHostFunction(the, fxAsyncGeneratorRejectYield, 1, XS_NO_ID, mxAsyncGeneratorRejectYieldProfileID);
1867
0
  home = mxFunctionInstanceHome(function);
1868
0
  home->value.home.object = instance;
1869
0
    property = fxNextSlotProperty(the, property, the->stack, XS_NO_ID, XS_INTERNAL_FLAG);
1870
0
  mxPop();
1871
  
1872
0
  return instance;
1873
0
}
1874
1875
1876
void fx_AsyncGenerator(txMachine* the)
1877
0
{
1878
0
  if (mxHasTarget)
1879
0
    mxTypeError("new: AsyncGenerator");
1880
0
}
1881
1882
void fx_AsyncGenerator_prototype_aux(txMachine* the, txFlag status)
1883
0
{
1884
0
  txSlot* stack = the->stack;
1885
0
  txSlot* resolveFunction;
1886
0
  txSlot* rejectFunction;
1887
0
  txSlot* slot;
1888
0
  txSlot* generator;
1889
0
  txSlot* state;
1890
0
  txSlot* queue;
1891
0
  txSlot* instance;
1892
0
  txSlot* property;
1893
  
1894
0
  mxTemporary(resolveFunction);
1895
0
  mxTemporary(rejectFunction);
1896
0
  mxPush(mxPromiseConstructor);
1897
0
  fxNewPromiseCapability(the, resolveFunction, rejectFunction);
1898
0
  mxPullSlot(mxResult);
1899
#ifdef mxPromisePrint
1900
  fprintf(stderr, "fx_AsyncGenerator_prototype_aux %d\n", mxResult->value.reference->next->ID);
1901
#endif
1902
0
  {
1903
0
    mxTry(the) {
1904
0
      generator = fxCheckAsyncGeneratorInstance(the, mxThis);
1905
0
      state = generator->next->next;
1906
0
      if (((status == XS_RETURN_STATUS) || (status == XS_THROW_STATUS)) && (state->value.integer == XS_CODE_START_ASYNC_GENERATOR))
1907
0
        state->value.integer = XS_CODE_END;
1908
0
      instance = property = fxNewInstance(the);
1909
0
      property = fxNextSlotProperty(the, property, resolveFunction, XS_NO_ID, XS_INTERNAL_FLAG);
1910
0
      property = fxNextSlotProperty(the, property, rejectFunction, XS_NO_ID, XS_INTERNAL_FLAG);
1911
0
      property = fxNextIntegerProperty(the, property, status, XS_NO_ID, XS_INTERNAL_FLAG);
1912
0
      if (mxArgc > 0)
1913
0
        property = fxNextSlotProperty(the, property, mxArgv(0), XS_NO_ID, XS_INTERNAL_FLAG);
1914
0
      else
1915
0
        property = fxNextUndefinedProperty(the, property, XS_NO_ID, XS_INTERNAL_FLAG);
1916
0
      slot = fxNewSlot(the);
1917
0
      slot->kind = XS_REFERENCE_KIND;
1918
0
      slot->value.reference = instance;
1919
0
      queue = state->next;
1920
0
      if (queue->value.list.first) {
1921
0
        queue->value.list.last->next = slot;
1922
0
        queue->value.list.last = slot;
1923
0
      }
1924
0
      else {
1925
0
        queue->value.list.first = slot;
1926
0
        queue->value.list.last = slot;
1927
0
        if (state->value.integer != XS_CODE_AWAIT) {
1928
0
          the->scratch.kind = property->kind;
1929
0
          the->scratch.value = property->value;
1930
0
          fxAsyncGeneratorStep(the, generator, status);
1931
0
        }
1932
0
      }
1933
0
    }
1934
0
    mxCatch(the) {
1935
0
      fxRejectException(the, rejectFunction);
1936
0
    }
1937
0
  }
1938
0
  the->stack = stack;
1939
0
}
1940
1941
void fx_AsyncGenerator_prototype_next(txMachine* the)
1942
0
{
1943
0
  fx_AsyncGenerator_prototype_aux(the, XS_NO_STATUS);
1944
0
}
1945
1946
void fx_AsyncGenerator_prototype_return(txMachine* the)
1947
0
{
1948
0
  fx_AsyncGenerator_prototype_aux(the, XS_RETURN_STATUS);
1949
0
}
1950
1951
void fx_AsyncGenerator_prototype_throw(txMachine* the)
1952
0
{
1953
0
  fx_AsyncGenerator_prototype_aux(the, XS_THROW_STATUS);
1954
0
}
1955
1956
txSlot* fxNewAsyncGeneratorFunctionInstance(txMachine* the, txID name)
1957
0
{
1958
0
  txSlot* instance;
1959
0
  txSlot* property;
1960
1961
0
  instance = fxNewFunctionInstance(the, name);
1962
0
  property = fxLastProperty(the, instance);
1963
0
  mxPush(mxAsyncGeneratorPrototype);
1964
0
  fxNewObjectInstance(the);
1965
0
  fxNextSlotProperty(the, property, the->stack, mxID(_prototype), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG);
1966
0
  mxPop();
1967
  
1968
0
  return instance;
1969
0
}
1970
1971
void fx_AsyncGeneratorFunction(txMachine* the)
1972
0
{ 
1973
0
  txInteger c, i;
1974
0
  txStringStream stream;
1975
0
  txSlot* module = mxFunctionInstanceHome(mxFunction->value.reference)->value.home.module;
1976
0
  if (!module) module = mxProgram.value.reference;
1977
  
1978
0
  c = mxArgc;
1979
0
  i = 0;
1980
0
  mxPushStringX("(async function* anonymous(");
1981
0
  while (c > 1) {
1982
0
    fxToString(the, mxArgv(i));
1983
0
    fxConcatString(the, the->stack, mxArgv(i));
1984
0
    if (c > 2)
1985
0
      fxConcatStringC(the, the->stack, ", ");
1986
0
    c--;
1987
0
    i++;
1988
0
  }
1989
0
  fxConcatStringC(the, the->stack, "\n){");
1990
0
  if (c > 0) {
1991
0
    fxToString(the, mxArgv(i));
1992
0
    fxConcatString(the, the->stack, mxArgv(i));
1993
0
  }
1994
0
  fxConcatStringC(the, the->stack, "\n})");
1995
0
  stream.slot = the->stack;
1996
0
  stream.offset = 0;
1997
0
  stream.size = mxStringLength(the->stack->value.string);
1998
0
  fxRunScript(the, fxParseScript(the, &stream, fxStringGetter, mxProgramFlag | mxGeneratorFlag), C_NULL, C_NULL, C_NULL, C_NULL, module);
1999
0
  mxPullSlot(mxResult);
2000
0
  if (mxHasTarget && !fxIsSameSlot(the, mxTarget, mxFunction)) {
2001
0
    mxPushSlot(mxTarget);
2002
0
    fxGetPrototypeFromConstructor(the, &mxAsyncGeneratorFunctionPrototype);
2003
0
    mxResult->value.reference->value.instance.prototype = the->stack->value.reference;
2004
0
    mxPop();
2005
0
  }
2006
0
}
2007
2008
void fxAsyncFromSyncIteratorDone(txMachine* the)
2009
0
{
2010
0
  txSlot* slot = mxFunctionInstanceHome(mxFunction->value.reference);
2011
0
  txSlot* instance = slot->value.home.object;
2012
0
  txSlot* iterator = instance->next;
2013
0
  txSlot* nextFunction = iterator->next;
2014
0
  txSlot* doneFunction = nextFunction->next;
2015
0
  txSlot* doneFlag = doneFunction->next;
2016
0
  mxPushSlot(mxArgv(0));
2017
0
  fxNewGeneratorResult(the, doneFlag->value.boolean);
2018
0
  mxPullSlot(mxResult);
2019
0
}
2020
2021
void fxAsyncFromSyncIteratorFailed(txMachine* the)
2022
0
{
2023
0
  txSlot* slot = mxFunctionInstanceHome(mxFunction->value.reference);
2024
0
  txSlot* instance = slot->value.home.object;
2025
0
  txSlot* iterator = instance->next;
2026
0
  mxTry(the) {
2027
0
    mxPushSlot(iterator);
2028
0
    mxPushSlot(iterator);
2029
0
    mxGetID(mxID(_return));
2030
0
    if (!mxIsUndefined(the->stack) && !mxIsNull(the->stack)) {
2031
0
      mxCall();
2032
0
      mxRunCount(0);
2033
0
    }
2034
0
    mxPop();
2035
0
  }
2036
0
  mxCatch(the) {
2037
0
  }
2038
0
  mxException.kind = mxArgv(0)->kind;
2039
0
  mxException.value = mxArgv(0)->value;
2040
0
  fxThrow(the, NULL, 0);
2041
0
}
2042
2043
txSlot* fxCheckAsyncFromSyncIteratorInstance(txMachine* the, txSlot* slot)
2044
0
{
2045
0
  if (slot->kind == XS_REFERENCE_KIND) {
2046
0
    txSlot* instance = slot->value.reference;
2047
0
    slot = instance->next;
2048
0
    if (slot && (slot->flag & XS_INTERNAL_FLAG) && (slot->ID == mxID(_iterator)))
2049
0
      return instance;
2050
0
  }
2051
0
  mxTypeError("this: not an AsyncFromSyncIterator instance");
2052
0
  return C_NULL;
2053
0
}
2054
2055
txSlot* fxNewAsyncFromSyncIteratorInstance(txMachine* the)
2056
0
{
2057
0
  txSlot* iterator = the->stack;
2058
0
  txSlot* instance;
2059
0
  txSlot* slot;
2060
0
  txSlot* function;
2061
0
  txSlot* home;
2062
0
  mxPush(mxAsyncFromSyncIteratorPrototype);
2063
0
  instance = fxNewObjectInstance(the);
2064
0
  slot = fxLastProperty(the, instance);
2065
  
2066
0
  slot = fxNextSlotProperty(the, slot, iterator, mxID(_iterator), XS_INTERNAL_FLAG);
2067
2068
0
  mxPushSlot(iterator);
2069
0
  mxGetID(mxID(_next));
2070
0
  slot = fxNextSlotProperty(the, slot, the->stack, XS_NO_ID, XS_INTERNAL_FLAG);
2071
0
  mxPop();
2072
  
2073
0
  function = fxNewHostFunction(the, fxAsyncFromSyncIteratorDone, 1, XS_NO_ID, mxAsyncFromSyncIteratorDoneProfileID);
2074
0
  home = mxFunctionInstanceHome(function);
2075
0
  home->value.home.object = instance;
2076
0
    slot = fxNextSlotProperty(the, slot, the->stack, XS_NO_ID, XS_INTERNAL_FLAG);
2077
0
  mxPop();
2078
  
2079
0
    slot = fxNextBooleanProperty(the, slot, 0, XS_NO_ID, XS_INTERNAL_FLAG);
2080
    
2081
0
  function = fxNewHostFunction(the, fxAsyncFromSyncIteratorFailed, 1, XS_NO_ID, mxAsyncFromSyncIteratorDoneProfileID);
2082
0
  home = mxFunctionInstanceHome(function);
2083
0
  home->value.home.object = instance;
2084
0
    slot = fxNextSlotProperty(the, slot, the->stack, XS_NO_ID, XS_INTERNAL_FLAG);
2085
0
  mxPop();
2086
2087
0
  mxPullSlot(iterator);
2088
0
  return instance;
2089
0
}
2090
2091
void fx_AsyncFromSyncIterator_prototype_aux(txMachine* the, txFlag status, txSlot* iterator, txBoolean* flag)
2092
0
{
2093
0
  txSlot* stack = the->stack;
2094
0
  txSlot* resolveFunction;
2095
0
    txSlot* rejectFunction;
2096
0
  txSlot* slot;
2097
0
  txSlot* stepFunction = C_NULL;
2098
  
2099
0
  mxTemporary(resolveFunction);
2100
0
  mxTemporary(rejectFunction);
2101
0
  mxPush(mxPromiseConstructor);
2102
0
  fxNewPromiseCapability(the, resolveFunction, rejectFunction);
2103
0
  mxPullSlot(mxResult);
2104
#ifdef mxPromisePrint
2105
  fprintf(stderr, "fx_AsyncFromSyncIterator_prototype_aux %d %d\n", mxResult->value.reference->next->ID, status);
2106
#endif
2107
0
    {
2108
0
    mxTry(the) {
2109
0
      if (status == XS_NO_STATUS) {
2110
0
        stepFunction = iterator->next;
2111
0
      }
2112
0
      else if (status == XS_RETURN_STATUS) {
2113
0
        mxPushSlot(iterator);
2114
0
        mxGetID(mxID(_return));
2115
0
        if (mxIsUndefined(the->stack) || mxIsNull(the->stack)) {
2116
0
          mxPushUndefined();
2117
0
          mxPushSlot(resolveFunction);
2118
0
          mxCall();
2119
0
          mxPushUndefined();
2120
0
          fxNewGeneratorResult(the, 1);
2121
0
          mxRunCount(1);
2122
0
        }
2123
0
        else
2124
0
          stepFunction = the->stack;
2125
0
      }
2126
0
      else {
2127
0
        mxPushSlot(iterator);
2128
0
        mxGetID(mxID(_throw));
2129
0
        if (mxIsUndefined(the->stack) || mxIsNull(the->stack)) {
2130
0
          *flag = 0;
2131
0
          fxIteratorReturn(the, iterator, 0);
2132
0
          mxTypeError("no throw");
2133
0
        }
2134
0
        else
2135
0
          stepFunction = the->stack;
2136
0
      }
2137
0
      if (stepFunction) {
2138
0
        txSlot* doneFunction = iterator->next->next;
2139
0
        txSlot* doneFlag = doneFunction->next;
2140
0
        txSlot* failedFunction = doneFlag->next;
2141
0
        mxPushSlot(iterator);
2142
0
        mxPushSlot(stepFunction);
2143
0
        mxCall();
2144
0
        if (mxArgc == 0)
2145
0
          mxRunCount(0);
2146
0
        else {
2147
0
          mxPushSlot(mxArgv(0));
2148
0
          mxRunCount(1);
2149
0
        }
2150
0
        slot = the->stack;
2151
0
                if (!mxIsReference(slot)) {
2152
0
                    mxTypeError("iterator result: not an object");
2153
0
                }
2154
2155
0
        mxPushSlot(slot);
2156
0
        mxGetID(mxID(_done));
2157
0
        doneFlag->value.boolean = fxToBoolean(the, the->stack);
2158
2159
0
        mxPushUndefined();
2160
0
        mxPush(mxPromiseConstructor);
2161
0
        mxPushSlot(slot);
2162
0
        mxGetID(mxID(_value));
2163
0
        fx_Promise_resolveAux(the);
2164
0
        mxPop();
2165
0
        mxPop();
2166
0
        fxPromiseThen(the, the->stack->value.reference, doneFunction, failedFunction, resolveFunction, rejectFunction);
2167
0
      }
2168
0
    }
2169
0
    mxCatch(the) {
2170
0
      if (*flag)
2171
0
        fxIteratorReturn(the, iterator, 1);
2172
0
      fxRejectException(the, rejectFunction);
2173
0
    }
2174
0
    }
2175
0
  the->stack = stack;
2176
0
}
2177
2178
void fx_AsyncFromSyncIterator_prototype_next(txMachine* the)
2179
0
{
2180
0
  txBoolean flag = 1;
2181
0
  txSlot* instance = fxCheckAsyncFromSyncIteratorInstance(the, mxThis);
2182
0
  fx_AsyncFromSyncIterator_prototype_aux(the, XS_NO_STATUS, instance->next, &flag);
2183
0
}
2184
2185
void fx_AsyncFromSyncIterator_prototype_return(txMachine* the)
2186
0
{
2187
0
  txBoolean flag = 1;
2188
0
  txSlot* instance = fxCheckAsyncFromSyncIteratorInstance(the, mxThis);
2189
0
  fx_AsyncFromSyncIterator_prototype_aux(the, XS_RETURN_STATUS, instance->next, &flag);
2190
0
}
2191
2192
void fx_AsyncFromSyncIterator_prototype_throw(txMachine* the)
2193
0
{
2194
0
  txBoolean flag = 1;
2195
0
  txSlot* instance = fxCheckAsyncFromSyncIteratorInstance(the, mxThis);
2196
0
  fx_AsyncFromSyncIterator_prototype_aux(the, XS_THROW_STATUS, instance->next, &flag);
2197
0
}
2198
2199