diff --git a/Makefile b/Makefile index 48df38ca..34dc0cbe 100644 --- a/Makefile +++ b/Makefile @@ -30,6 +30,11 @@ test: # Run backend tests go test ./server/... +.PHONY: bench +bench: + # Run frontend tests + deno task bench + generate: # Regenerate the Lua parser from the the grammar deno run -A npm:@lezer/generator@1.5.1 client/space_lua/lua.grammar -o client/space_lua/parse-lua.js diff --git a/bench/lua.bench.ts b/bench/lua.bench.ts new file mode 100644 index 00000000..0820f56b --- /dev/null +++ b/bench/lua.bench.ts @@ -0,0 +1,91 @@ +import { parse } from "../client/space_lua/parse.ts"; +import { luaBuildStandardEnv } from "../client/space_lua/stdlib.ts"; +import { + LuaEnv, + LuaRuntimeError, + LuaStackFrame, +} from "../client/space_lua/runtime.ts"; +import { evalStatement } from "../client/space_lua/eval.ts"; +import { assert } from "@std/assert/assert"; +import { fileURLToPath } from "node:url"; + +Deno.bench("[Lua] Core language", async () => { + await runLuaTest("../client/space_lua/language_core_test.lua"); +}); + +Deno.bench("[Lua] Core language (length)", async () => { + await runLuaTest("../client/space_lua/len_test.lua"); +}); + +Deno.bench("[Lua] Core language (truthiness)", async () => { + await runLuaTest("../client/space_lua/truthiness_test.lua"); +}); + +Deno.bench("[Lua] Core language (arithmetic)", async () => { + await runLuaTest("../client/space_lua/arithmetic_test.lua"); +}); + +Deno.bench("[Lua] Load tests", async () => { + await runLuaTest("../client/space_lua/stdlib/load_test.lua"); +}); + +Deno.bench("[Lua] Table tests", async () => { + await runLuaTest("../client/space_lua/stdlib/table_test.lua"); +}); + +Deno.bench("[Lua] String to number tests", async () => { + await runLuaTest("../client/space_lua/tonumber_test.lua"); +}); + +Deno.bench("[Lua] String tests", async () => { + await runLuaTest("../client/space_lua/stdlib/string_test.lua"); + // await runLuaTest("../client/space_lua/stdlib/string_test2.lua"); +}); + +Deno.bench("[Lua] Space Lua tests", async () => { + await runLuaTest("../client/space_lua/stdlib/space_lua_test.lua"); +}); + +Deno.bench("[Lua] OS tests", async () => { + await runLuaTest("../client/space_lua/stdlib/os_test.lua"); +}); + +Deno.bench("[Lua] Math tests", async () => { + await runLuaTest("../client/space_lua/stdlib/math_test.lua"); +}); + +Deno.bench("[Lua] JS tests", async () => { + await runLuaTest("../client/space_lua/stdlib/js_test.lua"); +}); + +Deno.bench("[Lua] Global functions tests", async () => { + await runLuaTest("../client/space_lua/stdlib/global_test.lua"); +}); + +Deno.bench("[Lua] Encoding functions tests", async () => { + await runLuaTest("../client/space_lua/stdlib/encoding_test.lua"); +}); + +Deno.bench("[Lua] Lume functions tests", async () => { + await runLuaTest("../client/space_lua/lume_test.lua"); +}); + +async function runLuaTest(luaPath: string) { + const luaFile = await Deno.readTextFile( + fileURLToPath(new URL(luaPath, import.meta.url)), + ); + const chunk = parse(luaFile, {}); + const env = new LuaEnv(luaBuildStandardEnv()); + const sf = LuaStackFrame.createWithGlobalEnv(env, chunk.ctx); + + try { + await evalStatement(chunk, env, sf); + } catch (e: any) { + if (e instanceof LuaRuntimeError) { + console.error(`Error evaluating script:`, e.toPrettyString(luaFile)); + } else { + console.error(`Error evaluating script:`, e); + } + assert(false); + } +} diff --git a/client/space_lua/language_core_test.lua b/client/space_lua/language_core_test.lua index 0c1983f2..e8c42fcb 100644 --- a/client/space_lua/language_core_test.lua +++ b/client/space_lua/language_core_test.lua @@ -215,21 +215,22 @@ assert(t.bar == "bar") assertEqual(t.foo, "Key not found: foo") -- Test the __newindex metamethod -t = setmetatable( +t, c = setmetatable( {}, { __newindex = function(table, key, value) - print("Raw set", key, value) + c = c + 1 rawset(table, key, "Value: " .. value) - print("Raw set done") + c = c + 1 end } -) +), 0 t.name = "John" -- rawset ignores the metamethod rawset(t, "age", 100) assertEqual(t.name, "Value: John") assertEqual(t.age, 100) +assertEqual(c, 2) -- Test some of the operator metamethods t = setmetatable( diff --git a/deno.json b/deno.json index ead1b04b..45c43366 100644 --- a/deno.json +++ b/deno.json @@ -10,6 +10,7 @@ "lint": "deno lint --fix", "fmt": "deno fmt", "test": "deno test -A", + "bench": "deno bench -A", "plugs": "deno run -A build_plugs_libraries.ts" }, "publish": {