- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
def __gc(self, state):
userdata = self.lib.luaL_testudata(self.state, 1, b'python.object')
if userdata:
obj_ptr = (c.cast(userdata, c.POINTER(c.py_object))).contents
if obj_ptr:
c.pythonapi.Py_DecRef(obj_ptr)
return 0
def push_object(self, obj):
userdata = self.lib.lua_newuserdata(self.state, c.sizeof(c.py_object))
(c.cast(userdata, c.POINTER(c.py_object)))[0] = c.py_object()
if self.lib.luaL_newmetatable(self.state, b'python.object'):
self.gc_thunk = c.CFUNCTYPE(c.c_int, c.c_void_p)(self.__gc)
self.lib.lua_pushcclosure(self.state, self.gc_thunk, 0)
self.lib.lua_setfield(self.state, -2, b'__gc')
self.lib.lua_pushstring(self.state, b'protected')
self.lib.lua_setfield(self.state, -2, b'__metatable')
self.lib.lua_setmetatable(self.state, -2)
obj_ptr = c.py_object(obj)
c.pythonapi.Py_IncRef(obj_ptr)
(c.cast(userdata, c.POINTER(c.py_object)))[0] = obj_ptr