Skip to content

Commit

Permalink
added property reflection support for Flash8
Browse files Browse the repository at this point in the history
  • Loading branch information
Simn committed Jul 4, 2012
1 parent 70f2cd3 commit cf4fdad
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
17 changes: 17 additions & 0 deletions codegen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,23 @@ let get_properties fields =
| _ -> acc
) [] fields

let add_property_field com c =
let p = c.cl_pos in
let props = get_properties (c.cl_ordered_statics @ c.cl_ordered_fields) in
match props with
| [] -> ()
| _ ->
let fields,values = List.fold_left (fun (fields,values) (n,v) ->
let cf = mk_field n com.basic.tstring p in
PMap.add n cf fields,(n, string com v p) :: values
) (PMap.empty,[]) props in
let t = mk_anon fields in
let e = mk (TObjectDecl values) t p in
let cf = mk_field "__properties__" t p in
cf.cf_expr <- Some e;
c.cl_statics <- PMap.add cf.cf_name cf c.cl_statics;
c.cl_ordered_statics <- cf :: c.cl_ordered_statics

(* -------------------------------------------------------------------------- *)
(* REMOTING PROXYS *)

Expand Down
2 changes: 2 additions & 0 deletions genswf8.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1411,6 +1411,8 @@ let gen_type_def ctx t =
setvar ctx VarObj;
(* true if implements mt.Protect *)
let flag = is_protected ctx ~stat:true (TInst (c,[])) "" in
if (Common.has_feature ctx.com "Reflect.getProperty") || (Common.has_feature ctx.com "Reflect.setProperty") then
Codegen.add_property_field ctx.com c;
List.iter (gen_class_static_field ctx c flag) c.cl_ordered_statics;
let flag = is_protected ctx (TInst (c,[])) "" in
PMap.iter (fun _ f -> match f.cf_kind with Var { v_read = AccResolve } -> () | _ -> gen_class_field ctx flag f) c.cl_fields;
Expand Down
26 changes: 22 additions & 4 deletions std/flash8/_std/Reflect.hx
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,30 @@
o[field] = value;
}

public static inline function getProperty( o : Dynamic, field : String ) : Dynamic {
return Reflect.field(o,field);
static function findAccessor( c : Class<Dynamic>, name : String ):Dynamic untyped {
do {
var getter = c.__properties__[name];
if (getter != null)
return getter;
c = c.__super__;
} while (c != null);
return null;
}

public static function getProperty( o : Dynamic, field : String ) : Dynamic untyped {
var getter = findAccessor( Std.is(o,Class) ? o : o.__class__, "get_" +field);
return if (getter != null)
o[getter]["apply"](o, [field]);
else
Reflect.field(o, field);
}

public static inline function setProperty( o : Dynamic, field : String, value : Dynamic ) : Void {
setField(o,field,value);
public static inline function setProperty( o : Dynamic, field : String, value : Dynamic ) : Void untyped {
var setter = findAccessor( Std.is(o,Class) ? o : o.__class__, "set_" +field);
return if (setter != null)
o[setter]["apply"](o, [value]);
else
Reflect.setField(o, field, value);
}

public inline static function callMethod( o : Dynamic, func : Dynamic, args : Array<Dynamic> ) : Dynamic untyped {
Expand Down

0 comments on commit cf4fdad

Please sign in to comment.