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-16 05:16:28 +08:00
|
|
|
|
|
|
|
|
2021-03-06 14:03:23 +08:00
|
|
|
int main(int, char*[]) {
|
2018-03-16 05:16:28 +08:00
|
|
|
|
|
|
|
struct bark {
|
|
|
|
int operator()(int x) {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
sol::state lua;
|
|
|
|
lua.open_libraries(sol::lib::base);
|
|
|
|
|
2021-03-06 23:14:48 +08:00
|
|
|
lua.new_usertype<bark>("bark",
|
|
|
|
sol::meta_function::call_function,
|
|
|
|
&bark::operator());
|
2018-03-16 05:16:28 +08:00
|
|
|
|
|
|
|
bark b;
|
|
|
|
lua.set("b", &b);
|
|
|
|
|
2021-03-06 14:03:23 +08:00
|
|
|
sol::table b_as_table = lua["b"];
|
2018-03-16 05:16:28 +08:00
|
|
|
sol::table b_metatable = b_as_table[sol::metatable_key];
|
|
|
|
sol::function b_call = b_metatable["__call"];
|
|
|
|
sol::function b_as_function = lua["b"];
|
|
|
|
|
|
|
|
int result1 = b_as_function(1);
|
|
|
|
// pass 'self' directly to argument
|
|
|
|
int result2 = b_call(b, 1);
|
2022-09-28 13:56:26 +08:00
|
|
|
SOL_ASSERT(result1 == result2);
|
|
|
|
SOL_ASSERT(result1 == 1);
|
|
|
|
SOL_ASSERT(result2 == 1);
|
2018-03-16 05:16:28 +08:00
|
|
|
}
|