lua-users home
lua-l archive

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


I have an application that calls functions in Lua scripts from C (C++, actually). My design expects a certain function, "filter()", to exist in the scripts it uses. filter() typically calls other functions in the script as part of its behavior, but the C/C++ code doesn't know of or use these other functions.

The scripts also contain a section of code that calls filter(). This exists so the script as a whole can be run stand-alone via the lua command line.

Here's a minimal illustration of what my lua script looks like:
--internal functions:
function internal1() end
function internal2() end

--function called by C/C++:
function filter(a,b)
  internal1()
  internal2()
end

--code to allow the script to run stand-alone:
a = 1
b = 2
filter(a,b)

When I load and compile the script via luaL_loadfile() and lua_pcall(), the code at the end begins to execute when I call lua_pcall().

My question is, what is the best way to process the script file so that my C/C++ code can call filter() yet the entire script does get executed in the process?

-evan