lua-users home
lua-l archive

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


2009/8/4 Evan Burkitt <evanb@edulinksys.com>:
> 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?

You can pass a value to the script when you load it from your app, and
check for that parameter inside the script. If it's present you're
within your app (or another app (like a unit test) that wants your
script to believe it's in your app). If the parameter is absent you
can assume your script was run standalone (or with dofile).