mirror of
https://github.com/centau/vide.git
synced 2026-08-20 14:41:37 +00:00
Support nesting Parent (#48)
* allow nesting parent property * update changelog * fix mock instances Parent property not being a proxy * add tests Closes #47
This commit is contained in:
parent
7064489a36
commit
37e8e05206
4 changed files with 39 additions and 21 deletions
|
|
@ -86,6 +86,9 @@ local Instance = {} :: any do
|
|||
local proxies = {} :: { [Data]: userdata? }
|
||||
setmetatable(proxies :: any, { __mode = "v" })
|
||||
|
||||
-- allocate variables for the metamethods as __index and get_proxy cross refrences each other
|
||||
local __index, __newindex
|
||||
|
||||
local function get_data(userdata: userdata): Data
|
||||
local function f(userdata: userdata): ProxyMT
|
||||
return getmetatable(userdata :: any)
|
||||
|
|
@ -94,6 +97,19 @@ local Instance = {} :: any do
|
|||
return f(userdata).data
|
||||
end
|
||||
|
||||
local function get_proxy(data: Data): userdata
|
||||
return proxies[data] or (function()
|
||||
local userdata = newproxy(true)
|
||||
local proxy = getmetatable(userdata)
|
||||
proxy.proxy = userdata
|
||||
proxy.data = data
|
||||
proxy.__index = __index
|
||||
proxy.__newindex = __newindex
|
||||
proxies[data] = userdata
|
||||
return userdata
|
||||
end)()
|
||||
end
|
||||
|
||||
local function is_instance(value: unknown): boolean
|
||||
local mt = getmetatable(value :: any)
|
||||
return mt and mt.data and mt.data.type == "Instance"
|
||||
|
|
@ -101,16 +117,16 @@ local Instance = {} :: any do
|
|||
|
||||
local methods = {}
|
||||
|
||||
local function __index(userdata: userdata, property: string): ()
|
||||
__index = function(userdata: userdata, property: string): ()
|
||||
local data = get_data(userdata)
|
||||
return if methods[property] then methods[property]
|
||||
elseif property == "Name" then data.name
|
||||
elseif property == "Parent" then data.parent
|
||||
elseif property == "Parent" then (data.parent and get_proxy(data.parent))
|
||||
elseif property == "Destroying" then data.destroying
|
||||
else data.properties[property]
|
||||
end
|
||||
|
||||
local function __newindex(userdata: userdata, property: string, value: unknown)
|
||||
__newindex = function(userdata: userdata, property: string, value: unknown)
|
||||
local data = get_data(userdata)
|
||||
if property == "Name" then
|
||||
if type(value) ~= "string" then error("name must be a string", 2) end
|
||||
|
|
@ -135,19 +151,6 @@ local Instance = {} :: any do
|
|||
end
|
||||
end
|
||||
|
||||
local function get_proxy(data: Data): userdata
|
||||
return proxies[data] or (function()
|
||||
local userdata = newproxy(true)
|
||||
local proxy = getmetatable(userdata)
|
||||
proxy.proxy = userdata
|
||||
proxy.data = data
|
||||
proxy.__index = __index
|
||||
proxy.__newindex = __newindex
|
||||
proxies[data] = userdata
|
||||
return userdata
|
||||
end)()
|
||||
end
|
||||
|
||||
function Instance.new(class: string): Instance
|
||||
local data = {
|
||||
name = "UNNAMED",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue