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 | static int nlua_table_copy( lua_State* L )
|
---|
10 | {
|
---|
11 | luaL_checktype( L, 1, LUA_TTABLE );
|
---|
12 | 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 | }
|
---|
21 | return 1;
|
---|
22 | }
|
---|
23 |
|
---|
24 | static int nlua_table_icopy( lua_State* L )
|
---|
25 | {
|
---|
26 | luaL_checktype( L, 1, LUA_TTABLE );
|
---|
27 | 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 | };
|
---|
41 | return 1;
|
---|
42 | }
|
---|
43 |
|
---|
44 | static int nlua_table_merge( lua_State* L )
|
---|
45 | {
|
---|
46 | luaL_checktype( L, 1, LUA_TTABLE );
|
---|
47 | luaL_checktype( L, 2, LUA_TTABLE );
|
---|
48 | 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 | }
|
---|
56 | return 0;
|
---|
57 | }
|
---|
58 |
|
---|
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 )
|
---|
80 | {
|
---|
81 | luaL_checktype( L, 1, LUA_TTABLE );
|
---|
82 | 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 | }
|
---|
92 | return 1;
|
---|
93 | }
|
---|
94 |
|
---|
95 | static int nlua_table_toset( lua_State* L )
|
---|
96 | {
|
---|
97 | luaL_checktype( L, 1, LUA_TTABLE );
|
---|
98 | 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 | };
|
---|
113 | return 1;
|
---|
114 | }
|
---|
115 |
|
---|
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 },
|
---|
123 | { NULL, NULL }
|
---|
124 | };
|
---|
125 |
|
---|
126 | static const struct luaL_Reg nlua_math_aux_f [] = {
|
---|
127 | { NULL, NULL }
|
---|
128 | };
|
---|
129 |
|
---|
130 | void nlua_register_aux( lua_State* L )
|
---|
131 | {
|
---|
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 );
|
---|
139 | }
|
---|
140 |
|
---|