JS iterable handling
parent
ce18078480
commit
ccea2200ac
|
@ -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(
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in New Issue