Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sandbox #29

Closed
DominikMS opened this issue May 31, 2014 · 3 comments

Comments

Projects
None yet
3 participants
@DominikMS
Copy link

commented May 31, 2014

Sandboxing is very usefull while you want to load more than one file (ex. for handlers, callbacks).
This code is not working under 5.2, function lua_setfenv is missing.

        lua_State* l = L.get();
        if(luaL_loadfile(state, filename.c_str())) {
            lua_error(state);
            return;
        }

    // create env table
    lua_newtable( state );
    int envTable = lua_gettop( state );
    lua_newtable( state );

    // set the env
    lua_setfenv( state, -2 );

    // copy _G
    lua_newtable( state );
    int __index = lua_gettop( state );
    lua_pushstring( state, "__index" );
    lua_getfield( state, LUA_GLOBALSINDEX, "_G" );
    lua_settable( state, __index );

    // set the metatable for the env table
    lua_setmetatable( state, envTable );

    // set the env
    lua_pushvalue(state, -1);
    lua_setfield(state, LUA_GLOBALSINDEX, name.c_str());

    lua_setfenv( state, -2 );

    lua_pcall(state, 0, -1, 0);
@ThePhD

This comment has been minimized.

Copy link
Contributor

commented May 31, 2014

I can't help you here. I don't even know what Sandboxing means in terms of Lua. I'm not even sure this is related to sol: we don't provide debugging and helpdesking for general Lua issues. If you're trying to make a feature-request to support "Sandboxing", whatever that is supposed to be in this situation, please be more clear about that. If this is just a general Lua question, I can't help you.

@Rapptz

This comment has been minimized.

Copy link
Owner

commented May 31, 2014

Lua 5.2 removed that method of sandboxing. I don't plan on supporting the other much more verbose way of sandboxing any time soon and I never plan on supporting Lua 5.1.

You can do it yourself. See here for more details: http://stackoverflow.com/a/6982080/1381108
sol::state has a member function to return the managed state, lua_state. So you can do what you want manually on the Lua side if you feel something is missing. The only gotcha is that you can't delete it because it's managed internally by sol::state.

@Rapptz Rapptz added the wontfix label May 31, 2014

@DominikMS

This comment has been minimized.

Copy link
Author

commented May 31, 2014

I have do it in lua, now is easy to add this to c++. Thread can be closed.

g_test = setmetatable({}, { __index = _G })
loadfile("script2.lua", "g_test", g_test)()

@Rapptz Rapptz closed this May 31, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.