load lua libs correctly (#3529)

* fix lua lib loading

* test
This commit is contained in:
Matthew Kennedy 2021-11-11 10:44:13 -08:00 committed by GitHub
parent 93d31c6c9b
commit baf11a27b9
2 changed files with 20 additions and 10 deletions

View File

@ -128,6 +128,19 @@ static int lua_setTickRate(lua_State* l) {
return 0;
}
static void loadLibraries(LuaHandle& ls) {
constexpr luaL_Reg libs[] = {
// TODO: do we even need the base lib?
//{ LUA_GNAME, luaopen_base },
{ LUA_MATHLIBNAME, luaopen_math },
};
for (size_t i = 0; i < efi::size(libs); i++) {
luaL_requiref(ls, libs[i].name, libs[i].func, 1);
lua_pop(ls, 1);
}
}
static LuaHandle setupLuaState(lua_Alloc alloc) {
LuaHandle ls = lua_newstate(alloc, NULL);
@ -137,9 +150,8 @@ static LuaHandle setupLuaState(lua_Alloc alloc) {
return nullptr;
}
// load libraries
luaopen_base(ls);
luaopen_math(ls);
// Load Lua's own libraries
loadLibraries(ls);
// Load rusEFI hooks
lua_register(ls, "setTickRate", lua_setTickRate);

View File

@ -13,18 +13,16 @@ TEST(LuaBasic, ReturnsNumber) {
EXPECT_FLOAT_EQ(result, 5.5f);
}
TEST(LuaBasic, MathMin) {
/*
TEST(LuaBasic, MathLib) {
auto script = R"(
function testFunc()
return min(1, 2)
end
)";
function testFunc()
return math.min(3, 1, 2)
end
)";
float result = testLuaReturnsNumber(script);
EXPECT_FLOAT_EQ(result, 1.0f);
*/
}
TEST(LuaBasic, ReturnsInteger) {