Index: /trunk/nv/lib/detail/lua_functions.inc
===================================================================
--- /trunk/nv/lib/detail/lua_functions.inc	(revision 164)
+++ /trunk/nv/lib/detail/lua_functions.inc	(revision 164)
@@ -0,0 +1,200 @@
+/* State manipulation */
+NV_LUA_FUN( lua_State *,lua_newstate,(lua_Alloc f, void *ud) );
+NV_LUA_FUN( void       ,lua_close,(lua_State *L) );
+NV_LUA_FUN( lua_State *,lua_newthread,(lua_State *L) );
+NV_LUA_FUN( lua_CFunction,lua_atpanic,(lua_State *L, lua_CFunction panicf) );
+NV_LUA_FUN_52( const lua_Number *,lua_version,(lua_State *L) );
+
+/* Basic stack manipulation */
+NV_LUA_FUN_52( int  ,lua_absindex,(lua_State *L, int idx) );
+NV_LUA_FUN( int  ,lua_gettop,(lua_State *L) );
+NV_LUA_FUN( void ,lua_settop,(lua_State *L, int idx) );
+NV_LUA_FUN( void ,lua_pushvalue,(lua_State *L, int idx) );
+NV_LUA_FUN( void ,lua_remove,(lua_State *L, int idx) );
+NV_LUA_FUN( void ,lua_insert,(lua_State *L, int idx) );
+NV_LUA_FUN( void ,lua_replace,(lua_State *L, int idx) );
+NV_LUA_FUN_52( void ,lua_copy,(lua_State *L, int fromidx, int toidx) );
+NV_LUA_FUN( int  ,lua_checkstack,(lua_State *L, int sz) );
+NV_LUA_FUN( void ,lua_xmove,(lua_State *from, lua_State *to, int n) );
+
+/* Access functions (stack -> C) */
+NV_LUA_FUN( int            ,lua_isnumber,(lua_State *L, int idx) );
+NV_LUA_FUN( int            ,lua_isstring,(lua_State *L, int idx) );
+NV_LUA_FUN( int            ,lua_iscfunction,(lua_State *L, int idx) );
+NV_LUA_FUN( int            ,lua_isuserdata,(lua_State *L, int idx) );
+NV_LUA_FUN( int            ,lua_type,(lua_State *L, int idx) );
+NV_LUA_FUN( const char*    ,lua_typename,(lua_State *L, int tp) );
+
+NV_LUA_FUN_52( lua_Number     ,lua_tonumberx,(lua_State *L, int idx, int *isnum) );
+NV_LUA_FUN_52( lua_Integer    ,lua_tointegerx,(lua_State *L, int idx, int *isnum) );
+NV_LUA_FUN_52( lua_Unsigned   ,lua_tounsignedx,(lua_State *L, int idx, int *isnum) );
+NV_LUA_FUN_51( lua_Number     ,lua_tonumber,(lua_State *L, int idx) );
+NV_LUA_FUN_51( lua_Integer    ,lua_tointeger,(lua_State *L, int idx) );
+NV_LUA_FUN( int            ,lua_toboolean,(lua_State *L, int idx) );
+NV_LUA_FUN( const char*    ,lua_tolstring,(lua_State *L, int idx, size_t *len) );
+NV_LUA_FUN_52( size_t         ,lua_rawlen,(lua_State *L, int idx) );
+NV_LUA_FUN_51( size_t         ,lua_objlen,(lua_State *L, int idx) );
+NV_LUA_FUN( lua_CFunction  ,lua_tocfunction,(lua_State *L, int idx) );
+NV_LUA_FUN( void*          ,lua_touserdata,(lua_State *L, int idx) );
+NV_LUA_FUN( lua_State*     ,lua_tothread,(lua_State *L, int idx) );
+NV_LUA_FUN( const void*    ,lua_topointer,(lua_State *L, int idx) );
+
+/* Comparison and arithmetic functions */
+NV_LUA_FUN_52( void ,lua_arith,(lua_State *L, int op) );
+NV_LUA_FUN( int  ,lua_rawequal,(lua_State *L, int idx1, int idx2) );
+NV_LUA_FUN_52( int,lua_compare,(lua_State *L, int idx1, int idx2, int op) );
+NV_LUA_FUN_51( int,lua_equal, (lua_State *L, int idx1, int idx2) );
+NV_LUA_FUN_51( int,lua_lessthan, (lua_State *L, int idx1, int idx2) );
+
+/* Push functions (C -> stack) */
+NV_LUA_FUN( void        ,lua_pushnil,(lua_State *L) );
+NV_LUA_FUN( void        ,lua_pushnumber,(lua_State *L, lua_Number n) );
+NV_LUA_FUN( void        ,lua_pushinteger,(lua_State *L, lua_Integer n) );
+NV_LUA_FUN_52( void        ,lua_pushunsigned,(lua_State *L, lua_Unsigned n) );
+NV_LUA_FUN_52( const char* ,lua_pushlstring,(lua_State *L, const char *s, size_t l) );
+NV_LUA_FUN_52( const char* ,lua_pushstring,(lua_State *L, const char *s) );
+NV_LUA_FUN_51( void,lua_pushlstring,(lua_State *L, const char *s, size_t l) );
+NV_LUA_FUN_51( void,lua_pushstring,(lua_State *L, const char *s) );
+NV_LUA_FUN( const char* ,lua_pushvfstring,(lua_State *L, const char *fmt,
+                                                      va_list argp) );
+NV_LUA_FUN( const char *,lua_pushfstring,(lua_State *L, const char *fmt, ...) );
+NV_LUA_FUN( void ,lua_pushcclosure,(lua_State *L, lua_CFunction fn, int n) );
+NV_LUA_FUN( void ,lua_pushboolean,(lua_State *L, int b) );
+NV_LUA_FUN( void ,lua_pushlightuserdata,(lua_State *L, void *p) );
+NV_LUA_FUN( int  ,lua_pushthread,(lua_State *L) );
+
+/* Get functions (Lua -> stack) */
+NV_LUA_FUN_52( void ,lua_getglobal,(lua_State *L, const char *var) );
+NV_LUA_FUN( void ,lua_gettable,(lua_State *L, int idx) );
+NV_LUA_FUN( void ,lua_getfield,(lua_State *L, int idx, const char *k) );
+NV_LUA_FUN( void ,lua_rawget,(lua_State *L, int idx) );
+NV_LUA_FUN( void ,lua_rawgeti,(lua_State *L, int idx, int n) );
+NV_LUA_FUN_52( void ,lua_rawgetp,(lua_State *L, int idx, const void *p) );
+NV_LUA_FUN( void ,lua_createtable,(lua_State *L, int narr, int nrec) );
+NV_LUA_FUN( void*,lua_newuserdata,(lua_State *L, size_t sz) );
+NV_LUA_FUN( int  ,lua_getmetatable,(lua_State *L, int objindex) );
+NV_LUA_FUN_52( void ,lua_getuservalue,(lua_State *L, int idx) );
+NV_LUA_FUN_51( void ,lua_getfenv,(lua_State *L, int idx) );
+
+/* Set functions (stack -> Lua) */
+NV_LUA_FUN_52( void ,lua_setglobal,(lua_State *L, const char *var) );
+NV_LUA_FUN( void ,lua_settable,(lua_State *L, int idx) );
+NV_LUA_FUN( void ,lua_setfield,(lua_State *L, int idx, const char *k) );
+NV_LUA_FUN( void ,lua_rawset,(lua_State *L, int idx) );
+NV_LUA_FUN( void ,lua_rawseti,(lua_State *L, int idx, int n) );
+NV_LUA_FUN_52( void ,lua_rawsetp,(lua_State *L, int idx, const void *p) );
+NV_LUA_FUN( int  ,lua_setmetatable,(lua_State *L, int objindex) );
+NV_LUA_FUN_52( void ,lua_setuservalue,(lua_State *L, int idx) );
+NV_LUA_FUN_51( void ,lua_setfenv,(lua_State *L, int idx) );
+
+/* 'load' and 'call' functions (load and run Lua code) */
+NV_LUA_FUN_52( void ,lua_callk,(lua_State *L, int nargs, int nresults, int ctx, lua_CFunction k) );
+NV_LUA_FUN_52( int  ,lua_pcallk,(lua_State *L, int nargs, int nresults, int errfunc, int ctx, lua_CFunction k) );
+NV_LUA_FUN_51( void ,lua_call,(lua_State *L, int nargs, int nresults) );
+NV_LUA_FUN_51( int  ,lua_pcall,(lua_State *L, int nargs, int nresults, int errfunc) );
+NV_LUA_FUN_51( int  ,lua_cpcall,(lua_State *L, lua_CFunction func, void *ud) );
+NV_LUA_FUN_52( int  ,lua_getctx,(lua_State *L, int *ctx) );
+NV_LUA_FUN_52( int  ,lua_load,(lua_State *L, lua_Reader reader, void *dt, const char *chunkname, const char *mode) );
+NV_LUA_FUN_51( int  ,lua_load,(lua_State *L, lua_Reader reader, void *dt, const char *chunkname) );
+NV_LUA_FUN( int  ,lua_dump,(lua_State *L, lua_Writer writer, void *data) );
+
+/* Coroutine functions */
+NV_LUA_FUN_52( int ,lua_yieldk,(lua_State *L, int nresults, int ctx, lua_CFunction k) );
+NV_LUA_FUN_51( int ,lua_yield,(lua_State *L, int nresults) );
+NV_LUA_FUN_52( int ,lua_resume,(lua_State *L, lua_State *from, int narg) );
+NV_LUA_FUN_51( int ,lua_resume,(lua_State *L, int narg) );
+NV_LUA_FUN( int ,lua_status,(lua_State *L) );
+
+/* Garbage-collection function and options */
+NV_LUA_FUN( int,lua_gc,(lua_State *L, int what, int data) );
+
+/* Miscellaneous functions */
+NV_LUA_FUN( int  ,lua_error,(lua_State *L) );
+NV_LUA_FUN( int  ,lua_next,(lua_State *L, int idx) );
+NV_LUA_FUN( void ,lua_concat,(lua_State *L, int n) );
+NV_LUA_FUN_52( void ,lua_len   ,(lua_State *L, int idx) );
+NV_LUA_FUN( lua_Alloc,lua_getallocf,(lua_State *L, void **ud) );
+NV_LUA_FUN( void     ,lua_setallocf,(lua_State *L, lua_Alloc f, void *ud) );
+
+/* Debug API */
+NV_LUA_FUN( int,lua_getstack,(lua_State *L, int level, lua_Debug *ar) );
+NV_LUA_FUN( int,lua_getinfo,(lua_State *L, const char *what, lua_Debug *ar) );
+NV_LUA_FUN( const char *,lua_getlocal,(lua_State *L, const lua_Debug *ar, int n) );
+NV_LUA_FUN( const char *,lua_setlocal,(lua_State *L, const lua_Debug *ar, int n) );
+NV_LUA_FUN( const char *,lua_getupvalue,(lua_State *L, int funcindex, int n) );
+NV_LUA_FUN( const char *,lua_setupvalue,(lua_State *L, int funcindex, int n) );
+NV_LUA_FUN_52( void *,lua_upvalueid,(lua_State *L, int fidx, int n) );
+NV_LUA_FUN_52( void  ,lua_upvaluejoin,(lua_State *L, int fidx1, int n1, int fidx2, int n2) );
+NV_LUA_FUN( int,lua_sethook,(lua_State *L, lua_Hook func, int mask, int count) );
+NV_LUA_FUN( lua_Hook,lua_gethook,(lua_State *L) );
+NV_LUA_FUN( int,lua_gethookmask,(lua_State *L) );
+NV_LUA_FUN( int,lua_gethookcount,(lua_State *L) );
+
+/* buffer API */
+NV_LUA_FUN( void,luaL_buffinit, (lua_State *L, luaL_Buffer *B) );
+NV_LUA_FUN_51( char *,luaL_prepbuffer,(luaL_Buffer *B) );
+NV_LUA_FUN_52( char *,luaL_prepbuffsize,(luaL_Buffer *B, size_t sz) );
+NV_LUA_FUN( void,luaL_addlstring,(luaL_Buffer *B, const char *s, size_t l) );
+NV_LUA_FUN( void,luaL_addstring,(luaL_Buffer *B, const char *s) );
+NV_LUA_FUN( void,luaL_addvalue,(luaL_Buffer *B) );
+NV_LUA_FUN( void,luaL_pushresult,(luaL_Buffer *B) );	
+NV_LUA_FUN_52( void,luaL_pushresultsize,(luaL_Buffer *B, size_t sz) );
+NV_LUA_FUN_52( char *,luaL_buffinitsize,(lua_State *L, luaL_Buffer *B, size_t sz) );
+
+/* lualib API */
+NV_LUA_FUN( int,luaopen_base,(lua_State *L) );
+NV_LUA_FUN( int,luaopen_table,(lua_State *L) );
+NV_LUA_FUN( int,luaopen_io,(lua_State *L) );
+NV_LUA_FUN( int,luaopen_os,(lua_State *L) );
+NV_LUA_FUN( int,luaopen_string,(lua_State *L) );
+NV_LUA_FUN( int,luaopen_math,(lua_State *L) );
+NV_LUA_FUN( int,luaopen_debug,(lua_State *L) );
+NV_LUA_FUN( int,luaopen_package,(lua_State *L) );
+NV_LUA_FUN( int,luaL_openlibs,(lua_State *L) );
+NV_LUA_FUN_52( int,luaopen_coroutine,(lua_State *L) );
+NV_LUA_FUN_52( int,luaopen_bit32,(lua_State *L) );
+
+/* lauxlib API */
+NV_LUA_FUN_52( void  ,luaL_checkversion_,(lua_State *L, lua_Number ver) );
+NV_LUA_FUN_52( const char *,luaL_tolstring,(lua_State *L, int idx, size_t *len) );
+NV_LUA_FUN_52( lua_Unsigned ,luaL_checkunsigned,(lua_State *L, int numArg) );
+NV_LUA_FUN_52( lua_Unsigned ,luaL_optunsigned,(lua_State *L, int numArg, lua_Unsigned def) );
+NV_LUA_FUN_52( void  ,luaL_setmetatable,(lua_State *L, const char *tname) );
+NV_LUA_FUN_52( void *,luaL_testudata,(lua_State *L, int ud, const char *tname) );
+NV_LUA_FUN_52( int ,luaL_fileresult,(lua_State *L, int stat, const char *fname) );
+NV_LUA_FUN_52( int ,luaL_execresult,(lua_State *L, int stat) );
+NV_LUA_FUN_51( void  ,luaL_register,(lua_State *L, const char *libname,	const luaL_Reg *l) );
+NV_LUA_FUN_51( void  ,luaL_typerror,(lua_State *L, int narg, const char *tname) );
+NV_LUA_FUN( int ,luaL_getmetafield,(lua_State *L, int obj, const char *e) );
+NV_LUA_FUN( int ,luaL_callmeta,(lua_State *L, int obj, const char *e) );
+NV_LUA_FUN( int ,luaL_argerror,(lua_State *L, int numarg, const char *extramsg) );
+NV_LUA_FUN( const char *,luaL_checklstring,(lua_State *L, int numArg, size_t *l) );
+NV_LUA_FUN( const char *,luaL_optlstring,(lua_State *L, int numArg, const char *def, size_t *l) );
+NV_LUA_FUN( lua_Number ,luaL_checknumber,(lua_State *L, int numArg) );
+NV_LUA_FUN( lua_Number ,luaL_optnumber,(lua_State *L, int nArg, lua_Number def) );
+NV_LUA_FUN( lua_Integer ,luaL_checkinteger,(lua_State *L, int numArg) );
+NV_LUA_FUN( lua_Integer ,luaL_optinteger,(lua_State *L, int nArg, lua_Integer def) );
+NV_LUA_FUN( void ,luaL_checkstack,(lua_State *L, int sz, const char *msg) );
+NV_LUA_FUN( void ,luaL_checktype,(lua_State *L, int narg, int t) );
+NV_LUA_FUN( void ,luaL_checkany,(lua_State *L, int narg) );
+NV_LUA_FUN( int   ,luaL_newmetatable,(lua_State *L, const char *tname) );
+NV_LUA_FUN( void *,luaL_checkudata,(lua_State *L, int ud, const char *tname) );
+NV_LUA_FUN( void ,luaL_where,(lua_State *L, int lvl) );
+NV_LUA_FUN( int ,luaL_error,(lua_State *L, const char *fmt, ...) );
+NV_LUA_FUN( int ,luaL_checkoption,(lua_State *L, int narg, const char *def, const char *const lst[]) );
+
+NV_LUA_FUN_52( int ,luaL_loadfilex,(lua_State *L, const char *filename, const char *mode) );
+NV_LUA_FUN_51( int ,luaL_loadfile,(lua_State *L, const char *filename) );
+NV_LUA_FUN_52( int ,luaL_loadbufferx,(lua_State *L, const char *buff, size_t sz, const char *name, const char *mode) );
+NV_LUA_FUN_51( int ,luaL_loadbuffer,(lua_State *L, const char *buff, size_t sz, const char *name ) );
+NV_LUA_FUN_52( int ,luaL_len,(lua_State *L, int idx) );
+NV_LUA_FUN_51( const char *,luaL_findtable, (lua_State *L, int idx, const char *fname, int szhint) );
+NV_LUA_FUN_52( void ,luaL_setfuncs,(lua_State *L, const luaL_Reg *l, int nup) );
+NV_LUA_FUN_52( int ,luaL_getsubtable,(lua_State *L, int idx, const char *fname) );
+NV_LUA_FUN_52( void ,luaL_traceback,(lua_State *L, lua_State *L1, const char *msg, int level) );
+NV_LUA_FUN_52( void ,luaL_requiref,(lua_State *L, const char *modname, lua_CFunction openf, int glb) );
+NV_LUA_FUN( int ,luaL_ref,(lua_State *L, int t) );
+NV_LUA_FUN( void ,luaL_unref,(lua_State *L, int t, int ref) );
+NV_LUA_FUN( int ,luaL_loadstring,(lua_State *L, const char *s) );
+NV_LUA_FUN( lua_State *,luaL_newstate, (void) );
+NV_LUA_FUN( const char *,luaL_gsub,(lua_State *L, const char *s, const char *p, const char *r) );
Index: /trunk/nv/lib/lua.hh
===================================================================
--- /trunk/nv/lib/lua.hh	(revision 163)
+++ /trunk/nv/lib/lua.hh	(revision 164)
@@ -38,10 +38,38 @@
 //#define NV_LUA_SHARED
 
