Skip to content

Commit c6a76dc

Browse files
Mike PallBuristan
authored andcommitted
FFI: Fix ffi.metatype() for non-raw types.
Reported by 999pingGG. (cherry picked from commit 4c35a42) This patch is a follow-up to the previous commit. Since the child id is used for the index in the lookup table, it has no effect, and metatype methods are not overloaded. This patch fixes the behaviour by using the correct index. Sergey Kaplun: * added the description and the test for the problem Part of tarantool/tarantool#9924
1 parent a7e0e05 commit c6a76dc

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/lib_ffi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ LJLIB_CF(ffi_metatype)
776776
if (!(ctype_isstruct(ct->info) || ctype_iscomplex(ct->info) ||
777777
ctype_isvector(ct->info)))
778778
lj_err_arg(L, 1, LJ_ERR_FFI_INVTYPE);
779-
tv = lj_tab_setinth(L, t, -(int32_t)id);
779+
tv = lj_tab_setinth(L, t, -(int32_t)ctype_typeid(cts, ct));
780780
if (!tvisnil(tv))
781781
lj_err_caller(L, LJ_ERR_PROTMT);
782782
settabV(L, tv, mt);

test/tarantool-tests/lj-861-ctype-attributes.test.lua

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ local tap = require('tap')
22

33
-- Test file to demonstrate LuaJIT incorrect behaviour during
44
-- parsing and working with ctypes with attributes.
5-
-- See also: https://github.com/LuaJIT/LuaJIT/issues/861.
5+
-- See also:
6+
-- * https://github.com/LuaJIT/LuaJIT/issues/861,
7+
-- * https://github.com/LuaJIT/LuaJIT/issues/1005.
68

79
local test = tap.test('lj-861-ctype-attributes')
810
local ffi = require('ffi')
911

10-
test:plan(5)
12+
test:plan(6)
1113

1214
local EXPECTED_ALIGN = 4
1315

@@ -37,8 +39,15 @@ test:is(ffi.sizeof('struct test_parsing_sizeof'), EXPECTED_ALIGN,
3739
test:is(ffi.sizeof('struct test_parsing_alignof'), EXPECTED_ALIGN,
3840
'correct alignof during C parsing')
3941

40-
local ok, _ = pcall(ffi.metatype, 's_aligned', {})
42+
local EXPECTED_TOSTR = '__tostring overloaded'
43+
local ok, obj = pcall(ffi.metatype, 's_aligned', {
44+
__tostring = function()
45+
return EXPECTED_TOSTR
46+
end,
47+
})
4148

4249
test:ok(ok, 'ffi.metatype is called at the structure with attributes')
50+
test:is(tostring(obj()), EXPECTED_TOSTR,
51+
'__tostring is overloaded for the structure with attributes')
4352

4453
test:done(true)

0 commit comments

Comments
 (0)