JS iterable handling
parent
ce18078480
commit
ccea2200ac
|
@ -730,9 +730,11 @@ export async function evalStatement(
|
||||||
),
|
),
|
||||||
).flatten();
|
).flatten();
|
||||||
let iteratorValue: ILuaFunction | any = iteratorMultiRes.values[0];
|
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) {
|
if (Array.isArray(iteratorValue) || iteratorValue instanceof LuaTable) {
|
||||||
iteratorValue = env.get("each").call(sf, iteratorValue);
|
iteratorValue = env.get("each").call(sf, iteratorValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!iteratorValue?.call) {
|
if (!iteratorValue?.call) {
|
||||||
console.error("Cannot iterate over", iteratorMultiRes.values[0]);
|
console.error("Cannot iterate over", iteratorMultiRes.values[0]);
|
||||||
throw new LuaRuntimeError(
|
throw new LuaRuntimeError(
|
||||||
|
|
|
@ -14,7 +14,6 @@ export const jsApi = new LuaTable({
|
||||||
*/
|
*/
|
||||||
new: new LuaBuiltinFunction(
|
new: new LuaBuiltinFunction(
|
||||||
(_sf, constructorFn: any, ...args) => {
|
(_sf, constructorFn: any, ...args) => {
|
||||||
console.log("New", constructorFn, args);
|
|
||||||
return new constructorFn(
|
return new constructorFn(
|
||||||
...args.map(luaValueToJS),
|
...args.map(luaValueToJS),
|
||||||
);
|
);
|
||||||
|
@ -33,6 +32,16 @@ export const jsApi = new LuaTable({
|
||||||
}
|
}
|
||||||
return m;
|
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.
|
* Converts a JavaScript value to a Lua value.
|
||||||
* @param val - The JavaScript value to convert.
|
* @param val - The JavaScript value to convert.
|
||||||
|
|
Loading…
Reference in New Issue