]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - module/lua/lbaselib.c
Vendor import of openzfs master @ 184df27eef0abdc7ab2105b21257f753834b936b
[FreeBSD/FreeBSD.git] / module / lua / lbaselib.c
1 /* BEGIN CSTYLED */
2 /*
3 ** $Id: lbaselib.c,v 1.276.1.1 2013/04/12 18:48:47 roberto Exp $
4 ** Basic library
5 ** See Copyright Notice in lua.h
6 */
7
8 /* The following built-in lua functions have been removed and are not available
9  * for use in ZFS channel programs:
10  *
11  * dofile
12  * loadfile
13  * load
14  * pcall
15  * print
16  * xpcall
17  */
18
19
20 #define lbaselib_c
21 #define LUA_LIB
22
23 #include <sys/lua/lua.h>
24
25 #include <sys/lua/lauxlib.h>
26 #include <sys/lua/lualib.h>
27
28 #define SPACECHARS      " \f\n\r\t\v"
29
30 static int luaB_tonumber (lua_State *L) {
31   if (lua_isnoneornil(L, 2)) {  /* standard conversion */
32     int isnum;
33     lua_Number n = lua_tonumberx(L, 1, &isnum);
34     if (isnum) {
35       lua_pushnumber(L, n);
36       return 1;
37     }  /* else not a number; must be something */
38     luaL_checkany(L, 1);
39   }
40   else {
41     size_t l;
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);
45     int neg = 0;
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)) {
51       lua_Number n = 0;
52       do {
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;
57         s++;
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);
62         return 1;
63       }  /* else not a number */
64     }  /* else not a number */
65   }
66   lua_pushnil(L);  /* not a number */
67   return 1;
68 }
69
70
71 static int luaB_error (lua_State *L) {
72   int level = luaL_optint(L, 2, 1);
73   lua_settop(L, 1);
74   if (lua_isstring(L, 1) && level > 0) {  /* add extra information? */
75     luaL_where(L, level);
76     lua_pushvalue(L, 1);
77     lua_concat(L, 2);
78   }
79   return lua_error(L);
80 }
81
82
83 static int luaB_getmetatable (lua_State *L) {
84   luaL_checkany(L, 1);
85   if (!lua_getmetatable(L, 1)) {
86     lua_pushnil(L);
87     return 1;  /* no metatable */
88   }
89   luaL_getmetafield(L, 1, "__metatable");
90   return 1;  /* returns either __metatable field (if present) or metatable */
91 }
92
93
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");
101   lua_settop(L, 2);
102   lua_setmetatable(L, 1);
103   return 1;
104 }
105
106
107 static int luaB_rawequal (lua_State *L) {
108   luaL_checkany(L, 1);
109   luaL_checkany(L, 2);
110   lua_pushboolean(L, lua_rawequal(L, 1, 2));
111   return 1;
112 }
113
114
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));
120   return 1;
121 }
122
123
124 static int luaB_rawget (lua_State *L) {
125   luaL_checktype(L, 1, LUA_TTABLE);
126   luaL_checkany(L, 2);
127   lua_settop(L, 2);
128   lua_rawget(L, 1);
129   return 1;
130 }
131
132 static int luaB_rawset (lua_State *L) {
133   luaL_checktype(L, 1, LUA_TTABLE);
134   luaL_checkany(L, 2);
135   luaL_checkany(L, 3);
136   lua_settop(L, 3);
137   lua_rawset(L, 1);
138   return 1;
139 }
140
141
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);
152   switch (o) {
153     case LUA_GCCOUNT: {
154       int b = lua_gc(L, LUA_GCCOUNTB, 0);
155       lua_pushnumber(L, res + ((lua_Number)b/1024));
156       lua_pushinteger(L, b);
157       return 2;
158     }
159     case LUA_GCSTEP: case LUA_GCISRUNNING: {
160       lua_pushboolean(L, res);
161       return 1;
162     }
163     default: {
164       lua_pushinteger(L, res);
165       return 1;
166     }
167   }
168 }
169
170
171 static int luaB_type (lua_State *L) {
172   luaL_checkany(L, 1);
173   lua_pushstring(L, luaL_typename(L, 1));
174   return 1;
175 }
176
177
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 */
185     else lua_pushnil(L);
186   }
187   else {
188     lua_pushvalue(L, 1);  /* argument 'self' to metamethod */
189     lua_call(L, 1, 3);  /* get 3 values from metamethod */
190   }
191   return 3;
192 }
193
194
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 */
198   if (lua_next(L, 1))
199     return 2;
200   else {
201     lua_pushnil(L);
202     return 1;
203   }
204 }
205
206
207 static int luaB_pairs (lua_State *L) {
208   return pairsmeta(L, "__pairs", 0, luaB_next);
209 }
210
211
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;
219 }
220
221
222 static int luaB_ipairs (lua_State *L) {
223   return pairsmeta(L, "__ipairs", 1, ipairsaux);
224 }
225
226
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);
231 }
232
233
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);
238     return 1;
239   }
240   else {
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");
245     return n - i;
246   }
247 }
248
249 static int luaB_tostring (lua_State *L) {
250   luaL_checkany(L, 1);
251   luaL_tolstring(L, 1, NULL);
252   return 1;
253 }
254
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},
263 #endif
264   {"next", luaB_next},
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},
274   {"type", luaB_type},
275   {NULL, NULL}
276 };
277
278
279 LUAMOD_API int luaopen_base (lua_State *L) {
280   /* set global _G */
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 */
288   return 1;
289 }
290
291 #if defined(_KERNEL)
292
293 EXPORT_SYMBOL(luaopen_base);
294
295 #endif
296 /* END CSTYLED */