Skip to content

Commit

Permalink
Added __newindex handling to all object types when setting keys
Browse files Browse the repository at this point in the history
  • Loading branch information
rvirding committed Feb 26, 2022
1 parent 7789f30 commit 4fdcb90
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
13 changes: 11 additions & 2 deletions src/luerl_heap.erl
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,17 @@ set_table_key(Tab, nil=Key, _, St) ->
{error,{illegal_index,Tab,Key},St};
set_table_key(#tref{}=Tref, Key, Val, St) ->
set_table_key_key(Tref, Key, Val, St);
set_table_key(Tab, Key, _, St) ->
{error,{illegal_index,Tab,Key},St}.
set_table_key(Other, Key, Val, St) ->
Meta = get_metamethod(Other, <<"__newindex">>, St),
%% io:format("stk ~p ~p ~p -> ~p\n", [Other,Key,Val,aMeta]),
case Meta of
nil ->
{error,{illegal_index,Other,Key},St};
Meth when ?IS_FUNCTION(Meth) ->
{meta,Meth,[Other,Key,Val],St};
Meth -> %Recurse down the metatable
set_table_key(Meth, Key, Val, St)
end.

set_table_key_key(#tref{i=N}=Tab, Key, Val, #luerl{tabs=Tst0}=St) ->
Ts0 = Tst0#tstruct.data,
Expand Down
8 changes: 4 additions & 4 deletions src/luerl_lib.erl
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ badarg_error(What, Args, St) -> lua_error({badarg,What,Args}, St).

format_error({badarg,Where,As}) ->
io_lib:format("badarg in ~w: ~w", [Where,As]);
format_error({illegal_index,Where,I}) ->
io_lib:format("invalid index in ~w: ~w", [Where,I]);
format_error({illegal_index,Where,Index}) ->
io_lib:format("invalid index in ~w: ~w", [Where,Index]);
format_error({illegal_value,Where,Val}) ->
io_lib:format("invalid value in ~w: ~w", [Where,Val]);
format_error({illegal_value,Val}) ->
Expand All @@ -67,8 +67,8 @@ format_error({invalid_order,Where}) -> %Keep text!
io_lib:format("invalid order function in ~w", [Where]);
format_error({undefined_function,Name}) ->
io_lib:format("undefined function ~w", [Name]);
format_error({undefined_method,Obj,Name}) ->
io_lib:format("undefined method in ~w: ~w", [Obj,Name]);
format_error({undefined_method,Object,Name}) ->
io_lib:format("undefined method in ~w: ~w", [Object,Name]);
%% Pattern errors.
format_error(invalid_pattern) -> %Keep text!
io_lib:format("malformed pattern", []);
Expand Down
2 changes: 1 addition & 1 deletion src/luerl_lib_basic.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
-export([install/1]).

%% Export some functions which can be called from elsewhere.
-export([print/2,tostring/1,tostring/2]).
-export([print/2,tostring/1,tostring/2,type/1]).

-import(luerl_lib, [lua_error/2,badarg_error/3]). %Shorten these

Expand Down

0 comments on commit 4fdcb90

Please sign in to comment.