2019-05-22 07:17:31 +08:00
|
|
|
#define SOL_ALL_SAFETIES_ON 1
|
2018-09-28 13:27:38 +08:00
|
|
|
#include <sol/sol.hpp>
|
2018-03-17 04:47:09 +08:00
|
|
|
|
|
|
|
|
2021-03-06 14:03:23 +08:00
|
|
|
int main() {
|
2018-03-17 04:47:09 +08:00
|
|
|
sol::state lua;
|
2021-03-06 14:03:23 +08:00
|
|
|
|
|
|
|
lua.set_function("bark", [](sol::this_state s, int a, int b) {
|
2018-03-17 04:47:09 +08:00
|
|
|
lua_State* L = s; // current state
|
|
|
|
return a + b + lua_gettop(L);
|
|
|
|
});
|
2021-03-06 14:03:23 +08:00
|
|
|
|
2018-03-17 04:47:09 +08:00
|
|
|
lua.script("first = bark(2, 2)"); // only takes 2 arguments, NOT 3
|
2021-03-06 14:03:23 +08:00
|
|
|
|
2018-03-17 04:47:09 +08:00
|
|
|
// Can be at the end, too, or in the middle: doesn't matter
|
2021-03-06 14:03:23 +08:00
|
|
|
lua.set_function("bark", [](int a, int b, sol::this_state s) {
|
2018-03-17 04:47:09 +08:00
|
|
|
lua_State* L = s; // current state
|
|
|
|
return a + b + lua_gettop(L);
|
|
|
|
});
|
|
|
|
|
|
|
|
lua.script("second = bark(2, 2)"); // only takes 2 arguments
|
|
|
|
int first = lua["first"];
|
2021-03-06 14:03:23 +08:00
|
|
|
sol_c_assert(first == 6);
|
2018-03-17 04:47:09 +08:00
|
|
|
int second = lua["second"];
|
2021-03-06 14:03:23 +08:00
|
|
|
sol_c_assert(second == 6);
|
2018-03-17 04:47:09 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|