| 9 | // low-level operation! |
| 10 | |
| 11 | int main() { |
| 12 | struct thing { |
| 13 | |
| 14 | thing(sol::this_state ts) { |
| 15 | lua_State* L = ts; |
| 16 | // references the object that called this function |
| 17 | // in constructors: |
| 18 | sol::stack_object selfobj(L, 1); |
| 19 | |
| 20 | // definitely the same |
| 21 | thing& self = selfobj.as<thing>(); |
| 22 | SOL_ASSERT(&self == this); |
| 23 | } |
| 24 | |
| 25 | void func(sol::this_state ts) const { |
| 26 | lua_State* L = ts; |
| 27 | // references the object that called this function |
| 28 | // in regular member functions: |
| 29 | sol::stack_object selfobj(L, 1); |
| 30 | // "1" is the bottom of the Lua stack |
| 31 | // 2 is one up, so on and so forth... |
| 32 | thing& self = selfobj.as<thing>(); |
| 33 | |
| 34 | // definitely the same |
| 35 | SOL_ASSERT(&self == this); |
| 36 | } |
| 37 | }; |
| 38 | |
| 39 | sol::state lua; |
| 40 | lua.open_libraries(sol::lib::base); |
| 41 | |
| 42 | lua.new_usertype<thing>("thing", |
| 43 | sol::constructors<thing(sol::this_state)>(), |
| 44 | "func", |
| 45 | &thing::func); |
| 46 | |
| 47 | lua.script(R"( |
| 48 | obj = thing.new() |
| 49 | obj:func() |
| 50 | )"); |
| 51 | |
| 52 | return 0; |
| 53 | } |
nothing calls this directly
no test coverage detected