Tarantool development patches archive
 help / color / mirror / Atom feed
From: Sergey Kaplun via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: Maxim Kokryashkin <m.kokryashkin@tarantool.org>,
	Sergey Bronnikov <sergeyb@tarantool.org>
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH luajit 4/4] FFI: Fix ffi.metatype() for non-raw types.
Date: Mon,  3 Jun 2024 17:34:02 +0300	[thread overview]
Message-ID: <f32221784d5f0e491932087a2bee666acd498e3a.1717424008.git.skaplun@tarantool.org> (raw)
In-Reply-To: <cover.1717424008.git.skaplun@tarantool.org>

From: Mike Pall <mike>

Reported by 999pingGG.

(cherry picked from commit 4c35a42d9cd3b8be98fa419c68e4724ccdf4f2cf)

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
---
 src/lib_ffi.c                                     |  2 +-
 .../lj-861-ctype-attributes.test.lua              | 15 ++++++++++++---
 2 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/src/lib_ffi.c b/src/lib_ffi.c
index dfb01658..7ed6fc78 100644
--- a/src/lib_ffi.c
+++ b/src/lib_ffi.c
@@ -776,7 +776,7 @@ LJLIB_CF(ffi_metatype)
   if (!(ctype_isstruct(ct->info) || ctype_iscomplex(ct->info) ||
 	ctype_isvector(ct->info)))
     lj_err_arg(L, 1, LJ_ERR_FFI_INVTYPE);
-  tv = lj_tab_setinth(L, t, -(int32_t)id);
+  tv = lj_tab_setinth(L, t, -(int32_t)ctype_typeid(cts, ct));
   if (!tvisnil(tv))
     lj_err_caller(L, LJ_ERR_PROTMT);
   settabV(L, tv, mt);
diff --git a/test/tarantool-tests/lj-861-ctype-attributes.test.lua b/test/tarantool-tests/lj-861-ctype-attributes.test.lua
index a407fae0..79121cdb 100644
--- a/test/tarantool-tests/lj-861-ctype-attributes.test.lua
+++ b/test/tarantool-tests/lj-861-ctype-attributes.test.lua
@@ -2,12 +2,14 @@ local tap = require('tap')
 
 -- Test file to demonstrate LuaJIT incorrect behaviour during
 -- parsing and working with ctypes with attributes.
--- See also: https://github.com/LuaJIT/LuaJIT/issues/861.
+-- See also:
+-- * https://github.com/LuaJIT/LuaJIT/issues/861,
+-- * https://github.com/LuaJIT/LuaJIT/issues/1005.
 
 local test = tap.test('lj-861-ctype-attributes')
 local ffi = require('ffi')
 
-test:plan(5)
+test:plan(6)
 
 local EXPECTED_ALIGN = 4
 
@@ -37,8 +39,15 @@ test:is(ffi.sizeof('struct test_parsing_sizeof'), EXPECTED_ALIGN,
 test:is(ffi.sizeof('struct test_parsing_alignof'), EXPECTED_ALIGN,
         'correct alignof during C parsing')
 
-local ok, _ = pcall(ffi.metatype, 's_aligned', {})
+local EXPECTED_TOSTR = '__tostring overloaded'
+local ok, obj = pcall(ffi.metatype, 's_aligned', {
+  __tostring = function()
+    return EXPECTED_TOSTR
+  end,
+})
 
 test:ok(ok, 'ffi.metatype is called at the structure with attributes')
+test:is(tostring(obj()), EXPECTED_TOSTR,
+        '__tostring is overloaded for the structure with attributes')
 
 test:done(true)
-- 
2.45.1


  parent reply	other threads:[~2024-06-03 14:40 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-03 14:33 [Tarantool-patches] [PATCH luajit 0/4] Fixes for ctypes with attributes Sergey Kaplun via Tarantool-patches
2024-06-03 14:33 ` [Tarantool-patches] [PATCH luajit 1/4] FFI: Fix ffi.alignof() for reference types Sergey Kaplun via Tarantool-patches
2024-06-11 15:40   ` Sergey Bronnikov via Tarantool-patches
2024-06-14 12:19   ` Maxim Kokryashkin via Tarantool-patches
2024-06-03 14:34 ` [Tarantool-patches] [PATCH luajit 2/4] FFI: Fix sizeof expression in C parser " Sergey Kaplun via Tarantool-patches
2024-06-11 15:42   ` Sergey Bronnikov via Tarantool-patches
2024-06-14 12:21   ` Maxim Kokryashkin via Tarantool-patches
2024-06-03 14:34 ` [Tarantool-patches] [PATCH luajit 3/4] FFI: Allow ffi.metatype() for typedefs with attributes Sergey Kaplun via Tarantool-patches
2024-06-11 15:48   ` Sergey Bronnikov via Tarantool-patches
2024-06-13  7:34     ` Sergey Kaplun via Tarantool-patches
2024-06-13 15:14       ` Sergey Bronnikov via Tarantool-patches
2024-06-14 12:39   ` Maxim Kokryashkin via Tarantool-patches
2024-06-03 14:34 ` Sergey Kaplun via Tarantool-patches [this message]
2024-06-11 15:50   ` [Tarantool-patches] [PATCH luajit 4/4] FFI: Fix ffi.metatype() for non-raw types Sergey Bronnikov via Tarantool-patches
2024-06-14 12:40   ` Maxim Kokryashkin via Tarantool-patches
2024-07-09  8:03 ` [Tarantool-patches] [PATCH luajit 0/4] Fixes for ctypes with attributes Sergey Kaplun via Tarantool-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f32221784d5f0e491932087a2bee666acd498e3a.1717424008.git.skaplun@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=m.kokryashkin@tarantool.org \
    --cc=sergeyb@tarantool.org \
    --cc=skaplun@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH luajit 4/4] FFI: Fix ffi.metatype() for non-raw types.' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox