Changeset 85
- Timestamp:
- 06/02/13 18:25:29 (12 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/nv/lua/lua_raw.hh
r51 r85 15 15 16 16 std::string nlua_typecontent( lua_State* L, int idx ); 17 void nlua_shallowmerge( lua_State *L, int index ); 17 18 void nlua_shallowcopy( lua_State *L, int index ); 19 void nlua_shallowicopy( lua_State *L, int index ); 18 20 void nlua_deepcopy( lua_State *L, int index ); 21 void nlua_pushreversed( lua_State *L, int index ); 22 void nlua_toset( lua_State *L, int index ); 23 void nlua_tokeyset( lua_State *L, int index ); 24 25 void nlua_register( lua_State *L, const char* fname, lua_CFunction func, int index ); 26 void nlua_register( lua_State *L, const luaL_Reg *l, int index ); 27 void nlua_register( lua_State *L, const char* lname, const char* fname, lua_CFunction func ); 28 void nlua_register( lua_State *L, const char* lname, const luaL_Reg *l ); 19 29 20 30 #endif // NV_LUA_RAW_HH -
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 -
trunk/src/lua/lua_glm.cc
r75 r85 6 6 7 7 #include "nv/lua/lua_glm.hh" 8 9 #include "nv/lua/lua_raw.hh" 8 10 #include "nv/string.hh" 9 11 … … 203 205 size_t idx = 255; 204 206 205 if ( len == 1 )207 if ( len == 1 ) 206 208 { 207 209 idx = nlua_swizzel_lookup[ key[ 0 ] ]; … … 222 224 } 223 225 224 lua_getmetatable( L, 1 ); 225 lua_getfield( L, -1, "__base" ); 226 luaL_getmetafield( L, 1, "__base" ); 226 227 lua_pushvalue( L, 2 ); 227 228 lua_rawget( L, -2 ); … … 305 306 306 307 luaL_newmetatable( L, nlua_metatable_name<T>() ); 307 luaL_setfuncs( L, nlua_vec_m, 0 ); 308 luaL_newlib(L, nlua_vec_f); 308 nlua_register( L, nlua_vec_m, -1 ); 309 lua_createtable( L, 0, 0 ); 310 nlua_register( L, nlua_vec_f, -1 ); 309 311 lua_pushvalue(L, -1); 310 312 lua_setfield(L, -3, "__base" ); 311 313 lua_replace(L, -2); 312 lua_newtable( L);313 luaL_setfuncs( L, nlua_vec_fm, 0);314 lua_createtable( L, 0, 0 ); 315 nlua_register( L, nlua_vec_fm, -1 ); 314 316 lua_setmetatable( L, -2 ); 315 317 return 1; -
trunk/src/lua/lua_raw.cc
r51 r85 27 27 } 28 28 29 void nlua_pushreversed( lua_State *L, int index ) 30 { 31 index = lua_absindex( L, index ); 32 int len = lua_rawlen( L, index ); 33 int i = len; 34 lua_createtable( L, len, 0 ); 35 while ( i != 0 ) 36 { 37 lua_rawgeti( L, index, i ); 38 lua_rawseti( L, -2, len-i+1 ); 39 i--; 40 } 41 } 42 43 29 44 void nlua_shallowcopy( lua_State *L, int index ) 30 45 { 31 index = lua_absindex( L,index);32 lua_ newtable(L);33 lua_pushnil( L);46 index = lua_absindex( L, index ); 47 lua_createtable( L, 0, 0 ); 48 lua_pushnil( L ); 34 49 35 50 while ( lua_next( L, index ) != 0 ) 36 51 { 37 lua_pushvalue( L, -2);38 lua_insert( L, -2);39 lua_settable( L, -4);52 lua_pushvalue( L, -2 ); 53 lua_insert( L, -2 ); 54 lua_settable( L, -4 ); 40 55 } 56 } 57 58 void nlua_shallowicopy( lua_State *L, int index ) 59 { 60 index = lua_absindex( L, index ); 61 lua_createtable( L, 0, 0 ); 62 int i = 0; 63 for(;;) 64 { 65 i++; 66 lua_rawgeti( L, 1, i ); 67 if ( lua_isnil( L, -1 ) ) 68 { 69 lua_pop( L, 1 ); 70 break; 71 } 72 lua_rawseti( L, 2, i ); 73 }; 41 74 } 42 75 43 76 void nlua_shallowmerge( lua_State *L, int index ) 44 77 { 45 index = lua_absindex( L,index);46 lua_pushnil( L);78 index = lua_absindex( L, index ); 79 lua_pushnil( L ); 47 80 48 while( lua_next( L, index) != 0 )81 while( lua_next( L, index ) != 0 ) 49 82 { 50 lua_pushvalue( L, -2);51 lua_insert( L, -2);52 lua_rawset( L, -4);83 lua_pushvalue( L, -2 ); 84 lua_insert( L, -2 ); 85 lua_rawset( L, -4 ); 53 86 } 54 87 } … … 56 89 void nlua_deepcopy( lua_State *L, int index ) 57 90 { 58 index = lua_absindex( L,index);59 lua_ newtable(L);60 lua_pushnil( L);91 index = lua_absindex( L, index ); 92 lua_createtable( L, 0, 0 ); 93 lua_pushnil( L ); 61 94 62 95 while ( lua_next( L, index ) != 0 ) … … 68 101 lua_pop( L, 1 ); 69 102 } 70 lua_pushvalue( L, -2);71 lua_insert( L, -2);72 lua_settable( L, -4);103 lua_pushvalue( L, -2 ); 104 lua_insert( L, -2 ); 105 lua_settable( L, -4 ); 73 106 } 74 107 } 75 108 109 void nlua_toset( lua_State *L, int index ) 110 { 111 index = lua_absindex( L, index ); 112 lua_createtable( L, 0, 0 ); 113 int i = 0; 114 for(;;) 115 { 116 i++; 117 lua_rawgeti( L, index, i ); 118 if ( lua_isnil( L, -1 ) ) 119 { 120 lua_pop( L, 1 ); 121 break; 122 } 123 lua_pushboolean( L, true ); 124 lua_rawset( L, -3 ); 125 }; 126 } 127 128 void nlua_tokeyset( lua_State *L, int index ) 129 { 130 index = lua_absindex( L, index ); 131 lua_createtable( L, 0, 0 ); 132 lua_pushnil( L ); 133 while ( lua_next( L, index ) != 0 ) 134 { 135 lua_pushvalue( L, -2 ); 136 lua_pushboolean( L, true ); 137 lua_settable( L, -5 ); 138 lua_pop( L, 1 ); 139 } 140 } 141 142 void nlua_register( lua_State *L, const char* fname, lua_CFunction func, int index ) 143 { 144 index = lua_absindex( L, index ); 145 lua_pushstring( L, fname ); 146 lua_pushcfunction( L, func ); 147 lua_rawset( L, index ); 148 } 149 150 void nlua_register( lua_State *L, const luaL_Reg *l, int index ) 151 { 152 index = lua_absindex( L, index ); 153 for (; l->name != NULL; l++) 154 { 155 lua_pushstring( L, l->name ); 156 lua_pushcfunction( L, l->func ); 157 lua_rawset( L, index ); 158 } 159 } 160 161 void nlua_register( lua_State *L, const char* lname, const char* fname, lua_CFunction func ) 162 { 163 lua_getglobal( L, lname ); 164 if ( !lua_istable( L, -1 ) ) 165 { 166 lua_pop( L, 1 ); 167 lua_createtable( L, 0, 0 ); 168 nlua_register( L, fname, func, -1 ); 169 lua_setglobal( L, lname ); 170 return; 171 } 172 nlua_register( L, fname, func, -1 ); 173 lua_pop( L, 1 ); 174 } 175 176 void nlua_register( lua_State *L, const char* lname, const luaL_Reg *l ) 177 { 178 lua_getglobal( L, lname ); 179 if ( !lua_istable( L, -1 ) ) 180 { 181 lua_pop( L, 1 ); 182 lua_createtable( L, 0, 0 ); 183 nlua_register( L, l, -1 ); 184 lua_setglobal( L, lname ); 185 return; 186 } 187 nlua_register( L, l, -1 ); 188 lua_pop( L, 1 ); 189 }
Note: See TracChangeset
for help on using the changeset viewer.