Jump to content




Read-Only Tables?


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

#1 tenshae

  • Members
  • 66 posts

Posted 16 September 2014 - 03:05 AM

I've seen a few examples of this that are either dated or just completely untested and absolutely none that I've seen even work a bit (at least with this version of CC). I was wondering, and yes I have searched on google, how you would do read-only tables in Lua.
I want to guard the OS and FS apis from userspace code, so that they can't modify it and break some things/break out of the sandbox. Could anyone help?

#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 16 September 2014 - 03:21 AM

You'll want to look at metatables for this. Here's a tutorial on how to use metatables.

#3 natedogith1

  • Members
  • 110 posts

Posted 16 September 2014 - 03:22 AM

function readOnly(tbl)
  return setmetatable({}, __index = tbl, __newindex =
	function(self,key,value)
	  error("not modifyable")
	end,__metatable={})
end
'__index' defines where/how to look up values
'__newindex' defines where/how to set values
'__metatable' prevents me from using getmetatable and modifying it that way
if you're interested in more about metatables, you might like to look at http://www.lua.org/m...manual.html#2.8

There are a few issues with this though, like the fact that 'pairs' doesn't work, though it does do a better job of read-only than the CC's OS

#4 tenshae

  • Members
  • 66 posts

Posted 16 September 2014 - 03:34 AM

View Postnatedogith1, on 16 September 2014 - 03:22 AM, said:

--snippeth--
I guess I should try to override setmetatable so that people can't "unreadonly-ize" the tables, right?
Is there anything else I should watch out for?

#5 natedogith1

  • Members
  • 110 posts

Posted 16 September 2014 - 03:46 AM

View Postashnwill, on 16 September 2014 - 03:34 AM, said:

View Postnatedogith1, on 16 September 2014 - 03:22 AM, said:

--snippeth--
I guess I should try to override setmetatable so that people can't "unreadonly-ize" the tables, right?
Is there anything else I should watch out for?
Actually, since the metatable has a '__metatable' field, setmetatable will throw an error; however you should maybe watch out for 'rawset'.

#6 TsarN

  • Members
  • 29 posts

Posted 24 January 2015 - 08:55 PM

If you want to also block rawset here the code to put into "startup" (I'm using this in UberOS):
local absoluteReadOnly = {}
function applyreadonly(table)
  local tmp = {}
  setmetatable(tmp, {
	__index = table,
	__newindex = function(table, key, value)
	  error("Attempt to modify read-only table")
	end,
	__metatable = false
  })
  absoluteReadOnly[#absoluteReadOnly + 1] = tmp
  return tmp
end
local oldrawset = rawset
rawset = function(table, index, value)
  for i = 1, #absoluteReadOnly do
	if (table == absoluteReadOnly[i]) or (index == absoluteReadOnly[i]) then
	  error("Attempt to modify read-only table")
	  return
	end
  end
  oldrawset(table, index, value)
end

Then, to make table "a" read-only, do the following:
a = applyreadonly(a)

Edited by TsarN, 26 January 2015 - 05:35 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users