Browse Source

[cs] Started to convert functionCode functions to actual Haxe code

Cauê Waneck 11 years ago
parent
commit
fcf41f2bc7
2 changed files with 119 additions and 138 deletions
  1. 15 7
      gencs.ml
  2. 104 131
      std/cs/internal/Runtime.hx

+ 15 - 7
gencs.ml

@@ -201,6 +201,10 @@ struct
 		let uint = match get_type gen ([], "UInt") with | TTypeDecl t -> TType(t, []) | TAbstractDecl a -> TAbstract(a, []) | _ -> assert false in
 		let uint = match get_type gen ([], "UInt") with | TTypeDecl t -> TType(t, []) | TAbstractDecl a -> TAbstract(a, []) | _ -> assert false in
 
 
 		let is_var = alloc_var "__is__" t_dynamic in
 		let is_var = alloc_var "__is__" t_dynamic in
+		let name () = match gen.gcurrent_class with
+			| Some cl -> path_s cl.cl_path
+			| _ -> ""
+		in
 
 
 		let rec run e =
 		let rec run e =
 			match e.eexpr with
 			match e.eexpr with
@@ -247,7 +251,7 @@ struct
 
 
 					let obj = run obj in
 					let obj = run obj in
 					(match follow_module follow md with
 					(match follow_module follow md with
-						| TAbstractDecl{ a_path = ([], "Float") } ->
+						| TAbstractDecl{ a_path = ([], "Float") } when name() <> "haxe.lang.Runtime" ->
 							(* on the special case of seeing if it is a Float, we need to test if both it is a float and if it is an Int *)
 							(* on the special case of seeing if it is a Float, we need to test if both it is a float and if it is an Int *)
 							let mk_is local =
 							let mk_is local =
 								(* we check if it float or int or uint *)
 								(* we check if it float or int or uint *)
@@ -258,7 +262,7 @@ struct
 							in
 							in
 							wrap_if_needed obj mk_is
 							wrap_if_needed obj mk_is
 
 
-						| TAbstractDecl{ a_path = ([], "Int") } ->
+						| TAbstractDecl{ a_path = ([], "Int") } when name() <> "haxe.lang.Runtime" ->
 							(* int can be stored in double variable because of anonymous functions, check that case *)
 							(* int can be stored in double variable because of anonymous functions, check that case *)
 							let mk_isint_call local =
 							let mk_isint_call local =
 								{
 								{
@@ -277,7 +281,7 @@ struct
 							in
 							in
 							wrap_if_needed obj mk_is
 							wrap_if_needed obj mk_is
 
 
-						| TAbstractDecl{ a_path = ([], "UInt") } ->
+						| TAbstractDecl{ a_path = ([], "UInt") } when name() <> "haxe.lang.Runtime" ->
 							(* uint can be stored in double variable because of anonymous functions, check that case *)
 							(* uint can be stored in double variable because of anonymous functions, check that case *)
 							let mk_isuint_call local =
 							let mk_isuint_call local =
 								{
 								{
@@ -395,6 +399,10 @@ struct
 		in
 		in
 
 
 		let is_cl t = match gen.greal_type t with | TInst ( { cl_path = (["System"], "Type") }, [] ) -> true | _ -> false in
 		let is_cl t = match gen.greal_type t with | TInst ( { cl_path = (["System"], "Type") }, [] ) -> true | _ -> false in
+		let name () = match gen.gcurrent_class with
+			| Some cl -> path_s cl.cl_path
+			| _ -> ""
+		in
 
 
 		let rec run e =
 		let rec run e =
 			match e.eexpr with
 			match e.eexpr with
@@ -438,7 +446,7 @@ struct
 						etype = basic.tbool;
 						etype = basic.tbool;
 						epos = e.epos
 						epos = e.epos
 					}
 					}
-				| TCast(expr, _) when is_int_float e.etype && not (is_cs_basic_type expr.etype) && not (is_null e.etype) ->
+				| TCast(expr, _) when is_int_float e.etype && not (is_cs_basic_type expr.etype) && not (is_null e.etype) && name() <> "haxe.lang.Runtime" ->
 					let needs_cast = match gen.gfollow#run_f e.etype with
 					let needs_cast = match gen.gfollow#run_f e.etype with
 						| TInst _ -> false
 						| TInst _ -> false
 						| _ -> true
 						| _ -> true
@@ -456,7 +464,7 @@ struct
 					} in
 					} in
 
 
 					if needs_cast then mk_cast e.etype ret else ret
 					if needs_cast then mk_cast e.etype ret else ret
-				| TCast(expr, _) when (is_string e.etype) && (not (is_string expr.etype)) ->
+				| TCast(expr, _) when (is_string e.etype) && (not (is_string expr.etype)) && name() <> "haxe.lang.Runtime" ->
 					{ e with eexpr = TCall( mk_static_field_access_infer runtime_cl "toString" expr.epos [], [run 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.OpNotEq as op), e1, e2)
 				| TBinop( (Ast.OpEq as op), e1, e2) when is_string e1.etype || is_string e2.etype ->
 				| TBinop( (Ast.OpEq as op), e1, e2) when is_string e1.etype || is_string e2.etype ->
@@ -469,7 +477,7 @@ struct
 						}, [ run e1; run e2 ])
 						}, [ run e1; run e2 ])
 					}
 					}
 
 
