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