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

[Question][Help] How do I Passing a lua.LTable to a CallByParam #178

Closed
2 tasks done
Skarlso opened this issue May 21, 2018 · 1 comment
Closed
2 tasks done

[Question][Help] How do I Passing a lua.LTable to a CallByParam #178

Skarlso opened this issue May 21, 2018 · 1 comment

Comments

@Skarlso
Copy link

Skarlso commented May 21, 2018

  • GopherLua is a Lua5.1 implementation. You should be familiar with Lua programming language. Have you read Lua 5.1 reference manual carefully?
  • GopherLua is a Lua5.1 implementation. In Lua, to keep it simple, it is more important to remove functionalities rather than to add functionalities unlike other languages . If you are going to introduce some new cool functionalities into the GopherLua code base and the functionalities can be implemented by existing APIs, It should be implemented as a library.

Please answer the following before submitting your issue:

  1. What version of GopherLua are you using? :
    Latest
  2. What version of Go are you using? :
    Latest
  3. What operating system and processor architecture are you using? :
    OSX

HI.

I'm trying to pass in a lua table to a CallByParam like this:

// Call will call a Lua method in a loaded plugin.
func Call(function string, args lua.LTable) (lua.LValue, error) {
	if err := L.CallByParam(lua.P{
		Fn:      L.GetGlobal(function),
		NRet:    1,
		Protect: true,
	}, args); err != nil {
		panic(err)
	}
	ret := L.Get(-1) // returned value
	L.Pop(1)         // remove received value
	return ret, nil
}

So, obviously this isn't permitted since lua.P requires a lua.LValue. So I looked at userType because maybe that could help, but I'm still confused on how to do this the right way. I wonder if I'm missing something? Is there a way to nicely convert table to something usable instead?

The point of the table is that I have arbitrary parameters I don't want to parse rather pass in as a table so the corresponding lua script can deal with handling the data however it wants.

I saw something like map -> json -> table somewhere in the old issues, but I didn't see an example on that comment.

Thanks!

@Skarlso
Copy link
Author

Skarlso commented May 21, 2018

Well I'll be.

So what worked is if I instead of the LValue or convert, I just pass in the reference to the table.

I'm constructing a table using L.NewTable than set field via table.RawSetString than passing it in like this:

L.CallByParam(lua.P{
		Fn:      L.GetGlobal(function),
		NRet:    1,
		Protect: true,
	}, &args)

Where args is args lua.LTable. And in my lua script:

function git (args)
    print("translating...")
    return string.format( "git clone -b %s git@github.com:%s ", args['branch'], args['repo'] )
end

And done. this works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant