Index: trunk/src/lua/lua_aux.cc
===================================================================
--- trunk/src/lua/lua_aux.cc	(revision 56)
+++ 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 );
 }
 
