| |
|
|
| 27 Apr 2015 10:37 PM |
local CreateReadOnlyTable = function(...) local Table = {...} local Data = newproxy(true) getmetatable(Data).__index = Table return Data end
CreateReadOnlyTable(6, 1, print, "hi") |
|
|
| Report Abuse |
|
|
|
| 27 Apr 2015 10:38 PM |
| Wouldn't that be a read-only userdata (not that it matters much)? |
|
|
| Report Abuse |
|
|
|
| 27 Apr 2015 10:40 PM |
-_- Yes, but it references a table. Using a userdata is the best method anyways. |
|
|
| Report Abuse |
|
|
| |
|
NotAshley
|
  |
| Joined: 16 Jan 2014 |
| Total Posts: 14257 |
|
|
| 27 Apr 2015 10:40 PM |
| local tbl = setmetatable({a="I'm read only!"}, {__metatable = "Locked"}) |
|
|
| Report Abuse |
|
|
NotAshley
|
  |
| Joined: 16 Jan 2014 |
| Total Posts: 14257 |
|
|
| 27 Apr 2015 10:41 PM |
| I think, I could be horribly wrong |
|
|
| Report Abuse |
|
|
|
| 27 Apr 2015 10:42 PM |
| You are horrible wrong. That only makes the metatable inaccessible (Which means you can't even read it) |
|
|
| Report Abuse |
|
|
| |
|
| |
|
|
| 27 Apr 2015 10:45 PM |
My arrow was actually directed at Jimmy. Though you are right, the goal was to make a read-only table. |
|
|
| Report Abuse |
|
|
|
| 27 Apr 2015 10:49 PM |
This is what I did:
function readonly(t) local proxy={} local meta={ __index=t, __newindex=function()error("Attempt to modify read-only table")end } setmetatable(proxy,meta) return proxy end |
|
|
| Report Abuse |
|
|
|
| 27 Apr 2015 10:55 PM |
It appears I forgot a few things:
getfenv().type = function(Value) if pcall(function() return Value["WhAt-ArE//tHe^OdDs_ThAt*SoMeOnE=aCcEsSeS+tHiS~kEy?"] end) then return "table" else return type(Value) end end local CreateReadOnlyTable = function(...) local Table = {["WhAt-ArE//tHe^OdDs_ThAt*SoMeOnE=aCcEsSeS+tHiS~kEy?"] = true, ...} local Data = newproxy(true) local Meta = getmetatable(Data) Meta.__index = Table Meta.__metatable = "Hai ther" return Data end |
|
|
| Report Abuse |
|
|
|
| 27 Apr 2015 10:56 PM |
Eternal, that doesn't work. For one, you forgot to lock the metatable just like me. For another, watch me change your table. rawset(EternalsReadOnlyTable, "SomeKey", "lolololol this is why you use newproxy") |
|
|
| Report Abuse |
|
|
|
| 27 Apr 2015 10:59 PM |
It still isn't the best because it doesn't prevent you from making a wrapper, but in some cases this can make the problem VERY annoying, because wrappers are just not good enough.
SomeReadOnlyTable = setmetatable({SomeKey = "lololol I just made a wrapper for your read-only table!"}, {__index = SomeReadOnlyTable})
print(SomeReadOnlyTable.SomeKey)
You can, of course, make much better wrappers than that, but you get the idea. |
|
|
| Report Abuse |
|
|