@agoric/xsnap 0.14.3-u13.0 → 0.14.3-u16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. package/README.md +3 -3
  2. package/api.js +4 -2
  3. package/build.env +1 -1
  4. package/moddable/modules/data/base64/base64.js +28 -0
  5. package/moddable/modules/data/base64/manifest.json +11 -0
  6. package/moddable/modules/data/base64/modBase64.c +188 -0
  7. package/moddable/modules/data/binaryMessage/BinaryMessage.js +106 -0
  8. package/moddable/modules/data/crc/crc.c +205 -0
  9. package/moddable/modules/data/crc/crc.js +36 -0
  10. package/moddable/modules/data/crc/manifest.json +8 -0
  11. package/moddable/modules/data/hex/hex.js +28 -0
  12. package/moddable/modules/data/hex/manifest.json +11 -0
  13. package/moddable/modules/data/hex/modHex.c +139 -0
  14. package/moddable/modules/data/logical/logical.js +32 -0
  15. package/moddable/modules/data/logical/modLogical.c +98 -0
  16. package/moddable/modules/data/qrcode/manifest.json +9 -0
  17. package/moddable/modules/data/qrcode/qrcode.c +93 -0
  18. package/moddable/modules/data/qrcode/qrcode.js +23 -0
  19. package/moddable/modules/data/qrcode/qrcodegen.c +1025 -0
  20. package/moddable/modules/data/qrcode/qrcodegen.h +267 -0
  21. package/moddable/modules/data/text/decoder/manifest.json +8 -0
  22. package/moddable/modules/data/text/decoder/textdecoder.c +480 -0
  23. package/moddable/modules/data/text/decoder/textdecoder.js +27 -0
  24. package/moddable/modules/data/text/encoder/manifest.json +8 -0
  25. package/moddable/modules/data/text/encoder/textencoder.c +232 -0
  26. package/moddable/modules/data/text/encoder/textencoder.js +24 -0
  27. package/moddable/modules/data/tinyint/tinyint.c +150 -0
  28. package/moddable/modules/data/tinyint/tinyint.js +53 -0
  29. package/moddable/modules/data/url/manifest.json +17 -0
  30. package/moddable/modules/data/url/url.c +1959 -0
  31. package/moddable/modules/data/url/url.js +210 -0
  32. package/moddable/modules/data/wavreader/manifest.json +8 -0
  33. package/moddable/modules/data/wavreader/wavreader.js +128 -0
  34. package/moddable/modules/data/zlib/deflate.c +161 -0
  35. package/moddable/modules/data/zlib/deflate.js +63 -0
  36. package/moddable/modules/data/zlib/inflate.c +145 -0
  37. package/moddable/modules/data/zlib/inflate.js +66 -0
  38. package/moddable/modules/data/zlib/manifest_deflate.json +9 -0
  39. package/moddable/modules/data/zlib/manifest_inflate.json +9 -0
  40. package/moddable/modules/data/zlib/miniz.c +4924 -0
  41. package/moddable/xs/includes/xs.d.ts +73 -0
  42. package/moddable/xs/includes/xs.h +1533 -0
  43. package/moddable/xs/includes/xsmc.h +206 -0
  44. package/moddable/xs/makefiles/lin/makefile +33 -0
  45. package/moddable/xs/makefiles/lin/xsc.mk +118 -0
  46. package/moddable/xs/makefiles/lin/xsid.mk +90 -0
  47. package/moddable/xs/makefiles/lin/xsl.mk +168 -0
  48. package/moddable/xs/makefiles/lin/xst.mk +201 -0
  49. package/moddable/xs/makefiles/mac/makefile +33 -0
  50. package/moddable/xs/makefiles/mac/xsc.mk +130 -0
  51. package/moddable/xs/makefiles/mac/xsid.mk +102 -0
  52. package/moddable/xs/makefiles/mac/xsl.mk +177 -0
  53. package/moddable/xs/makefiles/mac/xst.mk +203 -0
  54. package/moddable/xs/makefiles/mac/xst_no_asan.txt +52 -0
  55. package/moddable/xs/makefiles/win/build.bat +26 -0
  56. package/moddable/xs/makefiles/win/xsc.mak +142 -0
  57. package/moddable/xs/makefiles/win/xsid.mak +113 -0
  58. package/moddable/xs/makefiles/win/xsl.mak +186 -0
  59. package/moddable/xs/makefiles/win/xst.mak +195 -0
  60. package/moddable/xs/platforms/lin_xs.h +99 -0
  61. package/moddable/xs/platforms/mac_xs.h +97 -0
  62. package/moddable/xs/platforms/wasm_xs.h +79 -0
  63. package/moddable/xs/platforms/win_xs.h +104 -0
  64. package/moddable/xs/platforms/xsHost.h +63 -0
  65. package/moddable/xs/platforms/xsPlatform.h +618 -0
  66. package/moddable/xs/sources/xsAPI.c +2555 -0
  67. package/moddable/xs/sources/xsAll.c +294 -0
  68. package/moddable/xs/sources/xsAll.h +2741 -0
  69. package/moddable/xs/sources/xsArguments.c +222 -0
  70. package/moddable/xs/sources/xsArray.c +2657 -0
  71. package/moddable/xs/sources/xsAtomics.c +844 -0
  72. package/moddable/xs/sources/xsBigInt.c +1859 -0
  73. package/moddable/xs/sources/xsBoolean.c +109 -0
  74. package/moddable/xs/sources/xsCode.c +4493 -0
  75. package/moddable/xs/sources/xsCommon.c +1710 -0
  76. package/moddable/xs/sources/xsCommon.h +1142 -0
  77. package/moddable/xs/sources/xsDataView.c +2890 -0
  78. package/moddable/xs/sources/xsDate.c +1541 -0
  79. package/moddable/xs/sources/xsDebug.c +2710 -0
  80. package/moddable/xs/sources/xsDefaults.c +134 -0
  81. package/moddable/xs/sources/xsError.c +353 -0
  82. package/moddable/xs/sources/xsFunction.c +776 -0
  83. package/moddable/xs/sources/xsGenerator.c +865 -0
  84. package/moddable/xs/sources/xsGlobal.c +839 -0
  85. package/moddable/xs/sources/xsJSON.c +1091 -0
  86. package/moddable/xs/sources/xsLexical.c +1969 -0
  87. package/moddable/xs/sources/xsLockdown.c +933 -0
  88. package/moddable/xs/sources/xsMapSet.c +1649 -0
  89. package/moddable/xs/sources/xsMarshall.c +1020 -0
  90. package/moddable/xs/sources/xsMath.c +624 -0
  91. package/moddable/xs/sources/xsMemory.c +1941 -0
  92. package/moddable/xs/sources/xsModule.c +3101 -0
  93. package/moddable/xs/sources/xsNumber.c +560 -0
  94. package/moddable/xs/sources/xsObject.c +1102 -0
  95. package/moddable/xs/sources/xsPlatforms.c +480 -0
  96. package/moddable/xs/sources/xsProfile.c +577 -0
  97. package/moddable/xs/sources/xsPromise.c +1199 -0
  98. package/moddable/xs/sources/xsProperty.c +636 -0
  99. package/moddable/xs/sources/xsProxy.c +1014 -0
  100. package/moddable/xs/sources/xsRegExp.c +1168 -0
  101. package/moddable/xs/sources/xsRun.c +4889 -0
  102. package/moddable/xs/sources/xsScope.c +1293 -0
  103. package/moddable/xs/sources/xsScript.c +288 -0
  104. package/moddable/xs/sources/xsScript.h +1186 -0
  105. package/moddable/xs/sources/xsSnapshot.c +2161 -0
  106. package/moddable/xs/sources/xsSnapshot.h +51 -0
  107. package/moddable/xs/sources/xsSourceMap.c +218 -0
  108. package/moddable/xs/sources/xsString.c +3332 -0
  109. package/moddable/xs/sources/xsSymbol.c +503 -0
  110. package/moddable/xs/sources/xsSyntaxical.c +4193 -0
  111. package/moddable/xs/sources/xsTree.c +1893 -0
  112. package/moddable/xs/sources/xsType.c +1488 -0
  113. package/moddable/xs/sources/xsdtoa.c +6672 -0
  114. package/moddable/xs/sources/xsmc.c +340 -0
  115. package/moddable/xs/sources/xsre.c +7578 -0
  116. package/package.json +39 -22
  117. package/scripts/get_xsnap_version.sh +14 -0
  118. package/scripts/test-package.sh +21 -0
  119. package/src/avaAssertXS.js +6 -2
  120. package/src/avaHandler.cjs +2 -5
  121. package/src/avaXS.js +7 -8
  122. package/src/build.js +161 -28
  123. package/src/replay.js +0 -3
  124. package/src/xsnap.js +105 -91
  125. package/src/xsrepl.js +2 -3
  126. package/xsnap-native/xsnap/makefiles/lin/makefile +10 -0
  127. package/xsnap-native/xsnap/makefiles/lin/xsnap-worker.mk +156 -0
  128. package/xsnap-native/xsnap/makefiles/lin/xsnap.mk +144 -0
  129. package/xsnap-native/xsnap/makefiles/mac/makefile +10 -0
  130. package/xsnap-native/xsnap/makefiles/mac/xsnap-worker.mk +165 -0
  131. package/xsnap-native/xsnap/makefiles/mac/xsnap.mk +153 -0
  132. package/xsnap-native/xsnap/sources/xsnap-worker.c +1008 -0
  133. package/xsnap-native/xsnap/sources/xsnap.c +717 -0
  134. package/xsnap-native/xsnap/sources/xsnap.h +142 -0
  135. package/xsnap-native/xsnap/sources/xsnapPlatform.c +1501 -0
  136. package/xsnap-native/xsnap/sources/xsnapPlatform.h +105 -0
  137. package/CHANGELOG.md +0 -646
