Browse Source

add support for KwArgs, VarArgs, implement makeVarArgs, still failing unit test

frabbit 11 years ago
parent
commit
879ce5b70a
3 changed files with 51 additions and 5 deletions
  1. 20 4
      genpy.ml
  2. 3 1
      std/python/_std/Reflect.hx
  3. 28 0
      tests/unit/TestPython.hx

+ 20 - 4
genpy.ml

@@ -90,6 +90,7 @@ module Transformer = struct
 		let s = Type.s_expr_pretty "\t" s_type e in
 		Printf.printf "%s\n" s
 
+
 	let new_counter () =
 		let n = ref (-1) in
 		(fun () ->
@@ -775,7 +776,7 @@ module Transformer = struct
 			transform_expr ~is_value:is_value e
 		| (is_value, TCast(e,t)) ->
 			let e = trans is_value [] e in
-			let r = { a_expr with eexpr = e.a_expr.eexpr } in
+			let r = { a_expr with eexpr = e.a_expr.eexpr; etype = a_expr.etype } in
 			lift_expr ~blocks:e.a_blocks r
 		| (_, TField(e,f)) ->
 			let e = trans true [] e in
@@ -783,7 +784,7 @@ module Transformer = struct
 			lift_expr ~blocks:e.a_blocks r
 		| (is_value, TMeta(m,e)) ->
 			let e = trans is_value [] e in
-			let r = { a_expr with eexpr = TMeta(m, e.a_expr) } in
+			let r = { a_expr with eexpr = TMeta(m, e.a_expr); etype = e.a_expr.etype } in
 			lift_expr ~blocks:e.a_blocks r
 		| ( _, TPatMatch _ ) -> assert false
 		| ( _, TLocal _ ) -> lift_expr a_expr
@@ -917,7 +918,8 @@ module Printer = struct
 		let sl = List.map (fun (v,cto) ->
 			let name = handle_keywords v.v_name in
 			let arg_string = match follow v.v_type with
-				| TAbstract({a_path = [],"KwdArgs"},_) -> "**" ^ name
+				| TAbstract({a_path = ["python"; "lib"],"KwArgs"},_) -> "**" ^ name
+				| TAbstract({a_path = ["python"; "lib"],"VarArgs"},_) -> "*" ^ name
 				| _ -> name
 			in
 			let arg_value = match cto with
@@ -1266,7 +1268,7 @@ module Printer = struct
 				in
 				print_expr pctx {e1 with eexpr = TBinop(OpEq,e2,e3)} *)
 			| _,el ->
-				Printf.sprintf "%s(%s)" id (print_exprs pctx ", " el)
+				Printf.sprintf "%s(%s)" id (print_call_args pctx e1 el)
 
 	and print_call pctx e1 el =
 		match e1.eexpr with
@@ -1275,6 +1277,20 @@ module Printer = struct
 			| _ ->
 				print_call2 pctx e1 el
 
+	and print_call_args pctx e1 el =
+		let print_arg pctx i x =
+			let prefix = match e1.eexpr, follow x.etype with
+				(* the should not apply for the instance methods of the abstract itself *)
+				| TField(_, FStatic({cl_path = ["python"; "lib"; "_Types"],"KwArgs_Impl_"},f)), _ when i == 0 && Meta.has Meta.Impl f.cf_meta -> ""
+				| TField(_, FStatic({cl_path = ["python"; "lib"; "_Types"],"VarArgs_Impl_"},f)), _ when i == 0 && Meta.has Meta.Impl f.cf_meta -> ""
+				| _, TAbstract({a_path = ["python"; "lib"],"KwArgs"},_) -> "**"
+				| _, TAbstract({a_path = ["python"; "lib"],"VarArgs"},_) -> "*"
+				| _, _ -> ""
+			in
+			prefix ^ (print_expr pctx x)
+		in
+		String.concat "," (ExtList.List.mapi (print_arg pctx) el)
+
 	and print_exprs pctx sep el =
 		String.concat sep (List.map (print_expr pctx) el)
 

+ 3 - 1
std/python/_std/Reflect.hx

@@ -181,7 +181,9 @@ class Reflect {
 
 	@:overload(function( f : Array<Dynamic> -> Void ) : Dynamic {})
 	public static function makeVarArgs( f : Array<Dynamic> -> Dynamic ) : Dynamic {
-		return throw "not implemented";
+		return function (v:VarArgs) {
+			return f((v:Array<Dynamic>));
+		}
 	}
 
 }

+ 28 - 0
tests/unit/TestPython.hx

@@ -1,5 +1,6 @@
 package unit;
 
+import python.lib.Types.KwArgs;
 import sys.io.File;
 import sys.io.Process;
 
@@ -124,4 +125,31 @@ class TestPython extends Test {
 	}
 	*/
 
+
+	function testMakeVarArgs () {
+		var f = function (a:Array<Dynamic>) {
+			return a[0] + a[1];
+		}
+		var g = Reflect.makeVarArgs(f);
+		var res = g(1,2);
+		eq(3, res);
+	}
+
+	function testKwArgs () {
+		function x (args:python.lib.Types.KwArgs) {
+			var a = args.get("a", 0);
+			var b = args.get("b", 0);
+			return a + b;
+		}
+
+
+		var res = x( python.lib.Types.Dict.fromObject({ "a" : 1, "b" : 2}) );
+
+
+		eq(3, res);
+
+		var res2 = python.Syntax.callNamedUntyped(x, { a : 3, b : 5});
+
+		eq(8, res2);
+	}
 }