Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
To: olegrok@tarantool.org, tarantool-patches@dev.tarantool.org,
	imun@tarantool.org
Cc: Oleg Babin <babinoleg@mail.ru>
Subject: Re: [Tarantool-patches] [PATCH 1/3] lua: fix incorrect table.deepcopy __metatable handling
Date: Thu, 13 Feb 2020 23:50:18 +0100	[thread overview]
Message-ID: <699c3487-7fd4-9ca8-6de0-0bde4089d7ad@tarantool.org> (raw)
In-Reply-To: <be90bf2be31fbe4be09dccf1662d3d988416fccf.1581625524.git.babinoleg@mail.ru>

Thanks for the patch!

See 3 comments below.

On 13/02/2020 21:33, olegrok@tarantool.org wrote:
> From: Oleg Babin <babinoleg@mail.ru>
> 
> Before this patch we got metatabe of original table
> using "getmetatable" function. It leads to the error
> if original table contained __metatable that could be
> e.g. a string or a number.

1. What error and why? (I know why, because I googled this,
but it should be in the commit message).

> To fix this problem getmetatable was replaced to debug.getmetatable.
> 
> Closes #4340
> ---


> Issue: https://github.com/tarantool/tarantool/issues/4340
>  src/lua/table.lua           |  2 +-
>  test/app-tap/table.test.lua | 20 +++++++++++++++++++-
>  2 files changed, 20 insertions(+), 2 deletions(-)
> 
> diff --git a/src/lua/table.lua b/src/lua/table.lua
> index 7e8ba7aae..d83217dcb 100644
> --- a/src/lua/table.lua
> +++ b/src/lua/table.lua
> @@ -2,7 +2,7 @@ local function table_deepcopy_internal(orig, cyclic)
>      cyclic = cyclic or {}
>      local copy = orig
>      if type(orig) == 'table' then
> -        local mt, copy_function = getmetatable(orig), nil
> +        local mt, copy_function = debug.getmetatable(orig), nil

2. Please, add a comment why do you use debug instead of normal
getmetatable().

>          if mt then copy_function = mt.__copy end
>          if copy_function == nil then
>              copy = {}
> diff --git a/test/app-tap/table.test.lua b/test/app-tap/table.test.lua
> index 60c095fdf..07894f69e 100755
> --- a/test/app-tap/table.test.lua
> +++ b/test/app-tap/table.test.lua
> @@ -8,7 +8,7 @@ yaml.cfg{
>      encode_invalid_as_nil  = true,
>  }
>  local test = require('tap').test('table')
> -test:plan(31)
> +test:plan(33)
>  
>  do -- check basic table.copy (deepcopy)
>      local example_table = {
> @@ -223,4 +223,22 @@ do -- check usage of not __copy metamethod on second level + shallow
>      )
>  end
>  
> +do -- gh-4340: deepcopy doesn't handle __metatable correctly.

3. Please, put comment on a separate line, before 'do'. The code
above, which places comment and code on the same line, is
incorrect. Probably because it is old, and at that time there
was no such rule.

> +    local original = {
> +        content = 'string'
> +    }
> +    setmetatable(original, { __metatable = 'protection' })
> +    local copy = table.deepcopy(original)
> +    test:is(
> +            copy.content,
> +            original.content,
> +            "checking that original string was copied"
> +    )
> +    test:is(
> +            getmetatable(copy),
> +            'protection',
> +            "checking that __metatable was correctly copied"
> +    )
> +end
> +
>  os.exit(test:check() == true and 0 or 1)
> 

  reply	other threads:[~2020-02-13 22:50 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-13 20:33 [Tarantool-patches] [PATCH 0/3] lua: table fixes olegrok
2020-02-13 20:33 ` [Tarantool-patches] [PATCH 1/3] lua: fix incorrect table.deepcopy __metatable handling olegrok
2020-02-13 22:50   ` Vladislav Shpilevoy [this message]
2020-02-20 11:12   ` Igor Munkin
2020-02-13 20:33 ` [Tarantool-patches] [PATCH 2/3] lua: table.deepcopy ignores __pairs metamethod olegrok
2020-02-13 22:50   ` Vladislav Shpilevoy
2020-02-20 11:00   ` Igor Munkin
2020-02-13 20:33 ` [Tarantool-patches] [PATCH 3/3] tap: is_deeply " olegrok
2020-02-13 22:50   ` Vladislav Shpilevoy
2020-02-20 10:57   ` Igor Munkin
2020-02-13 22:50 ` [Tarantool-patches] [PATCH 0/3] lua: table fixes Vladislav Shpilevoy
2020-02-15 10:05   ` Oleg Babin
2020-02-15 15:35     ` Vladislav Shpilevoy

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=699c3487-7fd4-9ca8-6de0-0bde4089d7ad@tarantool.org \
    --to=v.shpilevoy@tarantool.org \
    --cc=babinoleg@mail.ru \
    --cc=imun@tarantool.org \
    --cc=olegrok@tarantool.org \
    --cc=tarantool-patches@dev.tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH 1/3] lua: fix incorrect table.deepcopy __metatable handling' \
    /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