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:
22 #include <sys/lua/lua.h>
24 #include <sys/lua/lauxlib.h>
25 #include <sys/lua/lualib.h>
27 #define SPACECHARS " \f\n\r\t\v"
29 static int luaB_tonumber (lua_State
*L
) {
30 if (lua_isnoneornil(L
, 2)) { /* standard conversion */
32 lua_Number n
= lua_tonumberx(L
, 1, &isnum
);
36 } /* else not a number; must be something */
41 const char *s
= luaL_checklstring(L
, 1, &l
);
42 const char *e
= s
+ l
; /* end point for 's' */
43 int base
= luaL_checkint(L
, 2);
45 luaL_argcheck(L
, 2 <= base
&& base
<= 36, 2, "base out of range");
46 s
+= strspn(s
, SPACECHARS
); /* skip initial spaces */
47 if (*s
== '-') { s
++; neg
= 1; } /* handle signal */
48 else if (*s
== '+') s
++;
49 if (isalnum((unsigned char)*s
)) {
52 int digit
= (isdigit((unsigned char)*s
)) ? *s
- '0'
53 : toupper((unsigned char)*s
) - 'A' + 10;
54 if (digit
>= base
) break; /* invalid numeral; force a fail */
55 n
= n
* (lua_Number
)base
+ (lua_Number
)digit
;
57 } while (isalnum((unsigned char)*s
));
58 s
+= strspn(s
, SPACECHARS
); /* skip trailing spaces */
59 if (s
== e
) { /* no invalid trailing characters? */
60 lua_pushnumber(L
, (neg
) ? -n
: n
);
62 } /* else not a number */
63 } /* else not a number */
65 lua_pushnil(L
); /* not a number */
70 static int luaB_error (lua_State
*L
) {
71 int level
= luaL_optint(L
, 2, 1);
73 if (lua_isstring(L
, 1) && level
> 0) { /* add extra information? */
82 static int luaB_getmetatable (lua_State
*L
) {
84 if (!lua_getmetatable(L
, 1)) {
86 return 1; /* no metatable */
88 luaL_getmetafield(L
, 1, "__metatable");
89 return 1; /* returns either __metatable field (if present) or metatable */
93 static int luaB_setmetatable (lua_State
*L
) {
94 int t
= lua_type(L
, 2);
95 luaL_checktype(L
, 1, LUA_TTABLE
);
96 luaL_argcheck(L
, t
== LUA_TNIL
|| t
== LUA_TTABLE
, 2,
97 "nil or table expected");
98 if (luaL_getmetafield(L
, 1, "__metatable"))
99 return luaL_error(L
, "cannot change a protected metatable");
101 lua_setmetatable(L
, 1);
106 static int luaB_rawequal (lua_State
*L
) {
109 lua_pushboolean(L
, lua_rawequal(L
, 1, 2));
114 static int luaB_rawlen (lua_State
*L
) {
115 int t
= lua_type(L
, 1);
116 luaL_argcheck(L
, t
== LUA_TTABLE
|| t
== LUA_TSTRING
, 1,
117 "table or string expected");
118 lua_pushinteger(L
, lua_rawlen(L
, 1));
123 static int luaB_rawget (lua_State
*L
) {
124 luaL_checktype(L
, 1, LUA_TTABLE
);
131 static int luaB_rawset (lua_State
*L
) {
132 luaL_checktype(L
, 1, LUA_TTABLE
);
141 static int luaB_collectgarbage (lua_State
*L
) {
142 static const char *const opts
[] = {"stop", "restart", "collect",
143 "count", "step", "setpause", "setstepmul",
144 "setmajorinc", "isrunning", "generational", "incremental", NULL
};
145 static const int optsnum
[] = {LUA_GCSTOP
, LUA_GCRESTART
, LUA_GCCOLLECT
,
146 LUA_GCCOUNT
, LUA_GCSTEP
, LUA_GCSETPAUSE
, LUA_GCSETSTEPMUL
,
147 LUA_GCSETMAJORINC
, LUA_GCISRUNNING
, LUA_GCGEN
, LUA_GCINC
};
148 int o
= optsnum
[luaL_checkoption(L
, 1, "collect", opts
)];
149 int ex
= luaL_optint(L
, 2, 0);
150 int res
= lua_gc(L
, o
, ex
);
153 int b
= lua_gc(L
, LUA_GCCOUNTB
, 0);
154 lua_pushnumber(L
, res
+ ((lua_Number
)b
/1024));
155 lua_pushinteger(L
, b
);
158 case LUA_GCSTEP
: case LUA_GCISRUNNING
: {
159 lua_pushboolean(L
, res
);
163 lua_pushinteger(L
, res
);
170 static int luaB_type (lua_State
*L
) {
172 lua_pushstring(L
, luaL_typename(L
, 1));
177 static int pairsmeta (lua_State
*L
, const char *method
, int iszero
,
178 lua_CFunction iter
) {
179 if (!luaL_getmetafield(L
, 1, method
)) { /* no metamethod? */
180 luaL_checktype(L
, 1, LUA_TTABLE
); /* argument must be a table */
181 lua_pushcfunction(L
, iter
); /* will return generator, */
182 lua_pushvalue(L
, 1); /* state, */
183 if (iszero
) lua_pushinteger(L
, 0); /* and initial value */
187 lua_pushvalue(L
, 1); /* argument 'self' to metamethod */
188 lua_call(L
, 1, 3); /* get 3 values from metamethod */
194 static int luaB_next (lua_State
*L
) {
195 luaL_checktype(L
, 1, LUA_TTABLE
);
196 lua_settop(L
, 2); /* create a 2nd argument if there isn't one */
206 static int luaB_pairs (lua_State
*L
) {
207 return pairsmeta(L
, "__pairs", 0, luaB_next
);
211 static int ipairsaux (lua_State
*L
) {
212 int i
= luaL_checkint(L
, 2);
213 luaL_checktype(L
, 1, LUA_TTABLE
);
214 i
++; /* next value */
215 lua_pushinteger(L
, i
);
216 lua_rawgeti(L
, 1, i
);
217 return (lua_isnil(L
, -1)) ? 1 : 2;
221 static int luaB_ipairs (lua_State
*L
) {
222 return pairsmeta(L
, "__ipairs", 1, ipairsaux
);
226 static int luaB_assert (lua_State
*L
) {
227 if (!lua_toboolean(L
, 1))
228 return luaL_error(L
, "%s", luaL_optstring(L
, 2, "assertion failed!"));
229 return lua_gettop(L
);
233 static int luaB_select (lua_State
*L
) {
234 int n
= lua_gettop(L
);
235 if (lua_type(L
, 1) == LUA_TSTRING
&& *lua_tostring(L
, 1) == '#') {
236 lua_pushinteger(L
, n
-1);
240 int i
= luaL_checkint(L
, 1);
241 if (i
< 0) i
= n
+ i
;
242 else if (i
> n
) i
= n
;
243 luaL_argcheck(L
, 1 <= i
, 1, "index out of range");
248 static int luaB_tostring (lua_State
*L
) {
250 luaL_tolstring(L
, 1, NULL
);
254 static const luaL_Reg base_funcs
[] = {
255 {"assert", luaB_assert
},
256 {"collectgarbage", luaB_collectgarbage
},
257 {"error", luaB_error
},
258 {"getmetatable", luaB_getmetatable
},
259 {"ipairs", luaB_ipairs
},
260 #if defined(LUA_COMPAT_LOADSTRING)
261 {"loadstring", luaB_load
},
264 {"pairs", luaB_pairs
},
265 {"rawequal", luaB_rawequal
},
266 {"rawlen", luaB_rawlen
},
267 {"rawget", luaB_rawget
},
268 {"rawset", luaB_rawset
},
269 {"select", luaB_select
},
270 {"setmetatable", luaB_setmetatable
},
271 {"tonumber", luaB_tonumber
},
272 {"tostring", luaB_tostring
},
278 LUAMOD_API
int luaopen_base (lua_State
*L
) {
280 lua_pushglobaltable(L
);
281 lua_pushglobaltable(L
);
282 lua_setfield(L
, -2, "_G");
283 /* open lib into global table */
284 luaL_setfuncs(L
, base_funcs
, 0);
285 lua_pushliteral(L
, LUA_VERSION
);
286 lua_setfield(L
, -2, "_VERSION"); /* set global _VERSION */
292 EXPORT_SYMBOL(luaopen_base
);