From 5a8bb00df443dfdb5708689f32c64e90f2557bf8 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 4 Mar 1999 18:23:39 -0300 Subject: [PATCH] storing chunk "sources" instead of "filenames". --- lapi.c | 9 ++++----- lauxlib.c | 19 +++++++++++++++++-- lauxlib.h | 4 ++-- lbuiltin.c | 4 ++-- ldblib.c | 14 +++++++------- ldo.c | 47 +++++++++++++---------------------------------- lfunc.c | 4 ++-- lgc.c | 4 ++-- liolib.c | 22 +++++++++++++--------- llex.c | 14 +++++++++----- lobject.h | 4 ++-- lparser.c | 10 +++++----- luadebug.h | 4 ++-- lundump.c | 4 ++-- manual.tex | 14 +++++++++----- 15 files changed, 91 insertions(+), 86 deletions(-) diff --git a/lapi.c b/lapi.c index aea8462d..98b6e213 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 1.39 1999/02/25 19:13:56 roberto Exp roberto $ +** $Id: lapi.c,v 1.40 1999/03/01 17:49:04 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -594,18 +594,17 @@ int lua_setlocal (lua_Function func, int local_number) } -void lua_funcinfo (lua_Object func, char **filename, int *linedefined) -{ +void lua_funcinfo (lua_Object func, char **source, int *linedefined) { if (!lua_isfunction(func)) lua_error("API - `funcinfo' called with a non-function value"); else { TObject *f = luaA_protovalue(Address(func)); if (normalized_type(f) == LUA_T_PROTO) { - *filename = tfvalue(f)->fileName->str; + *source = tfvalue(f)->source->str; *linedefined = tfvalue(f)->lineDefined; } else { - *filename = "(C)"; + *source = "(C)"; *linedefined = -1; } } diff --git a/lauxlib.c b/lauxlib.c index 37931dc6..090ded3c 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.13 1998/09/07 18:59:59 roberto Exp roberto $ +** $Id: lauxlib.c,v 1.14 1999/02/25 19:13:56 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -34,7 +34,7 @@ void luaL_argerror (int numarg, char *extramsg) { lua_getobjname(f, &funcname); numarg -= lua_nups(f); if (funcname == NULL) - funcname = "(unknown)"; + funcname = "?"; if (extramsg == NULL) luaL_verror("bad argument #%d to function `%.50s'", numarg, funcname); else @@ -111,3 +111,18 @@ void luaL_verror (char *fmt, ...) lua_error(buff); } + +void luaL_chunkid (char *out, char *source, int len) { + len -= 13; /* 13 = strlen("string ''...\0") */ + if (*source == '@') + sprintf(out, "file `%.*s'", len, source+1); + else if (*source == '(') + strcpy(out, "(C code)"); + else { + char *b = strchr(source , '\n'); /* stop string at first new line */ + int lim = (b && (b-source)stack.stack+base-1; if (tf) - (*L->callhook)(Ref(f), tf->fileName->str, tf->lineDefined); + (*L->callhook)(Ref(f), tf->source->str, tf->lineDefined); else (*L->callhook)(Ref(f), "(C)", -1); } @@ -355,23 +355,27 @@ void luaD_gcIM (TObject *o) } -int lua_dofile (char *filename) -{ +#define MAXFILENAME 200 /* maximum part of a file name kept */ + +int lua_dofile (char *filename) { ZIO z; int status; int c; int bin; + char name[MAXFILENAME+2]; /* +2 for '@' and '\0' */ FILE *f = (filename == NULL) ? stdin : fopen(filename, "r"); if (f == NULL) return 2; if (filename == NULL) - filename = "(stdin)"; + strcpy(name, "@stdin"); + else + sprintf(name, "@%.*s", MAXFILENAME, filename); c = fgetc(f); ungetc(c, f); bin = (c == ID_CHUNK); if (bin) f = freopen(filename, "rb", f); /* set binary mode */ - luaZ_Fopen(&z, f, filename); + luaZ_Fopen(&z, f, name); status = do_main(&z, bin); if (f != stdin) fclose(f); @@ -379,40 +383,15 @@ int lua_dofile (char *filename) } -#define SIZE_PREF 20 /* size of string prefix to appear in error messages */ -#define SSIZE_PREF "20" - - -static void build_name (char *str, char *name) { - if (str == NULL || *str == ID_CHUNK) - strcpy(name, "(buffer)"); - else { - char *temp; - sprintf(name, "(dostring) >> \"%." SSIZE_PREF "s\"", str); - temp = strchr(name, '\n'); - if (temp) { /* end string after first line */ - *temp = '"'; - *(temp+1) = 0; - } - } -} - - int lua_dostring (char *str) { - return lua_dobuffer(str, strlen(str), NULL); + return lua_dobuffer(str, strlen(str), str); } int lua_dobuffer (char *buff, int size, char *name) { - char newname[SIZE_PREF+25]; ZIO z; - int status; - if (name==NULL) { - build_name(buff, newname); - name = newname; - } + if (!name) name = "?"; luaZ_mopen(&z, buff, size, name); - status = do_main(&z, buff[0]==ID_CHUNK); - return status; + return do_main(&z, buff[0]==ID_CHUNK); } diff --git a/lfunc.c b/lfunc.c index efabfa3f..e1bdd2bd 100644 --- a/lfunc.c +++ b/lfunc.c @@ -1,5 +1,5 @@ /* -** $Id: lfunc.c,v 1.8 1997/12/15 16:17:20 roberto Exp roberto $ +** $Id: lfunc.c,v 1.9 1998/06/19 16:14:09 roberto Exp roberto $ ** Auxiliary functions to manipulate prototypes and closures ** See Copyright Notice in lua.h */ @@ -31,7 +31,7 @@ TProtoFunc *luaF_newproto (void) TProtoFunc *f = luaM_new(TProtoFunc); f->code = NULL; f->lineDefined = 0; - f->fileName = NULL; + f->source = NULL; f->consts = NULL; f->nconsts = 0; f->locvars = NULL; diff --git a/lgc.c b/lgc.c index b321e76c..5686a296 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 1.21 1999/02/25 15:16:26 roberto Exp roberto $ +** $Id: lgc.c,v 1.22 1999/02/26 15:48:55 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -162,7 +162,7 @@ static void protomark (TProtoFunc *f) { if (!f->head.marked) { int i; f->head.marked = 1; - strmark(f->fileName); + strmark(f->source); for (i=0; inconsts; i++) markobject(&f->consts[i]); } diff --git a/liolib.c b/liolib.c index ed40b1ac..7b3eb9ea 100644 --- a/liolib.c +++ b/liolib.c @@ -1,5 +1,5 @@ /* -** $Id: liolib.c,v 1.30 1999/02/05 15:22:43 roberto Exp roberto $ +** $Id: liolib.c,v 1.31 1999/02/22 14:17:24 roberto Exp roberto $ ** Standard I/O (and system) library ** See Copyright Notice in lua.h */ @@ -455,6 +455,10 @@ static void io_debug (void) { #define MESSAGESIZE 150 #define MAXMESSAGE (MESSAGESIZE*10) + +#define MAXSRC 40 + + static void errorfb (void) { char buff[MAXMESSAGE]; int level = 1; /* skip level 0 (it's this function) */ @@ -464,8 +468,10 @@ static void errorfb (void) { char *name; int currentline; char *chunkname; + char buffchunk[MAXSRC]; int linedefined; lua_funcinfo(func, &chunkname, &linedefined); + luaL_chunkid(buffchunk, chunkname, MAXSRC); if (level == 2) strcat(buff, "Active Stack:\n"); strcat(buff, "\t"); if (strlen(buff) > MAXMESSAGE-MESSAGESIZE) { @@ -474,26 +480,24 @@ static void errorfb (void) { } switch (*lua_getobjname(func, &name)) { case 'g': - sprintf(buff+strlen(buff), "function %.50s", name); + sprintf(buff+strlen(buff), "function `%.50s'", name); break; case 't': sprintf(buff+strlen(buff), "`%.50s' tag method", name); break; default: { if (linedefined == 0) - sprintf(buff+strlen(buff), "main of %.50s", chunkname); - else if (linedefined < 0) - sprintf(buff+strlen(buff), "%.50s", chunkname); - else - sprintf(buff+strlen(buff), "function (%.50s:%d)", - chunkname, linedefined); + sprintf(buff+strlen(buff), "main of %.50s", buffchunk); + else if (linedefined > 0) + sprintf(buff+strlen(buff), "function <%d:%.50s>", + linedefined, buffchunk); chunkname = NULL; } } if ((currentline = lua_currentline(func)) > 0) sprintf(buff+strlen(buff), " at line %d", currentline); if (chunkname) - sprintf(buff+strlen(buff), " [in chunk %.50s]", chunkname); + sprintf(buff+strlen(buff), " [in %.50s]", buffchunk); strcat(buff, "\n"); } func = lua_rawgetglobal("_ALERT"); diff --git a/llex.c b/llex.c index 88a57d97..0addaf47 100644 --- a/llex.c +++ b/llex.c @@ -1,5 +1,5 @@ /* -** $Id: llex.c,v 1.29 1999/02/25 15:17:01 roberto Exp roberto $ +** $Id: llex.c,v 1.30 1999/02/25 19:13:56 roberto Exp roberto $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ @@ -32,8 +32,7 @@ char *reserved [] = {"and", "do", "else", "elseif", "end", "function", "until", "while"}; -void luaX_init (void) -{ +void luaX_init (void) { int i; for (i=0; i<(sizeof(reserved)/sizeof(reserved[0])); i++) { TaggedString *ts = luaS_new(reserved[i]); @@ -42,11 +41,16 @@ void luaX_init (void) } +#define MAXSRC 40 + void luaX_syntaxerror (LexState *ls, char *s, char *token) { + char buff[MAXSRC]; + luaL_chunkid(buff, zname(ls->lex_z), MAXSRC); if (token[0] == '\0') token = ""; - luaL_verror("%.100s;\n last token read: `%.50s' at line %d in chunk `%.50s'", - s, token, ls->linenumber, zname(ls->lex_z)); + luaL_verror("%.100s;\n last token read: `%.50s' " + "at line %d from %.50s", + s, token, ls->linenumber, buff); } diff --git a/lobject.h b/lobject.h index 4c5d0c12..b1221f0e 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 1.25 1999/01/04 13:37:07 roberto Exp roberto $ +** $Id: lobject.h,v 1.26 1999/02/03 13:53:48 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -128,7 +128,7 @@ typedef struct TProtoFunc { int nconsts; Byte *code; /* ends with opcode ENDCODE */ int lineDefined; - TaggedString *fileName; + TaggedString *source; struct LocVar *locvars; /* ends with line = -1 */ } TProtoFunc; diff --git a/lparser.c b/lparser.c index e967ad88..a30c3777 100644 --- a/lparser.c +++ b/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 1.24 1999/02/25 19:13:56 roberto Exp roberto $ +** $Id: lparser.c,v 1.25 1999/02/26 15:48:55 roberto Exp roberto $ ** LL(1) Parser and code generator for Lua ** See Copyright Notice in lua.h */ @@ -534,7 +534,7 @@ static void func_onstack (LexState *ls, FuncState *func) { } -static void init_state (LexState *ls, FuncState *fs, TaggedString *filename) { +static void init_state (LexState *ls, FuncState *fs, TaggedString *source) { TProtoFunc *f = luaF_newproto(); fs->prev = ls->fs; /* linked list of funcstates */ ls->fs = fs; @@ -544,7 +544,7 @@ static void init_state (LexState *ls, FuncState *fs, TaggedString *filename) { fs->nupvalues = 0; fs->lastsetline = 0; fs->f = f; - f->fileName = filename; + f->source = source; fs->pc = 0; f->code = NULL; fs->nvars = (L->debug) ? 0 : -1; /* flag no debug information? */ @@ -828,7 +828,7 @@ static int funcname (LexState *ls, vardesc *v) { static void body (LexState *ls, int needself, int line) { /* body -> '(' parlist ')' chunk END */ FuncState newfs; - init_state(ls, &newfs, ls->fs->f->fileName); + init_state(ls, &newfs, ls->fs->f->source); newfs.f->lineDefined = line; check(ls, '('); if (needself) @@ -1140,7 +1140,7 @@ static int funcparams (LexState *ls, int slf) { luaX_error(ls, "function arguments expected"); break; } - code_byte(fs, CALLFUNC); + code_byte(fs, CALL); code_byte(fs, 0); /* save space for nresult */ code_byte(fs, (Byte)(nparams+slf)); return fs->pc-1; diff --git a/luadebug.h b/luadebug.h index d2e3c862..f7727119 100644 --- a/luadebug.h +++ b/luadebug.h @@ -1,5 +1,5 @@ /* -** $Id: luadebug.h,v 1.4 1999/01/15 13:11:22 roberto Exp roberto $ +** $Id: luadebug.h,v 1.5 1999/02/04 17:47:59 roberto Exp roberto $ ** Debugging API ** See Copyright Notice in lua.h */ @@ -17,7 +17,7 @@ typedef void (*lua_LHFunction) (int line); typedef void (*lua_CHFunction) (lua_Function func, char *file, int line); lua_Function lua_stackedfunction (int level); -void lua_funcinfo (lua_Object func, char **filename, int *linedefined); +void lua_funcinfo (lua_Object func, char **source, int *linedefined); int lua_currentline (lua_Function func); char *lua_getobjname (lua_Object o, char **name); diff --git a/lundump.c b/lundump.c index 55860284..941f8ae8 100644 --- a/lundump.c +++ b/lundump.c @@ -1,5 +1,5 @@ /* -** $Id: lundump.c,v 1.5 1998/12/15 14:59:43 roberto Exp $ +** $Id: lundump.c,v 1.6 1998/12/27 20:23:22 roberto Exp roberto $ ** load bytecodes from files ** See Copyright Notice in lua.h */ @@ -161,7 +161,7 @@ static TProtoFunc* LoadFunction (ZIO* Z) { TProtoFunc* tf=luaF_newproto(); tf->lineDefined=LoadWord(Z); - tf->fileName=LoadTString(Z); + tf->source=LoadTString(Z); tf->code=LoadCode(Z); LoadLocals(tf,Z); LoadConstants(tf,Z); diff --git a/manual.tex b/manual.tex index cbde48e0..7979064d 100644 --- a/manual.tex +++ b/manual.tex @@ -1,4 +1,4 @@ -% $Id: manual.tex,v 1.23 1999/02/12 19:23:02 roberto Exp roberto $ +% $Id: manual.tex,v 1.24 1999/02/25 19:13:56 roberto Exp roberto $ \documentclass[11pt]{article} \usepackage{fullpage,bnf} @@ -41,7 +41,7 @@ Waldemar Celes \tecgraf\ --- Computer Science Department --- PUC-Rio } -%\date{\small \verb$Date: 1999/02/12 19:23:02 $} +%\date{\small \verb$Date: 1999/02/25 19:13:56 $} \maketitle @@ -2926,12 +2926,16 @@ it accepts only a handle returned by Three other functions produce extra information about a function: \begin{verbatim} -void lua_funcinfo (lua_Object func, char **filename, int *linedefined); +void lua_funcinfo (lua_Object func, char **source, int *linedefined); int lua_currentline (lua_Function func); char *lua_getobjname (lua_Object o, char **name); \end{verbatim} -\verb|lua_funcinfo| gives the file name and the line where the -given function has been defined. +\verb|lua_funcinfo| gives the source and the line where the +given function has been defined: +If the function was defined in a string, +\verb|source| is that string; +If the function was defined in a file, +\verb|source| starts with a \verb|@| followed by the file name. If the ``function'' is in fact the main code of a chunk, then \verb|linedefined| is 0. If the function is a C function,