2024-10-04 23:15:50 +08:00
|
|
|
import { parse } from "$common/space_lua/parse.ts";
|
|
|
|
import { luaBuildStandardEnv } from "$common/space_lua/stdlib.ts";
|
2025-01-12 23:54:04 +08:00
|
|
|
import {
|
|
|
|
LuaEnv,
|
|
|
|
LuaRuntimeError,
|
|
|
|
LuaStackFrame,
|
|
|
|
} from "$common/space_lua/runtime.ts";
|
2024-10-04 23:15:50 +08:00
|
|
|
import { evalStatement } from "$common/space_lua/eval.ts";
|
|
|
|
import { assert } from "@std/assert/assert";
|
2024-11-14 04:08:24 +08:00
|
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
|
2025-01-16 03:47:58 +08:00
|
|
|
Deno.test("[Lua] Core language", async () => {
|
|
|
|
await runLuaTest("./language_core_test.lua");
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("[Lua] Table tests", async () => {
|
|
|
|
await runLuaTest("./stdlib/table_test.lua");
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("[Lua] String tests", async () => {
|
|
|
|
await runLuaTest("./stdlib/string_test.lua");
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("[Lua] Space Lua tests", async () => {
|
|
|
|
await runLuaTest("./stdlib/space_lua_test.lua");
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("[Lua] OS tests", async () => {
|
|
|
|
await runLuaTest("./stdlib/os_test.lua");
|
|
|
|
});
|
|
|
|
|
2025-01-17 17:40:47 +08:00
|
|
|
Deno.test("[Lua] Math tests", async () => {
|
|
|
|
await runLuaTest("./stdlib/math_test.lua");
|
|
|
|
});
|
|
|
|
|
2025-01-16 03:47:58 +08:00
|
|
|
Deno.test("[Lua] JS tests", async () => {
|
|
|
|
await runLuaTest("./stdlib/js_test.lua");
|
2025-01-15 03:26:47 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
async function runLuaTest(luaPath: string) {
|
2024-10-11 21:34:27 +08:00
|
|
|
const luaFile = await Deno.readTextFile(
|
2025-01-15 03:26:47 +08:00
|
|
|
fileURLToPath(new URL(luaPath, import.meta.url)),
|
2024-10-11 21:34:27 +08:00
|
|
|
);
|
|
|
|
const chunk = parse(luaFile, {});
|
|
|
|
const env = new LuaEnv(luaBuildStandardEnv());
|
2024-10-20 21:06:23 +08:00
|
|
|
const sf = new LuaStackFrame(new LuaEnv(), chunk.ctx);
|
2025-01-10 01:22:12 +08:00
|
|
|
sf.threadLocal.setLocal("_GLOBAL", env);
|
2024-10-04 23:15:50 +08:00
|
|
|
|
2024-10-11 21:34:27 +08:00
|
|
|
try {
|
2024-10-20 21:06:23 +08:00
|
|
|
await evalStatement(chunk, env, sf);
|
2024-10-11 21:34:27 +08:00
|
|
|
} catch (e: any) {
|
2025-01-12 23:54:04 +08:00
|
|
|
if (e instanceof LuaRuntimeError) {
|
|
|
|
console.error(`Error evaluating script:`, e.toPrettyString(luaFile));
|
|
|
|
} else {
|
|
|
|
console.error(`Error evaluating script:`, e);
|
|
|
|
}
|
2024-10-11 21:34:27 +08:00
|
|
|
assert(false);
|
|
|
|
}
|
2025-01-15 03:26:47 +08:00
|
|
|
}
|