Index: /trunk/nv/lua/lua_raw.hh
===================================================================
--- /trunk/nv/lua/lua_raw.hh	(revision 84)
+++ /trunk/nv/lua/lua_raw.hh	(revision 85)
@@ -15,6 +15,16 @@
 
 std::string nlua_typecontent( lua_State* L, int idx );
+void nlua_shallowmerge( lua_State *L, int index );
 void nlua_shallowcopy( lua_State *L, int index );
+void nlua_shallowicopy( lua_State *L, int index );
 void nlua_deepcopy( lua_State *L, int index );
+void nlua_pushreversed( lua_State *L, int index );
+void nlua_toset( lua_State *L, int index );
+void nlua_tokeyset( lua_State *L, int index );
+
+void nlua_register( lua_State *L, const char* fname, lua_CFunction func, int index );
+void nlua_register( lua_State *L, const luaL_Reg *l, int index );
+void nlua_register( lua_State *L, const char* lname, const char* fname, lua_CFunction func );
+void nlua_register( lua_State *L, const char* lname, const luaL_Reg *l );
 
 #endif // NV_LUA_RAW_HH
Index: /trunk/src/lua/lua_aux.cc
===================================================================
--- /trunk/src/lua/lua_aux.cc	(revision 84)
+++ /trunk/src/lua/lua_aux.cc	(revision 85)
@@ -7,122 +7,87 @@
 #include "nv/lua/lua_aux.hh"
 
-static int nlua_table_copy( lua_State* L )
+#include "nv/lua/lua_raw.hh"
+
+static int nluaaux_table_copy( lua_State* L )
 {
 	luaL_checktype( L, 1, LUA_TTABLE );
 	lua_settop( L, 1 );
-	lua_newtable(L);
-	lua_pushnil(L);
-	while ( lua_next( L, 1 ) != 0 )
-	{
-		lua_pushvalue(L, -2);
-		lua_insert(L, -2);
-		lua_settable(L, -4);
-	}
+	nlua_shallowcopy( L, 1 );
 	return 1;
 }
 
-static int nlua_table_icopy( lua_State* L )
+static int nluaaux_table_deepcopy( lua_State* L )
 {
 	luaL_checktype( L, 1, LUA_TTABLE );
 	lua_settop( L, 1 );
-	lua_newtable(L);
-	int i = 0;
-	for(;;) 
-	{
-		i++;
-		lua_rawgeti(L, 1, i);
-		if ( lua_isnil( L, -1 ) ) 
-		{
-			lua_pop( L, 1 );
-			break;
-		}
-		lua_rawseti(L, 2, i);
-	};
+	nlua_deepcopy( L, 1 );
+	return 0;
+}
+
+static int nluaaux_table_icopy( lua_State* L )
+{
+	luaL_checktype( L, 1, LUA_TTABLE );
+	lua_settop( L, 1 );
+	nlua_shallowicopy( L, 1 );
 	return 1;
 }
 
-static int nlua_table_merge( lua_State* L )
+static int nluaaux_table_merge( lua_State* L )
 {
 	luaL_checktype( L, 1, LUA_TTABLE );
 	luaL_checktype( L, 2, LUA_TTABLE );
 	lua_settop( L, 2 );
-	lua_pushnil(L);
-	while ( lua_next( L, 2 ) != 0 )
-	{
-		lua_pushvalue(L, -2);
-		lua_insert(L, -2);
-		lua_settable(L, 1);
-	}
+	nlua_shallowmerge( L, 1 );
 	return 0;
 }
 
-static int nlua_table_imerge( lua_State* L )
-{
-	luaL_checktype( L, 1, LUA_TTABLE );
-	luaL_checktype( L, 2, LUA_TTABLE );
-	lua_settop( L, 2 );
-	int i = 0;
-	for(;;) 
-	{
-		i++;
-		lua_rawgeti(L, 2, i);
-		if ( lua_isnil( L, -1 ) ) 
-		{
-			lua_pop( L, 1 );
-			break;
-		}
-		lua_rawseti(L, 1, i);
-	};
-	return 0;
-}
-
-static int nlua_table_reversed( lua_State* L )
+static int nluaaux_table_reversed( lua_State* L )
 {
 	luaL_checktype( L, 1, LUA_TTABLE );
 	lua_settop( L, 1 );
-	int len = lua_rawlen(L,1);
-	int i   = len;
-	lua_createtable(L,len,0);
-	while ( i != 0 )
-	{
-		lua_rawgeti(L, 1, i);
-		lua_rawseti(L, 2, len-i+1);
-		i--;
-	}
+	nlua_pushreversed( L, 1 );
 	return 1;
 }
 
-static int nlua_table_toset( lua_State* L )
+static int nluaaux_table_toset( lua_State* L )
 {
 	luaL_checktype( L, 1, LUA_TTABLE );
 	lua_settop( L, 1 );
-	lua_newtable(L);
-	int i = 0;
-	for(;;) 
-	{
-		i++;
-		lua_rawgeti(L, 1, i);
-		if ( lua_isnil( L, -1 ) ) 
-		{
-			lua_pop( L, 1 );
-			break;
-		}
-		lua_pushboolean( L, true );
-		lua_rawset(L, 2);
-	};
+	nlua_toset( L, 1 );
 	return 1;
 }
 
