Jump to content




[LUA MAGIC] Locked tables


  • You cannot reply to this topic
23 replies to this topic

#21 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 28 September 2014 - 10:32 AM

It would work way better to do this using upvalues!
local tbl = {
   "some table here"
}

local proxy( tbl )
	return setmetatable( {}, {
		__index = functon(t, k ,v)
			return tbl[k]
		end
	} )
end

local passThisAroundAsReadOnly = proxy(tbl)
This way you dont have to bother about rawget or have mess with ID's. Of course this snippet is very primitive, but you should know how to do it properly, this is just the concept

Edited by Engineer, 28 September 2014 - 10:55 AM.


#22 sci4me

  • Members
  • 225 posts
  • LocationEarth

Posted 04 October 2014 - 10:45 PM

View PostEngineer, on 28 September 2014 - 10:32 AM, said:

It would work way better to do this using upvalues!
local tbl = {
   "some table here"
}

local proxy( tbl )
	return setmetatable( {}, {
		__index = functon(t, k ,v)
			return tbl[k]
		end
	} )
end

local passThisAroundAsReadOnly = proxy(tbl)
This way you dont have to bother about rawget or have mess with ID's. Of course this snippet is very primitive, but you should know how to do it properly, this is just the concept

Aah, that is actually a great idea... :P

#23 ElvishJerricco

  • Members
  • 803 posts

Posted 05 October 2014 - 06:06 AM

View Postsci4me, on 04 October 2014 - 10:45 PM, said:

View PostEngineer, on 28 September 2014 - 10:32 AM, said:

It would work way better to do this using upvalues!
local tbl = {
   "some table here"
}

local proxy( tbl )
	return setmetatable( {}, {
		__index = functon(t, k ,v)
			return tbl[k]
		end
	} )
end

local passThisAroundAsReadOnly = proxy(tbl)
This way you dont have to bother about rawget or have mess with ID's. Of course this snippet is very primitive, but you should know how to do it properly, this is just the concept

Aah, that is actually a great idea... :P

Except the returned table and its metatable can still be modified

#24 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 05 October 2014 - 11:23 AM

View PostElvishJerricco, on 05 October 2014 - 06:06 AM, said:

Except the returned table and its metatable can still be modified
That is not the point, because if someone wants to break it, go ahead. They wont have an advantage of it, because if you play it smart you only have access to the table, hence only you can modify it.
You can add to the metatable __newindex = function ( ... ) end, so it technically never is modified. And if someone is too lazy to write that just like me, the original table does not get modified at all...

And as I mentioned in my post, the snippet is primitive, it is up to you how extensive you want to protect it





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users