MCPcopy Create free account
hub / github.com/ThePhD/sol2 / main

Function main

examples/source/stack_aligned_function.cpp:5–34  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3
4
5int 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
17 // working 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_ASSERT(result == 22);
30 SOL_ASSERT(
31 lua.stack_top() == 0); // stack is empty/balanced
32
33 return 0;
34}

Callers

nothing calls this directly

Calls 5

scriptMethod · 0.80
stack_topMethod · 0.80
stack_countClass · 0.50
pushMethod · 0.45
lua_stateMethod · 0.45

Tested by

no test coverage detected