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