lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


hi all,

here I present my solution for the problem in subj, any comments are
welcome.

Best regards,
Dmitriy Iassenev

bool load_file_into_namespace(lua_State *L, LPCSTR S, LPCSTR N, bool bCall =
true)
{
    lua_pushstring  (L,N);
    lua_gettable    (L,LUA_GLOBALSINDEX);
    if (lua_isnil(L,-1)) {
        lua_pop         (L,1);
        lua_newtable    (L);
        lua_pushstring  (L,N);
        lua_pushvalue   (L,-2);
        lua_settable    (L,LUA_GLOBALSINDEX);
    }
    else
        if (!lua_istable(L,-1)) {
            lua_pop         (L,1);
            printf          (" Error : the namespace name is already being
used by the non-table object!\n");
            return          (false);
        }

    lua_newtable    (L);
    lua_pushstring  (L,"_G");
    lua_gettable    (L,LUA_GLOBALSINDEX);
    lua_pushnil     (L);
    while (lua_next(L, -2) != 0) {
        lua_pushvalue   (L,-2);
        lua_pushvalue   (L,-2);
        lua_settable    (L,-6);
        lua_pop         (L, 1);
    }

    if (luaL_loadfile(L,S)) {
        printf          (" Error in file %s!\n",S);
        for (int i=0; ; i++)
            if (lua_isstring(L,i))
                printf  (" %s\n",lua_tostring(L,i));
            else
                break;
        lua_error       (L);
        lua_pop         (L,3);
        return          (false);
    }

    if (bCall)
        lua_call        (L,0,0);
    else
        lua_insert      (L,-4);

    lua_pushnil     (L);
    while (lua_next(L, -2) != 0) {
        lua_pushvalue   (L,-2);
        lua_gettable    (L,-5);
        if (lua_isnil(L,-1)) {
            lua_pop         (L,1);
            lua_pushvalue   (L,-2);
            lua_pushvalue   (L,-2);
            lua_pushvalue   (L,-2);
            lua_pushnil     (L);
            lua_settable    (L,-7);
            lua_settable    (L,-7);
        }
        else {
            lua_pop         (L,1);
            lua_pushvalue   (L,-2);
            lua_gettable    (L,-4);
            if (!lua_equal(L,-1,-2)) {
                lua_pushvalue   (L,-3);
                lua_pushvalue   (L,-2);
                lua_pushvalue   (L,-2);
                lua_pushvalue   (L,-5);
                lua_settable    (L,-8);
                lua_settable    (L,-8);
            }
            lua_pop         (L,1);
        }
        lua_pushvalue   (L,-2);
        lua_pushnil     (L);
        lua_settable    (L,-6);
        lua_pop         (L, 1);
    }

    lua_pop         (L,3);
    return          (true);
}

// main
int main(int argc, char* argv[])
{
    lua_State       *L = lua_open();
    if (!L)
        lua_error   (L);

    luaopen_base    (L);
    luaopen_string  (L);
    luaopen_math    (L);
    luaopen_table   (L);

    lua_pop         (L,4);
    load_file_into_namespace(L,"extension.lua","core");
    load_file_into_namespace(L,"extension1.lua","_G");
    load_file_into_namespace(L,"extension2.lua","core");
    lua_State       *T = lua_newthread(L);
    load_file_into_namespace(T,"test1.lua","test1",false);

    lua_resume      (T,0);

    lua_close       (L);
}