Browse Source

luaL_openlib -> luaL_register and new function luaL_loadstring

Roberto Ierusalimschy 20 years ago
parent
commit
16ddf86168
2 changed files with 36 additions and 7 deletions
  1. 16 4
      lauxlib.c
  2. 20 3
      lauxlib.h

+ 16 - 4
lauxlib.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lauxlib.c,v 1.142 2005/08/09 12:30:19 roberto Exp roberto $
+** $Id: lauxlib.c,v 1.143 2005/08/10 18:47:09 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
 */
 */
@@ -235,7 +235,13 @@ LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
 }
 }
 
 
 
 
-LUALIB_API void luaL_openlib (lua_State *L, const char *libname,
+LUALIB_API void (luaL_register) (lua_State *L, const char *libname,
+                                const luaL_reg *l) {
+  luaI_openlib(L, libname, l, 0);
+}
+
+
+LUALIB_API void luaI_openlib (lua_State *L, const char *libname,
                               const luaL_reg *l, int nup) {
                               const luaL_reg *l, int nup) {
   if (libname) {
   if (libname) {
     /* check whether lib already exists */
     /* check whether lib already exists */
@@ -338,7 +344,7 @@ LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
     luaL_addstring(&b, r);  /* push replacement in place of pattern */
     luaL_addstring(&b, r);  /* push replacement in place of pattern */
     s = wild + l;  /* continue after `p' */
     s = wild + l;  /* continue after `p' */
   }
   }
-  luaL_addstring(&b, s);  /* push last suffix (`n' already includes this) */
+  luaL_addstring(&b, s);  /* push last suffix */
   luaL_pushresult(&b);
   luaL_pushresult(&b);
   return lua_tostring(L, -1);
   return lua_tostring(L, -1);
 }
 }
@@ -446,7 +452,7 @@ LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B) {
 
 
 LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
 LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
   while (l--)
   while (l--)
-    luaL_putchar(B, *s++);
+    luaL_addchar(B, *s++);
 }
 }
 
 
 
 
@@ -627,6 +633,12 @@ LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size,
 }
 }
 
 
 
 
+LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s) {
+  return luaL_loadbuffer(L, s, strlen(s), s);
+}
+
+
+
 /* }====================================================== */
 /* }====================================================== */
 
 
 
 

+ 20 - 3
lauxlib.h

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lauxlib.h,v 1.79 2005/05/31 14:34:02 roberto Exp roberto $
+** $Id: lauxlib.h,v 1.80 2005/07/13 19:02:42 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
 */
 */
@@ -20,6 +20,10 @@
 #define luaL_setn(L,i,j)        ((void)0)  /* no op! */
 #define luaL_setn(L,i,j)        ((void)0)  /* no op! */
 #endif
 #endif
 
 
+#if defined(LUA_COMPAT_OPENLIB)
+#define luaI_openlib	luaL_openlib
+#endif
+
 
 
 /* extra error code for `luaL_load' */
 /* extra error code for `luaL_load' */
 #define LUA_ERRFILE     (LUA_ERRERR+1)
 #define LUA_ERRFILE     (LUA_ERRERR+1)
@@ -31,8 +35,11 @@ typedef struct luaL_reg {
 } luaL_reg;
 } luaL_reg;
 
 
 
 
-LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname,
+
+LUALIB_API void (luaI_openlib) (lua_State *L, const char *libname,
                                 const luaL_reg *l, int nup);
                                 const luaL_reg *l, int nup);
+LUALIB_API void (luaL_register) (lua_State *L, const char *libname,
+                                const luaL_reg *l);
 LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e);
 LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e);
 LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e);
 LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e);
 LUALIB_API int (luaL_typerror) (lua_State *L, int narg, const char *tname);
 LUALIB_API int (luaL_typerror) (lua_State *L, int narg, const char *tname);
@@ -71,6 +78,7 @@ LUALIB_API void (luaL_setn) (lua_State *L, int t, int n);
 LUALIB_API int (luaL_loadfile) (lua_State *L, const char *filename);
 LUALIB_API int (luaL_loadfile) (lua_State *L, const char *filename);
 LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz,
 LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz,
                                   const char *name);
                                   const char *name);
+LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s);
 
 
 LUALIB_API lua_State *(luaL_newstate) (void);
 LUALIB_API lua_State *(luaL_newstate) (void);
 
 
@@ -84,6 +92,7 @@ LUALIB_API const char *(luaL_setfield) (lua_State *L, int idx,
 
 
 
 
 
 
+
 /*
 /*
 ** ===============================================================
 ** ===============================================================
 ** some useful macros
 ** some useful macros
@@ -101,6 +110,11 @@ LUALIB_API const char *(luaL_setfield) (lua_State *L, int idx,
 
 
 #define luaL_typename(L,i)	lua_typename(L, lua_type(L,(i)))
 #define luaL_typename(L,i)	lua_typename(L, lua_type(L,(i)))
 
 
+#define luaL_dofile(L, fn)	(luaL_loadfile(L, fn) || lua_pcall(L, 0, 0, 0))
+
+#define luaL_dostring(L, s)	(luaL_loadstring(L, s) || lua_pcall(L, 0, 0, 0))
+
+
 /*
 /*
 ** {======================================================
 ** {======================================================
 ** Generic Buffer manipulation
 ** Generic Buffer manipulation
@@ -116,10 +130,13 @@ typedef struct luaL_Buffer {
   char buffer[LUAL_BUFFERSIZE];
   char buffer[LUAL_BUFFERSIZE];
 } luaL_Buffer;
 } luaL_Buffer;
 
 
-#define luaL_putchar(B,c) \
+#define luaL_addchar(B,c) \
   ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \
   ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \
    (*(B)->p++ = (char)(c)))
    (*(B)->p++ = (char)(c)))
 
 
+/* compatibility only */
+#define luaL_putchar(B,c)	luaL_addchar(B,c)
+
 #define luaL_addsize(B,n)	((B)->p += (n))
 #define luaL_addsize(B,n)	((B)->p += (n))
 
 
 LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B);
 LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B);