Coverage Report

Created: 2026-01-10 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/moddable/xs/sources/xsFunction.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2016-2025  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* fxCheckFunctionInstance(txMachine* the, txSlot* slot);
41
static void fxStepAsync(txMachine* the, txSlot* instance, txFlag status);
42
43
static const txByte gxTailCode[1] = { XS_CODE_RUN_TAIL };
44
45
void fxBuildFunction(txMachine* the)
46
4.18k
{
47
4.18k
  txSlot* slot;
48
4.18k
  txSlot* function;
49
4.18k
  txSlot* constructor;
50
4.18k
  mxPush(mxFunctionPrototype);
51
4.18k
  slot = fxLastProperty(the, the->stack->value.reference);
52
4.18k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Function_prototype_apply), 2, mxID(_apply), XS_DONT_ENUM_FLAG);
53
4.18k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Function_prototype_bind), 1, mxID(_bind), XS_DONT_ENUM_FLAG);
54
4.18k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Function_prototype_call), 1, mxID(_call), XS_DONT_ENUM_FLAG);
55
4.18k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Function_prototype_toString), 0, mxID(_toString), XS_DONT_ENUM_FLAG);
56
4.18k
    slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Function_prototype_hasInstance), 1, mxID(_Symbol_hasInstance), XS_GET_ONLY);
57
4.18k
  function = mxThrowTypeErrorFunction.value.reference;
58
4.18k
  slot = slot->next = fxNewSlot(the);
59
4.18k
  slot->flag = XS_DONT_ENUM_FLAG;
60
4.18k
  slot->ID = mxID(_arguments);
61
4.18k
  slot->kind = XS_ACCESSOR_KIND;
62
4.18k
  slot->value.accessor.getter = function;
63
4.18k
  slot->value.accessor.setter = function;
64
4.18k
  slot = slot->next = fxNewSlot(the);
65
4.18k
  slot->flag = XS_DONT_ENUM_FLAG;
66
4.18k
  slot->ID = mxID(_caller);
67
4.18k
  slot->kind = XS_ACCESSOR_KIND;
68
4.18k
  slot->value.accessor.getter = function;
69
4.18k
  slot->value.accessor.setter = function;
70
4.18k
  constructor = fxBuildHostConstructor(the, mxCallback(fx_Function), 1, mxID(_Function));
71
4.18k
  mxFunctionConstructor = *the->stack;
72
4.18k
  mxPop();
73
  
74
4.18k
  mxPush(mxFunctionPrototype);
