Index: trunk/src/lib/lua.cc
===================================================================
--- trunk/src/lib/lua.cc	(revision 406)
+++ trunk/src/lib/lua.cc	(revision 489)
@@ -6,4 +6,28 @@
 
 #include "nv/lib/lua.hh"
+
+#if NV_LUA_VERSION == NV_LUA_5C
+size_t( *lua_rawlen )        ( lua_State *L, int idx ) = nullptr;
+int( *lua_absindex )      ( lua_State *L, int idx ) = nullptr;
+void( *lua_getglobal )     ( lua_State *L, const char *var ) = nullptr;
+void( *lua_setglobal )     ( lua_State *L, const char *var ) = nullptr;
+void( *luaL_requiref )     ( lua_State *L, const char *modname, lua_CFunction openf, int glb ) = nullptr;
+void( *luaL_setmetatable ) ( lua_State *L, const char *tname ) = nullptr;
+void*  ( *luaL_testudata )    ( lua_State *L, int ud, const char *tname ) = nullptr;
+void( *lua_copy )          ( lua_State *L, int fromidx, int toidx ) = nullptr;
+int( *lua_compare )       ( lua_State *L, int idx1, int idx2, int op ) = nullptr;
+void( *lua_rawgetp )       ( lua_State *L, int idx, const void *p ) = nullptr;
+void( *lua_rawsetp )       ( lua_State *L, int idx, const void *p ) = nullptr;
+void( *lua_pushglobaltable )( lua_State* L ) = 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;
+
+const lua_Number* ( *lua_version ) ( lua_State *L ) = nullptr;
+
+// only loaded in 5.1 mode to implement lua_compare
+static int( *lua_equal )        ( lua_State *L, int idx1, int idx2 ) = nullptr;
+static int( *lua_lessthan )     ( lua_State *L, int idx1, int idx2 ) = nullptr;
+#endif
+
 
 #if defined( NV_LUA_DYNAMIC )
@@ -45,28 +69,4 @@
 #	include <nv/lib/detail/lua_functions_compat.inc>
 #	undef NV_LUA_COMPAT_FUN
-int LUA_UPVALUEINDEX  = 0;
-int LUA_REGISTRYINDEX = 0;
-int LUA_VERSION_NUM   = 0;
-size_t (*lua_rawlen)        (lua_State *L, int idx) = nullptr;
-int    (*lua_absindex)      (lua_State *L, int idx) = nullptr;
-void   (*lua_getglobal)     (lua_State *L, const char *var) = nullptr;
-void   (*lua_setglobal)     (lua_State *L, const char *var) = nullptr;
-void   (*luaL_requiref)     (lua_State *L, const char *modname, lua_CFunction openf, int glb) = nullptr;
-void   (*luaL_setmetatable) (lua_State *L, const char *tname) = nullptr;
-void*  (*luaL_testudata)    (lua_State *L, int ud, const char *tname) = nullptr;
-void   (*lua_copy)          (lua_State *L, int fromidx, int toidx) = nullptr;
-int    (*lua_compare)       (lua_State *L, int idx1, int idx2, int op) = nullptr;
-void   (*lua_rawgetp)       (lua_State *L, int idx, const void *p) = nullptr;
-void   (*lua_rawsetp)       (lua_State *L, int idx, const void *p) = nullptr;
-void   (*lua_pushglobaltable)(lua_State* L) = 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;
-
-const lua_Number* (*lua_version) (lua_State *L) = nullptr;
-
-// only loaded in 5.1 mode to implement lua_compare
-static int    (*lua_equal)        (lua_State *L, int idx1, int idx2) = nullptr;
-static int    (*lua_lessthan)     (lua_State *L, int idx1, int idx2) = nullptr;
-
 
 #	define NV_LUA_COMPAT_FUN( u1,u2,u3,rt2,fn2,fp2,u4,u5 ) static rt2 (*fn2##_compat) fp2 = nullptr;
@@ -79,4 +79,12 @@
 #	undef NV_LUA_COMPAT_FUN
 
+#endif
+
+#endif
+
+#if NV_LUA_VERSION == NV_LUA_5C
+int LUA_UPVALUEINDEX = 0;
+int LUA_REGISTRYINDEX = 0;
+int LUA_VERSION_NUM = 0;
 #	define LUAI_MAXSTACK_52		    1000000
 #	define LUAI_FIRSTPSEUDOIDX_52	(-LUAI_MAXSTACK_52 - 1000)
@@ -86,38 +94,50 @@
 #	define LUA_REGISTRYINDEX_51     (-10000)
 
