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 ➜ Help with error

Help with error

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


Posted by Bast   (78 posts)  Bio
Date Fri 02 Jan 2009 04:57 PM (UTC)
Message
I am trying to use the class implementation from:

http://class.luaforge.net/

It works fine in the lua 5.1.3 interpreter on my linux box. But when I create a simple plugin to use the class, I get this error in Mush:


Run-time error
Plugin: Test_Plugin (called from world: Aardwolf)
Immediate execution
C:\Program Files\MUSHclient\lua\class.lua:136: bad argument #1 to 'unpack' (table expected, got nil)
stack traceback:
        [C]: in function 'unpack'
        C:\Program Files\MUSHclient\lua\class.lua:136: in function <C:\Program Files\MUSHclient\lua\class.lua:134>
        C:\Program Files\MUSHclient\lua\class.lua:370: in main chunk
        [C]: in function 'require'
        [string "Plugin"]:1: in main chunk


I don't know enough about lua to figure it out. I could use some help.

Thanks,
Eric

Here is the plugin

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<muclient>
<plugin
   name="Test_Plugin"
   author="Bast"
   id="76c897dda20af89787c6b438"
   language="Lua"
   purpose="Test class stuff"
   date_written="2008-12-24"
   requires="4.37"
   version="1.0"
   save_state="n"
   >
</plugin>

<script>
<![CDATA[
require 'class'

class "Window"

function Window:initialize(targs)
  --[[
    init the class, named arguments only
      the named arguments are 
      name, title, text, font, background_colour
      title_colour, hyperlink_colour, windowpos,
      title_width, title_colour, title_text_colour,
      text_colour, ishidden
  --]]
  self.name = targs.name or "Default"
end

print("making window")
questwin = Window{name="Quest"}
]]>
</script>

</muclient>

Bast

Scripts: http://github.com/endavis
Top

Posted by Nick Gammon   Australia  (23,165 posts)  Bio   Forum Administrator
Date Reply #1 on Fri 02 Jan 2009 07:26 PM (UTC)
Message
It fails if you even only have


require "class"


The offending line is here:


local a, b = unpack(arg) 


When run as we are trying to, arg will be nil. It appears that this particular line is trying to unpack the command-line arguments, of which there are none, as we are not using the command line.

I don't know what it is trying to do. According to the web page you linked:

Quote:

It is written in pure Lua and is fully compatible with Lua 5.0 .

The current state of library is "beta".


Its release date is May 20, 2007, which is almost 2 years ago.

Rather than trying to work out what this rather fancy library does, or doesn't do, I suggest you look at the treatment of classes in the Programming in Lua book here:

http://www.lua.org/pil/16.1.html

It seems strange to me to release a library, intended to be used inside scripts, that relies upon command-line arguments without any hints or comments about how to run it inside a larger script.

- Nick Gammon

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

Posted by Bast   (78 posts)  Bio
Date Reply #2 on Fri 02 Jan 2009 08:33 PM (UTC)
Message
Thanks for the quick answer Nick. It doesn't error out when I use it in a standalone lua script or the lua interpreter on my linux box. It only does that when using it in mushclient. Is there some special initialization that mushclient does? I will try looking into it.

I would use the class thing from the PIL book, but I firmly believe in not reinventing the wheel. Plus, the class.luaforge.com implementation has baseclasses and subclasses and makes it easy to do.

Thanks for the help,
Eric

Bast

Scripts: http://github.com/endavis
Top

Posted by Nick Gammon   Australia  (23,165 posts)  Bio   Forum Administrator
Date Reply #3 on Fri 02 Jan 2009 11:17 PM (UTC)
Message
OK, I have sort-of got to the bottom of it.

As their web page says, the module is compatible with Lua 5.0 (not Lua 5.1).

See this page for the differences between 5.0 and 5.1:

http://www.gammon.com.au/forum/?id=7322

One of the differences is the new way of handling varargs, that is, arguments to a function in the form (...).

The old method used: unpack (arg) to break up the ... argument into a table. Now Lua lets you assign ... to a table.

As mentioned in my thread above, you can simulate the old behaviour like this:


local arg = {...}; arg.n = select ("#", ...)  --> emulate old behaviour


I got the class to mainly work (96%) by laboriously going through and finding each function that used the ... form, and adding that line very near the start of it.

Now why does it work for you under Linux? Well the standard Lua distribution has this in it:


*
@@ LUA_COMPAT_VARARG controls compatibility with old vararg feature.
** CHANGE it to undefined as soon as your programs use only '...' to
** access vararg parameters (instead of the old 'arg' table).
*/
#define LUA_COMPAT_VARARG


In other words, they have built in backwards compatibility for the old vararg system. However I disabled that as documented here:

http://www.gammon.com.au/forum/bbshowpost.php?id=7795

The advice from the Lua developers (above) is to "CHANGE it to undefined ..." which is exactly what I did, to encourage the use of the newer vararg system, rather than the old one.

So really, you are using a module that is designed for Lua 5.0, and is using a deprecated feature, namely the old vararg system.

You may get satisfactory results if you add the line above to wherever you can, but I can't guarantee it. The fact that they have not updated the module for Lua 5.1 tends to suggest it isn't supported, and thus may have other subtle bugs.


- Nick Gammon

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

Posted by Nick Gammon   Australia  (23,165 posts)  Bio   Forum Administrator
Date Reply #4 on Fri 02 Jan 2009 11:19 PM (UTC)
Message
A possible workaround would be to download the Lua 5.1 DLL from the LuaBinaries web site, and use that to replace the one used by MUSHclient. I presume that would allow the varargs to work, but can't guarantee it.

- Nick Gammon

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

Posted by Bast   (78 posts)  Bio
Date Reply #5 on Sat 03 Jan 2009 01:19 AM (UTC)
Message
Thank you very much for the help Nick. I will test the class with the ... changes.

Eric

Bast

Scripts: http://github.com/endavis
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.


19,813 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.