Ver código fonte

support Array @:runtime fields at runtime (closes #3071)

frabbit 11 anos atrás
pai
commit
12d963e861
2 arquivos alterados com 66 adições e 12 exclusões
  1. 10 12
      genpy.ml
  2. 56 0
      tests/unit/issues/Issue3071.hx

+ 10 - 12
genpy.ml

@@ -1337,6 +1337,11 @@ module Printer = struct
 		let do_default () =
 			Printf.sprintf "%s.%s" obj (if is_extern then name else (handle_keywords name))
 		in
+		let call_override s =
+			match s with
+			| "iterator" | "toUpperCase" | "toLowerCase" | "pop" | "shift" | "join" | "push" -> true
+			| _ -> false
+		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) ->
@@ -1347,17 +1352,10 @@ module Printer = struct
 				Printf.sprintf "HxString.fromCharCode"
 			| FInstance _ | FStatic _ ->
 				do_default ()
-			| FAnon cf when name = "iterator" && not is_assign ->
-				begin match follow cf.cf_type with
-					| TFun([],_) ->
-						Printf.sprintf "python_lib_FuncTools.partial(HxOverrides.iterator, %s)" obj
-					| _ ->
-						do_default()
-				end
-			| FAnon cf when name = "shift" && not is_assign ->
+			| FAnon cf when is_assign && call_override(name) ->
 				begin match follow cf.cf_type with
 					| TFun([],_) ->
-						Printf.sprintf "python_lib_FuncTools.partial(HxOverrides.shift, %s)" obj
+						Printf.sprintf "python_lib_FuncTools.partial(HxOverrides.%s, %s)" name obj
 					| _ ->
 						do_default()
 				end
@@ -1506,9 +1504,9 @@ module Printer = struct
 
 	and print_call pctx e1 el =
 		match e1.eexpr, el with
-			| TField(e1,((FAnon {cf_name = "iterator"}) | FDynamic ("iterator"))), [] ->
-				Printf.sprintf "HxOverrides.iterator(%s)" (print_expr pctx e1)
-			| TField(e1,((FAnon {cf_name = ("toUpperCase" | "toLowerCase" as s)}) | FDynamic ("toUpperCase" | "toLowerCase" as s))), [] ->
+			| TField(e1,((FAnon {cf_name = (("join" | "push") as s)}) | FDynamic (("join" | "push") as s))), [x] ->
+				Printf.sprintf "HxOverrides.%s(%s, %s)" s (print_expr pctx e1) (print_expr pctx x)
+			| TField(e1,((FAnon {cf_name = (("iterator" | "toUpperCase" | "toLowerCase" | "pop" | "shift") as s)}) | FDynamic (("iterator" | "toUpperCase" | "toLowerCase" | "pop" | "shift") as s))), [] ->
 				Printf.sprintf "HxOverrides.%s(%s)" s (print_expr pctx e1)
 			| _,_ ->
 				print_call2 pctx e1 el

+ 56 - 0
tests/unit/issues/Issue3071.hx

@@ -0,0 +1,56 @@
+package unit.issues;
+import unit.Test;
+
+class Issue3071 extends Test {
+
+
+    function test() {
+    	// join
+    	var a : { function join (s:String):String; } = [1,2];
+    	eq("1,2", a.join(","));
+
+    	var a : Dynamic = [1,2];
+    	eq("1,2", a.join(","));
+
+    	// pop
+    	var a : { function pop ():Int; } = [1,2];
+    	a.pop();
+    	var a : Array<Int> = cast a;
+    	eq(1, a.length);
+    	eq(a[0], 1);
+
+    	var a : Dynamic = [1,2];
+    	a.pop();
+    	var a : Array<Int> = cast a;
+    	eq(1, a.length);
+    	eq(a[0], 1);
+
+    	// push
+    	var a : { function push (x:Int):Int; } = [];
+    	a.push(1);
+    	var a : Array<Int> = cast a;
+    	eq(1, a.length);
+    	eq(a[0], 1);
+
+    	var a : Dynamic = [];
+    	a.push(1);
+    	var a : Array<Int> = cast a;
+    	eq(1, a.length);
+    	eq(a[0], 1);
+
+    	// shift
+    	var a : { function shift ():Int; } = [1,2];
+    	a.shift();
+    	var a : Array<Int> = cast a;
+    	eq(1, a.length);
+    	eq(a[0], 2);
+
+    	var a : Dynamic = [1,2];
+    	a.shift();
+    	var a : Array<Int> = cast a;
+    	eq(1, a.length);
+    	eq(a[0], 2);
+
+    }
+
+}