-#if NV_PLATFORM == NV_WINDOWS
-#	define NV_LUA_PATH "lua52.dll"
-#elif NV_PLATFORM == NV_APPLE
-#	define NV_LUA_PATH "lua5.2.dylib"
-#else
-#	define NV_LUA_PATH "lua5.2.so"
+#define NV_LUA_51     0
+#define NV_LUA_51_JIT 1
+#define NV_LUA_52     2
+
+#ifndef NV_LUA_VERSION
+#	define NV_LUA_VERSION NV_LUA_52
+#endif
+
+#if NV_LUA_VERSION == NV_LUA_52
+#	if NV_PLATFORM == NV_WINDOWS
+#		define NV_LUA_PATH "lua52.dll"
+#	elif NV_PLATFORM == NV_APPLE
+#		define NV_LUA_PATH "lua5.2.dylib"
+#	else
+#		define NV_LUA_PATH "lua5.2.so"
+#	endif
+#elif NV_LUA_VERSION == NV_LUA_51 
+#	if NV_PLATFORM == NV_WINDOWS
+#		define NV_LUA_PATH "lua51.dll"
+#	elif NV_PLATFORM == NV_APPLE
+#		define NV_LUA_PATH "lua5.1.dylib"
+#	else
+#		define NV_LUA_PATH "lua5.1.so"
+#	endif
+#elif NV_LUA_VERSION == NV_LUA_51_JIT
+#	if NV_PLATFORM == NV_WINDOWS
+#		define NV_LUA_PATH "luajit.dll"
+#	elif NV_PLATFORM == NV_APPLE
+#		define NV_LUA_PATH "luajit.dylib"
+#	else
+#		define NV_LUA_PATH "luajit.so"
+#	endif
+#else
+#	error "Unrecognized NV_LUA_VERSION!"
 #endif
 
