From 356ed88525c73d13533c3335b07ff4cb2a2fa71a Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Mon, 2 Mar 2015 13:51:22 +0000 Subject: [PATCH] elua: add transparent destructor support to elua objects --- src/scripts/elua/core/util.lua | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/scripts/elua/core/util.lua b/src/scripts/elua/core/util.lua index 1ef16fb..99a7203 100644 --- a/src/scripts/elua/core/util.lua +++ b/src/scripts/elua/core/util.lua @@ -12,6 +12,7 @@ local C = ffi.C local M = {} local getmetatable, setmetatable = getmetatable, setmetatable +local dgetmt = debug.getmetatable -- multiple inheritance index with depth-first search local proto_lookup = function(protos, name) @@ -46,9 +47,24 @@ local Object_MT = { end } +local obj_gc = function(px) + local obj = dgetmt(px).__obj + local dtor = obj and obj.__dtor or nil + if dtor then dtor(obj) end +end + M.Object = { + __enable_dtor = false, + __call = function(self, ...) local r = self:clone() + if self.__enable_dtor then + local px = newproxy(true) + local pxmt = dgetmt(px) + r.__gcproxy = px + pxmt.__gc = obj_gc + pxmt.__obj = r + end if self.__ctor then return r, self.__ctor(r, ...) end return r end, -- 2.7.4