aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2004-09-21 13:54:32 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2004-09-21 13:54:32 -0300
commit6acfb91c8d4dd1174545537ef85e1dacfdb04633 (patch)
tree045cf5caa21bdc6707ac5fccfc120c34cfecc62c
parent2419f2bf02a9165471248f09bae57e3fa134e545 (diff)
downloadlua-6acfb91c8d4dd1174545537ef85e1dacfdb04633.tar.gz
lua-6acfb91c8d4dd1174545537ef85e1dacfdb04633.tar.bz2
lua-6acfb91c8d4dd1174545537ef85e1dacfdb04633.zip
new auxiliary functions to implement new package system
Diffstat (limited to '')
-rw-r--r--lauxlib.c49
-rw-r--r--lauxlib.h8
-rw-r--r--ltests.c43
3 files changed, 95 insertions, 5 deletions
diff --git a/lauxlib.c b/lauxlib.c
index bf91f102..9de786b4 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -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
355static const char *luaL_gsub (lua_State *L, const char *s, 355LUALIB_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
394LUALIB_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
409LUALIB_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
diff --git a/lauxlib.h b/lauxlib.h
index 1a0295d6..334ddd6e 100644
--- a/lauxlib.h
+++ b/lauxlib.h
@@ -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,
70LUALIB_API lua_State *(luaL_newstate) (void); 70LUALIB_API lua_State *(luaL_newstate) (void);
71 71
72 72
73LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
74 const char *r);
75LUALIB_API const char *luaL_getfield (lua_State *L, const char *fname);
76LUALIB_API const char *luaL_setfield (lua_State *L, const char *fname);
77
78
73 79
74/* 80/*
75** =============================================================== 81** ===============================================================
diff --git a/ltests.c b/ltests.c
index 5f2cddf1..2579cd0d 100644
--- a/ltests.c
+++ b/ltests.c
@@ -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
1038static 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
1049static 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
1058static 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
1032static const struct luaL_reg tests_funcs[] = { 1070static 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