Skip to content

Commit

Permalink
Make calls to atom_to_binary of 2 argument specific utf8
Browse files Browse the repository at this point in the history
Older versions of Erlang did not have the atom_to_binary/1 so this
will be safer with older OTP versions.
  • Loading branch information
rvirding committed Feb 29, 2024
1 parent fc39e0f commit c312d9a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/luerl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ encode(nil, 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, utf8),St};
encode(N, St) when is_number(N) -> {N,St}; %Integers and floats
encode(F, St) when ?IS_MAP(F) -> encode(maps:to_list(F), St);
encode(L, St0) when is_list(L) ->
Expand Down
8 changes: 4 additions & 4 deletions src/luerl_lib.erl
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ format_error(Format, Vals) ->
format_value(nil) -> <<"nil">>;
format_value(N) when is_integer(N) -> integer_to_list(N);
format_value(N) when is_float(N) -> float_to_list(N);
format_value(B) when is_boolean(B) -> atom_to_binary(B);
format_value(B) when is_boolean(B) -> atom_to_binary(B, utf8);
format_value(B) when is_binary(B) ->
%% A luerl string which we print with quotes around it.
%% Note that the string can contain unicode codepoints.
Expand All @@ -144,14 +144,14 @@ format_value(#usdref{}) -> <<"userdata">>;
format_value(#funref{}) -> <<"function">>;
format_value(#erl_func{code=Fun}) ->
{name,Name} = erlang:fun_info(Fun, name),
atom_to_binary(Name);
format_value(#erl_mfa{f=Func}) -> atom_to_binary(Func);
atom_to_binary(Name, utf8);
format_value(#erl_mfa{f=Func}) -> atom_to_binary(Func, utf8);
format_value(List) when is_list(List) ->
Pl = lists:map(fun format_value/1, List),
lists:join($\,, Pl);
%% Treat atoms as binary strings here, probably just a name.
format_value(A) when is_atom(A) ->
[$\',atom_to_list(A),$\'];
[$\',atom_to_binary(A, utf8),$\'];
%% Everything else just straight through.
format_value(_Other) -> <<"unknown stuff">>.

Expand Down
2 changes: 1 addition & 1 deletion src/luerl_new.erl
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ encode(nil, 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, utf8),St};
encode(N, St) when is_number(N) -> {N,St}; %Integers and floats
encode(F, St) when ?IS_MAP(F) -> encode(maps:to_list(F), St);
encode(L, St0) when is_list(L) ->
Expand Down

0 comments on commit c312d9a

Please sign in to comment.