Jelajahi Sumber

[java/cs] Closure enhancements and minor bugfix

Caue Waneck 13 tahun lalu
induk
melakukan
05c3a797a0

+ 14 - 0
gencommon.ml

@@ -7211,6 +7211,20 @@ struct
     in
     closure_fun
     
+  let get_closure_func ctx closure_cl =
+    let gen = ctx.rcf_gen in
+    let basic = gen.gcon.basic in
+    let closure_func eclosure e field is_static =
+      { eclosure with 
+        eexpr = TNew(closure_cl, [], [ 
+          e; 
+          { eexpr = TConst(TString field); etype = basic.tstring; epos = eclosure.epos }
+        ] @ (
+          if ctx.rcf_optimize then [ { eexpr = TConst(TInt (hash_field_i32 ctx eclosure.epos field)); etype = basic.tint; epos = eclosure.epos } ] else [] 
+        ))
+      }
+    in
+    closure_func
   
   (* 
       main expr -> field expr -> field string -> possible set expr -> should_throw_exceptions -> changed expression

+ 5 - 2
gencs.ml

@@ -297,7 +297,7 @@ struct
           
           if needs_cast then mk_cast e.etype ret else ret
         | TCast(expr, _) when is_string e.etype ->
-          { e with eexpr = TCall( mk_static_field_access_infer runtime_cl "toString" expr.epos [], [expr] ) }
+          { e with eexpr = TCall( mk_static_field_access_infer runtime_cl "toString" expr.epos [], [run expr] ) }
         | TBinop( (Ast.OpNotEq as op), e1, e2)
         | TBinop( (Ast.OpEq as op), e1, e2) when is_string e1.etype || is_string e2.etype ->
           let mk_ret e = match op with | Ast.OpNotEq -> { e with eexpr = TUnop(Ast.Not, Ast.Prefix, e) } | _ -> e in
@@ -1482,7 +1482,10 @@ let configure gen =
   
   ReflectionCFs.configure_dynamic_field_access rcf_ctx false;
   
-  let closure_func = ReflectionCFs.implement_closure_cl rcf_ctx ( get_cl (get_type gen (["haxe";"lang"],"Closure")) ) in
+  (* let closure_func = ReflectionCFs.implement_closure_cl rcf_ctx ( get_cl (get_type gen (["haxe";"lang"],"Closure")) ) in *)
+  let closure_cl = get_cl (get_type gen (["haxe";"lang"],"Closure")) in
+  
+  let closure_func = ReflectionCFs.get_closure_func rcf_ctx closure_cl in
   
   ReflectionCFs.implement_varargs_cl rcf_ctx ( get_cl (get_type gen (["haxe";"lang"], "VarArgsBase")) );
   

+ 4 - 18
genjava.ml

@@ -557,7 +557,7 @@ struct
           in
           if need_second_cast then { e with eexpr = TCast(mk_cast (follow e.etype) (run expr), c) }  else Type.map_expr run e*)
         | TCast(expr, _) when is_string e.etype ->
-          { e with eexpr = TCall( mk_static_field_access_infer runtime_cl "toString" expr.epos [], [expr] ) }
+          { e with eexpr = TCall( mk_static_field_access_infer runtime_cl "toString" expr.epos [], [run expr] ) }
           
         | TSwitch(cond, ecases, edefault) when is_string cond.etype ->
           (*let change_string_switch gen eswitch e1 ecases edefault =*)
@@ -1540,24 +1540,10 @@ let configure gen =
   
   ReflectionCFs.configure_dynamic_field_access rcf_ctx false;
   
-  let closure_func = ReflectionCFs.implement_closure_cl rcf_ctx ( get_cl (get_type gen (["haxe";"lang"],"Closure")) ) in
+  (* let closure_func = ReflectionCFs.implement_closure_cl rcf_ctx ( get_cl (get_type gen (["haxe";"lang"],"Closure")) ) in *)
+  let closure_cl = get_cl (get_type gen (["haxe";"lang"],"Closure")) in
   
-  let closure_func eclosure e field is_static =
-    let is_hxobject = match real_type e.etype with
-      | TInst(cl,_) -> is_hxgen (TClassDecl cl)
-      | TEnum(e, _) -> is_hxgen (TEnumDecl e)
-      | TType(t, _) -> is_hxgen (TTypeDecl t)
-      | TDynamic _ | TAnon _ -> false
-      | _ -> assert false
-    in
-    if is_hxobject then 
-      closure_func eclosure e field is_static 
-    else begin
-      let static = mk_static_field_access_infer (runtime_cl) "closure" eclosure.epos [] in
-      let field = { eexpr = TConst(TString field); etype = basic.tstring; epos = eclosure.epos } in
-      mk_cast eclosure.etype { eclosure with eexpr = TCall(static, [ e; field ]); etype = t_dynamic }
-    end
-  in
+  let closure_func = ReflectionCFs.get_closure_func rcf_ctx closure_cl in
   
   ReflectionCFs.implement_varargs_cl rcf_ctx ( get_cl (get_type gen (["haxe";"lang"], "VarArgsBase")) );
   

+ 1 - 5
std/cs/_std/Reflect.hx

@@ -175,11 +175,7 @@ import haxe.lang.Function;
 			haxe.lang.Closure f1c = (haxe.lang.Closure) f1;
 			haxe.lang.Closure f2c = (haxe.lang.Closure) f2;
 			
-			return haxe.lang.Runtime.refEq(f1c.target, f2c.target) && f1c.field.Equals(f2c.field);
-		} else if (f1 is haxe.lang.NativeMethodFunction && f2 is haxe.lang.NativeMethodFunction) {
-			haxe.lang.NativeMethodFunction f1n = (haxe.lang.NativeMethodFunction) f1;
-			haxe.lang.NativeMethodFunction f2n = (haxe.lang.NativeMethodFunction) f2;
-			return haxe.lang.Runtime.refEq(f1n.obj, f2n.obj) && f1n.field.Equals(f2n.field);
+			return haxe.lang.Runtime.refEq(f1c.obj, f2c.obj) && f1c.field.Equals(f2c.field);
 		}
 		
 		return false;

+ 10 - 14
std/cs/_std/haxe/lang/Function.hx

@@ -1,12 +1,11 @@
 package haxe.lang;
 
 /**
- * These classes are automatically generated by the compiler. They are only
- * here so there is an option for e.g. defining them as externs if you are compiling
- * in modules (untested)
- * 
- * @author waneck
- */
+ These classes are automatically generated by the compiler. They are only
+ here so there is an option for e.g. defining them as externs if you are compiling
+ in modules (untested).
+**/
+ 
 @:abstract @:nativegen @:native("haxe.lang.Function") private class Function 
 {
 	function new(arity:Int, type:Int)
@@ -15,11 +14,6 @@ package haxe.lang;
 	}
 }
 
-@:nativegen @:native("haxe.lang.Closure") private class Closure extends Function
-{
-	
-}
-
 @:nativegen @:native("haxe.lang.VarArgsBase") private class VarArgsBase extends Function
 {
 	public function __hx_invokeDynamic(dynArgs:Array<Dynamic>):Dynamic
@@ -44,20 +38,22 @@ package haxe.lang;
 	}
 }
 
-@:nativegen class NativeMethodFunction extends VarArgsBase
+@:nativegen class Closure extends VarArgsBase
 {
 	private var obj:Dynamic;
 	private var field:String;
+	private var hash:Int;
 	
-	public function new(obj, field)
+	public function new(obj, field, hash)
 	{
 		super(-1, -1);
 		this.obj = obj;
 		this.field = field;
+		this.hash = hash;
 	}
 	
 	override public function __hx_invokeDynamic(dynArgs:Array<Dynamic>):Dynamic 
 	{
-		return untyped __cs__("haxe.lang.Runtime.slowCallField(this.obj, this.field, dynArgs)");
+		return Runtime.callField(obj, field, hash, dynArgs);
 	}
 }

File diff ditekan karena terlalu besar
+ 0 - 0
std/cs/_std/haxe/lang/HxObject.hx


+ 7 - 14
std/cs/_std/haxe/lang/Runtime.hx

@@ -1,13 +1,11 @@
 package haxe.lang;
 
 /**
- * ...
- * @author waneck
- */
+ This class is meant for internal compiler use only. It provides the Haxe runtime
+ compatibility to the host language.
+**/
 
 @:nativegen
