lua-users home
lua-l archive

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


On 17/02/2010 00:42, Matthew Wild wrote:
> I don't find this confusing at all, it's perfectly logical. But
> jumping over local statements and then having the variable magically
> exist is logically wrong :)

The local statement doesn't generate code (unless it's combined with an
assignment). It merely causes the compiler to associate a name with a
particular stack slot. Jumping over the local statement therefore has no
effect.

What's not harmless is that the compiler is at liberty to assign
multiple variables to the same stack slot, if their usage doesn't
overlap. e.g.:

do
  local a = 1
  fnord(a)
  local b = 1
  fnord(b)
end

A proper register allocator would track the usage of a and b, note that
they're disjoint, and use the same stack slot for each one. This means
that jumping from one scope to another might cause odd problems:

do
  local a = 1
  if cond() then
    goto label
  end

  local b = 9
label:
  print(b)
end

If cond() returns true, then this might cause the print statement to
return 1, not 9!

Of course, right now the Lua interpreter *doesn't* do this, IIRC ---
each variable gets its own dedicated stack slot --- so we can rely on
them being initialised to nil. But I'm willing to bet that LuaJIT does.

This is why changing these semantics is a very hairy operation that
requires careful thought. (Personally, I'm willing to live with
variables being initialised to undefined values if it means I get a
proper goto.)

-- 
┌─── dg@cowlark.com ───── http://www.cowlark.com ─────
│
│ "They laughed at Newton. They laughed at Einstein. Of course, they
│ also laughed at Bozo the Clown." --- Carl Sagan