]> git.proxmox.com Git - mirror_zfs.git/blob - module/lua/lbaselib.c
Clean up CSTYLEDs
[mirror_zfs.git] / module / lua / lbaselib.c
1 /*
2 ** $Id: lbaselib.c,v 1.276.1.1 2013/04/12 18:48:47 roberto Exp $
3 ** Basic library
4 ** See Copyright Notice in lua.h
5 */
6
7 /* The following built-in lua functions have been removed and are not available
8 * for use in ZFS channel programs:
9 *
10 * dofile
11 * loadfile
12 * load
13 * pcall
14 * print
15 * xpcall
16 */
17
18
19 #define lbaselib_c
20 #define LUA_LIB
21
22 #include <sys/lua/lua.h>
23
24 #include <sys/lua/lauxlib.h>
25 #include <sys/lua/lualib.h>
26
27 #define SPACECHARS " \f\n\r\t\v"
28
29 static int luaB_tonumber (lua_State *L) {
30 if (lua_isnoneornil(L, 2)) { /* standard conversion */
31 int isnum;
32 lua_Number n = lua_tonumberx(L, 1, &isnum);
33 if (isnum) {
34 lua_pushnumber(L, n);
35 return 1;
36 } /* else not a number; must be something */
37 luaL_checkany(L, 1);
38 }
39 else {
40 size_t l;
41 const char *s = luaL_checklstring(L, 1, &l);
42 const char *e = s + l; /* end point for 's' */
43 int base = luaL_checkint(L, 2);
44 int neg = 0;
45 luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
46 s += strspn(s, SPACECHARS); /* skip initial spaces */
47 if (*s == '-') { s++; neg = 1; } /* handle signal */
48 else if (*s == '+') s++;
49 if (isalnum((unsigned char)*s)) {
50 lua_Number n = 0;
51 do {
52 int digit = (isdigit((unsigned char)*s)) ? *s - '0'
53 : toupper((unsigned char)*s) - 'A' + 10;
54 if (digit >= base) break; /* invalid numeral; force a fail */
55 n = n * (lua_Number)base + (lua_Number)digit;
56 s++;
57 } while (isalnum((unsigned char)*s));
58 s += strspn(s, SPACECHARS); /* skip trailing spaces */
59 if (s == e) { /* no invalid trailing characters? */
60 lua_pushnumber(L, (neg) ? -n : n);
61 return 1;
62 } /* else not a number */
63 } /* else not a number */
64 }
65 lua_pushnil(L); /* not a number */
66 return 1;
67 }
68
69
70 static int luaB_error (lua_State *L) {
71 int level = luaL_optint(L, 2, 1);
72 lua_settop(L, 1);
73 if (lua_isstring(L, 1) && level > 0) { /* add extra information? */
74 luaL_where(L, level);
75 lua_pushvalue(L, 1);
76 lua_concat(L, 2);
77 }
78 return lua_error(L);
79 }
80
81
82 static int luaB_getmetatable (lua_State *L) {
83 luaL_checkany(L, 1);
84 if (!lua_getmetatable(L, 1)) {
85 lua_pushnil(L);
86 return 1; /* no metatable */
87 }
88 luaL_getmetafield(L, 1, "__metatable");
89 return 1; /* returns either __metatable field (if present) or metatable */
90 }
91
92
93 static int luaB_setmetatable (lua_State *L) {
94 int t = lua_type(L, 2);
95 luaL_checktype(L, 1, LUA_TTABLE);
96 luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
97 "nil or table expected");
98 if (luaL_getmetafield(L, 1, "__metatable"))
99 return luaL_error(L, "cannot change a protected metatable");
100 lua_settop(L, 2);
101 lua_setmetatable(L, 1);
102 return 1;
103 }
104
105
106 static int luaB_rawequal (lua_State *L) {
107 luaL_checkany(L, 1);
108 luaL_checkany(L, 2);
109 lua_pushboolean(L, lua_rawequal(L, 1, 2));
110 return 1;
111 }
112
113
114 static int luaB_rawlen (lua_State *L) {
115 int t = lua_type(L, 1);
116 luaL_argcheck(L, t == LUA_TTABLE || t == LUA_TSTRING, 1,
117 "table or string expected");
118 lua_pushinteger(L, lua_rawlen(L, 1));
119 return 1;
120 }
121
122
123 static int luaB_rawget (lua_State *L) {
124 luaL_checktype(L, 1, LUA_TTABLE);
125 luaL_checkany(L, 2);
126 lua_settop(L, 2);
127 lua_rawget(L, 1);
128 return 1;
129 }
130
131 static int luaB_rawset (lua_State *L) {
132 luaL_checktype(L, 1, LUA_TTABLE);
133 luaL_checkany(L, 2);
134 luaL_checkany(L, 3);
135 lua_settop(L, 3);
136 lua_rawset(L, 1);
137 return 1;
138 }
139
140
141 static int luaB_collectgarbage (lua_State *L) {
142 static const char *const opts[] = {"stop", "restart", "collect",
143 "count", "step", "setpause", "setstepmul",
144 "setmajorinc", "isrunning", "generational", "incremental", NULL};
145 static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
146 LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL,
147 LUA_GCSETMAJORINC, LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC};
148 int o = optsnum[luaL_checkoption(L, 1, "collect", opts)];
149 int ex = luaL_optint(L, 2, 0);
150 int res = lua_gc(L, o, ex);
151 switch (o) {
152 case LUA_GCCOUNT: {
153 int b = lua_gc(L, LUA_GCCOUNTB, 0);
154 lua_pushnumber(L, res + ((lua_Number)b/1024));
155 lua_pushinteger(L, b);
156 return 2;
157 }
158 case LUA_GCSTEP: case LUA_GCISRUNNING: {
159 lua_pushboolean(L, res);
160 return 1;
161 }
162 default: {
163 lua_pushinteger(L, res);
164 return 1;
165 }
166 }
167 }
168
169
170 static int luaB_type (lua_State *L) {
171 luaL_checkany(L, 1);
172 lua_pushstring(L, luaL_typename(L, 1));
173 return 1;
174 }
175
176
177 static int pairsmeta (lua_State *L, const char *method, int iszero,
178 lua_CFunction iter) {
179 if (!luaL_getmetafield(L, 1, method)) { /* no metamethod? */
180 luaL_checktype(L, 1, LUA_TTABLE); /* argument must be a table */
181 lua_pushcfunction(L, iter); /* will return generator, */
182 lua_pushvalue(L, 1); /* state, */
183 if (iszero) lua_pushinteger(L, 0); /* and initial value */
184 else lua_pushnil(L);
185 }
186 else {
187 lua_pushvalue(L, 1); /* argument 'self' to metamethod */
188 lua_call(L, 1, 3); /* get 3 values from metamethod */
189 }
190 return 3;
191 }
192
193
194 static int luaB_next (lua_State *L) {
195 luaL_checktype(L, 1, LUA_TTABLE);
196 lua_settop(L, 2); /* create a 2nd argument if there isn't one */
197 if (lua_next(L, 1))
198 return 2;
199 else {
200 lua_pushnil(L);
201 return 1;
202 }
203 }
204
205
206 static int luaB_pairs (lua_State *L) {
207 return pairsmeta(L, "__pairs", 0, luaB_next);
208 }
209
210
211 static int ipairsaux (lua_State *L) {
212 int i = luaL_checkint(L, 2);
213 luaL_checktype(L, 1, LUA_TTABLE);
214 i++; /* next value */
215 lua_pushinteger(L, i);
216 lua_rawgeti(L, 1, i);
217 return (lua_isnil(L, -1)) ? 1 : 2;
218 }
219
220
221 static int luaB_ipairs (lua_State *L) {
222 return pairsmeta(L, "__ipairs", 1, ipairsaux);
223 }
224
225
226 static int luaB_assert (lua_State *L) {
227 if (!lua_toboolean(L, 1))
228 return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!"));
229 return lua_gettop(L);
230 }
231
232
233 static int luaB_select (lua_State *L) {
234 int n = lua_gettop(L);
235 if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') {
236 lua_pushinteger(L, n-1);
237 return 1;
238 }
239 else {
240 int i = luaL_checkint(L, 1);
241 if (i < 0) i = n + i;
242 else if (i > n) i = n;
243 luaL_argcheck(L, 1 <= i, 1, "index out of range");
244 return n - i;
245 }
246 }
247
248 static int luaB_tostring (lua_State *L) {
249 luaL_checkany(L, 1);
250 luaL_tolstring(L, 1, NULL);
251 return 1;
252 }
253
254 static const luaL_Reg base_funcs[] = {
255 {"assert", luaB_assert},
256 {"collectgarbage", luaB_collectgarbage},
257 {"error", luaB_error},
258 {"getmetatable", luaB_getmetatable},
259 {"ipairs", luaB_ipairs},
260 #if defined(LUA_COMPAT_LOADSTRING)
261 {"loadstring", luaB_load},
262 #endif
263 {"next", luaB_next},
264 {"pairs", luaB_pairs},
265 {"rawequal", luaB_rawequal},
266 {"rawlen", luaB_rawlen},
267 {"rawget", luaB_rawget},
268 {"rawset", luaB_rawset},
269 {"select", luaB_select},
270 {"setmetatable", luaB_setmetatable},
271 {"tonumber", luaB_tonumber},
272 {"tostring", luaB_tostring},
273 {"type", luaB_type},
274 {NULL, NULL}
275 };
276
277
278 LUAMOD_API int luaopen_base (lua_State *L) {
279 /* set global _G */
280 lua_pushglobaltable(L);
281 lua_pushglobaltable(L);
282 lua_setfield(L, -2, "_G");
283 /* open lib into global table */
284 luaL_setfuncs(L, base_funcs, 0);
285 lua_pushliteral(L, LUA_VERSION);
286 lua_setfield(L, -2, "_VERSION"); /* set global _VERSION */
287 return 1;
288 }
289
290 #if defined(_KERNEL)
291
292 EXPORT_SYMBOL(luaopen_base);
293
294 #endif