Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
wrapping C++ classes with Lua
-- ============ Create new class
SpecialEdit = {}
setmetatable(SpecialEdit, mimas.LineEdit_mt)
-- calling SpecialEdit() should do the following things
-- 1. create userdata
-- 2. set new table as uservale ---> self
-- 3. create new lua thread and install "self" on the stack
-- 4. set access to userdata from self ---> self.super = userdata
-- 5. set "SpecialEdit" as metatable for "self" ---> setmetatable(self, SpecialEdit)
-- 6. return "self"
-- This is a callback called from the C side. The call goes as follows
-- 1. the object's lua_thread's stack contains "self", ready to be used.
-- 2. call self.textChanged(self, text)
function SpecialEdit:textChanged(text)
-- just to show that self is accessed by the callback with custom values ("owner").
self.owner:doSomething()
-- call a native method
-- "hide" is found in mimas.LineEdit_mt ---> LineEdit_mt.hide(self)
-- the userdata is found in self ---> LineEdit *c_obj = self.super // C pseudo-code
-- call the class method ---> c_obj->hide()
self:hide(nil)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment