lua-users home
lua-l archive

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


I'm trying to write some C code that executes user-supplied chunks. I
want to use the same environment between chunks, so that (for example)

my_execute_chunk(L, "function f() print(\"Hello, world\") end")
my_execute_chunk(L, "f()")

should print "Hello, world".

At the moment, it doesn't, because each chunk is evaluated with a
local scope, so that the f defined in the first chunk is lost, and f
in the second chunk is nil.

I've tried simple things like "function _G.f() ..." but that doesn't
seem to help here. I expect I'm doing something silly, but I don't see
what. Can anyone help?

(BTW, my_execute_chunk is pretty trivial, just a sequence of
luaL_loadbuffer, then lua_pcall)

Thanks,
Paul.