Browse Source

"dostring" accepts chunk name.

Roberto Ierusalimschy 27 years ago
parent
commit
df0df08bc5
4 changed files with 41 additions and 22 deletions
  1. 7 4
      lbuiltin.c
  2. 24 13
      ldo.c
  3. 2 2
      llex.c
  4. 8 3
      manual.tex

+ 7 - 4
lbuiltin.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lbuiltin.c,v 1.29 1998/06/05 22:17:44 roberto Exp roberto $
+** $Id: lbuiltin.c,v 1.30 1998/06/19 16:14:09 roberto Exp roberto $
 ** Built-in functions
 ** See Copyright Notice in lua.h
 */
@@ -22,6 +22,7 @@
 #include "ltable.h"
 #include "ltm.h"
 #include "lua.h"
+#include "lundump.h"
 
 
 
@@ -114,9 +115,11 @@ static void foreach (void)
 
 static void internaldostring (void)
 {
-  if (lua_getparam(2) != LUA_NOOBJECT)
-    lua_error("invalid 2nd argument (probably obsolete code)");
-  if (lua_dostring(luaL_check_string(1)) == 0)
+  long l;
+  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 (luaA_passresults() == 0)
       lua_pushuserdata(NULL);  /* at least one result to signal no errors */
 }

+ 24 - 13
ldo.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ldo.c,v 1.25 1998/05/31 22:22:00 roberto Exp roberto $
+** $Id: ldo.c,v 1.26 1998/06/15 21:34:14 roberto Exp roberto $
 ** Stack and Call structure of Lua
 ** See Copyright Notice in lua.h
 */
@@ -392,24 +392,35 @@ int lua_dofile (char *filename)
 #define SSIZE_PREF "20"
 
 
-int lua_dostring (char *str) {
-  char name[SIZE_PREF+25];
-  char *temp;
-  if (str == NULL || *str == ID_CHUNK) return 1;
-  sprintf(name, "(dostring) >> \"%." SSIZE_PREF "s\"", str);
-  temp = strchr(name, '\n');
-  if (temp) {  /* end string after first line */
-   *temp = '"';
-   *(temp+1) = 0;
+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;
+    }
   }
-  return lua_dobuffer(str, strlen(str), name);
+}
+
+
+int lua_dostring (char *str) {
+  return lua_dobuffer(str, strlen(str), NULL);
 }
 
 
 int lua_dobuffer (char *buff, int size, char *name) {
-  int status;
+  char newname[SIZE_PREF+25];
   ZIO z;
-  luaZ_mopen(&z, buff, size, (name==NULL) ? "(buffer)" : name);
+  int status;
+  if (name==NULL) {
+    build_name(buff, newname);
+    name = newname;
+  }
+  luaZ_mopen(&z, buff, size, name);
   status = do_main(&z, buff[0]==ID_CHUNK);
   return status;
 }

+ 2 - 2
llex.c

@@ -1,5 +1,5 @@
 /*
-** $Id: llex.c,v 1.20 1998/06/06 20:44:05 roberto Exp roberto $
+** $Id: llex.c,v 1.21 1998/06/18 16:57:03 roberto Exp roberto $
 ** Lexical Analizer
 ** See Copyright Notice in lua.h
 */
@@ -48,7 +48,7 @@ void luaX_init (void)
 void luaX_syntaxerror (LexState *ls, char *s, char *token) {
   if (token[0] == 0)
     token = "<eof>";
-  luaL_verror("%.100s;\n  last token read: `%.50s' at line %d in file %.50s",
+  luaL_verror("%.100s;\n  last token read: `%.50s' at line %d in chunk `%.50s'",
               s, token, ls->linenumber, zname(ls->lex_z));
 }
 

+ 8 - 3
manual.tex

@@ -1,4 +1,4 @@
-% $Id: manual.tex,v 1.14 1998/06/15 21:34:14 roberto Exp roberto $
+% $Id: manual.tex,v 1.15 1998/06/18 17:36:27 roberto Exp roberto $
 
 \documentclass[11pt]{article}
 \usepackage{fullpage,bnf}
@@ -39,7 +39,7 @@ Waldemar Celes
 \tecgraf\ --- Computer Science Department --- PUC-Rio
 }
 
-%\date{\small \verb$Date: 1998/06/15 21:34:14 $}
+%\date{\small \verb$Date: 1998/06/18 17:36:27 $}
 
 \maketitle
 
@@ -1602,6 +1602,8 @@ Function \verb|lua_dostring| executes only source code.
 The third parameter to \verb|lua_dobuffer| (\verb|name|)
 is the ``name of the chunk'',
 used in error messages and debug information.
+If \verb|name| is \verb|NULL|,
+Lua gives a default name to the chunk.
 In files this name is the file name,
 and \verb|lua_dostring| uses a small prefix
 of the string as the chunk name.
@@ -1949,12 +1951,15 @@ or a non \nil\ value if the chunk returns no values.
 It issues an error when called with a non string argument.
 \verb|dofile| is equivalent to the API function \verb|lua_dofile|.
 
-\subsubsection*{\ff \T{dostring (string)}}\Deffunc{dostring}
+\subsubsection*{\ff \T{dostring (string [, chunkname])}}\Deffunc{dostring}
 This function executes a given string as a Lua chunk.
 If there is any error executing the string,
 \verb|dostring| returns \nil.
 Otherwise, it returns the values returned by the chunk,
 or a non \nil\ value if the chunk returns no values.
+An optional second parameter (\verb|chunkname|)
+is the ``name of the chunk'',
+used in error messages and debug information.
 \verb|dostring| is equivalent to the API function \verb|lua_dostring|.
 
 \subsubsection*{\ff \T{newtag ()}}\Deffunc{newtag}\label{pdf-newtag}