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