-				| TCast(expr, _) when is_tparam e.etype ->
+				| TCast(expr, _) when is_tparam e.etype && name() <> "haxe.lang.Runtime" ->
 					let static = mk_static_field_access_infer (runtime_cl) "genericCast" e.epos [e.etype] in
 					let static = mk_static_field_access_infer (runtime_cl) "genericCast" e.epos [e.etype] in
 					{ e with eexpr = TCall(static, [mk_local (alloc_var "$type_param" e.etype) expr.epos; run expr]); }
 					{ e with eexpr = TCall(static, [mk_local (alloc_var "$type_param" e.etype) expr.epos; run expr]); }
 
 
@@ -485,7 +493,7 @@ struct
 					}
 					}
 
 
 				| TBinop ( (Ast.OpEq as op), e1, e2 )
 				| TBinop ( (Ast.OpEq as op), e1, e2 )
-				| TBinop ( (Ast.OpNotEq as op), e1, e2 ) when is_cl e1.etype ->
+				| TBinop ( (Ast.OpNotEq as op), e1, e2 ) when is_cl e1.etype && name() <> "haxe.lang.Runtime" ->
 					let static = mk_static_field_access_infer (runtime_cl) "typeEq" e.epos [] in
 					let static = mk_static_field_access_infer (runtime_cl) "typeEq" e.epos [] in
 					let ret = { e with eexpr = TCall(static, [run e1; run e2]); } in
 					let ret = { e with eexpr = TCall(static, [run e1; run e2]); } in
 					if op = Ast.OpNotEq then
 					if op = Ast.OpNotEq then

+ 104 - 131
std/cs/internal/Runtime.hx

@@ -80,152 +80,130 @@ import cs.system.Object;
 		return null;
 		return null;
 	}
 	}
 
 
