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,39 +413,40 @@ 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)
-- Don't expose private members Metatable.__index = function(_, key)
if string.find(key, "^_") then -- Don't expose private members
return nil if string.find(key, "^_") then
end return nil
end
-- Existence check -- Existence check
if Rojo._apiDescriptions[key] == nil then if Rojo._apiDescriptions[key] == nil then
warn(string.format("Rojo.%s is not a valid API", tostring(key))) warn(string.format("Rojo.%s is not a valid API", tostring(key)))
return nil return nil
end end
-- Permissions check -- Permissions check
local granted = Rojo:_permissionCheck(key) local granted = Rojo:_permissionCheck(key)
if not granted then if not granted then
error( error(
string.format( string.format(
'Attempted to read Rojo.%s, but the plugin does not have permission to do so.\nPlease first use Rojo:RequestAccess({ "%s" }) to gain access to this API.', 'Attempted to read Rojo.%s, but the plugin does not have permission to do so.\nPlease first use Rojo:RequestAccess({ "%s" }) to gain access to this API.',
key, key,
key key
), ),
2 2
) )
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