Browse Source

storing chunk "sources" instead of "filenames".

Roberto Ierusalimschy 26 years ago
parent
commit
5a8bb00df4
15 changed files with 91 additions and 86 deletions
  1. 4 5
      lapi.c
  2. 17 2
      lauxlib.c
  3. 2 2
      lauxlib.h
  4. 2 2
      lbuiltin.c
  5. 7 7
      ldblib.c
  6. 13 34
      ldo.c
  7. 2 2
      lfunc.c
  8. 2 2
      lgc.c
  9. 13 9
      liolib.c
  10. 9 5
      llex.c
  11. 2 2
      lobject.h
  12. 5 5
      lparser.c
  13. 2 2
      luadebug.h
  14. 2 2
      lundump.c
  15. 9 5
      manual.tex

+ 4 - 5
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;
     }
   }

+ 17 - 2
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)<len) ? b-source : len;
+    sprintf(out, "string `%.*s'", lim, source);
+    strcpy(out+lim+(13-5), "...'");  /* 5 = strlen("...'\0") */
+  }
+}
+

+ 2 - 2
lauxlib.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lauxlib.h,v 1.9 1998/06/19 16:14:09 roberto Exp roberto $
+** $Id: lauxlib.h,v 1.10 1998/12/28 13:44:54 roberto Exp roberto $
 ** Auxiliary functions for building Lua libraries
 ** See Copyright Notice in lua.h
 */
@@ -46,6 +46,6 @@ int luaL_newbuffer (int size);
 void luaL_oldbuffer (int old);
 char *luaL_buffer (void);
 int luaL_findstring (char *name, char *list[]);
-
+void luaL_chunkid (char *out, char *source, int len);
 
 #endif

+ 2 - 2
lbuiltin.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lbuiltin.c,v 1.54 1999/02/23 14:57:28 roberto Exp roberto $
+** $Id: lbuiltin.c,v 1.55 1999/03/01 20:22:16 roberto Exp roberto $
 ** Built-in functions
 ** See Copyright Notice in lua.h
 */
@@ -250,7 +250,7 @@ static void luaB_dostring (void) {
   char *s = luaL_check_lstr(1, &l);
   if (*s == ID_CHUNK)
     lua_error("`dostring' cannot run pre-compiled code");
-  if (lua_dobuffer(s, l, luaL_opt_string(2, NULL)) == 0)
+  if (lua_dobuffer(s, l, luaL_opt_string(2, s)) == 0)
     if (luaA_passresults() == 0)
       lua_pushuserdata(NULL);  /* at least one result to signal no errors */
 }

+ 7 - 7
ldblib.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ldblib.c,v 1.3 1999/01/15 11:36:28 roberto Exp roberto $
+** $Id: ldblib.c,v 1.4 1999/02/04 17:47:59 roberto Exp roberto $
 ** Interface from Lua to its debug API
 ** See Copyright Notice in lua.h
 */
@@ -33,24 +33,24 @@ static void settabsi (lua_Object t, char *i, int v) {
 
 static lua_Object getfuncinfo (lua_Object func) {
   lua_Object result = lua_createtable();
-  char *name;
+  char *str;
   int line;
-  lua_funcinfo(func, &name, &line);
+  lua_funcinfo(func, &str, &line);
   if (line == -1)  /* C function? */
     settabss(result, "kind", "C");
   else if (line == 0) {  /* "main"? */
       settabss(result, "kind", "chunk");
-      settabss(result, "name", name);
+      settabss(result, "source", str);
     }
   else {  /* Lua function */
     settabss(result, "kind", "Lua");
     settabsi(result, "def_line", line);
-    settabss(result, "def_chunk", name);
+    settabss(result, "source", str);
   }
   if (line != 0) {  /* is it not a "main"? */
-    char *kind = lua_getobjname(func, &name);
+    char *kind = lua_getobjname(func, &str);
     if (*kind) {
-      settabss(result, "name", name);
+      settabss(result, "name", str);
       settabss(result, "where", kind);
     }
   }

+ 13 - 34
ldo.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ldo.c,v 1.35 1999/02/22 19:23:36 roberto Exp roberto $
+** $Id: ldo.c,v 1.36 1999/02/26 15:48:55 roberto Exp roberto $
 ** Stack and Call structure of Lua
 ** See Copyright Notice in lua.h
 */
@@ -108,7 +108,7 @@ void luaD_callHook (StkId base, TProtoFunc *tf, int isreturn)
   else {
     TObject *f = L->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);
 }
 

+ 2 - 2
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;

+ 2 - 2
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; i<f->nconsts; i++)
       markobject(&f->consts[i]);
   }

+ 13 - 9
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");

+ 9 - 5
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 = "<eof>";
-  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);
 }
 
 

+ 2 - 2
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;
 

+ 5 - 5
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;

+ 2 - 2
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);
 

+ 2 - 2
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);

+ 9 - 5
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,