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 |
|
---|
9 | #include "nv/lua/lua_raw.hh"
|
---|
10 |
|
---|
11 | static int nluaaux_table_copy( lua_State* L )
|
---|
12 | {
|
---|
13 | luaL_checktype( L, 1, LUA_TTABLE );
|
---|
14 | lua_settop( L, 1 );
|
---|
15 | nlua_shallowcopy( L, 1 );
|
---|
16 | return 1;
|
---|
17 | }
|
---|
18 |
|
---|
19 | static int nluaaux_table_deepcopy( lua_State* L )
|
---|
20 | {
|
---|
21 | luaL_checktype( L, 1, LUA_TTABLE );
|
---|
22 | lua_settop( L, 1 );
|
---|
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 );
|
---|
32 | return 1;
|
---|
33 | }
|
---|
34 |
|
---|
35 | static int nluaaux_table_merge( lua_State* L )
|
---|
36 | {
|
---|
37 | luaL_checktype( L, 1, LUA_TTABLE );
|
---|
38 | luaL_checktype( L, 2, LUA_TTABLE );
|
---|
39 | lua_settop( L, 2 );
|
---|
40 | nlua_shallowmerge( L, 1 );
|
---|
41 | return 0;
|
---|
42 | }
|
---|
43 |
|
---|
44 | static int nluaaux_table_reversed( lua_State* L )
|
---|
45 | {
|
---|
46 | luaL_checktype( L, 1, LUA_TTABLE );
|
---|
47 | lua_settop( L, 1 );
|
---|
48 | nlua_pushreversed( L, 1 );
|
---|
49 | return 1;
|
---|
50 | }
|
---|
51 |
|
---|
52 | static int nluaaux_table_toset( lua_State* L )
|
---|
53 | {
|
---|
54 | luaL_checktype( L, 1, LUA_TTABLE );
|
---|
55 | lua_settop( L, 1 );
|
---|
56 | nlua_toset( L, 1 );
|
---|
57 | return 1;
|
---|
58 | }
|
---|
59 |
|
---|
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 },
|
---|
76 | { NULL, NULL }
|
---|
77 | };
|
---|
78 |
|
---|
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 },
|
---|
92 | { NULL, NULL }
|
---|
93 | };
|
---|
94 |
|
---|
95 | void nlua_register_aux( lua_State* L )
|
---|
96 | {
|
---|
97 | nlua_register( L, "table", nluaaux_table_aux_f );
|
---|
98 | nlua_register( L, "math", nluaaux_math_aux_f );
|
---|
99 | }
|
---|
100 |
|
---|