Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this the same as just data.push(...newBytes);?

return 0;
};

const data: number[] = [];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe move this up since you already used it in the definition of writer


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;
Expand Down Expand Up @@ -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 (
Expand All @@ -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");
}

Expand Down