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 713083baa5
commit 57ad75aa98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 10 deletions

View File

@ -128,6 +128,19 @@ static int lua_setTickRate(lua_State* l) {
return 0; 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) { static LuaHandle setupLuaState(lua_Alloc alloc) {
LuaHandle ls = lua_newstate(alloc, NULL); LuaHandle ls = lua_newstate(alloc, NULL);
@ -137,9 +150,8 @@ static LuaHandle setupLuaState(lua_Alloc alloc) {
return nullptr; return nullptr;
} }
// load libraries // Load Lua's own libraries
luaopen_base(ls); loadLibraries(ls);
luaopen_math(ls);
// Load rusEFI hooks // Load rusEFI hooks
lua_register(ls, "setTickRate", lua_setTickRate); lua_register(ls, "setTickRate", lua_setTickRate);

View File

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