Skip to content

Commit e8b1bcb

Browse files
committed
Add missing setmetatable call in set_metatable_gc()
1 parent 96956e0 commit e8b1bcb

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

gateway/src/apicast/gc.lua

+11-17
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
local rawgetmetatable = debug.getmetatable
66
local getmetatable = getmetatable
7+
local setmetatable = setmetatable
78
local newproxy = newproxy
89
local ipairs = ipairs
910
local pairs = pairs
1011
local pcall = pcall
1112
local table = table
1213
local unpack = unpack
1314
local error = error
15+
local tostring = tostring
1416

1517
local _M = {}
1618

@@ -26,31 +28,21 @@ local function __gc(proxy)
2628
end
2729

2830
local function __tostring(proxy)
29-
local mt = getmetatable(proxy)
30-
31-
if mt and mt.__tostring then
32-
return mt.__tostring()
33-
end
31+
return tostring(original_table(proxy))
3432
end
3533

3634
local function __call(proxy, ...)
3735
local t = original_table(proxy)
3836

39-
local mt = getmetatable(proxy)
40-
4137
-- Try to run __call() and if it's not possible, try to run it in a way that
4238
-- it returns a meaningful error.
43-
if mt and mt.__call then
44-
return mt.__call(t, ...)
39+
local ret = { pcall(t, ...) }
40+
local ok = table.remove(ret, 1)
41+
42+
if ok then
43+
return unpack(ret)
4544
else
46-
local ret = { pcall(t, t, ...) }
47-
local ok = table.remove(ret, 1)
48-
49-
if ok then
50-
return unpack(ret)
51-
else
52-
error(ret[1], 2)
53-
end
45+
error(ret[1], 2)
5446
end
5547
end
5648

@@ -71,6 +63,8 @@ end
7163
-- @tparam table metatable A table that will be used as a metatable. It needs
7264
-- to define __gc.
7365
function _M.set_metatable_gc(t, metatable)
66+
setmetatable(t, metatable)
67+
7468
-- newproxy() returns a userdata instance
7569
local proxy = newproxy(true)
7670

spec/gc_spec.lua

+14-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe('GC', function()
4444
assert.same({ 1, 2, 3 }, res)
4545
end)
4646

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

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

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

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

113113
assert.same(test_metatable, getmetatable(table_with_gc))
114114
end)
115+
116+
it('returns an object that respects the __index in the mt passed in the params', function()
117+
local test_table = { 1, 2, 3 }
118+
local test_metatable = {
119+
__gc = function() end,
120+
__index = { some_func = function() return 'abc' end }
121+
}
122+
local table_with_gc = GC.set_metatable_gc(test_table, test_metatable)
123+
124+
assert.equals('abc', table_with_gc:some_func())
125+
end)
115126
end)
116127
end)

0 commit comments

Comments
 (0)