-	@:functionCode('
-			if (System.Object.ReferenceEquals(v1, v2))
-				return true;
-			if (v1 == null || v2 == null)
-				return false;
+	public static function eq(v1:Dynamic, v2:Dynamic):Bool
+	{
+		if (Object.ReferenceEquals(v1, v2))
+			return true;
+		if (Object.ReferenceEquals(v1,null) || Object.ReferenceEquals(v2,null))
+			return false;
 
 
-			System.IConvertible v1c = v1 as System.IConvertible;
+		var v1c = Lib.as(v1, IConvertible);
 
 
-			if (v1c != null)
-			{
-				System.IConvertible v2c = v2 as System.IConvertible;
+		if (v1c != null)
+		{
+			var v2c = Lib.as(v2, IConvertible);
 
 
-				if (v2c == null)
-				{
-					return false;
-				}
+			if (v2c == null)
+			{
+				return false;
+			}
 
 
-				System.TypeCode t1 = v1c.GetTypeCode();
-				System.TypeCode t2 = v2c.GetTypeCode();
-				if (t1 == t2)
-					return v1c.Equals(v2c);
+			var t1 = v1c.GetTypeCode(),
+					t2 = v2c.GetTypeCode();
+			if (t1 == t2)
+				return Object._Equals(v1c,v2c);
 
 
-				if (t1 == System.TypeCode.String || t2 == System.TypeCode.String)
-					return false;
+			if (t1 == cs.system.TypeCode.String || t2 == cs.system.TypeCode.String)
+				return false;
 
 
-				switch(t1)
-				{
-					case System.TypeCode.Decimal:
+			switch(t1)
+			{
+				case Decimal:
+					return v1c.ToDecimal(null) == v2c.ToDecimal(null);
+				case Int64:
+				case UInt64:
+					if (t2 == Decimal)
 						return v1c.ToDecimal(null) == v2c.ToDecimal(null);
 						return v1c.ToDecimal(null) == v2c.ToDecimal(null);
-					case System.TypeCode.Int64:
-					case System.TypeCode.UInt64:
-						if (t2 == System.TypeCode.Decimal)
+					else
+						return v1c.ToUInt64(null) == v2c.ToUInt64(null);
+				default:
+					switch(t2)
+					{
+						case Decimal:
 							return v1c.ToDecimal(null) == v2c.ToDecimal(null);
 							return v1c.ToDecimal(null) == v2c.ToDecimal(null);
-						else
-							return v1c.ToUInt64(null) == v2c.ToUInt64(null);
-					default:
-						switch(t2)
-						{
-							case System.TypeCode.Decimal:
+						case Int64:
+						case UInt64:
+							if (t2 == Decimal)
 								return v1c.ToDecimal(null) == v2c.ToDecimal(null);
 								return v1c.ToDecimal(null) == v2c.ToDecimal(null);
-							case System.TypeCode.Int64:
-							case System.TypeCode.UInt64:
-								if (t2 == System.TypeCode.Decimal)
-									return v1c.ToDecimal(null) == v2c.ToDecimal(null);
-								else
-									return v1c.ToUInt64(null) == v2c.ToUInt64(null);
-							default:
-								return v1c.ToDouble(null) == v2c.ToDouble(null);
-						}
-				}
+							else
+								return v1c.ToUInt64(null) == v2c.ToUInt64(null);
+						default:
+							return v1c.ToDouble(null) == v2c.ToDouble(null);
+					}
 			}
 			}
+		}
 
 
-			System.ValueType v1v = v1 as System.ValueType;
-			if (v1v != null)
+		var v1v = Lib.as(v1, cs.system.ValueType);
+		if (v1v != null)
+		{
+			return v1.Equals(v2);
+		} else {
+			var v1t = Lib.as(v1, Type);
+			if (v1t != null)
 			{
 			{
-				return v1.Equals(v2);
-			} else {
-				System.Type v1t = v1 as System.Type;
-				if (v1t != null)
-				{
-					System.Type v2t = v2 as System.Type;
-					if (v2t != null)
-						return typeEq(v1t, v2t);
-					return false;
-				}
+				var v2t = Lib.as(v2, Type);
+				if (v2t != null)
+					return typeEq(v1t, v2t);
+				return false;
 			}
 			}
+		}
 
 
-			return false;
-	')
-	public static function eq(v1:Dynamic, v2:Dynamic):Bool
-	{
 		return false;
 		return false;
 	}
 	}
 
 
-	@:functionCode('
-			if (v1 is System.Type)
-				return typeEq(v1 as System.Type, v2 as System.Type);
-			return System.Object.ReferenceEquals(v1, v2);
-	')
 	public static function refEq(v1: { }, v2: { } ):Bool
 	public static function refEq(v1: { }, v2: { } ):Bool
 	{
 	{
-		return false;
+		if (Std.is(v1, Type))
+			return typeEq(Lib.as(v1,Type), Lib.as(v2,Type));
+		return Object.ReferenceEquals(v1,v2);
 	}
 	}
 
 
-	@:functionCode('
-		return (obj == null) ? 0.0 : (obj is double) ? (double)obj : ((System.IConvertible) obj).ToDouble(null);
-	')
 	public static function toDouble(obj:Dynamic):Float
 	public static function toDouble(obj:Dynamic):Float
 	{
 	{
-		return 0.0;
+		return (obj == null) ? .0 : Std.is(obj,Float) ? cast obj : Lib.as(obj,IConvertible).ToDouble(null);
 	}
 	}
 
 
-	@:functionCode('
-		return (obj == null) ? 0 : (obj is int) ? (int)obj : ((System.IConvertible) obj).ToInt32(null);
-	')
 	public static function toInt(obj:Dynamic):Int
 	public static function toInt(obj:Dynamic):Int
 	{
 	{
-		return 0;
+		return (obj == null) ? 0 : Std.is(obj,Int) ? cast obj : Lib.as(obj,IConvertible).ToInt32(null);
 	}
 	}
 
 
-	@:functionCode('
-			System.IConvertible cv1 = obj as System.IConvertible;
-			if (cv1 != null)
-			{
-                switch (cv1.GetTypeCode())
-                {
-                    case System.TypeCode.Double:
-                        double d = (double)obj;
-
-				        return d >= int.MinValue && d <= int.MaxValue && d == ( (int)d );
-                    case System.TypeCode.UInt32:
-                    case System.TypeCode.Int32:
-                        return true;
-                    default:
-                        return false;
-                }
-
-			}
-			return false;
-	')
 	public static function isInt(obj:Dynamic):Bool
 	public static function isInt(obj:Dynamic):Bool
 	{
 	{
+		var cv1 = Lib.as(obj, IConvertible);
+		if (cv1 != null)
+		{
+			switch (cv1.GetTypeCode())
+			{
+				case Double:
+					var d:Float = cast obj;
+					return d >= cs.system.Int32.MinValue && d <= cs.system.Int32.MaxValue && d == ( cast(d,Int) );
+				case UInt32, Int32:
+					return true;
+				default:
+					return false;
+			}
+		}
 		return false;
 		return false;
 	}
 	}
 
 
-	@:functionCode('
-			System.IConvertible cv1 = obj as System.IConvertible;
-			if (cv1 != null)
-			{
-                switch (cv1.GetTypeCode())
-                {
-                    case System.TypeCode.Double:
-                        double d = (double)obj;
-
-				        return d >= uint.MinValue && d <= uint.MaxValue && d == ( (uint)d );
-                    case System.TypeCode.UInt32:
-                        return true;
-                    default:
-                        return false;
-                }
-
-			}
-			return false;
-	')
 	public static function isUInt(obj:Dynamic):Bool
 	public static function isUInt(obj:Dynamic):Bool
 	{
 	{
+		var cv1 = Lib.as(obj, IConvertible);
+		if (cv1 != null)
+		{
+			switch (cv1.GetTypeCode())
+			{
+				case Double:
+					var d:Float = cast obj;
+					return d >= cs.system.UInt32.MinValue && d <= cs.system.UInt32.MaxValue && d == ( cast(d,UInt) );
+				case UInt32:
+					return true;
+				default:
+					return false;
+			}
+
+		}
 		return false;
 		return false;
 	}
 	}
 
 
@@ -278,28 +256,23 @@ import cs.system.Object;
 		return c1.CompareTo(c2);
 		return c1.CompareTo(c2);
 	}
 	}
 
 
-	@:functionCode('
-			if (v1 is string || v2 is string)
-				return Std.@string(v1) + Std.@string(v2);
+	public static function plus(v1:Dynamic, v2:Dynamic):Dynamic
+	{
+		if (Std.is(v1,String) || Std.is(v2,String))
+			return Std.string(v1) + Std.string(v2);
 
 
-			System.IConvertible cv1 = v1 as System.IConvertible;
-			if (cv1 != null)
+		var cv1 = Lib.as(v1, IConvertible);
+		if (cv1 != null)
+		{
+			var cv2 = Lib.as(v2, IConvertible);
+			if (cv2 == null)
 			{
 			{
-				System.IConvertible cv2 = v2 as System.IConvertible;
-
-				if (cv2 == null)
-				{
-					throw new System.ArgumentException("Cannot dynamically add " + v1.GetType().ToString() + " and " + v2.GetType().ToString());
-				}
-
-				return cv1.ToDouble(null) + cv2.ToDouble(null);
+				throw new cs.system.ArgumentException("Cannot dynamically add " + cs.Lib.nativeType(v1).ToString() + " and " + cs.Lib.nativeType(v2).ToString());
 			}
 			}
+			return cv1.ToDouble(null) + cv2.ToDouble(null);
+		}
 
 
-			throw new System.ArgumentException("Cannot dynamically add " + v1 + " and " + v2);
-	')
-	public static function plus(v1:Dynamic, v2:Dynamic):Dynamic
-	{
-		return null;
+		throw new cs.system.ArgumentException("Cannot dynamically add " + v1 + " and " + v2);
 	}
 	}
 
 
 	@:functionCode('
 	@:functionCode('
@@ -435,7 +408,7 @@ import cs.system.Object;
 		if (methodLength == 0) throw "No available methods";
 		if (methodLength == 0) throw "No available methods";
 		var length = args.length;
 		var length = args.length;
 		var oargs:NativeArray<Dynamic> = new NativeArray(length);
 		var oargs:NativeArray<Dynamic> = new NativeArray(length);
-		var ts:NativeArray<cs.system.Type> = new NativeArray(length);
+		var ts:NativeArray<Type> = new NativeArray(length);
 		var rates:NativeArray<Int> = new NativeArray(methods.Length);
 		var rates:NativeArray<Int> = new NativeArray(methods.Length);
 
 
 		for (i in 0...length)
 		for (i in 0...length)
@@ -567,7 +540,7 @@ import cs.system.Object;
 			return haxe.lang.Null<object>.ofDynamic<object>(obj);
 			return haxe.lang.Null<object>.ofDynamic<object>(obj);
 		return nullableType.GetMethod("_ofDynamic").Invoke(null, new object[] { obj });
 		return nullableType.GetMethod("_ofDynamic").Invoke(null, new object[] { obj });
 	')
 	')
-	public static function mkNullable(obj:Dynamic, nullableType:cs.system.Type):Dynamic
+	public static function mkNullable(obj:Dynamic, nullableType:Type):Dynamic
 	{
 	{
 		return null;
 		return null;
 	}
 	}
@@ -708,7 +681,7 @@ import cs.system.Object;
 			string n2 = Type.getClassName(t2);
 			string n2 = Type.getClassName(t2);
 			return n1.Equals(n2);
 			return n1.Equals(n2);
 	')
 	')
-	public static function typeEq(t1:cs.system.Type, t2:cs.system.Type):Bool
+	public static function typeEq(t1:Type, t2:Type):Bool
 	{
 	{
 		return false;
 		return false;
 	}
 	}