Skip to content

Commit 75b157b

Browse files
author
Patrick Soquet
committed
XS 17 backup
1 parent 5694d90 commit 75b157b

File tree

7 files changed

+287
-7
lines changed

7 files changed

+287
-7
lines changed

xs/sources/xsAll.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,7 @@ mxExport void fxArrayLengthSetter(txMachine* the);
14531453

14541454
mxExport void fx_Array(txMachine* the);
14551455
mxExport void fx_Array_from(txMachine* the);
1456+
mxExport void fx_Array_fromAsync(txMachine* the);
14561457
mxExport void fx_Array_isArray(txMachine* the);
14571458
mxExport void fx_Array_of(txMachine* the);
14581459
mxExport void fx_Array_prototype_at(txMachine* the);

xs/sources/xsArray.c

Lines changed: 280 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ static void fxMoveThisItem(txMachine* the, txNumber from, txNumber to);
5353
static void fxReduceThisItem(txMachine* the, txSlot* function, txIndex index);
5454
static txBoolean fxSetArrayLength(txMachine* the, txSlot* array, txIndex target);
5555
static void fx_Array_from_aux(txMachine* the, txSlot* function, txSlot* value, txIndex index);
56+
static void fx_Array_fromAsync_items_next(txMachine* the);
57+
static void fx_Array_fromAsync_aux(txMachine* the, txSlot* closure);
58+
static void fx_Array_fromAsync_onIterated(txMachine* the);
59+
static void fx_Array_fromAsync_onMapped(txMachine* the);
60+
static void fx_Array_fromAsync_onRejected(txMachine* the);
5661
static txIndex fx_Array_prototype_flatAux(txMachine* the, txSlot* source, txIndex length, txIndex start, txIndex depth, txSlot* function);
5762

5863
static txBoolean fxArrayDefineOwnProperty(txMachine* the, txSlot* instance, txID id, txIndex index, txSlot* slot, txFlag mask);
@@ -175,6 +180,9 @@ void fxBuildArray(txMachine* the)
175180
mxArrayConstructor = *the->stack;
176181
slot = fxLastProperty(the, slot);
177182
slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Array_from), 1, mxID(_from), XS_DONT_ENUM_FLAG);
183+
#if mxECMAScript2026
184+
slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Array_fromAsync), 1, mxID(_fromAsync), XS_DONT_ENUM_FLAG);
185+
#endif
178186
slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Array_isArray), 1, mxID(_isArray), XS_DONT_ENUM_FLAG);
179187
slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Array_of), 0, mxID(_of), XS_DONT_ENUM_FLAG);
180188
slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_species_get), C_NULL, mxID(_Symbol_species), XS_DONT_ENUM_FLAG);
@@ -404,10 +412,8 @@ txIndex fxCheckArrayLength(txMachine* the, txSlot* slot)
404412
txUnsigned length;
405413
txNumber check;
406414
mxPushSlot(slot);
407-
length = fxToUnsigned(the, the->stack);
408-
mxPop();
409-
mxPushSlot(slot);
410415
check = fxToNumber(the, the->stack);
416+
length = fxToUnsigned(the, the->stack);
411417
mxPop();
412418
if (length == check)
413419
return length;
@@ -1243,6 +1249,277 @@ void fx_Array_from_aux(txMachine* the, txSlot* function, txSlot* value, txIndex
12431249
mxPop();
12441250
}
12451251