@@ -59,10 +87,4 @@
 #endif
 
-#if defined(NV_LUA_DYNAMIC)
-#	define NV_LUA_FUN( rtype, fname, fparams ) NV_LUA_API rtype (*fname) fparams
-#else
-#	define NV_LUA_FUN( rtype, fname, fparams ) NV_LUA_API rtype fname fparams
-#endif
-
 /* luaconf.h definitions (only the ones needed in the headers) */
 #define LUA_NUMBER   double
@@ -76,7 +98,14 @@
 /* lua.h */
 #define LUA_VERSION_MAJOR	"5"
-#define LUA_VERSION_MINOR	"2"
-#define LUA_VERSION_NUM		502
-#define LUA_VERSION_RELEASE	"1"
+#if NV_LUA_VERSION == NV_LUA_52
+#	define LUA_VERSION_MINOR	"2"
+#	define LUA_VERSION_NUM		502
+#	define LUA_VERSION_RELEASE	"1"
+#else
+#	define LUA_VERSION_MAJOR	"5"
+#	define LUA_VERSION_MINOR	"1"
+#	define LUA_VERSION_NUM		501
+#	define LUA_VERSION_RELEASE	"4"
+#endif 
 
 #define LUA_VERSION	"Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR
@@ -90,6 +119,13 @@
 #define LUA_REFNIL      (-1)
 
-#define LUA_REGISTRYINDEX	LUAI_FIRSTPSEUDOIDX
-#define lua_upvalueindex(i)	(LUA_REGISTRYINDEX - (i))
+#if NV_LUA_VERSION == NV_LUA_52
+#	define LUA_REGISTRYINDEX	LUAI_FIRSTPSEUDOIDX
+#	define lua_upvalueindex(i)	(LUA_REGISTRYINDEX - (i))
+#else
+#	define LUA_REGISTRYINDEX      (-10000)
+#	define LUA_ENVIRONINDEX       (-10001)
+#	define LUA_GLOBALSINDEX       (-10002)
+#	define lua_upvalueindex(i)    (LUA_GLOBALSINDEX-(i))
+#endif
 
 #define LUAL_BUFFERSIZE		BUFSIZ
@@ -112,4 +148,5 @@
   const char *source;	/* (S) */
   int currentline;	/* (l) */
+#if NV_LUA_VERSION == NV_LUA_52
   int linedefined;	/* (S) */
   int lastlinedefined;	/* (S) */
@@ -120,4 +157,11 @@
   char short_src[LUA_IDSIZE]; /* (S) */
   struct CallInfo *i_ci;  /* active function */
+#else
+  int nups;
+  int linedefined;	/* (S) */
+  int lastlinedefined;	/* (S) */
+  char short_src[LUA_IDSIZE]; /* (S) */
+  int i_ci;  /* active function */
+#endif
 };
 
@@ -128,16 +172,27 @@
 } luaL_Reg;
 
+#if NV_LUA_VERSION == NV_LUA_52
 typedef struct luaL_Buffer {
-  char *b;  /* buffer address */
-  size_t size;  /* buffer size */
-  size_t n;  /* number of characters in buffer */
-  lua_State *L;
-  char initb[LUAL_BUFFERSIZE];  /* initial buffer */
+	char *b;  /* buffer address */
+	size_t size;  /* buffer size */
+	size_t n;  /* number of characters in buffer */
+	lua_State *L;
+	char initb[LUAL_BUFFERSIZE];  /* initial buffer */
 } luaL_Buffer;
-
+#else
+typedef struct luaL_Buffer {
+	char *p;
+	int lvl;
+	lua_State *L;
+	char initb[LUAL_BUFFERSIZE];
+} luaL_Buffer;
+#endif
+
+#if NV_LUA_VERSION == NV_LUA_52
 typedef struct luaL_Stream {
   FILE *f;  /* stream (NULL for incompletely created streams) */
   lua_CFunction closef;  /* to close stream (NULL for closed streams) */
 } luaL_Stream;
+#endif
 
 typedef LUA_NUMBER lua_Number;
@@ -151,8 +206,12 @@
 #define LUA_ERRSYNTAX	3
 #define LUA_ERRMEM	4
-#define LUA_ERRGCMM	5
-#define LUA_ERRERR	6
-#define LUA_ERRFILE 7
-
+#if NV_LUA_VERSION == NV_LUA_52
+#	define LUA_ERRGCMM	5
+#	define LUA_ERRERR	6
+#	define LUA_ERRFILE  7
+#else
+#	define LUA_ERRERR	5
+#	define LUA_ERRFILE  6
+#endif
 
 /* Type codes */
@@ -168,25 +227,33 @@
 #define LUA_TTHREAD			8
 
-#define LUA_NUMTAGS			9
+#if NV_LUA_VERSION == NV_LUA_52
+#	define LUA_NUMTAGS			9
+#endif
 #define LUA_MINSTACK		20
 
+#if NV_LUA_VERSION == NV_LUA_52
 /* Pre-defined registers */
-#define LUA_RIDX_MAINTHREAD	1
-#define LUA_RIDX_GLOBALS	2
-#define LUA_RIDX_LAST		LUA_RIDX_GLOBALS
-
+#	define LUA_RIDX_MAINTHREAD	1
+#	define LUA_RIDX_GLOBALS	2
+#	define LUA_RIDX_LAST		LUA_RIDX_GLOBALS
+#endif
+
+#if NV_LUA_VERSION == NV_LUA_52
 /* OP codes */
-#define LUA_OPADD	0
-#define LUA_OPSUB	1
-#define LUA_OPMUL	2
-#define LUA_OPDIV	3
-#define LUA_OPMOD	4
-#define LUA_OPPOW	5
-#define LUA_OPUNM	6
+#	define LUA_OPADD	0
+#	define LUA_OPSUB	1
+#	define LUA_OPMUL	2
+#	define LUA_OPDIV	3
+#	define LUA_OPMOD	4
+#	define LUA_OPPOW	5
+#	define LUA_OPUNM	6
+#endif
 
 /* Compare codes */
-#define LUA_OPEQ	0
-#define LUA_OPLT	1
-#define LUA_OPLE	2
+#if NV_LUA_VERSION == NV_LUA_52
+#	define LUA_OPEQ	0
+#	define LUA_OPLT	1
+#	define LUA_OPLE	2
+#endif
 
 /* Event codes */
@@ -199,8 +266,10 @@
 #define LUA_GCSETPAUSE		6
 #define LUA_GCSETSTEPMUL	7