@@ -0,0 +1,776 @@
1
+ /*
2
+ * Copyright (c) 2016-2017 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
+ void fxBuildFunction(txMachine* the)
44
+ {
45
+ txSlot* slot;
46
+ txSlot* function;
47
+ txSlot* constructor;
48
+ mxPush(mxFunctionPrototype);
49
+ slot = fxLastProperty(the, the->stack->value.reference);
50
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Function_prototype_apply), 2, mxID(_apply), XS_DONT_ENUM_FLAG);
51
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Function_prototype_bind), 1, mxID(_bind), XS_DONT_ENUM_FLAG);
52
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Function_prototype_call), 1, mxID(_call), XS_DONT_ENUM_FLAG);
53
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Function_prototype_toString), 0, mxID(_toString), XS_DONT_ENUM_FLAG);
54
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Function_prototype_hasInstance), 1, mxID(_Symbol_hasInstance), XS_GET_ONLY);
55
+ function = mxThrowTypeErrorFunction.value.reference;
56
+ slot = slot->next = fxNewSlot(the);
57
+ slot->flag = XS_DONT_ENUM_FLAG;
58
+ slot->ID = mxID(_arguments);
59
+ slot->kind = XS_ACCESSOR_KIND;
60
+ slot->value.accessor.getter = function;
61
+ slot->value.accessor.setter = function;
62
+ slot = slot->next = fxNewSlot(the);
63
+ slot->flag = XS_DONT_ENUM_FLAG;
64
+ slot->ID = mxID(_caller);
65
+ slot->kind = XS_ACCESSOR_KIND;
66
+ slot->value.accessor.getter = function;
67
+ slot->value.accessor.setter = function;
68
+ constructor = fxBuildHostConstructor(the, mxCallback(fx_Function), 1, mxID(_Function));
69
+ mxFunctionConstructor = *the->stack;
70
+ mxPop();
71
+
72
+ mxPush(mxFunctionPrototype);
73
+ slot = fxLastProperty(the, fxNewObjectInstance(the));
74
+ slot = fxNextStringXProperty(the, slot, "AsyncFunction", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
75
+ mxAsyncFunctionPrototype = *the->stack;
76
+ slot = fxBuildHostConstructor(the, mxCallback(fx_AsyncFunction), 1, mxID(_AsyncFunction));
77
+ slot->value.instance.prototype = constructor;
78
+ mxPop();
79
+ slot = mxBehaviorGetProperty(the, mxAsyncFunctionPrototype.value.reference, mxID(_constructor), 0, XS_OWN);
80
+ slot->flag |= XS_DONT_SET_FLAG;
81
+ }
82
+
83
+ void fxCheckCallable(txMachine* the, txSlot* slot)
84
+ {
85
+ if (fxIsCallable(the, slot))
86
+ return;
87
+ mxTypeError("this is no Function instance");
88
+ }
89
+
90
+ txSlot* fxCheckFunctionInstance(txMachine* the, txSlot* slot)
91
+ {
92
+ if (slot->kind == XS_REFERENCE_KIND) {
93
+ slot = slot->value.reference;
94
+ if (fxIsFunction(the, slot))
95
+ return slot;
96
+ }
97
+ mxTypeError("this is no Function instance");
98
+ return C_NULL;
99
+ }
100
+
101
+ txBoolean fxIsCallable(txMachine* the, txSlot* slot)
102
+ {
103
+ if (slot->kind == XS_REFERENCE_KIND)
104
+ return fxIsFunction(the, slot->value.reference);
105
+ #ifdef mxHostFunctionPrimitive
106
+ if (slot->kind == XS_HOST_FUNCTION_KIND)
107
+ return 1;
108
+ #endif
109
+ return 0;
110
+ }
111
+
112
+ txBoolean fxIsFunction(txMachine* the, txSlot* instance)
113
+ {
114
+ again:
115
+ if (instance) {
116
+ txSlot* exotic = instance->next;
117
+ if (exotic && (exotic->flag & XS_INTERNAL_FLAG)) {
118
+ if (((exotic->kind == XS_CALLBACK_KIND) || (exotic->kind == XS_CALLBACK_X_KIND) || (exotic->kind == XS_CODE_KIND) || (exotic->kind == XS_CODE_X_KIND)))
119
+ return 1;
120
+ if (exotic->kind == XS_PROXY_KIND) {
121
+ instance = exotic->value.proxy.target;
122
+ goto again;
123
+ }
124
+ }
125
+ }
126
+ return 0;
127
+ }
128
+
129
+ txSlot* fxNewFunctionInstance(txMachine* the, txID name)
130
+ {
131
+ txSlot* instance;
132
+ txSlot* property;
133
+
134
+ instance = fxNewObjectInstance(the);
135
+ instance->flag |= XS_CAN_CALL_FLAG;
136
+
137
+ /* CODE */
138
+ property = instance->next = fxNewSlot(the);
139
+ property->flag = XS_INTERNAL_FLAG;
140
+ property->kind = mxEmptyCode.kind;
141
+ property->value.code.address = mxEmptyCode.value.code.address;
142
+ property->value.code.closures = C_NULL;
143
+
144
+ /* HOME */
145
+ property = property->next = fxNewSlot(the);
146
+ property->flag = XS_INTERNAL_FLAG;
147
+ property->kind = XS_HOME_KIND;
148
+ property->value.home.object = C_NULL;
149
+ if (the->frame && (mxFunction->kind == XS_REFERENCE_KIND) && (mxIsFunction(mxFunction->value.reference))) {
150
+ txSlot* slot = mxFunctionInstanceHome(mxFunction->value.reference);
151
+ property->value.home.module = slot->value.home.module;
152
+ }
153
+ else
154
+ property->value.home.module = C_NULL;
155
+
156
+ /* LENGTH */
157
+ if (gxDefaults.newFunctionLength)
158
+ gxDefaults.newFunctionLength(the, instance, 0);
159
+
160
+ /* NAME */
161
+ fxRenameFunction(the, instance, name, 0, XS_NO_ID, C_NULL);
162
+
163
+ return instance;
164
+ }
165
+
166
+ void fxDefaultFunctionPrototype(txMachine* the)
167
+ {
168
+ txSlot* instance;
169
+ txSlot* property;
170
+ instance = the->stack->value.reference;
171
+ instance->flag |= XS_CAN_CONSTRUCT_FLAG;
172
+ property = fxLastProperty(the, instance);
173
+ mxPush(mxObjectPrototype);
174
+ instance = fxNewObjectInstance(the);
175
+ fxNextSlotProperty(the, property, the->stack, mxID(_prototype), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG);
176
+ mxPop();
177
+ fxNextSlotProperty(the, instance, the->stack, mxID(_constructor), XS_DONT_ENUM_FLAG);
178
+ }
179
+
180
+ txSlot* fxGetPrototypeFromConstructor(txMachine* the, txSlot* defaultPrototype)
181
+ {
182
+ txSlot* result = the->stack;
183
+ fxCheckCallable(the, result);
184
+ mxDub();
185
+ mxGetID(mxID(_prototype));
186
+ if (!mxIsReference(the->stack)) {
187
+ txSlot* instance = result->value.reference;
188
+ txSlot* proxy = instance->next;
189
+ if (proxy->kind == XS_PROXY_KIND) {
190
+ if (!proxy->value.proxy.handler)
191
+ mxTypeError("(proxy).%s: handler is no object", fxName(the, mxID(_prototype)));
192
+ if (!proxy->value.proxy.target)
193
+ mxTypeError("(proxy).%s: target is no object", fxName(the, mxID(_prototype)));
194
+ }
195
+ the->stack->kind = defaultPrototype->kind;
196
+ the->stack->value = defaultPrototype->value;
197
+ }
198
+ mxPullSlot(result);
199
+ return result->value.reference;
200
+ }
201
+
202
+ #ifndef mxLink
203
+ txSlot* fxNewFunctionLength(txMachine* the, txSlot* instance, txNumber length)
204
+ {
205
+ txSlot* property = mxBehaviorGetProperty(the, instance, mxID(_length), 0, XS_OWN);
206
+ if (!property)
207
+ property = fxNextIntegerProperty(the, fxLastProperty(the, instance), 0, mxID(_length), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
208
+ if (length <= 0x7FFFFFFF) {
209
+ property->kind = XS_INTEGER_KIND;
210
+ property->value.integer = (txInteger)length;
211
+ }
212
+ else {
213
+ property->kind = XS_NUMBER_KIND;
214
+ property->value.number = length;
215
+ }
216
+ return property;
217
+ }
218
+
219
+ txSlot* fxNewFunctionName(txMachine* the, txSlot* instance, txID id, txIndex index, txID former, txString prefix)
220
+ {
221
+ txSlot* property;
222
+ txSlot* key;
223
+ property = mxBehaviorGetProperty(the, instance, mxID(_name), 0, XS_OWN);
224
+ if (property) {
225
+ if ((property->kind != mxEmptyString.kind) || (property->value.string != mxEmptyString.value.string))
226
+ return property;
227
+ }
228
+ else
229
+ property = fxNextSlotProperty(the, fxLastProperty(the, instance), &mxEmptyString, mxID(_name), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
230
+ if (id != XS_NO_ID) {
231
+ key = fxGetKey(the, (txID)id);
232
+ if (key) {
233
+ txKind kind = mxGetKeySlotKind(key);
234
+ if (kind == XS_KEY_KIND) {
235
+ property->kind = XS_STRING_KIND;
236
+ property->value.string = key->value.key.string;
237
+ if (!(key->flag & XS_DONT_ENUM_FLAG))
238
+ fxAdornStringC(the, "[", property, "]");
239
+ }
240
+ else if (kind == XS_KEY_X_KIND) {
241
+ property->kind = XS_STRING_X_KIND;
242
+ property->value.string = key->value.key.string;
243
+ if (!(key->flag & XS_DONT_ENUM_FLAG))
244
+ fxAdornStringC(the, "[", property, "]");
245
+ }
246
+ else if ((kind == XS_STRING_KIND) || (kind == XS_STRING_X_KIND)) {
247
+ property->kind = kind;
248
+ property->value.string = key->value.string;
249
+ fxAdornStringC(the, "[", property, "]");
250
+ }
251
+ else {
252
+ property->kind = mxEmptyString.kind;
253
+ property->value = mxEmptyString.value;
254
+ }
255
+ }
256
+ else {
257
+ property->kind = mxEmptyString.kind;
258
+ property->value = mxEmptyString.value;
259
+ }
260
+ }
261
+ else if (former) {
262
+ char buffer[16];
263
+ fxCopyStringC(the, property, fxNumberToString(the->dtoa, index, buffer, sizeof(buffer), 0, 0));
264
+ }
265
+ if (prefix)
266
+ fxAdornStringC(the, prefix, property, C_NULL);
267
+ return property;
268
+ }
269
+ #endif
270
+
271
+ void fxRenameFunction(txMachine* the, txSlot* instance, txID id, txIndex index, txID former, txString prefix)
272
+ {
273
+ txSlot* property;
274
+ if (instance->flag & XS_MARK_FLAG)
275
+ return;
276
+ property = mxFunctionInstanceCode(instance);
277
+ if ((property->ID == XS_NO_ID) || (property->ID == former)) {
278
+ if (id != XS_NO_ID)
279
+ property->ID = (txID)id;
280
+ }
281
+ if (gxDefaults.newFunctionName)
282
+ property = gxDefaults.newFunctionName(the, instance, id, index, former, prefix);
283
+ }
284
+
285
+ void fx_Function(txMachine* the)
286
+ {
287
+ txInteger c, i;
288
+ txStringStream stream;
289
+ txSlot* module = mxFunctionInstanceHome(mxFunction->value.reference)->value.home.module;
290
+ if (!module) module = mxProgram.value.reference;
291
+
292
+ c = mxArgc;
293
+ i = 0;
294
+ mxPushStringX("(function anonymous(");
295
+ while (c > 1) {
296
+ fxToString(the, mxArgv(i));
297
+ fxConcatString(the, the->stack, mxArgv(i));
298
+ if (c > 2)
299
+ fxConcatStringC(the, the->stack, ", ");
300
+ c--;
301
+ i++;
302
+ }
303
+ fxConcatStringC(the, the->stack, "\n){");
304
+ if (c > 0) {
305
+ fxToString(the, mxArgv(i));
306
+ fxConcatString(the, the->stack, mxArgv(i));
307
+ }
308
+ fxConcatStringC(the, the->stack, "\n})");
309
+ stream.slot = the->stack;
310
+ stream.offset = 0;
311
+ stream.size = mxStringLength(the->stack->value.string);
312
+ fxRunScript(the, fxParseScript(the, &stream, fxStringGetter, mxProgramFlag | mxFunctionFlag), C_NULL, C_NULL, C_NULL, C_NULL, module);
313
+ mxPullSlot(mxResult);
314
+ if (!mxIsUndefined(mxTarget) && !fxIsSameSlot(the, mxTarget, mxFunction)) {
315
+ mxPushSlot(mxTarget);
316
+ fxGetPrototypeFromConstructor(the, &mxFunctionPrototype);
317
+ mxResult->value.reference->value.instance.prototype = the->stack->value.reference;
318
+ mxPop();
319
+ }
320
+ }
321
+
322
+ void fx_Function_prototype_apply(txMachine* the)
323
+ {
324
+ txIndex c, i;
325
+ fxCheckCallable(the, mxThis);
326
+ /* THIS */
327
+ if (mxArgc < 1)
328
+ mxPushUndefined();
329
+ else
330
+ mxPushSlot(mxArgv(0));
331
+ /* FUNCTION */
332
+ mxPushSlot(mxThis);
333
+ mxCall();
334
+ /* ARGUMENTS */
335
+ if ((mxArgc < 2) || (mxArgv(1)->kind == XS_UNDEFINED_KIND) || (mxArgv(1)->kind == XS_NULL_KIND))
336
+ c = 0;
337
+ else {
338
+ if (mxArgv(1)->kind != XS_REFERENCE_KIND)
339
+ mxTypeError("argArray is no object");
340
+ fxToInstance(the, mxArgv(1));
341
+ mxPushSlot(mxArgv(1));
342
+ mxGetID(mxID(_length));
343
+ c = (txIndex)fxToLength(the, the->stack);
344
+ mxPop();
345
+ for (i = 0; i < c; i++) {
346
+ mxPushSlot(mxArgv(1));
347
+ mxGetIndex(i);
348
+ }
349
+ }
350
+ mxRunCount(c);
351
+ mxPullSlot(mxResult);
352
+ }
353
+
354
+ void fx_Function_prototype_bind(txMachine* the)
355
+ {
356
+ txSlot* function = fxToInstance(the, mxThis);
357
+ txSlot* instance;
358
+ txSlot* property;
359
+ txSlot* arguments;
360
+ txSlot* argument;
361
+ txSize c = mxArgc, i;
362
+
363
+ fxCheckCallable(the, mxThis);
364
+ mxPushNull();
365
+ if (mxBehaviorGetPrototype(the, function, the->stack))
366
+ instance = fxNewObjectInstance(the);
367
+ else {
368
+ mxPop();
369
+ instance = fxNewInstance(the);
370
+ }
371
+ instance->flag |= function->flag & (XS_CAN_CALL_FLAG | XS_CAN_CONSTRUCT_FLAG);
372
+ mxPullSlot(mxResult);
373
+
374
+ /* CODE */
375
+ property = instance->next = fxNewSlot(the);
376
+ property->flag = XS_INTERNAL_FLAG;
377
+ property->kind = XS_CALLBACK_KIND;
378
+ property->value.callback.address = fx_Function_prototype_bound;
379
+ property->value.callback.closures = C_NULL;
380
+
381
+ /* HOME */
382
+ property = property->next = fxNewSlot(the);
383
+ property->flag = XS_INTERNAL_FLAG;
384
+ property->kind = XS_HOME_KIND;
385
+ property->value.home.object = C_NULL;
386
+ property->value.home.module = C_NULL;
387
+
388
+ property = fxNextSlotProperty(the, property, mxThis, mxID(_boundFunction), XS_INTERNAL_FLAG);
389
+ if (c > 0)
390
+ property = fxNextSlotProperty(the, property, mxArgv(0), mxID(_boundThis), XS_INTERNAL_FLAG);
391
+ else
392
+ property = fxNextUndefinedProperty(the, property, mxID(_boundThis), XS_INTERNAL_FLAG);
393
+
394
+ if (c > 1) {
395
+ mxPush(mxArrayPrototype);
396
+ arguments = fxNewArrayInstance(the);
397
+ argument = fxLastProperty(the, arguments);
398
+ for (i = 1; i < c; i++) {
399
+ argument->next = fxNewSlot(the);
400
+ argument = argument->next;
401
+ argument->kind = mxArgv(i)->kind;
402
+ argument->value = mxArgv(i)->value;
403
+ }
404
+ arguments->next->value.array.length = c - 1;
405
+ fxCacheArray(the, arguments);
406
+ property = fxNextSlotProperty(the, property, the->stack, mxID(_boundArguments), XS_INTERNAL_FLAG);
407
+ mxPop();
408
+ }
409
+ else {
410
+ property = fxNextNullProperty(the, property, mxID(_boundArguments), XS_INTERNAL_FLAG);
411
+ }
412
+
413
+ if (gxDefaults.newFunctionLength) {
414
+ txNumber length = 0;
415
+ mxPushUndefined();
416
+ if (mxBehaviorGetOwnProperty(the, mxThis->value.reference, mxID(_length), 0, the->stack)) {
417
+ mxPushSlot(mxThis);
418
+ mxGetID(mxID(_length));
419
+ property = the->stack;
420
+ if (property->kind == XS_INTEGER_KIND) {
421
+ length = property->value.integer;
422
+ }
423
+ else if (property->kind == XS_NUMBER_KIND) {
424
+ length = property->value.number;
425
+ if (c_isnan(length))
426
+ length = 0;
427
+ else
428
+ length = c_trunc(length);
429
+ }
430
+ if (c > 1)
431
+ length -= c - 1;
432
+ if (length < 0)
433
+ length = 0;
434
+ mxPop();
435
+ }
436
+ mxPop();
437
+ gxDefaults.newFunctionLength(the, instance, length);
438
+ }
439
+
440
+ if (gxDefaults.newFunctionName) {
441
+ txSize length = 0;
442
+ txString name;
443
+ mxPushSlot(mxThis);
444
+ mxGetID(mxID(_name));
445
+ if ((the->stack->kind == XS_STRING_KIND) || (the->stack->kind == XS_STRING_X_KIND))
446
+ length = mxStringLength(the->stack->value.string);
447
+ property = fxNextSlotProperty(the, fxLastProperty(the, instance), &mxEmptyString, mxID(_name), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
448
+ name = (txString)fxNewChunk(the, fxAddChunkSizes(the, length, 6 + 1));
449
+ c_memcpy(name, "bound ", 6);
450
+ if (length)
451
+ c_memcpy(name + 6, the->stack->value.string, length);
452
+ name[6 + length] = 0;
453
+ property->value.string = name;
454
+ property->kind = XS_STRING_KIND;
455
+ mxPop();
456
+ }
457
+ }
458
+
459
+ void fx_Function_prototype_bound(txMachine* the)
460
+ {
461
+ txSlot* function = fxToInstance(the, mxFunction);
462
+ txSlot* boundArguments;
463
+ txInteger c, i;
464
+ txSlot* argument;
465
+ /* THIS */
466
+ if (mxTarget->kind == XS_UNDEFINED_KIND) {
467
+ mxPushSlot(mxFunctionInstanceHome(function)->next->next);
468
+ }
469
+ else
470
+ mxPushUninitialized();
471
+ /* FUNCTION */
472
+ mxPushSlot(mxFunctionInstanceHome(function)->next);
473
+ /* TARGET */
474
+ if (fxIsSameSlot(the, mxFunction, mxTarget)) {
475
+ txSlot* slot = the->stack;
476
+ mxPushSlot(slot);
477
+ }
478
+ else
479
+ mxPushSlot(mxTarget);
480
+ /* RESULT */
481
+ mxPushUndefined();
482
+ mxPushUninitialized();
483
+ mxPushUninitialized();
484
+ /* ARGUMENTS */
485
+ mxPushSlot(mxFunctionInstanceHome(function)->next->next->next);
486
+ if (the->stack->kind == XS_REFERENCE_KIND) {
487
+ boundArguments = fxGetInstance(the, the->stack);
488
+ mxPop();
489
+ c = boundArguments->next->value.array.length;
490
+ argument = boundArguments->next->value.array.address;
491
+ for (i = 0; i < c; i++) {
492
+ mxPushSlot(argument);
493
+ argument++;
494
+ }
495
+ }
496
+ else {
497
+ mxPop();
498
+ c = 0;
499
+ }
500
+ for (i = 0; i < mxArgc; i++)
501
+ mxPushSlot(mxArgv(i));
502
+ mxRunCount(c + i);
503
+ mxPullSlot(mxResult);
504
+ }
505
+
506
+ void fx_Function_prototype_call(txMachine* the)
507
+ {
508
+ txInteger c, i;
509
+ fxCheckCallable(the, mxThis);
510
+ /* THIS */
511
+ if (mxArgc < 1)
512
+ mxPushUndefined();
513
+ else
514
+ mxPushSlot(mxArgv(0));
515
+ /* FUNCTION */
516
+ mxPushSlot(mxThis);
517
+ mxCall();
518
+ /* ARGUMENTS */
519
+ c = mxArgc;
520
+ i = 1;
521
+ while (i < c) {
522
+ mxPushSlot(mxArgv(i));
523
+ i++;
524
+ }
525
+ mxRunCount(i - 1);
526
+ mxPullSlot(mxResult);
527
+ }
528
+
529
+ void fx_Function_prototype_hasInstance(txMachine* the)
530
+ {
531
+ txSlot* function;
532
+ txSlot* slot;
533
+ txSlot* instance;
534
+ txSlot* prototype;
535
+ mxResult->kind = XS_BOOLEAN_KIND;
536
+ mxResult->value.boolean = 0;
537
+ if (!fxIsCallable(the, mxThis))
538
+ return;
539
+ function = fxToInstance(the, mxThis);
540
+ if (!function)
541
+ return;
542
+ if (mxIsFunction(function)) {
543
+ slot = mxFunctionInstanceHome(function)->next;
544
+ if (slot && (slot->flag & XS_INTERNAL_FLAG) && (slot->ID == mxID(_boundFunction))) {
545
+ if (!fxIsCallable(the, slot))
546
+ return;
547
+ function = fxToInstance(the, slot);
548
+ if (!function)
549
+ return;
550
+ }
551
+ }
552
+ if (mxArgc == 0)
553
+ return;
554
+ instance = fxGetInstance(the, mxArgv(0));
555
+ if (!instance)
556
+ return;
557
+ mxPushReference(function);
558
+ mxGetID(mxID(_prototype));
559
+ prototype = fxGetInstance(the, the->stack);
560
+ mxPop();
561
+ if (!prototype)
562
+ mxTypeError("prototype is no object");
563
+ if (prototype->ID) {
564
+ txSlot* alias = the->aliasArray[prototype->ID];
565
+ if (alias)
566
+ prototype = alias;
567
+ }
568
+ mxPushNull();
569
+ while (mxBehaviorGetPrototype(the, instance, the->stack)) {
570
+ instance = the->stack->value.reference;
571
+ if (instance == prototype) {
572
+ mxResult->value.boolean = 1;
573
+ break;
574
+ }
575
+ }
576
+ mxPop();
577
+ }
578
+
579
+ void fx_Function_prototype_toString(txMachine* the)
580
+ {
581
+ fxCheckFunctionInstance(the, mxThis);
582
+ mxPushStringX("function [\"");
583
+ mxPushSlot(mxThis);
584
+ mxGetID(mxID(_name));
585
+ if ((the->stack->kind == XS_STRING_KIND) || (the->stack->kind == XS_STRING_X_KIND))
586
+ fxConcatString(the, the->stack + 1, the->stack);
587
+ mxPop();
588
+ mxPushStringX("\"] (){[native code]}");
589
+ fxConcatString(the, the->stack + 1, the->stack);
590
+ mxPop();
591
+ mxPullSlot(mxResult);
592
+ }
593
+
594
+ txSlot* fxNewAsyncInstance(txMachine* the)
595
+ {
596
+ txSlot* instance;
597
+ txSlot* property;
598
+ txSlot* promise;
599
+ txSlot* status;
600
+ txSlot* function;
601
+ txSlot* home;
602
+
603
+ mxPushUndefined();
604
+
605
+ instance = fxNewSlot(the);
606
+ instance->kind = XS_INSTANCE_KIND;
607
+ instance->value.instance.garbage = C_NULL;
608
+ instance->value.instance.prototype = C_NULL;
609
+ the->stack->value.reference = instance;
610
+ the->stack->kind = XS_REFERENCE_KIND;
611
+
612
+ property = instance->next = fxNewSlot(the);
613
+ property->flag = XS_INTERNAL_FLAG;
614
+ property->kind = XS_STACK_KIND;
615
+ property->ID = XS_NO_ID;
616
+ property->value.stack.length = 0;
617
+ property->value.stack.address = C_NULL;
618
+
619
+ property = fxNextIntegerProperty(the, property, XS_CODE_START_ASYNC, XS_NO_ID, XS_INTERNAL_FLAG);
620
+
621
+ mxPush(mxPromisePrototype);
622
+ promise = fxNewPromiseInstance(the);
623
+ status = mxPromiseStatus(promise);
624
+ status->value.integer = mxPendingStatus;
625
+ property = fxNextSlotProperty(the, property, the->stack, XS_NO_ID, XS_INTERNAL_FLAG);
626
+ mxPop();
627
+
628
+ fxPushPromiseFunctions(the, promise);
629
+ property = fxNextSlotProperty(the, property, the->stack + 1, XS_NO_ID, XS_INTERNAL_FLAG);
630
+ property = fxNextSlotProperty(the, property, the->stack, XS_NO_ID, XS_INTERNAL_FLAG);
631
+ mxPop();
632
+ mxPop();
633
+
634
+ function = fxNewHostFunction(the, fxResolveAwait, 1, XS_NO_ID, mxResolveAwaitProfileID);
635
+ home = mxFunctionInstanceHome(function);
636
+ home->value.home.object = instance;
637
+ property = fxNextSlotProperty(the, property, the->stack, XS_NO_ID, XS_INTERNAL_FLAG);
638
+ mxPop();
639
+
640
+ function = fxNewHostFunction(the, fxRejectAwait, 1, XS_NO_ID, mxRejectAwaitProfileID);
641
+ home = mxFunctionInstanceHome(function);
642
+ home->value.home.object = instance;
643
+ property = fxNextSlotProperty(the, property, the->stack, XS_NO_ID, XS_INTERNAL_FLAG);
644
+ mxPop();
645
+
646
+ return instance;
647
+ }
648
+
649
+ void fxResolveAwait(txMachine* the)
650
+ {
651
+ txSlot* slot = mxFunctionInstanceHome(mxFunction->value.reference);
652
+ txSlot* instance = slot->value.home.object;
653
+ the->scratch.kind = mxArgv(0)->kind;
654
+ the->scratch.value = mxArgv(0)->value;
655
+ fxStepAsync(the, instance, XS_NO_STATUS);
656
+ }
657
+
658
+ void fxRejectAwait(txMachine* the)
659
+ {
660
+ txSlot* slot = mxFunctionInstanceHome(mxFunction->value.reference);
661
+ txSlot* instance = slot->value.home.object;
662
+ the->scratch.kind = mxArgv(0)->kind;
663
+ the->scratch.value = mxArgv(0)->value;
664
+ fxStepAsync(the, instance, XS_THROW_STATUS);
665
+ }
666
+
667
+ void fxRunAsync(txMachine* the, txSlot* instance)
668
+ {
669
+ txSlot* promise = instance->next->next->next;
670
+ fxBeginHost(the);
671
+ the->scratch.kind = XS_UNDEFINED_KIND;
672
+ fxStepAsync(the, instance, XS_NO_STATUS);
673
+ fxEndHost(the);
674
+ mxResult->kind = promise->kind;
675
+ mxResult->value = promise->value;
676
+ }
677
+
678
+ void fxStepAsync(txMachine* the, txSlot* instance, txFlag status)
679
+ {
680
+ txSlot* state = instance->next->next;
681
+ txSlot* promise = state->next;
682
+ txSlot* resolveFunction = promise->next;
683
+ txSlot* rejectFunction = resolveFunction->next;
684
+ txSlot* resolveAwaitFunction = rejectFunction->next;
685
+ txSlot* rejectAwaitFunction = resolveAwaitFunction->next;
686
+ txSlot* value;
687
+ mxTry(the) {
688
+ the->status = status;
689
+ state->value.integer = XS_NO_CODE;
690
+ fxRunID(the, instance, XS_NO_ID);
691
+ value = the->stack;
692
+ if (state->value.integer == XS_NO_CODE) {
693
+ /* THIS */
694
+ mxPushUndefined();
695
+ /* FUNCTION */
696
+ mxPushSlot(resolveFunction);
697
+ mxCall();
698
+ /* ARGUMENTS */
699
+ mxPushSlot(value);
700
+ mxRunCount(1);
701
+ mxPop();
702
+ }
703
+ else {
704
+ if (mxIsReference(value) && mxIsPromise(value->value.reference)) {
705
+ mxDub();
706
+ mxGetID(mxID(_constructor));
707
+ if (fxIsSameValue(the, &mxPromiseConstructor, the->stack, 0)) {
708
+ mxPop();
709
+ fxPromiseThen(the, value->value.reference, resolveAwaitFunction, rejectAwaitFunction, C_NULL, C_NULL);
710
+ goto exit;
711
+ }
712
+ mxPop();
713
+ }
714
+ mxTemporary(resolveFunction);
715
+ mxTemporary(rejectFunction);
716
+ mxPush(mxPromiseConstructor);
717
+ fxNewPromiseCapability(the, resolveFunction, rejectFunction);
718
+ #ifdef mxPromisePrint
719
+ fprintf(stderr, "fxStepAsync %d\n", the->stack->value.reference->next->ID);
720
+ #endif
721
+ fxPromiseThen(the, the->stack->value.reference, resolveAwaitFunction, rejectAwaitFunction, C_NULL, C_NULL);
722
+ /* THIS */
723
+ mxPushUndefined();
724
+ /* FUNCTION */
725
+ mxPushSlot(resolveFunction);
726
+ mxCall();
727
+ /* ARGUMENTS */
728
+ mxPushSlot(value);
729
+ /* COUNT */
730
+ mxRunCount(1);
731
+ mxPop();
732
+ }
733
+ exit:
734
+ mxPop();
735
+ }
736
+ mxCatch(the) {
737
+ fxRejectException(the, rejectFunction);
738
+ }
739
+ }
740
+
741
+ void fx_AsyncFunction(txMachine* the)
742
+ {
743
+ txInteger c, i;
744
+ txStringStream stream;
745
+ txSlot* module = mxFunctionInstanceHome(mxFunction->value.reference)->value.home.module;
746
+ if (!module) module = mxProgram.value.reference;
747
+
748
+ c = mxArgc;
749
+ i = 0;
750
+ mxPushStringX("(async function anonymous(");
751
+ while (c > 1) {
752
+ fxToString(the, mxArgv(i));
753
+ fxConcatString(the, the->stack, mxArgv(i));
754
+ if (c > 2)
755
+ fxConcatStringC(the, the->stack, ", ");
756
+ c--;
757
+ i++;
758
+ }
759
+ fxConcatStringC(the, the->stack, "\n){");
760
+ if (c > 0) {
761
+ fxToString(the, mxArgv(i));
762
+ fxConcatString(the, the->stack, mxArgv(i));
763
+ }
764
+ fxConcatStringC(the, the->stack, "\n})");
765
+ stream.slot = the->stack;
766
+ stream.offset = 0;
767
+ stream.size = mxStringLength(the->stack->value.string);
768
+ fxRunScript(the, fxParseScript(the, &stream, fxStringGetter, mxProgramFlag | mxFunctionFlag), C_NULL, C_NULL, C_NULL, C_NULL, module);
769
+ mxPullSlot(mxResult);
770
+ if (!mxIsUndefined(mxTarget) && !fxIsSameSlot(the, mxTarget, mxFunction)) {
771
+ mxPushSlot(mxTarget);
772
+ fxGetPrototypeFromConstructor(the, &mxAsyncFunctionPrototype);
773
+ mxResult->value.reference->value.instance.prototype = the->stack->value.reference;
774
+ mxPop();
775
+ }
776
+ }