diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-09-21 13:54:32 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-09-21 13:54:32 -0300 |
| commit | 6acfb91c8d4dd1174545537ef85e1dacfdb04633 (patch) | |
| tree | 045cf5caa21bdc6707ac5fccfc120c34cfecc62c | |
| parent | 2419f2bf02a9165471248f09bae57e3fa134e545 (diff) | |
| download | lua-6acfb91c8d4dd1174545537ef85e1dacfdb04633.tar.gz lua-6acfb91c8d4dd1174545537ef85e1dacfdb04633.tar.bz2 lua-6acfb91c8d4dd1174545537ef85e1dacfdb04633.zip | |
new auxiliary functions to implement new package system
| -rw-r--r-- | lauxlib.c | 49 | ||||
| -rw-r--r-- | lauxlib.h | 8 | ||||
| -rw-r--r-- | ltests.c | 43 |
3 files changed, 95 insertions, 5 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lauxlib.c,v 1.123 2004/08/30 18:35:14 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.124 2004/09/03 13:17:14 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 | */ |
| @@ -352,8 +352,8 @@ static const char *pushnexttemplate (lua_State *L, const char *path) { | |||
| 352 | } | 352 | } |
| 353 | 353 | ||
| 354 | 354 | ||
| 355 | static const char *luaL_gsub (lua_State *L, const char *s, | 355 | LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p, |
| 356 | const char *p, const char *r) { | 356 | const char *r) { |
| 357 | const char *wild; | 357 | const char *wild; |
| 358 | int l = strlen(p); | 358 | int l = strlen(p); |
| 359 | luaL_Buffer b; | 359 | luaL_Buffer b; |
| @@ -391,6 +391,49 @@ LUALIB_API const char *luaL_searchpath (lua_State *L, const char *name, | |||
| 391 | } | 391 | } |
| 392 | 392 | ||
| 393 | 393 | ||
| 394 | LUALIB_API const char *luaL_getfield (lua_State *L, const char *fname) { | ||
| 395 | const char *e; | ||
| 396 | while ((e = strchr(fname, '.')) != NULL) { | ||
| 397 | lua_pushlstring(L, fname, e - fname); | ||
| 398 | lua_gettable(L, -2); | ||
| 399 | lua_remove(L, -2); /* remove previous table */ | ||
| 400 | fname = e + 1; | ||
| 401 | if (!lua_istable(L, -1)) return fname; | ||
| 402 | } | ||
| 403 | lua_getfield(L, -1, fname); /* get last field */ | ||
| 404 | lua_remove(L, -2); /* remove previous table */ | ||
| 405 | return NULL; | ||
| 406 | } | ||
| 407 | |||
| 408 | |||
| 409 | LUALIB_API const char *luaL_setfield (lua_State *L, const char *fname) { | ||
| 410 | const char *e; | ||
| 411 | lua_insert(L, -2); /* move value to below table */ | ||
| 412 | while ((e = strchr(fname, '.')) != NULL) { | ||
| 413 | lua_pushlstring(L, fname, e - fname); | ||
| 414 | lua_gettable(L, -2); | ||
| 415 | if (lua_isnil(L, -1)) { /* no such field? */ | ||
| 416 | lua_pop(L, 1); /* remove this nil */ | ||
| 417 | lua_newtable(L); /* create a new table for field */ | ||
| 418 | lua_pushlstring(L, fname, e - fname); | ||
| 419 | lua_pushvalue(L, -2); | ||
| 420 | lua_settable(L, -4); /* set new table into field */ | ||
| 421 | } | ||
| 422 | lua_remove(L, -2); /* remove previous table */ | ||
| 423 | fname = e + 1; | ||
| 424 | if (!lua_istable(L, -1)) { | ||
| 425 | lua_pop(L, 2); /* remove table and value */ | ||
| 426 | return fname; | ||
| 427 | } | ||
| 428 | } | ||
| 429 | lua_insert(L, -2); /* move table to below value */ | ||
| 430 | lua_setfield(L, -2, fname); /* set last field */ | ||
| 431 | lua_remove(L, -2); /* remove table */ | ||
| 432 | return NULL; | ||
| 433 | } | ||
| 434 | |||
| 435 | |||
| 436 | |||
| 394 | /* | 437 | /* |
| 395 | ** {====================================================== | 438 | ** {====================================================== |
| 396 | ** Generic Buffer manipulation | 439 | ** Generic Buffer manipulation |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lauxlib.h,v 1.69 2004/06/30 12:58:44 roberto Exp roberto $ | 2 | ** $Id: lauxlib.h,v 1.70 2004/07/09 18:23:17 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 | */ |
| @@ -70,6 +70,12 @@ LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t sz, | |||
| 70 | LUALIB_API lua_State *(luaL_newstate) (void); | 70 | LUALIB_API lua_State *(luaL_newstate) (void); |
| 71 | 71 | ||
| 72 | 72 | ||
| 73 | LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p, | ||
| 74 | const char *r); | ||
| 75 | LUALIB_API const char *luaL_getfield (lua_State *L, const char *fname); | ||
| 76 | LUALIB_API const char *luaL_setfield (lua_State *L, const char *fname); | ||
| 77 | |||
| 78 | |||
| 73 | 79 | ||
| 74 | /* | 80 | /* |
| 75 | ** =============================================================== | 81 | ** =============================================================== |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltests.c,v 2.10 2004/07/09 18:23:17 roberto Exp roberto $ | 2 | ** $Id: ltests.c,v 2.11 2004/08/24 20:12:06 roberto Exp roberto $ |
| 3 | ** Internal Module for Debugging of the Lua Implementation | 3 | ** Internal Module for Debugging of the Lua Implementation |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -1029,6 +1029,44 @@ static int coresume (lua_State *L) { | |||
| 1029 | 1029 | ||
| 1030 | 1030 | ||
| 1031 | 1031 | ||
| 1032 | /* | ||
| 1033 | ** {====================================================== | ||
| 1034 | ** tests auxlib functions | ||
| 1035 | ** ======================================================= | ||
| 1036 | */ | ||
| 1037 | |||
| 1038 | static int auxgsub (lua_State *L) { | ||
| 1039 | const char *s1 = luaL_checkstring(L, 1); | ||
| 1040 | const char *s2 = luaL_checkstring(L, 2); | ||
| 1041 | const char *s3 = luaL_checkstring(L, 3); | ||
| 1042 | lua_settop(L, 3); | ||
| 1043 | luaL_gsub(L, s1, s2, s3); | ||
| 1044 | lua_assert(lua_gettop(L) == 4); | ||
| 1045 | return 1; | ||
| 1046 | } | ||
| 1047 | |||
| 1048 | |||
| 1049 | static int auxgetf (lua_State *L) { | ||
| 1050 | const char *s = luaL_checkstring(L, 1); | ||
| 1051 | lua_settop(L, 2); | ||
| 1052 | lua_pushstring(L, luaL_getfield(L, s)); | ||
| 1053 | lua_assert(lua_gettop(L) == 3); | ||
| 1054 | return 2; | ||
| 1055 | } | ||
| 1056 | |||
| 1057 | |||
| 1058 | static int auxsetf (lua_State *L) { | ||
| 1059 | const char *s = luaL_checkstring(L, 1); | ||
| 1060 | lua_settop(L, 3); | ||
| 1061 | lua_pushstring(L, luaL_setfield(L, s)); | ||
| 1062 | lua_assert(lua_gettop(L) == 2); | ||
| 1063 | return 1; | ||
| 1064 | } | ||
| 1065 | |||
| 1066 | /* }====================================================== */ | ||
| 1067 | |||
| 1068 | |||
| 1069 | |||
| 1032 | static const struct luaL_reg tests_funcs[] = { | 1070 | static const struct luaL_reg tests_funcs[] = { |
| 1033 | {"hash", hash_query}, | 1071 | {"hash", hash_query}, |
| 1034 | {"limits", get_limits}, | 1072 | {"limits", get_limits}, |
| @@ -1063,6 +1101,9 @@ static const struct luaL_reg tests_funcs[] = { | |||
| 1063 | {"totalmem", mem_query}, | 1101 | {"totalmem", mem_query}, |
| 1064 | {"resume", coresume}, | 1102 | {"resume", coresume}, |
| 1065 | {"setyhook", setyhook}, | 1103 | {"setyhook", setyhook}, |
| 1104 | {"gsub", auxgsub}, | ||
| 1105 | {"getfield", auxgetf}, | ||
| 1106 | {"setfield", auxsetf}, | ||
| 1066 | {NULL, NULL} | 1107 | {NULL, NULL} |
| 1067 | }; | 1108 | }; |
| 1068 | 1109 | ||
