فهرست منبع

some minor fixes, added some comments that we have to deal with field access on anons in general

frabbit 11 سال پیش
والد
کامیت
40bfa91f2b
2فایلهای تغییر یافته به همراه33 افزوده شده و 19 حذف شده
  1. 27 9
      genpy.ml
  2. 6 10
      std/python/internal/HxOverrides.hx

+ 27 - 9
genpy.ml

@@ -163,6 +163,7 @@ module Transformer = struct
 			e_set_field;
 		]) e_set_field.etype e_set_field.epos
 
+
 	let add_non_locals_to_func e =
 		match e.eexpr with
 		| TFunction f ->
@@ -400,6 +401,7 @@ module Transformer = struct
 				end else
 					transform_exprs_to_block block ae.a_expr.etype false ae.a_expr.epos ae.a_next_id
 			| _ ->
+				debug_expr e1_.a_expr;
 				assert false
 
 	and var_to_treturn_expr ?(capture = false) n t p =
@@ -630,6 +632,29 @@ module Transformer = struct
 
 		| (is_value, TSwitch(e, cases, edef)) ->
 			transform_switch ae is_value e cases edef
+
+		(* anon field access on optional params *)
+		| (_, TField(e,FAnon cf)) when Meta.has Meta.Optional cf.cf_meta ->
+			let e = dynamic_field_read e cf.cf_name in
+			transform_expr e
+		| (_, TBinop(OpAssign,{eexpr = TField(e1,FAnon cf)},e2)) when Meta.has Meta.Optional cf.cf_meta ->
+			let e = dynamic_field_write e1 cf.cf_name e2 in
+			transform_expr e
+		| (_, TBinop(OpAssignOp op,{eexpr = TField(e1,FAnon cf)},e2)) when Meta.has Meta.Optional cf.cf_meta ->
+			let e = dynamic_field_read_write ae.a_next_id e1 cf.cf_name op e2 in
+			transform_expr e
+		(* TODO we need to deal with Increment, Decrement too!
+
+		| (_, TUnop( (Increment | Decrement) as unop, op,{eexpr = TField(e1,FAnon cf)})) when Meta.has Meta.Optional cf.cf_meta  ->
+			let  = dynamic_field_read e cf.cf_name in
+
+			let e = dynamic_field_read_write_unop ae.a_next_id e1 cf.cf_name unop op in
+			Printf.printf "dyn read write\n";
+			transform_expr e
+		*)
+		(*
+			anon field access with non optional members like iterator, length, split must be handled too, we need to Reflect on them too when it's a runtime method
+		*)
 		| (is_value, TUnop( (Increment | Decrement) as unop, op, e)) ->
 			let one = { ae.a_expr with eexpr = TConst(TInt(Int32.of_int(1)))} in
 			let is_postfix = match op with
@@ -644,15 +669,7 @@ module Transformer = struct
 			let e1 = trans true [] e in
 			let r = { a_expr with eexpr = TUnop(op, Prefix, e1.a_expr) } in
 			lift_expr ~blocks:e1.a_blocks r
-		| (_, TField(e,FAnon cf)) when Meta.has Meta.Optional cf.cf_meta ->
-			let e = dynamic_field_read e cf.cf_name in
-			transform_expr e
-		| (_, TBinop(OpAssign,{eexpr = TField(e1,FAnon cf)},e2)) when Meta.has Meta.Optional cf.cf_meta ->
-			let e = dynamic_field_write e1 cf.cf_name e2 in
-			transform_expr e
-		| (_, TBinop(OpAssignOp op,{eexpr = TField(e1,FAnon cf)},e2)) when Meta.has Meta.Optional cf.cf_meta ->
-			let e = dynamic_field_read_write ae.a_next_id e1 cf.cf_name op e2 in
-			transform_expr e
+
 		| (_, TField(e,FDynamic s)) ->
 			let e = dynamic_field_read e s in
 			transform_expr e
@@ -1101,6 +1118,7 @@ module Printer = struct
 			Printf.sprintf "%s.%s" obj (if is_extern then name else (handle_keywords name))
 		in
 		match fa with
+			(* we need to get rid of these cases in the transformer, how is this handled in js *)
 			| FInstance(c,{cf_name = "length" | "get_length"}) when (is_type "" "list")(TClassDecl c) ->
 				Printf.sprintf "_hx_builtin.len(%s)" (print_expr pctx e1)
 			| FInstance(c,{cf_name = "length"}) when (is_type "" "String")(TClassDecl c) ->

+ 6 - 10
std/python/internal/HxOverrides.hx

@@ -7,20 +7,16 @@ import python.Syntax.untypedPython in py;
 @:keep
 @:native("HxOverrides")
 class HxOverrides {
+
+	// this two cases iterator and shift are like all methods in String and Array and are already handled in Reflect
+	// we need to modify the transformer to call Reflect directly
+
 	static public function iterator(x) {
-		if (Std.is(x, Array)) {
-			return Syntax.untypedPython(" _hx_c.python_internal_ArrayImpl.iterator(x)");
-		} else {
-			return Syntax.untypedPython("x.iterator()");
-		}
+		return Reflect.callMethod(null, Reflect.field(x, "iterator"), []);
 	}
 
 	static public function shift(x) {
-		if (Std.is(x, Array)) {
-			return Syntax.untypedPython(" _hx_c.python_internal_ArrayImpl.shift(x)");
-		} else {
-			return Syntax.untypedPython("x.shift()");
-		}
+		return Reflect.callMethod(null, Reflect.field(x, "shift"), []);
 	}
 
 	static public function hx_rshift(val:Int, n:Int) {