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

Allow string operators to be disabled #80

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion rebar.config.script
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
%% -*- mode: erlang; indent-tabs-mode: nil -*-

Conf0 = CONFIG, %The original config
Conf0 = CONFIG ++ application:get_all_env(luerl),

%% Do a deep set stepping down a list of keys replacing/adding last
%% with value. Named funs would be nicer but not always available.
Expand Down Expand Up @@ -45,6 +45,7 @@ Version = case erlang:system_info(otp_release) of
%% Collect the macro definitions we will add to the compiler options.

%% Erlc macro definitions.

HasOpt = {d,'HAS_MAPS',true},
FullOpt = {d,'HAS_FULL_KEYS',true},
RecOpt = {d,'NEW_REC_CORE',true},
Expand Down
10 changes: 10 additions & 0 deletions src/luerl.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@
-define(IS_INTEGER(N,I), (float(I=round(N)) =:= N)).
-define(IS_TRUE(X), (((X) =/= nil) and ((X) =/= false))).

-ifdef(SANDBOX_STRINGS).
-define(IOLIST_TO_BINARY(S),
case luerl_lib_string:iolist_byte_size(S) < 64 of
true -> {ok, iolist_to_binary(S)};
false -> {error, string_too_large}
end).
-else.
-define(IOLIST_TO_BINARY(S), {ok, iolist_to_binary(S)}).
-endif.

%% Different methods for storing tables in the global data #luerl{}.
%% Access through macros to allow testing with different storage
%% methods. This is inefficient with ETS tables where it would
Expand Down
1 change: 1 addition & 0 deletions src/luerl_comp.erl
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ tableconstructor(Fs, St0) ->
%% var_name(Line, Name) -> #var{}.
%% lit_name(Line, Name) -> #lit{}.

name_string(Name) when is_binary(Name) -> Name;
name_string(Name) -> atom_to_binary(Name, latin1).

lit_name(L, N) -> #lit{l=L,v=name_string(N)}.
Expand Down
28 changes: 17 additions & 11 deletions src/luerl_emul.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1223,18 +1223,24 @@ le_meta(Op, A1, A2, St0) ->
{boolean_value(Ret),St1}
end.

-ifdef(SANDBOX_STRINGS).
-define(CONCAT(S1, S2), ?IOLIST_TO_BINARY([S1, S2])).
-else.
-define(CONCAT(S1, S2), {ok, <<S1/binary,S2/binary>>}).
-endif.

concat_op(A1, A2) ->
case luerl_lib:tostring(A1) of
nil ->
{meta,fun (_, St) -> concat_meta(A1, A2, St) end};
S1 ->
case luerl_lib:tostring(A2) of
nil ->
{meta,fun (_, St) -> concat_meta(A1, A2, St) end};
S2 ->
{ok,<<S1/binary,S2/binary>>}
end
end.
case luerl_lib:tostring(A1) of
nil ->
{meta,fun (_, St) -> concat_meta(A1, A2, St) end};
S1 ->
case luerl_lib:tostring(A2) of
nil ->
{meta,fun (_, St) -> concat_meta(A1, A2, St) end};
S2 ->
?CONCAT(S1, S2)
end
end.

concat_meta(A1, A2, St0) ->
case getmetamethod(A1, A2, <<"__concat">>, St0) of
Expand Down
11 changes: 9 additions & 2 deletions src/luerl_lib_basic.erl
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,16 @@ tostring(N) when is_number(N) ->
integer_to_list(round(N));
true -> io_lib:write(N)
end,
iolist_to_binary(S);
case ?IOLIST_TO_BINARY(S) of
{ok, S} -> S;
{error, E} -> error({E, N})
end;
tostring(S) when is_binary(S) -> S;
tostring(#tref{i=I}) -> iolist_to_binary(["table: ",io_lib:write(I)]);
tostring(#tref{i=I}) ->
case ?IOLIST_TO_BINARY(["table: ",io_lib:write(I)]) of
{ok, S} -> S;
{error, E} -> error({E, I})
end;
tostring(#lua_func{}) -> <<"function:">>; %Functions defined in Lua
tostring(#erl_func{}) -> <<"function:">>; %Internal functions
tostring(#thread{}) -> <<"thread">>;
Expand Down
7 changes: 7 additions & 0 deletions src/luerl_lib_string.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
%% The basic entry point to set up the function table.
-export([install/1]).

%% Utility
-export([iolist_byte_size/1]).

%% Export some test functions.
-export([test_gsub/3,test_match_pat/3,test_pat/1,
test_byte/3,test_do_find/4,test_sub/2,test_sub/3]).
Expand Down Expand Up @@ -302,6 +305,10 @@ lower(As, St) ->
_ -> badarg_error(lower, As, St) %nil or []
end.

iolist_byte_size(S) when is_list(S) -> lists:sum(lists:map(fun iolist_byte_size/1, S));
iolist_byte_size(S) when is_integer(S) -> 1;
iolist_byte_size(S) when is_binary(S) -> byte_size(S).

%% match(String, Pattern [, Init]) -> [Match].

match(As, St0) ->
Expand Down
71 changes: 40 additions & 31 deletions src/luerl_scan.xrl
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,24 @@ Rules.

Erlang code.

-include("luerl.hrl").

-export([is_keyword/1]).

%% name_token(Chars, Line) ->
%% {token,{'NAME',Line,Symbol}} | {Name,Line} | {error,E}.
%% Build a name from list of legal characters, else error.

name_token(Cs, L) ->
case catch {ok,list_to_atom(Cs)} of
{ok,Name} ->
case is_keyword(Name) of
true -> {token,{Name,L}};
false -> {token,{'NAME',L,Name}}
end;
_ -> {error,"illegal name"}
case is_keyword(Cs) of
true -> {token, {list_to_atom(Cs), L}};
false ->
case ?IOLIST_TO_BINARY(Cs) of
{ok, B} ->
{token, {'NAME', L, B}};
{error, string_too_large} ->
{error, "identifier too long"}
end
end.

%% base_token(Chars, Base, Line) -> Integer.
Expand Down Expand Up @@ -163,8 +167,13 @@ string_token(Cs0, Len, L) ->
Cs = string:substr(Cs0, 2, Len - 2), %Strip quotes
case catch {ok,chars(Cs)} of
{ok,S} ->
{token,{'STRING',L,list_to_binary(S)}};
error -> {error,"illegal string"}
case ?IOLIST_TO_BINARY(S) of
{ok, B} ->
{token,{'STRING',L, B}};
{error, string_too_large} ->
{error, "string too large"}
end;
error -> {error,"illegal string"}
end.

chars([$\\,C1|Cs0]) when C1 >= $0, C1 =< $9 -> %1-3 decimal digits
Expand Down Expand Up @@ -223,26 +232,26 @@ long_bracket(Line, Cs) ->
%% is_keyword(Name) -> boolean().
%% Test if the name is a keyword.

is_keyword('and') -> true;
is_keyword('break') -> true;
is_keyword('do') -> true;
is_keyword('else') -> true;
is_keyword('elseif') -> true;
is_keyword('end') -> true;
is_keyword('false') -> true;
is_keyword('for') -> true;
is_keyword('function') -> true;
is_keyword('goto') -> true;
is_keyword('if') -> true;
is_keyword('in') -> true;
is_keyword('local') -> true;
is_keyword('nil') -> true;
is_keyword('not') -> true;
is_keyword('or') -> true;
is_keyword('repeat') -> true;
is_keyword('return') -> true;
is_keyword('then') -> true;
is_keyword('true') -> true;
is_keyword('until') -> true;
is_keyword('while') -> true;
is_keyword("and") -> true;
is_keyword("break") -> true;
is_keyword("do") -> true;
is_keyword("else") -> true;
is_keyword("elseif") -> true;
is_keyword("end") -> true;
is_keyword("false") -> true;
is_keyword("for") -> true;
is_keyword("function") -> true;
is_keyword("goto") -> true;
is_keyword("if") -> true;
is_keyword("in") -> true;
is_keyword("local") -> true;
is_keyword("nil") -> true;
is_keyword("not") -> true;
is_keyword("or") -> true;
is_keyword("repeat") -> true;
is_keyword("return") -> true;
is_keyword("then") -> true;
is_keyword("true") -> true;
is_keyword("until") -> true;
is_keyword("while") -> true;
is_keyword(_) -> false.