lua-users home
lua-l archive

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


I'm not sure if the subject aptly describes my question.  But it'll have to do.

I've spent the last day doing google searches and perusing the mailing
list archives.  Seems what I am looking for may be common, but I can't
find exactly what I'm looking for.

What I'd like to do is have a Lua script expose a list of functions
that are callable from C/C++.  But I do not want to "execute" the Lua
script.  For example, assume I have the following script (call it
foo.lua):

function foo()
  print "foo"
end

function bar()
  print "bar"
end

And in my C/C++, I do the following:

luaL_loadfile(L, "foo.lua");

But immediately following this with something like:

lua_getglobal(L, "foo");

Returns nil, since the script has not been run yet (i.e. with a call
to lua_call/lua_pcall).  Since I am not the owner of such Lua scripts,
I only want to call specific functions, not to have the script run any
arbitrary code.  For example, if the user did something like:

function foo()
  print "foo"
end

print "I want to do whatever I want!  Who cares about the side effects!"

Now loading and running such as script has a side effect.

How can I load a Lua script and call a function without any other side
effects (such as executed code)?  Perhaps more like a compile and
load, without the execution?

Thanks,
Pete
-- 
--
"To love for the sake of being loved is human;  to love for the sake
of loving is Angelic."  -- Alphonse de Lamartine