75
4.18k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
76
4.18k
  slot = fxNextStringXProperty(the, slot, "AsyncFunction", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
77
4.18k
  mxAsyncFunctionPrototype = *the->stack;
78
4.18k
  slot = fxBuildHostConstructor(the, mxCallback(fx_AsyncFunction), 1, mxID(_AsyncFunction));
79
4.18k
  slot->value.instance.prototype = constructor;
80
4.18k
  mxPop();
81
4.18k
  slot = mxBehaviorGetProperty(the, mxAsyncFunctionPrototype.value.reference, mxID(_constructor), 0, XS_OWN);
82
4.18k
  slot->flag |= XS_DONT_SET_FLAG;
83
4.18k
}
84
85
void fxCheckCallable(txMachine* the, txSlot* slot)
86
0
{
87
0
  if (fxIsCallable(the, slot))
88
0
    return;
89
0
  mxTypeError("this: not a Function instance");
90
0
}
91
92
txSlot* fxCheckFunctionInstance(txMachine* the, txSlot* slot)
93
0
{
94
0
  if (slot->kind == XS_REFERENCE_KIND) {
95
0
    slot = slot->value.reference;
96
0
    if (fxIsFunction(the, slot))
97
0
      return slot;
98
0
  }
99
0
  mxTypeError("this: not a Function instance");
100
0
  return C_NULL;
101
0
}
102
103
txBoolean fxIsCallable(txMachine* the, txSlot* slot) 
104
0
{
105
0
  if (slot->kind == XS_REFERENCE_KIND)
106
0
    return fxIsFunction(the, slot->value.reference);
107
#if mxHostFunctionPrimitive
108
  if (slot->kind == XS_HOST_FUNCTION_KIND)
109
    return 1;
110
#endif
111
0
  return 0;
112
0
}
113
114
txBoolean fxIsFunction(txMachine* the, txSlot* instance)
115
0
{
116
0
again:
117
0
  if (instance) {
118
0
    txSlot* exotic = instance->next;
119
0
    if (exotic && (exotic->flag & XS_INTERNAL_FLAG)) {
120
0
      if (((exotic->kind == XS_CALLBACK_KIND) || (exotic->kind == XS_CALLBACK_X_KIND) || (exotic->kind == XS_CODE_KIND) || (exotic->kind == XS_CODE_X_KIND)))
121
0
        return 1;
122
0
      if (exotic->kind == XS_PROXY_KIND) {
123
0
        instance = exotic->value.proxy.target;
124
0
        goto again;
125
0
      }
126
0
    }
127
0
  }
128
0
  return 0;
129
0
}
130
131
txSlot* fxNewFunctionInstance(txMachine* the, txID name)
132
4.18k
{
133
4.18k
  txSlot* instance;
134
4.18k
  txSlot* property;
135
136
4.18k
  instance = fxNewObjectInstance(the);
137
4.18k
  instance->flag |= XS_CAN_CALL_FLAG;
138
139
  /* CODE */
140
4.18k
  property = instance->next = fxNewSlot(the);
141
4.18k
  property->flag = XS_INTERNAL_FLAG;
142
4.18k
  property->kind = mxEmptyCode.kind;
143
4.18k
  property->value.code.address = mxEmptyCode.value.code.address;
144
4.18k
  property->value.code.closures = C_NULL;
145
146
  /* HOME */
147
4.18k
  property = property->next = fxNewSlot(the);
148
4.18k
  property->flag = XS_INTERNAL_FLAG;
149
4.18k
  property->kind = XS_HOME_KIND;
150
4.18k
  property->value.home.object = C_NULL;
151
4.18k
  if (the->frame && (mxFunction->kind == XS_REFERENCE_KIND) && (mxIsFunction(mxFunction->value.reference))) {
152
0
    txSlot* slot = mxFunctionInstanceHome(mxFunction->value.reference);
153
0
    property->value.home.module = slot->value.home.module;
154
0
  }
155
4.18k
  else
156
4.18k
    property->value.home.module = C_NULL;
157
    
158
  /* LENGTH */
159
4.18k
  if (gxDefaults.newFunctionLength)
160
4.18k
    gxDefaults.newFunctionLength(the, instance, 0);
161
    
162
  /* NAME */
163
4.18k
  fxRenameFunction(the, instance, name, 0, XS_NO_ID, C_NULL);
164
165
4.18k
  return instance;
166
4.18k
}
167
168
void fxDefaultFunctionPrototype(txMachine* the)
169
0
{
170
0
  txSlot* instance;
171
0
  txSlot* property;
172
0
  instance = the->stack->value.reference;
173
0
  instance->flag |= XS_CAN_CONSTRUCT_FLAG;
174
0
  property = fxLastProperty(the, instance);
175
0
  mxPush(mxObjectPrototype);
176
0
  instance = fxNewObjectInstance(the);
177
0
  fxNextSlotProperty(the, property, the->stack, mxID(_prototype), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG);
178
0
  mxPop();
179
0
  fxNextSlotProperty(the, instance, the->stack, mxID(_constructor), XS_DONT_ENUM_FLAG);
180
0
}
181
182
txSlot* fxGetPrototypeFromConstructor(txMachine* the, txSlot* defaultPrototype)
183
0
{
184
0
  txSlot* result = the->stack;
185
0
  fxCheckCallable(the, result);
186
0
  mxDub();
187
0
  mxGetID(mxID(_prototype));
188
0
  if (!mxIsReference(the->stack)) {
189
0
    txSlot* instance = result->value.reference;
190
0
    txSlot* proxy = instance->next;
191
0
    if (proxy->kind == XS_PROXY_KIND) {
192
0
      if (!proxy->value.proxy.handler)
193
0
        mxTypeError("(proxy).%s: no handler", fxName(the, mxID(_prototype)));
194
0
      if (!proxy->value.proxy.target)
195
0
        mxTypeError("(proxy).%s: no target", fxName(the, mxID(_prototype)));
196
0
    }
197
0
    the->stack->kind = defaultPrototype->kind;
198
0
    the->stack->value = defaultPrototype->value;
199
0
  }
200
0
  mxPullSlot(result);
201
0
  return result->value.reference;
202
0
}
203
204
#ifndef mxLink
205
txSlot* fxNewFunctionLength(txMachine* the, txSlot* instance, txNumber length)
206
2.44M
{
207
2.44M
  txSlot* property = mxBehaviorGetProperty(the, instance, mxID(_length), 0, XS_OWN);
208
2.44M
  if (!property)
209
2.44M
    property = fxNextIntegerProperty(the, fxLastProperty(the, instance), 0, mxID(_length), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
210
2.44M
  if (length <= 0x7FFFFFFF) {
211
2.44M
    property->kind = XS_INTEGER_KIND;
212
2.44M
    property->value.integer = (txInteger)length;
213
2.44M
  }
214
0
  else {
215
0
    property->kind = XS_NUMBER_KIND;
216
0
    property->value.number = length;
217
0
  }
218
2.44M
  return property;
219
2.44M
}
220
221
txSlot* fxNewFunctionName(txMachine* the, txSlot* instance, txID id, txIndex index, txID former, txString prefix)
222
2.66M
{
223
2.66M
  txSlot* property;
224
2.66M
  property = mxBehaviorGetProperty(the, instance, mxID(_name), 0, XS_OWN);
225
2.66M
  if (property) {
226
217k
    if ((property->kind != mxEmptyString.kind) || (property->value.string != mxEmptyString.value.string))
227
0
      return property;
228
217k
  }
229
2.44M
  else
230
2.44M
    property = fxNextSlotProperty(the, fxLastProperty(the, instance), &mxEmptyString, mxID(_name), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
231
2.66M
  if (id != XS_NO_ID) {
232
2.35M
    txBoolean adorn;
233
2.35M
    fxPushKeyString(the, id, &adorn);
234
2.35M
    mxPullSlot(property);
235
2.35M
    if (adorn)
236
100k
      fxAdornStringC(the, "[", property, "]");
237
2.35M
  }
238
309k
  else if (former) {
239
0
    char buffer[16];
240
0
    fxCopyStringC(the, property, fxNumberToString(the, index, buffer, sizeof(buffer), 0, 0)); 
241
0
  }
242
2.66M
  if (prefix) 
243
217k
    fxAdornStringC(the, prefix, property, C_NULL);
244
2.66M
  return property;
245
2.66M
}
246
#endif
247
248
void fxRenameFunction(txMachine* the, txSlot* instance, txID id, txIndex index, txID former, txString prefix)
249
2.66M
{
250
2.66M
  txSlot* property;
251
2.66M
  if (instance->flag & XS_MARK_FLAG)
252
0
    return;
253
2.66M
  property = mxFunctionInstanceCode(instance);
254
2.66M
  if ((property->ID == XS_NO_ID) || (property->ID == former)) {
255
2.66M
    if (id != XS_NO_ID)
256
2.35M
      property->ID = (txID)id;
257
2.66M
  }
258
2.66M
  if (gxDefaults.newFunctionName)
259
2.66M
    property = gxDefaults.newFunctionName(the, instance, id, index, former, prefix);
260
2.66M
}
261
262
void fx_Function(txMachine* the)
263
0
{ 
264
0
  txInteger c, i;
265
0
  txStringStream stream;
266
0
  txSlot* module = mxFunctionInstanceHome(mxFunction->value.reference)->value.home.module;
267
0
  if (!module) module = mxProgram.value.reference;
268
  
269
0
  c = mxArgc;
270
0
  i = 0;
271
0
  mxPushStringX("(function anonymous(");
272
0
  while (c > 1) {
273
0
    fxToString(the, mxArgv(i));
274
0
    fxConcatString(the, the->stack, mxArgv(i));
275
0
    if (c > 2)
276
0
      fxConcatStringC(the, the->stack, ", ");
277
0
    c--;
278
0
    i++;
279
0
  }
280
0
  fxConcatStringC(the, the->stack, "\n){");
281
0
  if (c > 0) {
282
0
    fxToString(the, mxArgv(i));
283
0
    fxConcatString(the, the->stack, mxArgv(i));
284
0
  }
285
0
  fxConcatStringC(the, the->stack, "\n})");
286
0
  stream.slot = the->stack;
287
0
  stream.offset = 0;
288
0
  stream.size = mxStringLength(the->stack->value.string);
289
0
  fxRunScript(the, fxParseScript(the, &stream, fxStringGetter, mxProgramFlag | mxFunctionFlag), C_NULL, C_NULL, C_NULL, C_NULL, module);
290
0
  mxPullSlot(mxResult);
291
0
  if (!mxIsUndefined(mxTarget) && !fxIsSameSlot(the, mxTarget, mxFunction)) {
292
0
    mxPushSlot(mxTarget);
293
0
    fxGetPrototypeFromConstructor(the, &mxFunctionPrototype);
294
0
    mxResult->value.reference->value.instance.prototype = the->stack->value.reference;
295
0
    mxPop();
296
0
  }
297
0
}
298
299
void fx_Function_prototype_apply(txMachine* the)
300
0
{
301
0
  txIndex c, i;
302
0
  fxCheckCallable(the, mxThis);
303
  /* THIS */
304
0
  if (mxArgc < 1)
305
0
    mxPushUndefined();
306
0
  else
307
0
    mxPushSlot(mxArgv(0));
308
  /* FUNCTION */
309
0
  mxPushSlot(mxThis);
310
0
  mxCall();
311
  /* ARGUMENTS */
312
0
  if ((mxArgc < 2) || (mxArgv(1)->kind == XS_UNDEFINED_KIND) || (mxArgv(1)->kind == XS_NULL_KIND))
313
0
    c = 0;
314
0
  else {
315
0
    if (mxArgv(1)->kind != XS_REFERENCE_KIND)
316
0
      mxTypeError("argArray: not an object");
317
0
    fxToInstance(the, mxArgv(1));
318
0
    mxPushSlot(mxArgv(1));
319
0
    mxGetID(mxID(_length));
320
0
    c = (txIndex)fxToLength(the, the->stack);
321
0
    mxPop();
322
0
    for (i = 0; i < c; i++) {
323
0
      mxPushSlot(mxArgv(1));
324
0
      mxGetIndex(i);
325
0
    }
326
0
  }
327
0
  mxPushInteger(c);
328
0
  the->code = (txByte *)gxTailCode;
329
0
}
330
331
void fx_Function_prototype_bind(txMachine* the)
332
0
{
333
0
  txSlot* function = fxToInstance(the, mxThis);
334
0
  txSlot* instance;
335
0
  txSlot* property;
336
0
  txSlot* arguments;
337
0
  txSlot* argument;
338
0
  txSize c = mxArgc, i;
339
340
0
  fxCheckCallable(the, mxThis);
341
0
  mxPushNull();
342
0
  if (mxBehaviorGetPrototype(the, function, the->stack))
343
0
    instance = fxNewObjectInstance(the);
344
0
  else {
345
0
    mxPop();
346
0
    instance = fxNewInstance(the);
347
0
  }
348
0
  instance->flag |= function->flag & (XS_CAN_CALL_FLAG | XS_CAN_CONSTRUCT_FLAG);
349
0
    mxPullSlot(mxResult);
350
      
351
  /* CODE */
352
0
  property = instance->next = fxNewSlot(the);
353
0
  property->flag = XS_INTERNAL_FLAG;
354
0
  property->kind = XS_CALLBACK_KIND;
355
0
  property->value.callback.address = fx_Function_prototype_bound;
356
0
  property->value.callback.closures = C_NULL;
357
358
  /* HOME */
359
0
  property = property->next = fxNewSlot(the);
360
0
  property->flag = XS_INTERNAL_FLAG;
361
0
  property->kind = XS_HOME_KIND;
362
0
  property->value.home.object = C_NULL;
363
0
  property->value.home.module = C_NULL;
364
365
0
  property = fxNextSlotProperty(the, property, mxThis, mxID(_boundFunction), XS_INTERNAL_FLAG);
366
0
  if (c > 0)
367
0
    property = fxNextSlotProperty(the, property, mxArgv(0), mxID(_boundThis), XS_INTERNAL_FLAG);
368
0
  else
369
0
    property = fxNextUndefinedProperty(the, property, mxID(_boundThis), XS_INTERNAL_FLAG);
370
  
371
0
  if (c > 1) {
372
0
    mxPush(mxArrayPrototype);
373
0
    arguments = fxNewArrayInstance(the);
374
0
    argument = fxLastProperty(the, arguments);
375
0
    for (i = 1; i < c; i++) {
376
0
      argument->next = fxNewSlot(the);
377
0
      argument = argument->next;
378
0
      argument->kind = mxArgv(i)->kind;
379
0
      argument->value = mxArgv(i)->value;
380
0
    }
381
0
    arguments->next->value.array.length = c - 1;
382
0
    fxCacheArray(the, arguments);
383
0
    property = fxNextSlotProperty(the, property, the->stack, mxID(_boundArguments), XS_INTERNAL_FLAG);
384
0
    mxPop();
385
0
  }
386
0
  else {
387
0
    property = fxNextNullProperty(the, property, mxID(_boundArguments), XS_INTERNAL_FLAG);
388
0
  }
389
  
390
0
  if (gxDefaults.newFunctionLength) {
391
0
    txNumber length = 0;
392
0
    mxPushUndefined();
393
0
    if (mxBehaviorGetOwnProperty(the, mxThis->value.reference, mxID(_length), 0, the->stack)) {
394
0
      mxPushSlot(mxThis);
395
0
      mxGetID(mxID(_length));
396
0
      property = the->stack;
397
0
      if (property->kind == XS_INTEGER_KIND) {
398
0
        length = property->value.integer;
399
0
      }
400
0
      else if (property->kind == XS_NUMBER_KIND) {
401
0
        length = property->value.number;
402
0
        if (c_isnan(length))
403
0
          length = 0;
404
0
        else
405
0
          length = c_trunc(length);
406
0
      }
407
0
      if (c > 1)
408
0
        length -= c - 1;
409
0
      if (length < 0)
410
0
        length = 0;
411
0
      mxPop();
412
0
    }
413
0
    mxPop();
414
0
    gxDefaults.newFunctionLength(the, instance, length);
415
0
  }
416
  
417
0
  if (gxDefaults.newFunctionName) {
418
0
    txSize length = 0;
419
0
    txString name;
420
0
    mxPushSlot(mxThis);
421
0
    mxGetID(mxID(_name));
422
0
    if ((the->stack->kind == XS_STRING_KIND) || (the->stack->kind == XS_STRING_X_KIND))
423
0
      length = mxStringLength(the->stack->value.string);
424
0
    property = fxNextSlotProperty(the, fxLastProperty(the, instance), &mxEmptyString, mxID(_name), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
425
0
    name = (txString)fxNewChunk(the, fxAddChunkSizes(the, length, 6 + 1));
426
0
    c_memcpy(name, "bound ", 6);
427
0
    if (length)
428
0
      c_memcpy(name + 6, the->stack->value.string, length);
429
0
    name[6 + length] = 0;
430
0
    property->value.string = name;
431
0
    property->kind = XS_STRING_KIND;
432
0
    mxPop();
433
0
  }
434
0
}
435
436
void fx_Function_prototype_bound(txMachine* the)
437
0
{
438
0
  txSlot* function = fxToInstance(the, mxFunction);
439
0
  txSlot* boundArguments;
440
0
  txInteger c, i;
441
0
  txSlot* argument;
442
  /* THIS */
443
0
  if (mxTarget->kind == XS_UNDEFINED_KIND) {
444
0
    mxPushSlot(mxFunctionInstanceHome(function)->next->next);
445
0
  }
446
0
  else
447
0
    mxPushUninitialized();
448
  /* FUNCTION */
449
0
  mxPushSlot(mxFunctionInstanceHome(function)->next);
450
  /* TARGET */
451
0
  if (fxIsSameSlot(the, mxFunction, mxTarget)) {
452
0
    txSlot* slot = the->stack;
453
0
    mxPushSlot(slot);
454
0
  }
455
0
  else
456
0
    mxPushSlot(mxTarget);
457
  /* RESULT */
458
0
  mxPushUndefined();
459
0
  mxPushUninitialized();
460
0
  mxPushUninitialized();
461
  /* ARGUMENTS */
462
0
  mxPushSlot(mxFunctionInstanceHome(function)->next->next->next);
463
0
  if (the->stack->kind == XS_REFERENCE_KIND) {
464
0
    boundArguments = fxGetInstance(the, the->stack);
465
0
    mxPop();
466
0
    c = boundArguments->next->value.array.length;
467
0
    argument = boundArguments->next->value.array.address;
468
0
    for (i = 0; i < c; i++) {
469
0
      mxPushSlot(argument);
470
0
      argument++;
471
0
    }
472
0
  }
473
0
  else {
474
0
    mxPop();
475
0
    c = 0;
476
0
  }
477
0
  for (i = 0; i < mxArgc; i++)
478
0
    mxPushSlot(mxArgv(i));
479
0
  if (mxTarget->kind) {
480
0
    mxRunCount(c + i);
481
0
    mxPullSlot(mxResult);
482
0
  }
483
0
  else {
484
0
    mxPushInteger(c + i);
485
0
    the->code = (txByte *)gxTailCode;
486
0
  }
487
0
}
488
489
void fx_Function_prototype_call(txMachine* the)
490
0
{ 
491
0
  txInteger c, i;
492
0
  fxCheckCallable(the, mxThis);
493
  /* THIS */
494
0
  if (mxArgc < 1)
495
0
    mxPushUndefined();
496
0
  else
497
0
    mxPushSlot(mxArgv(0));
498
  /* FUNCTION */
499
0
  mxPushSlot(mxThis);
500
0
  mxCall();
501
  /* ARGUMENTS */
502
0
  c = mxArgc;
503
0
  i = 1;
504
0
  while (i < c) {
505
0
    mxPushSlot(mxArgv(i));
506
0
    i++;
507
0
  }
508
0
  mxPushInteger(i - 1);
509
0
  the->code = (txByte *)gxTailCode;
510
0
}
511
512
void fx_Function_prototype_hasInstance(txMachine* the)
513
0
{ 
514
0
  txSlot* function;
515
0
  txSlot* slot;
516
0
  txSlot* instance;
517
0
  txSlot* prototype;
518
0
  mxResult->kind = XS_BOOLEAN_KIND;
519
0
  mxResult->value.boolean = 0;
520
0
  if (!fxIsCallable(the, mxThis))
521
0
    return;
522
0
  function = fxToInstance(the, mxThis);
523
0
  if (!function)
524
0
    return;
525
0
  if (mxIsFunction(function)) {
526
0
    slot = mxFunctionInstanceHome(function)->next;
527
0
    if (slot && (slot->flag & XS_INTERNAL_FLAG) && (slot->ID == mxID(_boundFunction))) {
528
0
      if (!fxIsCallable(the, slot))
529
0
        return;
530
0
      function = fxToInstance(the, slot);
531
0
      if (!function)
532
0
        return;
533
0
    }
534
0
  }
535
0
  if (mxArgc == 0)
536
0
    return;
537
0
  instance = fxGetInstance(the, mxArgv(0));
538
0
  if (!instance)
539
0
    return;
540
0
  mxPushReference(function);
541
0
  mxGetID(mxID(_prototype));
542
0
  prototype = fxGetInstance(the, the->stack);
543
0
  mxPop();
544
0
  if (!prototype)
545
0
    mxTypeError("this.prototype: not an object");
546
#if mxAliasInstance
547
  if (prototype->ID) {
548
    txSlot* alias = the->aliasArray[prototype->ID];
549
    if (alias)
550
      prototype = alias;
551
  }
552
#endif
553
0
  mxPushNull();
554
0
  while (mxBehaviorGetPrototype(the, instance, the->stack)) {
555
0
    instance = the->stack->value.reference;
556
0
    if (instance == prototype) {
557
0
      mxResult->value.boolean = 1;
558
0
      break;
559
0
    }
560
0
  }
561
0
  mxPop();
562
0
}
563
564
void fx_Function_prototype_toString(txMachine* the)
565
0
{ 
566
0
  fxCheckFunctionInstance(the, mxThis);
567
0
  mxPushStringX("function [\"");
568
0
  mxPushSlot(mxThis);
569
0
  mxGetID(mxID(_name));
570
0
  if ((the->stack->kind == XS_STRING_KIND) || (the->stack->kind == XS_STRING_X_KIND))
571
0
    fxConcatString(the, the->stack + 1, the->stack);
572
0
  mxPop();
573
0
  mxPushStringX("\"] (){[native code]}");
574
0
  fxConcatString(the, the->stack + 1, the->stack);
575
0
  mxPop();
576
0
  mxPullSlot(mxResult);
577
0
}
578
579
txSlot* fxNewAsyncInstance(txMachine* the)
580
0
{
581
0
  txSlot* instance;
582
0
  txSlot* property;
583
0
  txSlot* promise;
584
0
  txSlot* status;
585
0
  txSlot* function;
586
0
  txSlot* home;
587
  
588
0
  mxPushUndefined();
589
590
0
  instance = fxNewSlot(the);
591
0
  instance->kind = XS_INSTANCE_KIND;
592
0
  instance->value.instance.garbage = C_NULL;
593
0
  instance->value.instance.prototype = C_NULL;
594
0
  the->stack->value.reference = instance;
595
0
  the->stack->kind = XS_REFERENCE_KIND;
596
597
0
  property = instance->next = fxNewSlot(the);
598
0
  property->flag = XS_INTERNAL_FLAG;
599
0
  property->kind = XS_STACK_KIND;
600
0
  property->ID = XS_NO_ID;
601
0
  property->value.stack.length = 0;
602
0
  property->value.stack.address = C_NULL;
603
  
604
0
    property = fxNextIntegerProperty(the, property, XS_CODE_START_ASYNC, XS_NO_ID, XS_INTERNAL_FLAG);
605
606
0
  mxPush(mxPromisePrototype);
607
0
  promise = fxNewPromiseInstance(the);
608
0
  status = mxPromiseStatus(promise);
609
0
  status->value.integer = mxPendingStatus;
610
0
    property = fxNextSlotProperty(the, property, the->stack, XS_NO_ID, XS_INTERNAL_FLAG);
611
0
  mxPop();
612
  
613
0
  fxPushPromiseFunctions(the, promise);
614
0
    property = fxNextSlotProperty(the, property, the->stack + 1, XS_NO_ID, XS_INTERNAL_FLAG);
615
0
    property = fxNextSlotProperty(the, property, the->stack, XS_NO_ID, XS_INTERNAL_FLAG);
616
0
  mxPop();
617
0
  mxPop();
618
  
619
0
  function = fxNewHostFunction(the, fxResolveAwait, 1, XS_NO_ID, mxResolveAwaitProfileID);
620
0
  home = mxFunctionInstanceHome(function);
621
0
  home->value.home.object = instance;
622
0
    property = fxNextSlotProperty(the, property, the->stack, XS_NO_ID, XS_INTERNAL_FLAG);
623
0
  mxPop();
624
  
625
0
  function = fxNewHostFunction(the, fxRejectAwait, 1, XS_NO_ID, mxRejectAwaitProfileID);
626
0
  home = mxFunctionInstanceHome(function);
627
0
  home->value.home.object = instance;
628
0
    property = fxNextSlotProperty(the, property, the->stack, XS_NO_ID, XS_INTERNAL_FLAG);
629
0
  mxPop();
630
  
631
0
  return instance;
632
0
}
633
634
void fxResolveAwait(txMachine* the)
635
0
{
636
0
  txSlot* slot = mxFunctionInstanceHome(mxFunction->value.reference);
637
0
  txSlot* instance = slot->value.home.object;
638
0
  the->scratch.kind = mxArgv(0)->kind;
639
0
  the->scratch.value = mxArgv(0)->value;
640
0
  fxStepAsync(the, instance, XS_NO_STATUS);
641
0
}
642
643
void fxRejectAwait(txMachine* the)
644
0
{
645
0
  txSlot* slot = mxFunctionInstanceHome(mxFunction->value.reference);
646
0
  txSlot* instance = slot->value.home.object;
647
0
  the->scratch.kind = mxArgv(0)->kind;
648
0
  the->scratch.value = mxArgv(0)->value;
649
0
  fxStepAsync(the, instance, XS_THROW_STATUS);
650
0
}
651
652
void fxRunAsync(txMachine* the, txSlot* instance)
653
0
{
654
0
  txSlot* promise = instance->next->next->next;
655
0
  fxBeginHost(the);
656
0
  the->scratch.kind = XS_UNDEFINED_KIND;
657
0
  fxStepAsync(the, instance, XS_NO_STATUS);
658
0
  fxEndHost(the);
659
0
  mxResult->kind = promise->kind;
660
0
  mxResult->value = promise->value;
661
0
}
662
663
void fxStepAsync(txMachine* the, txSlot* instance, txFlag status)
664
0
{
665
0
  txSlot* state = instance->next->next;
666
0
  txSlot* promise = state->next;
667
0
  txSlot* resolveFunction = promise->next;
668
0
  txSlot* rejectFunction = resolveFunction->next;
669
0
  txSlot* resolveAwaitFunction = rejectFunction->next;
670
0
  txSlot* rejectAwaitFunction = resolveAwaitFunction->next;
671
0
  txSlot* value;
672
0
  mxTry(the) {
673
0
    the->status = status;
674
0
    state->value.integer = XS_NO_CODE;
675
0
    fxRunID(the, instance, XS_NO_ID);
676
0
    value = the->stack;
677
0
    if (state->value.integer == XS_NO_CODE) {
678
      /* THIS */
679
0
      mxPushUndefined();
680
      /* FUNCTION */
681
0
      mxPushSlot(resolveFunction);
682
0
      mxCall();
683
      /* ARGUMENTS */
684
0
      mxPushSlot(value);
685
0
      mxRunCount(1);
686
0
      mxPop();
687
0
    }
688
0
    else {
689
0
      if (mxIsReference(value) && mxIsPromise(value->value.reference)) {
690
0
        mxDub();
691
0
        mxGetID(mxID(_constructor));
692
0
        if (fxIsSameValue(the, &mxPromiseConstructor, the->stack, 0)) {
693
0
          mxPop();
694
0
          fxPromiseThen(the, value->value.reference, resolveAwaitFunction, rejectAwaitFunction, C_NULL, C_NULL);
695
0
          goto exit;
696
0
        }
697
0
        mxPop();
698
0
      }
699
0
      mxTemporary(resolveFunction);
700
0
      mxTemporary(rejectFunction);
701
0
      mxPush(mxPromiseConstructor);
702
0
      fxNewPromiseCapability(the, resolveFunction, rejectFunction);
703
#ifdef mxPromisePrint
704
      fprintf(stderr, "fxStepAsync %d\n", the->stack->value.reference->next->ID);
705
#endif
706
0
      fxPromiseThen(the, the->stack->value.reference, resolveAwaitFunction, rejectAwaitFunction, C_NULL, C_NULL);
707
      /* THIS */
708
0
      mxPushUndefined();
709
      /* FUNCTION */
710
0
      mxPushSlot(resolveFunction);
711
0
      mxCall();
712
      /* ARGUMENTS */
713
0
      mxPushSlot(value);
714
      /* COUNT */
715
0
      mxRunCount(1);
716
0
      mxPop();
717
0
    }
718
0
exit:     
719
0
    mxPop();
720
0
  }
721
0
  mxCatch(the) {
722
0
    fxRejectException(the, rejectFunction);
723
0
  }
724
0
}
725
726
void fx_AsyncFunction(txMachine* the)
727
0
{ 
728
0
  txInteger c, i;
729
0
  txStringStream stream;
730
0
  txSlot* module = mxFunctionInstanceHome(mxFunction->value.reference)->value.home.module;
731
0
  if (!module) module = mxProgram.value.reference;
732
  
733
0
  c = mxArgc;
734
0
  i = 0;
735
0
  mxPushStringX("(async function anonymous(");
736
0
  while (c > 1) {
737
0
    fxToString(the, mxArgv(i));
738
0
    fxConcatString(the, the->stack, mxArgv(i));
739
0
    if (c > 2)
740
0
      fxConcatStringC(the, the->stack, ", ");
741
0
    c--;
742
0
    i++;
743
0
  }
744
0
  fxConcatStringC(the, the->stack, "\n){");
745
0
  if (c > 0) {
746
0
    fxToString(the, mxArgv(i));
747
0
    fxConcatString(the, the->stack, mxArgv(i));
748
0
  }
749
0
  fxConcatStringC(the, the->stack, "\n})");
750
0
  stream.slot = the->stack;
751
0
  stream.offset = 0;
752
0
  stream.size = mxStringLength(the->stack->value.string);
753
0
  fxRunScript(the, fxParseScript(the, &stream, fxStringGetter, mxProgramFlag | mxFunctionFlag), C_NULL, C_NULL, C_NULL, C_NULL, module);
754
0
  mxPullSlot(mxResult);
755
0
  if (!mxIsUndefined(mxTarget) && !fxIsSameSlot(the, mxTarget, mxFunction)) {
756
0
    mxPushSlot(mxTarget);
757
0
    fxGetPrototypeFromConstructor(the, &mxAsyncFunctionPrototype);
758
0
    mxResult->value.reference->value.instance.prototype = the->stack->value.reference;
759
0
    mxPop();
760
0
  }
761
0
}