summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/lauxlib.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/lua/lauxlib.c')
-rw-r--r--apps/plugins/lua/lauxlib.c974
1 files changed, 327 insertions, 647 deletions
diff --git a/apps/plugins/lua/lauxlib.c b/apps/plugins/lua/lauxlib.c
index f451fb7c9e..b8020b7475 100644
--- a/apps/plugins/lua/lauxlib.c
+++ b/apps/plugins/lua/lauxlib.c
@@ -1,10 +1,11 @@
1/* 1/*
2** $Id: lauxlib.c,v 1.248.1.1 2013/04/12 18:48:47 roberto Exp $ 2** $Id: lauxlib.c,v 1.159.1.3 2008/01/21 13:20:51 roberto Exp $
3** Auxiliary functions for building Lua libraries 3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
6 6
7 7
8#include <ctype.h>
8#include <errno.h> 9#include <errno.h>
9#include <stdarg.h> 10#include <stdarg.h>
10#include <stdio.h> 11#include <stdio.h>
@@ -22,127 +23,14 @@
22#include "lua.h" 23#include "lua.h"
23 24
24#include "lauxlib.h" 25#include "lauxlib.h"
25#include "rocklibc.h"
26 26
27 27
28/* 28#define FREELIST_REF 0 /* free list of references */
29** {======================================================
30** Traceback
31** =======================================================
32*/
33
34
35#define LEVELS1 12 /* size of the first part of the stack */
36#define LEVELS2 10 /* size of the second part of the stack */
37
38
39
40/*
41** search for 'objidx' in table at index -1.
42** return 1 + string at top if find a good name.
43*/
44static int findfield (lua_State *L, int objidx, int level) {
45 if (level == 0 || !lua_istable(L, -1))
46 return 0; /* not found */
47 lua_pushnil(L); /* start 'next' loop */
48 while (lua_next(L, -2)) { /* for each pair in table */
49 if (lua_type(L, -2) == LUA_TSTRING) { /* ignore non-string keys */
50 if (lua_rawequal(L, objidx, -1)) { /* found object? */
51 lua_pop(L, 1); /* remove value (but keep name) */
52 return 1;
53 }
54 else if (findfield(L, objidx, level - 1)) { /* try recursively */
55 lua_remove(L, -2); /* remove table (but keep name) */
56 lua_pushliteral(L, ".");
57 lua_insert(L, -2); /* place '.' between the two names */
58 lua_concat(L, 3);
59 return 1;
60 }
61 }
62 lua_pop(L, 1); /* remove value */
63 }
64 return 0; /* not found */
65}
66
67
68static int pushglobalfuncname (lua_State *L, lua_Debug *ar) {
69 int top = lua_gettop(L);
70 lua_getinfo(L, "f", ar); /* push function */
71 lua_pushglobaltable(L);
72 if (findfield(L, top + 1, 2)) {
73 lua_copy(L, -1, top + 1); /* move name to proper place */
74 lua_pop(L, 2); /* remove pushed values */
75 return 1;
76 }
77 else {
78 lua_settop(L, top); /* remove function and global table */
79 return 0;
80 }
81}
82
83
84static void pushfuncname (lua_State *L, lua_Debug *ar) {
85 if (*ar->namewhat != '\0') /* is there a name? */
86 lua_pushfstring(L, "function " LUA_QS, ar->name);
87 else if (*ar->what == 'm') /* main? */
88 lua_pushliteral(L, "main chunk");
89 else if (*ar->what == 'C') {
90 if (pushglobalfuncname(L, ar)) {
91 lua_pushfstring(L, "function " LUA_QS, lua_tostring(L, -1));
92 lua_remove(L, -2); /* remove name */
93 }
94 else
95 lua_pushliteral(L, "?");
96 }
97 else
98 lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined);
99}
100 29
101 30
102static int countlevels (lua_State *L) { 31/* convert a stack index to positive */
103 lua_Debug ar; 32#define abs_index(L, i) ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : \
104 int li = 1, le = 1; 33 lua_gettop(L) + (i) + 1)
105 /* find an upper bound */
106 while (lua_getstack(L, le, &ar)) { li = le; le *= 2; }
107 /* do a binary search */
108 while (li < le) {
109 int m = (li + le)/2;
110 if (lua_getstack(L, m, &ar)) li = m + 1;
111 else le = m;
112 }
113 return le - 1;
114}
115
116
117LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1,
118 const char *msg, int level) {
119 lua_Debug ar;
120 int top = lua_gettop(L);
121 int numlevels = countlevels(L1);
122 int mark = (numlevels > LEVELS1 + LEVELS2) ? LEVELS1 : 0;
123 if (msg) lua_pushfstring(L, "%s\n", msg);
124 lua_pushliteral(L, "stack traceback:");
125 while (lua_getstack(L1, level++, &ar)) {
126 if (level == mark) { /* too many levels? */
127 lua_pushliteral(L, "\n\t..."); /* add a '...' */
128 level = numlevels - LEVELS2; /* and skip to last ones */
129 }
130 else {
131 lua_getinfo(L1, "Slnt", &ar);
132 lua_pushfstring(L, "\n\t%s:", ar.short_src);
133 if (ar.currentline > 0)
134 lua_pushfstring(L, "%d:", ar.currentline);
135 lua_pushliteral(L, " in ");
136 pushfuncname(L, &ar);
137 if (ar.istailcall)
138 lua_pushliteral(L, "\n\t(...tail calls...)");
139 lua_concat(L, lua_gettop(L) - top);
140 }
141 }
142 lua_concat(L, lua_gettop(L) - top);
143}
144
145/* }====================================================== */
146 34
147 35
148/* 36/*
@@ -151,6 +39,7 @@ LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1,
151** ======================================================= 39** =======================================================
152*/ 40*/
153 41
42
154LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) { 43LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) {
155 lua_Debug ar; 44 lua_Debug ar;
156 if (!lua_getstack(L, 0, &ar)) /* no stack frame? */ 45 if (!lua_getstack(L, 0, &ar)) /* no stack frame? */
@@ -163,13 +52,13 @@ LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) {
163 ar.name, extramsg); 52 ar.name, extramsg);
164 } 53 }
165 if (ar.name == NULL) 54 if (ar.name == NULL)
166 ar.name = (pushglobalfuncname(L, &ar)) ? lua_tostring(L, -1) : "?"; 55 ar.name = "?";
167 return luaL_error(L, "bad argument #%d to " LUA_QS " (%s)", 56 return luaL_error(L, "bad argument #%d to " LUA_QS " (%s)",
168 narg, ar.name, extramsg); 57 narg, ar.name, extramsg);
169} 58}
170 59
171 60
172static int typeerror (lua_State *L, int narg, const char *tname) { 61LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname) {
173 const char *msg = lua_pushfstring(L, "%s expected, got %s", 62 const char *msg = lua_pushfstring(L, "%s expected, got %s",
174 tname, luaL_typename(L, narg)); 63 tname, luaL_typename(L, narg));
175 return luaL_argerror(L, narg, msg); 64 return luaL_argerror(L, narg, msg);
@@ -177,7 +66,7 @@ static int typeerror (lua_State *L, int narg, const char *tname) {
177 66
178 67
179static void tag_error (lua_State *L, int narg, int tag) { 68static void tag_error (lua_State *L, int narg, int tag) {
180 typeerror(L, narg, lua_typename(L, tag)); 69 luaL_typerror(L, narg, lua_typename(L, tag));
181} 70}
182 71
183 72
@@ -204,74 +93,24 @@ LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) {
204 return lua_error(L); 93 return lua_error(L);
205} 94}
206 95
207 96/* }====================================================== */
208LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) {
209 int en = errno; /* calls to Lua API may change this value */
210 if (stat) {
211 lua_pushboolean(L, 1);
212 return 1;
213 }
214 else {
215 lua_pushnil(L);
216 if (fname)
217 lua_pushfstring(L, "%s: %s", fname, strerror(en));
218 else
219 lua_pushstring(L, strerror(en));
220 lua_pushinteger(L, en);
221 return 3;
222 }
223}
224
225
226#if !defined(inspectstat) /* { */
227
228#if defined(LUA_USE_POSIX)
229
230#include <sys/wait.h>
231
232/*
233** use appropriate macros to interpret 'pclose' return status
234*/
235#define inspectstat(stat,what) \
236 if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \
237 else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; }
238
239#else
240
241#define inspectstat(stat,what) /* no op */
242
243#endif
244
245#endif /* } */
246 97
247 98
248LUALIB_API int luaL_execresult (lua_State *L, int stat) { 99LUALIB_API int luaL_checkoption (lua_State *L, int narg, const char *def,
249 const char *what = "exit"; /* type of termination */ 100 const char *const lst[]) {
250 if (stat == -1) /* error? */ 101 const char *name = (def) ? luaL_optstring(L, narg, def) :
251 return luaL_fileresult(L, 0, NULL); 102 luaL_checkstring(L, narg);
252 else { 103 int i;
253 inspectstat(stat, what); /* interpret result */ 104 for (i=0; lst[i]; i++)
254 if (*what == 'e' && stat == 0) /* successful termination? */ 105 if (strcmp(lst[i], name) == 0)
255 lua_pushboolean(L, 1); 106 return i;
256 else 107 return luaL_argerror(L, narg,
257 lua_pushnil(L); 108 lua_pushfstring(L, "invalid option " LUA_QS, name));
258 lua_pushstring(L, what);
259 lua_pushinteger(L, stat);
260 return 3; /* return true/nil,what,code */
261 }
262} 109}
263 110
264/* }====================================================== */
265
266
267/*
268** {======================================================
269** Userdata's metatable manipulation
270** =======================================================
271*/
272 111
273LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) { 112LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {
274 luaL_getmetatable(L, tname); /* try to get metatable */ 113 lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get registry.name */
275 if (!lua_isnil(L, -1)) /* name already in use? */ 114 if (!lua_isnil(L, -1)) /* name already in use? */
276 return 0; /* leave previous value on top, but return 0 */ 115 return 0; /* leave previous value on top, but return 0 */
277 lua_pop(L, 1); 116 lua_pop(L, 1);
@@ -282,64 +121,25 @@ LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {
282} 121}
283 122
284 123
285LUALIB_API void luaL_setmetatable (lua_State *L, const char *tname) { 124LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {
286 luaL_getmetatable(L, tname);
287 lua_setmetatable(L, -2);
288}
289
290
291LUALIB_API void *luaL_testudata (lua_State *L, int ud, const char *tname) {
292 void *p = lua_touserdata(L, ud); 125 void *p = lua_touserdata(L, ud);
293 if (p != NULL) { /* value is a userdata? */ 126 if (p != NULL) { /* value is a userdata? */
294 if (lua_getmetatable(L, ud)) { /* does it have a metatable? */ 127 if (lua_getmetatable(L, ud)) { /* does it have a metatable? */
295 luaL_getmetatable(L, tname); /* get correct metatable */ 128 lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */
296 if (!lua_rawequal(L, -1, -2)) /* not the same? */ 129 if (lua_rawequal(L, -1, -2)) { /* does it have the correct mt? */
297 p = NULL; /* value is a userdata with wrong metatable */ 130 lua_pop(L, 2); /* remove both metatables */
298 lua_pop(L, 2); /* remove both metatables */ 131 return p;
299 return p; 132 }
300 } 133 }
301 } 134 }
302 return NULL; /* value is not a userdata with a metatable */ 135 luaL_typerror(L, ud, tname); /* else error */
136 return NULL; /* to avoid warnings */
303} 137}
304 138
305 139
306LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) { 140LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) {
307 void *p = luaL_testudata(L, ud, tname); 141 if (!lua_checkstack(L, space))
308 if (p == NULL) typeerror(L, ud, tname); 142 luaL_error(L, "stack overflow (%s)", mes);
309 return p;
310}
311
312/* }====================================================== */
313
314
315/*
316** {======================================================
317** Argument check functions
318** =======================================================
319*/
320
321LUALIB_API int luaL_checkoption (lua_State *L, int narg, const char *def,
322 const char *const lst[]) {
323 const char *name = (def) ? luaL_optstring(L, narg, def) :
324 luaL_checkstring(L, narg);
325 int i;
326 for (i=0; lst[i]; i++)
327 if (strcmp(lst[i], name) == 0)
328 return i;
329 return luaL_argerror(L, narg,
330 lua_pushfstring(L, "invalid option " LUA_QS, name));
331}
332
333
334LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) {
335 /* keep some extra space to run error routines, if needed */
336 const int extra = LUA_MINSTACK;
337 if (!lua_checkstack(L, space + extra)) {
338 if (msg)
339 luaL_error(L, "stack overflow (%s)", msg);
340 else
341 luaL_error(L, "stack overflow");
342 }
343} 143}
344 144
345 145
@@ -374,9 +174,8 @@ LUALIB_API const char *luaL_optlstring (lua_State *L, int narg,
374 174
375 175
376LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) { 176LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) {
377 int isnum; 177 lua_Number d = lua_tonumber(L, narg);
378 lua_Number d = lua_tonumberx(L, narg, &isnum); 178 if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
379 if (!isnum)
380 tag_error(L, narg, LUA_TNUMBER); 179 tag_error(L, narg, LUA_TNUMBER);
381 return d; 180 return d;
382} 181}
@@ -388,18 +187,8 @@ LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) {
388 187
389 188
390LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) { 189LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) {
391 int isnum; 190 lua_Integer d = lua_tointeger(L, narg);
392 lua_Integer d = lua_tointegerx(L, narg, &isnum); 191 if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
393 if (!isnum)
394 tag_error(L, narg, LUA_TNUMBER);
395 return d;
396}
397
398
399LUALIB_API lua_Unsigned luaL_checkunsigned (lua_State *L, int narg) {
400 int isnum;
401 lua_Unsigned d = lua_tounsignedx(L, narg, &isnum);
402 if (!isnum)
403 tag_error(L, narg, LUA_TNUMBER); 192 tag_error(L, narg, LUA_TNUMBER);
404 return d; 193 return d;
405} 194}
@@ -411,56 +200,255 @@ LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg,
411} 200}
412 201
413 202
414LUALIB_API lua_Unsigned luaL_optunsigned (lua_State *L, int narg, 203LUALIB_API int luaL_checkboolean (lua_State *L, int narg) {
415 lua_Unsigned def) { 204 int b = lua_toboolean(L, narg);
416 return luaL_opt(L, luaL_checkunsigned, narg, def); 205 if( b == 0 && !lua_isboolean(L, narg))
206 tag_error(L, narg, LUA_TBOOLEAN);
207 return b;
208}
209
210
211LUALIB_API int luaL_optboolean (lua_State *L, int narg, int def) {
212 return luaL_opt(L, luaL_checkboolean, narg, def);
213}
214
215
216LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
217 if (!lua_getmetatable(L, obj)) /* no metatable? */
218 return 0;
219 lua_pushstring(L, event);
220 lua_rawget(L, -2);
221 if (lua_isnil(L, -1)) {
222 lua_pop(L, 2); /* remove metatable and metafield */
223 return 0;
224 }
225 else {
226 lua_remove(L, -2); /* remove only metatable */
227 return 1;
228 }
229}
230
231
232LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
233 obj = abs_index(L, obj);
234 if (!luaL_getmetafield(L, obj, event)) /* no metafield? */
235 return 0;
236 lua_pushvalue(L, obj);
237 lua_call(L, 1, 1);
238 return 1;
239}
240
241
242LUALIB_API void (luaL_register) (lua_State *L, const char *libname,
243 const luaL_Reg *l) {
244 luaI_openlib(L, libname, l, 0);
245}
246
247
248static int libsize (const luaL_Reg *l) {
249 int size = 0;
250 for (; l->name; l++) size++;
251 return size;
252}
253
254
255LUALIB_API void luaI_openlib (lua_State *L, const char *libname,
256 const luaL_Reg *l, int nup) {
257 if (libname) {
258 int size = libsize(l);
259 /* check whether lib already exists */
260 luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1);
261 lua_getfield(L, -1, libname); /* get _LOADED[libname] */
262 if (!lua_istable(L, -1)) { /* not found? */
263 lua_pop(L, 1); /* remove previous result */
264 /* try global variable (and create one if it does not exist) */
265 if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != NULL)
266 luaL_error(L, "name conflict for module " LUA_QS, libname);
267 lua_pushvalue(L, -1);
268 lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */
269 }
270 lua_remove(L, -2); /* remove _LOADED table */
271 lua_insert(L, -(nup+1)); /* move library table to below upvalues */
272 }
273 for (; l->name; l++) {
274 int i;
275 for (i=0; i<nup; i++) /* copy upvalues to the top */
276 lua_pushvalue(L, -nup);
277 lua_pushcclosure(L, l->func, nup);
278 lua_setfield(L, -(nup+2), l->name);
279 }
280 lua_pop(L, nup); /* remove upvalues */
417} 281}
418 282
419/* }====================================================== */
420 283
421 284
422/* 285/*
423** {====================================================== 286** {======================================================
424** Generic Buffer manipulation 287** getn-setn: size for arrays
425** ======================================================= 288** =======================================================
426*/ 289*/
427 290
428/* 291#if defined(LUA_COMPAT_GETN)
429** check whether buffer is using a userdata on the stack as a temporary 292
430** buffer 293static int checkint (lua_State *L, int topop) {
431*/ 294 int n = (lua_type(L, -1) == LUA_TNUMBER) ? lua_tointeger(L, -1) : -1;
432#define buffonstack(B) ((B)->b != (B)->initb) 295 lua_pop(L, topop);
296 return n;
297}
298
299
300static void getsizes (lua_State *L) {
301 lua_getfield(L, LUA_REGISTRYINDEX, "LUA_SIZES");
302 if (lua_isnil(L, -1)) { /* no `size' table? */
303 lua_pop(L, 1); /* remove nil */
304 lua_newtable(L); /* create it */
305 lua_pushvalue(L, -1); /* `size' will be its own metatable */
306 lua_setmetatable(L, -2);
307 lua_pushliteral(L, "kv");
308 lua_setfield(L, -2, "__mode"); /* metatable(N).__mode = "kv" */
309 lua_pushvalue(L, -1);
310 lua_setfield(L, LUA_REGISTRYINDEX, "LUA_SIZES"); /* store in register */
311 }
312}
313
314
315LUALIB_API void luaL_setn (lua_State *L, int t, int n) {
316 t = abs_index(L, t);
317 lua_pushliteral(L, "n");
318 lua_rawget(L, t);
319 if (checkint(L, 1) >= 0) { /* is there a numeric field `n'? */
320 lua_pushliteral(L, "n"); /* use it */
321 lua_pushinteger(L, n);
322 lua_rawset(L, t);
323 }
324 else { /* use `sizes' */
325 getsizes(L);
326 lua_pushvalue(L, t);
327 lua_pushinteger(L, n);
328 lua_rawset(L, -3); /* sizes[t] = n */
329 lua_pop(L, 1); /* remove `sizes' */
330 }
331}
332
333
334LUALIB_API int luaL_getn (lua_State *L, int t) {
335 int n;
336 t = abs_index(L, t);
337 lua_pushliteral(L, "n"); /* try t.n */
338 lua_rawget(L, t);
339 if ((n = checkint(L, 1)) >= 0) return n;
340 getsizes(L); /* else try sizes[t] */
341 lua_pushvalue(L, t);
342 lua_rawget(L, -2);
343 if ((n = checkint(L, 2)) >= 0) return n;
344 return (int)lua_objlen(L, t);
345}
346
347#endif
348
349/* }====================================================== */
350
351
352
353LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
354 const char *r) {
355 const char *wild;
356 size_t l = strlen(p);
357 luaL_Buffer b;
358 luaL_buffinit(L, &b);
359 while ((wild = strstr(s, p)) != NULL) {
360 luaL_addlstring(&b, s, wild - s); /* push prefix */
361 luaL_addstring(&b, r); /* push replacement in place of pattern */
362 s = wild + l; /* continue after `p' */
363 }
364 luaL_addstring(&b, s); /* push last suffix */
365 luaL_pushresult(&b);
366 return lua_tostring(L, -1);
367}
368
369
370LUALIB_API const char *luaL_findtable (lua_State *L, int idx,
371 const char *fname, int szhint) {
372 const char *e;
373 lua_pushvalue(L, idx);
374 do {
375 e = strchr(fname, '.');
376 if (e == NULL) e = fname + strlen(fname);
377 lua_pushlstring(L, fname, e - fname);
378 lua_rawget(L, -2);
379 if (lua_isnil(L, -1)) { /* no such field? */
380 lua_pop(L, 1); /* remove this nil */
381 lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */
382 lua_pushlstring(L, fname, e - fname);
383 lua_pushvalue(L, -2);
384 lua_settable(L, -4); /* set new table into field */
385 }
386 else if (!lua_istable(L, -1)) { /* field has a non-table value? */
387 lua_pop(L, 2); /* remove table and value */
388 return fname; /* return problematic part of the name */
389 }
390 lua_remove(L, -2); /* remove previous table */
391 fname = e + 1;
392 } while (*e == '.');
393 return NULL;
394}
395
433 396
434 397
435/* 398/*
436** returns a pointer to a free area with at least 'sz' bytes 399** {======================================================
400** Generic Buffer manipulation
401** =======================================================
437*/ 402*/
438LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) { 403
439 lua_State *L = B->L; 404
440 if (B->size - B->n < sz) { /* not enough space? */ 405#define bufflen(B) ((B)->p - (B)->buffer)
441 char *newbuff; 406#define bufffree(B) ((size_t)(LUAL_BUFFERSIZE - bufflen(B)))
442 size_t newsize = B->size * 2; /* double buffer size */ 407
443 if (newsize - B->n < sz) /* not big enough? */ 408#define LIMIT (LUA_MINSTACK/2)
444 newsize = B->n + sz; 409
445 if (newsize < B->n || newsize - B->n < sz) 410
446 luaL_error(L, "buffer too large"); 411static int emptybuffer (luaL_Buffer *B) {
447 /* create larger buffer */ 412 size_t l = bufflen(B);
448 newbuff = (char *)lua_newuserdata(L, newsize * sizeof(char)); 413 if (l == 0) return 0; /* put nothing on stack */
449 /* move content to new buffer */ 414 else {
450 memcpy(newbuff, B->b, B->n * sizeof(char)); 415 lua_pushlstring(B->L, B->buffer, l);
451 if (buffonstack(B)) 416 B->p = B->buffer;
452 lua_remove(L, -2); /* remove old buffer */ 417 B->lvl++;
453 B->b = newbuff; 418 return 1;
454 B->size = newsize; 419 }
420}
421
422
423static void adjuststack (luaL_Buffer *B) {
424 if (B->lvl > 1) {
425 lua_State *L = B->L;
426 int toget = 1; /* number of levels to concat */
427 size_t toplen = lua_strlen(L, -1);
428 do {
429 size_t l = lua_strlen(L, -(toget+1));
430 if (B->lvl - toget + 1 >= LIMIT || toplen > l) {
431 toplen += l;
432 toget++;
433 }
434 else break;
435 } while (toget < B->lvl);
436 lua_concat(L, toget);
437 B->lvl = B->lvl - toget + 1;
455 } 438 }
456 return &B->b[B->n]; 439}
440
441
442LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B) {
443 if (emptybuffer(B))
444 adjuststack(B);
445 return B->buffer;
457} 446}
458 447
459 448
460LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) { 449LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
461 char *b = luaL_prepbuffsize(B, l); 450 while (l--)
462 memcpy(b, s, l * sizeof(char)); 451 luaL_addchar(B, *s++);
463 luaL_addsize(B, l);
464} 452}
465 453
466 454
@@ -470,72 +458,57 @@ LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
470 458
471 459
472LUALIB_API void luaL_pushresult (luaL_Buffer *B) { 460LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
473 lua_State *L = B->L; 461 emptybuffer(B);
474 lua_pushlstring(L, B->b, B->n); 462 lua_concat(B->L, B->lvl);
475 if (buffonstack(B)) 463 B->lvl = 1;
476 lua_remove(L, -2); /* remove old buffer */
477}
478
479
480LUALIB_API void luaL_pushresultsize (luaL_Buffer *B, size_t sz) {
481 luaL_addsize(B, sz);
482 luaL_pushresult(B);
483} 464}
484 465
485 466
486LUALIB_API void luaL_addvalue (luaL_Buffer *B) { 467LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
487 lua_State *L = B->L; 468 lua_State *L = B->L;
488 size_t l; 469 size_t vl;
489 const char *s = lua_tolstring(L, -1, &l); 470 const char *s = lua_tolstring(L, -1, &vl);
490 if (buffonstack(B)) 471 if (vl <= bufffree(B)) { /* fit into buffer? */
491 lua_insert(L, -2); /* put value below buffer */ 472 memcpy(B->p, s, vl); /* put it there */
492 luaL_addlstring(B, s, l); 473 B->p += vl;
493 lua_remove(L, (buffonstack(B)) ? -2 : -1); /* remove value */ 474 lua_pop(L, 1); /* remove from stack */
475 }
476 else {
477 if (emptybuffer(B))
478 lua_insert(L, -2); /* put buffer before new value */
479 B->lvl++; /* add new value into B stack */
480 adjuststack(B);
481 }
494} 482}
495 483
496 484
497LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) { 485LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
498 B->L = L; 486 B->L = L;
499 B->b = B->initb; 487 B->p = B->buffer;
500 B->n = 0; 488 B->lvl = 0;
501 B->size = LUAL_BUFFERSIZE;
502}
503
504
505LUALIB_API char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz) {
506 luaL_buffinit(L, B);
507 return luaL_prepbuffsize(B, sz);
508} 489}
509 490
510/* }====================================================== */ 491/* }====================================================== */
511 492
512 493
513/*
514** {======================================================
515** Reference system
516** =======================================================
517*/
518
519/* index of free-list header */
520#define freelist 0
521
522
523LUALIB_API int luaL_ref (lua_State *L, int t) { 494LUALIB_API int luaL_ref (lua_State *L, int t) {
524 int ref; 495 int ref;
496 t = abs_index(L, t);
525 if (lua_isnil(L, -1)) { 497 if (lua_isnil(L, -1)) {
526 lua_pop(L, 1); /* remove from stack */ 498 lua_pop(L, 1); /* remove from stack */
527 return LUA_REFNIL; /* `nil' has a unique fixed reference */ 499 return LUA_REFNIL; /* `nil' has a unique fixed reference */
528 } 500 }
529 t = lua_absindex(L, t); 501 lua_rawgeti(L, t, FREELIST_REF); /* get first free element */
530 lua_rawgeti(L, t, freelist); /* get first free element */ 502 ref = (int)lua_tointeger(L, -1); /* ref = t[FREELIST_REF] */
531 ref = (int)lua_tointeger(L, -1); /* ref = t[freelist] */
532 lua_pop(L, 1); /* remove it from stack */ 503 lua_pop(L, 1); /* remove it from stack */
533 if (ref != 0) { /* any free element? */ 504 if (ref != 0) { /* any free element? */
534 lua_rawgeti(L, t, ref); /* remove it from list */ 505 lua_rawgeti(L, t, ref); /* remove it from list */
535 lua_rawseti(L, t, freelist); /* (t[freelist] = t[ref]) */ 506 lua_rawseti(L, t, FREELIST_REF); /* (t[FREELIST_REF] = t[ref]) */
507 }
508 else { /* no free elements */
509 ref = (int)lua_objlen(L, t);
510 ref++; /* create new reference */
536 } 511 }
537 else /* no free elements */
538 ref = (int)lua_rawlen(L, t) + 1; /* get a new reference */
539 lua_rawseti(L, t, ref); 512 lua_rawseti(L, t, ref);
540 return ref; 513 return ref;
541} 514}
@@ -543,15 +516,14 @@ LUALIB_API int luaL_ref (lua_State *L, int t) {
543 516
544LUALIB_API void luaL_unref (lua_State *L, int t, int ref) { 517LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
545 if (ref >= 0) { 518 if (ref >= 0) {
546 t = lua_absindex(L, t); 519 t = abs_index(L, t);
547 lua_rawgeti(L, t, freelist); 520 lua_rawgeti(L, t, FREELIST_REF);
548 lua_rawseti(L, t, ref); /* t[ref] = t[freelist] */ 521 lua_rawseti(L, t, ref); /* t[ref] = t[FREELIST_REF] */
549 lua_pushinteger(L, ref); 522 lua_pushinteger(L, ref);
550 lua_rawseti(L, t, freelist); /* t[freelist] = ref */ 523 lua_rawseti(L, t, FREELIST_REF); /* t[FREELIST_REF] = ref */
551 } 524 }
552} 525}
553 526
554/* }====================================================== */
555 527
556 528
557/* 529/*
@@ -561,28 +533,22 @@ LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
561*/ 533*/
562 534
563typedef struct LoadF { 535typedef struct LoadF {
564 int n; /* number of pre-read characters */ 536 int extraline;
565 int f; 537 int f;
566 char buff[LUAL_BUFFERSIZE]; /* area for reading file */ 538 char buff[LUAL_BUFFERSIZE];
567} LoadF; 539} LoadF;
568 540
569 541static const char *getF(lua_State *L, void *ud, size_t *size) {
570static const char *getF (lua_State *L, void *ud, size_t *size) {
571 LoadF *lf = (LoadF *)ud; 542 LoadF *lf = (LoadF *)ud;
572 (void)L; /* not used */ 543 (void)L;
573 if (lf->n > 0) { /* are there pre-read characters to be read? */ 544 if (lf->extraline) {
574 *size = lf->n; /* return them (chars already in buffer) */ 545 lf->extraline = 0;
575 lf->n = 0; /* no more pre-read characters */ 546 *size = 1;
576 } 547 return "\n";
577 else { /* read a block from file */
578 /* 'fread' can return > 0 *and* set the EOF flag. If next call to
579 'getF' called 'fread', it might still wait for user input.
580 The next check avoids this problem. */
581 *size = rb->read(lf->f, lf->buff, LUAL_BUFFERSIZE);
582 if (*size <= 0) return NULL;
583 return (*size > 0) ? lf->buff : NULL;
584 } 548 }
585 return lf->buff; 549 *size = rb->read(lf->f, lf->buff, LUAL_BUFFERSIZE);
550 if (*size <= 0) return NULL;
551 return (*size > 0) ? lf->buff : NULL;
586} 552}
587 553
588 554
@@ -594,68 +560,16 @@ static int errfile (lua_State *L, const char *what, int fnameindex) {
594 return LUA_ERRFILE; 560 return LUA_ERRFILE;
595} 561}
596 562
597 563LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
598static int skipBOM (LoadF *lf) {
599 const char *p = "\xEF\xBB\xBF"; /* Utf8 BOM mark */
600 int c;
601 lf->n = 0;
602 do {
603 c = PREFIX(getc)(lf->f);
604 if (c == EOF || c != *(const unsigned char *)p++) return c;
605 lf->buff[lf->n++] = c; /* to be read by the parser */
606 } while (*p != '\0');
607 lf->n = 0; /* prefix matched; discard it */
608 return PREFIX(getc)(lf->f); /* return next character */
609}
610
611
612/*
613** reads the first character of file 'f' and skips an optional BOM mark
614** in its beginning plus its first line if it starts with '#'. Returns
615** true if it skipped the first line. In any case, '*cp' has the
616** first "valid" character of the file (after the optional BOM and
617** a first-line comment).
618*/
619static int skipcomment (LoadF *lf, int *cp) {
620 int c = *cp = skipBOM(lf);
621 if (c == '#') { /* first line is a comment (Unix exec. file)? */
622 do { /* skip first line */
623 c = PREFIX(getc)(lf->f);
624 } while (c != EOF && c != '\n') ;
625 *cp = PREFIX(getc)(lf->f); /* skip end-of-line, if present */
626 return 1; /* there was a comment */
627 }
628 else return 0; /* no comment */
629}
630
631
632LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
633 const char *mode) {
634 LoadF lf; 564 LoadF lf;
635 int status, readstatus; 565 int status;
636 int c;
637 int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ 566 int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */
638 lua_pushfstring(L, "@%s", filename); 567 lf.extraline = 0;
639 lf.n = 0;
640 lf.f = rb->open(filename, O_RDONLY); 568 lf.f = rb->open(filename, O_RDONLY);
569 lua_pushfstring(L, "@%s", filename);
641 if (lf.f < 0) return errfile(L, "open", fnameindex); 570 if (lf.f < 0) return errfile(L, "open", fnameindex);
642 if (skipcomment(&lf, &c)) /* read initial portion */ 571 status = lua_load(L, getF, &lf, lua_tostring(L, -1));
643 lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */ 572 rb->close(lf.f);
644 if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */
645 rb->close(lf.f);
646 lf.f = rb->open(filename, O_RDONLY);
647 if (lf.f < 0) return errfile(L, "reopen", fnameindex);
648 skipcomment(&lf, &c); /* re-read initial portion */
649 }
650 if (c != EOF)
651 lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */
652 status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode);
653 readstatus = 0;/*ferror(lf.f);*/
654 if (filename) rb->close(lf.f); /* close file (even in case of errors) */
655 if (readstatus) {
656 lua_settop(L, fnameindex); /* ignore results from `lua_load' */
657 return errfile(L, "read", fnameindex);
658 }
659 lua_remove(L, fnameindex); 573 lua_remove(L, fnameindex);
660 return status; 574 return status;
661} 575}
@@ -669,7 +583,7 @@ typedef struct LoadS {
669 583
670static const char *getS (lua_State *L, void *ud, size_t *size) { 584static const char *getS (lua_State *L, void *ud, size_t *size) {
671 LoadS *ls = (LoadS *)ud; 585 LoadS *ls = (LoadS *)ud;
672 (void)L; /* not used */ 586 (void)L;
673 if (ls->size == 0) return NULL; 587 if (ls->size == 0) return NULL;
674 *size = ls->size; 588 *size = ls->size;
675 ls->size = 0; 589 ls->size = 0;
@@ -677,245 +591,27 @@ static const char *getS (lua_State *L, void *ud, size_t *size) {
677} 591}
678 592
679 593
680LUALIB_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t size, 594LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size,
681 const char *name, const char *mode) { 595 const char *name) {
682 LoadS ls; 596 LoadS ls;
683 ls.s = buff; 597 ls.s = buff;
684 ls.size = size; 598 ls.size = size;
685 return lua_load(L, getS, &ls, name, mode); 599 return lua_load(L, getS, &ls, name);
686} 600}
687 601
688 602
689LUALIB_API int luaL_loadstring (lua_State *L, const char *s) { 603LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s) {
690 return luaL_loadbuffer(L, s, strlen(s), s); 604 return luaL_loadbuffer(L, s, strlen(s), s);
691} 605}
692 606
693/* }====================================================== */
694
695
696
697LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
698 if (!lua_getmetatable(L, obj)) /* no metatable? */
699 return 0;
700 lua_pushstring(L, event);
701 lua_rawget(L, -2);
702 if (lua_isnil(L, -1)) {
703 lua_pop(L, 2); /* remove metatable and metafield */
704 return 0;
705 }
706 else {
707 lua_remove(L, -2); /* remove only metatable */
708 return 1;
709 }
710}
711
712
713LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
714 obj = lua_absindex(L, obj);
715 if (!luaL_getmetafield(L, obj, event)) /* no metafield? */
716 return 0;
717 lua_pushvalue(L, obj);
718 lua_call(L, 1, 1);
719 return 1;
720}
721
722
723LUALIB_API int luaL_len (lua_State *L, int idx) {
724 int l;
725 int isnum;
726 lua_len(L, idx);
727 l = (int)lua_tointegerx(L, -1, &isnum);
728 if (!isnum)
729 luaL_error(L, "object length is not a number");
730 lua_pop(L, 1); /* remove object */
731 return l;
732}
733
734
735LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
736 if (!luaL_callmeta(L, idx, "__tostring")) { /* no metafield? */
737 switch (lua_type(L, idx)) {
738 case LUA_TNUMBER:
739 case LUA_TSTRING:
740 lua_pushvalue(L, idx);
741 break;
742 case LUA_TBOOLEAN:
743 lua_pushstring(L, (lua_toboolean(L, idx) ? "true" : "false"));
744 break;
745 case LUA_TNIL:
746 lua_pushliteral(L, "nil");
747 break;
748 default:
749 lua_pushfstring(L, "%s: %p", luaL_typename(L, idx),
750 lua_topointer(L, idx));
751 break;
752 }
753 }
754 return lua_tolstring(L, -1, len);
755}
756
757
758/*
759** {======================================================
760** Compatibility with 5.1 module functions
761** =======================================================
762*/
763#if defined(LUA_COMPAT_MODULE)
764
765static const char *luaL_findtable (lua_State *L, int idx,
766 const char *fname, int szhint) {
767 const char *e;
768 if (idx) lua_pushvalue(L, idx);
769 do {
770 e = strchr(fname, '.');
771 if (e == NULL) e = fname + strlen(fname);
772 lua_pushlstring(L, fname, e - fname);
773 lua_rawget(L, -2);
774 if (lua_isnil(L, -1)) { /* no such field? */
775 lua_pop(L, 1); /* remove this nil */
776 lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */
777 lua_pushlstring(L, fname, e - fname);
778 lua_pushvalue(L, -2);
779 lua_settable(L, -4); /* set new table into field */
780 }
781 else if (!lua_istable(L, -1)) { /* field has a non-table value? */
782 lua_pop(L, 2); /* remove table and value */
783 return fname; /* return problematic part of the name */
784 }
785 lua_remove(L, -2); /* remove previous table */
786 fname = e + 1;
787 } while (*e == '.');
788 return NULL;
789}
790
791
792/*
793** Count number of elements in a luaL_Reg list.
794*/
795static int libsize (const luaL_Reg *l) {
796 int size = 0;
797 for (; l && l->name; l++) size++;
798 return size;
799}
800
801
802/*
803** Find or create a module table with a given name. The function
804** first looks at the _LOADED table and, if that fails, try a
805** global variable with that name. In any case, leaves on the stack
806** the module table.
807*/
808LUALIB_API void luaL_pushmodule (lua_State *L, const char *modname,
809 int sizehint) {
810 luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); /* get _LOADED table */
811 lua_getfield(L, -1, modname); /* get _LOADED[modname] */
812 if (!lua_istable(L, -1)) { /* not found? */
813 lua_pop(L, 1); /* remove previous result */
814 /* try global variable (and create one if it does not exist) */
815 lua_pushglobaltable(L);
816 if (luaL_findtable(L, 0, modname, sizehint) != NULL)
817 luaL_error(L, "name conflict for module " LUA_QS, modname);
818 lua_pushvalue(L, -1);
819 lua_setfield(L, -3, modname); /* _LOADED[modname] = new table */
820 }
821 lua_remove(L, -2); /* remove _LOADED table */
822}
823 607
824 608
825LUALIB_API void luaL_openlib (lua_State *L, const char *libname,
826 const luaL_Reg *l, int nup) {
827 luaL_checkversion(L);
828 if (libname) {
829 luaL_pushmodule(L, libname, libsize(l)); /* get/create library table */
830 lua_insert(L, -(nup + 1)); /* move library table to below upvalues */
831 }
832 if (l)
833 luaL_setfuncs(L, l, nup);
834 else
835 lua_pop(L, nup); /* remove upvalues */
836}
837
838#endif
839/* }====================================================== */ 609/* }====================================================== */
840 610
841/*
842** set functions from list 'l' into table at top - 'nup'; each
843** function gets the 'nup' elements at the top as upvalues.
844** Returns with only the table at the stack.
845*/
846LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
847 luaL_checkversion(L);
848 luaL_checkstack(L, nup, "too many upvalues");
849 for (; l->name != NULL; l++) { /* fill the table with given functions */
850 int i;
851 for (i = 0; i < nup; i++) /* copy upvalues to the top */
852 lua_pushvalue(L, -nup);
853 lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
854 lua_setfield(L, -(nup + 2), l->name);
855 }
856 lua_pop(L, nup); /* remove upvalues */
857}
858
859
860/*
861** ensure that stack[idx][fname] has a table and push that table
862** into the stack
863*/
864LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) {
865 lua_getfield(L, idx, fname);
866 if (lua_istable(L, -1)) return 1; /* table already there */
867 else {
868 lua_pop(L, 1); /* remove previous result */
869 idx = lua_absindex(L, idx);
870 lua_newtable(L);
871 lua_pushvalue(L, -1); /* copy to be left at top */
872 lua_setfield(L, idx, fname); /* assign new table to field */
873 return 0; /* false, because did not find table there */
874 }
875}
876
877
878/*
879** stripped-down 'require'. Calls 'openf' to open a module,
880** registers the result in 'package.loaded' table and, if 'glb'
881** is true, also registers the result in the global table.
882** Leaves resulting module on the top.
883*/
884LUALIB_API void luaL_requiref (lua_State *L, const char *modname,
885 lua_CFunction openf, int glb) {
886 lua_pushcfunction(L, openf);
887 lua_pushstring(L, modname); /* argument to open function */
888 lua_call(L, 1, 1); /* open module */
889 luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED");
890 lua_pushvalue(L, -2); /* make copy of module (call result) */
891 lua_setfield(L, -2, modname); /* _LOADED[modname] = module */
892 lua_pop(L, 1); /* remove _LOADED table */
893 if (glb) {
894 lua_pushvalue(L, -1); /* copy of 'mod' */
895 lua_setglobal(L, modname); /* _G[modname] = module */
896 }
897}
898
899
900LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
901 const char *r) {
902 const char *wild;
903 size_t l = strlen(p);
904 luaL_Buffer b;
905 luaL_buffinit(L, &b);
906 while ((wild = strstr(s, p)) != NULL) {
907 luaL_addlstring(&b, s, wild - s); /* push prefix */
908 luaL_addstring(&b, r); /* push replacement in place of pattern */
909 s = wild + l; /* continue after `p' */
910 }
911 luaL_addstring(&b, s); /* push last suffix */
912 luaL_pushresult(&b);
913 return lua_tostring(L, -1);
914}
915
916 611
917static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { 612static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
918 (void)ud; (void)osize; /* not used */ 613 (void)ud;
614 (void)osize;
919 if (nsize == 0) { 615 if (nsize == 0) {
920 free(ptr); 616 free(ptr);
921 return NULL; 617 return NULL;
@@ -926,11 +622,12 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
926 622
927 623
928static int panic (lua_State *L) { 624static int panic (lua_State *L) {
929 DEBUGF("PANIC: unprotected error in call to Lua API (%s)\n", 625 DEBUGF("PANIC: unprotected error in call to Lua API (%s)\n",
930 lua_tostring(L, -1)); 626 lua_tostring(L, -1));
931 rb->splashf(5 * HZ, "PANIC: unprotected error in call to Lua API (%s)", 627 rb->splashf(5 * HZ, "PANIC: unprotected error in call to Lua API (%s)",
932 lua_tostring(L, -1)); 628 lua_tostring(L, -1));
933 return 0; /* return to Lua to abort */ 629
630 return 0;
934} 631}
935 632
936 633
@@ -940,20 +637,3 @@ LUALIB_API lua_State *luaL_newstate (void) {
940 return L; 637 return L;
941} 638}
942 639
943
944LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver) {
945 const lua_Number *v = lua_version(L);
946 if (v != lua_version(NULL))
947 luaL_error(L, "multiple Lua VMs detected");
948 else if (*v != ver)
949 luaL_error(L, "version mismatch: app. needs %f, Lua core provides %f",
950 ver, *v);
951 /* check conversions number -> integer types */
952 lua_pushnumber(L, -(lua_Number)0x1234);
953 if (lua_tointeger(L, -1) != -0x1234 ||
954 lua_tounsigned(L, -1) != (lua_Unsigned)-0x1234)
955 luaL_error(L, "bad conversion number->int;"
956 " must recompile Lua with proper settings");
957 lua_pop(L, 1);
958}
959