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_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
Note: See TracChangeset for help on using the changeset viewer.