Skip to content

Commit

Permalink
add stdClass, dynamic attributes creation and remove dead code from e…
Browse files Browse the repository at this point in the history
…php_class
  • Loading branch information
manuel-rubio committed Feb 25, 2017
1 parent e675736 commit 30c90dc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
38 changes: 27 additions & 11 deletions src/ephp_class.erl
Expand Up @@ -15,16 +15,16 @@
get/2,
get_constructor/1,
get_destructor/1,
get_attribute/3,
get_attribute/2,
get_method/2,
get_method/3,
get_const/2,
get_const/3,

register_class/3,
set_alias/3,
instance/5
instance/5,

add_if_no_exists_attrib/2
]).

%% ------------------------------------------------------------------
Expand All @@ -34,6 +34,7 @@
start_link() ->
Ref = make_ref(),
erlang:put(Ref, dict:new()),
ok = register_stdclass(Ref),
{ok, Ref}.

destroy(Classes) ->
Expand Down Expand Up @@ -88,6 +89,7 @@ instance(Ref, LocalCtx, GlobalCtx, RawClassName, Line) ->
{ok, #class{name=ClassName}=Class} ->
{ok, Ctx} = ephp_context:start_link(),
ephp_context:set_global(Ctx, GlobalCtx),
% FIXME: looks like the increment is global for all of the instances
InsCount = Class#class.instance_counter + 1,
RegClass = #reg_instance{
id=InsCount,
Expand Down Expand Up @@ -143,10 +145,6 @@ get_destructor(#class{methods=Methods}) ->
ClassMethod
end.

get_attribute(Ref, ClassName, AttributeName) ->
{ok, Class} = get(Ref, ClassName),
get_attribute(Class, AttributeName).

get_attribute(#class{attrs=Attrs}, AttributeName) ->
case lists:keyfind(AttributeName, #class_attr.name, Attrs) of
false ->
Expand All @@ -155,10 +153,6 @@ get_attribute(#class{attrs=Attrs}, AttributeName) ->
ClassAttr
end.

get_method(Ref, ClassName, MethodName) ->
{ok, Class} = get(Ref, ClassName),
get_method(Class, MethodName).

get_method(#class{methods=Methods,line=Index}, MethodName) ->
case lists:keyfind(MethodName, #class_method.name, Methods) of
false ->
Expand All @@ -178,6 +172,28 @@ get_const(#class{constants=Const}, ConstName) ->
error -> ConstName
end.

add_if_no_exists_attrib(#class{attrs=Attrs}=Class, Name) ->
case get_attribute(Class, Name) of
undefined ->
Class#class{
attrs=Attrs ++ [#class_attr{name = Name}]
};
_ ->
Class
end.

%% ------------------------------------------------------------------
%% Private functions
%% ------------------------------------------------------------------

register_stdclass(Ref) ->
Name = <<"stdClass">>,
Classes = erlang:get(Ref),
StdClass = #class{
name = Name,
static_context = undefined,
constants = dict:new(),
attrs = []
},
erlang:put(Ref, dict:store(Name, StdClass, Classes)),
ok.
18 changes: 12 additions & 6 deletions src/ephp_vars.erl
Expand Up @@ -114,7 +114,8 @@ search(#variable{name=Root, idx=[NewRoot|Idx], line=Line}, Vars, Context) ->
undefined
end.

change(#variable{name = <<"GLOBALS">>, idx=[]}, Value, _Vars) when ?IS_ARRAY(Value) ->
change(#variable{name = <<"GLOBALS">>, idx=[]}, Value, _Vars)
when ?IS_ARRAY(Value) ->
ephp_array:fold(fun(Root, Val, NewVars) ->
ephp_array:store(Root, Val, NewVars)
end, ephp_array:new(), Value);
Expand All @@ -135,14 +136,19 @@ change(#variable{name=Root, idx=[]}=_Var, Value, Vars) ->
end;

change(#variable{name=Root, idx=[{object,NewRoot,_Line}]}=_Var, Value, Vars) ->
{ok, #reg_instance{context=Ctx}} = ephp_array:find(Root, Vars),
{ok, #reg_instance{context=Ctx}=RI} = ephp_array:find(Root, Vars),
Class = ephp_class:add_if_no_exists_attrib(RI#reg_instance.class, NewRoot),
NewVars = ephp_array:store(Root, RI#reg_instance{class=Class}, Vars),
ephp_context:set(Ctx, #variable{name=NewRoot}, Value),
Vars;
NewVars;

change(#variable{name=Root, idx=[{object,NewRoot,_Line}|Idx]}=_Var, Value, Vars) ->
{ok, #reg_instance{context=Ctx}} = ephp_array:find(Root, Vars),
change(#variable{name=Root, idx=[{object,NewRoot,_Line}|Idx]}=_Var,
Value, Vars) ->
{ok, #reg_instance{context=Ctx}=RI} = ephp_array:find(Root, Vars),
Class = ephp_class:add_if_no_exists_attrib(RI#reg_instance.class, NewRoot),
NewVars = ephp_array:store(Root, RI#reg_instance{class=Class}, Vars),
ephp_context:set(Ctx, #variable{name=NewRoot, idx=Idx}, Value),
Vars;
NewVars;

change(#variable{name=Root, idx=[NewRoot|Idx]}=_Var, Value, Vars) ->
case ephp_array:find(Root, Vars) of
Expand Down

0 comments on commit 30c90dc

Please sign in to comment.