Changeset 85 for trunk/src


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
Location:
trunk/src/lua
Files:
3 edited

Legend:

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

    r56 r85  
    77#include "nv/lua/lua_aux.hh"
    88
    9 static int nlua_table_copy( lua_State* L )
     9#include "nv/lua/lua_raw.hh"
     10
     11static int nluaaux_table_copy( lua_State* L )
    1012{
    1113        luaL_checktype( L, 1, LUA_TTABLE );
    1214        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 );
    2116        return 1;
    2217}
    2318
    24 static int nlua_table_icopy( lua_State* L )
     19static int nluaaux_table_deepcopy( lua_State* L )
    2520{
    2621        luaL_checktype( L, 1, LUA_TTABLE );
    2722        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
     27static 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 );
    4132        return 1;
    4233}
    4334
    44 static int nlua_table_merge( lua_State* L )
     35static int nluaaux_table_merge( lua_State* L )
    4536{
    4637        luaL_checktype( L, 1, LUA_TTABLE );
    4738        luaL_checktype( L, 2, LUA_TTABLE );
    4839        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 );
    5641        return 0;
    5742}
    5843
    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 )
     44static int nluaaux_table_reversed( lua_State* L )
    8045{
    8146        luaL_checktype( L, 1, LUA_TTABLE );
    8247        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 );
    9249        return 1;
    9350}
    9451
    95 static int nlua_table_toset( lua_State* L )
     52static int nluaaux_table_toset( lua_State* L )
    9653{
    9754        luaL_checktype( L, 1, LUA_TTABLE );
    9855        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 );
    11357        return 1;
    11458}
    11559
    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 },
     60static 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
     68static 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 },
    12376        { NULL, NULL }
    12477};
    12578
    126 static const struct luaL_Reg nlua_math_aux_f [] = {
     79static 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
     90static const struct luaL_Reg nluaaux_math_aux_f [] = {
     91        { "clamp",     nluaaux_math_clamp },
    12792        { NULL, NULL }
    12893};
     
    13095void nlua_register_aux( lua_State* L )
    13196{
    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 );
    13999}
    140100
  • trunk/src/lua/lua_glm.cc

    r75 r85  
    66
    77#include "nv/lua/lua_glm.hh"
     8
     9#include "nv/lua/lua_raw.hh"
    810#include "nv/string.hh"
    911
     
    203205        size_t idx = 255;
    204206
    205         if( len == 1 )
     207        if ( len == 1 )
    206208        {
    207209                idx = nlua_swizzel_lookup[ key[ 0 ] ];
     
    222224        }
    223225
    224         lua_getmetatable( L, 1 );
    225         lua_getfield( L, -1, "__base" );
     226        luaL_getmetafield( L, 1, "__base" );
    226227        lua_pushvalue( L, 2 );
    227228        lua_rawget( L, -2 );
     
    305306
    306307        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 );
    309311        lua_pushvalue(L, -1);
    310312        lua_setfield(L, -3, "__base" );
    311313        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 );
    314316        lua_setmetatable( L, -2 );
    315317        return 1;
  • 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.