mirror of
https://github.com/opentx/opentx.git
synced 2025-07-17 13:25:20 +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:
parent
2572ed0eba
commit
754b875d81
2 changed files with 55 additions and 0 deletions
|
@ -379,6 +379,10 @@ WATCHDOG_TEST = NO
|
||||||
# Values = NO, YES
|
# Values = NO, YES
|
||||||
WARNINGS_AS_ERRORS = NO
|
WARNINGS_AS_ERRORS = NO
|
||||||
|
|
||||||
|
# Enable saving of Lua byte-code of all loaded scripts
|
||||||
|
# Values = NO, YES
|
||||||
|
LUA_COMPILER = NO
|
||||||
|
|
||||||
#------- END BUILD OPTIONS ---------------------------
|
#------- END BUILD OPTIONS ---------------------------
|
||||||
|
|
||||||
# Define programs and commands.
|
# Define programs and commands.
|
||||||
|
@ -1416,6 +1420,10 @@ ifeq ($(IRPROTOS), YES)
|
||||||
CPPDEFS += -DIRPROTOS
|
CPPDEFS += -DIRPROTOS
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
ifeq ($(LUA_COMPILER), YES)
|
||||||
|
CPPDEFS += -DLUA_COMPILER
|
||||||
|
endif
|
||||||
|
|
||||||
#---------------- Compiler Options C++ ----------------
|
#---------------- Compiler Options C++ ----------------
|
||||||
# -g*: generate debugging information
|
# -g*: generate debugging information
|
||||||
# -O*: optimization level
|
# -O*: optimization level
|
||||||
|
|
|
@ -40,6 +40,11 @@
|
||||||
#include "bin_allocator.h"
|
#include "bin_allocator.h"
|
||||||
#include "lua/lua_api.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 PERMANENT_SCRIPTS_MAX_INSTRUCTIONS (10000/100)
|
||||||
#define MANUAL_SCRIPTS_MAX_INSTRUCTIONS (20000/100)
|
#define MANUAL_SCRIPTS_MAX_INSTRUCTIONS (20000/100)
|
||||||
#define SET_LUA_INSTRUCTIONS_COUNT(x) (instructionsPercent=0, lua_sethook(L, hook, LUA_MASKCOUNT, x))
|
#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();
|
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 luaLoad(const char *filename, ScriptInternalData & sid, ScriptInputsOutputs * sio=NULL)
|
||||||
{
|
{
|
||||||
int init = 0;
|
int init = 0;
|
||||||
|
@ -244,6 +287,10 @@ int luaLoad(const char *filename, ScriptInternalData & sid, ScriptInputsOutputs
|
||||||
return SCRIPT_PANIC;
|
return SCRIPT_PANIC;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if defined(LUA_COMPILER) && defined(SIMU)
|
||||||
|
luaCompileAndSave(filename);
|
||||||
|
#endif
|
||||||
|
|
||||||
SET_LUA_INSTRUCTIONS_COUNT(MANUAL_SCRIPTS_MAX_INSTRUCTIONS);
|
SET_LUA_INSTRUCTIONS_COUNT(MANUAL_SCRIPTS_MAX_INSTRUCTIONS);
|
||||||
|
|
||||||
PROTECT_LUA() {
|
PROTECT_LUA() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue