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