1 /*
2 ** $Id: lauxlib.c,v 1.159.1.3 2008/01/21 13:20:51 roberto Exp $
3 ** Auxiliary functions for building Lua libraries
4 ** See Copyright Notice in lua.h
5 */
6
7
8 #include <ctype.h>
9 #include <errno.h>
10 #include <stdarg.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15
16 /* This file uses only the official API of Lua.
17 ** Any function declared here could be written as an application function.
18 */
19
20 #define lauxlib_c
21 #define LUA_LIB
22
23 #include "lua.h"
24 #include "lauxlib.h"
25
26 #include "streflop_cond.h" // SPRING
27
28
29 #define FREELIST_REF 0 /* free list of references */
30
31
32 /* convert a stack index to positive */
33 #define abs_index(L, i) ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : \
34 lua_gettop(L) + (i) + 1)
35
36
37 /*
38 ** {======================================================
39 ** Error-report functions
40 ** =======================================================
41 */
42
43
luaL_argerror(lua_State * L,int narg,const char * extramsg)44 LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) {
45 lua_Debug ar;
46 if (!lua_getstack(L, 0, &ar)) /* no stack frame? */
47 return luaL_error(L, "bad argument #%d (%s)", narg, extramsg);
48 lua_getinfo(L, "n", &ar);
49 if (strcmp(ar.namewhat, "method") == 0) {
50 narg--; /* do not count `self' */
51 if (narg == 0) /* error is in the self argument itself? */
52 return luaL_error(L, "calling " LUA_QS " on bad self (%s)",
53 ar.name, extramsg);
54 }
55 if (ar.name == NULL)
56 ar.name = "?";
57 return luaL_error(L, "bad argument #%d to " LUA_QS " (%s)",
58 narg, ar.name, extramsg);
59 }
60
61
luaL_typerror(lua_State * L,int narg,const char * tname)62 LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname) {
63 const char *msg = lua_pushfstring(L, "%s expected, got %s",
64 tname, luaL_typename(L, narg));
65 return luaL_argerror(L, narg, msg);
66 }
67
68
tag_error(lua_State * L,int narg,int tag)69 static void tag_error (lua_State *L, int narg, int tag) {
70 luaL_typerror(L, narg, lua_typename(L, tag));
71 }
72
73
luaL_where(lua_State * L,int level)74 LUALIB_API void luaL_where (lua_State *L, int level) {
75 lua_Debug ar;
76 if (lua_getstack(L, level, &ar)) { /* check function at level */
77 lua_getinfo(L, "Sl", &ar); /* get info about it */
78 if (ar.currentline > 0) { /* is there info? */
79 lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline);
80 return;
81 }
82 }
83 lua_pushliteral(L, ""); /* else, no information available... */
84 }
85
86
luaL_error(lua_State * L,const char * fmt,...)87 LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) {
88 va_list argp;
89 va_start(argp, fmt);
90 luaL_where(L, 1);
91 lua_pushvfstring(L, fmt, argp);
92 va_end(argp);
93 lua_concat(L, 2);
94 return lua_error(L);
95 }
96
97 /* }====================================================== */
98
99
luaL_checkoption(lua_State * L,int narg,const char * def,const char * const lst[])100 LUALIB_API int luaL_checkoption (lua_State *L, int narg, const char *def,
101 const char *const lst[]) {
102 const char *name = (def) ? luaL_optstring(L, narg, def) :
103 luaL_checkstring(L, narg);
104 int i;
105 for (i=0; lst[i]; i++)
106 if (strcmp(lst[i], name) == 0)
107 return i;
108 return luaL_argerror(L, narg,
109 lua_pushfstring(L, "invalid option " LUA_QS, name));
110 }
111
112
luaL_newmetatable(lua_State * L,const char * tname)113 LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {
114 lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get registry.name */
115 if (!lua_isnil(L, -1)) /* name already in use? */
116 return 0; /* leave previous value on top, but return 0 */
117 lua_pop(L, 1);
118 lua_newtable(L); /* create metatable */
119 lua_pushvalue(L, -1);
120 lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */
121 return 1;
122 }
123
124
luaL_checkudata(lua_State * L,int ud,const char * tname)125 LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {
126 void *p = lua_touserdata(L, ud);
127 if (p != NULL) { /* value is a userdata? */
128 if (lua_getmetatable(L, ud)) { /* does it have a metatable? */
129 lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */
130 if (lua_rawequal(L, -1, -2)) { /* does it have the correct mt? */
131 lua_pop(L, 2); /* remove both metatables */
132 return p;
133 }
134 }
135 }
136 luaL_typerror(L, ud, tname); /* else error */
137 return NULL; /* to avoid warnings */
138 }
139
140
luaL_checkstack(lua_State * L,int space,const char * mes)141 LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) {
142 if (!lua_checkstack(L, space))
143 luaL_error(L, "stack overflow (%s)", mes);
144 }
145
146
luaL_checktype(lua_State * L,int narg,int t)147 LUALIB_API void luaL_checktype (lua_State *L, int narg, int t) {
148 if (lua_type(L, narg) != t)
149 tag_error(L, narg, t);
150 }
151
152
luaL_checkany(lua_State * L,int narg)153 LUALIB_API void luaL_checkany (lua_State *L, int narg) {
154 if (lua_type(L, narg) == LUA_TNONE)
155 luaL_argerror(L, narg, "value expected");
156 }
157
158
luaL_checklstring(lua_State * L,int narg,size_t * len)159 LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) {
160 const char *s = lua_tolstring(L, narg, len);
161 if (!s) tag_error(L, narg, LUA_TSTRING);
162 return s;
163 }
164
165
luaL_optlstring(lua_State * L,int narg,const char * def,size_t * len)166 LUALIB_API const char *luaL_optlstring (lua_State *L, int narg,
167 const char *def, size_t *len) {
168 if (lua_isnoneornil(L, narg)) {
169 if (len)
170 *len = (def ? strlen(def) : 0);
171 return def;
172 }
173 else return luaL_checklstring(L, narg, len);
174 }
175
176
luaL_checknumber(lua_State * L,int narg)177 LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) {
178 lua_Number d = lua_tonumber(L, narg);
179 if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
180 tag_error(L, narg, LUA_TNUMBER);
181
182 #ifdef DEBUG
183 // SPRING
184 // this is used by luaL_optnumber, luaL_optfloat (via luaL_optnumber),
185 // and luaL_checkfloat, so the asserts should cover 90% of all cases
186 // in which non-numbers can infect the engine -- lua_tofloat asserts
187 // take care of the rest
188 if (math::isinf(d) || math::isnan(d)) luaL_argerror(L, narg, "number expected, got NAN (check your code for div0)");
189 //assert(!math::isinf(d));
190 //assert(!math::isnan(d));
191 #endif
192
193 return d;
194 }
195
luaL_checknumber_noassert(lua_State * L,int narg)196 LUALIB_API lua_Number luaL_checknumber_noassert (lua_State *L, int narg) {
197 lua_Number d = lua_tonumber(L, narg);
198 if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
199 tag_error(L, narg, LUA_TNUMBER);
200
201 return d;
202 }
203
204
luaL_optnumber(lua_State * L,int narg,lua_Number def)205 LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) {
206 return luaL_opt(L, luaL_checknumber, narg, def);
207 }
208
209
luaL_checkinteger(lua_State * L,int narg)210 LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) {
211 lua_Integer d = lua_tointeger(L, narg);
212 if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
213 tag_error(L, narg, LUA_TNUMBER);
214 return d;
215 }
216
217
luaL_optinteger(lua_State * L,int narg,lua_Integer def)218 LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg,
219 lua_Integer def) {
220 return luaL_opt(L, luaL_checkinteger, narg, def);
221 }
222
223
luaL_getmetafield(lua_State * L,int obj,const char * event)224 LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
225 if (!lua_getmetatable(L, obj)) /* no metatable? */
226 return 0;
227 lua_pushstring(L, event);
228 lua_rawget(L, -2);
229 if (lua_isnil(L, -1)) {
230 lua_pop(L, 2); /* remove metatable and metafield */
231 return 0;
232 }
233 else {
234 lua_remove(L, -2); /* remove only metatable */
235 return 1;
236 }
237 }
238
239
luaL_callmeta(lua_State * L,int obj,const char * event)240 LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
241 obj = abs_index(L, obj);
242 if (!luaL_getmetafield(L, obj, event)) /* no metafield? */
243 return 0;
244 lua_pushvalue(L, obj);
245 lua_call(L, 1, 1);
246 return 1;
247 }
248
249
250 LUALIB_API void (luaL_register) (lua_State *L, const char *libname,
251 const luaL_Reg *l) {
252 luaI_openlib(L, libname, l, 0);
253 }
254
255
libsize(const luaL_Reg * l)256 static int libsize (const luaL_Reg *l) {
257 int size = 0;
258 for (; l->name; l++) size++;
259 return size;
260 }
261
262
luaI_openlib(lua_State * L,const char * libname,const luaL_Reg * l,int nup)263 LUALIB_API void luaI_openlib (lua_State *L, const char *libname,
264 const luaL_Reg *l, int nup) {
265 if (libname) {
266 int size = libsize(l);
267 /* check whether lib already exists */
268 luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1);
269 lua_getfield(L, -1, libname); /* get _LOADED[libname] */
270 if (!lua_istable(L, -1)) { /* not found? */
271 lua_pop(L, 1); /* remove previous result */
272 /* try global variable (and create one if it does not exist) */
273 if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != NULL)
274 luaL_error(L, "name conflict for module " LUA_QS, libname);
275 lua_pushvalue(L, -1);
276 lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */
277 }
278 lua_remove(L, -2); /* remove _LOADED table */
279 lua_insert(L, -(nup+1)); /* move library table to below upvalues */
280 }
281 for (; l->name; l++) {
282 int i;
283 for (i=0; i<nup; i++) /* copy upvalues to the top */
284 lua_pushvalue(L, -nup);
285 lua_pushcclosure(L, l->func, nup);
286 lua_setfield(L, -(nup+2), l->name);
287 }
288 lua_pop(L, nup); /* remove upvalues */
289 }
290
291
292
293 /*
294 ** {======================================================
295 ** getn-setn: size for arrays
296 ** =======================================================
297 */
298
299 #if defined(LUA_COMPAT_GETN)
300
checkint(lua_State * L,int topop)301 static int checkint (lua_State *L, int topop) {
302 int n = (lua_type(L, -1) == LUA_TNUMBER) ? lua_tointeger(L, -1) : -1;
303 lua_pop(L, topop);
304 return n;
305 }
306
307
getsizes(lua_State * L)308 static void getsizes (lua_State *L) {
309 lua_getfield(L, LUA_REGISTRYINDEX, "LUA_SIZES");
310 if (lua_isnil(L, -1)) { /* no `size' table? */
311 lua_pop(L, 1); /* remove nil */
312 lua_newtable(L); /* create it */
313 lua_pushvalue(L, -1); /* `size' will be its own metatable */
314 lua_setmetatable(L, -2);
315 lua_pushliteral(L, "kv");
316 lua_setfield(L, -2, "__mode"); /* metatable(N).__mode = "kv" */
317 lua_pushvalue(L, -1);
318 lua_setfield(L, LUA_REGISTRYINDEX, "LUA_SIZES"); /* store in register */
319 }
320 }
321
322
luaL_setn(lua_State * L,int t,int n)323 LUALIB_API void luaL_setn (lua_State *L, int t, int n) {
324 t = abs_index(L, t);
325 lua_pushliteral(L, "n");
326 lua_rawget(L, t);
327 if (checkint(L, 1) >= 0) { /* is there a numeric field `n'? */
328 lua_pushliteral(L, "n"); /* use it */
329 lua_pushinteger(L, n);
330 lua_rawset(L, t);
331 }
332 else { /* use `sizes' */
333 getsizes(L);
334 lua_pushvalue(L, t);
335 lua_pushinteger(L, n);
336 lua_rawset(L, -3); /* sizes[t] = n */
337 lua_pop(L, 1); /* remove `sizes' */
338 }
339 }
340
341
luaL_getn(lua_State * L,int t)342 LUALIB_API int luaL_getn (lua_State *L, int t) {
343 int n;
344 t = abs_index(L, t);
345 lua_pushliteral(L, "n"); /* try t.n */
346 lua_rawget(L, t);
347 if ((n = checkint(L, 1)) >= 0) return n;
348 getsizes(L); /* else try sizes[t] */
349 lua_pushvalue(L, t);
350 lua_rawget(L, -2);
351 if ((n = checkint(L, 2)) >= 0) return n;
352 return (int)lua_objlen(L, t);
353 }
354
355 #endif
356
357 /* }====================================================== */
358
359
360
luaL_gsub(lua_State * L,const char * s,const char * p,const char * r)361 LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
362 const char *r) {
363 const char *wild;
364 size_t l = strlen(p);
365 luaL_Buffer b;
366 luaL_buffinit(L, &b);
367 while ((wild = strstr(s, p)) != NULL) {
368 luaL_addlstring(&b, s, wild - s); /* push prefix */
369 luaL_addstring(&b, r); /* push replacement in place of pattern */
370 s = wild + l; /* continue after `p' */
371 }
372 luaL_addstring(&b, s); /* push last suffix */
373 luaL_pushresult(&b);
374 return lua_tostring(L, -1);
375 }
376
377
luaL_findtable(lua_State * L,int idx,const char * fname,int szhint)378 LUALIB_API const char *luaL_findtable (lua_State *L, int idx,
379 const char *fname, int szhint) {
380 const char *e;
381 lua_pushvalue(L, idx);
382 do {
383 e = strchr(fname, '.');
384 if (e == NULL) e = fname + strlen(fname);
385 lua_pushlstring(L, fname, e - fname);
386 lua_rawget(L, -2);
387 if (lua_isnil(L, -1)) { /* no such field? */
388 lua_pop(L, 1); /* remove this nil */
389 lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */
390 lua_pushlstring(L, fname, e - fname);
391 lua_pushvalue(L, -2);
392 lua_settable(L, -4); /* set new table into field */
393 }
394 else if (!lua_istable(L, -1)) { /* field has a non-table value? */
395 lua_pop(L, 2); /* remove table and value */
396 return fname; /* return problematic part of the name */
397 }
398 lua_remove(L, -2); /* remove previous table */
399 fname = e + 1;
400 } while (*e == '.');
401 return NULL;
402 }
403
404
405
406 /*
407 ** {======================================================
408 ** Generic Buffer manipulation
409 ** =======================================================
410 */
411
412
413 #define bufflen(B) ((B)->p - (B)->buffer)
414 #define bufffree(B) ((size_t)(LUAL_BUFFERSIZE - bufflen(B)))
415
416 #define LIMIT (LUA_MINSTACK/2)
417
418
emptybuffer(luaL_Buffer * B)419 static int emptybuffer (luaL_Buffer *B) {
420 size_t l = bufflen(B);
421 if (l == 0) return 0; /* put nothing on stack */
422 else {
423 lua_pushlstring(B->L, B->buffer, l);
424 B->p = B->buffer;
425 B->lvl++;
426 return 1;
427 }
428 }
429
430
adjuststack(luaL_Buffer * B)431 static void adjuststack (luaL_Buffer *B) {
432 if (B->lvl > 1) {
433 lua_State *L = B->L;
434 int toget = 1; /* number of levels to concat */
435 size_t toplen = lua_strlen(L, -1);
436 do {
437 size_t l = lua_strlen(L, -(toget+1));
438 if (B->lvl - toget + 1 >= LIMIT || toplen > l) {
439 toplen += l;
440 toget++;
441 }
442 else break;
443 } while (toget < B->lvl);
444 lua_concat(L, toget);
445 B->lvl = B->lvl - toget + 1;
446 }
447 }
448
449
luaL_prepbuffer(luaL_Buffer * B)450 LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B) {
451 if (emptybuffer(B))
452 adjuststack(B);
453 return B->buffer;
454 }
455
456
luaL_addlstring(luaL_Buffer * B,const char * s,size_t l)457 LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
458 while (l--)
459 luaL_addchar(B, *s++);
460 }
461
462
luaL_addstring(luaL_Buffer * B,const char * s)463 LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
464 luaL_addlstring(B, s, strlen(s));
465 }
466
467
luaL_pushresult(luaL_Buffer * B)468 LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
469 emptybuffer(B);
470 lua_concat(B->L, B->lvl);
471 B->lvl = 1;
472 }
473
474
luaL_addvalue(luaL_Buffer * B)475 LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
476 lua_State *L = B->L;
477 size_t vl;
478 const char *s = lua_tolstring(L, -1, &vl);
479 if (vl <= bufffree(B)) { /* fit into buffer? */
480 memcpy(B->p, s, vl); /* put it there */
481 B->p += vl;
482 lua_pop(L, 1); /* remove from stack */
483 }
484 else {
485 if (emptybuffer(B))
486 lua_insert(L, -2); /* put buffer before new value */
487 B->lvl++; /* add new value into B stack */
488 adjuststack(B);
489 }
490 }
491
492
luaL_buffinit(lua_State * L,luaL_Buffer * B)493 LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
494 B->L = L;
495 B->p = B->buffer;
496 B->lvl = 0;
497 }
498
499 /* }====================================================== */
500
501
luaL_ref(lua_State * L,int t)502 LUALIB_API int luaL_ref (lua_State *L, int t) {
503 int ref;
504 t = abs_index(L, t);
505 if (lua_isnil(L, -1)) {
506 lua_pop(L, 1); /* remove from stack */
507 return LUA_REFNIL; /* `nil' has a unique fixed reference */
508 }
509 lua_rawgeti(L, t, FREELIST_REF); /* get first free element */
510 ref = (int)lua_tointeger(L, -1); /* ref = t[FREELIST_REF] */
511 lua_pop(L, 1); /* remove it from stack */
512 if (ref != 0) { /* any free element? */
513 lua_rawgeti(L, t, ref); /* remove it from list */
514 lua_rawseti(L, t, FREELIST_REF); /* (t[FREELIST_REF] = t[ref]) */
515 }
516 else { /* no free elements */
517 ref = (int)lua_objlen(L, t);
518 ref++; /* create new reference */
519 }
520 lua_rawseti(L, t, ref);
521 return ref;
522 }
523
524
luaL_unref(lua_State * L,int t,int ref)525 LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
526 if (ref >= 0) {
527 t = abs_index(L, t);
528 lua_rawgeti(L, t, FREELIST_REF);
529 lua_rawseti(L, t, ref); /* t[ref] = t[FREELIST_REF] */
530 lua_pushinteger(L, ref);
531 lua_rawseti(L, t, FREELIST_REF); /* t[FREELIST_REF] = ref */
532 }
533 }
534
535
536
537 /*
538 ** {======================================================
539 ** Load functions
540 ** =======================================================
541 */
542
543 typedef struct LoadF {
544 int extraline;
545 FILE *f;
546 char buff[LUAL_BUFFERSIZE];
547 } LoadF;
548
549
getF(lua_State * L,void * ud,size_t * size)550 static const char *getF (lua_State *L, void *ud, size_t *size) {
551 LoadF *lf = (LoadF *)ud;
552 (void)L;
553 if (lf->extraline) {
554 lf->extraline = 0;
555 *size = 1;
556 return "\n";
557 }
558 if (feof(lf->f)) return NULL;
559 *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f);
560 return (*size > 0) ? lf->buff : NULL;
561 }
562
563
errfile(lua_State * L,const char * what,int fnameindex)564 static int errfile (lua_State *L, const char *what, int fnameindex) {
565 const char *serr = strerror(errno);
566 const char *filename = lua_tostring(L, fnameindex) + 1;
567 lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr);
568 lua_remove(L, fnameindex);
569 return LUA_ERRFILE;
570 }
571
572
luaL_loadfile(lua_State * L,const char * filename)573 LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
574 LoadF lf;
575 int status, readstatus;
576 int c;
577 int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */
578 lf.extraline = 0;
579 if (filename == NULL) {
580 lua_pushliteral(L, "=stdin");
581 lf.f = stdin;
582 }
583 else {
584 lua_pushfstring(L, "@%s", filename);
585 lf.f = fopen(filename, "r");
586 if (lf.f == NULL) return errfile(L, "open", fnameindex);
587 }
588 c = getc(lf.f);
589 if (c == '#') { /* Unix exec. file? */
590 lf.extraline = 1;
591 while ((c = getc(lf.f)) != EOF && c != '\n') ; /* skip first line */
592 if (c == '\n') c = getc(lf.f);
593 }
594 if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */
595 lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */
596 if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
597 /* skip eventual `#!...' */
598 while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ;
599 lf.extraline = 0;
600 }
601 ungetc(c, lf.f);
602 status = lua_load(L, getF, &lf, lua_tostring(L, -1));
603 readstatus = ferror(lf.f);
604 if (filename) fclose(lf.f); /* close file (even in case of errors) */
605 if (readstatus) {
606 lua_settop(L, fnameindex); /* ignore results from `lua_load' */
607 return errfile(L, "read", fnameindex);
608 }
609 lua_remove(L, fnameindex);
610 return status;
611 }
612
613
614 typedef struct LoadS {
615 const char *s;
616 size_t size;
617 } LoadS;
618
619
getS(lua_State * L,void * ud,size_t * size)620 static const char *getS (lua_State *L, void *ud, size_t *size) {
621 LoadS *ls = (LoadS *)ud;
622 (void)L;
623 if (ls->size == 0) return NULL;
624 *size = ls->size;
625 ls->size = 0;
626 return ls->s;
627 }
628
629
luaL_loadbuffer(lua_State * L,const char * buff,size_t size,const char * name)630 LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size,
631 const char *name) {
632 LoadS ls;
633 ls.s = buff;
634 ls.size = size;
635 return lua_load(L, getS, &ls, name);
636 }
637
638
639 LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s) {
640 return luaL_loadbuffer(L, s, strlen(s), s);
641 }
642
643
644
645 /* }====================================================== */
646
647
l_alloc(void * ud,void * ptr,size_t osize,size_t nsize)648 static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
649 (void)ud;
650 (void)osize;
651 if (nsize == 0) {
652 free(ptr);
653 return NULL;
654 }
655 else
656 return realloc(ptr, nsize);
657 }
658
659
panic(lua_State * L)660 static int panic (lua_State *L) {
661 (void)L; /* to avoid warnings */
662 fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n",
663 lua_tostring(L, -1));
664 return 0;
665 }
666
667
luaL_newstate(void)668 LUALIB_API lua_State *luaL_newstate (void) {
669 lua_State *L = lua_newstate(l_alloc, NULL);
670 if (L) lua_atpanic(L, &panic);
671 return L;
672 }
673
674