|
| 03 Feb 2015 09:46 PM |
Too lazy to create em yourself? Here you go:
do
local oldrawset = rawset function rawset(t, k, v) local meta = getmetatable(t) if type(meta) == "table" and meta.__rawset then return meta.__rawset(t, k, v) else return oldrawset(t, k, v) end end
local oldrawget = rawget function rawget(t, k) local meta = getmetatable(t) if type(meta) == "table" and meta.__rawget then return meta.__rawget(t, k) else return oldrawget(t, k) end end
local oldpairs = pairs function pairs(t) local meta = getmetatable(t) if type(meta) == "table" and meta.__pairs then return meta.__pairs(t) else return oldpairs(t) end end
local oldipairs = ipairs function ipairs(t) local meta = getmetatable(t) if type(meta) == "table" and meta.__ipairs then return meta.__ipairs(t) else return oldipairs(t) end end
local oldnext = next function next(t, i, arg) local meta = getmetatable(t) if type(meta) == "table" and meta.__next then return meta.__next(t, i, arg) else return oldnext(t, i, arg) end end
local oldprint = print function print(...) local realargs = {} for I,v in pairs({...}) do local meta = getmetatable(v) if type(meta) == "table" and meta.__print then table.insert(realargs, meta.__print) else table.insert(realargs, v) end end return print(unpack(realargs)) end
end
Before this post I had 6666 post count!
--You can easily extend this for other functions too. |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 03 Feb 2015 09:48 PM |
| print already calls __tostring :( |
|
|
| Report Abuse |
|
|
|
| 03 Feb 2015 09:49 PM |
| @cnt But what if I wanted the tostring function to return something, but print to print something different? |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 03 Feb 2015 09:56 PM |
| how the hell do I have 29K posts and still this young |
|
|
| Report Abuse |
|
|
|
| 03 Feb 2015 09:57 PM |
Join Date: 2008
That's how. |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 03 Feb 2015 10:04 PM |
| I started foruming like, 2013 I think |
|
|
| Report Abuse |
|
|
|
| 03 Feb 2015 10:10 PM |
| Searches for posts on SH earlier than 2013 made by you* |
|
|
| Report Abuse |
|
|
vacharya
|
  |
| Joined: 06 Jan 2011 |
| Total Posts: 511 |
|
| |
|
| |
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
| |
|
128GB
|
  |
| Joined: 17 Apr 2014 |
| Total Posts: 8056 |
|
| |
|
Voiliax
|
  |
| Joined: 05 Nov 2009 |
| Total Posts: 15554 |
|
|
| 03 Feb 2015 11:03 PM |
Just wanted to point out that your way to check for a locked metatable is not the best way because you can set a __metatbale metamethod to any other table.
The right way is by trying to set it again with setmetatable and check if it errors or not using pcall.
if pcall(setmetatable, tab, getmetatable(tab)) then -- not a locked metatable end |
|
|
| Report Abuse |
|
|
| |
|
|
| 04 Feb 2015 03:09 PM |
| @Voil I know that, but if they want to send us to another table to proxy a metatable, so be it, I'll use whatever's in that. Just like how they expect in their code. |
|
|
| Report Abuse |
|
|
|
| 06 Feb 2015 06:53 AM |
| Bump, because I know people who wouldn't know how to change the behaviour of rawset/rawget |
|
|
| Report Abuse |
|
|
Voiliax
|
  |
| Joined: 05 Nov 2009 |
| Total Posts: 15554 |
|
|
| 06 Feb 2015 06:59 AM |
| Yeah it won't do much difference since it's just a custom metamethod but that was just for knowledge. |
|
|
| Report Abuse |
|
|