Skip to content

Commit

Permalink
improve callable to use array as name of the function ([object, name]…
Browse files Browse the repository at this point in the history
…) and other bugfixes
  • Loading branch information
manuel-rubio committed Feb 5, 2019
1 parent eea25d7 commit 31d10a2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/ephp_context.erl
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,16 @@ resolve(#call{name = Object} = Call, State) when ?IS_OBJECT(Object) ->
ephp_error:error({error, enostrfunc, Call#call.line, ?E_ERROR, {}})
end;

resolve(#call{name = Fun} = Call, State) when ?IS_ARRAY(Fun) ->
case ephp_array:to_list(Fun) of
[{_, ObjRef}, {_, Name}] when ?IS_OBJECT(ObjRef) andalso is_binary(Name) ->
RealCall = Call#call{name = Name, type = object},
run_method(ObjRef, RealCall, State);
_ ->
%% FIXME: something more here?
throw({error, implementation})
end;

resolve(#call{name = Fun} = Call, State) when not is_binary(Fun) ->
{Name, NewState} = resolve(Fun, State),
if %% FIXME: only to avoid infinite-loop
Expand Down Expand Up @@ -1480,7 +1490,10 @@ resolve_var(#variable{name = <<"this">>,
resolve_var(#variable{idx = [{object, #call{} = Call, _}]} = Var,
#state{ref = Ref, vars = Vars} = State) ->
InstanceVar = Var#variable{idx = []},
Instance = ephp_vars:get(Vars, InstanceVar, Ref),
Instance = case ephp_vars:get(Vars, InstanceVar, Ref) of
ObjRef when ?IS_OBJECT(ObjRef) -> ObjRef;
MemRef when ?IS_MEM(MemRef) -> ephp_mem:get(MemRef)
end,
#ephp_object{class = Class} = ephp_object:get(Instance),
case ephp_class:get_method(Class, Call#call.line, Call#call.name) of
#class_method{access = public} ->
Expand Down
1 change: 1 addition & 0 deletions test/code/test_callable_array.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Says: [master!] OK
9 changes: 9 additions & 0 deletions test/code/test_callable_array.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

class A {
function b($greet) { print "Says: [$greet] OK\n"; }
}

$a = new A;
$c = [$a, "b"];
$c("master!");

0 comments on commit 31d10a2

Please sign in to comment.