mirror of
https://github.com/centau/vide.git
synced 2026-08-20 14:41:37 +00:00
fix mock instances Parent property not being a proxy
This commit is contained in:
parent
0a21ae8a67
commit
c7163eeb5c
1 changed files with 19 additions and 16 deletions
|
|
@ -86,6 +86,9 @@ local Instance = {} :: any do
|
||||||
local proxies = {} :: { [Data]: userdata? }
|
local proxies = {} :: { [Data]: userdata? }
|
||||||
setmetatable(proxies :: any, { __mode = "v" })
|
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 get_data(userdata: userdata): Data
|
||||||
local function f(userdata: userdata): ProxyMT
|
local function f(userdata: userdata): ProxyMT
|
||||||
return getmetatable(userdata :: any)
|
return getmetatable(userdata :: any)
|
||||||
|
|
@ -94,6 +97,19 @@ local Instance = {} :: any do
|
||||||
return f(userdata).data
|
return f(userdata).data
|
||||||
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
|
||||||
|
|
||||||
local function is_instance(value: unknown): boolean
|
local function is_instance(value: unknown): boolean
|
||||||
local mt = getmetatable(value :: any)
|
local mt = getmetatable(value :: any)
|
||||||
return mt and mt.data and mt.data.type == "Instance"
|
return mt and mt.data and mt.data.type == "Instance"
|
||||||
|
|
@ -101,16 +117,16 @@ local Instance = {} :: any do
|
||||||
|
|
||||||
local methods = {}
|
local methods = {}
|
||||||
|
|
||||||
local function __index(userdata: userdata, property: string): ()
|
__index = function(userdata: userdata, property: string): ()
|
||||||
local data = get_data(userdata)
|
local data = get_data(userdata)
|
||||||
return if methods[property] then methods[property]
|
return if methods[property] then methods[property]
|
||||||
elseif property == "Name" then data.name
|
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
|
elseif property == "Destroying" then data.destroying
|
||||||
else data.properties[property]
|
else data.properties[property]
|
||||||
end
|
end
|
||||||
|
|
||||||
local function __newindex(userdata: userdata, property: string, value: unknown)
|
__newindex = function(userdata: userdata, property: string, value: unknown)
|
||||||
local data = get_data(userdata)
|
local data = get_data(userdata)
|
||||||
if property == "Name" then
|
if property == "Name" then
|
||||||
if type(value) ~= "string" then error("name must be a string", 2) end
|
if type(value) ~= "string" then error("name must be a string", 2) end
|
||||||
|
|
@ -135,19 +151,6 @@ local Instance = {} :: any do
|
||||||
end
|
end
|
||||||
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
|
function Instance.new(class: string): Instance
|
||||||
local data = {
|
local data = {
|
||||||
name = "UNNAMED",
|
name = "UNNAMED",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue