Changeset 85 for trunk/src/lua/lua_aux.cc
- Timestamp:
- 06/02/13 18:25:29 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lua/lua_aux.cc
r56 r85 7 7 #include "nv/lua/lua_aux.hh" 8 8 9 static int nlua_table_copy( lua_State* L ) 9 #include "nv/lua/lua_raw.hh" 10 11 static int nluaaux_table_copy( lua_State* L ) 10 12 { 11 13 luaL_checktype( L, 1, LUA_TTABLE ); 12 14 lua_settop( L, 1 ); 13 lua_newtable(L); 14 lua_pushnil(L); 15 while ( lua_next( L, 1 ) != 0 ) 16 { 17 lua_pushvalue(L, -2); 18 lua_insert(L, -2); 19 lua_settable(L, -4); 20 } 15 nlua_shallowcopy( L, 1 ); 21 16 return 1; 22 17 } 23 18 24 static int nlua _table_icopy( lua_State* L )19 static int nluaaux_table_deepcopy( lua_State* L ) 25 20 { 26 21 luaL_checktype( L, 1, LUA_TTABLE ); 27 22 lua_settop( L, 1 ); 28 lua_newtable(L); 29 int i = 0; 30 for(;;) 31 { 32 i++; 33 lua_rawgeti(L, 1, i); 34 if ( lua_isnil( L, -1 ) ) 35 { 36 lua_pop( L, 1 ); 37 break; 38 } 39 lua_rawseti(L, 2, i); 40 }; 23 nlua_deepcopy( L, 1 ); 24 return 0; 25 } 26 27 static int nluaaux_table_icopy( lua_State* L ) 28 { 29 luaL_checktype( L, 1, LUA_TTABLE ); 30 lua_settop( L, 1 ); 31 nlua_shallowicopy( L, 1 ); 41 32 return 1; 42 33 } 43 34 44 static int nlua _table_merge( lua_State* L )35 static int nluaaux_table_merge( lua_State* L ) 45 36 { 46 37 luaL_checktype( L, 1, LUA_TTABLE ); 47 38 luaL_checktype( L, 2, LUA_TTABLE ); 48 39 lua_settop( L, 2 ); 49 lua_pushnil(L); 50 while ( lua_next( L, 2 ) != 0 ) 51 { 52 lua_pushvalue(L, -2); 53 lua_insert(L, -2); 54 lua_settable(L, 1); 55 } 40 nlua_shallowmerge( L, 1 ); 56 41 return 0; 57 42 } 58 43 59 static int nlua_table_imerge( lua_State* L ) 60 { 61 luaL_checktype( L, 1, LUA_TTABLE ); 62 luaL_checktype( L, 2, LUA_TTABLE ); 63 lua_settop( L, 2 ); 64 int i = 0; 65 for(;;) 66 { 67 i++; 68 lua_rawgeti(L, 2, i); 69 if ( lua_isnil( L, -1 ) ) 70 { 71 lua_pop( L, 1 ); 72 break; 73 } 74 lua_rawseti(L, 1, i); 75 }; 76 return 0; 77 } 78 79 static int nlua_table_reversed( lua_State* L ) 44 static int nluaaux_table_reversed( lua_State* L ) 80 45 { 81 46 luaL_checktype( L, 1, LUA_TTABLE ); 82 47 lua_settop( L, 1 ); 83 int len = lua_rawlen(L,1); 84 int i = len; 85 lua_createtable(L,len,0); 86 while ( i != 0 ) 87 { 88 lua_rawgeti(L, 1, i); 89 lua_rawseti(L, 2, len-i+1); 90 i--; 91 } 48 nlua_pushreversed( L, 1 ); 92 49 return 1; 93 50 } 94 51 95 static int nlua _table_toset( lua_State* L )52 static int nluaaux_table_toset( lua_State* L ) 96 53 { 97 54 luaL_checktype( L, 1, LUA_TTABLE ); 98 55 lua_settop( L, 1 ); 99 lua_newtable(L); 100 int i = 0; 101 for(;;) 102 { 103 i++; 104 lua_rawgeti(L, 1, i); 105 if ( lua_isnil( L, -1 ) ) 106 { 107 lua_pop( L, 1 ); 108 break; 109 } 110 lua_pushboolean( L, true ); 111 lua_rawset(L, 2); 112 }; 56 nlua_toset( L, 1 ); 113 57 return 1; 114 58 } 115 59 116 static const struct luaL_Reg nlua_table_aux_f [] = { 117 { "copy", nlua_table_copy }, 118 { "icopy", nlua_table_icopy }, 119 { "merge", nlua_table_merge }, 120 { "imerge", nlua_table_imerge }, 121 { "reversed", nlua_table_reversed }, 122 { "toset", nlua_table_toset }, 60 static int nluaaux_table_tokeyset( lua_State* L ) 61 { 62 luaL_checktype( L, 1, LUA_TTABLE ); 63 lua_settop( L, 1 ); 64 nlua_tokeyset( L, 1 ); 65 return 1; 66 } 67 68 static const struct luaL_Reg nluaaux_table_aux_f [] = { 69 { "copy", nluaaux_table_copy }, 70 { "deepcopy", nluaaux_table_deepcopy }, 71 { "icopy", nluaaux_table_icopy }, 72 { "merge", nluaaux_table_merge }, 73 { "reversed", nluaaux_table_reversed }, 74 { "toset", nluaaux_table_toset }, 75 { "tokeyset", nluaaux_table_tokeyset }, 123 76 { NULL, NULL } 124 77 }; 125 78 126 static const struct luaL_Reg nlua_math_aux_f [] = { 79 static int nluaaux_math_clamp( lua_State* L ) 80 { 81 double v = luaL_checknumber( L, 1 ); 82 double min = luaL_optnumber( L, 2, 0 ); 83 double max = luaL_optnumber( L, 3, 1 ); 84 if ( min > max ) luaL_argerror( L, 2, "min is larger than max!" ); 85 lua_pushnumber( L, v < min ? 2 : ( v > max ? 3 : 1 ) ); 86 return 1; 87 } 88 89 90 static const struct luaL_Reg nluaaux_math_aux_f [] = { 91 { "clamp", nluaaux_math_clamp }, 127 92 { NULL, NULL } 128 93 }; … … 130 95 void nlua_register_aux( lua_State* L ) 131 96 { 132 lua_getglobal( L, "table" ); 133 luaL_setfuncs( L, nlua_table_aux_f, 0 ); 134 lua_pop( L, 1 ); 135 136 lua_getglobal( L, "math" ); 137 luaL_setfuncs( L, nlua_math_aux_f, 0 ); 138 lua_pop( L, 1 ); 97 nlua_register( L, "table", nluaaux_table_aux_f ); 98 nlua_register( L, "math", nluaaux_math_aux_f ); 139 99 } 140 100
Note: See TracChangeset
for help on using the changeset viewer.