-#define LUA_GCSETMAJORINC	8
-#define LUA_GCISRUNNING		9
-#define LUA_GCGEN			10
-#define LUA_GCINC			11
+#if NV_LUA_VERSION == NV_LUA_52
+#	define LUA_GCSETMAJORINC	8
+#	define LUA_GCISRUNNING		9
+#	define LUA_GCGEN			10
+#	define LUA_GCINC			11
+#endif
 
 /* Debug event codes */
@@ -209,5 +278,9 @@
 #define LUA_HOOKLINE	2
 #define LUA_HOOKCOUNT	3
-#define LUA_HOOKTAILCALL 4
+#if NV_LUA_VERSION == NV_LUA_52
+#	define LUA_HOOKTAILCALL 4
+#else
+#	define LUA_HOOKTAILRET 4
+#endif
 
 /* Debug event masks */
@@ -223,188 +296,40 @@
 #define LUA_OSLIBNAME	"os"
 #define LUA_STRLIBNAME	"string"
-#define LUA_BITLIBNAME	"bit32"
 #define LUA_MATHLIBNAME	"math"
 #define LUA_DBLIBNAME	"debug"
 #define LUA_LOADLIBNAME	"package"
-
-/* State manipulation */
-NV_LUA_FUN( lua_State *,(lua_newstate),(lua_Alloc f, void *ud) );
-NV_LUA_FUN( void       ,(lua_close),(lua_State *L) );
-NV_LUA_FUN( lua_State *,(lua_newthread),(lua_State *L) );
-NV_LUA_FUN( lua_CFunction,(lua_atpanic),(lua_State *L, lua_CFunction panicf) );
-NV_LUA_FUN( const lua_Number *,(lua_version),(lua_State *L) );
-
-/* Basic stack manipulation */
-NV_LUA_FUN( int  ,(lua_absindex),(lua_State *L, int idx) );
-NV_LUA_FUN( int  ,(lua_gettop),(lua_State *L) );
-NV_LUA_FUN( void ,(lua_settop),(lua_State *L, int idx) );
-NV_LUA_FUN( void ,(lua_pushvalue),(lua_State *L, int idx) );
-NV_LUA_FUN( void ,(lua_remove),(lua_State *L, int idx) );
-NV_LUA_FUN( void ,(lua_insert),(lua_State *L, int idx) );
-NV_LUA_FUN( void ,(lua_replace),(lua_State *L, int idx) );
-NV_LUA_FUN( void ,(lua_copy),(lua_State *L, int fromidx, int toidx) );
-NV_LUA_FUN( int  ,(lua_checkstack),(lua_State *L, int sz) );
-NV_LUA_FUN( void ,(lua_xmove),(lua_State *from, lua_State *to, int n) );
-
-/* Access functions (stack -> C) */
-NV_LUA_FUN( int            ,(lua_isnumber),(lua_State *L, int idx) );
-NV_LUA_FUN( int            ,(lua_isstring),(lua_State *L, int idx) );
-NV_LUA_FUN( int            ,(lua_iscfunction),(lua_State *L, int idx) );
-NV_LUA_FUN( int            ,(lua_isuserdata),(lua_State *L, int idx) );
-NV_LUA_FUN( int            ,(lua_type),(lua_State *L, int idx) );
-NV_LUA_FUN( const char*    ,(lua_typename),(lua_State *L, int tp) );
-
-NV_LUA_FUN( lua_Number     ,(lua_tonumberx),(lua_State *L, int idx, int *isnum) );
-NV_LUA_FUN( lua_Integer    ,(lua_tointegerx),(lua_State *L, int idx, int *isnum) );
-NV_LUA_FUN( lua_Unsigned   ,(lua_tounsignedx),(lua_State *L, int idx, int *isnum) );
-NV_LUA_FUN( int            ,(lua_toboolean),(lua_State *L, int idx) );
-NV_LUA_FUN( const char*    ,(lua_tolstring),(lua_State *L, int idx, size_t *len) );
-NV_LUA_FUN( size_t         ,(lua_rawlen),(lua_State *L, int idx) );
-NV_LUA_FUN( lua_CFunction  ,(lua_tocfunction),(lua_State *L, int idx) );
-NV_LUA_FUN( void*          ,(lua_touserdata),(lua_State *L, int idx) );
-NV_LUA_FUN( lua_State*     ,(lua_tothread),(lua_State *L, int idx) );
-NV_LUA_FUN( const void*    ,(lua_topointer),(lua_State *L, int idx) );
-
-/* Comparison and arithmetic functions */
-NV_LUA_FUN( void ,(lua_arith),(lua_State *L, int op) );
-NV_LUA_FUN( int  ,(lua_rawequal),(lua_State *L, int idx1, int idx2) );
-NV_LUA_FUN( int  ,(lua_compare),(lua_State *L, int idx1, int idx2, int op) );
-
-/* Push functions (C -> stack) */
-NV_LUA_FUN( void        ,(lua_pushnil),(lua_State *L) );
-NV_LUA_FUN( void        ,(lua_pushnumber),(lua_State *L, lua_Number n) );
-NV_LUA_FUN( void        ,(lua_pushinteger),(lua_State *L, lua_Integer n) );
-NV_LUA_FUN( void        ,(lua_pushunsigned),(lua_State *L, lua_Unsigned n) );
-NV_LUA_FUN( const char* ,(lua_pushlstring),(lua_State *L, const char *s, size_t l) );
-NV_LUA_FUN( const char* ,(lua_pushstring),(lua_State *L, const char *s) );
-NV_LUA_FUN( const char* ,(lua_pushvfstring),(lua_State *L, const char *fmt,
-                                                      va_list argp) );
-NV_LUA_FUN( const char *,(lua_pushfstring),(lua_State *L, const char *fmt, ...) );
-NV_LUA_FUN( void ,(lua_pushcclosure),(lua_State *L, lua_CFunction fn, int n) );
-NV_LUA_FUN( void ,(lua_pushboolean),(lua_State *L, int b) );
-NV_LUA_FUN( void ,(lua_pushlightuserdata),(lua_State *L, void *p) );
-NV_LUA_FUN( int  ,(lua_pushthread),(lua_State *L) );
-
-/* Get functions (Lua -> stack) */
-NV_LUA_FUN( void ,(lua_getglobal),(lua_State *L, const char *var) );
-NV_LUA_FUN( void ,(lua_gettable),(lua_State *L, int idx) );
-NV_LUA_FUN( void ,(lua_getfield),(lua_State *L, int idx, const char *k) );
-NV_LUA_FUN( void ,(lua_rawget),(lua_State *L, int idx) );
-NV_LUA_FUN( void ,(lua_rawgeti),(lua_State *L, int idx, int n) );
-NV_LUA_FUN( void ,(lua_rawgetp),(lua_State *L, int idx, const void *p) );
-NV_LUA_FUN( void ,(lua_createtable),(lua_State *L, int narr, int nrec) );
-NV_LUA_FUN( void*,(lua_newuserdata),(lua_State *L, size_t sz) );
-NV_LUA_FUN( int  ,(lua_getmetatable),(lua_State *L, int objindex) );
-NV_LUA_FUN( void ,(lua_getuservalue),(lua_State *L, int idx) );
-
-/* Set functions (stack -> Lua) */
-NV_LUA_FUN( void ,(lua_setglobal),(lua_State *L, const char *var) );
-NV_LUA_FUN( void ,(lua_settable),(lua_State *L, int idx) );
-NV_LUA_FUN( void ,(lua_setfield),(lua_State *L, int idx, const char *k) );
-NV_LUA_FUN( void ,(lua_rawset),(lua_State *L, int idx) );
-NV_LUA_FUN( void ,(lua_rawseti),(lua_State *L, int idx, int n) );
-NV_LUA_FUN( void ,(lua_rawsetp),(lua_State *L, int idx, const void *p) );
-NV_LUA_FUN( int  ,(lua_setmetatable),(lua_State *L, int objindex) );
-NV_LUA_FUN( void ,(lua_setuservalue),(lua_State *L, int idx) );
-
-/* 'load' and 'call' functions (load and run Lua code) */
-NV_LUA_FUN( void ,(lua_callk),(lua_State *L, int nargs, int nresults, int ctx, lua_CFunction k) );
-NV_LUA_FUN( int  ,(lua_getctx),(lua_State *L, int *ctx) );
-NV_LUA_FUN( int  ,(lua_pcallk),(lua_State *L, int nargs, int nresults, int errfunc, int ctx, lua_CFunction k) );
-NV_LUA_FUN( int  ,(lua_load),(lua_State *L, lua_Reader reader, void *dt, const char *chunkname, const char *mode) );
-NV_LUA_FUN( int  ,(lua_dump),(lua_State *L, lua_Writer writer, void *data) );
-
-/* Coroutine functions */
-NV_LUA_FUN( int ,(lua_yieldk),(lua_State *L, int nresults, int ctx, lua_CFunction k) );
-NV_LUA_FUN( int ,(lua_resume),(lua_State *L, lua_State *from, int narg) );
-NV_LUA_FUN( int ,(lua_status),(lua_State *L) );
-
-/* Garbage-collection function and options */
-NV_LUA_FUN( int,(lua_gc),(lua_State *L, int what, int data) );
-
-/* Miscellaneous functions */
-NV_LUA_FUN( int  ,(lua_error),(lua_State *L) );
-NV_LUA_FUN( int  ,(lua_next),(lua_State *L, int idx) );
-NV_LUA_FUN( void ,(lua_concat),(lua_State *L, int n) );
-NV_LUA_FUN( void ,(lua_len)   ,(lua_State *L, int idx) );
-NV_LUA_FUN( lua_Alloc,(lua_getallocf),(lua_State *L, void **ud) );
-NV_LUA_FUN( void     ,(lua_setallocf),(lua_State *L, lua_Alloc f, void *ud) );
-
-/* Debug API */
-NV_LUA_FUN( int,(lua_getstack),(lua_State *L, int level, lua_Debug *ar) );
-NV_LUA_FUN( int,(lua_getinfo),(lua_State *L, const char *what, lua_Debug *ar) );
-NV_LUA_FUN( const char *,(lua_getlocal),(lua_State *L, const lua_Debug *ar, int n) );
-NV_LUA_FUN( const char *,(lua_setlocal),(lua_State *L, const lua_Debug *ar, int n) );
-NV_LUA_FUN( const char *,(lua_getupvalue),(lua_State *L, int funcindex, int n) );
-NV_LUA_FUN( const char *,(lua_setupvalue),(lua_State *L, int funcindex, int n) );
-NV_LUA_FUN( void *,(lua_upvalueid),(lua_State *L, int fidx, int n) );
-NV_LUA_FUN( void  ,(lua_upvaluejoin),(lua_State *L, int fidx1, int n1, int fidx2, int n2) );
-NV_LUA_FUN( int,(lua_sethook),(lua_State *L, lua_Hook func, int mask, int count) );
-NV_LUA_FUN( lua_Hook,(lua_gethook),(lua_State *L) );
-NV_LUA_FUN( int,(lua_gethookmask),(lua_State *L) );
-NV_LUA_FUN( int,(lua_gethookcount),(lua_State *L) );
-
-/* lualib API */
-NV_LUA_FUN( int,(luaopen_base),(lua_State *L) );
-NV_LUA_FUN( int,(luaopen_coroutine),(lua_State *L) );
-NV_LUA_FUN( int,(luaopen_table),(lua_State *L) );
-NV_LUA_FUN( int,(luaopen_io),(lua_State *L) );
-NV_LUA_FUN( int,(luaopen_os),(lua_State *L) );
-NV_LUA_FUN( int,(luaopen_string),(lua_State *L) );
-NV_LUA_FUN( int,(luaopen_bit32),(lua_State *L) );
-NV_LUA_FUN( int,(luaopen_math),(lua_State *L) );
-NV_LUA_FUN( int,(luaopen_debug),(lua_State *L) );
-NV_LUA_FUN( int,(luaopen_package),(lua_State *L) );
-NV_LUA_FUN( int,(luaL_openlibs),(lua_State *L) );
-
-/* lauxlib API */
-NV_LUA_FUN( void  ,(luaL_checkversion_),(lua_State *L, lua_Number ver) );
-NV_LUA_FUN( int ,(luaL_getmetafield),(lua_State *L, int obj, const char *e) );
-NV_LUA_FUN( int ,(luaL_callmeta),(lua_State *L, int obj, const char *e) );
-NV_LUA_FUN( const char *,(luaL_tolstring),(lua_State *L, int idx, size_t *len) );
-NV_LUA_FUN( int ,(luaL_argerror),(lua_State *L, int numarg, const char *extramsg) );
-NV_LUA_FUN( const char *,(luaL_checklstring),(lua_State *L, int numArg, size_t *l) );
-NV_LUA_FUN( const char *,(luaL_optlstring),(lua_State *L, int numArg, const char *def, size_t *l) );
-NV_LUA_FUN( lua_Number ,(luaL_checknumber),(lua_State *L, int numArg) );
-NV_LUA_FUN( lua_Number ,(luaL_optnumber),(lua_State *L, int nArg, lua_Number def) );
-NV_LUA_FUN( lua_Integer ,(luaL_checkinteger),(lua_State *L, int numArg) );
-NV_LUA_FUN( lua_Integer ,(luaL_optinteger),(lua_State *L, int nArg, lua_Integer def) );
-NV_LUA_FUN( lua_Unsigned ,(luaL_checkunsigned),(lua_State *L, int numArg) );
-NV_LUA_FUN( lua_Unsigned ,(luaL_optunsigned),(lua_State *L, int numArg, lua_Unsigned def) );
-NV_LUA_FUN( void ,(luaL_checkstack),(lua_State *L, int sz, const char *msg) );
-NV_LUA_FUN( void ,(luaL_checktype),(lua_State *L, int narg, int t) );
-NV_LUA_FUN( void ,(luaL_checkany),(lua_State *L, int narg) );
-NV_LUA_FUN( int   ,(luaL_newmetatable),(lua_State *L, const char *tname) );
-NV_LUA_FUN( void  ,(luaL_setmetatable),(lua_State *L, const char *tname) );
-NV_LUA_FUN( void *,(luaL_testudata),(lua_State *L, int ud, const char *tname) );
-NV_LUA_FUN( void *,(luaL_checkudata),(lua_State *L, int ud, const char *tname) );
-NV_LUA_FUN( void ,(luaL_where),(lua_State *L, int lvl) );
-NV_LUA_FUN( int ,(luaL_error),(lua_State *L, const char *fmt, ...) );
-NV_LUA_FUN( int ,(luaL_checkoption),(lua_State *L, int narg, const char *def, const char *const lst[]) );
-NV_LUA_FUN( int ,(luaL_fileresult),(lua_State *L, int stat, const char *fname) );
-NV_LUA_FUN( int ,(luaL_execresult),(lua_State *L, int stat) );
-
-NV_LUA_FUN( int ,(luaL_ref),(lua_State *L, int t) );
-NV_LUA_FUN( void ,(luaL_unref),(lua_State *L, int t, int ref) );
-NV_LUA_FUN( int ,(luaL_loadfilex),(lua_State *L, const char *filename, const char *mode) );
-NV_LUA_FUN( int ,(luaL_loadbufferx),(lua_State *L, const char *buff, size_t sz, const char *name, const char *mode) );
-NV_LUA_FUN( int ,(luaL_loadstring),(lua_State *L, const char *s) );
-NV_LUA_FUN( lua_State *,(luaL_newstate), (void) );
-NV_LUA_FUN( int ,(luaL_len),(lua_State *L, int idx) );
-NV_LUA_FUN( const char *,(luaL_gsub),(lua_State *L, const char *s, const char *p, const char *r) );
-NV_LUA_FUN( void ,(luaL_setfuncs),(lua_State *L, const luaL_Reg *l, int nup) );
-NV_LUA_FUN( int ,(luaL_getsubtable),(lua_State *L, int idx, const char *fname) );
-NV_LUA_FUN( void ,(luaL_traceback),(lua_State *L, lua_State *L1, const char *msg, int level) );
-NV_LUA_FUN( void ,(luaL_requiref),(lua_State *L, const char *modname, lua_CFunction openf, int glb) );
+#if NV_LUA_VERSION == NV_LUA_52
+#	define LUA_BITLIBNAME	"bit32"
+#endif
+
+#if defined(NV_LUA_DYNAMIC)
+#	define NV_LUA_FUN( rtype, fname, fparams ) NV_LUA_API rtype (*fname) fparams
+#else
+#	define NV_LUA_FUN( rtype, fname, fparams ) NV_LUA_API rtype fname fparams
+#endif
+
+#if NV_LUA_VERSION == NV_LUA_52
+#	define NV_LUA_FUN_51( rtype, fname, fparams )
+#	define NV_LUA_FUN_52 NV_LUA_FUN
+#else
+#	define NV_LUA_FUN_51 NV_LUA_FUN
+#	define NV_LUA_FUN_52( rtype, fname, fparams ) 
+#endif
+
+#include <nv/lib/detail/lua_functions.inc>
 
 #undef NV_LUA_FUN
