1*dfc11533SChris Williamson /*
2*dfc11533SChris Williamson ** $Id: lauxlib.c,v 1.248.1.1 2013/04/12 18:48:47 roberto Exp $
3*dfc11533SChris Williamson ** Auxiliary functions for building Lua libraries
4*dfc11533SChris Williamson ** See Copyright Notice in lua.h
5*dfc11533SChris Williamson */
6*dfc11533SChris Williamson
7*dfc11533SChris Williamson
8*dfc11533SChris Williamson #include <sys/zfs_context.h>
9*dfc11533SChris Williamson
10*dfc11533SChris Williamson /* This file uses only the official API of Lua.
11*dfc11533SChris Williamson ** Any function declared here could be written as an application function.
12*dfc11533SChris Williamson */
13*dfc11533SChris Williamson
14*dfc11533SChris Williamson #define lauxlib_c
15*dfc11533SChris Williamson #define LUA_LIB
16*dfc11533SChris Williamson
17*dfc11533SChris Williamson #include "lua.h"
18*dfc11533SChris Williamson
19*dfc11533SChris Williamson #include "lauxlib.h"
20*dfc11533SChris Williamson
21*dfc11533SChris Williamson
22*dfc11533SChris Williamson /*
23*dfc11533SChris Williamson ** {======================================================
24*dfc11533SChris Williamson ** Traceback
25*dfc11533SChris Williamson ** =======================================================
26*dfc11533SChris Williamson */
27*dfc11533SChris Williamson
28*dfc11533SChris Williamson
29*dfc11533SChris Williamson #define LEVELS1 12 /* size of the first part of the stack */
30*dfc11533SChris Williamson #define LEVELS2 10 /* size of the second part of the stack */
31*dfc11533SChris Williamson
32*dfc11533SChris Williamson
33*dfc11533SChris Williamson
34*dfc11533SChris Williamson /*
35*dfc11533SChris Williamson ** search for 'objidx' in table at index -1.
36*dfc11533SChris Williamson ** return 1 + string at top if find a good name.
37*dfc11533SChris Williamson */
findfield(lua_State * L,int objidx,int level)38*dfc11533SChris Williamson static int findfield (lua_State *L, int objidx, int level) {
39*dfc11533SChris Williamson if (level == 0 || !lua_istable(L, -1))
40*dfc11533SChris Williamson return 0; /* not found */
41*dfc11533SChris Williamson lua_pushnil(L); /* start 'next' loop */
42*dfc11533SChris Williamson while (lua_next(L, -2)) { /* for each pair in table */
43*dfc11533SChris Williamson if (lua_type(L, -2) == LUA_TSTRING) { /* ignore non-string keys */
44*dfc11533SChris Williamson if (lua_rawequal(L, objidx, -1)) { /* found object? */
45*dfc11533SChris Williamson lua_pop(L, 1); /* remove value (but keep name) */
46*dfc11533SChris Williamson return 1;
47*dfc11533SChris Williamson }
48*dfc11533SChris Williamson else if (findfield(L, objidx, level - 1)) { /* try recursively */
49*dfc11533SChris Williamson lua_remove(L, -2); /* remove table (but keep name) */
50*dfc11533SChris Williamson lua_pushliteral(L, ".");
51*dfc11533SChris Williamson lua_insert(L, -2); /* place '.' between the two names */
52*dfc11533SChris Williamson lua_concat(L, 3);
53*dfc11533SChris Williamson return 1;
54*dfc11533SChris Williamson }
55*dfc11533SChris Williamson }
56*dfc11533SChris Williamson lua_pop(L, 1); /* remove value */
57*dfc11533SChris Williamson }
58*dfc11533SChris Williamson return 0; /* not found */
59*dfc11533SChris Williamson }
60*dfc11533SChris Williamson
61*dfc11533SChris Williamson
pushglobalfuncname(lua_State * L,lua_Debug * ar)62*dfc11533SChris Williamson static int pushglobalfuncname (lua_State *L, lua_Debug *ar) {
63*dfc11533SChris Williamson int top = lua_gettop(L);
64*dfc11533SChris Williamson lua_getinfo(L, "f", ar); /* push function */
65*dfc11533SChris Williamson lua_pushglobaltable(L);
66*dfc11533SChris Williamson if (findfield(L, top + 1, 2)) {
67*dfc11533SChris Williamson lua_copy(L, -1, top + 1); /* move name to proper place */
68*dfc11533SChris Williamson lua_pop(L, 2); /* remove pushed values */
69*dfc11533SChris Williamson return 1;
70*dfc11533SChris Williamson }
71*dfc11533SChris Williamson else {
72*dfc11533SChris Williamson lua_settop(L, top); /* remove function and global table */
73*dfc11533SChris Williamson return 0;
74*dfc11533SChris Williamson }
75*dfc11533SChris Williamson }
76*dfc11533SChris Williamson
77*dfc11533SChris Williamson
pushfuncname(lua_State * L,lua_Debug * ar)78*dfc11533SChris Williamson static void pushfuncname (lua_State *L, lua_Debug *ar) {
79*dfc11533SChris Williamson if (*ar->namewhat != '\0') /* is there a name? */
80*dfc11533SChris Williamson lua_pushfstring(L, "function " LUA_QS, ar->name);
81*dfc11533SChris Williamson else if (*ar->what == 'm') /* main? */
82*dfc11533SChris Williamson lua_pushliteral(L, "main chunk");
83*dfc11533SChris Williamson else if (*ar->what == 'C') {
84*dfc11533SChris Williamson if (pushglobalfuncname(L, ar)) {
85*dfc11533SChris Williamson lua_pushfstring(L, "function " LUA_QS, lua_tostring(L, -1));
86*dfc11533SChris Williamson lua_remove(L, -2); /* remove name */
87*dfc11533SChris Williamson }
88*dfc11533SChris Williamson else
89*dfc11533SChris Williamson lua_pushliteral(L, "?");
90*dfc11533SChris Williamson }
91*dfc11533SChris Williamson else
92*dfc11533SChris Williamson lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined);
93*dfc11533SChris Williamson }
94*dfc11533SChris Williamson
95*dfc11533SChris Williamson
countlevels(lua_State * L)96*dfc11533SChris Williamson static int countlevels (lua_State *L) {
97*dfc11533SChris Williamson lua_Debug ar;
98*dfc11533SChris Williamson int li = 1, le = 1;
99*dfc11533SChris Williamson /* find an upper bound */
100*dfc11533SChris Williamson while (lua_getstack(L, le, &ar)) { li = le; le *= 2; }
101*dfc11533SChris Williamson /* do a binary search */
102*dfc11533SChris Williamson while (li < le) {
103*dfc11533SChris Williamson int m = (li + le)/2;
104*dfc11533SChris Williamson if (lua_getstack(L, m, &ar)) li = m + 1;
105*dfc11533SChris Williamson else le = m;
106*dfc11533SChris Williamson }
107*dfc11533SChris Williamson return le - 1;
108*dfc11533SChris Williamson }
109*dfc11533SChris Williamson
110*dfc11533SChris Williamson
luaL_traceback(lua_State * L,lua_State * L1,const char * msg,int level)111*dfc11533SChris Williamson LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1,
112*dfc11533SChris Williamson const char *msg, int level) {
113*dfc11533SChris Williamson lua_Debug ar;
114*dfc11533SChris Williamson int top = lua_gettop(L);
115*dfc11533SChris Williamson int numlevels = countlevels(L1);
116*dfc11533SChris Williamson int mark = (numlevels > LEVELS1 + LEVELS2) ? LEVELS1 : 0;
117*dfc11533SChris Williamson if (msg) lua_pushfstring(L, "%s\n", msg);
118*dfc11533SChris Williamson lua_pushliteral(L, "stack traceback:");
119*dfc11533SChris Williamson while (lua_getstack(L1, level++, &ar)) {
120*dfc11533SChris Williamson if (level == mark) { /* too many levels? */
121*dfc11533SChris Williamson lua_pushliteral(L, "\n\t..."); /* add a '...' */
122*dfc11533SChris Williamson level = numlevels - LEVELS2; /* and skip to last ones */
123*dfc11533SChris Williamson }
124*dfc11533SChris Williamson else {
125*dfc11533SChris Williamson lua_getinfo(L1, "Slnt", &ar);
126*dfc11533SChris Williamson lua_pushfstring(L, "\n\t%s:", ar.short_src);
127*dfc11533SChris Williamson if (ar.currentline > 0)
128*dfc11533SChris Williamson lua_pushfstring(L, "%d:", ar.currentline);
129*dfc11533SChris Williamson lua_pushliteral(L, " in ");
130*dfc11533SChris Williamson pushfuncname(L, &ar);
131*dfc11533SChris Williamson if (ar.istailcall)
132*dfc11533SChris Williamson lua_pushliteral(L, "\n\t(...tail calls...)");
133*dfc11533SChris Williamson lua_concat(L, lua_gettop(L) - top);
134*dfc11533SChris Williamson }
135*dfc11533SChris Williamson }
136*dfc11533SChris Williamson lua_concat(L, lua_gettop(L) - top);
137*dfc11533SChris Williamson }
138*dfc11533SChris Williamson
139*dfc11533SChris Williamson /* }====================================================== */
140*dfc11533SChris Williamson
141*dfc11533SChris Williamson
142*dfc11533SChris Williamson /*
143*dfc11533SChris Williamson ** {======================================================
144*dfc11533SChris Williamson ** Error-report functions
145*dfc11533SChris Williamson ** =======================================================
146*dfc11533SChris Williamson */
147*dfc11533SChris Williamson
luaL_argerror(lua_State * L,int narg,const char * extramsg)148*dfc11533SChris Williamson LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) {
149*dfc11533SChris Williamson lua_Debug ar;
150*dfc11533SChris Williamson if (!lua_getstack(L, 0, &ar)) /* no stack frame? */
151*dfc11533SChris Williamson return luaL_error(L, "bad argument #%d (%s)", narg, extramsg);
152*dfc11533SChris Williamson lua_getinfo(L, "n", &ar);
153*dfc11533SChris Williamson if (strcmp(ar.namewhat, "method") == 0) {
154*dfc11533SChris Williamson narg--; /* do not count `self' */
155*dfc11533SChris Williamson if (narg == 0) /* error is in the self argument itself? */
156*dfc11533SChris Williamson return luaL_error(L, "calling " LUA_QS " on bad self (%s)",
157*dfc11533SChris Williamson ar.name, extramsg);
158*dfc11533SChris Williamson }
159*dfc11533SChris Williamson if (ar.name == NULL)
160*dfc11533SChris Williamson ar.name = (pushglobalfuncname(L, &ar)) ? lua_tostring(L, -1) : "?";
161*dfc11533SChris Williamson return luaL_error(L, "bad argument #%d to " LUA_QS " (%s)",
162*dfc11533SChris Williamson narg, ar.name, extramsg);
163*dfc11533SChris Williamson }
164*dfc11533SChris Williamson
165*dfc11533SChris Williamson
typeerror(lua_State * L,int narg,const char * tname)166*dfc11533SChris Williamson static int typeerror (lua_State *L, int narg, const char *tname) {
167*dfc11533SChris Williamson const char *msg = lua_pushfstring(L, "%s expected, got %s",
168*dfc11533SChris Williamson tname, luaL_typename(L, narg));
169*dfc11533SChris Williamson return luaL_argerror(L, narg, msg);
170*dfc11533SChris Williamson }
171*dfc11533SChris Williamson
172*dfc11533SChris Williamson
tag_error(lua_State * L,int narg,int tag)173*dfc11533SChris Williamson static void tag_error (lua_State *L, int narg, int tag) {
174*dfc11533SChris Williamson typeerror(L, narg, lua_typename(L, tag));
175*dfc11533SChris Williamson }
176*dfc11533SChris Williamson
177*dfc11533SChris Williamson
luaL_where(lua_State * L,int level)178*dfc11533SChris Williamson LUALIB_API void luaL_where (lua_State *L, int level) {
179*dfc11533SChris Williamson lua_Debug ar;
180*dfc11533SChris Williamson if (lua_getstack(L, level, &ar)) { /* check function at level */
181*dfc11533SChris Williamson lua_getinfo(L, "Sl", &ar); /* get info about it */
182*dfc11533SChris Williamson if (ar.currentline > 0) { /* is there info? */
183*dfc11533SChris Williamson lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline);
184*dfc11533SChris Williamson return;
185*dfc11533SChris Williamson }
186*dfc11533SChris Williamson }
187*dfc11533SChris Williamson lua_pushliteral(L, ""); /* else, no information available... */
188*dfc11533SChris Williamson }
189*dfc11533SChris Williamson
190*dfc11533SChris Williamson
luaL_error(lua_State * L,const char * fmt,...)191*dfc11533SChris Williamson LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) {
192*dfc11533SChris Williamson va_list argp;
193*dfc11533SChris Williamson va_start(argp, fmt);
194*dfc11533SChris Williamson luaL_where(L, 1);
195*dfc11533SChris Williamson lua_pushvfstring(L, fmt, argp);
196*dfc11533SChris Williamson va_end(argp);
197*dfc11533SChris Williamson lua_concat(L, 2);
198*dfc11533SChris Williamson return lua_error(L);
199*dfc11533SChris Williamson }
200*dfc11533SChris Williamson
201*dfc11533SChris Williamson
202*dfc11533SChris Williamson #if !defined(inspectstat) /* { */
203*dfc11533SChris Williamson
204*dfc11533SChris Williamson #if defined(LUA_USE_POSIX)
205*dfc11533SChris Williamson
206*dfc11533SChris Williamson #include <sys/wait.h>
207*dfc11533SChris Williamson
208*dfc11533SChris Williamson /*
209*dfc11533SChris Williamson ** use appropriate macros to interpret 'pclose' return status
210*dfc11533SChris Williamson */
211*dfc11533SChris Williamson #define inspectstat(stat,what) \
212*dfc11533SChris Williamson if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \
213*dfc11533SChris Williamson else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; }
214*dfc11533SChris Williamson
215*dfc11533SChris Williamson #else
216*dfc11533SChris Williamson
217*dfc11533SChris Williamson #define inspectstat(stat,what) /* no op */
218*dfc11533SChris Williamson
219*dfc11533SChris Williamson #endif
220*dfc11533SChris Williamson
221*dfc11533SChris Williamson #endif /* } */
222*dfc11533SChris Williamson
223*dfc11533SChris Williamson
224*dfc11533SChris Williamson /* }====================================================== */
225*dfc11533SChris Williamson
226*dfc11533SChris Williamson
227*dfc11533SChris Williamson /*
228*dfc11533SChris Williamson ** {======================================================
229*dfc11533SChris Williamson ** Userdata's metatable manipulation
230*dfc11533SChris Williamson ** =======================================================
231*dfc11533SChris Williamson */
232*dfc11533SChris Williamson
luaL_newmetatable(lua_State * L,const char * tname)233*dfc11533SChris Williamson LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {
234*dfc11533SChris Williamson luaL_getmetatable(L, tname); /* try to get metatable */
235*dfc11533SChris Williamson if (!lua_isnil(L, -1)) /* name already in use? */
236*dfc11533SChris Williamson return 0; /* leave previous value on top, but return 0 */
237*dfc11533SChris Williamson lua_pop(L, 1);
238*dfc11533SChris Williamson lua_newtable(L); /* create metatable */
239*dfc11533SChris Williamson lua_pushvalue(L, -1);
240*dfc11533SChris Williamson lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */
241*dfc11533SChris Williamson return 1;
242*dfc11533SChris Williamson }
243*dfc11533SChris Williamson
244*dfc11533SChris Williamson
luaL_setmetatable(lua_State * L,const char * tname)245*dfc11533SChris Williamson LUALIB_API void luaL_setmetatable (lua_State *L, const char *tname) {
246*dfc11533SChris Williamson luaL_getmetatable(L, tname);
247*dfc11533SChris Williamson lua_setmetatable(L, -2);
248*dfc11533SChris Williamson }
249*dfc11533SChris Williamson
250*dfc11533SChris Williamson
luaL_testudata(lua_State * L,int ud,const char * tname)251*dfc11533SChris Williamson LUALIB_API void *luaL_testudata (lua_State *L, int ud, const char *tname) {
252*dfc11533SChris Williamson void *p = lua_touserdata(L, ud);
253*dfc11533SChris Williamson if (p != NULL) { /* value is a userdata? */
254*dfc11533SChris Williamson if (lua_getmetatable(L, ud)) { /* does it have a metatable? */
255*dfc11533SChris Williamson luaL_getmetatable(L, tname); /* get correct metatable */
256*dfc11533SChris Williamson if (!lua_rawequal(L, -1, -2)) /* not the same? */
257*dfc11533SChris Williamson p = NULL; /* value is a userdata with wrong metatable */
258*dfc11533SChris Williamson lua_pop(L, 2); /* remove both metatables */
259*dfc11533SChris Williamson return p;
260*dfc11533SChris Williamson }
261*dfc11533SChris Williamson }
262*dfc11533SChris Williamson return NULL; /* value is not a userdata with a metatable */
263*dfc11533SChris Williamson }
264*dfc11533SChris Williamson
265*dfc11533SChris Williamson
luaL_checkudata(lua_State * L,int ud,const char * tname)266*dfc11533SChris Williamson LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {
267*dfc11533SChris Williamson void *p = luaL_testudata(L, ud, tname);
268*dfc11533SChris Williamson if (p == NULL) typeerror(L, ud, tname);
269*dfc11533SChris Williamson return p;
270*dfc11533SChris Williamson }
271*dfc11533SChris Williamson
272*dfc11533SChris Williamson /* }====================================================== */
273*dfc11533SChris Williamson
274*dfc11533SChris Williamson
275*dfc11533SChris Williamson /*
276*dfc11533SChris Williamson ** {======================================================
277*dfc11533SChris Williamson ** Argument check functions
278*dfc11533SChris Williamson ** =======================================================
279*dfc11533SChris Williamson */
280*dfc11533SChris Williamson
luaL_checkoption(lua_State * L,int narg,const char * def,const char * const lst[])281*dfc11533SChris Williamson LUALIB_API int luaL_checkoption (lua_State *L, int narg, const char *def,
282*dfc11533SChris Williamson const char *const lst[]) {
283*dfc11533SChris Williamson const char *name = (def) ? luaL_optstring(L, narg, def) :
284*dfc11533SChris Williamson luaL_checkstring(L, narg);
285*dfc11533SChris Williamson int i;
286*dfc11533SChris Williamson for (i=0; lst[i]; i++)
287*dfc11533SChris Williamson if (strcmp(lst[i], name) == 0)
288*dfc11533SChris Williamson return i;
289*dfc11533SChris Williamson return luaL_argerror(L, narg,
290*dfc11533SChris Williamson lua_pushfstring(L, "invalid option " LUA_QS, name));
291*dfc11533SChris Williamson }
292*dfc11533SChris Williamson
293*dfc11533SChris Williamson
luaL_checkstack(lua_State * L,int space,const char * msg)294*dfc11533SChris Williamson LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) {
295*dfc11533SChris Williamson /* keep some extra space to run error routines, if needed */
296*dfc11533SChris Williamson const int extra = LUA_MINSTACK;
297*dfc11533SChris Williamson if (!lua_checkstack(L, space + extra)) {
298*dfc11533SChris Williamson if (msg)
299*dfc11533SChris Williamson luaL_error(L, "stack overflow (%s)", msg);
300*dfc11533SChris Williamson else
301*dfc11533SChris Williamson luaL_error(L, "stack overflow");
302*dfc11533SChris Williamson }
303*dfc11533SChris Williamson }
304*dfc11533SChris Williamson
305*dfc11533SChris Williamson
luaL_checktype(lua_State * L,int narg,int t)306*dfc11533SChris Williamson LUALIB_API void luaL_checktype (lua_State *L, int narg, int t) {
307*dfc11533SChris Williamson if (lua_type(L, narg) != t)
308*dfc11533SChris Williamson tag_error(L, narg, t);
309*dfc11533SChris Williamson }
310*dfc11533SChris Williamson
311*dfc11533SChris Williamson
luaL_checkany(lua_State * L,int narg)312*dfc11533SChris Williamson LUALIB_API void luaL_checkany (lua_State *L, int narg) {
313*dfc11533SChris Williamson if (lua_type(L, narg) == LUA_TNONE)
314*dfc11533SChris Williamson luaL_argerror(L, narg, "value expected");
315*dfc11533SChris Williamson }
316*dfc11533SChris Williamson
317*dfc11533SChris Williamson
luaL_checklstring(lua_State * L,int narg,size_t * len)318*dfc11533SChris Williamson LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) {
319*dfc11533SChris Williamson const char *s = lua_tolstring(L, narg, len);
320*dfc11533SChris Williamson if (!s) tag_error(L, narg, LUA_TSTRING);
321*dfc11533SChris Williamson return s;
322*dfc11533SChris Williamson }
323*dfc11533SChris Williamson
324*dfc11533SChris Williamson
luaL_optlstring(lua_State * L,int narg,const char * def,size_t * len)325*dfc11533SChris Williamson LUALIB_API const char *luaL_optlstring (lua_State *L, int narg,
326*dfc11533SChris Williamson const char *def, size_t *len) {
327*dfc11533SChris Williamson if (lua_isnoneornil(L, narg)) {
328*dfc11533SChris Williamson if (len)
329*dfc11533SChris Williamson *len = (def ? strlen(def) : 0);
330*dfc11533SChris Williamson return def;
331*dfc11533SChris Williamson }
332*dfc11533SChris Williamson else return luaL_checklstring(L, narg, len);
333*dfc11533SChris Williamson }
334*dfc11533SChris Williamson
335*dfc11533SChris Williamson
luaL_checknumber(lua_State * L,int narg)336*dfc11533SChris Williamson LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) {
337*dfc11533SChris Williamson int isnum;
338*dfc11533SChris Williamson lua_Number d = lua_tonumberx(L, narg, &isnum);
339*dfc11533SChris Williamson if (!isnum)
340*dfc11533SChris Williamson tag_error(L, narg, LUA_TNUMBER);
341*dfc11533SChris Williamson return d;
342*dfc11533SChris Williamson }
343*dfc11533SChris Williamson
344*dfc11533SChris Williamson
luaL_optnumber(lua_State * L,int narg,lua_Number def)345*dfc11533SChris Williamson LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) {
346*dfc11533SChris Williamson return luaL_opt(L, luaL_checknumber, narg, def);
347*dfc11533SChris Williamson }
348*dfc11533SChris Williamson
349*dfc11533SChris Williamson
luaL_checkinteger(lua_State * L,int narg)350*dfc11533SChris Williamson LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) {
351*dfc11533SChris Williamson int isnum;
352*dfc11533SChris Williamson lua_Integer d = lua_tointegerx(L, narg, &isnum);
353*dfc11533SChris Williamson if (!isnum)
354*dfc11533SChris Williamson tag_error(L, narg, LUA_TNUMBER);
355*dfc11533SChris Williamson return d;
356*dfc11533SChris Williamson }
357*dfc11533SChris Williamson
358*dfc11533SChris Williamson
luaL_checkunsigned(lua_State * L,int narg)359*dfc11533SChris Williamson LUALIB_API lua_Unsigned luaL_checkunsigned (lua_State *L, int narg) {
360*dfc11533SChris Williamson int isnum;
361*dfc11533SChris Williamson lua_Unsigned d = lua_tounsignedx(L, narg, &isnum);
362*dfc11533SChris Williamson if (!isnum)
363*dfc11533SChris Williamson tag_error(L, narg, LUA_TNUMBER);
364*dfc11533SChris Williamson return d;
365*dfc11533SChris Williamson }
366*dfc11533SChris Williamson
367*dfc11533SChris Williamson
luaL_optinteger(lua_State * L,int narg,lua_Integer def)368*dfc11533SChris Williamson LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg,
369*dfc11533SChris Williamson lua_Integer def) {
370*dfc11533SChris Williamson return luaL_opt(L, luaL_checkinteger, narg, def);
371*dfc11533SChris Williamson }
372*dfc11533SChris Williamson
373*dfc11533SChris Williamson
luaL_optunsigned(lua_State * L,int narg,lua_Unsigned def)374*dfc11533SChris Williamson LUALIB_API lua_Unsigned luaL_optunsigned (lua_State *L, int narg,
375*dfc11533SChris Williamson lua_Unsigned def) {
376*dfc11533SChris Williamson return luaL_opt(L, luaL_checkunsigned, narg, def);
377*dfc11533SChris Williamson }
378*dfc11533SChris Williamson
379*dfc11533SChris Williamson /* }====================================================== */
380*dfc11533SChris Williamson
381*dfc11533SChris Williamson
382*dfc11533SChris Williamson /*
383*dfc11533SChris Williamson ** {======================================================
384*dfc11533SChris Williamson ** Generic Buffer manipulation
385*dfc11533SChris Williamson ** =======================================================
386*dfc11533SChris Williamson */
387*dfc11533SChris Williamson
388*dfc11533SChris Williamson /*
389*dfc11533SChris Williamson ** check whether buffer is using a userdata on the stack as a temporary
390*dfc11533SChris Williamson ** buffer
391*dfc11533SChris Williamson */
392*dfc11533SChris Williamson #define buffonstack(B) ((B)->b != (B)->initb)
393*dfc11533SChris Williamson
394*dfc11533SChris Williamson
395*dfc11533SChris Williamson /*
396*dfc11533SChris Williamson ** returns a pointer to a free area with at least 'sz' bytes
397*dfc11533SChris Williamson */
luaL_prepbuffsize(luaL_Buffer * B,size_t sz)398*dfc11533SChris Williamson LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) {
399*dfc11533SChris Williamson lua_State *L = B->L;
400*dfc11533SChris Williamson if (B->size - B->n < sz) { /* not enough space? */
401*dfc11533SChris Williamson char *newbuff;
402*dfc11533SChris Williamson size_t newsize = B->size * 2; /* double buffer size */
403*dfc11533SChris Williamson if (newsize - B->n < sz) /* not big enough? */
404*dfc11533SChris Williamson newsize = B->n + sz;
405*dfc11533SChris Williamson if (newsize < B->n || newsize - B->n < sz)
406*dfc11533SChris Williamson luaL_error(L, "buffer too large");
407*dfc11533SChris Williamson /* create larger buffer */
408*dfc11533SChris Williamson newbuff = (char *)lua_newuserdata(L, newsize * sizeof(char));
409*dfc11533SChris Williamson /* move content to new buffer */
410*dfc11533SChris Williamson memcpy(newbuff, B->b, B->n * sizeof(char));
411*dfc11533SChris Williamson if (buffonstack(B))
412*dfc11533SChris Williamson lua_remove(L, -2); /* remove old buffer */
413*dfc11533SChris Williamson B->b = newbuff;
414*dfc11533SChris Williamson B->size = newsize;
415*dfc11533SChris Williamson }
416*dfc11533SChris Williamson return &B->b[B->n];
417*dfc11533SChris Williamson }
418*dfc11533SChris Williamson
419*dfc11533SChris Williamson
luaL_addlstring(luaL_Buffer * B,const char * s,size_t l)420*dfc11533SChris Williamson LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
421*dfc11533SChris Williamson char *b = luaL_prepbuffsize(B, l);
422*dfc11533SChris Williamson memcpy(b, s, l * sizeof(char));
423*dfc11533SChris Williamson luaL_addsize(B, l);
424*dfc11533SChris Williamson }
425*dfc11533SChris Williamson
426*dfc11533SChris Williamson
luaL_addstring(luaL_Buffer * B,const char * s)427*dfc11533SChris Williamson LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
428*dfc11533SChris Williamson luaL_addlstring(B, s, strlen(s));
429*dfc11533SChris Williamson }
430*dfc11533SChris Williamson
431*dfc11533SChris Williamson
luaL_pushresult(luaL_Buffer * B)432*dfc11533SChris Williamson LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
433*dfc11533SChris Williamson lua_State *L = B->L;
434*dfc11533SChris Williamson lua_pushlstring(L, B->b, B->n);
435*dfc11533SChris Williamson if (buffonstack(B))
436*dfc11533SChris Williamson lua_remove(L, -2); /* remove old buffer */
437*dfc11533SChris Williamson }
438*dfc11533SChris Williamson
439*dfc11533SChris Williamson
luaL_pushresultsize(luaL_Buffer * B,size_t sz)440*dfc11533SChris Williamson LUALIB_API void luaL_pushresultsize (luaL_Buffer *B, size_t sz) {
441*dfc11533SChris Williamson luaL_addsize(B, sz);
442*dfc11533SChris Williamson luaL_pushresult(B);
443*dfc11533SChris Williamson }
444*dfc11533SChris Williamson
445*dfc11533SChris Williamson
luaL_addvalue(luaL_Buffer * B)446*dfc11533SChris Williamson LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
447*dfc11533SChris Williamson lua_State *L = B->L;
448*dfc11533SChris Williamson size_t l;
449*dfc11533SChris Williamson const char *s = lua_tolstring(L, -1, &l);
450*dfc11533SChris Williamson if (buffonstack(B))
451*dfc11533SChris Williamson lua_insert(L, -2); /* put value below buffer */
452*dfc11533SChris Williamson luaL_addlstring(B, s, l);
453*dfc11533SChris Williamson lua_remove(L, (buffonstack(B)) ? -2 : -1); /* remove value */
454*dfc11533SChris Williamson }
455*dfc11533SChris Williamson
456*dfc11533SChris Williamson
luaL_buffinit(lua_State * L,luaL_Buffer * B)457*dfc11533SChris Williamson LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
458*dfc11533SChris Williamson B->L = L;
459*dfc11533SChris Williamson B->b = B->initb;
460*dfc11533SChris Williamson B->n = 0;
461*dfc11533SChris Williamson B->size = LUAL_BUFFERSIZE;
462*dfc11533SChris Williamson }
463*dfc11533SChris Williamson
464*dfc11533SChris Williamson
luaL_buffinitsize(lua_State * L,luaL_Buffer * B,size_t sz)465*dfc11533SChris Williamson LUALIB_API char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz) {
466*dfc11533SChris Williamson luaL_buffinit(L, B);
467*dfc11533SChris Williamson return luaL_prepbuffsize(B, sz);
468*dfc11533SChris Williamson }
469*dfc11533SChris Williamson
470*dfc11533SChris Williamson /* }====================================================== */
471*dfc11533SChris Williamson
472*dfc11533SChris Williamson
473*dfc11533SChris Williamson /*
474*dfc11533SChris Williamson ** {======================================================
475*dfc11533SChris Williamson ** Reference system
476*dfc11533SChris Williamson ** =======================================================
477*dfc11533SChris Williamson */
478*dfc11533SChris Williamson
479*dfc11533SChris Williamson /* index of free-list header */
480*dfc11533SChris Williamson #define freelist 0
481*dfc11533SChris Williamson
482*dfc11533SChris Williamson
luaL_ref(lua_State * L,int t)483*dfc11533SChris Williamson LUALIB_API int luaL_ref (lua_State *L, int t) {
484*dfc11533SChris Williamson int ref;
485*dfc11533SChris Williamson if (lua_isnil(L, -1)) {
486*dfc11533SChris Williamson lua_pop(L, 1); /* remove from stack */
487*dfc11533SChris Williamson return LUA_REFNIL; /* `nil' has a unique fixed reference */
488*dfc11533SChris Williamson }
489*dfc11533SChris Williamson t = lua_absindex(L, t);
490*dfc11533SChris Williamson lua_rawgeti(L, t, freelist); /* get first free element */
491*dfc11533SChris Williamson ref = (int)lua_tointeger(L, -1); /* ref = t[freelist] */
492*dfc11533SChris Williamson lua_pop(L, 1); /* remove it from stack */
493*dfc11533SChris Williamson if (ref != 0) { /* any free element? */
494*dfc11533SChris Williamson lua_rawgeti(L, t, ref); /* remove it from list */
495*dfc11533SChris Williamson lua_rawseti(L, t, freelist); /* (t[freelist] = t[ref]) */
496*dfc11533SChris Williamson }
497*dfc11533SChris Williamson else /* no free elements */
498*dfc11533SChris Williamson ref = (int)lua_rawlen(L, t) + 1; /* get a new reference */
499*dfc11533SChris Williamson lua_rawseti(L, t, ref);
500*dfc11533SChris Williamson return ref;
501*dfc11533SChris Williamson }
502*dfc11533SChris Williamson
503*dfc11533SChris Williamson
luaL_unref(lua_State * L,int t,int ref)504*dfc11533SChris Williamson LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
505*dfc11533SChris Williamson if (ref >= 0) {
506*dfc11533SChris Williamson t = lua_absindex(L, t);
507*dfc11533SChris Williamson lua_rawgeti(L, t, freelist);
508*dfc11533SChris Williamson lua_rawseti(L, t, ref); /* t[ref] = t[freelist] */
509*dfc11533SChris Williamson lua_pushinteger(L, ref);
510*dfc11533SChris Williamson lua_rawseti(L, t, freelist); /* t[freelist] = ref */
511*dfc11533SChris Williamson }
512*dfc11533SChris Williamson }
513*dfc11533SChris Williamson
514*dfc11533SChris Williamson /* }====================================================== */
515*dfc11533SChris Williamson
516*dfc11533SChris Williamson
517*dfc11533SChris Williamson /*
518*dfc11533SChris Williamson ** {======================================================
519*dfc11533SChris Williamson ** Load functions
520*dfc11533SChris Williamson ** =======================================================
521*dfc11533SChris Williamson */
522*dfc11533SChris Williamson
523*dfc11533SChris Williamson typedef struct LoadS {
524*dfc11533SChris Williamson const char *s;
525*dfc11533SChris Williamson size_t size;
526*dfc11533SChris Williamson } LoadS;
527*dfc11533SChris Williamson
528*dfc11533SChris Williamson
getS(lua_State * L,void * ud,size_t * size)529*dfc11533SChris Williamson static const char *getS (lua_State *L, void *ud, size_t *size) {
530*dfc11533SChris Williamson LoadS *ls = (LoadS *)ud;
531*dfc11533SChris Williamson (void)L; /* not used */
532*dfc11533SChris Williamson if (ls->size == 0) return NULL;
533*dfc11533SChris Williamson *size = ls->size;
534*dfc11533SChris Williamson ls->size = 0;
535*dfc11533SChris Williamson return ls->s;
536*dfc11533SChris Williamson }
537*dfc11533SChris Williamson
538*dfc11533SChris Williamson
luaL_loadbufferx(lua_State * L,const char * buff,size_t size,const char * name,const char * mode)539*dfc11533SChris Williamson LUALIB_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t size,
540*dfc11533SChris Williamson const char *name, const char *mode) {
541*dfc11533SChris Williamson LoadS ls;
542*dfc11533SChris Williamson ls.s = buff;
543*dfc11533SChris Williamson ls.size = size;
544*dfc11533SChris Williamson return lua_load(L, getS, &ls, name, mode);
545*dfc11533SChris Williamson }
546*dfc11533SChris Williamson
547*dfc11533SChris Williamson
luaL_loadstring(lua_State * L,const char * s)548*dfc11533SChris Williamson LUALIB_API int luaL_loadstring (lua_State *L, const char *s) {
549*dfc11533SChris Williamson return luaL_loadbuffer(L, s, strlen(s), s);
550*dfc11533SChris Williamson }
551*dfc11533SChris Williamson
552*dfc11533SChris Williamson /* }====================================================== */
553*dfc11533SChris Williamson
554*dfc11533SChris Williamson
555*dfc11533SChris Williamson
luaL_getmetafield(lua_State * L,int obj,const char * event)556*dfc11533SChris Williamson LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
557*dfc11533SChris Williamson if (!lua_getmetatable(L, obj)) /* no metatable? */
558*dfc11533SChris Williamson return 0;
559*dfc11533SChris Williamson lua_pushstring(L, event);
560*dfc11533SChris Williamson lua_rawget(L, -2);
561*dfc11533SChris Williamson if (lua_isnil(L, -1)) {
562*dfc11533SChris Williamson lua_pop(L, 2); /* remove metatable and metafield */
563*dfc11533SChris Williamson return 0;
564*dfc11533SChris Williamson }
565*dfc11533SChris Williamson else {
566*dfc11533SChris Williamson lua_remove(L, -2); /* remove only metatable */
567*dfc11533SChris Williamson return 1;
568*dfc11533SChris Williamson }
569*dfc11533SChris Williamson }
570*dfc11533SChris Williamson
571*dfc11533SChris Williamson
luaL_callmeta(lua_State * L,int obj,const char * event)572*dfc11533SChris Williamson LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
573*dfc11533SChris Williamson obj = lua_absindex(L, obj);
574*dfc11533SChris Williamson if (!luaL_getmetafield(L, obj, event)) /* no metafield? */
575*dfc11533SChris Williamson return 0;
576*dfc11533SChris Williamson lua_pushvalue(L, obj);
577*dfc11533SChris Williamson lua_call(L, 1, 1);
578*dfc11533SChris Williamson return 1;
579*dfc11533SChris Williamson }
580*dfc11533SChris Williamson
581*dfc11533SChris Williamson
luaL_len(lua_State * L,int idx)582*dfc11533SChris Williamson LUALIB_API int luaL_len (lua_State *L, int idx) {
583*dfc11533SChris Williamson int l;
584*dfc11533SChris Williamson int isnum;
585*dfc11533SChris Williamson lua_len(L, idx);
586*dfc11533SChris Williamson l = (int)lua_tointegerx(L, -1, &isnum);
587*dfc11533SChris Williamson if (!isnum)
588*dfc11533SChris Williamson luaL_error(L, "object length is not a number");
589*dfc11533SChris Williamson lua_pop(L, 1); /* remove object */
590*dfc11533SChris Williamson return l;
591*dfc11533SChris Williamson }
592*dfc11533SChris Williamson
593*dfc11533SChris Williamson
luaL_tolstring(lua_State * L,int idx,size_t * len)594*dfc11533SChris Williamson LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
595*dfc11533SChris Williamson if (!luaL_callmeta(L, idx, "__tostring")) { /* no metafield? */
596*dfc11533SChris Williamson switch (lua_type(L, idx)) {
597*dfc11533SChris Williamson case LUA_TNUMBER:
598*dfc11533SChris Williamson case LUA_TSTRING:
599*dfc11533SChris Williamson lua_pushvalue(L, idx);
600*dfc11533SChris Williamson break;
601*dfc11533SChris Williamson case LUA_TBOOLEAN:
602*dfc11533SChris Williamson lua_pushstring(L, (lua_toboolean(L, idx) ? "true" : "false"));
603*dfc11533SChris Williamson break;
604*dfc11533SChris Williamson case LUA_TNIL:
605*dfc11533SChris Williamson lua_pushliteral(L, "nil");
606*dfc11533SChris Williamson break;
607*dfc11533SChris Williamson default:
608*dfc11533SChris Williamson lua_pushfstring(L, "%s: %p", luaL_typename(L, idx),
609*dfc11533SChris Williamson lua_topointer(L, idx));
610*dfc11533SChris Williamson break;
611*dfc11533SChris Williamson }
612*dfc11533SChris Williamson }
613*dfc11533SChris Williamson return lua_tolstring(L, -1, len);
614*dfc11533SChris Williamson }
615*dfc11533SChris Williamson
616*dfc11533SChris Williamson
617*dfc11533SChris Williamson /*
618*dfc11533SChris Williamson ** {======================================================
619*dfc11533SChris Williamson ** Compatibility with 5.1 module functions
620*dfc11533SChris Williamson ** =======================================================
621*dfc11533SChris Williamson */
622*dfc11533SChris Williamson #if defined(LUA_COMPAT_MODULE)
623*dfc11533SChris Williamson
luaL_findtable(lua_State * L,int idx,const char * fname,int szhint)624*dfc11533SChris Williamson static const char *luaL_findtable (lua_State *L, int idx,
625*dfc11533SChris Williamson const char *fname, int szhint) {
626*dfc11533SChris Williamson const char *e;
627*dfc11533SChris Williamson if (idx) lua_pushvalue(L, idx);
628*dfc11533SChris Williamson do {
629*dfc11533SChris Williamson e = strchr(fname, '.');
630*dfc11533SChris Williamson if (e == NULL) e = fname + strlen(fname);
631*dfc11533SChris Williamson lua_pushlstring(L, fname, e - fname);
632*dfc11533SChris Williamson lua_rawget(L, -2);
633*dfc11533SChris Williamson if (lua_isnil(L, -1)) { /* no such field? */
634*dfc11533SChris Williamson lua_pop(L, 1); /* remove this nil */
635*dfc11533SChris Williamson lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */
636*dfc11533SChris Williamson lua_pushlstring(L, fname, e - fname);
637*dfc11533SChris Williamson lua_pushvalue(L, -2);
638*dfc11533SChris Williamson lua_settable(L, -4); /* set new table into field */
639*dfc11533SChris Williamson }
640*dfc11533SChris Williamson else if (!lua_istable(L, -1)) { /* field has a non-table value? */
641*dfc11533SChris Williamson lua_pop(L, 2); /* remove table and value */
642*dfc11533SChris Williamson return fname; /* return problematic part of the name */
643*dfc11533SChris Williamson }
644*dfc11533SChris Williamson lua_remove(L, -2); /* remove previous table */
645*dfc11533SChris Williamson fname = e + 1;
646*dfc11533SChris Williamson } while (*e == '.');
647*dfc11533SChris Williamson return NULL;
648*dfc11533SChris Williamson }
649*dfc11533SChris Williamson
650*dfc11533SChris Williamson
651*dfc11533SChris Williamson /*
652*dfc11533SChris Williamson ** Count number of elements in a luaL_Reg list.
653*dfc11533SChris Williamson */
libsize(const luaL_Reg * l)654*dfc11533SChris Williamson static int libsize (const luaL_Reg *l) {
655*dfc11533SChris Williamson int size = 0;
656*dfc11533SChris Williamson for (; l && l->name; l++) size++;
657*dfc11533SChris Williamson return size;
658*dfc11533SChris Williamson }
659*dfc11533SChris Williamson
660*dfc11533SChris Williamson
661*dfc11533SChris Williamson /*
662*dfc11533SChris Williamson ** Find or create a module table with a given name. The function
663*dfc11533SChris Williamson ** first looks at the _LOADED table and, if that fails, try a
664*dfc11533SChris Williamson ** global variable with that name. In any case, leaves on the stack
665*dfc11533SChris Williamson ** the module table.
666*dfc11533SChris Williamson */
luaL_pushmodule(lua_State * L,const char * modname,int sizehint)667*dfc11533SChris Williamson LUALIB_API void luaL_pushmodule (lua_State *L, const char *modname,
668*dfc11533SChris Williamson int sizehint) {
669*dfc11533SChris Williamson luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); /* get _LOADED table */
670*dfc11533SChris Williamson lua_getfield(L, -1, modname); /* get _LOADED[modname] */
671*dfc11533SChris Williamson if (!lua_istable(L, -1)) { /* not found? */
672*dfc11533SChris Williamson lua_pop(L, 1); /* remove previous result */
673*dfc11533SChris Williamson /* try global variable (and create one if it does not exist) */
674*dfc11533SChris Williamson lua_pushglobaltable(L);
675*dfc11533SChris Williamson if (luaL_findtable(L, 0, modname, sizehint) != NULL)
676*dfc11533SChris Williamson luaL_error(L, "name conflict for module " LUA_QS, modname);
677*dfc11533SChris Williamson lua_pushvalue(L, -1);
678*dfc11533SChris Williamson lua_setfield(L, -3, modname); /* _LOADED[modname] = new table */
679*dfc11533SChris Williamson }
680*dfc11533SChris Williamson lua_remove(L, -2); /* remove _LOADED table */
681*dfc11533SChris Williamson }
682*dfc11533SChris Williamson
683*dfc11533SChris Williamson
luaL_openlib(lua_State * L,const char * libname,const luaL_Reg * l,int nup)684*dfc11533SChris Williamson LUALIB_API void luaL_openlib (lua_State *L, const char *libname,
685*dfc11533SChris Williamson const luaL_Reg *l, int nup) {
686*dfc11533SChris Williamson luaL_checkversion(L);
687*dfc11533SChris Williamson if (libname) {
688*dfc11533SChris Williamson luaL_pushmodule(L, libname, libsize(l)); /* get/create library table */
689*dfc11533SChris Williamson lua_insert(L, -(nup + 1)); /* move library table to below upvalues */
690*dfc11533SChris Williamson }
691*dfc11533SChris Williamson if (l)
692*dfc11533SChris Williamson luaL_setfuncs(L, l, nup);
693*dfc11533SChris Williamson else
694*dfc11533SChris Williamson lua_pop(L, nup); /* remove upvalues */
695*dfc11533SChris Williamson }
696*dfc11533SChris Williamson
697*dfc11533SChris Williamson #endif
698*dfc11533SChris Williamson /* }====================================================== */
699*dfc11533SChris Williamson
700*dfc11533SChris Williamson /*
701*dfc11533SChris Williamson ** set functions from list 'l' into table at top - 'nup'; each
702*dfc11533SChris Williamson ** function gets the 'nup' elements at the top as upvalues.
703*dfc11533SChris Williamson ** Returns with only the table at the stack.
704*dfc11533SChris Williamson */
luaL_setfuncs(lua_State * L,const luaL_Reg * l,int nup)705*dfc11533SChris Williamson LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
706*dfc11533SChris Williamson luaL_checkversion(L);
707*dfc11533SChris Williamson luaL_checkstack(L, nup, "too many upvalues");
708*dfc11533SChris Williamson for (; l->name != NULL; l++) { /* fill the table with given functions */
709*dfc11533SChris Williamson int i;
710*dfc11533SChris Williamson for (i = 0; i < nup; i++) /* copy upvalues to the top */
711*dfc11533SChris Williamson lua_pushvalue(L, -nup);
712*dfc11533SChris Williamson lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
713*dfc11533SChris Williamson lua_setfield(L, -(nup + 2), l->name);
714*dfc11533SChris Williamson }
715*dfc11533SChris Williamson lua_pop(L, nup); /* remove upvalues */
716*dfc11533SChris Williamson }
717*dfc11533SChris Williamson
718*dfc11533SChris Williamson
719*dfc11533SChris Williamson /*
720*dfc11533SChris Williamson ** ensure that stack[idx][fname] has a table and push that table
721*dfc11533SChris Williamson ** into the stack
722*dfc11533SChris Williamson */
luaL_getsubtable(lua_State * L,int idx,const char * fname)723*dfc11533SChris Williamson LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) {
724*dfc11533SChris Williamson lua_getfield(L, idx, fname);
725*dfc11533SChris Williamson if (lua_istable(L, -1)) return 1; /* table already there */
726*dfc11533SChris Williamson else {
727*dfc11533SChris Williamson lua_pop(L, 1); /* remove previous result */
728*dfc11533SChris Williamson idx = lua_absindex(L, idx);
729*dfc11533SChris Williamson lua_newtable(L);
730*dfc11533SChris Williamson lua_pushvalue(L, -1); /* copy to be left at top */
731*dfc11533SChris Williamson lua_setfield(L, idx, fname); /* assign new table to field */
732*dfc11533SChris Williamson return 0; /* false, because did not find table there */
733*dfc11533SChris Williamson }
734*dfc11533SChris Williamson }
735*dfc11533SChris Williamson
736*dfc11533SChris Williamson
737*dfc11533SChris Williamson /*
738*dfc11533SChris Williamson ** stripped-down 'require'. Calls 'openf' to open a module,
739*dfc11533SChris Williamson ** registers the result in 'package.loaded' table and, if 'glb'
740*dfc11533SChris Williamson ** is true, also registers the result in the global table.
741*dfc11533SChris Williamson ** Leaves resulting module on the top.
742*dfc11533SChris Williamson */
luaL_requiref(lua_State * L,const char * modname,lua_CFunction openf,int glb)743*dfc11533SChris Williamson LUALIB_API void luaL_requiref (lua_State *L, const char *modname,
744*dfc11533SChris Williamson lua_CFunction openf, int glb) {
745*dfc11533SChris Williamson lua_pushcfunction(L, openf);
746*dfc11533SChris Williamson lua_pushstring(L, modname); /* argument to open function */
747*dfc11533SChris Williamson lua_call(L, 1, 1); /* open module */
748*dfc11533SChris Williamson luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED");
749*dfc11533SChris Williamson lua_pushvalue(L, -2); /* make copy of module (call result) */
750*dfc11533SChris Williamson lua_setfield(L, -2, modname); /* _LOADED[modname] = module */
751*dfc11533SChris Williamson lua_pop(L, 1); /* remove _LOADED table */
752*dfc11533SChris Williamson if (glb) {
753*dfc11533SChris Williamson lua_pushvalue(L, -1); /* copy of 'mod' */
754*dfc11533SChris Williamson lua_setglobal(L, modname); /* _G[modname] = module */
755*dfc11533SChris Williamson }
756*dfc11533SChris Williamson }
757*dfc11533SChris Williamson
758*dfc11533SChris Williamson
luaL_gsub(lua_State * L,const char * s,const char * p,const char * r)759*dfc11533SChris Williamson LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
760*dfc11533SChris Williamson const char *r) {
761*dfc11533SChris Williamson const char *wild;
762*dfc11533SChris Williamson size_t l = strlen(p);
763*dfc11533SChris Williamson luaL_Buffer b;
764*dfc11533SChris Williamson luaL_buffinit(L, &b);
765*dfc11533SChris Williamson while ((wild = strstr(s, p)) != NULL) {
766*dfc11533SChris Williamson luaL_addlstring(&b, s, wild - s); /* push prefix */
767*dfc11533SChris Williamson luaL_addstring(&b, r); /* push replacement in place of pattern */
768*dfc11533SChris Williamson s = wild + l; /* continue after `p' */
769*dfc11533SChris Williamson }
770*dfc11533SChris Williamson luaL_addstring(&b, s); /* push last suffix */
771*dfc11533SChris Williamson luaL_pushresult(&b);
772*dfc11533SChris Williamson return lua_tostring(L, -1);
773*dfc11533SChris Williamson }
774*dfc11533SChris Williamson
775*dfc11533SChris Williamson
luaL_checkversion_(lua_State * L,lua_Number ver)776*dfc11533SChris Williamson LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver) {
777*dfc11533SChris Williamson const lua_Number *v = lua_version(L);
778*dfc11533SChris Williamson if (v != lua_version(NULL))
779*dfc11533SChris Williamson luaL_error(L, "multiple Lua VMs detected");
780*dfc11533SChris Williamson else if (*v != ver)
781*dfc11533SChris Williamson luaL_error(L, "version mismatch: app. needs %f, Lua core provides %f",
782*dfc11533SChris Williamson ver, *v);
783*dfc11533SChris Williamson /* check conversions number -> integer types */
784*dfc11533SChris Williamson lua_pushnumber(L, -(lua_Number)0x1234);
785*dfc11533SChris Williamson if (lua_tointeger(L, -1) != -0x1234 ||
786*dfc11533SChris Williamson lua_tounsigned(L, -1) != (lua_Unsigned)-0x1234)
787*dfc11533SChris Williamson luaL_error(L, "bad conversion number->int;"
788*dfc11533SChris Williamson " must recompile Lua with proper settings");
789*dfc11533SChris Williamson lua_pop(L, 1);
790*dfc11533SChris Williamson }
791*dfc11533SChris Williamson
792