Switch to userdata with locked metatable

This commit is contained in:
boatbomber 2024-02-12 22:28:30 -08:00
parent 1cd215359d
commit ab75e2b549

View file

@ -413,8 +413,9 @@ function API.new(app)
return ApiContext.new(baseUrl) return ApiContext.new(baseUrl)
end end
local ReadOnly = setmetatable({}, { local ReadOnly = newproxy(true)
__index = function(_, key) local Metatable = getmetatable(ReadOnly)
Metatable.__index = function(_, key)
-- Don't expose private members -- Don't expose private members
if string.find(key, "^_") then if string.find(key, "^_") then
return nil return nil
@ -440,12 +441,12 @@ function API.new(app)
end end
return Rojo[key] return Rojo[key]
end, end
__newindex = function(_, key, value) Metatable.__newindex = function(_, key, value)
error(string.format("Attempted to set Rojo.%s to %q but it's a read-only value", key, value), 2) error(string.format("Attempted to set Rojo.%s to %q but it's a read-only value", key, value), 2)
return return
end, end
}) Metatable.__metatable = "The metatable of the Rojo API is locked"
return Rojo, ReadOnly return Rojo, ReadOnly
end end