-//it's private so we don't have access to it in normal haxe code
-@:native('haxe.lang.Runtime')
 @:classContents('
 	public static object getField(haxe.lang.HxObject obj, string field, int fieldHash, bool throwErrors)
 	{
@@ -36,17 +34,12 @@ package haxe.lang;
 		return obj.__hx_invokeField(field, (fieldHash == 0) ? haxe.lang.FieldLookup.hash(field) : fieldHash, false, args);
 	}
 ')
-@:keep private class Runtime 
+@:keep class Runtime 
 {
-	public static var undefined:Dynamic = { };
+	public static var undefined(default, never):Dynamic = { };
 	
 	@:functionBody('
-		if (obj is haxe.lang.IHxObject)
-		{
-			return new haxe.lang.Closure(field, hash, (haxe.lang.IHxObject)obj);
-		} else {
-			return new haxe.lang.NativeMethodFunction(obj, field);
-		}
+		return new haxe.lang.Closure(obj, field, hash);
 	')
 	public static function closure(obj:Dynamic, hash:Int, field:String):Dynamic
 	{
@@ -218,7 +211,7 @@ package haxe.lang;
 				System.Reflection.MethodInfo m = t.GetMethod(field,  System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.FlattenHierarchy | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
 				if (m != null)
 				{
-					return new haxe.lang.NativeMethodFunction(obj, field);
+					return new haxe.lang.Closure(obj, field, 0);
 				} else {
 					if (throwErrors) 
 						throw HaxeException.wrap("Cannot access field \'" + field + "\'.");

+ 1 - 5
std/java/_std/Reflect.hx

@@ -172,11 +172,7 @@ import java.Boot;
 			haxe.lang.Closure f1c = (haxe.lang.Closure) f1;
 			haxe.lang.Closure f2c = (haxe.lang.Closure) f2;
 			
-			return haxe.lang.Runtime.refEq(f1c.target, f2c.target) && f1c.field.equals(f2c.field);
-		} else if (f1 instanceof haxe.lang.NativeMethodFunction && f2 instanceof haxe.lang.NativeMethodFunction) {
-			haxe.lang.NativeMethodFunction f1n = (haxe.lang.NativeMethodFunction) f1;
-			haxe.lang.NativeMethodFunction f2n = (haxe.lang.NativeMethodFunction) f2;
-			return haxe.lang.Runtime.refEq(f1n.obj, f2n.obj) && f1n.field.equals(f2n.field);
+			return haxe.lang.Runtime.refEq(f1c.obj, f2c.obj) && f1c.field.equals(f2c.field);
 		}
 		
 		

+ 3 - 7
std/java/_std/haxe/lang/Function.hx

@@ -1,4 +1,5 @@
 package haxe.lang;
+import haxe.lang.Runtime;
 
 /**
  * These classes are automatically generated by the compiler. They are only
@@ -15,11 +16,6 @@ package haxe.lang;
 	}
 }
 
-@:nativegen @:native("haxe.lang.Closure") private class Closure extends Function
-{
-	
-}
-
 @:nativegen @:native("haxe.lang.VarArgsBase") private class VarArgsBase extends Function
 {
 	public function __hx_invokeDynamic(dynArgs:Array<Dynamic>):Dynamic
@@ -44,7 +40,7 @@ package haxe.lang;
 	}
 }
 
-@:nativegen class NativeMethodFunction extends VarArgsBase
+@:nativegen class Closure extends VarArgsBase
 {
 	private var obj:Dynamic;
 	private var field:String;
@@ -58,6 +54,6 @@ package haxe.lang;
 	
 	override public function __hx_invokeDynamic(dynArgs:Array<Dynamic>):Dynamic 
 	{
-		return untyped __java__("haxe.lang.Runtime.slowCallField(this.obj, this.field, dynArgs)");
+		return Runtime.slowCallField(this.obj, this.field, dynArgs);
 	}
 }

+ 6 - 13
std/java/_std/haxe/lang/Runtime.hx

@@ -1,13 +1,11 @@
 package haxe.lang;
 
 /**
- * ...
- * @author waneck
- */
+ This class is meant for internal compiler use only. It provides the Haxe runtime
+ compatibility to the host language.
+**/
 
 @:nativegen
-//it's private so we don't have access to it in normal haxe code
-@:native('haxe.lang.Runtime')
 @:classContents('
 	public static java.lang.Object getField(haxe.lang.IHxObject obj, java.lang.String field, boolean throwErrors)
 	{
@@ -36,17 +34,12 @@ package haxe.lang;
 		return obj.__hx_invokeField(field, args);
 	}
 ')
-@:keep private class Runtime 
+@:keep class Runtime 
 {
 	public static var undefined:Dynamic = { };
 	
 	@:functionBody('
-		if (obj instanceof haxe.lang.IHxObject)
-		{
-			return new haxe.lang.Closure(field, (haxe.lang.IHxObject)obj);
-		} else {
-			return new haxe.lang.NativeMethodFunction(obj, field);
-		}
+	return new haxe.lang.Closure(obj, field);
 	')
 	public static function closure(obj:Dynamic, field:String):Dynamic
 	{
@@ -270,7 +263,7 @@ package haxe.lang;
 			{
 				if (ms[i].getName().equals(field))
 				{
-					return new haxe.lang.NativeMethodFunction(obj, field);
+					return new haxe.lang.Closure(obj, field);
 				}
 			}
 		} catch (Throwable t2)

Beberapa file tidak ditampilkan karena terlalu banyak file yang berubah dalam diff ini