浏览代码

new API functions to load (parse?) a chunk without running it.

Roberto Ierusalimschy 24 年之前
父节点
当前提交
6473f965ca
共有 4 个文件被更改,包括 55 次插入31 次删除
  1. 26 2
      lapi.c
  2. 20 1
      lbaselib.c
  3. 4 26
      ldo.c
  4. 5 2
      lua.h

+ 26 - 2
lapi.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lapi.c,v 1.137 2001/03/26 14:31:49 roberto Exp roberto $
+** $Id: lapi.c,v 1.138 2001/04/11 14:42:41 roberto Exp roberto $
 ** Lua API
 ** Lua API
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -533,7 +533,6 @@ LUA_API int lua_ref (lua_State *L,  int lock) {
 
 
 /*
 /*
 ** `do' functions (run Lua code)
 ** `do' functions (run Lua code)
-** (most of them are in ldo.c)
 */
 */
 
 
 LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) {
 LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) {
@@ -544,6 +543,31 @@ LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) {
 }
 }
 
 
 
 
+LUA_API int lua_dofile (lua_State *L, const l_char *filename) {
+  int status;
+  status = lua_loadfile(L, filename);
+  if (status == 0)  /* parse OK? */
+    status = lua_call(L, 0, LUA_MULTRET);  /* call main */
+  return status;
+}
+
+
+LUA_API int lua_dobuffer (lua_State *L, const l_char *buff, size_t size,
+                          const l_char *name) {
+  int status;
+  status = lua_loadbuffer(L, buff, size, name);
+  if (status == 0)  /* parse OK? */
+    status = lua_call(L, 0, LUA_MULTRET);  /* call main */
+  return status;
+}
+
+
+LUA_API int lua_dostring (lua_State *L, const l_char *str) {
+  return lua_dobuffer(L, str, strlen(str), str);
+}
+
+
+
 /*
 /*
 ** Garbage-collection functions
 ** Garbage-collection functions
 */
 */

+ 20 - 1
lbaselib.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lbaselib.c,v 1.32 2001/04/06 18:25:00 roberto Exp roberto $
+** $Id: lbaselib.c,v 1.33 2001/04/11 14:42:41 roberto Exp roberto $
 ** Basic library
 ** Basic library
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -311,6 +311,7 @@ static int passresults (lua_State *L, int status, int oldtop) {
   }
   }
 }
 }
 
 
+
 static int luaB_dostring (lua_State *L) {
 static int luaB_dostring (lua_State *L) {
   int oldtop = lua_gettop(L);
   int oldtop = lua_gettop(L);
   size_t l;
   size_t l;
@@ -320,6 +321,14 @@ static int luaB_dostring (lua_State *L) {
 }
 }
 
 
 
 
+static int luaB_loadstring (lua_State *L) {
+  int oldtop = lua_gettop(L);
+  size_t l;
+  const l_char *s = luaL_check_lstr(L, 1, &l);
+  const l_char *chunkname = luaL_opt_string(L, 2, s);
+  return passresults(L, lua_loadbuffer(L, s, l, chunkname), oldtop);
+}
+
 static int luaB_dofile (lua_State *L) {
 static int luaB_dofile (lua_State *L) {
   int oldtop = lua_gettop(L);
   int oldtop = lua_gettop(L);
   const l_char *fname = luaL_opt_string(L, 1, NULL);
   const l_char *fname = luaL_opt_string(L, 1, NULL);
@@ -327,6 +336,14 @@ static int luaB_dofile (lua_State *L) {
 }
 }
 
 
 
 
+static int luaB_loadfile (lua_State *L) {
+  int oldtop = lua_gettop(L);
+  const l_char *fname = luaL_opt_string(L, 1, NULL);
+  return passresults(L, lua_loadfile(L, fname), oldtop);
+}
+
+
+
 #define LUA_PATH	l_s("LUA_PATH")
 #define LUA_PATH	l_s("LUA_PATH")
 
 
 #define LUA_PATH_SEP	l_s(";")
 #define LUA_PATH_SEP	l_s(";")
@@ -753,6 +770,8 @@ static const luaL_reg base_funcs[] = {
   {l_s("getglobal"), luaB_getglobal},
   {l_s("getglobal"), luaB_getglobal},
   {l_s("gettagmethod"), luaB_gettagmethod},
   {l_s("gettagmethod"), luaB_gettagmethod},
   {l_s("globals"), luaB_globals},
   {l_s("globals"), luaB_globals},
+  {l_s("loadfile"), luaB_loadfile},
+  {l_s("loadstring"), luaB_loadstring},
   {l_s("newtype"), luaB_newtype},
   {l_s("newtype"), luaB_newtype},
   {l_s("newtag"), luaB_newtype},  /* for compatibility 4.0 */
   {l_s("newtag"), luaB_newtype},  /* for compatibility 4.0 */
   {l_s("next"), luaB_next},
   {l_s("next"), luaB_next},

+ 4 - 26
ldo.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: ldo.c,v 1.132 2001/03/26 14:31:49 roberto Exp roberto $
+** $Id: ldo.c,v 1.133 2001/04/06 19:26:06 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
 */
 */
@@ -255,7 +255,7 @@ static int protectedparser (lua_State *L, ZIO *z, int bin) {
 }
 }
 
 
 
 
-static int parse_file (lua_State *L, const l_char *filename) {
+LUA_API int lua_loadfile (lua_State *L, const l_char *filename) {
   ZIO z;
   ZIO z;
   int status;
   int status;
   int bin;  /* flag for file mode */
   int bin;  /* flag for file mode */
@@ -282,17 +282,8 @@ static int parse_file (lua_State *L, const l_char *filename) {
 }
 }
 
 
 
 
-LUA_API int lua_dofile (lua_State *L, const l_char *filename) {
-  int status;
-  status = parse_file(L, filename);
-  if (status == 0)  /* parse OK? */
-    status = lua_call(L, 0, LUA_MULTRET);  /* call main */
-  return status;
-}
-
-
-static int parse_buffer (lua_State *L, const l_char *buff, size_t size,
-                         const l_char *name) {
+LUA_API int lua_loadbuffer (lua_State *L, const l_char *buff, size_t size,
+                          const l_char *name) {
   ZIO z;
   ZIO z;
   int status;
   int status;
   if (!name) name = l_s("?");
   if (!name) name = l_s("?");
@@ -302,19 +293,6 @@ static int parse_buffer (lua_State *L, const l_char *buff, size_t size,
 }
 }
 
 
 
 
-LUA_API int lua_dobuffer (lua_State *L, const l_char *buff, size_t size, const l_char *name) {
-  int status;
-  status = parse_buffer(L, buff, size, name);
-  if (status == 0)  /* parse OK? */
-    status = lua_call(L, 0, LUA_MULTRET);  /* call main */
-  return status;
-}
-
-
-LUA_API int lua_dostring (lua_State *L, const l_char *str) {
-  return lua_dobuffer(L, str, strlen(str), str);
-}
-
 
 
 /*
 /*
 ** {======================================================
 ** {======================================================

+ 5 - 2
lua.h

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lua.h,v 1.93 2001/04/06 21:17:37 roberto Exp roberto $
+** $Id: lua.h,v 1.94 2001/04/11 14:42:41 roberto Exp roberto $
 ** Lua - An Extensible Extension Language
 ** Lua - An Extensible Extension Language
 ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
 ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
 ** e-mail: [email protected]
 ** e-mail: [email protected]
@@ -178,12 +178,15 @@ LUA_API int   lua_ref (lua_State *L, int lock);
 
 
 
 
 /*
 /*
-** `do' functions (run Lua code)
+** `load' and `do' functions (load and run Lua code)
 */
 */
 LUA_API int   lua_call (lua_State *L, int nargs, int nresults);
 LUA_API int   lua_call (lua_State *L, int nargs, int nresults);
 LUA_API void  lua_rawcall (lua_State *L, int nargs, int nresults);
 LUA_API void  lua_rawcall (lua_State *L, int nargs, int nresults);
+LUA_API int   lua_loadfile (lua_State *L, const lua_char *filename);
 LUA_API int   lua_dofile (lua_State *L, const lua_char *filename);
 LUA_API int   lua_dofile (lua_State *L, const lua_char *filename);
 LUA_API int   lua_dostring (lua_State *L, const lua_char *str);
 LUA_API int   lua_dostring (lua_State *L, const lua_char *str);
+LUA_API int   lua_loadbuffer (lua_State *L, const lua_char *buff, size_t size,
+                            const lua_char *name);
 LUA_API int   lua_dobuffer (lua_State *L, const lua_char *buff, size_t size,
 LUA_API int   lua_dobuffer (lua_State *L, const lua_char *buff, size_t size,
                             const lua_char *name);
                             const lua_char *name);