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