lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Xmilia:

On Sat, Sep 11, 2021 at 1:10 PM Xmilia Hermit <xmilia.hermit@gmail.com> wrote:
> As stated in the manual at
> https://www.lua.org/manual/5.4/manual.html#3.3.3 the assignment
> statement should first evaluate all its expressions and only then the
> assignments should be performed.

>From what I see:

   stat ::= varlist ‘=’ explist
   varlist ::= var {‘,’ var}
   explist ::= exp {‘,’ exp}

It looks like the "expressions" refer only to the RHS ones. Somewhere
above vars are defined using prefixexp and exp , and a comment stating
that may be clarifying.

> However, this seems to not always be the case.
> The following code shows the problem:
> local up = {}
> setmetatable(up, {__newindex=function()
>      up = ""
> end})
> local function test()
>      up.x, up.y = 1, 1
> end
> test()
>
> This failes with: attempt to index a string value (upvalue 'up').
> However, I would expect from the manual that
> a=up, b=up, c=1, d=1 are evaluated (in any order) as they are
> expressions and then the assignments
> a.x = c, b.y = d are assigned in any order which would not result in the
> error observed.
> Am I missing something from the manual or is this a bug?

I think it may be a lack of detail. The manual, IMO, is trying to say that

up.x,up.y=up.y,up.x

works somehow like a=up.y, b=up.x, up.x=a, up.y=b, which may not be
obvious. In fact your example, from what I read, is behaving correctly
with all the exp, and is hitting the var[exp] variant of prefixexp
correctly preevaluating the exp part, but failing on the var part. I
would leave as is, as IMO someone doing this kind of things ( modiying
a thing via hidden captures in the metatables like you do ), should be
prepared for the "you broke it, you keep the two pieces" effects.

Note your code will also fault on up.x=1, up.x=1, and normally any
competent programmer is able to "crash" a language.

Francisco Olarte.


>
> Regards,
> Xmilia