lua-users home
lua-l archive

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

2008/7/10 Paul Moore <>:
> 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)

I think you're doing something non-trivial in your my_execute_chunk
because what you're trying to do should work as is. Just to be sure,
when you say that it doesn't print "Hello World", you mean that you
get an error saying that you tried to call a nil value, right ? Anyway
whether you get an error or just no output, the problem is probably in
the code you didn't give us.