From 1685228ab776d8d75b3e32d91fd9ddd65ff9853c Mon Sep 17 00:00:00 2001
From: helixhorned <helixhorned@1a8010ca-5511-0410-912e-c29ae57300e0>
Date: Sun, 24 Feb 2013 16:05:31 +0000
Subject: [PATCH] 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
---
 polymer/eduke32/Makefile                  |  7 ++++++-
 polymer/eduke32/Makefile.common           |  7 ++++++-
 polymer/eduke32/build/include/lunatic.h   |  2 +-
 polymer/eduke32/build/src/lunatic.c       | 11 ++++++++---
 polymer/eduke32/source/astub.c            |  2 +-
 polymer/eduke32/source/game.c             | 12 +++++++++---
 polymer/eduke32/source/lunatic/dynsymlist |  1 +
 7 files changed, 32 insertions(+), 10 deletions(-)

diff --git a/polymer/eduke32/Makefile b/polymer/eduke32/Makefile
index ba6437b65..9da975b63 100644
--- a/polymer/eduke32/Makefile
+++ b/polymer/eduke32/Makefile
@@ -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
diff --git a/polymer/eduke32/Makefile.common b/polymer/eduke32/Makefile.common
index b343bf9d3..82bce5284 100644
--- a/polymer/eduke32/Makefile.common
+++ b/polymer/eduke32/Makefile.common
@@ -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
diff --git a/polymer/eduke32/build/include/lunatic.h b/polymer/eduke32/build/include/lunatic.h
index 333e4e805..6d28cfc9e 100644
--- a/polymer/eduke32/build/include/lunatic.h
+++ b/polymer/eduke32/build/include/lunatic.h
@@ -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); }
 
diff --git a/polymer/eduke32/build/src/lunatic.c b/polymer/eduke32/build/src/lunatic.c
index b3e3fa398..ddce89292 100644
--- a/polymer/eduke32/build/src/lunatic.c
+++ b/polymer/eduke32/build/src/lunatic.c
@@ -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);
 }
diff --git a/polymer/eduke32/source/astub.c b/polymer/eduke32/source/astub.c
index ca6831b0b..a9be55c7b 100644
--- a/polymer/eduke32/source/astub.c
+++ b/polymer/eduke32/source/astub.c
@@ -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
diff --git a/polymer/eduke32/source/game.c b/polymer/eduke32/source/game.c
index 8bd8e6e37..4580f5f3e 100644
--- a/polymer/eduke32/source/game.c
+++ b/polymer/eduke32/source/game.c
@@ -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)
diff --git a/polymer/eduke32/source/lunatic/dynsymlist b/polymer/eduke32/source/lunatic/dynsymlist
index f1fa2a06a..2f4b1850a 100644
--- a/polymer/eduke32/source/lunatic/dynsymlist
+++ b/polymer/eduke32/source/lunatic/dynsymlist
@@ -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;