mirror of
https://github.com/rojo-rbx/rojo.git
synced 2026-10-21 20:21:38 +00:00
Switch to userdata with locked metatable
This commit is contained in:
parent
1cd215359d
commit
ab75e2b549
1 changed files with 31 additions and 30 deletions
|
|
@ -413,39 +413,40 @@ function API.new(app)
|
|||
return ApiContext.new(baseUrl)
|
||||
end
|
||||
|
||||
local ReadOnly = setmetatable({}, {
|
||||
__index = function(_, key)
|
||||
-- Don't expose private members
|
||||
if string.find(key, "^_") then
|
||||
return nil
|
||||
end
|
||||
local ReadOnly = newproxy(true)
|
||||
local Metatable = getmetatable(ReadOnly)
|
||||
Metatable.__index = function(_, key)
|
||||
-- Don't expose private members
|
||||
if string.find(key, "^_") then
|
||||
return nil
|
||||
end
|
||||
|
||||
-- Existence check
|
||||
if Rojo._apiDescriptions[key] == nil then
|
||||
warn(string.format("Rojo.%s is not a valid API", tostring(key)))
|
||||
return nil
|
||||
end
|
||||
-- Existence check
|
||||
if Rojo._apiDescriptions[key] == nil then
|
||||
warn(string.format("Rojo.%s is not a valid API", tostring(key)))
|
||||
return nil
|
||||
end
|
||||
|
||||
-- Permissions check
|
||||
local granted = Rojo:_permissionCheck(key)
|
||||
if not granted then
|
||||
error(
|
||||
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.',
|
||||
key,
|
||||
key
|
||||
),
|
||||
2
|
||||
)
|
||||
end
|
||||
-- Permissions check
|
||||
local granted = Rojo:_permissionCheck(key)
|
||||
if not granted then
|
||||
error(
|
||||
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.',
|
||||
key,
|
||||
key
|
||||
),
|
||||
2
|
||||
)
|
||||
end
|
||||
|
||||
return Rojo[key]
|
||||
end,
|
||||
__newindex = function(_, key, value)
|
||||
error(string.format("Attempted to set Rojo.%s to %q but it's a read-only value", key, value), 2)
|
||||
return
|
||||
end,
|
||||
})
|
||||
return Rojo[key]
|
||||
end
|
||||
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)
|
||||
return
|
||||
end
|
||||
Metatable.__metatable = "The metatable of the Rojo API is locked"
|
||||
|
||||
return Rojo, ReadOnly
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue