Coverage Report

Created: 2025-12-14 06:18

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