Lua stack frame refactor
This commit is contained in:
+23
-18
@@ -1,8 +1,10 @@
|
||||
import {
|
||||
type ILuaFunction,
|
||||
LuaBuiltinFunction,
|
||||
luaCall,
|
||||
LuaEnv,
|
||||
LuaMultiRes,
|
||||
LuaRuntimeError,
|
||||
type LuaTable,
|
||||
luaToString,
|
||||
luaTypeOf,
|
||||
@@ -13,19 +15,19 @@ import { tableApi } from "$common/space_lua/stdlib/table.ts";
|
||||
import { osApi } from "$common/space_lua/stdlib/os.ts";
|
||||
import { jsApi } from "$common/space_lua/stdlib/js.ts";
|
||||
|
||||
const printFunction = new LuaBuiltinFunction((...args) => {
|
||||
const printFunction = new LuaBuiltinFunction((_sf, ...args) => {
|
||||
console.log("[Lua]", ...args.map(luaToString));
|
||||
});
|
||||
|
||||
const assertFunction = new LuaBuiltinFunction(
|
||||
async (value: any, message?: string) => {
|
||||
async (sf, value: any, message?: string) => {
|
||||
if (!await value) {
|
||||
throw new Error(`Assertion failed: ${message}`);
|
||||
throw new LuaRuntimeError(`Assertion failed: ${message}`, sf);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
const ipairsFunction = new LuaBuiltinFunction((ar: LuaTable) => {
|
||||
const ipairsFunction = new LuaBuiltinFunction((_sf, ar: LuaTable) => {
|
||||
let i = 1;
|
||||
return () => {
|
||||
if (i > ar.length) {
|
||||
@@ -37,7 +39,7 @@ const ipairsFunction = new LuaBuiltinFunction((ar: LuaTable) => {
|
||||
};
|
||||
});
|
||||
|
||||
const pairsFunction = new LuaBuiltinFunction((t: LuaTable) => {
|
||||
const pairsFunction = new LuaBuiltinFunction((_sf, t: LuaTable) => {
|
||||
const keys = t.keys();
|
||||
let i = 0;
|
||||
return () => {
|
||||
@@ -50,7 +52,7 @@ const pairsFunction = new LuaBuiltinFunction((t: LuaTable) => {
|
||||
};
|
||||
});
|
||||
|
||||
const unpackFunction = new LuaBuiltinFunction((t: LuaTable) => {
|
||||
const unpackFunction = new LuaBuiltinFunction((_sf, t: LuaTable) => {
|
||||
const values: LuaValue[] = [];
|
||||
for (let i = 1; i <= t.length; i++) {
|
||||
values.push(t.get(i));
|
||||
@@ -58,26 +60,26 @@ const unpackFunction = new LuaBuiltinFunction((t: LuaTable) => {
|
||||
return new LuaMultiRes(values);
|
||||
});
|
||||
|
||||
const typeFunction = new LuaBuiltinFunction((value: LuaValue): string => {
|
||||
const typeFunction = new LuaBuiltinFunction((_sf, value: LuaValue): string => {
|
||||
return luaTypeOf(value);
|
||||
});
|
||||
|
||||
const tostringFunction = new LuaBuiltinFunction((value: any) => {
|
||||
const tostringFunction = new LuaBuiltinFunction((_sf, value: any) => {
|
||||
return luaToString(value);
|
||||
});
|
||||
|
||||
const tonumberFunction = new LuaBuiltinFunction((value: LuaValue) => {
|
||||
const tonumberFunction = new LuaBuiltinFunction((_sf, value: LuaValue) => {
|
||||
return Number(value);
|
||||
});
|
||||
|
||||
const errorFunction = new LuaBuiltinFunction((message: string) => {
|
||||
const errorFunction = new LuaBuiltinFunction((_sf, message: string) => {
|
||||
throw new Error(message);
|
||||
});
|
||||
|
||||
const pcallFunction = new LuaBuiltinFunction(
|
||||
async (fn: ILuaFunction, ...args) => {
|
||||
async (sf, fn: ILuaFunction, ...args) => {
|
||||
try {
|
||||
return new LuaMultiRes([true, await fn.call(...args)]);
|
||||
return new LuaMultiRes([true, await luaCall(fn, args, sf.astCtx!, sf)]);
|
||||
} catch (e: any) {
|
||||
return new LuaMultiRes([false, e.message]);
|
||||
}
|
||||
@@ -85,30 +87,33 @@ const pcallFunction = new LuaBuiltinFunction(
|
||||
);
|
||||
|
||||
const xpcallFunction = new LuaBuiltinFunction(
|
||||
async (fn: ILuaFunction, errorHandler: ILuaFunction, ...args) => {
|
||||
async (sf, fn: ILuaFunction, errorHandler: ILuaFunction, ...args) => {
|
||||
try {
|
||||
return new LuaMultiRes([true, await fn.call(...args)]);
|
||||
return new LuaMultiRes([true, await fn.call(sf, ...args)]);
|
||||
} catch (e: any) {
|
||||
return new LuaMultiRes([false, await errorHandler.call(e.message)]);
|
||||
return new LuaMultiRes([
|
||||
false,
|
||||
await luaCall(errorHandler, [e.message], sf.astCtx!, sf),
|
||||
]);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
const setmetatableFunction = new LuaBuiltinFunction(
|
||||
(table: LuaTable, metatable: LuaTable) => {
|
||||
(_sf, table: LuaTable, metatable: LuaTable) => {
|
||||
table.metatable = metatable;
|
||||
return table;
|
||||
},
|
||||
);
|
||||
|
||||
const rawsetFunction = new LuaBuiltinFunction(
|
||||
(table: LuaTable, key: LuaValue, value: LuaValue) => {
|
||||
(_sf, table: LuaTable, key: LuaValue, value: LuaValue) => {
|
||||
table.rawSet(key, value);
|
||||
return table;
|
||||
},
|
||||
);
|
||||
|
||||
const getmetatableFunction = new LuaBuiltinFunction((table: LuaTable) => {
|
||||
const getmetatableFunction = new LuaBuiltinFunction((_sf, table: LuaTable) => {
|
||||
return table.metatable;
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user