Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pattern cache key collision bug #21

Merged
merged 1 commit into from
Sep 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/API.lua
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,15 @@ do
end


-- Give a unique string for a combination of two patterns.
local function nameify(a, b)
return tostring(a)..tostring(b)
return ('%s:%s'):format(a.id, b.id)
end

-- pt*pt
local
function choice (a, b)
local name = tostring(a)..tostring(b)
local name = nameify(a, b)
local ch = Builder.ptcache.choice[name]
if not ch then
ch = factorize_choice(a, b) or constructors.binary("choice", a, b)
Expand All @@ -238,7 +239,7 @@ end

local
function sequence (a, b)
local name = tostring(a)..tostring(b)
local name = nameify(a, b)
local seq = Builder.ptcache.sequence[name]
if not seq then
seq = factorize_sequence(a, b) or constructors.binary("sequence", a, b)
Expand Down
18 changes: 12 additions & 6 deletions src/constructors.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ local newpattern, pattmt
-- This deals with the Lua 5.1/5.2 compatibility, and restricted
-- environements without access to newproxy and/or debug.setmetatable.

-- Augment a pattern with unique identifier.
local next_pattern_id = 1
if compat.proxies and not compat.lua52_len then
-- Lua 5.1 / LuaJIT without compat.
local proxycache = weakkey{}
Expand All @@ -109,6 +111,8 @@ if compat.proxies and not compat.lua52_len then
local pt = newproxy(baseproxy)
setmetatable(cons, __index_LL)
proxycache[pt]=cons
pt.id = "__ptid" .. next_pattern_id
next_pattern_id = next_pattern_id + 1
return pt
end
else
Expand All @@ -122,6 +126,8 @@ else
function LL.getdirect (p) return p end

function newpattern(pt)
pt.id = "__ptid" .. next_pattern_id
next_pattern_id = next_pattern_id + 1
return setmetatable(pt,LL)
end
end
Expand Down Expand Up @@ -230,13 +236,13 @@ end
constructors["subpt"] = function(typ, pt)
-- [[DP]]print("CONS: ", typ, pt, aux)
local cache = ptcache[typ]
if not cache[pt] then
cache[pt] = newpattern{
if not cache[pt.id] then
cache[pt.id] = newpattern{
pkind = typ,
pattern = pt
}
end
return cache[pt]
return cache[pt.id]
end

constructors["both"] = function(typ, pt, aux)
Expand All @@ -246,15 +252,15 @@ constructors["both"] = function(typ, pt, aux)
ptcache[typ][aux] = weakval{}
cache = ptcache[typ][aux]
end
if not cache[pt] then
cache[pt] = newpattern{
if not cache[pt.id] then
cache[pt.id] = newpattern{
pkind = typ,
pattern = pt,
aux = aux,
cache = cache -- needed to keep the cache as long as the pattern exists.
}
end
return cache[pt]
return cache[pt.id]
end

constructors["binary"] = function(typ, a, b)
Expand Down