]>
git.proxmox.com Git - mirror_zfs.git/blob - module/lua/lauxlib.c
3 ** $Id: lauxlib.c,v 1.248.1.1 2013/04/12 18:48:47 roberto Exp $
4 ** Auxiliary functions for building Lua libraries
5 ** See Copyright Notice in lua.h
9 /* This file uses only the official API of Lua.
10 ** Any function declared here could be written as an application function.
16 #include <sys/lua/lua.h>
18 #include <sys/lua/lauxlib.h>
22 ** {======================================================
24 ** =======================================================
28 #define LEVELS1 12 /* size of the first part of the stack */
29 #define LEVELS2 10 /* size of the second part of the stack */
34 ** search for 'objidx' in table at index -1.
35 ** return 1 + string at top if find a good name.
37 static int findfield (lua_State
*L
, int objidx
, int level
) {
38 if (level
== 0 || !lua_istable(L
, -1))
39 return 0; /* not found */
40 lua_pushnil(L
); /* start 'next' loop */
41 while (lua_next(L
, -2)) { /* for each pair in table */
42 if (lua_type(L
, -2) == LUA_TSTRING
) { /* ignore non-string keys */
43 if (lua_rawequal(L
, objidx
, -1)) { /* found object? */
44 lua_pop(L
, 1); /* remove value (but keep name) */
47 else if (findfield(L
, objidx
, level
- 1)) { /* try recursively */
48 lua_remove(L
, -2); /* remove table (but keep name) */
49 lua_pushliteral(L
, ".");
50 lua_insert(L
, -2); /* place '.' between the two names */
55 lua_pop(L
, 1); /* remove value */
57 return 0; /* not found */
61 static int pushglobalfuncname (lua_State
*L
, lua_Debug
*ar
) {
62 int top
= lua_gettop(L
);
63 lua_getinfo(L
, "f", ar
); /* push function */
64 lua_pushglobaltable(L
);
65 if (findfield(L
, top
+ 1, 2)) {
66 lua_copy(L
, -1, top
+ 1); /* move name to proper place */
67 lua_pop(L
, 2); /* remove pushed values */
71 lua_settop(L
, top
); /* remove function and global table */
77 static void pushfuncname (lua_State
*L
, lua_Debug
*ar
) {
78 if (*ar
->namewhat
!= '\0') /* is there a name? */
79 lua_pushfstring(L
, "function " LUA_QS
, ar
->name
);
80 else if (*ar
->what
== 'm') /* main? */
81 lua_pushliteral(L
, "main chunk");
82 else if (*ar
->what
== 'C') {
83 if (pushglobalfuncname(L
, ar
)) {
84 lua_pushfstring(L
, "function " LUA_QS
, lua_tostring(L
, -1));
85 lua_remove(L
, -2); /* remove name */
88 lua_pushliteral(L
, "?");
91 lua_pushfstring(L
, "function <%s:%d>", ar
->short_src
, ar
->linedefined
);
95 static int countlevels (lua_State
*L
) {
98 /* find an upper bound */
99 while (lua_getstack(L
, le
, &ar
)) { li
= le
; le
*= 2; }
100 /* do a binary search */
103 if (lua_getstack(L
, m
, &ar
)) li
= m
+ 1;
110 LUALIB_API
void luaL_traceback (lua_State
*L
, lua_State
*L1
,
111 const char *msg
, int level
) {
113 int top
= lua_gettop(L
);
114 int numlevels
= countlevels(L1
);
115 int mark
= (numlevels
> LEVELS1
+ LEVELS2
) ? LEVELS1
: 0;
116 if (msg
) lua_pushfstring(L
, "%s\n", msg
);
117 lua_pushliteral(L
, "stack traceback:");
118 while (lua_getstack(L1
, level
++, &ar
)) {
119 if (level
== mark
) { /* too many levels? */
120 lua_pushliteral(L
, "\n\t..."); /* add a '...' */
121 level
= numlevels
- LEVELS2
; /* and skip to last ones */
124 lua_getinfo(L1
, "Slnt", &ar
);
125 lua_pushfstring(L
, "\n\t%s:", ar
.short_src
);
126 if (ar
.currentline
> 0)
127 lua_pushfstring(L
, "%d:", ar
.currentline
);
128 lua_pushliteral(L
, " in ");
129 pushfuncname(L
, &ar
);
131 lua_pushliteral(L
, "\n\t(...tail calls...)");
132 lua_concat(L
, lua_gettop(L
) - top
);
135 lua_concat(L
, lua_gettop(L
) - top
);
138 /* }====================================================== */
142 ** {======================================================
143 ** Error-report functions
144 ** =======================================================
147 LUALIB_API
int luaL_argerror (lua_State
*L
, int narg
, const char *extramsg
) {
149 if (!lua_getstack(L
, 0, &ar
)) /* no stack frame? */
150 return luaL_error(L
, "bad argument #%d (%s)", narg
, extramsg
);
151 lua_getinfo(L
, "n", &ar
);
152 if (strcmp(ar
.namewhat
, "method") == 0) {
153 narg
--; /* do not count `self' */
154 if (narg
== 0) /* error is in the self argument itself? */
155 return luaL_error(L
, "calling " LUA_QS
" on bad self (%s)",
159 ar
.name
= (pushglobalfuncname(L
, &ar
)) ? lua_tostring(L
, -1) : "?";
160 return luaL_error(L
, "bad argument #%d to " LUA_QS
" (%s)",
161 narg
, ar
.name
, extramsg
);
165 static int typeerror (lua_State
*L
, int narg
, const char *tname
) {
166 const char *msg
= lua_pushfstring(L
, "%s expected, got %s",
167 tname
, luaL_typename(L
, narg
));
168 return luaL_argerror(L
, narg
, msg
);
172 static void tag_error (lua_State
*L
, int narg
, int tag
) {
173 typeerror(L
, narg
, lua_typename(L
, tag
));
177 LUALIB_API
void luaL_where (lua_State
*L
, int level
) {
179 if (lua_getstack(L
, level
, &ar
)) { /* check function at level */
180 lua_getinfo(L
, "Sl", &ar
); /* get info about it */
181 if (ar
.currentline
> 0) { /* is there info? */
182 lua_pushfstring(L
, "%s:%d: ", ar
.short_src
, ar
.currentline
);
186 lua_pushliteral(L
, ""); /* else, no information available... */
190 LUALIB_API
int luaL_error (lua_State
*L
, const char *fmt
, ...) {
194 lua_pushvfstring(L
, fmt
, argp
);
201 #if !defined(inspectstat) /* { */
203 #if defined(LUA_USE_POSIX)
205 #include <sys/wait.h>
208 ** use appropriate macros to interpret 'pclose' return status
210 #define inspectstat(stat,what) \
211 if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \
212 else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; }
216 #define inspectstat(stat,what) /* no op */
223 /* }====================================================== */
227 ** {======================================================
228 ** Userdata's metatable manipulation
229 ** =======================================================
232 LUALIB_API
int luaL_newmetatable (lua_State
*L
, const char *tname
) {
233 luaL_getmetatable(L
, tname
); /* try to get metatable */
234 if (!lua_isnil(L
, -1)) /* name already in use? */
235 return 0; /* leave previous value on top, but return 0 */
237 lua_newtable(L
); /* create metatable */
238 lua_pushvalue(L
, -1);
239 lua_setfield(L
, LUA_REGISTRYINDEX
, tname
); /* registry.name = metatable */
244 LUALIB_API
void luaL_setmetatable (lua_State
*L
, const char *tname
) {
245 luaL_getmetatable(L
, tname
);
246 lua_setmetatable(L
, -2);
250 LUALIB_API
void *luaL_testudata (lua_State
*L
, int ud
, const char *tname
) {
251 void *p
= lua_touserdata(L
, ud
);
252 if (p
!= NULL
) { /* value is a userdata? */
253 if (lua_getmetatable(L
, ud
)) { /* does it have a metatable? */
254 luaL_getmetatable(L
, tname
); /* get correct metatable */
255 if (!lua_rawequal(L
, -1, -2)) /* not the same? */
256 p
= NULL
; /* value is a userdata with wrong metatable */
257 lua_pop(L
, 2); /* remove both metatables */
261 return NULL
; /* value is not a userdata with a metatable */
265 LUALIB_API
void *luaL_checkudata (lua_State
*L
, int ud
, const char *tname
) {
266 void *p
= luaL_testudata(L
, ud
, tname
);
267 if (p
== NULL
) typeerror(L
, ud
, tname
);
271 /* }====================================================== */
275 ** {======================================================
276 ** Argument check functions
277 ** =======================================================
280 LUALIB_API
int luaL_checkoption (lua_State
*L
, int narg
, const char *def
,
281 const char *const lst
[]) {
282 const char *name
= (def
) ? luaL_optstring(L
, narg
, def
) :
283 luaL_checkstring(L
, narg
);
285 for (i
=0; lst
[i
]; i
++)
286 if (strcmp(lst
[i
], name
) == 0)
288 return luaL_argerror(L
, narg
,
289 lua_pushfstring(L
, "invalid option " LUA_QS
, name
));
293 LUALIB_API
void luaL_checkstack (lua_State
*L
, int space
, const char *msg
) {
294 /* keep some extra space to run error routines, if needed */
295 const int extra
= LUA_MINSTACK
;
296 if (!lua_checkstack(L
, space
+ extra
)) {
298 luaL_error(L
, "stack overflow (%s)", msg
);
300 luaL_error(L
, "stack overflow");
305 LUALIB_API
void luaL_checktype (lua_State
*L
, int narg
, int t
) {
306 if (lua_type(L
, narg
) != t
)
307 tag_error(L
, narg
, t
);
311 LUALIB_API
void luaL_checkany (lua_State
*L
, int narg
) {
312 if (lua_type(L
, narg
) == LUA_TNONE
)
313 luaL_argerror(L
, narg
, "value expected");
317 LUALIB_API
const char *luaL_checklstring (lua_State
*L
, int narg
, size_t *len
) {
318 const char *s
= lua_tolstring(L
, narg
, len
);
319 if (!s
) tag_error(L
, narg
, LUA_TSTRING
);
324 LUALIB_API
const char *luaL_optlstring (lua_State
*L
, int narg
,
325 const char *def
, size_t *len
) {
326 if (lua_isnoneornil(L
, narg
)) {
328 *len
= (def
? strlen(def
) : 0);
331 else return luaL_checklstring(L
, narg
, len
);
335 LUALIB_API lua_Number
luaL_checknumber (lua_State
*L
, int narg
) {
337 lua_Number d
= lua_tonumberx(L
, narg
, &isnum
);
339 tag_error(L
, narg
, LUA_TNUMBER
);
344 LUALIB_API lua_Number
luaL_optnumber (lua_State
*L
, int narg
, lua_Number def
) {
345 return luaL_opt(L
, luaL_checknumber
, narg
, def
);
349 LUALIB_API lua_Integer
luaL_checkinteger (lua_State
*L
, int narg
) {
351 lua_Integer d
= lua_tointegerx(L
, narg
, &isnum
);
353 tag_error(L
, narg
, LUA_TNUMBER
);
358 LUALIB_API lua_Unsigned
luaL_checkunsigned (lua_State
*L
, int narg
) {
360 lua_Unsigned d
= lua_tounsignedx(L
, narg
, &isnum
);
362 tag_error(L
, narg
, LUA_TNUMBER
);
367 LUALIB_API lua_Integer
luaL_optinteger (lua_State
*L
, int narg
,
369 return luaL_opt(L
, luaL_checkinteger
, narg
, def
);
373 LUALIB_API lua_Unsigned
luaL_optunsigned (lua_State
*L
, int narg
,
375 return luaL_opt(L
, luaL_checkunsigned
, narg
, def
);
378 /* }====================================================== */
382 ** {======================================================
383 ** Generic Buffer manipulation
384 ** =======================================================
388 ** check whether buffer is using a userdata on the stack as a temporary
391 #define buffonstack(B) ((B)->b != (B)->initb)
395 ** returns a pointer to a free area with at least 'sz' bytes
397 LUALIB_API
char *luaL_prepbuffsize (luaL_Buffer
*B
, size_t sz
) {
399 if (B
->size
- B
->n
< sz
) { /* not enough space? */
401 size_t newsize
= B
->size
* 2; /* double buffer size */
402 if (newsize
- B
->n
< sz
) /* not big enough? */
404 if (newsize
< B
->n
|| newsize
- B
->n
< sz
)
405 luaL_error(L
, "buffer too large");
406 /* create larger buffer */
407 newbuff
= (char *)lua_newuserdata(L
, newsize
* sizeof(char));
408 /* move content to new buffer */
409 memcpy(newbuff
, B
->b
, B
->n
* sizeof(char));
411 lua_remove(L
, -2); /* remove old buffer */
419 LUALIB_API
void luaL_addlstring (luaL_Buffer
*B
, const char *s
, size_t l
) {
420 char *b
= luaL_prepbuffsize(B
, l
);
421 memcpy(b
, s
, l
* sizeof(char));
426 LUALIB_API
void luaL_addstring (luaL_Buffer
*B
, const char *s
) {
427 luaL_addlstring(B
, s
, strlen(s
));
431 LUALIB_API
void luaL_pushresult (luaL_Buffer
*B
) {
433 lua_pushlstring(L
, B
->b
, B
->n
);
435 lua_remove(L
, -2); /* remove old buffer */
439 LUALIB_API
void luaL_pushresultsize (luaL_Buffer
*B
, size_t sz
) {
445 LUALIB_API
void luaL_addvalue (luaL_Buffer
*B
) {
448 const char *s
= lua_tolstring(L
, -1, &l
);
450 lua_insert(L
, -2); /* put value below buffer */
451 luaL_addlstring(B
, s
, l
);
452 lua_remove(L
, (buffonstack(B
)) ? -2 : -1); /* remove value */
456 LUALIB_API
void luaL_buffinit (lua_State
*L
, luaL_Buffer
*B
) {
460 B
->size
= LUAL_BUFFERSIZE
;
464 LUALIB_API
char *luaL_buffinitsize (lua_State
*L
, luaL_Buffer
*B
, size_t sz
) {
466 return luaL_prepbuffsize(B
, sz
);
469 /* }====================================================== */
473 ** {======================================================
475 ** =======================================================
478 /* index of free-list header */
482 LUALIB_API
int luaL_ref (lua_State
*L
, int t
) {
484 if (lua_isnil(L
, -1)) {
485 lua_pop(L
, 1); /* remove from stack */
486 return LUA_REFNIL
; /* `nil' has a unique fixed reference */
488 t
= lua_absindex(L
, t
);
489 lua_rawgeti(L
, t
, freelist
); /* get first free element */
490 ref
= (int)lua_tointeger(L
, -1); /* ref = t[freelist] */
491 lua_pop(L
, 1); /* remove it from stack */
492 if (ref
!= 0) { /* any free element? */
493 lua_rawgeti(L
, t
, ref
); /* remove it from list */
494 lua_rawseti(L
, t
, freelist
); /* (t[freelist] = t[ref]) */
496 else /* no free elements */
497 ref
= (int)lua_rawlen(L
, t
) + 1; /* get a new reference */
498 lua_rawseti(L
, t
, ref
);
503 LUALIB_API
void luaL_unref (lua_State
*L
, int t
, int ref
) {
505 t
= lua_absindex(L
, t
);
506 lua_rawgeti(L
, t
, freelist
);
507 lua_rawseti(L
, t
, ref
); /* t[ref] = t[freelist] */
508 lua_pushinteger(L
, ref
);
509 lua_rawseti(L
, t
, freelist
); /* t[freelist] = ref */
513 /* }====================================================== */
517 ** {======================================================
519 ** =======================================================
522 typedef struct LoadS
{
528 static const char *getS (lua_State
*L
, void *ud
, size_t *size
) {
529 LoadS
*ls
= (LoadS
*)ud
;
530 (void)L
; /* not used */
531 if (ls
->size
== 0) return NULL
;
538 LUALIB_API
int luaL_loadbufferx (lua_State
*L
, const char *buff
, size_t size
,
539 const char *name
, const char *mode
) {
543 return lua_load(L
, getS
, &ls
, name
, mode
);
547 LUALIB_API
int luaL_loadstring (lua_State
*L
, const char *s
) {
548 return luaL_loadbuffer(L
, s
, strlen(s
), s
);
551 /* }====================================================== */
555 LUALIB_API
int luaL_getmetafield (lua_State
*L
, int obj
, const char *event
) {
556 if (!lua_getmetatable(L
, obj
)) /* no metatable? */
558 lua_pushstring(L
, event
);
560 if (lua_isnil(L
, -1)) {
561 lua_pop(L
, 2); /* remove metatable and metafield */
565 lua_remove(L
, -2); /* remove only metatable */
571 LUALIB_API
int luaL_callmeta (lua_State
*L
, int obj
, const char *event
) {
572 obj
= lua_absindex(L
, obj
);
573 if (!luaL_getmetafield(L
, obj
, event
)) /* no metafield? */
575 lua_pushvalue(L
, obj
);
581 LUALIB_API
int luaL_len (lua_State
*L
, int idx
) {
585 l
= (int)lua_tointegerx(L
, -1, &isnum
);
587 luaL_error(L
, "object length is not a number");
588 lua_pop(L
, 1); /* remove object */
593 LUALIB_API
const char *luaL_tolstring (lua_State
*L
, int idx
, size_t *len
) {
594 if (!luaL_callmeta(L
, idx
, "__tostring")) { /* no metafield? */
595 switch (lua_type(L
, idx
)) {
598 lua_pushvalue(L
, idx
);
601 lua_pushstring(L
, (lua_toboolean(L
, idx
) ? "true" : "false"));
604 lua_pushliteral(L
, "nil");
607 lua_pushfstring(L
, "%s: %p", luaL_typename(L
, idx
),
608 lua_topointer(L
, idx
));
612 return lua_tolstring(L
, -1, len
);
617 ** {======================================================
618 ** Compatibility with 5.1 module functions
619 ** =======================================================
621 #if defined(LUA_COMPAT_MODULE)
623 static const char *luaL_findtable (lua_State
*L
, int idx
,
624 const char *fname
, int szhint
) {
626 if (idx
) lua_pushvalue(L
, idx
);
628 e
= strchr(fname
, '.');
629 if (e
== NULL
) e
= fname
+ strlen(fname
);
630 lua_pushlstring(L
, fname
, e
- fname
);
632 if (lua_isnil(L
, -1)) { /* no such field? */
633 lua_pop(L
, 1); /* remove this nil */
634 lua_createtable(L
, 0, (*e
== '.' ? 1 : szhint
)); /* new table for field */
635 lua_pushlstring(L
, fname
, e
- fname
);
636 lua_pushvalue(L
, -2);
637 lua_settable(L
, -4); /* set new table into field */
639 else if (!lua_istable(L
, -1)) { /* field has a non-table value? */
640 lua_pop(L
, 2); /* remove table and value */
641 return fname
; /* return problematic part of the name */
643 lua_remove(L
, -2); /* remove previous table */
651 ** Count number of elements in a luaL_Reg list.
653 static int libsize (const luaL_Reg
*l
) {
655 for (; l
&& l
->name
; l
++) size
++;
661 ** Find or create a module table with a given name. The function
662 ** first looks at the _LOADED table and, if that fails, try a
663 ** global variable with that name. In any case, leaves on the stack
666 LUALIB_API
void luaL_pushmodule (lua_State
*L
, const char *modname
,
668 luaL_findtable(L
, LUA_REGISTRYINDEX
, "_LOADED", 1); /* get _LOADED table */
669 lua_getfield(L
, -1, modname
); /* get _LOADED[modname] */
670 if (!lua_istable(L
, -1)) { /* not found? */
671 lua_pop(L
, 1); /* remove previous result */
672 /* try global variable (and create one if it does not exist) */
673 lua_pushglobaltable(L
);
674 if (luaL_findtable(L
, 0, modname
, sizehint
) != NULL
)
675 luaL_error(L
, "name conflict for module " LUA_QS
, modname
);
676 lua_pushvalue(L
, -1);
677 lua_setfield(L
, -3, modname
); /* _LOADED[modname] = new table */
679 lua_remove(L
, -2); /* remove _LOADED table */
683 LUALIB_API
void luaL_openlib (lua_State
*L
, const char *libname
,
684 const luaL_Reg
*l
, int nup
) {
685 luaL_checkversion(L
);
687 luaL_pushmodule(L
, libname
, libsize(l
)); /* get/create library table */
688 lua_insert(L
, -(nup
+ 1)); /* move library table to below upvalues */
691 luaL_setfuncs(L
, l
, nup
);
693 lua_pop(L
, nup
); /* remove upvalues */
697 /* }====================================================== */
700 ** set functions from list 'l' into table at top - 'nup'; each
701 ** function gets the 'nup' elements at the top as upvalues.
702 ** Returns with only the table at the stack.
704 LUALIB_API
void luaL_setfuncs (lua_State
*L
, const luaL_Reg
*l
, int nup
) {
705 luaL_checkversion(L
);
706 luaL_checkstack(L
, nup
, "too many upvalues");
707 for (; l
->name
!= NULL
; l
++) { /* fill the table with given functions */
709 for (i
= 0; i
< nup
; i
++) /* copy upvalues to the top */
710 lua_pushvalue(L
, -nup
);
711 lua_pushcclosure(L
, l
->func
, nup
); /* closure with those upvalues */
712 lua_setfield(L
, -(nup
+ 2), l
->name
);
714 lua_pop(L
, nup
); /* remove upvalues */
719 ** ensure that stack[idx][fname] has a table and push that table
722 LUALIB_API
int luaL_getsubtable (lua_State
*L
, int idx
, const char *fname
) {
723 lua_getfield(L
, idx
, fname
);
724 if (lua_istable(L
, -1)) return 1; /* table already there */
726 lua_pop(L
, 1); /* remove previous result */
727 idx
= lua_absindex(L
, idx
);
729 lua_pushvalue(L
, -1); /* copy to be left at top */
730 lua_setfield(L
, idx
, fname
); /* assign new table to field */
731 return 0; /* false, because did not find table there */
737 ** stripped-down 'require'. Calls 'openf' to open a module,
738 ** registers the result in 'package.loaded' table and, if 'glb'
739 ** is true, also registers the result in the global table.
740 ** Leaves resulting module on the top.
742 LUALIB_API
void luaL_requiref (lua_State
*L
, const char *modname
,
743 lua_CFunction openf
, int glb
) {
744 lua_pushcfunction(L
, openf
);
745 lua_pushstring(L
, modname
); /* argument to open function */
746 lua_call(L
, 1, 1); /* open module */
747 luaL_getsubtable(L
, LUA_REGISTRYINDEX
, "_LOADED");
748 lua_pushvalue(L
, -2); /* make copy of module (call result) */
749 lua_setfield(L
, -2, modname
); /* _LOADED[modname] = module */
750 lua_pop(L
, 1); /* remove _LOADED table */
752 lua_pushvalue(L
, -1); /* copy of 'mod' */
753 lua_setglobal(L
, modname
); /* _G[modname] = module */
758 LUALIB_API
const char *luaL_gsub (lua_State
*L
, const char *s
, const char *p
,
761 size_t l
= strlen(p
);
763 luaL_buffinit(L
, &b
);
764 while ((wild
= strstr(s
, p
)) != NULL
) {
765 luaL_addlstring(&b
, s
, wild
- s
); /* push prefix */
766 luaL_addstring(&b
, r
); /* push replacement in place of pattern */
767 s
= wild
+ l
; /* continue after `p' */
769 luaL_addstring(&b
, s
); /* push last suffix */
771 return lua_tostring(L
, -1);
775 LUALIB_API
void luaL_checkversion_ (lua_State
*L
, lua_Number ver
) {
776 const lua_Number
*v
= lua_version(L
);
777 if (v
!= lua_version(NULL
))
778 luaL_error(L
, "multiple Lua VMs detected");
780 luaL_error(L
, "version mismatch: app. needs %f, Lua core provides %f",
782 /* check conversions number -> integer types */
783 lua_pushnumber(L
, -(lua_Number
)0x1234);
784 if (lua_tointeger(L
, -1) != -0x1234 ||
785 lua_tounsigned(L
, -1) != (lua_Unsigned
)-0x1234)
786 luaL_error(L
, "bad conversion number->int;"
787 " must recompile Lua with proper settings");
793 EXPORT_SYMBOL(luaL_argerror
);
794 EXPORT_SYMBOL(luaL_error
);
795 EXPORT_SYMBOL(luaL_loadbufferx
);
796 EXPORT_SYMBOL(luaL_newmetatable
);
797 EXPORT_SYMBOL(luaL_traceback
);