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