+#undef NV_LUA_FUN_51
+#undef NV_LUA_FUN_52
 
 /* Macros */
-#define lua_call(L,n,r)		lua_callk(L, (n), (r), 0, NULL)
-#define lua_pcall(L,n,r,f)	lua_pcallk(L, (n), (r), (f), 0, NULL)
-#define lua_yield(L,n)		lua_yieldk(L, (n), 0, NULL)
-#define lua_tonumber(L,i)	lua_tonumberx(L,i,NULL)
-#define lua_tointeger(L,i)	lua_tointegerx(L,i,NULL)
-#define lua_tounsigned(L,i)	lua_tounsignedx(L,i,NULL)
+#if NV_LUA_VERSION == NV_LUA_52
+#	define lua_yield(L,n)		lua_yieldk(L, (n), 0, NULL)
+#	define lua_call(L,n,r)		lua_callk(L, (n), (r), 0, NULL)
+#	define lua_pcall(L,n,r,f)	lua_pcallk(L, (n), (r), (f), 0, NULL)
+#	define lua_tonumber(L,i)	lua_tonumberx(L,i,NULL)
+#	define lua_tointeger(L,i)	lua_tointegerx(L,i,NULL)
+#	define lua_tounsigned(L,i)	lua_tounsignedx(L,i,NULL)
+#endif
 #define lua_pop(L,n)		lua_settop(L, -(n)-1)
 #define lua_newtable(L)		lua_createtable(L, 0, 0)
