system lua: load a script! (#3231)

* load script

* test

* needs more rams

* embiggen stack

* init system lua from lua thread

* de-embiggen stack

* system needs a little more

* memory

* om nom nom delicious memory

* how much more memory could it want

Co-authored-by: Matthew Kennedy <makenne@microsoft.com>
This commit is contained in:
Matthew Kennedy 2021-09-23 18:45:14 -07:00 committed by GitHub
parent fa4866e970
commit f41a2715a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 56 additions and 27 deletions

View File

@ -772,7 +772,7 @@ void initEngineContoller(DECLARE_ENGINE_PARAMETER_SUFFIX) {
* UNUSED_SIZE constants.
*/
#ifndef RAM_UNUSED_SIZE
#define RAM_UNUSED_SIZE 8000
#define RAM_UNUSED_SIZE 3500
#endif
#ifndef CCM_UNUSED_SIZE
#define CCM_UNUSED_SIZE 600

View File

@ -16,7 +16,7 @@
#define LUA_USER_HEAP 12000
#endif
#ifndef CCM_UNUSED_SIZE
#define LUA_SYSTEM_HEAP 10000
#define LUA_SYSTEM_HEAP 15000
#endif
static char luaUserHeap[LUA_USER_HEAP];
@ -204,6 +204,33 @@ static bool loadScript(LuaHandle& ls, const char* scriptStr) {
return true;
}
static LuaHandle systemLua;
const char* getSystemLuaScript();
void initSystemLua() {
efiAssertVoid(OBD_PCM_Processor_Fault, !systemLua, "system lua already init");
Timer startTimer;
startTimer.reset();
systemLua = setupLuaState(myAlloc<1>);
efiAssertVoid(OBD_PCM_Processor_Fault, systemLua, "system lua init fail");
if (!loadScript(systemLua, getSystemLuaScript())) {
firmwareError(OBD_PCM_Processor_Fault, "system lua script load fail");
systemLua = nullptr;
return;
}
auto startTime = startTimer.getElapsedSeconds();
#if !EFI_UNIT_TEST
efiPrintf("System Lua loaded in %.2f ms using %d bytes", startTime * 1'000, heaps[1].used());
#endif
}
#if !EFI_UNIT_TEST
static bool interactivePending = false;
static char interactiveCmd[100];
@ -312,6 +339,8 @@ static bool runOneLua(lua_Alloc alloc, const char* script) {
}
void LuaThread::ThreadTask() {
initSystemLua();
while (!chThdShouldTerminateX()) {
bool wasOk = runOneLua(myAlloc<0>, config->luaScript);
@ -327,33 +356,9 @@ void LuaThread::ThreadTask() {
static LuaThread luaThread;
static LuaHandle systemLua;
void initSystemLua() {
efiAssertVoid(OBD_PCM_Processor_Fault, !systemLua, "system lua already init");
Timer startTimer;
startTimer.reset();
systemLua = setupLuaState(myAlloc<1>);
efiAssertVoid(OBD_PCM_Processor_Fault, systemLua, "system lua init fail");
if (!loadScript(systemLua, "function x() end")) {
firmwareError(OBD_PCM_Processor_Fault, "system lua script load fail");
systemLua = nullptr;
return;
}
auto startTime = startTimer.getElapsedSeconds();
efiPrintf("System Lua loaded in %.2f ms using %d bytes", startTime * 1'000, heaps[1].used());
}
void startLua() {
luaThread.Start();
initSystemLua();
addConsoleActionS("lua", [](const char* str){
if (interactivePending) {
return;
@ -382,7 +387,7 @@ void startLua() {
#else // not EFI_UNIT_TEST
void startLua() {
// todo
initSystemLua();
}
#include <stdexcept>

View File

@ -3,6 +3,7 @@ LUA_EXT=$(PROJECT_DIR)/ext/lua
ALLCPPSRC += $(LUA_DIR)/lua.cpp \
$(LUA_DIR)/lua_hooks.cpp \
$(LUA_DIR)/system_lua.cpp \
ALLINC += $(LUA_DIR) $(LUA_DIR)/luaaa $(LUA_EXT)
ALLCSRC += \

View File

@ -0,0 +1,19 @@
const char* getSystemLuaScript() {
return R"LUASCRIPT(
function getConfig(x)
return x
end
primeTimer = Timer.new()
primeTimer:reset();
function pumpLogic()
local prime = primeTimer:getElapsedSeconds() > getConfig(0)
local spinning = getTimeSinceTrigger() < 1
return prime or spinning
end
)LUASCRIPT";
}

View File

@ -76,3 +76,7 @@ TEST(LuaBasic, ExpectNumOrNilReturnsNothing) {
end
)"), unexpected);
}
TEST(SystemLua, ScriptLoads) {
startLua();
}