-static const struct luaL_Reg nlua_table_aux_f [] = {
-	{ "copy",      nlua_table_copy },
-	{ "icopy",     nlua_table_icopy },
-	{ "merge",     nlua_table_merge },
-	{ "imerge",    nlua_table_imerge },
-	{ "reversed",  nlua_table_reversed },
-	{ "toset",     nlua_table_toset },
+static int nluaaux_table_tokeyset( lua_State* L )
+{
+	luaL_checktype( L, 1, LUA_TTABLE );
+	lua_settop( L, 1 );
+	nlua_tokeyset( L, 1 );
+	return 1;
+}
+
+static const struct luaL_Reg nluaaux_table_aux_f [] = {
+	{ "copy",      nluaaux_table_copy },
+	{ "deepcopy",  nluaaux_table_deepcopy },
+	{ "icopy",     nluaaux_table_icopy },
+	{ "merge",     nluaaux_table_merge },
+	{ "reversed",  nluaaux_table_reversed },
+	{ "toset",     nluaaux_table_toset },
+	{ "tokeyset",  nluaaux_table_tokeyset },
 	{ NULL, NULL }
 };
 
-static const struct luaL_Reg nlua_math_aux_f [] = {
+static int nluaaux_math_clamp( lua_State* L )
+{
+	double v   = luaL_checknumber( L,  1 );
+	double min = luaL_optnumber( L, 2, 0 );
+	double max = luaL_optnumber( L, 3, 1 );  
+	if ( min > max ) luaL_argerror( L, 2, "min is larger than max!" );
+	lua_pushnumber( L, v < min ? 2 : ( v > max ? 3 : 1 ) );
+	return 1;
+}
+
+
+static const struct luaL_Reg nluaaux_math_aux_f [] = {
+	{ "clamp",     nluaaux_math_clamp },
 	{ NULL, NULL }
 };
@@ -130,11 +95,6 @@
 void nlua_register_aux( lua_State* L )
 {
-	lua_getglobal( L, "table" );
-	luaL_setfuncs( L, nlua_table_aux_f, 0 );
-	lua_pop( L, 1 );
-
-	lua_getglobal( L, "math" );
-	luaL_setfuncs( L, nlua_math_aux_f, 0 );
-	lua_pop( L, 1 );
+	nlua_register( L, "table", nluaaux_table_aux_f );
+	nlua_register( L, "math", nluaaux_math_aux_f );
 }
 
Index: /trunk/src/lua/lua_glm.cc
===================================================================
--- /trunk/src/lua/lua_glm.cc	(revision 84)
+++ /trunk/src/lua/lua_glm.cc	(revision 85)
@@ -6,4 +6,6 @@
 
 #include "nv/lua/lua_glm.hh"
+
+#include "nv/lua/lua_raw.hh"
 #include "nv/string.hh"
 
@@ -203,5 +205,5 @@
 	size_t idx = 255;
 
-	if( len == 1 )
+	if ( len == 1 )
 	{
 		idx = nlua_swizzel_lookup[ key[ 0 ] ];
@@ -222,6 +224,5 @@
 	}
 
-	lua_getmetatable( L, 1 );
-	lua_getfield( L, -1, "__base" );
+	luaL_getmetafield( L, 1, "__base" );
 	lua_pushvalue( L, 2 );
 	lua_rawget( L, -2 );
@@ -305,11 +306,12 @@
 
 	luaL_newmetatable( L, nlua_metatable_name<T>() );
-	luaL_setfuncs( L, nlua_vec_m, 0 );
-	luaL_newlib(L, nlua_vec_f);
+	nlua_register( L, nlua_vec_m, -1 );
+	lua_createtable( L, 0, 0 );
+	nlua_register( L, nlua_vec_f, -1 );
 	lua_pushvalue(L, -1);
 	lua_setfield(L, -3, "__base" );
 	lua_replace(L, -2);
-    lua_newtable( L );
-	luaL_setfuncs( L, nlua_vec_fm, 0 );
+	lua_createtable( L, 0, 0 );
+	nlua_register( L, nlua_vec_fm, -1 );
 	lua_setmetatable( L, -2 );
 	return 1;
Index: /trunk/src/lua/lua_raw.cc
===================================================================
--- /trunk/src/lua/lua_raw.cc	(revision 84)
+++ /trunk/src/lua/lua_raw.cc	(revision 85)
@@ -27,28 +27,61 @@
 }
 
+void nlua_pushreversed( lua_State *L, int index )
+{
+	index = lua_absindex( L, index );
+	int len = lua_rawlen( L, index );
+	int i   = len;
+	lua_createtable( L, len, 0 );
+	while ( i != 0 )
+	{
+		lua_rawgeti( L, index, i );
+		lua_rawseti( L, -2, len-i+1 );
+		i--;
+	}
+}
+
+
 void nlua_shallowcopy( lua_State *L, int index )
 {
-	index = lua_absindex(L,index);
-	lua_newtable(L);
-	lua_pushnil(L);
+	index = lua_absindex( L, index );
+	lua_createtable( L, 0, 0 );
+	lua_pushnil( L );
 
 	while ( lua_next( L, index ) != 0 )
 	{
-		lua_pushvalue(L, -2);
-		lua_insert(L, -2);
-		lua_settable(L, -4);
+		lua_pushvalue( L, -2 );
+		lua_insert( L, -2 );
+		lua_settable( L, -4 );
 	}
+}
+
+void nlua_shallowicopy( lua_State *L, int index )
+{
+	index = lua_absindex( L, index );
+	lua_createtable( L, 0, 0 );
+	int i = 0;
+	for(;;) 
+	{
+		i++;
+		lua_rawgeti( L, 1, i );
+		if ( lua_isnil( L, -1 ) ) 
+		{
+			lua_pop( L, 1 );
+			break;
+		}
+		lua_rawseti( L, 2, i );
+	};
 }
 
 void nlua_shallowmerge( lua_State *L, int index )
 {
-	index = lua_absindex(L,index);
-	lua_pushnil(L);
+	index = lua_absindex( L, index );
+	lua_pushnil( L );
 
-	while( lua_next(L, index) != 0 )
+	while( lua_next( L, index ) != 0 )
 	{
-		lua_pushvalue(L, -2);
-		lua_insert(L, -2);
-		lua_rawset(L, -4);
+		lua_pushvalue( L, -2 );
+		lua_insert( L, -2 );
+		lua_rawset( L, -4 );
 	}
 }
@@ -56,7 +89,7 @@
 void nlua_deepcopy( lua_State *L, int index )
 {
-	index = lua_absindex(L,index);
-	lua_newtable(L);
-	lua_pushnil(L);
+	index = lua_absindex( L, index );
+	lua_createtable( L, 0, 0 );
+	lua_pushnil( L );
 
 	while ( lua_next( L, index ) != 0 )
@@ -68,8 +101,89 @@
 			lua_pop( L, 1 );
 		}
-		lua_pushvalue(L, -2);
-		lua_insert(L, -2);
-		lua_settable(L, -4);
+		lua_pushvalue( L, -2 );
+		lua_insert( L, -2 );
+		lua_settable( L, -4 );
 	}
 }
 
