Space Lua: Refactor control flow signals (#1794)
* Space Lua: Refactor control flow signals Removes "return throws an exception" failure mode. @zef :) Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz> * Remove unused function Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz> * Remove another unused function Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz> * Remove unused `LuaBreak`/`LuaReturn` exception control flow Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz> --------- Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz>
This commit is contained in:
+19
-12
@@ -445,11 +445,28 @@ export class LuaFunction implements ILuaFunction {
|
||||
|
||||
// Evaluate the function body with returnOnReturn set to true
|
||||
const r = evalStatement(this.body.block, env, sfWithFn, true);
|
||||
|
||||
const map = (val: any) => {
|
||||
if (val !== undefined) {
|
||||
return mapFunctionReturnValue(val);
|
||||
if (val === undefined) {
|
||||
return;
|
||||
}
|
||||
if (val && typeof val === "object" && val.ctrl === "return") {
|
||||
return mapFunctionReturnValue(val.values);
|
||||
}
|
||||
if (val && typeof val === "object" && val.ctrl === "break") {
|
||||
throw new LuaRuntimeError(
|
||||
"break outside loop",
|
||||
sfWithFn.withCtx(this.body.block.ctx),
|
||||
);
|
||||
}
|
||||
if (val && typeof val === "object" && val.ctrl === "goto") {
|
||||
throw new LuaRuntimeError(
|
||||
"unexpected goto signal",
|
||||
sfWithFn.withCtx(this.body.block.ctx),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
if (isPromise(r)) {
|
||||
return r.then(map);
|
||||
} else {
|
||||
@@ -1042,16 +1059,6 @@ export function luaTypeOf(val: any): LuaType | Promise<LuaType> {
|
||||
}
|
||||
}
|
||||
|
||||
// Both `break` and `return` are implemented by exception throwing
|
||||
export class LuaBreak extends Error {
|
||||
}
|
||||
|
||||
export class LuaReturn extends Error {
|
||||
constructor(readonly values: LuaValue[]) {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
||||
export class LuaRuntimeError extends Error {
|
||||
constructor(
|
||||
override readonly message: string,
|
||||
|
||||
Reference in New Issue
Block a user