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_raw.hh"
|
---|
8 |
|
---|
9 | #include "nv/string.hh"
|
---|
10 |
|
---|
11 | std::string nlua_typecontent( lua_State* L, int idx )
|
---|
12 | {
|
---|
13 | switch ( lua_type( L, idx ) )
|
---|
14 | {
|
---|
15 | case LUA_TNONE : return "NONE";
|
---|
16 | case LUA_TNIL : return "NIL";
|
---|
17 | case LUA_TBOOLEAN : return lua_toboolean( L, idx ) == 0 ? "false" : "true";
|
---|
18 | case LUA_TLIGHTUSERDATA : return nv::to_string( nv::uint64( lua_touserdata( L, idx ) ) );
|
---|
19 | case LUA_TNUMBER : return nv::to_string( lua_tonumber( L, idx ) );
|
---|
20 | case LUA_TSTRING : return lua_tostring( L, idx );
|
---|
21 | case LUA_TTABLE : return "TABLE";
|
---|
22 | case LUA_TFUNCTION : return "FUNCTION";
|
---|
23 | case LUA_TUSERDATA : return nv::to_string( nv::uint64( lua_touserdata( L, idx ) ) );
|
---|
24 | case LUA_TTHREAD : return "THREAD";
|
---|
25 | default : return "UNKNOWN!";
|
---|
26 | }
|
---|
27 | }
|
---|
28 |
|
---|
29 | void 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 |
|
---|
44 | void nlua_shallowcopy( lua_State *L, int index )
|
---|
45 | {
|
---|
46 | index = lua_absindex( L, index );
|
---|
47 | lua_createtable( L, 0, 0 );
|
---|
48 | lua_pushnil( L );
|
---|
49 |
|
---|
50 | while ( lua_next( L, index ) != 0 )
|
---|
51 | {
|
---|
52 | lua_pushvalue( L, -2 );
|
---|
53 | lua_insert( L, -2 );
|
---|
54 | lua_settable( L, -4 );
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | void 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 | };
|
---|
74 | }
|
---|
75 |
|
---|
76 | void nlua_shallowmerge( lua_State *L, int index )
|
---|
77 | {
|
---|
78 | index = lua_absindex( L, index );
|
---|
79 | lua_pushnil( L );
|
---|
80 |
|
---|
81 | while( lua_next( L, index ) != 0 )
|
---|
82 | {
|
---|
83 | lua_pushvalue( L, -2 );
|
---|
84 | lua_insert( L, -2 );
|
---|
85 | lua_rawset( L, -4 );
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | void nlua_deepcopy( lua_State *L, int index )
|
---|
90 | {
|
---|
91 | index = lua_absindex( L, index );
|
---|
92 | lua_createtable( L, 0, 0 );
|
---|
93 | lua_pushnil( L );
|
---|
94 |
|
---|
95 | while ( lua_next( L, index ) != 0 )
|
---|
96 | {
|
---|
97 | if ( lua_istable( L, -1 ) )
|
---|
98 | {
|
---|
99 | nlua_deepcopy( L, -1 );
|
---|
100 | lua_insert( L, -2 );
|
---|
101 | lua_pop( L, 1 );
|
---|
102 | }
|
---|
103 | lua_pushvalue( L, -2 );
|
---|
104 | lua_insert( L, -2 );
|
---|
105 | lua_settable( L, -4 );
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | void 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 |
|
---|
128 | void 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 |
|
---|
142 | void 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 |
|
---|
150 | void 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 |
|
---|
161 | void 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 |
|
---|
176 | void 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 | }
|
---|