+void nlua_toset( lua_State *L, int index )
+{
+	index = lua_absindex( L, index );
+	lua_createtable( L, 0, 0 );
+	int i = 0;
+	for(;;) 
+	{
+		i++;
+		lua_rawgeti( L, index, i );
+		if ( lua_isnil( L, -1 ) ) 
+		{
+			lua_pop( L, 1 );
+			break;
+		}
+		lua_pushboolean( L, true );
+		lua_rawset( L, -3 );
+	};
+}
+
+void nlua_tokeyset( lua_State *L, int index )
+{
+	index = lua_absindex( L, index );
+	lua_createtable( L, 0, 0 );
+	lua_pushnil( L );
+	while ( lua_next( L, index ) != 0 )
+	{
+		lua_pushvalue( L, -2 );
+		lua_pushboolean( L, true );
+		lua_settable( L, -5 );
+		lua_pop( L, 1 );
+	}
+}
+
+void nlua_register( lua_State *L, const char* fname, lua_CFunction func, int index )
+{
+	index = lua_absindex( L, index );
+	lua_pushstring( L, fname );
+	lua_pushcfunction( L, func );
+	lua_rawset( L, index );
+}
+
+void nlua_register( lua_State *L, const luaL_Reg *l, int index )
+{
+	index = lua_absindex( L, index );
+	for (; l->name != NULL; l++) 
+	{
+		lua_pushstring( L, l->name );
+		lua_pushcfunction( L, l->func );
+		lua_rawset( L, index );
+	}
+}
+
+void nlua_register( lua_State *L, const char* lname, const char* fname, lua_CFunction func )
+{
+	lua_getglobal( L, lname );
+	if ( !lua_istable( L, -1 ) )
+	{
+		lua_pop( L, 1 );
+		lua_createtable( L, 0, 0 );
+		nlua_register( L, fname, func, -1 );
+		lua_setglobal( L, lname );
+		return;
+	}
+	nlua_register( L, fname, func, -1 );
+	lua_pop( L, 1 );
+}
+
+void nlua_register( lua_State *L, const char* lname, const luaL_Reg *l )
+{
+	lua_getglobal( L, lname );
+	if ( !lua_istable( L, -1 ) )
+	{
+		lua_pop( L, 1 );
+		lua_createtable( L, 0, 0 );
+		nlua_register( L, l, -1 );
+		lua_setglobal( L, lname );
+		return;
+	}
+	nlua_register( L, l, -1 );
+	lua_pop( L, 1 );
+}
