Top level is a chunk

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Top level is a chunk

Gavin Wraith
It had somehow never hit home to me that the top level of a Lua program
is a chunk. Because of this I had been enclosing my programs within
do ... end structures so that I could use local variables at the top level
( sledgehammers for nuts, as the programs were tiny, but that is not
the point ). Then I experimented, writing ( in Lua 5.2beta )

    local x = 8
    print(x)

This ran fine, so all those do ... ends had been unnecessary. Disassembly
gives

    local x = 8
    --[[
         1 LOADK     0 -1 ; 8
    --]]
    print(x)
    --[[
         2 GETTABUP  1 0 -2 ; _ENV "print"
         3 MOVE      2 0
         4 CALL      1 2 1
         5 RETURN    0 1
    --]]

whereas disassembly of

    x = 8
    print(x)

gives

    x = 8
    --[[
         1 SETTABUP  0 -1 -2 ; _ENV "x" 8
    --]]
    print(x)
    --[[
         2 GETTABUP  0 0 -3 ; _ENV "print"
         3 GETTABUP  1 0 -1 ; _ENV "x"
         4 CALL      0 2 1
         5 RETURN    0 1
    --]]

I do not have to worry about all those extra do ... end structures because
disassembly of

    do
     local x = 8
     print(x)
    end

gives

    do
    local x = 8
    --[[
         1 LOADK     0 -1 ; 8
    --]]
    print(x)
    --[[
         2 GETTABUP  1 0 -2 ; _ENV "print"
         3 MOVE      2 0
         4 CALL      1 2 1
    --]]
    end
    --[[
         5 RETURN    0 1
    --]]

- exactly the same bytecode as if the do ... end were not there.

I expect all this is not news to seasoned Lua users, but I thought the
disassembly listings might entertain.

--
Gavin Wraith ([hidden email])
Home page: http://www.wra1th.plus.com/

Reply | Threaded
Open this post in threaded view
|

Re: Top level is a chunk

Luiz Henrique de Figueiredo
> Because of this I had been enclosing my programs within
> do ... end structures so that I could use local variables at the top level

Local variables at the top level work since Lua 2.2, released in 1995...
 
> I expect all this is not news to seasoned Lua users, but I thought the
> disassembly listings might entertain.

Yes, comparing and reading disassembly listings is a good way to see how
the VM (and the parser) works.

Reply | Threaded
Open this post in threaded view
|

Re: Top level is a chunk

Matthew Wild
In reply to this post by Gavin Wraith
(function () {

On 31 October 2011 22:59, Gavin Wraith <[hidden email]> wrote:
> It had somehow never hit home to me that the top level of a Lua program
> is a chunk. Because of this I had been enclosing my programs within
> do ... end structures so that I could use local variables at the top level

> I expect all this is not news to seasoned Lua users, but I thought the
> disassembly listings might entertain.

I still remember the moment I realised (with a groan) that "var foo =
true" in the top level of a Javascript script had visibility
throughout the current environment. Sigh.

Regards,
Matthew

})()

Reply | Threaded
Open this post in threaded view
|

Re: Top level is a chunk

Rena
On Mon, Oct 31, 2011 at 17:27, Matthew Wild <[hidden email]> wrote:

> (function () {
>
> On 31 October 2011 22:59, Gavin Wraith <[hidden email]> wrote:
>> It had somehow never hit home to me that the top level of a Lua program
>> is a chunk. Because of this I had been enclosing my programs within
>> do ... end structures so that I could use local variables at the top level
>
>> I expect all this is not news to seasoned Lua users, but I thought the
>> disassembly listings might entertain.
>
> I still remember the moment I realised (with a groan) that "var foo =
> true" in the top level of a Javascript script had visibility
> throughout the current environment. Sigh.
>
> Regards,
> Matthew
>
> })()
>
>

Javascript then does some interesting things like hoisting to confuse
you further. ;-)

One of my first attempts to write a program was in assembly (ha!). It
was intended to be a simple countdown timer. All it did was decrement
EAX a few times. It hadn't occurred to me that I had to do more things
to actually display that number on the screen, nor that without a
delay my 10-second countdown would be closer to a 10-microsecond
countdown...

I ended up learning most of what I know about computers by hacking and
studying Nintendo 64 games, so I can vouch for how helpful it can be
to read bytecode/machine code.

--
Sent from my toaster.