annotate src/lua/lauxlib.c @ 52:9f63ad276c0d tip

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