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