Julien Danjou yazmış:
> Hi Ali,
>
> At 1241818438 time_t, Ali Polatel wrote:
>
> Ah, I've played with this piece of code, that's sad it is replaced. :)
>
> > /* add Lua lib path (/usr/share/awesome/lib by default) */
> > + lua_getglobal(L, "package");
> > + if (LUA_TTABLE != lua_type(L, 1)) {
> > + warn("package is not a table");
> > + return;
> > + }
> > + lua_getfield(L, 1, "path");
> > + if (LUA_TSTRING != lua_type(L, 2)) {
> > + warn("package.path is not a string");
> > + lua_pop(L, 1);
> > + return;
> > + }
> > + lua_pushliteral(L, ";" AWESOME_LUA_LIB_PATH "/?.lua");
> > + lua_pushliteral(L, ";" AWESOME_LUA_LIB_PATH "/?/init.lua");
> > + lua_concat(L, 3); /* concatenate with package.path */
> > luaA_dostring(L, "package.path = package.path .. \";"
> > AWESOME_LUA_LIB_PATH "/?.lua\"");
> > luaA_dostring(L, "package.path = package.path .. \";"
> > AWESOME_LUA_LIB_PATH "/?/init.lua\"");
>
> What's the point of letting the 2 luaA_dostring()?
> You forget to remove?Oops, i forget to remove them :) Attached is the updated patch. > Cheers, -- Regards, Ali Polatel
From 6fc0d5aab004b5fe009f06f4cd7a051a82961c73 Mon Sep 17 00:00:00 2001 From: Ali Polatel <[email protected]> Date: Sat, 9 May 2009 11:09:21 +0300 Subject: [PATCH] luaA_init: use Lua C API to add paths to package.path Organization: Pink Floyd This is cleaner and it should be a bit faster than using luaL_dostring(). --- luaa.c | 52 +++++++++++++++++++++++++++++++--------------------- 1 files changed, 31 insertions(+), 21 deletions(-) diff --git a/luaa.c b/luaa.c index 32903f9..b872a2a 100644 --- a/luaa.c +++ b/luaa.c @@ -679,20 +679,25 @@ luaA_init(xdgHandle* xdg) /* Export root lib */ luaL_register(L, "root", awesome_root_lib); + lua_pop(L, 1); /* luaL_register() leaves the table on stack */ /* Export hooks lib */ luaL_register(L, "hooks", awesome_hooks_lib); + lua_pop(L, 1); /* luaL_register() leaves the table on stack */ #ifdef WITH_DBUS /* Export D-Bus lib */ luaL_register(L, "dbus", awesome_dbus_lib); + lua_pop(L, 1); /* luaL_register() leaves the table on stack */ #endif /* Export keygrabber lib */ luaL_register(L, "keygrabber", awesome_keygrabber_lib); + lua_pop(L, 1); /* luaL_register() leaves the table on stack */ /* Export mousegrabber lib */ luaL_register(L, "mousegrabber", awesome_mousegrabber_lib); + lua_pop(L, 1); /* luaL_register() leaves the table on stack */ /* Export screen */ luaA_openlib(L, "screen", awesome_screen_methods, awesome_screen_meta); @@ -740,34 +745,39 @@ luaA_init(xdgHandle* xdg) #endif /* add Lua lib path (/usr/share/awesome/lib by default) */ - luaA_dostring(L, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH "/?.lua\""); - luaA_dostring(L, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH "/?/init.lua\""); + lua_getglobal(L, "package"); + if (LUA_TTABLE != lua_type(L, 1)) { + warn("package is not a table"); + return; + } + lua_getfield(L, 1, "path"); + if (LUA_TSTRING != lua_type(L, 2)) { + warn("package.path is not a string"); + lua_pop(L, 1); + return; + } + lua_pushliteral(L, ";" AWESOME_LUA_LIB_PATH "/?.lua"); + lua_pushliteral(L, ";" AWESOME_LUA_LIB_PATH "/?/init.lua"); + lua_concat(L, 3); /* concatenate with package.path */ /* add XDG_CONFIG_DIR as include path */ const char * const *xdgconfigdirs = xdgSearchableConfigDirectories(xdg); - buffer_t buf; - int prev_len = 0; - buffer_init(&buf); -#define A_LUAA_FIRST_STRING "package.path = package.path .. \";" -#define A_LUAA_SECOND_STRING "/awesome/?.lua;" -#define A_LUAA_FULL_STRING A_LUAA_FIRST_STRING A_LUAA_SECOND_STRING "/awesome/?/init.lua\"" - buffer_addsl(&buf, A_LUAA_FULL_STRING); for(; *xdgconfigdirs; xdgconfigdirs++) { size_t len = a_strlen(*xdgconfigdirs); - buffer_splice(&buf, sizeof(A_LUAA_FIRST_STRING) - 1, prev_len, *xdgconfigdirs, len); - buffer_splice(&buf, - sizeof(A_LUAA_FIRST_STRING) - 1 - + len - + sizeof(A_LUAA_SECOND_STRING) - 1, - prev_len, *xdgconfigdirs, len); - luaA_dostring(L, buf.s); - prev_len = len; + lua_pushliteral(L, ";"); + lua_pushlstring(L, *xdgconfigdirs, len); + lua_pushliteral(L, "/awesome/?.lua"); + lua_concat(L, 3); + + lua_pushliteral(L, ";"); + lua_pushlstring(L, *xdgconfigdirs, len); + lua_pushliteral(L, "/awesome/?/init.lua"); + lua_concat(L, 3); + + lua_concat(L, 3); /* concatenate with package.path */ } -#undef A_LUAA_FIRST_STRING -#undef A_LUAA_SECOND_STRING -#undef A_LUAA_FULL_STRING - buffer_wipe(&buf); + lua_setfield(L, 1, "path"); /* package.path = "concatenated string" */ } static bool -- 1.6.2.5
pgplZDuPygW2T.pgp
Description: PGP signature
