source: trunk/src/lua/lua_aux.cc @ 85

Last change on this file since 85 was 85, checked in by epyon, 12 years ago
  • lua_raw - several utility functions added
  • lua_aux - aux implementations use lua_raw functions
  • lua_glm, lua_aux - registration through nlua_register
File size: 2.3 KB
RevLine 
[56]1// Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz
2// http://chaosforge.org/
3//
4// This file is part of NV Libraries.
5// For conditions of distribution and use, see copyright notice in nv.hh
6
7#include "nv/lua/lua_aux.hh"
8
[85]9#include "nv/lua/lua_raw.hh"
10
11static int nluaaux_table_copy( lua_State* L )
[56]12{
13        luaL_checktype( L, 1, LUA_TTABLE );
14        lua_settop( L, 1 );
[85]15        nlua_shallowcopy( L, 1 );
[56]16        return 1;
17}
18
[85]19static int nluaaux_table_deepcopy( lua_State* L )
[56]20{
21        luaL_checktype( L, 1, LUA_TTABLE );
22        lua_settop( L, 1 );
[85]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 );
[56]32        return 1;
33}
34
[85]35static int nluaaux_table_merge( lua_State* L )
[56]36{
37        luaL_checktype( L, 1, LUA_TTABLE );
38        luaL_checktype( L, 2, LUA_TTABLE );
39        lua_settop( L, 2 );
[85]40        nlua_shallowmerge( L, 1 );
[56]41        return 0;
42}
43
[85]44static int nluaaux_table_reversed( lua_State* L )
[56]45{
46        luaL_checktype( L, 1, LUA_TTABLE );
[85]47        lua_settop( L, 1 );
48        nlua_pushreversed( L, 1 );
49        return 1;
[56]50}
51
[85]52static int nluaaux_table_toset( lua_State* L )
[56]53{
54        luaL_checktype( L, 1, LUA_TTABLE );
55        lua_settop( L, 1 );
[85]56        nlua_toset( L, 1 );
[56]57        return 1;
58}
59
[85]60static int nluaaux_table_tokeyset( lua_State* L )
[56]61{
62        luaL_checktype( L, 1, LUA_TTABLE );
63        lua_settop( L, 1 );
[85]64        nlua_tokeyset( L, 1 );
[56]65        return 1;
66}
67
[85]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 },
[56]76        { NULL, NULL }
77};
78
[85]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 },
[56]92        { NULL, NULL }
93};
94
95void nlua_register_aux( lua_State* L )
96{
[85]97        nlua_register( L, "table", nluaaux_table_aux_f );
98        nlua_register( L, "math", nluaaux_math_aux_f );
[56]99}
100
Note: See TracBrowser for help on using the repository browser.