Skip to content

Commit

Permalink
Check if erlfunc return a list
Browse files Browse the repository at this point in the history
Closes #27
  • Loading branch information
davydog187 committed Oct 23, 2024
1 parent 37991d1 commit 6a6354a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/luerl_emul.erl
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ call_luafunc(#lua_func{lsz=Lsz,esz=Esz,pars=_Pars,body=Fis},
call_erlfunc(Func, Args, Stk, Cs0, #luerl{stk=Stk0}=St0) ->
case Func(Args, St0#luerl{stk=Stk,cs=Cs0}) of
%% {Ret,#luerl{}=St1} when is_list(Ret) ->
{Ret,St1} ->
{Ret,St1} when is_list(Ret) ->
[#call_frame{is=Is,cont=Cont,lvs=Lvs,env=Env}|Cs1] = Cs0,
emul(Is, Cont, Lvs, [Ret|Stk], Env, Cs1, St1#luerl{stk=Stk0,cs=Cs1});
_Other ->
Expand Down
5 changes: 4 additions & 1 deletion src/luerl_new.erl
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,8 @@ get_filename(Mod) ->
%% encode(Term, State) -> {LuerlTerm,State}.

encode_list(Ts, St) ->
lists:mapfoldl(fun encode/2, St, Ts).

lists:mapfoldl(fun encode/2, St, Ts).

encode(nil, St) -> {nil,St};
encode(false, St) -> {false,St};
Expand All @@ -380,13 +381,15 @@ encode(L, St0) when is_list(L) ->
encode(F, St) when is_function(F, 2) ->
F1 = fun(Args, State) ->
Args1 = decode_list(Args, State),
% TODO make this a case
{Res, State1} = F(Args1, State),
encode_list(Res, State1)
end,
{#erl_func{code=F1}, St};
encode(F, St) when is_function(F, 1) ->
F1 = fun(Args, State) ->
Args1 = decode_list(Args, State),
% TODO make this a case
Res = F(Args1),
encode_list(Res, State)
end,
Expand Down
8 changes: 8 additions & 0 deletions test/luerl_new_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,11 @@ encode_table_test() ->

invalid_table_test() ->
?assertException(error, badarg, luerl_new:encode({tref, 42}, luerl_new:init())).

invalid_erlfunc_return_test() ->
State = luerl_new:init(),
Func = fun(_Args, State) ->
{invalid, State}
end,
{ok, [], State1} = luerl_new:set_table_keys_dec([foo], Func, State),
?assertException(error, blah, luerl_new:do("return foo()", State1)).

0 comments on commit 6a6354a

Please sign in to comment.