-
Notifications
You must be signed in to change notification settings - Fork 142
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
failing to return a table #71
Comments
after some more research it seems this works: St0 = luerl:init(), ok.
F = fun(_, StIn) ->
{[[{<<"a">>, 1.0}]], StIn}
end.
St1 = luerl:set_table([t], F, St0), ok.
luerl:eval("return t()", St1). But I'm not sure I understand why the function alloc_table works in the lib but not in the call. |
It has to do with encoding and decoding data between the "external erlang" representation of lua and the internal representation of the same. So The So when you call The problem was that In your second attempt you explicitly return external format which could be encoded into internal format and returned to the emulator so it worked. As example of using your original function you could have done:
Here |
Ohhh I was really wondering what the 1 functions do :) they totally baffled me that makes sense. So to clarify if I understand it correctly (as I will document this ;). If I use Additionally would you be opposed to a PR for renaming the 1 to something like |
Doing something as radical as changing the main the interface module will have to wait until a major revision. I have plans to change the interface as such and I would prefer to wait until then. But if you feel upto writing something about it in the wiki I would not complain. 😄 |
Makes total sense. wiki entry incoming (I'll close this too as the question is answered) but will re-add a new issue for renaming functions if you don't mind so it's tracked. |
Ciao guys, I think I'm having a similar problem (and likely a little bit of confusion)... Here is the code showcasing my problem defmodule Helper do
def run(data) do
script = "function foo(data) print(data); return helper.date(); end"
{:ok, chunk, st} = :luerl.load(script, :luerl.init())
st = :luerl.load_module(["helper"], __MODULE__, st)
{_res, st} = :luerl.do(chunk, st)
{result, _stuff} = :luerl.call_function([:foo], [data], st)
result
end
def date([], st) do
datetime =
DateTime.utc_now()
|> NaiveDateTime.to_erl()
|> IO.inspect()
###############
# How do I pack datetime in place of "wouldlikedatetimehere"
{["wouldlikedatetimehere"], st}
###############
end
def install(st) do
:luerl_emul.alloc_table(table(), st)
end
def table() do
[
{"date", {:erl_func, &date/2}}
]
end
end |
After lots of trial and error and reading reading this other issue I reworked the example and am now able to use it: This code runs lua code which calls other erlang/elixir code, and data structures traverses the languages to be returned. Hope it can be of some help to ppl defmodule Helper do
def run(data) do
script = "
function foo(data)
print(data);
local datetime = helper.datetime();
print(datetime.time.h)
return datetime;
end"
{:ok, chunk, st} = :luerl.load(script, :luerl.init())
st = :luerl.load_module(["helper"], __MODULE__, st)
{_res, st} = :luerl.do(chunk, st)
{result, _stuff} = :luerl.call_function([:foo], [data], st)
result
end
def datetime([], st) do
{{year, month, day}, {hour, min, sec}} =
DateTime.utc_now()
|> NaiveDateTime.to_erl()
{res, st} =
[date: [y: year, m: month, d: day], time: [h: hour, m: min, s: sec]]
|> :luerl.encode(st)
{[res], st}
end
def install(st) do
:luerl_emul.alloc_table(table(), st)
end
def table() do
[
{"datetime", {:erl_func, &datetime/2}}
]
end
end |
Hi I've ran into a problem that a function seems unable to return a table.
The function is based on the pack function form luerl_lib_table:
luerl/src/luerl_lib_table.erl
Line 263 in 3e69763
The text was updated successfully, but these errors were encountered: