浏览代码

[cs] changes to start running C# unit tests

Caue Waneck 13 年之前
父节点
当前提交
6444f64472
共有 7 个文件被更改,包括 64 次插入20 次删除
  1. 1 0
      codegen.ml
  2. 8 1
      gencommon.ml
  3. 31 11
      gencs.ml
  4. 4 1
      genjava.ml
  5. 1 1
      main.ml
  6. 19 5
      std/haxe/Resource.hx
  7. 0 1
      std/haxe/io/Bytes.hx

+ 1 - 0
codegen.ml

@@ -1328,6 +1328,7 @@ let fix_override com c f fd =
 						{ e with eexpr = TBlock (v :: el) }
 				);
 			} in
+      let targs = List.map (fun(v,c) -> (v.v_name, Option.is_some c, v.v_type)) nargs in
 			let fde = (match f.cf_expr with None -> assert false | Some e -> e) in
 			{ f with cf_expr = Some { fde with eexpr = TFunction fd2 }; cf_type = TFun(targs,tret) }
 		| Some(f2), None when c.cl_interface ->

+ 8 - 1
gencommon.ml

@@ -2887,6 +2887,13 @@ struct
         end
       in
       
+      let const_type c def =
+        match c with
+          | TString _ -> basic.tstring | TInt _ -> basic.tint
+          | TFloat _ -> basic.tfloat | TBool _ -> basic.tbool
+          | _ -> def
+      in
+      
       let get_args_func args changed_args pos =
         let arity = List.length args in
         let mk_const const elocal t =
@@ -2895,7 +2902,7 @@ struct
             | Some const ->
               { eexpr = TIf( 
                 { elocal with eexpr = TBinop(Ast.OpEq, elocal, null elocal.etype elocal.epos); etype = basic.tbool },
-                { elocal with eexpr = TConst(const); etype = t },
+                { elocal with eexpr = TConst(const); etype = const_type const t },
                 Some ( mk_cast t elocal )
               ); etype = t; epos = elocal.epos }
         in

+ 31 - 11
gencs.ml

@@ -677,16 +677,27 @@ let configure gen =
       | _ -> t_s t
   in
 
+  let escape ichar b =
+    match ichar with
+      | 92 (* \ *) -> Buffer.add_string b "\\\\"
+      | 39 (* ' *) -> Buffer.add_string b "\\\'"
+      | 34 -> Buffer.add_string b "\\\""
+      | 13 (* \r *) -> Buffer.add_string b "\\r"
+      | 10 (* \n *) -> Buffer.add_string b "\\n"
+      | 9 (* \t *) -> Buffer.add_string b "\\t"
+      | c when c < 32 || c >= 127 -> Buffer.add_string b (Printf.sprintf "\\u%.4x" c)
+      | c -> Buffer.add_char b (Char.chr c)
+  in
+  
   let escape s =
     let b = Buffer.create 0 in
-    for i = 0 to String.length s - 1 do
-      match String.unsafe_get s i with
-      | '\\' -> Buffer.add_string b "\\\\"
-      | '\'' -> Buffer.add_string b "\\\'"
-      | '\"' -> Buffer.add_string b "\\\""
-      | c when (Char.code c) < 32 -> Buffer.add_string b (Printf.sprintf "\\x%.2X" (Char.code c))
-      | c -> Buffer.add_char b c
-    done;
+    (try 
+      UTF8.validate s;
+      UTF8.iter (fun c -> escape (UChar.code c) b) s
+    with
+      UTF8.Malformed_code ->
+        String.iter (fun c -> escape (Char.code c) b) s
+    );
     Buffer.contents b
   in
   
@@ -1534,6 +1545,7 @@ let configure gen =
       | TArray(e1, e2) -> 
         ( match follow e1.etype with 
           | TDynamic _ | TAnon _ | TMono _ -> true 
+          | TInst({ cl_kind = KTypeParameter }, _) -> true
           | _ -> false ) 
       | _ -> assert false
   ) "__get" "__set" );
@@ -1737,13 +1749,21 @@ let configure gen =
   CSharpSpecificSynf.configure gen (CSharpSpecificSynf.traverse gen runtime_cl);
   CSharpSpecificESynf.configure gen (CSharpSpecificESynf.traverse gen runtime_cl);
   
+  let mkdir dir = if not (Sys.file_exists dir) then Unix.mkdir dir 0o755 in
+  mkdir (gen.gcon.file ^ "/src");
+  
   (* add resources array *)
   (try
     let res = get_cl (Hashtbl.find gen.gtypes (["haxe"], "Resource")) in
+    mkdir (gen.gcon.file ^ "/src/Resources");
     let cf = PMap.find "content" res.cl_statics in
     let res = ref [] in
-    Hashtbl.iter (fun name _ -> 
-      res := { eexpr = TConst(TString name); etype = gen.gcon.basic.tstring; epos = Ast.null_pos } :: !res
+    Hashtbl.iter (fun name v -> 
+      res := { eexpr = TConst(TString name); etype = gen.gcon.basic.tstring; epos = Ast.null_pos } :: !res;
+      
+      let f = open_out (gen.gcon.file ^ "/src/Resources/" ^ name) in
+      output_string f v;
+      close_out f
     ) gen.gcon.resources;
     cf.cf_expr <- Some ({ eexpr = TArrayDecl(!res); etype = gen.gcon.basic.tarray gen.gcon.basic.tstring; epos = Ast.null_pos })
   with | Not_found -> ());
@@ -1796,7 +1816,7 @@ let configure gen =
 (* end of configure function *)
 	
 let before_generate con = 
-  List.iter (Codegen.fix_overrides con) con.types
+  ()
 
 let generate con =
   (try

+ 4 - 1
genjava.ml

@@ -1787,6 +1787,9 @@ let configure gen =
   let str_cl = match gen.gcon.basic.tstring with | TInst(cl,_) -> cl | _ -> assert false in
   str_cl.cl_super <- Some (get_cl (get_type gen (["haxe";"lang"], "NativeString")), []);
   
+  let mkdir dir = if not (Sys.file_exists dir) then Unix.mkdir dir 0o755 in
+  mkdir (gen.gcon.file ^ "/src");
+  
   (* add resources array *)
   (try
     let res = get_cl (Hashtbl.find gen.gtypes (["haxe"], "Resource")) in
@@ -1814,7 +1817,7 @@ let configure gen =
 (* end of configure function *)
 	
 let before_generate con = 
-  List.iter (Codegen.fix_overrides con) con.types
+  ()
 
 let generate con =
   let gen = new_ctx con in

+ 1 - 1
main.ml

@@ -1045,7 +1045,7 @@ try
 		| Some file ->
 			Common.log com ("Generating xml : " ^ file);
 			Genxml.generate com file);
-		if com.platform = Flash || com.platform = Cpp then List.iter (Codegen.fix_overrides com) com.types;
+		if com.platform = Flash || com.platform = Cpp || com.platform = Cs then List.iter (Codegen.fix_overrides com) com.types;
 		if Common.defined com "dump" then Codegen.dump_types com;
 		t();
 		(match com.platform with

+ 19 - 5
std/haxe/Resource.hx

@@ -26,17 +26,21 @@ package haxe;
 
 class Resource {
 	
-	#if !(java || cs)
-	static var content : Array<{ name : String, data : String, str : String }>;
-	#else
+	#if (java || cs)
 	static var content : Array<String>;
+	#else
+	static var content : Array<{ name : String, data : String, str : String }>;
 	#end
 
 	public static function listNames() : Array<String> {
 		var names = new Array();
-		#if (java || cs)
+		#if java
 		for ( x in content )
 			names.push(x);
+		#elseif cs
+		var all:cs.NativeArray<String> = untyped __cs__("typeof(haxe.Resource).Assembly.GetManifestResourceNames()");
+		for (i in 0...all.Length)
+			names.push(all[i]);
 		#else
 		for ( x in content )
 			names.push(x.name);
@@ -51,6 +55,11 @@ class Resource {
 			return null;
 		var stream = new java.io.NativeInput(stream);
 		return stream.readAll().toString();
+		#elseif cs
+		var str:system.io.Stream = untyped __cs__("typeof(haxe.Resource).Assembly.GetManifestResourceStream(\"Resources.\" + name)");
+		if (str != null)
+			return new cs.io.NativeInput(str).readAll().toString();
+		return null;
 		#else
 		for( x in content )
 			if( x.name == name ) {
@@ -73,6 +82,11 @@ class Resource {
 			return null;
 		var stream = new java.io.NativeInput(stream);
 		return stream.readAll();
+		#elseif cs
+		var str:system.io.Stream = untyped __cs__("typeof(haxe.Resource).Assembly.GetManifestResourceStream(\"Resources.\" + name)");
+		if (str != null)
+			return new cs.io.NativeInput(str).readAll();
+		return null;
 		#else
 		for( x in content )
 			if( x.name == name ) {
@@ -95,7 +109,7 @@ class Resource {
 		content = null;
 		#elseif as3
 		null;
-		#elseif java
+		#elseif (java || cs)
 		//do nothing
 		#else
 		content = untyped __resources__();

+ 0 - 1
std/haxe/io/Bytes.hx

@@ -305,7 +305,6 @@ class Bytes {
 		#elseif cs
 		var b = system.text.Encoding.UTF8.GetBytes(s);
 		return new Bytes(b.Length, b);
-		);
 		#elseif java
 		try
 		{