@@ -420,12 +345,21 @@
 #define lua_isnoneornil(L, n)	(lua_type(L, (n)) <= 0)
 #define lua_pushliteral(L, s)	lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1)
-#define lua_pushglobaltable(L)  lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS)
+#if NV_LUA_VERSION == NV_LUA_52
+#	define lua_pushglobaltable(L)  lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS)
+#else
+#	define lua_setglobal(L,s)     lua_setfield(L, LUA_GLOBALSINDEX, (s))
+#	define lua_getglobal(L,s)     lua_getfield(L, LUA_GLOBALSINDEX, (s))
+#endif
+
 #define lua_tostring(L,i)	lua_tolstring(L, (i), NULL)
 
 /* Aux lib macros */
-#define luaL_checkversion(L)	luaL_checkversion_(L, LUA_VERSION_NUM)
-#define luaL_loadfile(L,f)	luaL_loadfilex(L,f,NULL)
-#define luaL_newlibtable(L,l)	lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1)
-#define luaL_newlib(L,l)	(luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))
+#if NV_LUA_VERSION == NV_LUA_52
+#	define luaL_checkversion(L)	luaL_checkversion_(L, LUA_VERSION_NUM)
+#	define luaL_loadfile(L,f)	luaL_loadfilex(L,f,NULL)
+#	define luaL_loadbuffer(L,s,sz,n)	luaL_loadbufferx(L,s,sz,n,NULL)
+#	define luaL_newlibtable(L,l)	lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1)
+#	define luaL_newlib(L,l)	(luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))
+#endif
 #define luaL_argcheck(L, cond,numarg,extramsg)	((void)((cond) || luaL_argerror(L, (numarg), (extramsg))))
 #define luaL_checkstring(L,n)	(luaL_checklstring(L, (n), NULL))
@@ -440,8 +374,12 @@
 #define luaL_getmetatable(L,n)	(lua_getfield(L, LUA_REGISTRYINDEX, (n)))
 #define luaL_opt(L,f,n,d)	(lua_isnoneornil(L,(n)) ? (d) : f(L,(n)))
-#define luaL_loadbuffer(L,s,sz,n)	luaL_loadbufferx(L,s,sz,n,NULL)
-#define luaL_addchar(B,c)  ((void)((B)->n < (B)->size || luaL_prepbuffsize((B), 1)), ((B)->b[(B)->n++] = (c)))
-#define luaL_addsize(B,s)	((B)->n += (s))
-#define luaL_prepbuffer(B)	luaL_prepbuffsize(B, LUAL_BUFFERSIZE)
+#if NV_LUA_VERSION == NV_LUA_52
+#	define luaL_prepbuffer(B)	luaL_prepbuffsize(B, LUAL_BUFFERSIZE)
+#	define luaL_addchar(B,c) ((void)((B)->n < (B)->size || luaL_prepbuffsize((B), 1)), ((B)->b[(B)->n++] = (c)))
+#	define luaL_addsize(B,s)	((B)->n += (s))
+#else
+#	define luaL_addchar(B,c) ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), (*(B)->p++ = (char)(c)))
+#	define luaL_addsize(B,n)  ((B)->p += (n))
+#endif
 
 }
Index: /trunk/src/lib/lua.cc
===================================================================
--- /trunk/src/lib/lua.cc	(revision 163)
+++ /trunk/src/lib/lua.cc	(revision 164)
@@ -11,353 +11,51 @@
 #include "nv/library.hh"
 
-/* State manipulation */
-lua_State *        (*lua_newstate) (lua_Alloc f, void *ud) = nullptr;
-void               (*lua_close) (lua_State *L) = nullptr;
-lua_State *        (*lua_newthread) (lua_State *L) = nullptr;
-lua_CFunction      (*lua_atpanic) (lua_State *L, lua_CFunction panicf) = nullptr;
-const lua_Number * (*lua_version) (lua_State *L) = nullptr;
+#if NV_LUA_VERSION == NV_LUA_52
+#	undef luaL_loadfile
+#	undef luaL_loadbuffer
+#	undef luaL_prepbuffer
+#	undef lua_tonumber
+#	undef lua_tointeger
+#	undef lua_tounsigned
+#	undef lua_call
+#	undef lua_pcall
+#	undef lua_yield
+#endif
 
-/* Basic stack manipulation */
-int   (*lua_absindex) (lua_State *L, int idx) = nullptr;
-int   (*lua_gettop) (lua_State *L) = nullptr;
-void  (*lua_settop) (lua_State *L, int idx) = nullptr;
-void  (*lua_pushvalue) (lua_State *L, int idx) = nullptr;
-void  (*lua_remove) (lua_State *L, int idx) = nullptr;
-void  (*lua_insert) (lua_State *L, int idx) = nullptr;
-void  (*lua_replace) (lua_State *L, int idx) = nullptr;
-void  (*lua_copy) (lua_State *L, int fromidx, int toidx) = nullptr;
-int   (*lua_checkstack) (lua_State *L, int sz) = nullptr;
-void  (*lua_xmove) (lua_State *from, lua_State *to, int n) = nullptr;
+#define NV_LUA_FUN( rtype, fname, fparams ) rtype (*fname) fparams = nullptr;
+#if NV_LUA_VERSION == NV_LUA_52
+#	define NV_LUA_FUN_51( rtype, fname, fparams )
+#	define NV_LUA_FUN_52 NV_LUA_FUN
+#else
+#	define NV_LUA_FUN_51 NV_LUA_FUN
+#	define NV_LUA_FUN_52( rtype, fname, fparams )
+#endif
 
-/* Access functions (stack -> C) */
-int             (*lua_isnumber) (lua_State *L, int idx) = nullptr;
-int             (*lua_isstring) (lua_State *L, int idx) = nullptr;
-int             (*lua_iscfunction) (lua_State *L, int idx) = nullptr;
-int             (*lua_isuserdata) (lua_State *L, int idx) = nullptr;
-int             (*lua_type) (lua_State *L, int idx) = nullptr;
-const char*     (*lua_typename) (lua_State *L, int tp) = nullptr;
+#include <nv/lib/detail/lua_functions.inc>
 
