JS iterable handling

pull/1212/head
Zef Hemel 2025-01-16 12:35:15 +01:00
parent ce18078480
commit ccea2200ac
2 changed files with 12 additions and 1 deletions

View File

@ -730,9 +730,11 @@ export async function evalStatement(
),
).flatten();
let iteratorValue: ILuaFunction | any = iteratorMultiRes.values[0];
// Handle the case where the iterator is a table and we need to call the each function
if (Array.isArray(iteratorValue) || iteratorValue instanceof LuaTable) {
iteratorValue = env.get("each").call(sf, iteratorValue);
}
if (!iteratorValue?.call) {
console.error("Cannot iterate over", iteratorMultiRes.values[0]);
throw new LuaRuntimeError(

View File

@ -14,7 +14,6 @@ export const jsApi = new LuaTable({
*/
new: new LuaBuiltinFunction(
(_sf, constructorFn: any, ...args) => {
console.log("New", constructorFn, args);
return new constructorFn(
...args.map(luaValueToJS),
);
@ -33,6 +32,16 @@ export const jsApi = new LuaTable({
}
return m;
}),
each_iterable: new LuaBuiltinFunction((_sf, val) => {
let iterator = val[Symbol.asyncIterator]();
return async () => {
const result = await iterator.next();
if (result.done) {
return;
}
return result.value;
};
}),
/**
* Converts a JavaScript value to a Lua value.
* @param val - The JavaScript value to convert.