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
|
|
|
|
2021-03-06 23:14:48 +08:00
|
|
|
lua.set_function(
|
|
|
|
"bark", [](sol::this_state s, int a, int b) {
|
|
|
|
lua_State* L = s; // current state
|
|
|
|
return a + b + lua_gettop(L);
|
|
|
|
});
|
2021-03-06 14:03:23 +08:00
|
|
|
|
2021-03-06 23:14:48 +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 23:14:48 +08:00
|
|
|
lua.set_function(
|
|
|
|
"bark", [](int a, int b, sol::this_state s) {
|
|
|
|
lua_State* L = s; // current state
|
|
|
|
return a + b + lua_gettop(L);
|
|
|
|
});
|
2018-03-17 04:47:09 +08:00
|
|
|
|
|
|
|
lua.script("second = bark(2, 2)"); // only takes 2 arguments
|
|
|
|
int first = lua["first"];
|
2022-09-28 13:56:26 +08:00
|
|
|
SOL_ASSERT(first == 6);
|
2018-03-17 04:47:09 +08:00
|
|
|
int second = lua["second"];
|
2022-09-28 13:56:26 +08:00
|
|
|
SOL_ASSERT(second == 6);
|
2018-03-17 04:47:09 +08:00
|
|
|
|
|
|
|
return 0;
|
2022-09-28 13:56:26 +08:00
|
|
|
}
|