diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2005-08-26 14:32:05 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2005-08-26 14:32:05 -0300 |
| commit | 9273fbd131eb0be7d4e7ca4f44345b41f8a557cf (patch) | |
| tree | 1503563eb840378013125b4892b7686ac8d8787e | |
| parent | be666a662b79e12c300dbf6d74d0a6626acac06f (diff) | |
| download | lua-9273fbd131eb0be7d4e7ca4f44345b41f8a557cf.tar.gz lua-9273fbd131eb0be7d4e7ca4f44345b41f8a557cf.tar.bz2 lua-9273fbd131eb0be7d4e7ca4f44345b41f8a557cf.zip | |
no more 'luaL_get/setfield' (replaced by more direct luaL_findtable)
| -rw-r--r-- | lauxlib.c | 58 | ||||
| -rw-r--r-- | lauxlib.h | 9 | ||||
| -rw-r--r-- | loadlib.c | 38 | ||||
| -rw-r--r-- | ltests.c | 21 | ||||
| -rw-r--r-- | lua.c | 19 |
5 files changed, 50 insertions, 95 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lauxlib.c,v 1.148 2005/08/18 20:36:26 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.149 2005/08/25 15:39:16 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 | */ |
| @@ -66,7 +66,7 @@ LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname) { | |||
| 66 | 66 | ||
| 67 | 67 | ||
| 68 | static void tag_error (lua_State *L, int narg, int tag) { | 68 | static void tag_error (lua_State *L, int narg, int tag) { |
| 69 | luaL_typerror(L, narg, lua_typename(L, tag)); | 69 | luaL_typerror(L, narg, lua_typename(L, tag)); |
| 70 | } | 70 | } |
| 71 | 71 | ||
| 72 | 72 | ||
| @@ -235,15 +235,9 @@ LUALIB_API void luaI_openlib (lua_State *L, const char *libname, | |||
| 235 | lua_getfield(L, -1, libname); /* get _LOADED[libname] */ | 235 | lua_getfield(L, -1, libname); /* get _LOADED[libname] */ |
| 236 | if (!lua_istable(L, -1)) { /* not found? */ | 236 | if (!lua_istable(L, -1)) { /* not found? */ |
| 237 | lua_pop(L, 1); /* remove previous result */ | 237 | lua_pop(L, 1); /* remove previous result */ |
| 238 | luaL_getfield(L, LUA_GLOBALSINDEX, libname); /* try global variable */ | 238 | /* try global variable (and create one if it does not exist) */ |
| 239 | if (!lua_istable(L, -1)) { | 239 | if (luaL_findtable(L, LUA_GLOBALSINDEX, libname) != NULL) |
| 240 | if (!lua_isnil(L, -1)) | 240 | luaL_error(L, "name conflict for module " LUA_QS, libname); |
| 241 | luaL_error(L, "name conflict for module " LUA_QS, libname); | ||
| 242 | lua_pop(L, 1); | ||
| 243 | lua_newtable(L); /* create it */ | ||
| 244 | lua_pushvalue(L, -1); /* register it with given name */ | ||
| 245 | luaL_setfield(L, LUA_GLOBALSINDEX, libname); | ||
| 246 | } | ||
| 247 | lua_pushvalue(L, -1); | 241 | lua_pushvalue(L, -1); |
| 248 | lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */ | 242 | lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */ |
| 249 | } | 243 | } |
| @@ -337,30 +331,13 @@ LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p, | |||
| 337 | } | 331 | } |
| 338 | 332 | ||
| 339 | 333 | ||
| 340 | 334 | LUALIB_API const char *luaL_findtable (lua_State *L, int idx, | |
| 341 | LUALIB_API const char *luaL_getfield (lua_State *L, int idx, | 335 | const char *fname) { |
| 342 | const char *fname) { | ||
| 343 | const char *e; | ||
| 344 | lua_pushvalue(L, idx); | ||
| 345 | while ((e = strchr(fname, '.')) != NULL) { | ||
| 346 | lua_pushlstring(L, fname, e - fname); | ||
| 347 | lua_rawget(L, -2); | ||
| 348 | lua_remove(L, -2); /* remove previous table */ | ||
| 349 | fname = e + 1; | ||
| 350 | if (!lua_istable(L, -1)) return fname; | ||
| 351 | } | ||
| 352 | lua_pushstring(L, fname); | ||
| 353 | lua_rawget(L, -2); /* get last field */ | ||
| 354 | lua_remove(L, -2); /* remove previous table */ | ||
| 355 | return NULL; | ||
| 356 | } | ||
| 357 | |||
| 358 | |||
| 359 | LUALIB_API const char *luaL_setfield (lua_State *L, int idx, | ||
| 360 | const char *fname) { | ||
| 361 | const char *e; | 336 | const char *e; |
| 362 | lua_pushvalue(L, idx); | 337 | lua_pushvalue(L, idx); |
| 363 | while ((e = strchr(fname, '.')) != NULL) { | 338 | do { |
| 339 | e = strchr(fname, '.'); | ||
| 340 | if (e == NULL) e = fname + strlen(fname); | ||
| 364 | lua_pushlstring(L, fname, e - fname); | 341 | lua_pushlstring(L, fname, e - fname); |
| 365 | lua_rawget(L, -2); | 342 | lua_rawget(L, -2); |
| 366 | if (lua_isnil(L, -1)) { /* no such field? */ | 343 | if (lua_isnil(L, -1)) { /* no such field? */ |
| @@ -370,16 +347,13 @@ LUALIB_API const char *luaL_setfield (lua_State *L, int idx, | |||
| 370 | lua_pushvalue(L, -2); | 347 | lua_pushvalue(L, -2); |
| 371 | lua_settable(L, -4); /* set new table into field */ | 348 | lua_settable(L, -4); /* set new table into field */ |
| 372 | } | 349 | } |
| 373 | lua_remove(L, -2); /* remove previous table */ | 350 | else if (!lua_istable(L, -1)) { /* field has a non-table value? */ |
| 374 | fname = e + 1; | ||
| 375 | if (!lua_istable(L, -1)) { | ||
| 376 | lua_pop(L, 2); /* remove table and value */ | 351 | lua_pop(L, 2); /* remove table and value */ |
| 377 | return fname; | 352 | return fname; /* return problematic part of the name */ |
| 378 | } | 353 | } |
| 379 | } | 354 | lua_remove(L, -2); /* remove previous table */ |
| 380 | lua_pushvalue(L, -2); /* move value to the top */ | 355 | fname = e + 1; |
| 381 | lua_setfield(L, -2, fname); /* set last field */ | 356 | } while (*e == '.'); |
| 382 | lua_pop(L, 2); /* remove value and table */ | ||
| 383 | return NULL; | 357 | return NULL; |
| 384 | } | 358 | } |
| 385 | 359 | ||
| @@ -635,7 +609,7 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { | |||
| 635 | free(ptr); | 609 | free(ptr); |
| 636 | return NULL; | 610 | return NULL; |
| 637 | } | 611 | } |
| 638 | else | 612 | else |
| 639 | return realloc(ptr, nsize); | 613 | return realloc(ptr, nsize); |
| 640 | } | 614 | } |
| 641 | 615 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lauxlib.h,v 1.81 2005/08/15 14:12:32 roberto Exp roberto $ | 2 | ** $Id: lauxlib.h,v 1.82 2005/08/18 20:36:26 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 | */ |
| @@ -84,10 +84,9 @@ LUALIB_API lua_State *(luaL_newstate) (void); | |||
| 84 | 84 | ||
| 85 | LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, | 85 | LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, |
| 86 | const char *r); | 86 | const char *r); |
| 87 | LUALIB_API const char *(luaL_getfield) (lua_State *L, int idx, | 87 | |
| 88 | const char *fname); | 88 | LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx, |
| 89 | LUALIB_API const char *(luaL_setfield) (lua_State *L, int idx, | 89 | const char *fname); |
| 90 | const char *fname); | ||
| 91 | 90 | ||
| 92 | 91 | ||
| 93 | 92 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: loadlib.c,v 1.39 2005/08/17 19:05:04 roberto Exp roberto $ | 2 | ** $Id: loadlib.c,v 1.40 2005/08/25 15:39:16 roberto Exp roberto $ |
| 3 | ** Dynamic library loader for Lua | 3 | ** Dynamic library loader for Lua |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | ** | 5 | ** |
| @@ -511,15 +511,9 @@ static int ll_module (lua_State *L) { | |||
| 511 | lua_getfield(L, 2, modname); /* get _LOADED[modname] */ | 511 | lua_getfield(L, 2, modname); /* get _LOADED[modname] */ |
| 512 | if (!lua_istable(L, -1)) { /* not found? */ | 512 | if (!lua_istable(L, -1)) { /* not found? */ |
| 513 | lua_pop(L, 1); /* remove previous result */ | 513 | lua_pop(L, 1); /* remove previous result */ |
| 514 | luaL_getfield(L, LUA_GLOBALSINDEX, modname); /* try global variable */ | 514 | /* try global variable (and create one if it does not exist) */ |
| 515 | if (!lua_istable(L, -1)) { | 515 | if (luaL_findtable(L, LUA_GLOBALSINDEX, modname) != NULL) |
| 516 | if (!lua_isnil(L, -1)) | 516 | return luaL_error(L, "name conflict for module " LUA_QS, modname); |
| 517 | return luaL_error(L, "name conflict for module " LUA_QS, modname); | ||
| 518 | lua_pop(L, 1); | ||
| 519 | lua_newtable(L); /* create it */ | ||
| 520 | lua_pushvalue(L, -1); /* register it with given name */ | ||
| 521 | luaL_setfield(L, LUA_GLOBALSINDEX, modname); | ||
| 522 | } | ||
| 523 | lua_pushvalue(L, -1); | 517 | lua_pushvalue(L, -1); |
| 524 | lua_setfield(L, 2, modname); /* _LOADED[modname] = new table */ | 518 | lua_setfield(L, 2, modname); /* _LOADED[modname] = new table */ |
| 525 | } | 519 | } |
| @@ -573,6 +567,12 @@ static void setpath (lua_State *L, const char *fieldname, const char *envname, | |||
| 573 | } | 567 | } |
| 574 | 568 | ||
| 575 | 569 | ||
| 570 | static const luaL_reg pk_funcs[] = { | ||
| 571 | {"loadlib", ll_loadlib}, | ||
| 572 | {NULL, NULL} | ||
| 573 | }; | ||
| 574 | |||
| 575 | |||
| 576 | static const luaL_reg ll_funcs[] = { | 576 | static const luaL_reg ll_funcs[] = { |
| 577 | {"module", ll_module}, | 577 | {"module", ll_module}, |
| 578 | {"require", ll_require}, | 578 | {"require", ll_require}, |
| @@ -591,9 +591,11 @@ LUALIB_API int luaopen_package (lua_State *L) { | |||
| 591 | lua_pushcfunction(L, gctm); | 591 | lua_pushcfunction(L, gctm); |
| 592 | lua_setfield(L, -2, "__gc"); | 592 | lua_setfield(L, -2, "__gc"); |
| 593 | /* create `package' table */ | 593 | /* create `package' table */ |
| 594 | lua_newtable(L); | 594 | luaL_register(L, LUA_LOADLIBNAME, pk_funcs); |
| 595 | lua_pushvalue(L, -1); | 595 | #if defined(LUA_COMPAT_LOADLIB) |
| 596 | lua_setglobal(L, LUA_LOADLIBNAME); | 596 | lua_getfield(L, -1, "loadlib"); |
| 597 | lua_setfield(L, LUA_GLOBALSINDEX, "loadlib"); | ||
| 598 | #endif | ||
| 597 | lua_pushvalue(L, -1); | 599 | lua_pushvalue(L, -1); |
| 598 | lua_setfield(L, LUA_REGISTRYINDEX, "_PACKAGE"); | 600 | lua_setfield(L, LUA_REGISTRYINDEX, "_PACKAGE"); |
| 599 | lua_pushvalue(L, -1); | 601 | lua_pushvalue(L, -1); |
| @@ -618,15 +620,9 @@ LUALIB_API int luaopen_package (lua_State *L) { | |||
| 618 | /* set field `preload' */ | 620 | /* set field `preload' */ |
| 619 | lua_newtable(L); | 621 | lua_newtable(L); |
| 620 | lua_setfield(L, -2, "preload"); | 622 | lua_setfield(L, -2, "preload"); |
| 621 | /* create `loadlib' function */ | ||
| 622 | lua_pushcfunction(L, ll_loadlib); | ||
| 623 | #if defined(LUA_COMPAT_LOADLIB) | ||
| 624 | lua_pushvalue(L, -1); | ||
| 625 | lua_setfield(L, LUA_GLOBALSINDEX, "loadlib"); | ||
| 626 | #endif | ||
| 627 | lua_setfield(L, -2, "loadlib"); | ||
| 628 | lua_pushvalue(L, LUA_GLOBALSINDEX); | 623 | lua_pushvalue(L, LUA_GLOBALSINDEX); |
| 629 | luaL_register(L, NULL, ll_funcs); /* open lib into global table */ | 624 | luaL_register(L, NULL, ll_funcs); /* open lib into global table */ |
| 630 | return 1; | 625 | lua_pop(L, 1); |
| 626 | return 1; /* return 'package' table */ | ||
| 631 | } | 627 | } |
| 632 | 628 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltests.c,v 2.27 2005/07/12 14:32:08 roberto Exp roberto $ | 2 | ** $Id: ltests.c,v 2.28 2005/08/15 14:12:32 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 | */ |
| @@ -1078,23 +1078,6 @@ static int auxgsub (lua_State *L) { | |||
| 1078 | } | 1078 | } |
| 1079 | 1079 | ||
| 1080 | 1080 | ||
| 1081 | static int auxgetf (lua_State *L) { | ||
| 1082 | const char *s = luaL_checkstring(L, 1); | ||
| 1083 | lua_settop(L, 2); | ||
| 1084 | lua_pushstring(L, luaL_getfield(L, 2, s)); | ||
| 1085 | lua_assert(lua_gettop(L) == 4); | ||
| 1086 | return 2; | ||
| 1087 | } | ||
| 1088 | |||
| 1089 | |||
| 1090 | static int auxsetf (lua_State *L) { | ||
| 1091 | const char *s = luaL_checkstring(L, 1); | ||
| 1092 | lua_settop(L, 3); | ||
| 1093 | lua_pushstring(L, luaL_setfield(L, 2, s)); | ||
| 1094 | lua_assert(lua_gettop(L) == 3); | ||
| 1095 | return 1; | ||
| 1096 | } | ||
| 1097 | |||
| 1098 | /* }====================================================== */ | 1081 | /* }====================================================== */ |
| 1099 | 1082 | ||
| 1100 | 1083 | ||
| @@ -1107,7 +1090,6 @@ static const struct luaL_reg tests_funcs[] = { | |||
| 1107 | {"doremote", doremote}, | 1090 | {"doremote", doremote}, |
| 1108 | {"gccolor", get_gccolor}, | 1091 | {"gccolor", get_gccolor}, |
| 1109 | {"gcstate", gcstate}, | 1092 | {"gcstate", gcstate}, |
| 1110 | {"getfield", auxgetf}, | ||
| 1111 | {"getref", getref}, | 1093 | {"getref", getref}, |
| 1112 | {"gsub", auxgsub}, | 1094 | {"gsub", auxgsub}, |
| 1113 | {"hash", hash_query}, | 1095 | {"hash", hash_query}, |
| @@ -1127,7 +1109,6 @@ static const struct luaL_reg tests_funcs[] = { | |||
| 1127 | {"ref", tref}, | 1109 | {"ref", tref}, |
| 1128 | {"resume", coresume}, | 1110 | {"resume", coresume}, |
| 1129 | {"s2d", s2d}, | 1111 | {"s2d", s2d}, |
| 1130 | {"setfield", auxsetf}, | ||
| 1131 | {"setyhook", setyhook}, | 1112 | {"setyhook", setyhook}, |
| 1132 | {"stacklevel", stacklevel}, | 1113 | {"stacklevel", stacklevel}, |
| 1133 | {"testC", testC}, | 1114 | {"testC", testC}, |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lua.c,v 1.147 2005/08/25 15:39:16 roberto Exp roberto $ | 2 | ** $Id: lua.c,v 1.148 2005/08/25 19:55:38 roberto Exp roberto $ |
| 3 | ** Lua stand-alone interpreter | 3 | ** Lua stand-alone interpreter |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -71,14 +71,19 @@ static int report (lua_State *L, int status) { | |||
| 71 | 71 | ||
| 72 | 72 | ||
| 73 | static int traceback (lua_State *L) { | 73 | static int traceback (lua_State *L) { |
| 74 | luaL_getfield(L, LUA_GLOBALSINDEX, "debug.traceback"); | 74 | lua_getfield(L, LUA_GLOBALSINDEX, "debug"); |
| 75 | if (!lua_isfunction(L, -1)) | 75 | if (!lua_istable(L, -1)) { |
| 76 | lua_pop(L, 1); | 76 | lua_pop(L, 1); |
| 77 | else { | 77 | return 1; |
| 78 | lua_pushvalue(L, 1); /* pass error message */ | ||
| 79 | lua_pushinteger(L, 2); /* skip this function and traceback */ | ||
| 80 | lua_call(L, 2, 1); /* call debug.traceback */ | ||
| 81 | } | 78 | } |
| 79 | lua_getfield(L, -1, "traceback"); | ||
| 80 | if (!lua_isfunction(L, -1)) { | ||
| 81 | lua_pop(L, 2); | ||
| 82 | return 1; | ||
| 83 | } | ||
| 84 | lua_pushvalue(L, 1); /* pass error message */ | ||
| 85 | lua_pushinteger(L, 2); /* skip this function and traceback */ | ||
| 86 | lua_call(L, 2, 1); /* call debug.traceback */ | ||
| 82 | return 1; | 87 | return 1; |
| 83 | } | 88 | } |
| 84 | 89 | ||
