encapsulate in smart pointer (#2603)

Co-authored-by: Matthew Kennedy <makenne@microsoft.com>
This commit is contained in:
Matthew Kennedy 2021-04-29 04:44:32 -07:00 committed by GitHub
parent 99fe310a35
commit 5e66e8cb21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 35 additions and 32 deletions

View File

@ -47,10 +47,34 @@ static void* myAlloc(void* /*ud*/, void* ptr, size_t /*osize*/, size_t nsize) {
} }
#endif // EFI_PROD_CODE #endif // EFI_PROD_CODE
static lua_State* setupLuaState() { class LuaHandle {
auto *ls = lua_newstate(myAlloc, NULL); public:
LuaHandle(lua_State* ptr) : m_ptr(ptr) { }
// Don't allow copying!
LuaHandle(const LuaHandle&) = delete;
LuaHandle& operator=(const LuaHandle&) = delete;
// Allow moving!
LuaHandle(LuaHandle&& rhs) {
m_ptr = rhs.m_ptr;
rhs.m_ptr = nullptr;
}
// Destruction cleans up lua state
~LuaHandle() {
if (m_ptr) lua_close(m_ptr);
}
operator lua_State*() const { return m_ptr; }
private:
lua_State* m_ptr;
};
static LuaHandle setupLuaState() {
LuaHandle ls = lua_newstate(myAlloc, NULL);
// TODO handle null ls
if (!ls) { if (!ls) {
firmwareError(OBD_PCM_Processor_Fault, "Failed to start Lua interpreter"); firmwareError(OBD_PCM_Processor_Fault, "Failed to start Lua interpreter");
@ -74,11 +98,7 @@ static lua_State* setupLuaState() {
return ls; return ls;
} }
void stopLua(lua_State* ls) { static bool loadScript(LuaHandle& ls, const char* scriptStr) {
lua_close(ls);
}
static bool loadScript(lua_State* ls, const char* scriptStr) {
efiPrintf("loading script length: %d", efiStrlen(scriptStr)); efiPrintf("loading script length: %d", efiStrlen(scriptStr));
if (0 != luaL_dostring(ls, scriptStr)) { if (0 != luaL_dostring(ls, scriptStr)) {
@ -113,7 +133,9 @@ void LuaThread::ThreadTask() {
//auto scriptStr = "function onTick()\nlocal rpm = getSensor(3)\nif rpm ~= nil then\nprint('RPM: ' ..rpm)\nend\nend\n"; //auto scriptStr = "function onTick()\nlocal rpm = getSensor(3)\nif rpm ~= nil then\nprint('RPM: ' ..rpm)\nend\nend\n";
auto scriptStr = "n=0\nfunction onTick()\nprint('hello lua ' ..n)\nn=n+1\nend\n"; auto scriptStr = "n=0\nfunction onTick()\nprint('hello lua ' ..n)\nn=n+1\nend\n";
loadScript(ls, scriptStr); if (!loadScript(ls, scriptStr)) {
return;
}
while (!chThdShouldTerminateX()) { while (!chThdShouldTerminateX()) {
// run the tick function // run the tick function
@ -157,7 +179,7 @@ void startLua() {
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
static lua_State* runScript(const char* script) { static LuaHandle runScript(const char* script) {
auto ls = setupLuaState(); auto ls = setupLuaState();
if (!ls) { if (!ls) {
@ -165,13 +187,11 @@ static lua_State* runScript(const char* script) {
} }
if (!loadScript(ls, script)) { if (!loadScript(ls, script)) {
lua_close(ls);
throw new std::logic_error("Call to loadScript failed"); throw new std::logic_error("Call to loadScript failed");
} }
lua_getglobal(ls, "testFunc"); lua_getglobal(ls, "testFunc");
if (lua_isnil(ls, -1)) { if (lua_isnil(ls, -1)) {
lua_close(ls);
throw new std::logic_error("Failed to find function testFunc"); throw new std::logic_error("Failed to find function testFunc");
} }
@ -179,7 +199,6 @@ static lua_State* runScript(const char* script) {
if (0 != status) { if (0 != status) {
std::string msg = std::string("lua error while running script: ") + lua_tostring(ls, -1); std::string msg = std::string("lua error while running script: ") + lua_tostring(ls, -1);
lua_close(ls);
throw new std::logic_error(msg); throw new std::logic_error(msg);
} }
@ -191,22 +210,16 @@ expected<float> testLuaReturnsNumberOrNil(const char* script) {
// check nil return first // check nil return first
if (lua_isnil(ls, -1)) { if (lua_isnil(ls, -1)) {
lua_close(ls);
return unexpected; return unexpected;
} }
// If not nil, it should be a number // If not nil, it should be a number
if (!lua_isnumber(ls, -1)) { if (!lua_isnumber(ls, -1)) {
lua_close(ls);
throw new std::logic_error("Returned value is not a number"); throw new std::logic_error("Returned value is not a number");
} }
// pop the return value // pop the return value
float retVal = lua_tonumber(ls, -1); return lua_tonumber(ls, -1);
lua_close(ls);
return retVal;
} }
float testLuaReturnsNumber(const char* script) { float testLuaReturnsNumber(const char* script) {
@ -214,16 +227,11 @@ float testLuaReturnsNumber(const char* script) {
// check the return value // check the return value
if (!lua_isnumber(ls, -1)) { if (!lua_isnumber(ls, -1)) {
lua_close(ls);
throw new std::logic_error("Returned value is not a number"); throw new std::logic_error("Returned value is not a number");
} }
// pop the return value // pop the return value
float retVal = lua_tonumber(ls, -1); return lua_tonumber(ls, -1);
lua_close(ls);
return retVal;
} }
int testLuaReturnsInteger(const char* script) { int testLuaReturnsInteger(const char* script) {
@ -231,15 +239,10 @@ int testLuaReturnsInteger(const char* script) {
// pop the return value; // pop the return value;
if (!lua_isinteger(ls, -1)) { if (!lua_isinteger(ls, -1)) {
lua_close(ls);
throw new std::logic_error("Returned value is not an integer"); throw new std::logic_error("Returned value is not an integer");
} }
int retVal = lua_tointeger(ls, -1); return lua_tointeger(ls, -1);
lua_close(ls);
return retVal;
} }
#endif // EFI_UNIT_TEST #endif // EFI_UNIT_TEST