Skip to content

Commit

Permalink
Fix merging bugs with more than two tables
Browse files Browse the repository at this point in the history
  • Loading branch information
dphfox committed Aug 21, 2024
1 parent ebf1874 commit 474110b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
10 changes: 9 additions & 1 deletion src/Memory/deriveScopeImpl.luau
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ local function deriveScopeImpl<T>(
local metatable = getmetatable(existing)
if methods ~= nil then
metatable = table.clone(metatable)
metatable.__index = merge("first", table.clone(metatable.__index), methods, ...)
metatable.__index = merge(
true, {},
metatable.__index,
merge(
false, {},
methods,
...
)
)
end
local scope = setmetatable(
scopePool.reuseAny() :: any or {},
Expand Down
2 changes: 1 addition & 1 deletion src/Memory/scoped.luau
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ local function scoped(
): any
local scope = setmetatable(
scopePool.reuseAny() :: any or {},
{__index = merge("none", {}, ...)}
{__index = merge(false, {}, ...)}
) :: any
ExternalDebug.trackScope(scope)
return scope
Expand Down
20 changes: 11 additions & 9 deletions src/Utility/merge.luau
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,24 @@ local Package = script.Parent.Parent
local External = require(Package.External)

local function merge(
overwrite: "none" | "first" | "all",
overwrite: boolean,
into: {[unknown]: unknown},
from: {[unknown]: unknown}?,
...: {[unknown]: unknown}
): {[unknown]: unknown}
if from == nil then
local fromTables = {...}
if #fromTables < 1 then
return into
else
for key, value in from do
if into[key] == nil then
into[key] = value
elseif overwrite == "none" then
External.logError("mergeConflict", nil, tostring(key))
for _, fromTable in fromTables do
for key, value in fromTable do
if into[key] == nil then
into[key] = value
elseif not overwrite then
External.logError("mergeConflict", nil, tostring(key))
end
end
end
return merge(if overwrite == "first" then "none" else overwrite, into, ...)
return into
end
end

Expand Down

0 comments on commit 474110b

Please sign in to comment.