Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/contrib/openzfs/module/lua/lauxlib.c
48383 views
1
// SPDX-License-Identifier: MIT
2
/*
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
6
*/
7
8
9
/* This file uses only the official API of Lua.
10
** Any function declared here could be written as an application function.
11
*/
12
13
#define lauxlib_c
14
#define LUA_LIB
15
16
#include <sys/lua/lua.h>
17
18
#include <sys/lua/lauxlib.h>
19
20
21
/*
22
** {======================================================
23
** Traceback
24
** =======================================================
25
*/
26
27
28
#define LEVELS1 12 /* size of the first part of the stack */
29
#define LEVELS2 10 /* size of the second part of the stack */
30
31
32
33
/*
34
** search for 'objidx' in table at index -1.
35
** return 1 + string at top if find a good name.
36
*/
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) */
45
return 1;
46
}
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 */
51
lua_concat(L, 3);
52
return 1;
53
}
54
}
55
lua_pop(L, 1); /* remove value */
56
}
57
return 0; /* not found */
58
}
59
60
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 */
68
return 1;
69
}
70
else {
71
lua_settop(L, top); /* remove function and global table */
72
return 0;
73
}
74
}
75
76
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 */
86
}
87
else
88
lua_pushliteral(L, "?");
89
}
90
else
91
lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined);
92
}
93
94
95
static int countlevels (lua_State *L) {
96
lua_Debug ar;
97
int li = 1, le = 1;
98
/* find an upper bound */
99
while (lua_getstack(L, le, &ar)) { li = le; le *= 2; }
100
/* do a binary search */
101
while (li < le) {
102
int m = (li + le)/2;
103
if (lua_getstack(L, m, &ar)) li = m + 1;
104
else le = m;
105
}
106
return le - 1;
107
}
108
109
110
LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1,
111
const char *msg, int level) {
112
lua_Debug ar;
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 */
122
}
123
else {
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);
130
if (ar.istailcall)
131
lua_pushliteral(L, "\n\t(...tail calls...)");
132
lua_concat(L, lua_gettop(L) - top);
133
}
134
}
135
lua_concat(L, lua_gettop(L) - top);
136
}
137
138
/* }====================================================== */
139
140
141
/*
142
** {======================================================
143
** Error-report functions
144
** =======================================================
145
*/
146
147
LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) {
148
lua_Debug ar;
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)",
156
ar.name, extramsg);
157
}
158
if (ar.name == NULL)
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);
162
}
163
164
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);
169
}
170
171
172
static void tag_error (lua_State *L, int narg, int tag) {
173
typeerror(L, narg, lua_typename(L, tag));
174
}
175
176
177
LUALIB_API void luaL_where (lua_State *L, int level) {
178
lua_Debug ar;
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);
183
return;
184
}
185
}
186
lua_pushliteral(L, ""); /* else, no information available... */
187
}
188
189
190
LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) {
191
va_list argp;
192
va_start(argp, fmt);
193
luaL_where(L, 1);
194
lua_pushvfstring(L, fmt, argp);
195
va_end(argp);
196
lua_concat(L, 2);
197
return lua_error(L);
198
}
199
200
201
#if !defined(inspectstat) /* { */
202
203
#if defined(LUA_USE_POSIX)
204
205
#include <sys/wait.h>
206
207
/*
208
** use appropriate macros to interpret 'pclose' return status
209
*/
210
#define inspectstat(stat,what) \
211
if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \
212
else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; }
213
214
#else
215
216
#define inspectstat(stat,what) /* no op */
217
218
#endif
219
220
#endif /* } */
221
222
223
/* }====================================================== */
224
225
226
/*
227
** {======================================================
228
** Userdata's metatable manipulation
229
** =======================================================
230
*/
231
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 */
236
lua_pop(L, 1);
237
lua_newtable(L); /* create metatable */
238
lua_pushvalue(L, -1);
239
lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */
240
return 1;
241
}
242
243
244
LUALIB_API void luaL_setmetatable (lua_State *L, const char *tname) {
245
luaL_getmetatable(L, tname);
246
lua_setmetatable(L, -2);
247
}
248
249
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 */
258
return p;
259
}
260
}
261
return NULL; /* value is not a userdata with a metatable */
262
}
263
264
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);
268
return p;
269
}
270
271
/* }====================================================== */
272
273
274
/*
275
** {======================================================
276
** Argument check functions
277
** =======================================================
278
*/
279
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);
284
int i;
285
for (i=0; lst[i]; i++)
286
if (strcmp(lst[i], name) == 0)
287
return i;
288
return luaL_argerror(L, narg,
289
lua_pushfstring(L, "invalid option " LUA_QS, name));
290
}
291
292
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)) {
297
if (msg)
298
luaL_error(L, "stack overflow (%s)", msg);
299
else
300
luaL_error(L, "stack overflow");
301
}
302
}
303
304
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);
308
}
309
310
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");
314
}
315
316
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);
320
return s;
321
}
322
323
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)) {
327
if (len)
328
*len = (def ? strlen(def) : 0);
329
return def;
330
}
331
else return luaL_checklstring(L, narg, len);
332
}
333
334
335
LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) {
336
int isnum;
337
lua_Number d = lua_tonumberx(L, narg, &isnum);
338
if (!isnum)
339
tag_error(L, narg, LUA_TNUMBER);
340
return d;
341
}
342
343
344
LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) {
345
return luaL_opt(L, luaL_checknumber, narg, def);
346
}
347
348
349
LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) {
350
int isnum;
351
lua_Integer d = lua_tointegerx(L, narg, &isnum);
352
if (!isnum)
353
tag_error(L, narg, LUA_TNUMBER);
354
return d;
355
}
356
357
358
LUALIB_API lua_Unsigned luaL_checkunsigned (lua_State *L, int narg) {
359
int isnum;
360
lua_Unsigned d = lua_tounsignedx(L, narg, &isnum);
361
if (!isnum)
362
tag_error(L, narg, LUA_TNUMBER);
363
return d;
364
}
365
366
367
LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg,
368
lua_Integer def) {
369
return luaL_opt(L, luaL_checkinteger, narg, def);
370
}
371
372
373
LUALIB_API lua_Unsigned luaL_optunsigned (lua_State *L, int narg,
374
lua_Unsigned def) {
375
return luaL_opt(L, luaL_checkunsigned, narg, def);
376
}
377
378
/* }====================================================== */
379
380
381
/*
382
** {======================================================
383
** Generic Buffer manipulation
384
** =======================================================
385
*/
386
387
/*
388
** check whether buffer is using a userdata on the stack as a temporary
389
** buffer
390
*/
391
#define buffonstack(B) ((B)->b != (B)->initb)
392
393
394
/*
395
** returns a pointer to a free area with at least 'sz' bytes
396
*/
397
LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) {
398
lua_State *L = B->L;
399
if (B->size - B->n < sz) { /* not enough space? */
400
char *newbuff;
401
size_t newsize = B->size * 2; /* double buffer size */
402
if (newsize - B->n < sz) /* not big enough? */
403
newsize = B->n + sz;
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));
410
if (buffonstack(B))
411
lua_remove(L, -2); /* remove old buffer */
412
B->b = newbuff;
413
B->size = newsize;
414
}
415
return &B->b[B->n];
416
}
417
418
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));
422
luaL_addsize(B, l);
423
}
424
425
426
LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
427
luaL_addlstring(B, s, strlen(s));
428
}
429
430
431
LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
432
lua_State *L = B->L;
433
lua_pushlstring(L, B->b, B->n);
434
if (buffonstack(B))
435
lua_remove(L, -2); /* remove old buffer */
436
}
437
438
439
LUALIB_API void luaL_pushresultsize (luaL_Buffer *B, size_t sz) {
440
luaL_addsize(B, sz);
441
luaL_pushresult(B);
442
}
443
444
445
LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
446
lua_State *L = B->L;
447
size_t l;
448
const char *s = lua_tolstring(L, -1, &l);
449
if (buffonstack(B))
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 */
453
}
454
455
456
LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
457
B->L = L;
458
B->b = B->initb;
459
B->n = 0;
460
B->size = LUAL_BUFFERSIZE;
461
}
462
463
464
LUALIB_API char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz) {
465
luaL_buffinit(L, B);
466
return luaL_prepbuffsize(B, sz);
467
}
468
469
/* }====================================================== */
470
471
472
/*
473
** {======================================================
474
** Reference system
475
** =======================================================
476
*/
477
478
/* index of free-list header */
479
#define freelist 0
480
481
482
LUALIB_API int luaL_ref (lua_State *L, int t) {
483
int ref;
484
if (lua_isnil(L, -1)) {
485
lua_pop(L, 1); /* remove from stack */
486
return LUA_REFNIL; /* `nil' has a unique fixed reference */
487
}
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]) */
495
}
496
else /* no free elements */
497
ref = (int)lua_rawlen(L, t) + 1; /* get a new reference */
498
lua_rawseti(L, t, ref);
499
return ref;
500
}
501
502
503
LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
504
if (ref >= 0) {
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 */
510
}
511
}
512
513
/* }====================================================== */
514
515
516
/*
517
** {======================================================
518
** Load functions
519
** =======================================================
520
*/
521
522
typedef struct LoadS {
523
const char *s;
524
size_t size;
525
} LoadS;
526
527
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;
532
*size = ls->size;
533
ls->size = 0;
534
return ls->s;
535
}
536
537
538
LUALIB_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t size,
539
const char *name, const char *mode) {
540
LoadS ls;
541
ls.s = buff;
542
ls.size = size;
543
return lua_load(L, getS, &ls, name, mode);
544
}
545
546
547
LUALIB_API int luaL_loadstring (lua_State *L, const char *s) {
548
return luaL_loadbuffer(L, s, strlen(s), s);
549
}
550
551
/* }====================================================== */
552
553
554
555
LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
556
if (!lua_getmetatable(L, obj)) /* no metatable? */
557
return 0;
558
lua_pushstring(L, event);
559
lua_rawget(L, -2);
560
if (lua_isnil(L, -1)) {
561
lua_pop(L, 2); /* remove metatable and metafield */
562
return 0;
563
}
564
else {
565
lua_remove(L, -2); /* remove only metatable */
566
return 1;
567
}
568
}
569
570
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? */
574
return 0;
575
lua_pushvalue(L, obj);
576
lua_call(L, 1, 1);
577
return 1;
578
}
579
580
581
LUALIB_API int luaL_len (lua_State *L, int idx) {
582
int l;
583
int isnum;
584
lua_len(L, idx);
585
l = (int)lua_tointegerx(L, -1, &isnum);
586
if (!isnum)
587
luaL_error(L, "object length is not a number");
588
lua_pop(L, 1); /* remove object */
589
return l;
590
}
591
592
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)) {
596
case LUA_TNUMBER:
597
case LUA_TSTRING:
598
lua_pushvalue(L, idx);
599
break;
600
case LUA_TBOOLEAN:
601
lua_pushstring(L, (lua_toboolean(L, idx) ? "true" : "false"));
602
break;
603
case LUA_TNIL:
604
lua_pushliteral(L, "nil");
605
break;
606
default:
607
lua_pushfstring(L, "%s: %p", luaL_typename(L, idx),
608
lua_topointer(L, idx));
609
break;
610
}
611
}
612
return lua_tolstring(L, -1, len);
613
}
614
615
616
/*
617
** {======================================================
618
** Compatibility with 5.1 module functions
619
** =======================================================
620
*/
621
#if defined(LUA_COMPAT_MODULE)
622
623
static const char *luaL_findtable (lua_State *L, int idx,
624
const char *fname, int szhint) {
625
const char *e;
626
if (idx) lua_pushvalue(L, idx);
627
do {
628
e = strchr(fname, '.');
629
if (e == NULL) e = fname + strlen(fname);
630
lua_pushlstring(L, fname, e - fname);
631
lua_rawget(L, -2);
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 */
638
}
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 */
642
}
643
lua_remove(L, -2); /* remove previous table */
644
fname = e + 1;
645
} while (*e == '.');
646
return NULL;
647
}
648
649
650
/*
651
** Count number of elements in a luaL_Reg list.
652
*/
653
static int libsize (const luaL_Reg *l) {
654
int size = 0;
655
for (; l && l->name; l++) size++;
656
return size;
657
}
658
659
660
/*
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
664
** the module table.
665
*/
666
LUALIB_API void luaL_pushmodule (lua_State *L, const char *modname,
667
int sizehint) {
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 */
678
}
679
lua_remove(L, -2); /* remove _LOADED table */
680
}
681
682
683
LUALIB_API void luaL_openlib (lua_State *L, const char *libname,
684
const luaL_Reg *l, int nup) {
685
luaL_checkversion(L);
686
if (libname) {
687
luaL_pushmodule(L, libname, libsize(l)); /* get/create library table */
688
lua_insert(L, -(nup + 1)); /* move library table to below upvalues */
689
}
690
if (l)
691
luaL_setfuncs(L, l, nup);
692
else
693
lua_pop(L, nup); /* remove upvalues */
694
}
695
696
#endif
697
/* }====================================================== */
698
699
/*
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.
703
*/
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 */
708
int i;
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);
713
}
714
lua_pop(L, nup); /* remove upvalues */
715
}
716
717
718
/*
719
** ensure that stack[idx][fname] has a table and push that table
720
** into the stack
721
*/
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 */
725
else {
726
lua_pop(L, 1); /* remove previous result */
727
idx = lua_absindex(L, idx);
728
lua_newtable(L);
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 */
732
}
733
}
734
735
736
/*
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.
741
*/
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 */
751
if (glb) {
752
lua_pushvalue(L, -1); /* copy of 'mod' */
753
lua_setglobal(L, modname); /* _G[modname] = module */
754
}
755
}
756
757
758
LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
759
const char *r) {
760
const char *wild;
761
size_t l = strlen(p);
762
luaL_Buffer b;
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' */
768
}
769
luaL_addstring(&b, s); /* push last suffix */
770
luaL_pushresult(&b);
771
return lua_tostring(L, -1);
772
}
773
774
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");
779
else if (*v != ver)
780
luaL_error(L, "version mismatch: app. needs %f, Lua core provides %f",
781
ver, *v);
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");
788
lua_pop(L, 1);
789
}
790
791
#if defined(_KERNEL)
792
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);
798
799
#endif
800
801