3 ** $Id: lbaselib.c,v 1.276.1.1 2013/04/12 18:48:47 roberto Exp $
5 ** See Copyright Notice in lua.h
8 /* The following built-in lua functions have been removed and are not available
9 * for use in ZFS channel programs:
23 #include <sys/lua/lua.h>
25 #include <sys/lua/lauxlib.h>
26 #include <sys/lua/lualib.h>
28 #define SPACECHARS " \f\n\r\t\v"
30 static int luaB_tonumber (lua_State *L) {
31 if (lua_isnoneornil(L, 2)) { /* standard conversion */
33 lua_Number n = lua_tonumberx(L, 1, &isnum);
37 } /* else not a number; must be something */
42 const char *s = luaL_checklstring(L, 1, &l);
43 const char *e = s + l; /* end point for 's' */
44 int base = luaL_checkint(L, 2);
46 luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
47 s += strspn(s, SPACECHARS); /* skip initial spaces */
48 if (*s == '-') { s++; neg = 1; } /* handle signal */
49 else if (*s == '+') s++;
50 if (isalnum((unsigned char)*s)) {
53 int digit = (isdigit((unsigned char)*s)) ? *s - '0'
54 : toupper((unsigned char)*s) - 'A' + 10;
55 if (digit >= base) break; /* invalid numeral; force a fail */
56 n = n * (lua_Number)base + (lua_Number)digit;
58 } while (isalnum((unsigned char)*s));
59 s += strspn(s, SPACECHARS); /* skip trailing spaces */
60 if (s == e) { /* no invalid trailing characters? */
61 lua_pushnumber(L, (neg) ? -n : n);
63 } /* else not a number */
64 } /* else not a number */
66 lua_pushnil(L); /* not a number */
71 static int luaB_error (lua_State *L) {
72 int level = luaL_optint(L, 2, 1);
74 if (lua_isstring(L, 1) && level > 0) { /* add extra information? */
83 static int luaB_getmetatable (lua_State *L) {
85 if (!lua_getmetatable(L, 1)) {
87 return 1; /* no metatable */
89 luaL_getmetafield(L, 1, "__metatable");
90 return 1; /* returns either __metatable field (if present) or metatable */
94 static int luaB_setmetatable (lua_State *L) {
95 int t = lua_type(L, 2);
96 luaL_checktype(L, 1, LUA_TTABLE);
97 luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
98 "nil or table expected");
99 if (luaL_getmetafield(L, 1, "__metatable"))
100 return luaL_error(L, "cannot change a protected metatable");
102 lua_setmetatable(L, 1);
107 static int luaB_rawequal (lua_State *L) {
110 lua_pushboolean(L, lua_rawequal(L, 1, 2));
115 static int luaB_rawlen (lua_State *L) {
116 int t = lua_type(L, 1);
117 luaL_argcheck(L, t == LUA_TTABLE || t == LUA_TSTRING, 1,
118 "table or string expected");
119 lua_pushinteger(L, lua_rawlen(L, 1));
124 static int luaB_rawget (lua_State *L) {
125 luaL_checktype(L, 1, LUA_TTABLE);
132 static int luaB_rawset (lua_State *L) {
133 luaL_checktype(L, 1, LUA_TTABLE);
142 static int luaB_collectgarbage (lua_State *L) {
143 static const char *const opts[] = {"stop", "restart", "collect",
144 "count", "step", "setpause", "setstepmul",
145 "setmajorinc", "isrunning", "generational", "incremental", NULL};
146 static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
147 LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL,
148 LUA_GCSETMAJORINC, LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC};
149 int o = optsnum[luaL_checkoption(L, 1, "collect", opts)];
150 int ex = luaL_optint(L, 2, 0);
151 int res = lua_gc(L, o, ex);
154 int b = lua_gc(L, LUA_GCCOUNTB, 0);
155 lua_pushnumber(L, res + ((lua_Number)b/1024));
156 lua_pushinteger(L, b);
159 case LUA_GCSTEP: case LUA_GCISRUNNING: {
160 lua_pushboolean(L, res);
164 lua_pushinteger(L, res);
171 static int luaB_type (lua_State *L) {
173 lua_pushstring(L, luaL_typename(L, 1));
178 static int pairsmeta (lua_State *L, const char *method, int iszero,
179 lua_CFunction iter) {
180 if (!luaL_getmetafield(L, 1, method)) { /* no metamethod? */
181 luaL_checktype(L, 1, LUA_TTABLE); /* argument must be a table */
182 lua_pushcfunction(L, iter); /* will return generator, */
183 lua_pushvalue(L, 1); /* state, */
184 if (iszero) lua_pushinteger(L, 0); /* and initial value */
188 lua_pushvalue(L, 1); /* argument 'self' to metamethod */
189 lua_call(L, 1, 3); /* get 3 values from metamethod */
195 static int luaB_next (lua_State *L) {
196 luaL_checktype(L, 1, LUA_TTABLE);
197 lua_settop(L, 2); /* create a 2nd argument if there isn't one */
207 static int luaB_pairs (lua_State *L) {
208 return pairsmeta(L, "__pairs", 0, luaB_next);
212 static int ipairsaux (lua_State *L) {
213 int i = luaL_checkint(L, 2);
214 luaL_checktype(L, 1, LUA_TTABLE);
215 i++; /* next value */
216 lua_pushinteger(L, i);
217 lua_rawgeti(L, 1, i);
218 return (lua_isnil(L, -1)) ? 1 : 2;
222 static int luaB_ipairs (lua_State *L) {
223 return pairsmeta(L, "__ipairs", 1, ipairsaux);
227 static int luaB_assert (lua_State *L) {
228 if (!lua_toboolean(L, 1))
229 return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!"));
230 return lua_gettop(L);
234 static int luaB_select (lua_State *L) {
235 int n = lua_gettop(L);
236 if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') {
237 lua_pushinteger(L, n-1);
241 int i = luaL_checkint(L, 1);
242 if (i < 0) i = n + i;
243 else if (i > n) i = n;
244 luaL_argcheck(L, 1 <= i, 1, "index out of range");
249 static int luaB_tostring (lua_State *L) {
251 luaL_tolstring(L, 1, NULL);
255 static const luaL_Reg base_funcs[] = {
256 {"assert", luaB_assert},
257 {"collectgarbage", luaB_collectgarbage},
258 {"error", luaB_error},
259 {"getmetatable", luaB_getmetatable},
260 {"ipairs", luaB_ipairs},
261 #if defined(LUA_COMPAT_LOADSTRING)
262 {"loadstring", luaB_load},
265 {"pairs", luaB_pairs},
266 {"rawequal", luaB_rawequal},
267 {"rawlen", luaB_rawlen},
268 {"rawget", luaB_rawget},
269 {"rawset", luaB_rawset},
270 {"select", luaB_select},
271 {"setmetatable", luaB_setmetatable},
272 {"tonumber", luaB_tonumber},
273 {"tostring", luaB_tostring},
279 LUAMOD_API int luaopen_base (lua_State *L) {
281 lua_pushglobaltable(L);
282 lua_pushglobaltable(L);
283 lua_setfield(L, -2, "_G");
284 /* open lib into global table */
285 luaL_setfuncs(L, base_funcs, 0);
286 lua_pushliteral(L, LUA_VERSION);
287 lua_setfield(L, -2, "_VERSION"); /* set global _VERSION */
293 EXPORT_SYMBOL(luaopen_base);