You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'd like to retrieve the table that the script has returned, I've seen the other issue #101, and I don't quite understand how to obtain the return value.
Perhaps I need an example of how to access it.
Just a short example will do, as once I see it I am sure I'll be able to work my way to understanding how to do it for further cases.
The text was updated successfully, but these errors were encountered:
After performing DoString (or DoFile) the result will be at top of the stack.
Since you return a table you can fetch the items using L.GetField
Example:
funcTestReturnDoString(t*testing.T) {
L:=lua.NewState()
L.DoString(`return { answer = 42 }`)
assert.True(t, L.IsTable(-1)) // Should be true!L.GetField(-1, "answer")
assert.Equal(t, 42, L.ToInteger(-1))
}
Note that LoadString (or LoadFile) does not actually execute the file, but rather returns a function that contains the file. In that case you need to L.Call the loaded chunk first.
funcTestReturnTableFromLoadString(t*testing.T) {
L:=lua.NewState()
L.LoadString(`return { answer = 42 }`)
assert.True(t, L.IsFunction(-1)) // Should be true!L.Call(0, 1) // Has 0 parameters, and returns 1 value// Returned value will now be at the top of the stackassert.True(t, L.IsTable(-1)) // Should be true!L.GetField(-1, "answer")
assert.Equal(t, 42, L.ToInteger(-1))
}
To know what functions do with the stack, refer to the Lua Manual.
I have a lua script like below:
I'd like to retrieve the table that the script has returned, I've seen the other issue #101, and I don't quite understand how to obtain the return value.
Perhaps I need an example of how to access it.
The text was updated successfully, but these errors were encountered: