diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2005-08-15 11:12:32 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2005-08-15 11:12:32 -0300 |
commit | 16ddf86168998d500283e160977ef5ecea72c915 (patch) | |
tree | de27e08553a6ed9634f3571df9c73fd9ba8de412 | |
parent | e4d369c9b751154514ffa0b78e96f0d393d7fb3a (diff) | |
download | lua-16ddf86168998d500283e160977ef5ecea72c915.tar.gz lua-16ddf86168998d500283e160977ef5ecea72c915.tar.bz2 lua-16ddf86168998d500283e160977ef5ecea72c915.zip |
luaL_openlib -> luaL_register and new function luaL_loadstring
-rw-r--r-- | lauxlib.c | 20 | ||||
-rw-r--r-- | lauxlib.h | 23 |
2 files changed, 36 insertions, 7 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.c,v 1.142 2005/08/09 12:30:19 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.143 2005/08/10 18:47:09 roberto Exp roberto $ |
3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -235,7 +235,13 @@ LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) { | |||
235 | } | 235 | } |
236 | 236 | ||
237 | 237 | ||
238 | LUALIB_API void luaL_openlib (lua_State *L, const char *libname, | 238 | LUALIB_API void (luaL_register) (lua_State *L, const char *libname, |
239 | const luaL_reg *l) { | ||
240 | luaI_openlib(L, libname, l, 0); | ||
241 | } | ||
242 | |||
243 | |||
244 | LUALIB_API void luaI_openlib (lua_State *L, const char *libname, | ||
239 | const luaL_reg *l, int nup) { | 245 | const luaL_reg *l, int nup) { |
240 | if (libname) { | 246 | if (libname) { |
241 | /* check whether lib already exists */ | 247 | /* check whether lib already exists */ |
@@ -338,7 +344,7 @@ LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p, | |||
338 | luaL_addstring(&b, r); /* push replacement in place of pattern */ | 344 | luaL_addstring(&b, r); /* push replacement in place of pattern */ |
339 | s = wild + l; /* continue after `p' */ | 345 | s = wild + l; /* continue after `p' */ |
340 | } | 346 | } |
341 | luaL_addstring(&b, s); /* push last suffix (`n' already includes this) */ | 347 | luaL_addstring(&b, s); /* push last suffix */ |
342 | luaL_pushresult(&b); | 348 | luaL_pushresult(&b); |
343 | return lua_tostring(L, -1); | 349 | return lua_tostring(L, -1); |
344 | } | 350 | } |
@@ -446,7 +452,7 @@ LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B) { | |||
446 | 452 | ||
447 | LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) { | 453 | LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) { |
448 | while (l--) | 454 | while (l--) |
449 | luaL_putchar(B, *s++); | 455 | luaL_addchar(B, *s++); |
450 | } | 456 | } |
451 | 457 | ||
452 | 458 | ||
@@ -627,6 +633,12 @@ LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size, | |||
627 | } | 633 | } |
628 | 634 | ||
629 | 635 | ||
636 | LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s) { | ||
637 | return luaL_loadbuffer(L, s, strlen(s), s); | ||
638 | } | ||
639 | |||
640 | |||
641 | |||
630 | /* }====================================================== */ | 642 | /* }====================================================== */ |
631 | 643 | ||
632 | 644 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.h,v 1.79 2005/05/31 14:34:02 roberto Exp roberto $ | 2 | ** $Id: lauxlib.h,v 1.80 2005/07/13 19:02:42 roberto Exp roberto $ |
3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -20,6 +20,10 @@ | |||
20 | #define luaL_setn(L,i,j) ((void)0) /* no op! */ | 20 | #define luaL_setn(L,i,j) ((void)0) /* no op! */ |
21 | #endif | 21 | #endif |
22 | 22 | ||
23 | #if defined(LUA_COMPAT_OPENLIB) | ||
24 | #define luaI_openlib luaL_openlib | ||
25 | #endif | ||
26 | |||
23 | 27 | ||
24 | /* extra error code for `luaL_load' */ | 28 | /* extra error code for `luaL_load' */ |
25 | #define LUA_ERRFILE (LUA_ERRERR+1) | 29 | #define LUA_ERRFILE (LUA_ERRERR+1) |
@@ -31,8 +35,11 @@ typedef struct luaL_reg { | |||
31 | } luaL_reg; | 35 | } luaL_reg; |
32 | 36 | ||
33 | 37 | ||
34 | LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname, | 38 | |
39 | LUALIB_API void (luaI_openlib) (lua_State *L, const char *libname, | ||
35 | const luaL_reg *l, int nup); | 40 | const luaL_reg *l, int nup); |
41 | LUALIB_API void (luaL_register) (lua_State *L, const char *libname, | ||
42 | const luaL_reg *l); | ||
36 | LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); | 43 | LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); |
37 | LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); | 44 | LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); |
38 | LUALIB_API int (luaL_typerror) (lua_State *L, int narg, const char *tname); | 45 | 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); | |||
71 | LUALIB_API int (luaL_loadfile) (lua_State *L, const char *filename); | 78 | LUALIB_API int (luaL_loadfile) (lua_State *L, const char *filename); |
72 | LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, | 79 | LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, |
73 | const char *name); | 80 | const char *name); |
81 | LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); | ||
74 | 82 | ||
75 | LUALIB_API lua_State *(luaL_newstate) (void); | 83 | LUALIB_API lua_State *(luaL_newstate) (void); |
76 | 84 | ||
@@ -84,6 +92,7 @@ LUALIB_API const char *(luaL_setfield) (lua_State *L, int idx, | |||
84 | 92 | ||
85 | 93 | ||
86 | 94 | ||
95 | |||
87 | /* | 96 | /* |
88 | ** =============================================================== | 97 | ** =============================================================== |
89 | ** some useful macros | 98 | ** some useful macros |
@@ -101,6 +110,11 @@ LUALIB_API const char *(luaL_setfield) (lua_State *L, int idx, | |||
101 | 110 | ||
102 | #define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) | 111 | #define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) |
103 | 112 | ||
113 | #define luaL_dofile(L, fn) (luaL_loadfile(L, fn) || lua_pcall(L, 0, 0, 0)) | ||
114 | |||
115 | #define luaL_dostring(L, s) (luaL_loadstring(L, s) || lua_pcall(L, 0, 0, 0)) | ||
116 | |||
117 | |||
104 | /* | 118 | /* |
105 | ** {====================================================== | 119 | ** {====================================================== |
106 | ** Generic Buffer manipulation | 120 | ** Generic Buffer manipulation |
@@ -116,10 +130,13 @@ typedef struct luaL_Buffer { | |||
116 | char buffer[LUAL_BUFFERSIZE]; | 130 | char buffer[LUAL_BUFFERSIZE]; |
117 | } luaL_Buffer; | 131 | } luaL_Buffer; |
118 | 132 | ||
119 | #define luaL_putchar(B,c) \ | 133 | #define luaL_addchar(B,c) \ |
120 | ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \ | 134 | ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \ |
121 | (*(B)->p++ = (char)(c))) | 135 | (*(B)->p++ = (char)(c))) |
122 | 136 | ||
137 | /* compatibility only */ | ||
138 | #define luaL_putchar(B,c) luaL_addchar(B,c) | ||
139 | |||
123 | #define luaL_addsize(B,n) ((B)->p += (n)) | 140 | #define luaL_addsize(B,n) ((B)->p += (n)) |
124 | 141 | ||
125 | LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); | 142 | LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); |