1 #define SOL_ALL_SAFETIES_ON 1
2 #include <sol/sol.hpp>
3
4
main(int,char * [])5 int main(int, char*[]) {
6 sol::state lua;
7 lua.script("function func (a, b) return (a + b) * 2 end");
8
9 sol::reference func_ref = lua["func"];
10
11 // for some reason, you need to use the low-level API
12 func_ref.push(); // function on stack now
13
14 // maybe this is in a lua_CFunction you bind,
15 // or maybe you're trying to work with a pre-existing system
16 // maybe you've used a custom lua_load call, or you're working
17 // with state_view's load(lua_Reader, ...) call...
18 // here's a little bit of how you can work with the stack
19 lua_State* L = lua.lua_state();
20 sol::stack_aligned_unsafe_function func(L, -1);
21 lua_pushinteger(L, 5); // argument 1, using plain API
22 lua_pushinteger(L, 6); // argument 2
23
24 // take 2 arguments from the top,
25 // and use "stack_aligned_function" to call
26 int result = func(sol::stack_count(2));
27
28 // make sure everything is clean
29 sol_c_assert(result == 22);
30 sol_c_assert(lua.stack_top() == 0); // stack is empty/balanced
31
32 return 0;
33 }
34