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: How can I call a lua function with Erlang terms? #169

Open
artman41 opened this issue Jan 17, 2024 · 2 comments
Open

Question: How can I call a lua function with Erlang terms? #169

artman41 opened this issue Jan 17, 2024 · 2 comments

Comments

@artman41
Copy link
Contributor

I'm trying to call a Lua function with Erlang terms like maps.

I've had 2 ideas so far:

  1. Try to manually call the function with encoded arguments
{ok, F, S0} = luerl:load("function x(...)\n return arg\n end", luerl:init()).
luerl:call(F, [1, 2, 3], S0).

But this only ever returns an empty list rather than the [1, 2, 3] or #tref output I'd expect

  1. try and parse the terms into lua structures and eval the strings but this seems extremely inefficient.

What is the intended method of calling lua methods from erlang with a set of given arguments?

@rvirding
Copy link
Owner

rvirding commented Jan 17, 2024

You can't! Code written in Lua cannot access general raw Erlang data structures, it can only access data in the right formats passed in the right format and will only return data in that format. It is up to the caller to encode/decode the data.

If you check the documentation for the luerl module you will see that there are usually 2 versions of many of the access functions, those that encode/decode for you and those that don't encode/decode and pass the internal Luerl formats in and out. Those don't encode/decode have names which end in 1. So for example the is a luerl:call_function/3 which encodes/decodes and a luerl:call_function1/3 which does not. No, the naming convention is not that good. 😄

When you called luerl:load/2 it compiled the string and returned the generated chunk in F. Then when you called that chunk with luerl:call/2 it just called the chunk and all the chunk did was load the function into the state. And that returned Lua nil which became the empty list.

You will find all the current Luerl documentation in the github Luerl wiki which also contains a lot of examples. I am in the process of migrating that documentation to the repo itself so it will be included when you download Luerl. So far I have only done the interface modules luerl, luerl_new and luerl_sandbox but I am working on it.

The luerl_new module contains the new standard interface which will in a future major release replace luerl which will then be kept in luerl_old. I think the new interface is much cleaner.

Hopefully this will help you a bit. Check the wiki and look in the examples directory for a bunch of examples and test cases. Not all are easy to understand I am afraid.

Tyler, there is also a Luerl slack workspace mentioned in the README and the wiki as well. And discord as well for that matter.

@rvirding
Copy link
Owner

rvirding commented Feb 6, 2024

Actually an extra comment here. The variable arg is actually unbound here so its value is nil and is therefore not a table.

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

No branches or pull requests

2 participants