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

Validate Func(Args) return value #27

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
2 changes: 2 additions & 0 deletions rebar.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{erl_opts, [debug_info]}.

9 changes: 5 additions & 4 deletions src/luerl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ eval(Chunk, St0) ->
try do(Chunk, St0) of
{Ret,St1} -> {ok, decode_list(Ret, St1)}
catch
_E:R -> {error, R} % {error, {E, R}} ? <- todo: decide
_E:R -> {error, R, erlang:get_stacktrace()} % {error, {E, R}} ? <- todo: decide
end.

%% luerl:evalfile(Path[, State]) -> {ok, Result} | {error,Reason}.
Expand All @@ -48,7 +48,7 @@ evalfile(Path, St0) ->
try dofile(Path, St0) of
{Ret,St1} -> {ok, decode_list(Ret, St1)}
catch
_E:R -> {error, R} % {error, {E, R}} ? <- todo: decide
_E:R -> {error, R, erlang:get_stacktrace()} % {error, {E, R}} ? <- todo: decide
end.

%% luerl:do(String|Binary|Form[, State]) -> {Result, NewState}
Expand Down Expand Up @@ -214,10 +214,11 @@ encode_list(Ts, St) ->
lists:mapfoldl(fun encode/2, St, Ts).

encode(nil, St) -> {nil,St};
encode(undefined, St) -> {nil,St};
encode(false, St) -> {false,St};
encode(true, St) -> {true,St};
encode(B, St) when is_binary(B) -> {B,St};
encode(A, St) when is_atom(A) -> {atom_to_binary(A, latin1),St};
encode(A, St) when is_atom(A) -> {atom_to_binary(A,latin1),St};
encode(I, St) when is_integer(I) -> {float(I),St};
encode(F, St) when is_float(F) -> {F,St};
encode(L, St0) when is_list(L) ->
Expand All @@ -231,7 +232,7 @@ encode(L, St0) when is_list(L) ->
end, {1.0,St0}, L),
{T,St2} = luerl_emul:alloc_table(Es, St1),
{T,St2}; %No more to do for now
encode(_, _) -> error(badarg). %Can't encode anything else
encode(Val, _) -> error({badarg,Val}). %Can't encode anything else

%% decode_list([LuerlTerm], State) -> [Term].
%% decode(LuerlTerm, State) -> Term.
Expand Down
8 changes: 6 additions & 2 deletions src/luerl_emul.erl
Original file line number Diff line number Diff line change
Expand Up @@ -653,8 +653,12 @@ functioncall(#function{lsz=Lsz,esz=Esz,pars=Pars,env=Env,b=Fis},
{Ret,St2};
functioncall({function,Func}, Args, Stk, #luerl{stk=Stk0}=St0) ->
%% Here we must save the stack in state as function may need it.
{Ret,St1} = Func(Args, St0#luerl{stk=Stk}),
{Ret,St1#luerl{stk=Stk0}}; %Replace it
case Func(Args, St0#luerl{stk=Stk}) of
{Ret,St1} when is_list(Ret) ->
{Ret,St1#luerl{stk=Stk0}}; %Replace it
{Ret,St1} ->
error({invalid_reply,Ret,function,Func})
end;
functioncall(Func, Args, Stk, St) ->
case getmetamethod(Func, <<"__call">>, St) of
nil -> badarg_error(Func, Args, St);
Expand Down