1252+
void fx_Array_fromAsync(txMachine* the)
1253+
{
1254+
txSlot* stack = the->stack;
1255+
txSlot* iterator;
1256+
txSlot* resolveFunction;
1257+
txSlot* rejectFunction;
1258+
txSlot* promise;
1259+
txSlot* instance;
1260+
txSlot* property;
1261+
txSlot* home;
1262+
1263+
mxTemporary(iterator);
1264+
mxTemporary(resolveFunction);
1265+
mxTemporary(rejectFunction);
1266+
mxTemporary(promise);
1267+
mxPush(mxPromiseConstructor);
1268+
fxNewPromiseCapability(the, resolveFunction, rejectFunction);
1269+
mxPullSlot(promise);
1270+
{
1271+
mxTry(the) {
1272+
txSlot* function = (mxArgc > 1) ? fxArgToCallback(the, 1) : C_NULL;
1273+
txIndex length = 0;
1274+
1275+
if (mxArgc == 0)
1276+
mxTypeError("no items");
1277+
1278+
mxPushSlot(mxArgv(0));
1279+
mxDub();
1280+
mxGetID(mxID(_Symbol_asyncIterator));
1281+
if (mxIsUndefined(the->stack) || mxIsNull(the->stack)) {
1282+
mxPop();
1283+
mxDub();
1284+
mxGetID(mxID(_Symbol_iterator));
1285+
if (mxIsUndefined(the->stack) || mxIsNull(the->stack)) {
1286+
mxPop();
1287+
mxDub();
1288+
mxGetID(mxID(_length));
1289+
if (mxIsUndefined(the->stack))
1290+
length = 0;
1291+
else
1292+
length = fxCheckArrayLength(the, the->stack);
1293+
mxPop();
1294+
mxPush(mxIteratorPrototype);
1295+
property = fxLastProperty(the, fxNewIteratorInstance(the, mxArgv(0), mxID(_Array)));
1296+
property = fxNextIntegerProperty(the, property, length, XS_NO_ID, XS_INTERNAL_FLAG);
1297+
property = fxNextHostFunctionProperty(the, property, mxCallback(fx_Array_fromAsync_items_next), 0, mxID(_next), XS_DONT_ENUM_FLAG);
1298+
fxNewAsyncFromSyncIteratorInstance(the);
1299+
mxPullSlot(iterator);
1300+
fxCreateArray(the, 1, length);
1301+
}
1302+
else {
1303+
mxCall();
1304+
mxRunCount(0);
1305+
fxNewAsyncFromSyncIteratorInstance(the);
1306+
mxPullSlot(iterator);
1307+
fxCreateArray(the, 0, 0);
1308+
}
1309+
}
1310+
else {
1311+
mxCall();
1312+
mxRunCount(0);
1313+
mxPullSlot(iterator);
1314+
fxCreateArray(the, 0, 0);
1315+
}
1316+
1317+
property = instance = fxNewInstance(the);
1318+
1319+
property = fxNextSlotProperty(the, property, resolveFunction, XS_NO_ID, XS_INTERNAL_FLAG);
1320+
property = fxNextSlotProperty(the, property, rejectFunction, XS_NO_ID, XS_INTERNAL_FLAG);
1321+
1322+
function = fxNewHostFunction(the, fx_Array_fromAsync_onIterated, 1, XS_NO_ID, XS_NO_ID);
1323+
home = mxFunctionInstanceHome(function);
1324+
home->value.home.object = instance;
1325+
property = fxNextSlotProperty(the, property, the->stack, XS_NO_ID, XS_INTERNAL_FLAG);
1326+
mxPop();
1327+
1328+
function = fxNewHostFunction(the, fx_Array_fromAsync_onRejected, 1, XS_NO_ID, XS_NO_ID);
1329+
home = mxFunctionInstanceHome(function);
1330+
home->value.home.object = instance;
1331+
property = fxNextSlotProperty(the, property, the->stack, XS_NO_ID, XS_INTERNAL_FLAG);
1332+
mxPop();
1333+
1334+
property = fxNextSlotProperty(the, property, mxResult, XS_NO_ID, XS_INTERNAL_FLAG);
1335+
property = fxNextIntegerProperty(the, property, 0, XS_NO_ID, XS_INTERNAL_FLAG);
1336+
1337+
property = fxNextSlotProperty(the, property, iterator, XS_NO_ID, XS_INTERNAL_FLAG);
1338+
if (mxArgc > 1)
1339+
property = fxNextSlotProperty(the, property, mxArgv(1), XS_NO_ID, XS_INTERNAL_FLAG);
1340+
else
1341+
property = fxNextUndefinedProperty(the, property, XS_NO_ID, XS_INTERNAL_FLAG);
1342+
if (mxArgc > 2)
1343+
property = fxNextSlotProperty(the, property, mxArgv(2), XS_NO_ID, XS_INTERNAL_FLAG);
1344+
else
1345+
property = fxNextUndefinedProperty(the, property, XS_NO_ID, XS_INTERNAL_FLAG);
1346+
function = fxNewHostFunction(the, fx_Array_fromAsync_onMapped, 1, XS_NO_ID, XS_NO_ID);
1347+
home = mxFunctionInstanceHome(function);
1348+
home->value.home.object = instance;
1349+
property = fxNextSlotProperty(the, property, the->stack, XS_NO_ID, XS_INTERNAL_FLAG);
1350+
mxPop();
1351+
1352+
fx_Array_fromAsync_aux(the, instance);
1353+
}
1354+
mxCatch(the) {
1355+
fxRejectException(the, rejectFunction);
1356+
}
1357+
}
1358+
*mxResult = *promise;
1359+
the->stack = stack;
1360+
}
1361+
1362+
void fx_Array_fromAsync_aux(txMachine* the, txSlot* closure)
1363+
{
1364+
txSlot* resolveFunction = closure->next;
1365+
txSlot* rejectFunction = resolveFunction->next;
1366+
txSlot* onIteratedFunction = rejectFunction->next;
1367+
txSlot* onRejectedFunction = onIteratedFunction->next;
1368+
txSlot* result = onRejectedFunction->next;
1369+
txSlot* index = result->next;
1370+
txSlot* iterator = index->next;
1371+
1372+
mxPushUndefined();
1373+
mxPush(mxPromiseConstructor);
1374+
1375+
mxPushSlot(iterator);
1376+
mxDub();
1377+
mxGetID(mxID(_next));
1378+
mxCall();
1379+
mxRunCount(0);
1380+
1381+
fx_Promise_resolveAux(the);
1382+
mxPop();
1383+
mxPop();
1384+
fxPromiseThen(the, the->stack->value.reference, onIteratedFunction, onRejectedFunction, C_NULL, rejectFunction);
1385+
}
1386+
1387+
void fx_Array_fromAsync_items_next(txMachine* the)
1388+
{
1389+
txSlot* iterator = fxCheckIteratorInstance(the, mxThis, mxID(_Array));
1390+
txSlot* result = iterator->next;
1391+
txSlot* iterable = result->next;
1392+
txSlot* index = iterable->next;
1393+
txSlot* length = index->next;
1394+
txSlot* value = fxCheckIteratorResult(the, result);
1395+
txSlot* done = value->next;
1396+
if (!done->value.boolean) {
1397+
txIndex i = (txIndex)index->value.integer;
1398+
txIndex c = (txIndex)length->value.integer;
1399+
if (i < c) {
1400+
mxPushSlot(iterable);
1401+
mxGetIndex(i);
1402+
mxPullSlot(value);
1403+
index->value.integer = i + 1;
1404+
}
1405+
else {
1406+
value->kind = XS_UNDEFINED_KIND;
1407+
done->value.boolean = 1;
1408+
}
1409+
}
1410+
mxResult->kind = result->kind;
1411+
mxResult->value = result->value;
1412+
}
1413+
1414+
void fx_Array_fromAsync_onIterated(txMachine* the)
1415+
{
1416+
txSlot* slot = mxFunctionInstanceHome(mxFunction->value.reference);
1417+
txSlot* closure = slot->value.home.object;
1418+
txSlot* resolveFunction = closure->next;
1419+
txSlot* rejectFunction = resolveFunction->next;
1420+
txSlot* onIteratedFunction = rejectFunction->next;
1421+
txSlot* onRejectedFunction = onIteratedFunction->next;
1422+
txSlot* result = onRejectedFunction->next;
1423+
txSlot* index = result->next;
1424+
txSlot* iterator = index->next;
1425+
txSlot* mapFunction = iterator->next;
1426+
txSlot* mapThis = mapFunction->next;
1427+
txSlot* onMappedFunction = mapThis->next;
1428+
1429+
mxTry(the) {
1430+
mxPushSlot(mxArgv(0));
1431+
mxGetID(mxID(_done));
1432+
if (fxToBoolean(the, the->stack)) {
1433+
mxPushSlot(index);
1434+
mxPushSlot(result);
1435+
mxSetID(mxID(_length));
1436+
mxPop();
1437+
1438+
mxPushUndefined();
1439+
mxPushSlot(resolveFunction);
1440+
mxCall();
1441+
mxPushSlot(result);
1442+
mxRunCount(1);
1443+
mxPop();
1444+
return;
1445+
}
1446+
if (!mxIsUndefined(mapFunction)) {
1447+
mxPushUndefined();
1448+
mxPush(mxPromiseConstructor);
1449+
1450+
mxPushSlot(mapThis);
1451+
mxPushSlot(mapFunction);
1452+
mxCall();
1453+
mxPushSlot(mxArgv(0));
1454+
mxGetID(mxID(_value));
1455+
mxPushSlot(index);
1456+
mxRunCount(2);
1457+
1458+
fx_Promise_resolveAux(the);
1459+
mxPop();
1460+
mxPop();
1461+
fxPromiseThen(the, the->stack->value.reference, onMappedFunction, onRejectedFunction, C_NULL, rejectFunction);
1462+
}
1463+
else {
1464+
mxPushSlot(mxArgv(0));
1465+
mxGetID(mxID(_value));
1466+
mxPushSlot(result);
1467+
mxDefineIndex(index->value.integer, 0, XS_GET_ONLY);
1468+
mxPop();
1469+
index->value.integer++;
1470+
fx_Array_fromAsync_aux(the, closure);
1471+
}
1472+
}
1473+
mxCatch(the) {
1474+
fxIteratorReturn(the, iterator, 1);
1475+
fxThrow(the, NULL, 0);
1476+
}
1477+
}
1478+
1479+
void fx_Array_fromAsync_onMapped(txMachine* the)
1480+
{
1481+
txSlot* slot = mxFunctionInstanceHome(mxFunction->value.reference);
1482+
txSlot* closure = slot->value.home.object;
1483+
txSlot* resolveFunction = closure->next;
1484+
txSlot* rejectFunction = resolveFunction->next;
1485+
txSlot* onIteratedFunction = rejectFunction->next;
1486+
txSlot* onRejectedFunction = onIteratedFunction->next;
1487+
txSlot* result = onRejectedFunction->next;
1488+
txSlot* index = result->next;
1489+
txSlot* iterator = index->next;
1490+
1491+
mxTry(the) {
1492+
mxPushSlot(mxArgv(0));
1493+
mxPushSlot(result);
1494+
mxDefineIndex(index->value.integer, 0, XS_GET_ONLY);
1495+
mxPop();
1496+
index->value.integer++;
1497+
fx_Array_fromAsync_aux(the, closure);
1498+
}
1499+
mxCatch(the) {
1500+
fxIteratorReturn(the, iterator, 1);
1501+
fxThrow(the, NULL, 0);
1502+
}
1503+
}
1504+
1505+
void fx_Array_fromAsync_onRejected(txMachine* the)
1506+
{
1507+
txSlot* slot = mxFunctionInstanceHome(mxFunction->value.reference);
1508+
txSlot* closure = slot->value.home.object;
1509+
txSlot* resolveFunction = closure->next;
1510+
txSlot* rejectFunction = resolveFunction->next;
1511+
txSlot* onIteratedFunction = rejectFunction->next;
1512+
txSlot* onRejectedFunction = onIteratedFunction->next;
1513+
txSlot* result = onRejectedFunction->next;
1514+
txSlot* index = result->next;
1515+
txSlot* iterator = index->next;
1516+
1517+
fxIteratorReturn(the, iterator, 1);
1518+
mxException.kind = mxArgv(0)->kind;
1519+
mxException.value = mxArgv(0)->value;
1520+
fxThrow(the, NULL, 0);
1521+
}
1522+
12461523
void fx_Array_isArray(txMachine* the)
12471524
{
12481525
mxResult->kind = XS_BOOLEAN_KIND;

xs/sources/xsCommon.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,6 +1876,7 @@ const txString gxIDStrings[XS_ID_COUNT] = {
18761876
"isError",
18771877
#endif
18781878
#if mxECMAScript2026
1879+
"fromAsync",
18791880
"sumPrecise",
18801881
#endif
18811882
};

xs/sources/xsCommon.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,7 @@ enum {
13161316
_isError,
13171317
#endif
13181318
#if mxECMAScript2026
1319+
_fromAsync,
13191320
_sumPrecise,
13201321
#endif
13211322
XS_ID_COUNT

xs/sources/xsGenerator.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ txBoolean fxGetIteratorFlattenable(txMachine* the, txSlot* iterable, txSlot* ite
329329
if (!mxIsReference(iterable)) {
330330
if (optional) {
331331
if ((iterable->kind != XS_STRING_KIND) && (iterable->kind != XS_STRING_X_KIND))
332-
mxTypeError("iterator: not an string");
332+
mxTypeError("iterator: not a string");
333333
}
334334
else
335335
mxTypeError("iterator: not an object");

xs/sources/xsSnapshot.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ static void fxWriteStack(txMachine* the, txSnapshot* snapshot);
121121
#define mxNativeAdditions 0
122122
#endif
123123
#if mxECMAScript2026
124-
#define mxECMAScript2026Additions 2
124+
#define mxECMAScript2026Additions 3
125125
#else
126126
#define mxECMAScript2026Additions 0
127127
#endif
@@ -729,6 +729,7 @@ static txCallback gxCallbacks[mxCallbacksLength] = {
729729
#endif
730730
fxAsyncFromSyncIteratorFailed,
731731
#if mxECMAScript2026
732+
fx_Array_fromAsync,
732733
fx_Iterator_concat,
733734
fx_Math_sumPrecise,
734735
#endif

xs/tools/xst262.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,8 @@ static void fxLoadHook(txMachine* the);
146146
static void fxRunProgramFileInCompartment(txMachine* the, txString path, txUnsigned flags);
147147
static void fxRunModuleFileInCompartment(txMachine* the, txString path);
148148

149-
#define mxFeaturesCount 15
149+
#define mxFeaturesCount 14
150150
static char* gxFeatures[mxFeaturesCount] = {
151-
"Array.fromAsync",
152151
"Atomics.pause",
153152
"FinalizationRegistry.prototype.cleanupSome",
154153
"ShadowRealm",

0 commit comments

Comments
 (0)