-static int lua_absindex_51 (lua_State *L, int idx) { return (idx > 0 ? idx : idx + lua_gettop(L) + 1); };
-static void lua_getglobal_51 (lua_State *L, const char *var) { lua_getfield(L, LUA_GLOBALSINDEX_51, var ); };
-static void lua_setglobal_51 (lua_State *L, const char *var) { lua_setfield(L, LUA_GLOBALSINDEX_51, var ); };
-static void luaL_requiref_51 (lua_State *L, const char *modname, lua_CFunction openf, int glb)
-{
-	lua_pushcfunction(L, openf);
-	lua_pushstring(L, modname);
-	lua_call(L, 1, 1);
-	if (glb != 0)
+static int lua_absindex_51( lua_State *L, int idx )
+{
+	return ( idx > 0 ? idx : idx + lua_gettop( L ) + 1 );
+};
+
+static void lua_getglobal_51( lua_State *L, const char *var )
+{
+	lua_getfield( L, LUA_GLOBALSINDEX_51, var );
+};
+
+static void lua_setglobal_51( lua_State *L, const char *var )
+{
+	lua_setfield( L, LUA_GLOBALSINDEX_51, var );
+};
+
+static void luaL_requiref_51( lua_State *L, const char *modname, lua_CFunction openf, int glb )
+{
+	lua_pushcfunction( L, openf );
+	lua_pushstring( L, modname );
+	lua_call( L, 1, 1 );
+	if ( glb != 0 )
 	{
 		lua_pushvalue( L, LUA_GLOBALSINDEX_51 );
-		lua_pushvalue(L, -2); 
-		lua_setfield(L, -2, modname); 
-		lua_pop(L, 1); 
-	}
-}
-
-static void luaL_setmetatable_51 (lua_State *L, const char *tname)
-{
-	luaL_getmetatable(L, tname);
-	lua_setmetatable(L, -2);
-}
-
-static void *luaL_testudata_51 (lua_State *L, int ud, const char *tname)
-{
-	void *p = lua_touserdata(L, ud);
-	if (p != NULL) 
-	{  
-		if (lua_getmetatable(L, ud)) 
+		lua_pushvalue( L, -2 );
+		lua_setfield( L, -2, modname );
+		lua_pop( L, 1 );
+	}
+}
+
+static void luaL_setmetatable_51( lua_State *L, const char *tname )
+{
+	luaL_getmetatable( L, tname );
+	lua_setmetatable( L, -2 );
+}
+
+static void *luaL_testudata_51( lua_State *L, int ud, const char *tname )
+{
+	void *p = lua_touserdata( L, ud );
+	if ( p != NULL )
+	{
+		if ( lua_getmetatable( L, ud ) )
 		{
-			luaL_getmetatable(L, tname); 
-			if ( !lua_rawequal(L, -1, -2))
-				p = NULL;  
-			lua_pop(L, 2); 
+			luaL_getmetatable( L, tname );
+			if ( !lua_rawequal( L, -1, -2 ) )
+				p = NULL;
+			lua_pop( L, 2 );
 			return p;
 		}
@@ -126,5 +146,5 @@
 }
 
-static const lua_Number *lua_version_51 (lua_State*) 
+static const lua_Number *lua_version_51( lua_State* )
 {
 	static const lua_Number version = lua_Number( LUA_VERSION_NUM );
@@ -132,5 +152,5 @@
 }
 
-static void lua_copy_51 (lua_State *L, int fromidx, int toidx)
+static void lua_copy_51( lua_State *L, int fromidx, int toidx )
 {
 	toidx = lua_absindex( L, toidx );
@@ -139,11 +159,11 @@
 }
 
-static int lua_compare_51(lua_State *L, int idx1, int idx2, int op)
-{
-	switch (op)
-	{
-	case LUA_OPEQ : return lua_equal( L, idx1, idx2 );
-	case LUA_OPLT : return lua_lessthan( L, idx1, idx2 );
-	case LUA_OPLE : return lua_lessthan( L, idx1, idx2 ) || lua_equal( L, idx1, idx2 );
+static int lua_compare_51( lua_State *L, int idx1, int idx2, int op )
+{
+	switch ( op )
+	{
+	case LUA_OPEQ: return lua_equal( L, idx1, idx2 );
+	case LUA_OPLT: return lua_lessthan( L, idx1, idx2 );
+	case LUA_OPLE: return lua_lessthan( L, idx1, idx2 ) || lua_equal( L, idx1, idx2 );
 	default:
 		return 0;
@@ -151,16 +171,16 @@
 }
 
-static void lua_rawgetp_51(lua_State *L, int idx, const void *p)
+static void lua_rawgetp_51( lua_State *L, int idx, const void *p )
 {
 	idx = lua_absindex( L, idx );
-	void* pp = const_cast<void *>(p); // EVIL
+	void* pp = const_cast<void *>( p ); // EVIL
 	lua_pushlightuserdata( L, pp );
 	lua_rawget( L, idx );
 }
 
-static void lua_rawsetp_51(lua_State *L, int idx, const void *p)
+static void lua_rawsetp_51( lua_State *L, int idx, const void *p )
 {
 	idx = lua_absindex( L, idx );
-	void* pp = const_cast<void *>(p); // EVIL
+	void* pp = const_cast<void *>( p ); // EVIL
 	lua_pushlightuserdata( L, pp );
 	lua_insert( L, -1 );
@@ -168,44 +188,48 @@
 }
 
-static int luaL_getsubtable_51(lua_State *L, int idx, const char *fname)
-{
-	lua_getfield(L, idx, fname);
-	if ( lua_istable(L, -1) ) return 1;
-	else {
-		idx = lua_absindex(L, idx);
-		lua_pop(L, 1);
-		lua_newtable(L);
-		lua_pushvalue(L, -1);
-		lua_setfield(L, idx, fname); 
-		return 0; 
-	}
-}
-
-static void luaL_setfuncs_51(lua_State *L, const luaL_Reg *l, int nup)
-{
-	luaL_checkstack(L, nup, "too many upvalues");
-	for (; l->name != NULL; l++) 
-	{
-		for (int i = 0; i < nup; i++)
+static int luaL_getsubtable_51( lua_State *L, int idx, const char *fname )
+{
+	lua_getfield( L, idx, fname );
+	if ( lua_istable( L, -1 ) ) return 1;
+	else
+	{
+		idx = lua_absindex( L, idx );
+		lua_pop( L, 1 );
+		lua_newtable( L );
+		lua_pushvalue( L, -1 );
+		lua_setfield( L, idx, fname );
+		return 0;
+	}
+}
+
+static void luaL_setfuncs_51( lua_State *L, const luaL_Reg *l, int nup )
+{
+	luaL_checkstack( L, nup, "too many upvalues" );
+	for ( ; l->name != NULL; l++ )
+	{
+		for ( int i = 0; i < nup; i++ )
 		{
-			lua_pushvalue(L, -nup);
+			lua_pushvalue( L, -nup );
 		}
-		lua_pushcclosure(L, l->func, nup);
-		lua_setfield(L, -(nup + 2), l->name);
-	}
-	lua_pop(L, nup);
-}
-
-static void lua_pushglobaltable_51(lua_State* L)
+		lua_pushcclosure( L, l->func, nup );
+		lua_setfield( L, -( nup + 2 ), l->name );
+	}
+	lua_pop( L, nup );
+}
+
+static void lua_pushglobaltable_51( lua_State* L )
 {
 	lua_pushvalue( L, LUA_GLOBALSINDEX_51 );
 }
 
-static void lua_pushglobaltable_52(lua_State* L)
+static void lua_pushglobaltable_52( lua_State* L )
 {
 	lua_rawgeti( L, LUA_REGISTRYINDEX_52, LUA_GLOBALSINDEX_52 );
 }
 
-#endif
+
+#endif
+
+#if defined( NV_LUA_DYNAMIC )
 
 bool nv::load_lua_library( const char* path )
@@ -311,3 +335,31 @@
 }
 
-#endif
+#else
+
+bool nv::load_lua_library( const char* path )
+{
+	lua_rawlen = lua_objlen;
+	lua_absindex = lua_absindex_51;
+	lua_getglobal = lua_getglobal_51;
+	lua_setglobal = lua_setglobal_51;
+	luaL_setmetatable = luaL_setmetatable_51;
+	luaL_testudata = luaL_testudata_51;
+	lua_version = lua_version_51;
+	lua_copy = lua_copy_51;
+	lua_rawgetp = lua_rawgetp_51;
+	lua_rawsetp = lua_rawsetp_51;
+	lua_pushglobaltable = lua_pushglobaltable_51;
+	luaL_setfuncs = luaL_setfuncs_51;
+	luaL_getsubtable = luaL_getsubtable_51;
+	luaL_requiref = luaL_requiref_51;
+
+	NV_LUA_LOAD( lua_lessthan );
+	NV_LUA_LOAD( lua_equal );
+	lua_compare = lua_compare_51;
+	LUA_UPVALUEINDEX = LUA_GLOBALSINDEX_51;
+	LUA_REGISTRYINDEX = LUA_REGISTRYINDEX_51;
+	LUA_VERSION_NUM = 501;
+	return true;
+}
+
+#endif
