2 ** $Id: lbaselib.c,v 1.276.1.1 2013/04/12 18:48:47 roberto Exp $
4 ** See Copyright Notice in lua.h
7 /* The following built-in lua functions have been removed and are not available
8 * for use in ZFS channel programs:
18 #include <sys/zfs_context.h>
19 #include <sys/ctype.h>
21 #define toupper(C) (((C) >= 'a' && (C) <= 'z')? (C) - 'a' + 'A': (C))
23 #define isalnum(C) (isalpha(C) || isdigit(C))
34 #define SPACECHARS " \f\n\r\t\v"
36 static int luaB_tonumber (lua_State *L) {
37 if (lua_isnoneornil(L, 2)) { /* standard conversion */
39 lua_Number n = lua_tonumberx(L, 1, &isnum);
43 } /* else not a number; must be something */
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);
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)) {
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;
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);
69 } /* else not a number */
70 } /* else not a number */
72 lua_pushnil(L); /* not a number */
77 static int luaB_error (lua_State *L) {
78 int level = luaL_optint(L, 2, 1);
80 if (lua_isstring(L, 1) && level > 0) { /* add extra information? */
89 static int luaB_getmetatable (lua_State *L) {
91 if (!lua_getmetatable(L, 1)) {
93 return 1; /* no metatable */
95 luaL_getmetafield(L, 1, "__metatable");
96 return 1; /* returns either __metatable field (if present) or metatable */
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");
108 lua_setmetatable(L, 1);
113 static int luaB_rawequal (lua_State *L) {
116 lua_pushboolean(L, lua_rawequal(L, 1, 2));
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));
130 static int luaB_rawget (lua_State *L) {
131 luaL_checktype(L, 1, LUA_TTABLE);
138 static int luaB_rawset (lua_State *L) {
139 luaL_checktype(L, 1, LUA_TTABLE);
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);
160 int b = lua_gc(L, LUA_GCCOUNTB, 0);
161 lua_pushnumber(L, res + ((lua_Number)b/1024));
162 lua_pushinteger(L, b);
165 case LUA_GCSTEP: case LUA_GCISRUNNING: {
166 lua_pushboolean(L, res);
170 lua_pushinteger(L, res);
177 static int luaB_type (lua_State *L) {
179 lua_pushstring(L, luaL_typename(L, 1));
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 */
194 lua_pushvalue(L, 1); /* argument 'self' to metamethod */
195 lua_call(L, 1, 3); /* get 3 values from metamethod */
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 */
213 static int luaB_pairs (lua_State *L) {
214 return pairsmeta(L, "__pairs", 0, luaB_next);
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;
228 static int luaB_ipairs (lua_State *L) {
229 return pairsmeta(L, "__ipairs", 1, ipairsaux);
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);
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);
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");
255 static int luaB_tostring (lua_State *L) {
257 luaL_tolstring(L, 1, NULL);
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},
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},
285 LUAMOD_API int luaopen_base (lua_State *L) {
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 */