Lunatic: compile defs.ilua as bytecode and load it from the executable.

git-svn-id: https://svn.eduke32.com/eduke32@3517 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
helixhorned 2013-02-24 16:05:31 +00:00
parent acddac64be
commit 1685228ab7
7 changed files with 32 additions and 10 deletions

View file

@ -158,7 +158,8 @@ ifneq (0,$(LUNATIC))
$(OBJ)/luaJIT_BC_control.$o \
$(OBJ)/luaJIT_BC_bcarray.$o \
$(OBJ)/luaJIT_BC_bcheck.$o \
$(OBJ)/luaJIT_BC_xmath.$o
$(OBJ)/luaJIT_BC_xmath.$o \
$(OBJ)/luaJIT_BC_defs.$o \
# now, take care of having the necessary symbols (sector, wall, etc.) in the
# executable no matter what the debugging level
@ -438,6 +439,10 @@ else
if $(LUAJIT) -bg $< $@; then $(COMPILE_OK); else $(COMPILE_FAILED); fi
endif
# Same thing for defs.ilua which I'm too reluctant to rename now:
$(OBJ)/luaJIT_BC_%.$o: $(SRC)/lunatic/%.ilua
if $(LUAJIT) -bg $< $@; then $(COMPILE_OK); else $(COMPILE_FAILED); fi
$(OBJ)/%.$o: $(SRC)/lunatic/%.c
$(COMPILE_STATUS)
if $(COMPILER) $(OURCFLAGS) -c $< -o $@; then $(COMPILE_OK); else $(COMPILE_FAILED); fi

View file

@ -414,7 +414,7 @@ LUNATIC=0
LUAJIT=luajit
# for LJ headers:
LUAJIT_WIN_SRC:= g:/mod/LuaJIT-2.0.0-beta8/src
LUAJIT_WIN_SRC:= g:/mod/luajit-2.0/src
ifneq ($(LUNATIC),0)
# FIXME: Lunatic doesn't build with inlining because of wacky include
@ -428,6 +428,11 @@ ifneq ($(LUNATIC),0)
endif
BASECOMMONFLAGS+= -I$(SRC)/lunatic -DLUNATIC
# Determine size of defs.ilua bytecode
# XXX: Runs way too many times because it's a "recursively expanded" variable.
DEFS_BC_SIZE = $(shell $(LUAJIT) -bg -t h $(SRC)/lunatic/defs.ilua -)
BASECOMMONFLAGS+= -DLUNATIC_DEFS_BC_SIZE=$(word 3, $(DEFS_BC_SIZE))
ifeq ($(PLATFORM),WINDOWS)
BASELIBS+= -lluajit
else

View file

@ -28,7 +28,7 @@ void (*L_OutOfMemFunc)(void);
int L_CreateState(L_State *estate, const char *name, void (*StateSetupFunc)(lua_State *));
void L_DestroyState(L_State *estate);
int L_RunOnce(L_State *estate, const char *fn);
int L_RunString(L_State *estate, char *buf, int dofreebuf);
int L_RunString(L_State *estate, char *buf, int dofreebuf, int size, const char *name);
static inline int L_IsInitialized(const L_State *estate) { return (estate->L != NULL); }

View file

@ -180,7 +180,9 @@ static void L_ErrorPrint(const char *errmsg)
OSD_Printf(OSD_ERROR "runtime error: %s\n", errmsg);
}
int L_RunString(L_State *estate, char *buf, int dofreebuf)
// size < 0: length of <buf> is determined using strlen()
// size >= 0: size given, for loading of LuaJIT bytecode
int L_RunString(L_State *estate, char *buf, int dofreebuf, int size, const char *name)
{
int32_t i;
lua_State *L = estate->L;
@ -190,7 +192,10 @@ int L_RunString(L_State *estate, char *buf, int dofreebuf)
// on top: a traceback function
Bassert(lua_iscfunction(L, 1));
i = luaL_loadstring(L, buf);
if (size < 0)
i = luaL_loadstring(L, buf);
else
i = luaL_loadbuffer(L, buf, size, name);
Bassert(lua_gettop(L)==2);
if (dofreebuf)
Bfree(buf);
@ -233,5 +238,5 @@ int L_RunOnce(L_State *estate, const char *fn)
if (i != 0)
return i;
return L_RunString(estate, buf, 1);
return L_RunString(estate, buf, 1, -1, fn);
}

View file

@ -9262,7 +9262,7 @@ static int32_t osdcmd_lua(const osdfuncparm_t *parm)
return OSDCMD_OK;
}
ret = L_RunString(&g_EmState, (char *)parm->parms[0], 0);
ret = L_RunString(&g_EmState, (char *)parm->parms[0], 0, -1, "console");
if (ret != 0)
OSD_Printf("Error running the Lua code (error code %d)\n", ret);
else

View file

@ -10603,10 +10603,16 @@ int32_t app_main(int32_t argc, const char **argv)
{
initprintf("Lunatic: Error initializing global ELua state (code %d)\n", i);
}
else if ((i = L_RunOnce(&g_ElState, "defs.ilua")))
else
{
initprintf("Lunatic: Error preparing global ELua state (code %d)\n", i);
El_DestroyState(&g_ElState);
extern const char luaJIT_BC_defs[];
if ((i = L_RunString(&g_ElState, (char *)luaJIT_BC_defs, 0,
LUNATIC_DEFS_BC_SIZE, "defs.ilua")))
{
initprintf("Lunatic: Error preparing global ELua state (code %d)\n", i);
El_DestroyState(&g_ElState);
}
}
if (i)

View file

@ -126,6 +126,7 @@ luaJIT_BC_control;
luaJIT_BC_bcarray;
luaJIT_BC_bcheck;
luaJIT_BC_xmath;
luaJIT_BC_defs;
rand_jkiss_u32;
rand_jkiss_dbl;