-lua_Number      (*lua_tonumberx) (lua_State *L, int idx, int *isnum) = nullptr;
-lua_Integer     (*lua_tointegerx) (lua_State *L, int idx, int *isnum) = nullptr;
-lua_Unsigned    (*lua_tounsignedx) (lua_State *L, int idx, int *isnum) = nullptr;
-int             (*lua_toboolean) (lua_State *L, int idx) = nullptr;
-const char*     (*lua_tolstring) (lua_State *L, int idx, size_t *len) = nullptr;
-size_t          (*lua_rawlen) (lua_State *L, int idx) = nullptr;
-lua_CFunction   (*lua_tocfunction) (lua_State *L, int idx) = nullptr;
-void*           (*lua_touserdata) (lua_State *L, int idx) = nullptr;
-lua_State*      (*lua_tothread) (lua_State *L, int idx) = nullptr;
-const void*     (*lua_topointer) (lua_State *L, int idx) = nullptr;
-
-/* Comparison and arithmetic functions */
-void  (*lua_arith) (lua_State *L, int op) = nullptr;
-int   (*lua_rawequal) (lua_State *L, int idx1, int idx2) = nullptr;
-int   (*lua_compare) (lua_State *L, int idx1, int idx2, int op) = nullptr;
-
-/* Push functions (C -> stack) */
-void         (*lua_pushnil) (lua_State *L) = nullptr;
-void         (*lua_pushnumber) (lua_State *L, lua_Number n) = nullptr;
-void         (*lua_pushinteger) (lua_State *L, lua_Integer n) = nullptr;
-void         (*lua_pushunsigned) (lua_State *L, lua_Unsigned n) = nullptr;
-const char*  (*lua_pushlstring) (lua_State *L, const char *s, size_t l) = nullptr;
-const char*  (*lua_pushstring) (lua_State *L, const char *s) = nullptr;
-const char*  (*lua_pushvfstring) (lua_State *L, const char *fmt,
-                                                      va_list argp) = nullptr;
-const char * (*lua_pushfstring) (lua_State *L, const char *fmt, ...) = nullptr;
-void  (*lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n) = nullptr;
-void  (*lua_pushboolean) (lua_State *L, int b) = nullptr;
-void  (*lua_pushlightuserdata) (lua_State *L, void *p) = nullptr;
-int   (*lua_pushthread) (lua_State *L) = nullptr;
-
-/* Get functions (Lua -> stack) */
-void  (*lua_getglobal) (lua_State *L, const char *var) = nullptr;
-void  (*lua_gettable) (lua_State *L, int idx) = nullptr;
-void  (*lua_getfield) (lua_State *L, int idx, const char *k) = nullptr;
-void  (*lua_rawget) (lua_State *L, int idx) = nullptr;
-void  (*lua_rawgeti) (lua_State *L, int idx, int n) = nullptr;
-void  (*lua_rawgetp) (lua_State *L, int idx, const void *p) = nullptr;
-void  (*lua_createtable) (lua_State *L, int narr, int nrec) = nullptr;
-void* (*lua_newuserdata) (lua_State *L, size_t sz) = nullptr;
-int   (*lua_getmetatable) (lua_State *L, int objindex) = nullptr;
-void  (*lua_getuservalue) (lua_State *L, int idx) = nullptr;
-
-/* Set functions (stack -> Lua) */
-void  (*lua_setglobal) (lua_State *L, const char *var) = nullptr;
-void  (*lua_settable) (lua_State *L, int idx) = nullptr;
-void  (*lua_setfield) (lua_State *L, int idx, const char *k) = nullptr;
-void  (*lua_rawset) (lua_State *L, int idx) = nullptr;
-void  (*lua_rawseti) (lua_State *L, int idx, int n) = nullptr;
-void  (*lua_rawsetp) (lua_State *L, int idx, const void *p) = nullptr;
-int   (*lua_setmetatable) (lua_State *L, int objindex) = nullptr;
-void  (*lua_setuservalue) (lua_State *L, int idx) = nullptr;
-
-/* 'load' and 'call' functions (load and run Lua code) */
-void  (*lua_callk) (lua_State *L, int nargs, int nresults, int ctx, lua_CFunction k) = nullptr;
-int   (*lua_getctx) (lua_State *L, int *ctx) = nullptr;
-int   (*lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc, int ctx, lua_CFunction k) = nullptr;
-int   (*lua_load) (lua_State *L, lua_Reader reader, void *dt, const char *chunkname, const char *mode) = nullptr;
-int   (*lua_dump) (lua_State *L, lua_Writer writer, void *data) = nullptr;
-
-/* Coroutine functions */
-int  (*lua_yieldk) (lua_State *L, int nresults, int ctx, lua_CFunction k) = nullptr;
-int  (*lua_resume) (lua_State *L, lua_State *from, int narg) = nullptr;
-int  (*lua_status) (lua_State *L) = nullptr;
-
-/* Garbage-collection function and options */
-int (*lua_gc) (lua_State *L, int what, int data) = nullptr;
-
-/* Miscellaneous functions */
-int   (*lua_error) (lua_State *L) = nullptr;
-int   (*lua_next) (lua_State *L, int idx) = nullptr;
-void  (*lua_concat) (lua_State *L, int n) = nullptr;
-void  (*lua_len)    (lua_State *L, int idx) = nullptr;
-lua_Alloc (*lua_getallocf) (lua_State *L, void **ud) = nullptr;
-void      (*lua_setallocf) (lua_State *L, lua_Alloc f, void *ud) = nullptr;
-
-/* Debug API */
-int (*lua_getstack) (lua_State *L, int level, lua_Debug *ar) = nullptr;
-int (*lua_getinfo) (lua_State *L, const char *what, lua_Debug *ar) = nullptr;
-const char * (*lua_getlocal) (lua_State *L, const lua_Debug *ar, int n) = nullptr;
-const char * (*lua_setlocal) (lua_State *L, const lua_Debug *ar, int n) = nullptr;
-const char * (*lua_getupvalue) (lua_State *L, int funcindex, int n) = nullptr;
-const char * (*lua_setupvalue) (lua_State *L, int funcindex, int n) = nullptr;
-void * (*lua_upvalueid) (lua_State *L, int fidx, int n) = nullptr;
-void   (*lua_upvaluejoin) (lua_State *L, int fidx1, int n1, int fidx2, int n2) = nullptr;
-int (*lua_sethook) (lua_State *L, lua_Hook func, int mask, int count) = nullptr;
-lua_Hook (*lua_gethook) (lua_State *L) = nullptr;
-int (*lua_gethookmask) (lua_State *L) = nullptr;
-int (*lua_gethookcount) (lua_State *L) = nullptr;
-
-/* lualib API */
-int (*luaopen_base) (lua_State *L) = nullptr;
-int (*luaopen_coroutine) (lua_State *L) = nullptr;
-int (*luaopen_table) (lua_State *L) = nullptr;
-int (*luaopen_io) (lua_State *L) = nullptr;
-int (*luaopen_os) (lua_State *L) = nullptr;
-int (*luaopen_string) (lua_State *L) = nullptr;
-int (*luaopen_bit32) (lua_State *L) = nullptr;
-int (*luaopen_math) (lua_State *L) = nullptr;
-int (*luaopen_debug) (lua_State *L) = nullptr;
-int (*luaopen_package) (lua_State *L) = nullptr;
-int (*luaL_openlibs) (lua_State *L) = nullptr;
-
-/* lauxlib API */
-void   (*luaL_checkversion_) (lua_State *L, lua_Number ver) = nullptr;
-int  (*luaL_getmetafield) (lua_State *L, int obj, const char *e) = nullptr;
-int  (*luaL_callmeta) (lua_State *L, int obj, const char *e) = nullptr;
-const char * (*luaL_tolstring) (lua_State *L, int idx, size_t *len) = nullptr;
-int  (*luaL_argerror) (lua_State *L, int numarg, const char *extramsg) = nullptr;
-const char * (*luaL_checklstring) (lua_State *L, int numArg, size_t *l) = nullptr;
-const char * (*luaL_optlstring) (lua_State *L, int numArg, const char *def, size_t *l) = nullptr;
-lua_Number  (*luaL_checknumber) (lua_State *L, int numArg) = nullptr;
-lua_Number  (*luaL_optnumber) (lua_State *L, int nArg, lua_Number def) = nullptr;
-lua_Integer  (*luaL_checkinteger) (lua_State *L, int numArg) = nullptr;
-lua_Integer  (*luaL_optinteger) (lua_State *L, int nArg, lua_Integer def) = nullptr;
-lua_Unsigned  (*luaL_checkunsigned) (lua_State *L, int numArg) = nullptr;
-lua_Unsigned  (*luaL_optunsigned) (lua_State *L, int numArg, lua_Unsigned def) = nullptr;
-void  (*luaL_checkstack) (lua_State *L, int sz, const char *msg) = nullptr;
-void  (*luaL_checktype) (lua_State *L, int narg, int t) = nullptr;
-void  (*luaL_checkany) (lua_State *L, int narg) = nullptr;
-int    (*luaL_newmetatable) (lua_State *L, const char *tname) = nullptr;
-void   (*luaL_setmetatable) (lua_State *L, const char *tname) = nullptr;
-void * (*luaL_testudata) (lua_State *L, int ud, const char *tname) = nullptr;
-void * (*luaL_checkudata) (lua_State *L, int ud, const char *tname) = nullptr;
-void  (*luaL_where) (lua_State *L, int lvl) = nullptr;
-int  (*luaL_error) (lua_State *L, const char *fmt, ...) = nullptr;
-int  (*luaL_checkoption) (lua_State *L, int narg, const char *def, const char *const lst[]) = nullptr;
-int  (*luaL_fileresult) (lua_State *L, int stat, const char *fname) = nullptr;
-int  (*luaL_execresult) (lua_State *L, int stat) = nullptr;
-
-int  (*luaL_ref) (lua_State *L, int t) = nullptr;
-void  (*luaL_unref) (lua_State *L, int t, int ref) = nullptr;
-int  (*luaL_loadfilex) (lua_State *L, const char *filename, const char *mode) = nullptr;
-int  (*luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode) = nullptr;
-int  (*luaL_loadstring) (lua_State *L, const char *s) = nullptr;
-lua_State * (*luaL_newstate)  (void) = nullptr;
-int  (*luaL_len) (lua_State *L, int idx) = nullptr;
-const char * (*luaL_gsub) (lua_State *L, const char *s, const char *p, const char *r) = nullptr;
-void  (*luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup) = nullptr;
-int  (*luaL_getsubtable) (lua_State *L, int idx, const char *fname) = nullptr;
-void  (*luaL_traceback) (lua_State *L, lua_State *L1, const char *msg, int level) = nullptr;
-void  (*luaL_requiref) (lua_State *L, const char *modname, lua_CFunction openf, int glb) = nullptr;
+#undef NV_LUA_FUN
+#undef NV_LUA_FUN_51
+#undef NV_LUA_FUN_52
 
 bool nv::load_lua_library( const char* path )
 {
-#	define NV_LUA_LOAD( symbol ) *(void **) (&symbol) = lua_library.get(#symbol);
-
 	static nv::library lua_library;
 	if ( lua_library.is_open() ) return true;
 	lua_library.open( path );
 
+#	define NV_LUA_FUN( rtype, fname, fparams ) *(void **) (&fname) = lua_library.get(#fname);
+#	if NV_LUA_VERSION == NV_LUA_52
+#		define NV_LUA_FUN_51( rtype, fname, fparams )
+#		define NV_LUA_FUN_52 NV_LUA_FUN
+#	else
+#		define NV_LUA_FUN_51 NV_LUA_FUN
+#		define NV_LUA_FUN_52( rtype, fname, fparams ) 
+#	endif
 
-/* State manipulation */
-	NV_LUA_LOAD(lua_newstate);
-	NV_LUA_LOAD(lua_close);
-	NV_LUA_LOAD(lua_newthread);
-	NV_LUA_LOAD(lua_atpanic);
-	NV_LUA_LOAD(lua_version);
+#include <nv/lib/detail/lua_functions.inc>
 
-/* Basic stack manipulation */
-	NV_LUA_LOAD(lua_absindex);
-	NV_LUA_LOAD(lua_gettop);
-	NV_LUA_LOAD(lua_settop);
-	NV_LUA_LOAD(lua_pushvalue);
-	NV_LUA_LOAD(lua_remove);
-	NV_LUA_LOAD(lua_insert);
-	NV_LUA_LOAD(lua_replace);
-	NV_LUA_LOAD(lua_copy);
-	NV_LUA_LOAD(lua_checkstack);
-	NV_LUA_LOAD(lua_xmove);
-
-/* Access functions (stack -> C) */
-	NV_LUA_LOAD(lua_isnumber);
-	NV_LUA_LOAD(lua_isstring);
-	NV_LUA_LOAD(lua_iscfunction);
-	NV_LUA_LOAD(lua_isuserdata);
-	NV_LUA_LOAD(lua_type);
-	NV_LUA_LOAD(lua_typename);
-
-	NV_LUA_LOAD(lua_tonumberx);
-	NV_LUA_LOAD(lua_tointegerx);
-	NV_LUA_LOAD(lua_tounsignedx);
-	NV_LUA_LOAD(lua_toboolean);
-	NV_LUA_LOAD(lua_tolstring);
-	NV_LUA_LOAD(lua_rawlen);
-	NV_LUA_LOAD(lua_tocfunction);
-	NV_LUA_LOAD(lua_touserdata);
-	NV_LUA_LOAD(lua_tothread);
-	NV_LUA_LOAD(lua_topointer);
-
-/* Comparison and arithmetic functions */
-	NV_LUA_LOAD(lua_arith);
-	NV_LUA_LOAD(lua_rawequal);
-	NV_LUA_LOAD(lua_compare);
-
-/* Push functions (C -> stack) */
-	NV_LUA_LOAD(lua_pushnil);
-	NV_LUA_LOAD(lua_pushnumber);
-	NV_LUA_LOAD(lua_pushinteger);
-	NV_LUA_LOAD(lua_pushunsigned);
-	NV_LUA_LOAD(lua_pushlstring);
-	NV_LUA_LOAD(lua_pushstring);
-	NV_LUA_LOAD(lua_pushvfstring);
-	NV_LUA_LOAD(lua_pushfstring);
-	NV_LUA_LOAD(lua_pushcclosure);
-	NV_LUA_LOAD(lua_pushboolean);
-	NV_LUA_LOAD(lua_pushlightuserdata);
-	NV_LUA_LOAD(lua_pushthread);
-
-/* Get functions (Lua -> stack) */
-	NV_LUA_LOAD(lua_getglobal);
-	NV_LUA_LOAD(lua_gettable);
-	NV_LUA_LOAD(lua_getfield);
-	NV_LUA_LOAD(lua_rawget);
-	NV_LUA_LOAD(lua_rawgeti);
-	NV_LUA_LOAD(lua_rawgetp);
-	NV_LUA_LOAD(lua_createtable);
-	NV_LUA_LOAD(lua_newuserdata);
-	NV_LUA_LOAD(lua_getmetatable);
-	NV_LUA_LOAD(lua_getuservalue);
-
-/* Set functions (stack -> Lua) */
-	NV_LUA_LOAD(lua_setglobal);
-	NV_LUA_LOAD(lua_settable);
-	NV_LUA_LOAD(lua_setfield);
-	NV_LUA_LOAD(lua_rawset);
-	NV_LUA_LOAD(lua_rawseti);
-	NV_LUA_LOAD(lua_rawsetp);
-	NV_LUA_LOAD(lua_setmetatable);
-	NV_LUA_LOAD(lua_setuservalue);
-
-/* 'load' and 'call' functions (load and run Lua code) */
-	NV_LUA_LOAD(lua_callk);
-	NV_LUA_LOAD(lua_getctx);
-	NV_LUA_LOAD(lua_pcallk);
-	NV_LUA_LOAD(lua_load);
-	NV_LUA_LOAD(lua_dump);
-
-/* Coroutine functions */
-	NV_LUA_LOAD(lua_yieldk);
-	NV_LUA_LOAD(lua_resume);
-	NV_LUA_LOAD(lua_status);
-
-/* Garbage-collection function and options */
-	NV_LUA_LOAD(lua_gc);
-
-/* Miscellaneous functions */
-	NV_LUA_LOAD(lua_error);
-	NV_LUA_LOAD(lua_next);
-	NV_LUA_LOAD(lua_concat);
-	NV_LUA_LOAD(lua_len);
-	NV_LUA_LOAD(lua_getallocf);
-	NV_LUA_LOAD(lua_setallocf);
-
-/* Debug API */
-	NV_LUA_LOAD(lua_getstack);
-	NV_LUA_LOAD(lua_getinfo);
-	NV_LUA_LOAD(lua_getlocal);
-	NV_LUA_LOAD(lua_setlocal);
-	NV_LUA_LOAD(lua_getupvalue);
-	NV_LUA_LOAD(lua_setupvalue);
-	NV_LUA_LOAD(lua_upvalueid);
-	NV_LUA_LOAD(lua_upvaluejoin);
-	NV_LUA_LOAD(lua_sethook);
-	NV_LUA_LOAD(lua_gethook);
-	NV_LUA_LOAD(lua_gethookmask);
-	NV_LUA_LOAD(lua_gethookcount);
-
-/* lualib API */
-	NV_LUA_LOAD(luaopen_base);
-	NV_LUA_LOAD(luaopen_coroutine);
-	NV_LUA_LOAD(luaopen_table);
-	NV_LUA_LOAD(luaopen_io);
-	NV_LUA_LOAD(luaopen_os);
-	NV_LUA_LOAD(luaopen_string);
-	NV_LUA_LOAD(luaopen_bit32);
-	NV_LUA_LOAD(luaopen_math);
-	NV_LUA_LOAD(luaopen_debug);
-	NV_LUA_LOAD(luaopen_package);
-	NV_LUA_LOAD(luaL_openlibs);
-
-/* lauxlib API */
-	NV_LUA_LOAD(luaL_checkversion_);
-	NV_LUA_LOAD(luaL_getmetafield);
-	NV_LUA_LOAD(luaL_callmeta);
-	NV_LUA_LOAD(luaL_tolstring);
-	NV_LUA_LOAD(luaL_argerror);
-	NV_LUA_LOAD(luaL_checklstring);
-	NV_LUA_LOAD(luaL_optlstring);
-	NV_LUA_LOAD(luaL_checknumber);
-	NV_LUA_LOAD(luaL_optnumber);
-	NV_LUA_LOAD(luaL_checkinteger);
-	NV_LUA_LOAD(luaL_optinteger);
-	NV_LUA_LOAD(luaL_checkunsigned);
-	NV_LUA_LOAD(luaL_optunsigned);
-	NV_LUA_LOAD(luaL_checkstack);
-	NV_LUA_LOAD(luaL_checktype);
-	NV_LUA_LOAD(luaL_checkany);
-	NV_LUA_LOAD(luaL_newmetatable);
-	NV_LUA_LOAD(luaL_setmetatable);
-	NV_LUA_LOAD(luaL_testudata);
-	NV_LUA_LOAD(luaL_checkudata);
-	NV_LUA_LOAD(luaL_where);
-	NV_LUA_LOAD(luaL_error);
-	NV_LUA_LOAD(luaL_checkoption);
-	NV_LUA_LOAD(luaL_fileresult);
-	NV_LUA_LOAD(luaL_execresult);
-
-	NV_LUA_LOAD(luaL_ref);
-	NV_LUA_LOAD(luaL_unref);
-	NV_LUA_LOAD(luaL_loadfilex);
-	NV_LUA_LOAD(luaL_loadbufferx);
-	NV_LUA_LOAD(luaL_loadstring);
-	NV_LUA_LOAD(luaL_newstate);
-	NV_LUA_LOAD(luaL_len);
-	NV_LUA_LOAD(luaL_gsub);
-	NV_LUA_LOAD(luaL_setfuncs);
-	NV_LUA_LOAD(luaL_getsubtable);
-	NV_LUA_LOAD(luaL_traceback);
-	NV_LUA_LOAD(luaL_requiref);
-
-#	undef NV_LUA_LOAD
+#undef NV_LUA_FUN
+#undef NV_LUA_FUN_51
+#undef NV_LUA_FUN_52
 	return true;
 }
