Ignore:
Timestamp:
06/02/13 18:25:29 (12 years ago)
Author:
epyon
Message:
  • lua_raw - several utility functions added
  • lua_aux - aux implementations use lua_raw functions
  • lua_glm, lua_aux - registration through nlua_register
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lua/lua_raw.cc

    r51 r85  
    2727}
    2828
     29void 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
    2944void nlua_shallowcopy( lua_State *L, int index )
    3045{
    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 );
    3449
    3550        while ( lua_next( L, index ) != 0 )
    3651        {
    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 );
    4055        }
     56}
     57
     58void 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        };
    4174}
    4275
    4376void nlua_shallowmerge( lua_State *L, int index )
    4477{
    45         index = lua_absindex(L,index);
    46         lua_pushnil(L);
     78        index = lua_absindex( L, index );
     79        lua_pushnil( L );
    4780
    48         while( lua_next(L, index) != 0 )
     81        while( lua_next( L, index ) != 0 )
    4982        {
    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 );
    5386        }
    5487}
     
    5689void nlua_deepcopy( lua_State *L, int index )
    5790{
    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 );
    6194
    6295        while ( lua_next( L, index ) != 0 )
     
    68101                        lua_pop( L, 1 );
    69102                }
    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 );
    73106        }
    74107}
    75108
     109void 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
     128void 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
     142void 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
     150void 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
     161void 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
     176void 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.