1
0
Fork 0
mirror of https://github.com/opentx/opentx.git synced 2025-07-16 04:45:17 +03:00

Re #3054: Lua byte-code of every loaded script is dumped to a file if LUA_COMPILER=YES is defined (only for simu target. Original code author (dsbeach)

This commit is contained in:
Damjan Adamic 2015-11-15 16:32:12 +01:00
parent 2572ed0eba
commit 754b875d81
2 changed files with 55 additions and 0 deletions

View file

@ -40,6 +40,11 @@
#include "bin_allocator.h"
#include "lua/lua_api.h"
#if defined(LUA_COMPILER) && defined(SIMU)
#include <lundump.h>
#include <lstate.h>
#endif
#define PERMANENT_SCRIPTS_MAX_INSTRUCTIONS (10000/100)
#define MANUAL_SCRIPTS_MAX_INSTRUCTIONS (20000/100)
#define SET_LUA_INSTRUCTIONS_COUNT(x) (instructionsPercent=0, lua_sethook(L, hook, LUA_MASKCOUNT, x))
@ -228,6 +233,44 @@ void luaFree(ScriptInternalData & sid)
UNPROTECT_LUA();
}
#if defined(LUA_COMPILER) && defined(SIMU)
static int luaDumpWriter(lua_State* L, const void* p, size_t size, void* u)
{
UNUSED(L);
UINT written;
FRESULT result = f_write((FIL *)u, p, size, &written);
return (result != FR_OK && !written);
}
static void luaCompileAndSave(const char *filename)
{
FIL D;
char bytecodeName[1024];
strcpy(bytecodeName, filename);
strcat(bytecodeName, "c");
if (f_stat(bytecodeName, 0) == FR_OK) {
return; // compiled file already exists
}
if (f_open(&D, bytecodeName, FA_WRITE | FA_CREATE_ALWAYS) != FR_OK) {
TRACE("Could not open Lua bytecode output file %s", bytecodeName);
return;
}
PROTECT_LUA() {
if (luaL_loadfile(L, filename) == 0) {
lua_lock(L);
luaU_dump(L, getproto(L->top - 1), luaDumpWriter, &D, 1);
lua_unlock(L);
TRACE("Saved Lua bytecode to file %s", bytecodeName);
}
}
UNPROTECT_LUA();
f_close(&D);
}
#endif
int luaLoad(const char *filename, ScriptInternalData & sid, ScriptInputsOutputs * sio=NULL)
{
int init = 0;
@ -244,6 +287,10 @@ int luaLoad(const char *filename, ScriptInternalData & sid, ScriptInputsOutputs
return SCRIPT_PANIC;
}
#if defined(LUA_COMPILER) && defined(SIMU)
luaCompileAndSave(filename);
#endif
SET_LUA_INSTRUCTIONS_COUNT(MANUAL_SCRIPTS_MAX_INSTRUCTIONS);
PROTECT_LUA() {