Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bad argument #1 to <function name> (userdata expected, got string) #1

Closed
djosephsen opened this issue May 6, 2015 · 4 comments
Closed
Labels

Comments

@djosephsen
Copy link

Hi,

This is a fantastic project thanks for it.

I'm having some trouble that I think is being caused by luar's translation of my go functions.

panic:
[G]: bad argument #1 to Respond (userdata expected, got string)
stack traceback:
    [G]: in Respond
    test.lua:6: in main chunk
    [G]: ?

test .lua is this:

synfunc = function (msg)
    msg.reply("ack")
end

robot.Respond("syn",synfunc)

The general idea is that users will call robot.Respond with a regex they want to match, and a reference to a lua function they want to be called when the regex is detected by the upstream go program.

robot is a very simple struct type that enables the upstream Go to index this request back to the lua state that made it (there will be many):

type Robot struct{
   ID    int
}

the respond function on the go-side is also pretty simple:

func (r Robot)Respond(pat string, lfunc lua.LValue){
   newMsgCallback(r.ID, pat, lfunc, true)
}

As you can see in the error message, this panic's in runtime because the lua state seems to think my Respond function wants lua.LUserData instead of a string, which I assume(?) is probably being caused by luar's translation.

I've written some test scripts and I don't seem to be able to replicate this issue (methods on go structs with this signature (string,lval) seem to work in general), so I'm not sure if I'm doing something wrong in this codebase or if I've found a bug or what. You can take a look at the actual codebase here

Thanks in advance for your help.

-dave

@ghost
Copy link

ghost commented May 6, 2015

Try replacing

robot.Respond("syn",synfunc)

with

robot:Respond("syn",synfunc)

@djosephsen
Copy link
Author

yep that totally worked thanks. (dare I ask why?)

@ghost
Copy link

ghost commented May 6, 2015

From the Lua manual:

The form

functioncall ::= prefixexp `:´ Name args

can be used to call "methods". A call v:name(args) is syntactic sugar for v.name(v,args), except that v is evaluated only once.

Meaning following two statements are equivalent:

robot:Respond("syn",synfunc)
robot.Respond(robot, "syn",synfunc).

@ghost ghost closed this as completed May 6, 2015
@djosephsen
Copy link
Author

ah so! thanks so much.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

1 participant