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