]> git.e2factory.org Git - e2factory.git/commitdiff
class: add obj_(un)lock_rw methods for debugging
authorTobias Ulmer <tu@emlix.com>
Thu, 11 Dec 2014 19:21:49 +0000 (20:21 +0100)
committerTobias Ulmer <tu@emlix.com>
Wed, 16 Nov 2016 14:41:18 +0000 (15:41 +0100)
Signed-off-by: Tobias Ulmer <tu@emlix.com>
generic/class.lua

index 48b60e44e729ff99b5734ccd1bb1be014e89c84e..aa20ccd6d058d91a56512776104678053e785fd9 100644 (file)
@@ -159,6 +159,42 @@ function Object.static:includes(mixin)
          )
 end
 
+function Object:obj_lock_rw()
+  local mt = getmetatable(self)
+  assert(mt, "object: metatable is missing")
+  assert(mt.__newindex == nil, "object metatable: __newindex in use")
+  mt.__newindex = function (t, k, v)
+      error(string.format("attempt to set uninitialized key in (%s)[%s] to %s",
+      tostring(t), tostring(k), tostring(v)))
+  end
+
+  local idx = rawget(mt, "__index") -- upvalue
+  assert(type(idx) == "table", "object metatable: __index not a table")
+  if type(idx) == "table" then
+    rawset(mt, "__index_orig", idx)
+
+    mt.__index = function (t, k)
+      local r = idx[k]
+      if r == nil then
+        error(string.format("attempted to read nil value from (%s)[%s]",
+          tostring(idx), tostring(k)))
+      end
+      return r
+    end
+
+  end
+end
+
+function Object:obj_unlock_rw()
+  local mt = getmetatable(self)
+  assert(mt, "object lacks metatable!")
+  mt.__newindex = nil
+
+  local idx = rawget(mt, "__index_orig")
+  assert(type(idx) == "table", "object metatable: __index_orig not a table")
+  rawset(mt, "__index", idx)
+end
+
 function Object:initialize() end
 
 function Object:__tostring() return "instance of " .. tostring(self.class) end
@@ -185,3 +221,5 @@ middleclass.Object = Object
 setmetatable(middleclass, { __call = function(_, ...) return middleclass.class(...) end })
 
 return middleclass
+
+-- vim:sw=2:sts=2:et: