Skip to content

Commit

Permalink
Add checks for whether references to tables and userdata are valid
Browse files Browse the repository at this point in the history
  • Loading branch information
rvirding committed Sep 27, 2024
1 parent c6d9595 commit d26e91c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 18 deletions.
33 changes: 19 additions & 14 deletions src/luerl.hrl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%% Copyright (c) 2013-2019 Robert Virding
%% Copyright (c) 2013-2024 Robert Virding
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -111,10 +111,10 @@
body}). %Code block
-define(IS_LUAFUNC(F), is_record(F, lua_func)).

-record(erl_func,{code}). %Erlang code (fun)
-record(erl_func,{code}). %Erlang code (fun)
-define(IS_ERLFUNC(F), is_record(F, erl_func)).

-record(erl_mfa,{m,f,a}). %Erlang code (MFA)
-record(erl_mfa,{m,f,a}). %Erlang code (MFA)
-define(IS_ERLMFA(F), is_record(F, erl_mfa)).

%% Test if it a function, of either sort.
Expand Down Expand Up @@ -145,28 +145,31 @@
-define(SET_TABLE(N, T, Ts), maps:put(N, T, Ts)).
-define(UPD_TABLE(N, Upd, Ts), maps:update_with(N, Upd, Ts)).
-define(DEL_TABLE(N, Ts), maps:remove(N, Ts)).
-define(CHK_TABLE(N, Ts), maps:is_key(N, Ts)).
-define(FILTER_TABLES(Pred, Ts), maps:filter(Pred, Ts)).
-define(FOLD_TABLES(Fun, Acc, Ts), maps:fold(Fun, Acc, Ts)).
-endif.

-ifdef(TS_USE_ARRAY).
%% Use arrays to handle tables.
%% Use arrays to handle tables. We leave the default value as undefined.
-define(MAKE_TABLE(), array:new()).
-define(GET_TABLE(N, Ar), array:get(N, Ar)).
-define(SET_TABLE(N, T, Ar), array:set(N, T, Ar)).
-define(UPD_TABLE(N, Upd, Ar),
array:set(N, (Upd)(array:get(N, Ar)), Ar)).
array:set(N, (Upd)(array:get(N, Ar)), Ar)).
-define(DEL_TABLE(N, Ar), array:reset(N, Ar)).
-define(CHK_TABLE(N, Ar),
((N >= 0) andalso (array:get(N, Ar) =/= undefined))).
-define(FILTER_TABLES(Pred, Ar),
((fun (___Def) ->
___Fil = fun (___K, ___V) ->
case Pred(___K, ___V) of
true -> ___V;
false -> ___Def
end
end,
array:sparse_map(___Fil, Ar)
end)(array:default(Ar)))).
((fun (___Def) ->
___Fil = fun (___K, ___V) ->
case Pred(___K, ___V) of
true -> ___V;
false -> ___Def
end
end,
array:sparse_map(___Fil, Ar)
end)(array:default(Ar)))).
-define(FOLD_TABLES(Fun, Acc, Ar), array:sparse_foldl(Fun, Acc, Ar)).
-endif.

Expand All @@ -177,6 +180,7 @@
-define(SET_TABLE(N, T, Ts), orddict:store(N, T, Ts)).
-define(UPD_TABLE(N, Upd, Ts), orddict:update(N, Upd, Ts)).
-define(DEL_TABLE(N, Ts), orddict:erase(N, Ts)).
-define(CHK_TABLE(N, Ts), orddict:is_key(N, Ts)).
-define(FILTER_TABLES(Pred, Ts), orddict:filter(Pred, Ts)).
-define(FOLD_TABLES(Fun, Acc, Ts), orddict:fold(Fun, Acc, Ts)).
-endif.
Expand All @@ -188,6 +192,7 @@
-define(SET_TABLE(N, T, Pd), put(N, T)).
-define(UPD_TABLE(N, Upd, Pd), put(N, (Upd)(get(N)))).
-define(DEL_TABLE(N, Pd), erase(N)).
-define(CHK_TABLE(N, Pd), (get(N) =/= undefined)).
-define(FILTER_TABLES(Pred, Pd), Pd). %This needs work
-define(FOLD_TABLES(Fun, Acc, Pd), Pd). %This needs work
-endif.
Expand Down
37 changes: 33 additions & 4 deletions src/luerl_heap.erl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%% Copyright (c) 2020-2023 Robert Virding
%% Copyright (c) 2020-2024 Robert Virding
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,12 +30,12 @@
%% External interface.
-export([gc/1,
alloc_table/1,alloc_table/2,free_table/2,
get_table/2,set_table/3,upd_table/3,
get_table/2,set_table/3,upd_table/3,chk_table/2,
get_global_key/2,set_global_key/3,
get_table_key/3,set_table_key/4,
raw_get_table_key/3,raw_set_table_key/4,
alloc_userdata/2,alloc_userdata/3,
get_userdata/2,set_userdata/3,upd_userdata/3,
alloc_userdata/2,alloc_userdata/3,free_userdata/2,
get_userdata/2,set_userdata/3,upd_userdata/3,chk_userdata/2,
set_userdata_data/3,get_userdata_data/2,
alloc_funcdef/2,get_funcdef/2,set_funcdef/3,
alloc_environment/2,get_env_var/3,set_env_var/4,
Expand Down Expand Up @@ -69,6 +69,7 @@ init_tables(St) ->
%% set_tstruct(Index, Val, #tstruct{}) -> #tstruct{}.
%% upd_tstruct(Index, UpdFun, #tstruct{}) -> #tstruct{}.
%% del_tstruct(Index, #tstruct{}) -> #tstruct{}.
%% chk_tstruct(Index, #tstruct{}) -> ok | error.
%%
%% Functions for accessing tstructs.

Expand Down Expand Up @@ -98,6 +99,12 @@ del_tstruct(N, #tstruct{data=D0,free=Ns}=Tstr) ->
get_tstruct(N, Tstr) ->
?GET_TABLE(N, Tstr#tstruct.data).

chk_tstruct(N, Tstr) ->
case ?CHK_TABLE(N, Tstr#tstruct.data) of
true -> ok;
false -> error
end.

%% alloc_table(State) -> {Tref,State}
%%
%% Allocate an empty table.
Expand Down Expand Up @@ -164,6 +171,13 @@ upd_table(#tref{i=N}, Upd, #luerl{tabs=Tst0}=St) ->
Tst1 = upd_tstruct(N, Upd, Tst0),
St#luerl{tabs=Tst1}.

%% chk_table(Tref, State) -> ok | error.
%%
%% Check the table referenced by Tref actually exists.

chk_table(#tref{i=N}, #luerl{tabs=Tst}) ->
chk_tstruct(N, Tst).

%% set_global_key(Key, Value, State) ->
%% {value,Value,State} | {meta,Method,Args,State} | {error,Error,State}
%%
Expand Down Expand Up @@ -397,6 +411,14 @@ alloc_userdata(Data, Meta, #luerl{usds=Ust0}=St) ->
{N,Ust1} = alloc_tstruct(Ud, Ust0),
{#usdref{i=N},St#luerl{usds=Ust1}}.

%% free_userdata(Usdref, State) -> State
%%
%% Delete a table freeing its space.

free_userdata(#usdref{i=N}, #luerl{usds=Ust0}=St) ->
Ust1 = del_tstruct(N, Ust0),
St#luerl{usds=Ust1}.

%% get_userdata(Usdref, State) -> {UserData,State}
%%
%% Get the userdata refered to by Usdref,
Expand All @@ -422,6 +444,13 @@ upd_userdata(#usdref{i=N}, Upd, #luerl{usds=Ust0}=St) ->
Ust1 = upd_tstruct(N, Upd, Ust0),
St#luerl{usds=Ust1}.

%% chk_userdata(Usdref, State) -> ok | error.
%%
%% Check the userdata referenced by Tref actually exists.

chk_userdata(#usdref{i=N}, #luerl{usds=Ust}) ->
chk_tstruct(N, Ust).

%% get_userdata_data(Usdref, State) -> {Data,State}
%%
%% Get the data form the userdata refered to by Usdref.
Expand Down

0 comments on commit d26e91c

Please sign in to comment.