Skip to content

Commit

Permalink
Add missing setmetatable call in set_metatable_gc()
Browse files Browse the repository at this point in the history
  • Loading branch information
davidor committed Jun 26, 2018
1 parent 96956e0 commit e8b1bcb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
28 changes: 11 additions & 17 deletions gateway/src/apicast/gc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

local rawgetmetatable = debug.getmetatable
local getmetatable = getmetatable
local setmetatable = setmetatable
local newproxy = newproxy
local ipairs = ipairs
local pairs = pairs
local pcall = pcall
local table = table
local unpack = unpack
local error = error
local tostring = tostring

local _M = {}

Expand All @@ -26,31 +28,21 @@ local function __gc(proxy)
end

local function __tostring(proxy)
local mt = getmetatable(proxy)

if mt and mt.__tostring then
return mt.__tostring()
end
return tostring(original_table(proxy))
end

local function __call(proxy, ...)
local t = original_table(proxy)

local mt = getmetatable(proxy)

-- Try to run __call() and if it's not possible, try to run it in a way that
-- it returns a meaningful error.
if mt and mt.__call then
return mt.__call(t, ...)
local ret = { pcall(t, ...) }
local ok = table.remove(ret, 1)

if ok then
return unpack(ret)
else
local ret = { pcall(t, t, ...) }
local ok = table.remove(ret, 1)

if ok then
return unpack(ret)
else
error(ret[1], 2)
end
error(ret[1], 2)
end
end

Expand All @@ -71,6 +63,8 @@ end
-- @tparam table metatable A table that will be used as a metatable. It needs
-- to define __gc.
function _M.set_metatable_gc(t, metatable)
setmetatable(t, metatable)

-- newproxy() returns a userdata instance
local proxy = newproxy(true)

Expand Down
17 changes: 14 additions & 3 deletions spec/gc_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('GC', function()
assert.same({ 1, 2, 3 }, res)
end)

it('returns an object that respons to pairs', function()
it('returns an object that responds to pairs', function()
local test_table = { 1, 2, 3, some_key = 'some_val' }
local test_metatable = { __gc = function() end }

Expand All @@ -58,7 +58,7 @@ describe('GC', function()
assert.same({ [1] = 1, [2] = 2, [3] = 3, some_key = 'some_val' }, res)
end)

it('returns an object that respects the __call in the mt of the original table', function()
it('returns an object that respects the __call in the mt passed in the params', function()
local test_table = { 1, 2, 3 }
local test_metatable = {
__gc = function() end,
Expand All @@ -78,7 +78,7 @@ describe('GC', function()
assert.equals(3, table_with_gc(1, 2))
end)

it('returns an object that respects the __tostring in the mt of the original table', function()
it('returns an object that respects the __tostring in the mt passed in the params', function()
local test_table = { 1, 2, 3 }
local test_metatable = {
__gc = function() end,
Expand Down Expand Up @@ -112,5 +112,16 @@ describe('GC', function()

assert.same(test_metatable, getmetatable(table_with_gc))
end)

it('returns an object that respects the __index in the mt passed in the params', function()
local test_table = { 1, 2, 3 }
local test_metatable = {
__gc = function() end,
__index = { some_func = function() return 'abc' end }
}
local table_with_gc = GC.set_metatable_gc(test_table, test_metatable)

assert.equals('abc', table_with_gc:some_func())
end)
end)
end)

0 comments on commit e8b1bcb

Please sign in to comment.