new format for error messages

This commit is contained in:
Roberto Ierusalimschy 2002-05-15 15:57:44 -03:00
parent 1c328a191a
commit b7a0503c1d
15 changed files with 113 additions and 96 deletions

7
lapi.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lapi.c,v 1.189 2002/05/06 19:05:10 roberto Exp roberto $ ** $Id: lapi.c,v 1.190 2002/05/07 17:36:56 roberto Exp roberto $
** Lua API ** Lua API
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -11,6 +11,7 @@
#include "lua.h" #include "lua.h"
#include "lapi.h" #include "lapi.h"
#include "ldebug.h"
#include "ldo.h" #include "ldo.h"
#include "lfunc.h" #include "lfunc.h"
#include "lgc.h" #include "lgc.h"
@ -521,8 +522,8 @@ LUA_API void lua_setmetatable (lua_State *L, int objindex) {
uvalue(obj)->uv.metatable = hvalue(mt); uvalue(obj)->uv.metatable = hvalue(mt);
break; break;
default: default:
luaO_verror(L, "cannot change the meta table of a %s", luaG_runerror(L, "cannot change the meta table of a %s",
luaT_typenames[ttype(obj)]); luaT_typenames[ttype(obj)]);
} }
lua_unlock(L); lua_unlock(L);
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lauxlib.c,v 1.68 2002/05/06 19:05:10 roberto Exp roberto $ ** $Id: lauxlib.c,v 1.69 2002/05/07 17:36:56 roberto Exp roberto $
** Auxiliary functions for building Lua libraries ** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -151,10 +151,17 @@ LUALIB_API void luaL_vstr (lua_State *L, const char *fmt, ...) {
LUALIB_API int luaL_verror (lua_State *L, const char *fmt, ...) { LUALIB_API int luaL_verror (lua_State *L, const char *fmt, ...) {
lua_Debug ar;
va_list argp; va_list argp;
va_start(argp, fmt); va_start(argp, fmt);
lua_vpushstr(L, fmt, argp); lua_vpushstr(L, fmt, argp);
va_end(argp); va_end(argp);
if (lua_getstack(L, 1, &ar)) { /* check calling function */
lua_getinfo(L, "Snl", &ar);
if (ar.currentline > 0)
luaL_vstr(L, "%s:%d: %s",
ar.short_src, ar.currentline, lua_tostring(L, -1));
}
return lua_errorobj(L); return lua_errorobj(L);
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldblib.c,v 1.50 2002/05/06 19:05:10 roberto Exp roberto $ ** $Id: ldblib.c,v 1.51 2002/05/07 17:36:56 roberto Exp roberto $
** Interface from Lua to its debug API ** Interface from Lua to its debug API
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -54,8 +54,7 @@ static int getinfo (lua_State *L) {
switch (*options) { switch (*options) {
case 'S': case 'S':
settabss(L, "source", ar.source); settabss(L, "source", ar.source);
if (ar.source) settabss(L, "short_src", ar.short_src);
settabss(L, "short_src", ar.short_src);
settabsi(L, "linedefined", ar.linedefined); settabsi(L, "linedefined", ar.linedefined);
settabss(L, "what", ar.what); settabss(L, "what", ar.what);
break; break;
@ -205,31 +204,28 @@ static int errorfb (lua_State *L) {
firstpart = 0; firstpart = 0;
continue; continue;
} }
sprintf(buff, "%4d: ", level-1); sprintf(buff, "%4d- ", level-1);
lua_pushstring(L, buff); lua_pushstring(L, buff);
lua_getinfo(L, "Snl", &ar); lua_getinfo(L, "Snl", &ar);
luaL_vstr(L, "%s:", ar.short_src);
if (ar.currentline > 0)
luaL_vstr(L, "%d:", ar.currentline);
switch (*ar.namewhat) { switch (*ar.namewhat) {
case 'g': case 'l': /* global, local */ case 'g': /* global */
luaL_vstr(L, "function `%s'", ar.name); case 'l': /* local */
break;
case 'f': /* field */ case 'f': /* field */
case 'm': /* method */ case 'm': /* method */
luaL_vstr(L, "method `%s'", ar.name); luaL_vstr(L, " in function `%s'", ar.name);
break; break;
default: { default: {
if (*ar.what == 'm') /* main? */ if (*ar.what == 'm') /* main? */
luaL_vstr(L, "main of %s", ar.short_src); luaL_vstr(L, " in main chunk");
else if (*ar.what == 'C') /* C function? */ else if (*ar.what == 'C') /* C function? */
luaL_vstr(L, "%s", ar.short_src); luaL_vstr(L, "%s", ar.short_src);
else else
luaL_vstr(L, "function <%d:%s>", ar.linedefined, ar.short_src); luaL_vstr(L, " in function <%s:%d>", ar.short_src, ar.linedefined);
ar.source = NULL; /* do not print source again */
} }
} }
if (ar.currentline > 0)
luaL_vstr(L, " at line %d", ar.currentline);
if (ar.source)
luaL_vstr(L, " [%s]", ar.short_src);
lua_pushliteral(L, "\n"); lua_pushliteral(L, "\n");
lua_concat(L, lua_gettop(L)); lua_concat(L, lua_gettop(L));
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldebug.c,v 1.114 2002/05/13 13:09:00 roberto Exp roberto $ ** $Id: ldebug.c,v 1.115 2002/05/14 17:52:22 roberto Exp roberto $
** Debug Interface ** Debug Interface
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -144,11 +144,11 @@ static void funcinfo (lua_State *L, lua_Debug *ar, StkId func) {
if (ttype(func) == LUA_TFUNCTION) if (ttype(func) == LUA_TFUNCTION)
cl = clvalue(func); cl = clvalue(func);
else { else {
luaD_runerror(L, "value for `lua_getinfo' is not a function"); luaG_runerror(L, "value for `lua_getinfo' is not a function");
cl = NULL; /* to avoid warnings */ cl = NULL; /* to avoid warnings */
} }
if (cl->c.isC) { if (cl->c.isC) {
ar->source = "=C"; ar->source = "=[C]";
ar->linedefined = -1; ar->linedefined = -1;
ar->what = "C"; ar->what = "C";
} }
@ -481,10 +481,10 @@ void luaG_typeerror (lua_State *L, const TObject *o, const char *op) {
if (isinstack(L->ci, o)) if (isinstack(L->ci, o))
kind = getobjname(L, L->ci, o - L->ci->base, &name); kind = getobjname(L, L->ci, o - L->ci->base, &name);
if (kind) if (kind)
luaO_verror(L, "attempt to %s %s `%s' (a %s value)", luaG_runerror(L, "attempt to %s %s `%s' (a %s value)",
op, kind, name, t); op, kind, name, t);
else else
luaO_verror(L, "attempt to %s a %s value", op, t); luaG_runerror(L, "attempt to %s a %s value", op, t);
} }
@ -507,8 +507,24 @@ void luaG_ordererror (lua_State *L, const TObject *p1, const TObject *p2) {
const char *t1 = luaT_typenames[ttype(p1)]; const char *t1 = luaT_typenames[ttype(p1)];
const char *t2 = luaT_typenames[ttype(p2)]; const char *t2 = luaT_typenames[ttype(p2)];
if (t1[2] == t2[2]) if (t1[2] == t2[2])
luaO_verror(L, "attempt to compare two %s values", t1); luaG_runerror(L, "attempt to compare two %s values", t1);
else else
luaO_verror(L, "attempt to compare %s with %s", t1, t2); luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
}
void luaG_runerror (lua_State *L, const char *fmt, ...) {
const char *msg;
va_list argp;
va_start(argp, fmt);
msg = luaO_vpushstr(L, fmt, argp);
va_end(argp);
if (isLmark(L->ci)) {
char buff[LUA_IDSIZE];
int line = currentline(L, L->ci);
luaO_chunkid(buff, getstr(getluaproto(L->ci)->source), LUA_IDSIZE);
msg = luaO_pushstr(L, "%s:%d: %s", buff, line, msg);
}
luaD_error(L, msg, LUA_ERRRUN);
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldebug.h,v 1.19 2002/04/10 12:11:07 roberto Exp roberto $ ** $Id: ldebug.h,v 1.20 2002/05/02 13:06:20 roberto Exp roberto $
** Auxiliary functions from Debug Interface module ** Auxiliary functions from Debug Interface module
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -20,6 +20,7 @@ void luaG_typeerror (lua_State *L, const TObject *o, const char *opname);
void luaG_concaterror (lua_State *L, StkId p1, StkId p2); void luaG_concaterror (lua_State *L, StkId p1, StkId p2);
void luaG_aritherror (lua_State *L, StkId p1, const TObject *p2); void luaG_aritherror (lua_State *L, StkId p1, const TObject *p2);
void luaG_ordererror (lua_State *L, const TObject *p1, const TObject *p2); void luaG_ordererror (lua_State *L, const TObject *p1, const TObject *p2);
void luaG_runerror (lua_State *L, const char *fmt, ...);
int luaG_checkcode (const Proto *pt); int luaG_checkcode (const Proto *pt);

17
ldo.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldo.c,v 1.173 2002/05/01 20:40:42 roberto Exp roberto $ ** $Id: ldo.c,v 1.174 2002/05/07 17:36:56 roberto Exp roberto $
** Stack and Call structure of Lua ** Stack and Call structure of Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -113,7 +113,7 @@ static void luaD_growCI (lua_State *L) {
else { else {
luaD_reallocCI(L, 2*L->size_ci); luaD_reallocCI(L, 2*L->size_ci);
if (L->size_ci > LUA_MAXCALLS) if (L->size_ci > LUA_MAXCALLS)
luaD_runerror(L, "stack overflow"); luaG_runerror(L, "stack overflow");
} }
L->ci++; L->ci++;
} }
@ -279,7 +279,7 @@ void luaD_call (lua_State *L, StkId func, int nResults) {
firstResult = luaV_execute(L); /* call it */ firstResult = luaV_execute(L); /* call it */
if (firstResult == NULL) { if (firstResult == NULL) {
luaD_poscall(L, 0, L->top); luaD_poscall(L, 0, L->top);
luaD_runerror(L, "attempt to `yield' across tag-method/C-call boundary"); luaG_runerror(L, "attempt to `yield' across tag-method/C-call boundary");
} }
} }
luaD_poscall(L, nResults, firstResult); luaD_poscall(L, nResults, firstResult);
@ -335,9 +335,9 @@ LUA_API int lua_resume (lua_State *L, lua_State *co) {
lua_lock(L); lua_lock(L);
ci = co->ci; ci = co->ci;
if (ci == co->base_ci) /* no activation record? ?? */ if (ci == co->base_ci) /* no activation record? ?? */
luaD_runerror(L, "thread is dead - cannot be resumed"); luaG_runerror(L, "thread is dead - cannot be resumed");
if (co->errorJmp != NULL) /* ?? */ if (co->errorJmp != NULL) /* ?? */
luaD_runerror(L, "thread is active - cannot be resumed"); luaG_runerror(L, "thread is active - cannot be resumed");
if (L->errorJmp) { if (L->errorJmp) {
setobj(&ud.err, L->errorJmp->err); setobj(&ud.err, L->errorJmp->err);
} }
@ -359,7 +359,7 @@ LUA_API int lua_yield (lua_State *L, int nresults) {
lua_lock(L); lua_lock(L);
ci = L->ci; ci = L->ci;
if (ci_func(ci-1)->c.isC) if (ci_func(ci-1)->c.isC)
luaD_runerror(L, "cannot `yield' a C function"); luaG_runerror(L, "cannot `yield' a C function");
ci->yield_results = nresults; ci->yield_results = nresults;
lua_unlock(L); lua_unlock(L);
return -1; return -1;
@ -492,11 +492,6 @@ void luaD_error (lua_State *L, const char *s, int errcode) {
} }
void luaD_runerror (lua_State *L, const char *s) {
luaD_error(L, s, LUA_ERRRUN);
}
int luaD_runprotected (lua_State *L, Pfunc f, TObject *ud) { int luaD_runprotected (lua_State *L, Pfunc f, TObject *ud) {
struct lua_longjmp lj; struct lua_longjmp lj;
lj.ci = L->ci; lj.ci = L->ci;

3
ldo.h
View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldo.h,v 1.43 2002/04/22 14:40:50 roberto Exp roberto $ ** $Id: ldo.h,v 1.44 2002/05/01 20:40:42 roberto Exp roberto $
** Stack and Call structure of Lua ** Stack and Call structure of Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -43,7 +43,6 @@ void luaD_growstack (lua_State *L, int n);
void luaD_error (lua_State *L, const char *s, int errcode); void luaD_error (lua_State *L, const char *s, int errcode);
void luaD_errorobj (lua_State *L, const TObject *s, int errcode); void luaD_errorobj (lua_State *L, const TObject *s, int errcode);
void luaD_runerror (lua_State *L, const char *s);
int luaD_runprotected (lua_State *L, Pfunc f, TObject *ud); int luaD_runprotected (lua_State *L, Pfunc f, TObject *ud);

5
llex.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: llex.c,v 1.99 2002/03/08 19:25:24 roberto Exp roberto $ ** $Id: llex.c,v 1.100 2002/05/07 17:36:56 roberto Exp roberto $
** Lexical Analyzer ** Lexical Analyzer
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -61,8 +61,7 @@ static void luaX_error (LexState *ls, const char *s, const char *token) {
lua_State *L = ls->L; lua_State *L = ls->L;
char buff[MAXSRC]; char buff[MAXSRC];
luaO_chunkid(buff, getstr(ls->source), MAXSRC); luaO_chunkid(buff, getstr(ls->source), MAXSRC);
luaO_pushstr(L, "%s;\n last token read: `%s' at line %d in %s", luaO_pushstr(L, "%s:%d: %s near `%s'", buff, ls->linenumber, s, token);
s, token, ls->linenumber, buff);
luaD_errorobj(L, L->top - 1, LUA_ERRSYNTAX); luaD_errorobj(L, L->top - 1, LUA_ERRSYNTAX);
} }

7
lmem.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lmem.c,v 1.53 2002/04/22 14:40:23 roberto Exp roberto $ ** $Id: lmem.c,v 1.54 2002/05/01 20:40:42 roberto Exp roberto $
** Interface to Memory Manager ** Interface to Memory Manager
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -9,6 +9,7 @@
#include "lua.h" #include "lua.h"
#include "ldebug.h"
#include "ldo.h" #include "ldo.h"
#include "lmem.h" #include "lmem.h"
#include "lobject.h" #include "lobject.h"
@ -34,7 +35,7 @@ void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems,
else if (*size >= limit/2) { /* cannot double it? */ else if (*size >= limit/2) { /* cannot double it? */
if (*size < limit - MINSIZEARRAY) /* try something smaller... */ if (*size < limit - MINSIZEARRAY) /* try something smaller... */
newsize = limit; /* still have at least MINSIZEARRAY free places */ newsize = limit; /* still have at least MINSIZEARRAY free places */
else luaD_runerror(L, errormsg); else luaG_runerror(L, errormsg);
} }
newblock = luaM_realloc(L, block, newblock = luaM_realloc(L, block,
cast(lu_mem, *size)*cast(lu_mem, size_elems), cast(lu_mem, *size)*cast(lu_mem, size_elems),
@ -53,7 +54,7 @@ void *luaM_realloc (lua_State *L, void *block, lu_mem oldsize, lu_mem size) {
block = NULL; block = NULL;
} }
else if (size >= MAX_SIZET) else if (size >= MAX_SIZET)
luaD_runerror(L, "memory allocation error: block too big"); luaG_runerror(L, "memory allocation error: block too big");
else { else {
block = l_realloc(block, oldsize, size); block = l_realloc(block, oldsize, size);
if (block == NULL) { if (block == NULL) {

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lobject.c,v 1.78 2002/05/06 15:51:41 roberto Exp roberto $ ** $Id: lobject.c,v 1.79 2002/05/07 17:36:56 roberto Exp roberto $
** Some generic functions over Lua objects ** Some generic functions over Lua objects
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -100,7 +100,8 @@ static void pushstr (lua_State *L, const char *str) {
/* this function handles only `%d', `%c', %f, and `%s' formats */ /* this function handles only `%d', `%c', %f, and `%s' formats */
const char *luaO_vpushstr (lua_State *L, const char *fmt, va_list argp) { const char *luaO_vpushstr (lua_State *L, const char *fmt, va_list argp) {
int n = 0; int n = 1;
pushstr(L, "");
for (;;) { for (;;) {
const char *e = strchr(fmt, '%'); const char *e = strchr(fmt, '%');
if (e == NULL) break; if (e == NULL) break;
@ -150,47 +151,36 @@ const char *luaO_pushstr (lua_State *L, const char *fmt, ...) {
} }
void luaO_verror (lua_State *L, const char *fmt, ...) {
const char *msg;
va_list argp;
va_start(argp, fmt);
msg = luaO_vpushstr(L, fmt, argp);
va_end(argp);
luaD_runerror(L, msg);
}
void luaO_chunkid (char *out, const char *source, int bufflen) { void luaO_chunkid (char *out, const char *source, int bufflen) {
if (*source == '=') { if (*source == '=') {
strncpy(out, source+1, bufflen); /* remove first char */ strncpy(out, source+1, bufflen); /* remove first char */
out[bufflen-1] = '\0'; /* ensures null termination */ out[bufflen-1] = '\0'; /* ensures null termination */
} }
else { /* out = "file `source'", or "file `...source'" */ else { /* out = "source", or "...source" */
if (*source == '@') { if (*source == '@') {
int l; int l;
source++; /* skip the `@' */ source++; /* skip the `@' */
bufflen -= sizeof(" file `...' "); bufflen -= sizeof(" `...' ");
l = strlen(source); l = strlen(source);
strcpy(out, "file `"); strcpy(out, "");
if (l>bufflen) { if (l>bufflen) {
source += (l-bufflen); /* get last part of file name */ source += (l-bufflen); /* get last part of file name */
strcat(out, "..."); strcat(out, "...");
} }
strcat(out, source); strcat(out, source);
strcat(out, "'");
} }
else { else { /* out = [string "string"] */
int len = strcspn(source, "\n"); /* stop at first newline */ int len = strcspn(source, "\n"); /* stop at first newline */
bufflen -= sizeof(" string \"...\" "); bufflen -= sizeof(" [string \"...\"] ");
if (len > bufflen) len = bufflen; if (len > bufflen) len = bufflen;
strcpy(out, "string \""); strcpy(out, "[string \"");
if (source[len] != '\0') { /* must truncate? */ if (source[len] != '\0') { /* must truncate? */
strncat(out, source, len); strncat(out, source, len);
strcat(out, "..."); strcat(out, "...");
} }
else else
strcat(out, source); strcat(out, source);
strcat(out, "\""); strcat(out, "\"]");
} }
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lobject.h,v 1.130 2002/05/06 15:51:41 roberto Exp roberto $ ** $Id: lobject.h,v 1.131 2002/05/07 17:36:56 roberto Exp roberto $
** Type definitions for Lua objects ** Type definitions for Lua objects
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -252,7 +252,6 @@ int luaO_str2d (const char *s, lua_Number *result);
const char *luaO_vpushstr (lua_State *L, const char *fmt, va_list argp); const char *luaO_vpushstr (lua_State *L, const char *fmt, va_list argp);
const char *luaO_pushstr (lua_State *L, const char *fmt, ...); const char *luaO_pushstr (lua_State *L, const char *fmt, ...);
void luaO_verror (lua_State *L, const char *fmt, ...);
void luaO_chunkid (char *out, const char *source, int len); void luaO_chunkid (char *out, const char *source, int len);

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltable.c,v 1.106 2002/05/08 17:34:00 roberto Exp roberto $ ** $Id: ltable.c,v 1.107 2002/05/13 13:38:59 roberto Exp roberto $
** Lua tables (hash) ** Lua tables (hash)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -25,6 +25,7 @@
#include "lua.h" #include "lua.h"
#include "ldebug.h"
#include "ldo.h" #include "ldo.h"
#include "lmem.h" #include "lmem.h"
#include "lobject.h" #include "lobject.h"
@ -111,7 +112,7 @@ static int luaH_index (lua_State *L, Table *t, const TObject *key) {
else { else {
const TObject *v = luaH_get(t, key); const TObject *v = luaH_get(t, key);
if (v == &luaO_nilobject) if (v == &luaO_nilobject)
luaD_runerror(L, "invalid key for `next'"); luaG_runerror(L, "invalid key for `next'");
i = cast(int, (cast(const lu_byte *, v) - i = cast(int, (cast(const lu_byte *, v) -
cast(const lu_byte *, val(node(t, 0)))) / sizeof(Node)); cast(const lu_byte *, val(node(t, 0)))) / sizeof(Node));
return i + t->sizearray; /* hash elements are numbered after array ones */ return i + t->sizearray; /* hash elements are numbered after array ones */
@ -214,7 +215,7 @@ static void setnodevector (lua_State *L, Table *t, int lsize) {
int i; int i;
int size = twoto(lsize); int size = twoto(lsize);
if (lsize > MAXBITS) if (lsize > MAXBITS)
luaD_runerror(L, "table overflow"); luaG_runerror(L, "table overflow");
if (lsize == 0) { /* no elements to hash part? */ if (lsize == 0) { /* no elements to hash part? */
t->node = G(L)->dummynode; /* use common `dummynode' */ t->node = G(L)->dummynode; /* use common `dummynode' */
lua_assert(ttype(key(t->node)) == LUA_TNIL); /* assert invariants: */ lua_assert(ttype(key(t->node)) == LUA_TNIL); /* assert invariants: */
@ -449,7 +450,7 @@ void luaH_set (lua_State *L, Table *t, const TObject *key, const TObject *val) {
settableval(p, val); settableval(p, val);
} }
else { else {
if (ttype(key) == LUA_TNIL) luaD_runerror(L, "table index is nil"); if (ttype(key) == LUA_TNIL) luaG_runerror(L, "table index is nil");
newkey(L, t, key, val); newkey(L, t, key, val);
} }
t->flags = 0; t->flags = 0;

24
lua.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lua.c,v 1.84 2002/04/23 14:59:22 roberto Exp roberto $ ** $Id: lua.c,v 1.85 2002/05/01 20:40:42 roberto Exp roberto $
** Lua stand-alone interpreter ** Lua stand-alone interpreter
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -71,7 +71,7 @@ static void report (int status) {
else { else {
const char *msg = lua_tostring(L, -1); const char *msg = lua_tostring(L, -1);
if (msg == NULL) msg = "(no message)"; if (msg == NULL) msg = "(no message)";
fprintf(stderr, "error: %s\n", msg); fprintf(stderr, "%s\n", msg);
lua_pop(L, 1); lua_pop(L, 1);
} }
} }
@ -152,8 +152,8 @@ static int file_input (const char *name) {
} }
static int dostring (const char *s) { static int dostring (const char *s, const char *name) {
int status = lua_loadbuffer(L, s, strlen(s), s); int status = lua_loadbuffer(L, s, strlen(s), name);
if (status == 0) status = lcall(1); if (status == 0) status = lcall(1);
report(status); report(status);
return status; return status;
@ -198,7 +198,7 @@ static const char *get_prompt (int firstline) {
static int incomplete (int status) { static int incomplete (int status) {
if (status == LUA_ERRSYNTAX && if (status == LUA_ERRSYNTAX &&
strstr(lua_tostring(L, -1), "last token read: `<eof>'") != NULL) { strstr(lua_tostring(L, -1), "near `<eof>'") != NULL) {
lua_pop(L, 1); lua_pop(L, 1);
return 1; return 1;
} }
@ -289,7 +289,7 @@ static int handle_argv (char *argv[], int *toclose) {
print_usage(); print_usage();
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (dostring(argv[i]) != 0) { if (dostring(argv[i], "=prog. argument") != 0) {
fprintf(stderr, "%s: error running argument `%.99s'\n", fprintf(stderr, "%s: error running argument `%.99s'\n",
LUA_PROGNAME, argv[i]); LUA_PROGNAME, argv[i]);
return EXIT_FAILURE; return EXIT_FAILURE;
@ -340,6 +340,16 @@ static void openstdlibs (lua_State *l) {
} }
static int handle_luainit (void) {
const char *init = getenv("LUA_INIT");
if (init == NULL) return 0; /* status OK */
else if (init[0] == '@')
return file_input(init+1);
else
return dostring(init, "=LUA_INIT");
}
int main (int argc, char *argv[]) { int main (int argc, char *argv[]) {
int status; int status;
int toclose = 0; int toclose = 0;
@ -347,6 +357,8 @@ int main (int argc, char *argv[]) {
L = lua_open(); /* create state */ L = lua_open(); /* create state */
LUA_USERINIT(L); /* open libraries */ LUA_USERINIT(L); /* open libraries */
register_getargs(argv); /* create `getargs' function */ register_getargs(argv); /* create `getargs' function */
status = handle_luainit();
if (status != 0) return status;
status = handle_argv(argv+1, &toclose); status = handle_argv(argv+1, &toclose);
if (toclose) if (toclose)
lua_close(L); lua_close(L);

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lundump.c,v 1.45 2002/03/25 17:47:14 roberto Exp roberto $ ** $Id: lundump.c,v 1.46 2002/05/07 17:36:56 roberto Exp roberto $
** load pre-compiled Lua chunks ** load pre-compiled Lua chunks
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -27,7 +27,7 @@ static const char* ZNAME (ZIO* Z)
static void unexpectedEOZ (lua_State* L, ZIO* Z) static void unexpectedEOZ (lua_State* L, ZIO* Z)
{ {
luaO_verror(L,"unexpected end of file in `%s'",ZNAME(Z)); luaG_runerror(L,"unexpected end of file in `%s'",ZNAME(Z));
} }
static int ezgetc (lua_State* L, ZIO* Z) static int ezgetc (lua_State* L, ZIO* Z)
@ -157,7 +157,7 @@ static void LoadConstants (lua_State* L, Proto* f, ZIO* Z, int swap)
tsvalue(o)=LoadString(L,Z,swap); tsvalue(o)=LoadString(L,Z,swap);
break; break;
default: default:
luaO_verror(L,"bad constant type (%d) in `%s'",ttype(o),ZNAME(Z)); luaG_runerror(L,"bad constant type (%d) in `%s'",ttype(o),ZNAME(Z));
break; break;
} }
} }
@ -181,7 +181,7 @@ static Proto* LoadFunction (lua_State* L, TString* p, ZIO* Z, int swap)
LoadConstants(L,f,Z,swap); LoadConstants(L,f,Z,swap);
LoadCode(L,f,Z,swap); LoadCode(L,f,Z,swap);
#ifndef TRUST_BINARIES #ifndef TRUST_BINARIES
if (!luaG_checkcode(f)) luaO_verror(L,"bad code in `%s'",ZNAME(Z)); if (!luaG_checkcode(f)) luaG_runerror(L,"bad code in `%s'",ZNAME(Z));
#endif #endif
return f; return f;
} }
@ -191,14 +191,14 @@ static void LoadSignature (lua_State* L, ZIO* Z)
const char* s=LUA_SIGNATURE; const char* s=LUA_SIGNATURE;
while (*s!=0 && ezgetc(L,Z)==*s) while (*s!=0 && ezgetc(L,Z)==*s)
++s; ++s;
if (*s!=0) luaO_verror(L,"bad signature in `%s'",ZNAME(Z)); if (*s!=0) luaG_runerror(L,"bad signature in `%s'",ZNAME(Z));
} }
static void TestSize (lua_State* L, int s, const char* what, ZIO* Z) static void TestSize (lua_State* L, int s, const char* what, ZIO* Z)
{ {
int r=LoadByte(L,Z); int r=LoadByte(L,Z);
if (r!=s) if (r!=s)
luaO_verror(L,"virtual machine mismatch in `%s':\n" luaG_runerror(L,"virtual machine mismatch in `%s':\n"
" size of %.20s is %d but read %d",ZNAME(Z),what,s,r); " size of %.20s is %d but read %d",ZNAME(Z),what,s,r);
} }
@ -212,11 +212,11 @@ static int LoadHeader (lua_State* L, ZIO* Z)
LoadSignature(L,Z); LoadSignature(L,Z);
version=LoadByte(L,Z); version=LoadByte(L,Z);
if (version>VERSION) if (version>VERSION)
luaO_verror(L,"`%s' too new:\n" luaG_runerror(L,"`%s' too new:\n"
" read version %d.%d; expected at most %d.%d", " read version %d.%d; expected at most %d.%d",
ZNAME(Z),V(version),V(VERSION)); ZNAME(Z),V(version),V(VERSION));
if (version<VERSION0) /* check last major change */ if (version<VERSION0) /* check last major change */
luaO_verror(L,"`%s' too old:\n" luaG_runerror(L,"`%s' too old:\n"
" read version %d.%d; expected at least %d.%d", " read version %d.%d; expected at least %d.%d",
ZNAME(Z),V(version),V(VERSION)); ZNAME(Z),V(version),V(VERSION));
swap=(luaU_endianness()!=LoadByte(L,Z)); /* need to swap bytes? */ swap=(luaU_endianness()!=LoadByte(L,Z)); /* need to swap bytes? */
@ -230,7 +230,7 @@ static int LoadHeader (lua_State* L, ZIO* Z)
TESTSIZE(sizeof(lua_Number), "number"); TESTSIZE(sizeof(lua_Number), "number");
x=LoadNumber(L,Z,swap); x=LoadNumber(L,Z,swap);
if ((long)x!=(long)tx) /* disregard errors in last bits of fraction */ if ((long)x!=(long)tx) /* disregard errors in last bits of fraction */
luaO_verror(L,"unknown number format in `%s':\n" luaG_runerror(L,"unknown number format in `%s':\n"
" read " LUA_NUMBER_FMT "; expected " LUA_NUMBER_FMT, " read " LUA_NUMBER_FMT "; expected " LUA_NUMBER_FMT,
ZNAME(Z),x,tx); ZNAME(Z),x,tx);
return swap; return swap;
@ -248,7 +248,7 @@ Proto* luaU_undump (lua_State* L, ZIO* Z)
{ {
Proto* f=LoadChunk(L,Z); Proto* f=LoadChunk(L,Z);
if (zgetc(Z)!=EOZ) if (zgetc(Z)!=EOZ)
luaO_verror(L,"`%s' apparently contains more than one chunk",ZNAME(Z)); luaG_runerror(L,"`%s' apparently contains more than one chunk",ZNAME(Z));
return f; return f;
} }

16
lvm.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lvm.c,v 1.230 2002/05/09 14:14:34 roberto Exp roberto $ ** $Id: lvm.c,v 1.231 2002/05/13 13:09:00 roberto Exp roberto $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -134,7 +134,7 @@ void luaV_gettable (lua_State *L, const TObject *t, TObject *key, StkId res) {
if (ttype(tm) == LUA_TFUNCTION) if (ttype(tm) == LUA_TFUNCTION)
callTMres(L, tm, t, key, res); callTMres(L, tm, t, key, res);
else { else {
if (++loop == MAXTAGLOOP) luaD_runerror(L, "loop in gettable"); if (++loop == MAXTAGLOOP) luaG_runerror(L, "loop in gettable");
t = tm; t = tm;
goto init; /* return luaV_gettable(L, tm, key, res); */ goto init; /* return luaV_gettable(L, tm, key, res); */
} }
@ -164,7 +164,7 @@ void luaV_settable (lua_State *L, const TObject *t, TObject *key, StkId val) {
if (ttype(tm) == LUA_TFUNCTION) if (ttype(tm) == LUA_TFUNCTION)
callTM(L, tm, t, key, val); callTM(L, tm, t, key, val);
else { else {
if (++loop == MAXTAGLOOP) luaD_runerror(L, "loop in settable"); if (++loop == MAXTAGLOOP) luaG_runerror(L, "loop in settable");
t = tm; t = tm;
goto init; /* luaV_settable(L, tm, key, val); */ goto init; /* luaV_settable(L, tm, key, val); */
} }
@ -251,7 +251,7 @@ void luaV_strconc (lua_State *L, int total, int last) {
tl += tsvalue(top-n-1)->tsv.len; tl += tsvalue(top-n-1)->tsv.len;
n++; n++;
} }
if (tl > MAX_SIZET) luaD_runerror(L, "string size overflow"); if (tl > MAX_SIZET) luaG_runerror(L, "string size overflow");
buffer = luaO_openspace(L, tl, char); buffer = luaO_openspace(L, tl, char);
tl = 0; tl = 0;
for (i=n; i>0; i--) { /* concat all strings */ for (i=n; i>0; i--) { /* concat all strings */
@ -276,7 +276,7 @@ static void powOp (lua_State *L, StkId ra, StkId rb, StkId rc) {
setsvalue(&o, luaS_newliteral(L, "pow")); setsvalue(&o, luaS_newliteral(L, "pow"));
luaV_gettable(L, gt(L), &o, &f); luaV_gettable(L, gt(L), &o, &f);
if (ttype(&f) != LUA_TFUNCTION) if (ttype(&f) != LUA_TFUNCTION)
luaD_runerror(L, "`pow' (for `^' operator) is not a function"); luaG_runerror(L, "`pow' (for `^' operator) is not a function");
callTMres(L, &f, b, c, ra); callTMres(L, &f, b, c, ra);
} }
else else
@ -527,11 +527,11 @@ StkId luaV_execute (lua_State *L) {
const TObject *plimit = ra+1; const TObject *plimit = ra+1;
const TObject *pstep = ra+2; const TObject *pstep = ra+2;
if (ttype(ra) != LUA_TNUMBER) if (ttype(ra) != LUA_TNUMBER)
luaD_runerror(L, "`for' initial value must be a number"); luaG_runerror(L, "`for' initial value must be a number");
if (!tonumber(plimit, ra+1)) if (!tonumber(plimit, ra+1))
luaD_runerror(L, "`for' limit must be a number"); luaG_runerror(L, "`for' limit must be a number");
if (!tonumber(pstep, ra+2)) if (!tonumber(pstep, ra+2))
luaD_runerror(L, "`for' step must be a number"); luaG_runerror(L, "`for' step must be a number");
step = nvalue(pstep); step = nvalue(pstep);
index = nvalue(ra) + step; /* increment index */ index = nvalue(ra) + step; /* increment index */
limit = nvalue(plimit); limit = nvalue(plimit);