Add aggregate initialization

This commit is contained in:
Aaron Smith 2023-08-22 11:15:27 +01:00
parent be15f69522
commit 2aebaf5abb
9 changed files with 139 additions and 36 deletions

View file

@ -325,4 +325,41 @@ BENCH("cleanup gc removal", function()
vide.step(0) -- cleanup from previous benchmark
end)
do
-- the purpose of the two following benchmarks is to measure the overhead of
-- aggregate construction
BENCH("set explicit mock vector2", function()
local create = vide.create
local apply = require "src/apply"
local Vector2 = require "test/mock".Vector2
local label = create "TextLabel" {
AnchorPoint = Vector2.new(1, 1)
}
for i = 1, START(N) do
apply(label, {
AnchorPoint = Vector2.new(i, i)
})
end
end)
BENCH("set aggregate mock vector2", function()
local create = vide.create
local apply = require "src/apply"
local Vector2 = require "test/mock".Vector2
local label = create "TextLabel" {
AnchorPoint = Vector2.new(1, 1)
}
for i = 1, START(N) do
apply(label, {
AnchorPoint = { i, i }
})
end
end)
end
return nil

View file

@ -217,9 +217,28 @@ local Instance = {} :: any do
end
end
local Color3 = {} :: any do
local function table_to_proxy(t: any)
local proxy = newproxy(true)
local mt = getmetatable(proxy)
for i, v in getmetatable(t) do
mt[i] = v
end
function mt:__index(i)
return t[i]
end
function mt:__newindex(i, v)
t[i] = v
end
return proxy
end
local Color3 = { __type = "Color3" } :: any do
function Color3.new(r, g, b)
return setmetatable({ r = r, g = g, b = b}, Color3)
return table_to_proxy(setmetatable({ r = r, g = g, b = b }, Color3))
end
function Color3.__eq(a, b)
@ -227,19 +246,9 @@ local Color3 = {} :: any do
end
end
local Vector3 = {} :: any do
function Vector3.new(x, y, z)
return setmetatable({ x = x, y = y, z = z}, Vector3)
end
function Vector3.__eq(a, b)
return a.x == b.x and a.y == b.y and a.z == b.z
end
end
local Vector2 = {} :: any do
local Vector2 = { __type = "Vector2" } :: any do
function Vector2.new(x, y)
return setmetatable({ x = x, y = y }, Vector2)
return table_to_proxy(setmetatable({ x = x, y = y }, Vector2))
end
function Vector2.__eq(a, b)
@ -247,9 +256,13 @@ local Vector2 = {} :: any do
end
end
local UDim2 = {} :: any do
local UDim2 = { __type = "UDim2" } :: any do
function UDim2.new(sx, ox, sy, oy)
return table_to_proxy(setmetatable({ x = { scale = sx, offset = ox }, y = { scale = sy, offset = oy } }, UDim2))
end
function UDim2.fromScale(x, y)
return setmetatable({ x = { scale = x, offset = 0 }, y = { scale = y, offset = 0 } }, UDim2)
return table_to_proxy(setmetatable({ x = { scale = x, offset = 0 }, y = { scale = y, offset = 0 } }, UDim2))
end
function UDim2.__eq(a, b)

View file

@ -3,6 +3,7 @@ local TEST, CASE, CHECK, FINISH = testkit.test()
local mock = require "test/mock"
local Instance, Signal = mock.Instance, mock.Signal
local Vector2, UDim2 = mock.Vector2, mock.UDim2
local vide = require "src/init"
@ -598,15 +599,32 @@ TEST("create()", function()
do CASE "set nested properties"
local text = create "TextLabel" {
{ Name = "Label" },
Group = { Text = "test" }
{ Text = "test" }
}
CHECK(text.Name == "Label")
CHECK(text.Text == "test")
end
do CASE "aggregate construction"
local template = create "TextLabel" {
AnchorPoint = Vector2.new(),
Position = UDim2.new()
}
print("template", template.AnchorPoint)
local text = create(template) {
AnchorPoint = { 1, 2 },
Position = { 3, 4 }
}
CHECK(text.AnchorPoint == Vector2.new(1, 2))
CHECK(text.Position == UDim2.new(3, 4))
end
do CASE "nested precedence"
local text = create "TextLabel" {
Group = {
{
Text = "1",
{
@ -634,7 +652,7 @@ TEST("create()", function()
create "TextLabel" { Name = "E" }
}
},
Children = {
{
create "TextLabel" { Name = "F" } :: any,
{ create "TextLabel" { Name = "G" } }
}