[C API] Can I delete a chunk from my lua_State?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

[C API] Can I delete a chunk from my lua_State?

Felipe Ferreira da Silva
I'm creating a game editor which load scripts writen by the user. But, every time the user changes the script, this script needs to be updated. To do that, my program would have to "unload"(delete) that previous script, and load it again (the updated version). I wasn't able to find any resource about it. This is something like what I'm intended to do:


lua_State *lua = luaL_newstate();
luaL_openlibs(lua);
luaL_loadfile(lua, "./start.lua");
if (lua_pcall(lua, 0, 0, 0) != LUA_OK) {
printf("Fail to load script.\n");
}

// Test if the script was loaded correctly. Result is OK.
lua_getglobal(BWindowHandler->lua, "function");
if (lua_pcall(BWindowHandler->lua, 0, 0, 0) != LUA_OK) {
printf("Function called.\n");
};

// !!!Delete the chunk here!!!

// Test again, now the result can not be OK, since the chunk was deleted!
lua_getglobal(BWindowHandler->lua, "function");
if (lua_pcall(BWindowHandler->lua, 0, 0, 0) != LUA_OK) {
printf("Function called.\n");
};


Thanks...
--
- Felipe Ferreira da Silva
Reply | Threaded
Open this post in threaded view
|

Re: [C API] Can I delete a chunk from my lua_State?

Thomas Jericke
On 03/03/2014 12:06 PM, Felipe Ferreira da Silva wrote:
I'm creating a game editor which load scripts writen by the user. But, every time the user changes the script, this script needs to be updated. To do that, my program would have to "unload"(delete) that previous script, and load it again (the updated version). I wasn't able to find any resource about it. This is something like what I'm intended to do:


lua_State *lua = luaL_newstate();
luaL_openlibs(lua);
luaL_loadfile(lua, "./start.lua");
if (lua_pcall(lua, 0, 0, 0) != LUA_OK) {
printf("Fail to load script.\n");
}

// Test if the script was loaded correctly. Result is OK.
lua_getglobal(BWindowHandler->lua, "function");
if (lua_pcall(BWindowHandler->lua, 0, 0, 0) != LUA_OK) {
printf("Function called.\n");
};

// !!!Delete the chunk here!!!

// Test again, now the result can not be OK, since the chunk was deleted!
lua_getglobal(BWindowHandler->lua, "function");
if (lua_pcall(BWindowHandler->lua, 0, 0, 0) != LUA_OK) {
printf("Function called.\n");
};


Thanks...
--
- Felipe Ferreira da Silva
You don't have to delete Lua chunks, they will be deleted by the Garbage collector as soon as they wont be used any more.
--
Thomas
Reply | Threaded
Open this post in threaded view
|

Re: [C API] Can I delete a chunk from my lua_State?

Felipe Ferreira da Silva
In reply to this post by Felipe Ferreira da Silva
I'm creating a game editor which load scripts writen by the user. But, every time the user changes the script, this script needs to be updated. To do that, my program would have to "unload"(delete) that previous script, and load it again (the updated version). I wasn't able to find any resource about it. This is something like what I'm intended to do:


lua_State *lua = luaL_newstate();
luaL_openlibs(lua);
luaL_loadfile(lua, "./start.lua");
if (lua_pcall(lua, 0, 0, 0) != LUA_OK) {
printf("Fail to load script.\n");
}

// Test if the script was loaded correctly. Result is OK.
lua_getglobal(lua, "function");
if (lua_pcall(lua, 0, 0, 0) != LUA_OK) {
printf("Function called.\n");
};

// !!!Delete the chunk here!!!

// Test again, now the result can not be OK, since the chunk was deleted!
lua_getglobal(lua, "function");
if (lua_pcall(lua, 0, 0, 0) != LUA_OK) {
printf("Function called.\n");
};

Thanks...


2014-03-03 9:06 GMT-03:00 Felipe Ferreira da Silva <[hidden email]>:
I'm creating a game editor which load scripts writen by the user. But, every time the user changes the script, this script needs to be updated. To do that, my program would have to "unload"(delete) that previous script, and load it again (the updated version). I wasn't able to find any resource about it. This is something like what I'm intended to do:


lua_State *lua = luaL_newstate();
luaL_openlibs(lua);
luaL_loadfile(lua, "./start.lua");
if (lua_pcall(lua, 0, 0, 0) != LUA_OK) {
printf("Fail to load script.\n");
}

// Test if the script was loaded correctly. Result is OK.
lua_getglobal(BWindowHandler->lua, "function");
if (lua_pcall(BWindowHandler->lua, 0, 0, 0) != LUA_OK) {
printf("Function called.\n");
};

// !!!Delete the chunk here!!!

// Test again, now the result can not be OK, since the chunk was deleted!
lua_getglobal(BWindowHandler->lua, "function");
if (lua_pcall(BWindowHandler->lua, 0, 0, 0) != LUA_OK) {
printf("Function called.\n");
};


Thanks...
--
- Felipe Ferreira da Silva



--
- Felipe Ferreira da Silva
Reply | Threaded
Open this post in threaded view
|

Re: [C API] Can I delete a chunk from my lua_State?

Kevin Martin
In reply to this post by Felipe Ferreira da Silva
On 3 Mar 2014, at 12:06, Felipe Ferreira da Silva <[hidden email]> wrote:

> // !!!Delete the chunk here!!!

The chunk itself will be subject to garbage collection after your first pcall, as there are no longer any references to it. However, it doesn't appear to be the chunk you're worried about, but rather the side effects of running the chunk, which I don't think there is a general solution (other than recreate the lua_State)

Obviously you could remove the global called function (bad name for a variable, but whatever) as follows

lua_pushnil(BWindowHandler->lua);
lua_setglobal(BWindowHandler->lua, "function");

but this won't undo any other side effects of the script, nor will it remove any other references to that value.

If re-opening the state is an option, I would consider that, else rewrite your user scripts to work more like modules and return tables of functions, rather than modifying the global state.

Thanks,
Kevin



Reply | Threaded
Open this post in threaded view
|

Re: [C API] Can I delete a chunk from my lua_State?

Kevin Martin
In reply to this post by Felipe Ferreira da Silva
On 3 Mar 2014, at 12:06, Felipe Ferreira da Silva <[hidden email]> wrote:

> // !!!Delete the chunk here!!!

The chunk itself will be subject to garbage collection after your first pcall, as there are no longer any references to it. However, it doesn't appear to be the chunk you're worried about, but rather the side effects of running the chunk, which I don't think there is a general solution (other than recreate the lua_State)

Obviously you could remove the global called function (bad name for a variable, but whatever) as follows

lua_pushnil(BWindowHandler->lua);
lua_setglobal(BWindowHandler->lua, "function");

but this won't undo any other side effects of the script, nor will it remove any other references to that value.

If re-opening the state is an option, I would consider that, else rewrite your user scripts to work more like modules and return tables of functions, rather than modifying the global state.

Thanks,
Kevin



Reply | Threaded
Open this post in threaded view
|

Re: [C API] Can I delete a chunk from my lua_State?

Felipe Ferreira da Silva
In reply to this post by Kevin Martin
Thank you, guys.
As Kevin stated, I will try recreating the lua state.

Tables of functions seems jerry-rigged, since I don't know what the user will write in the script.
Garbage collector seems nice, but I still don't fully understand it (need to read more about it).

Thank you!


2014-03-03 9:23 GMT-03:00 Kevin Martin <[hidden email]>:
On 3 Mar 2014, at 12:06, Felipe Ferreira da Silva <[hidden email]> wrote:

>       // !!!Delete the chunk here!!!

The chunk itself will be subject to garbage collection after your first pcall, as there are no longer any references to it. However, it doesn't appear to be the chunk you're worried about, but rather the side effects of running the chunk, which I don't think there is a general solution (other than recreate the lua_State)

Obviously you could remove the global called function (bad name for a variable, but whatever) as follows

lua_pushnil(BWindowHandler->lua);
lua_setglobal(BWindowHandler->lua, "function");

but this won't undo any other side effects of the script, nor will it remove any other references to that value.

If re-opening the state is an option, I would consider that, else rewrite your user scripts to work more like modules and return tables of functions, rather than modifying the global state.

Thanks,
Kevin






--
- Felipe Ferreira da Silva
Reply | Threaded
Open this post in threaded view
|

Re: [C API] Can I delete a chunk from my lua_State?

David Favro
On 03/03/2014 07:36 AM, Felipe Ferreira da Silva wrote:
> Thank you, guys.
> As Kevin stated, I will try recreating the lua state.

Another option is to load and execute the user script in an environment; the
user can then modify "global" variables as they wish, yet you can discard
the effects without destroying the entire lua_State.  You will probably want
to provide an API to the user script within its environment.