Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ Changes to the Lua initialization in MUSHclient v4.60

Changes to the Lua initialization in MUSHclient v4.60

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Sun 05 Sep 2010 02:44 AM (UTC)

Amended on Sun 05 Sep 2010 02:45 AM (UTC) by Nick Gammon

Message
As part of code cleanups for version 4.60 of MUSHclient, I noticed that the implementation of the main script spaces metatables was wrong (it is confusing reading the C code to set it up).

To demonstrate, in any version below 4.60 you can try this:


print (_G)        --> table: 00D0B6B8
print (world)     --> table: 00CE6A38
print (rawget (_G, "__index"))  --> nil
print (rawget (world, "__index")) --> table: 00CE6A38
print (getmetatable (_G))         --> table: 00CE6A38
print (getmetatable (world))      --> nil
world.__index = nil
Note "hello"  --> Error: attempt to call global 'Note' (a nil value)


You can see that the above is wrong in a number of ways. It is as if this was coded in Lua:


setmetatable (_G, world)
world.__index = world


It should in fact look more like this:


setmetatable (_G, { __index = world } )


Also, setting world.__index to nil should not stop access to the world functions from the global environment.

In version 4.60 the results are like this:


print (_G)      --> table: 020547A0
print (world)   --> table: 0205ED30
print (rawget (_G, "__index"))    --> nil
print (rawget (world, "__index")) --> nil
print (getmetatable (_G))     --> table: 0205EC90
print (getmetatable (world))  --> nil
world.__index = nil 
Note "hello"   --> hello


Now there is no "__index" field in the world table, and the global environment has a metatable which is different from the world table. This metatable simply indexes back to the world table. If you set world.__index it has no effect (it was nil anyway).

Now these changes *shouldn't* affect existing scripts, but I mention them in case anyone has done some obscure mucking around with metatables and are relying on the current behaviour.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #1 on Sun 05 Sep 2010 03:02 AM (UTC)
Message
Fun side effect of the pre-4.60 behavior: you can access the world table as __index, too.

__index.Note("lolwat")

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #2 on Sun 05 Sep 2010 03:16 AM (UTC)
Message
Yeah, exactly why it was wrong. ;)

Well spotted.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


8,454 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.