From 68f441e383e41b8191852fa556e6c5585f654fe7 Mon Sep 17 00:00:00 2001 From: Lorenz Junglas Date: Fri, 12 Feb 2021 15:10:18 +0100 Subject: [PATCH] Precompile lualib and jsonlib for tests --- test/util.ts | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/test/util.ts b/test/util.ts index 5a59793be..67cfcef57 100644 --- a/test/util.ts +++ b/test/util.ts @@ -10,8 +10,32 @@ import * as vm from "vm"; import * as tstl from "../src"; import { createEmitOutputCollector } from "../src/transpilation/output-collector"; -const minimalTestLib = fs.readFileSync(path.join(__dirname, "json.lua"), "utf8"); -const lualibContent = fs.readFileSync(path.resolve(__dirname, "../dist/lualib/lualib_bundle.lua"), "utf8"); +export function toByteCode(luaCode: string) { + const L = lauxlib.luaL_newstate(); + + if (lauxlib.luaL_loadstring(L, to_luastring(luaCode)) !== lua.LUA_OK) throw Error(lua.lua_tojsstring(L, -1)); + + const writer = (_: any, newBytes: Uint8Array, size: number, data: number[]) => { + data.push(...newBytes.slice(0, size)); + return 0; + }; + + const data: number[] = []; + + const dumpExitCode = lua.lua_dump(L, writer, data, false); + + if (dumpExitCode !== 0) { + throw Error("Unable to dump byte code"); + } + + return Uint8Array.from(data); +} + +const jsonLib = fs.readFileSync(path.join(__dirname, "json.lua"), "utf8"); +const jsonLibByteCode = toByteCode(jsonLib); + +const luaLib = fs.readFileSync(path.resolve(__dirname, "../dist/lualib/lualib_bundle.lua"), "utf8"); +const luaLibByteCode = toByteCode(luaLib); // Using `test` directly makes eslint-plugin-jest consider this file as a test const defineTest = test; @@ -345,7 +369,7 @@ export abstract class TestBuilder { // Json lua.lua_getglobal(L, "package"); lua.lua_getfield(L, -1, "preload"); - lauxlib.luaL_loadstring(L, to_luastring(minimalTestLib)); + lauxlib.luaL_loadstring(L, jsonLibByteCode); lua.lua_setfield(L, -2, "json"); // Lua lib if ( @@ -354,7 +378,7 @@ export abstract class TestBuilder { ) { lua.lua_getglobal(L, "package"); lua.lua_getfield(L, -1, "preload"); - lauxlib.luaL_loadstring(L, to_luastring(lualibContent)); + lauxlib.luaL_loadstring(L, luaLibByteCode); lua.lua_setfield(L, -2, "lualib_bundle"); }