Pārlūkot izejas kodu

removed Std.chr, Std.ord
changed resources handling (haxe.Resource)

Nicolas Cannasse 17 gadi atpakaļ
vecāks
revīzija
8d331ea120

+ 2 - 0
doc/CHANGES.txt

@@ -47,6 +47,8 @@ TODO inlining : substitute class+function type parameters in order to have fully
 	flash9 : bugfix, generated interfaces were empty
 	fixed bug while writing block-vars in flash/js
 	added parameters default value (constants)
+	removed Std.resource, Std.ord, Std.chr
+	added haxe.Resource, allow binary data in resources
 
 2008-04-05: 1.19
 	fixed flash9 Array.toString

+ 14 - 7
genjs.ml

@@ -72,11 +72,12 @@ let rec concat ctx s f = function
 		concat ctx s f l
 
 let fun_block ctx f p =
+	let e = (match f.tf_expr with { eexpr = TBlock [{ eexpr = TBlock _ } as e] } -> e | e -> e) in
 	let e = List.fold_left (fun e (a,c,t) ->
 		match c with
 		| None | Some TNull -> e
 		| Some c -> Codegen.concat (Codegen.set_default ctx.com a c t p) e
-	) f.tf_expr f.tf_args in
+	) e f.tf_args in
 	if ctx.com.debug then
 		Codegen.stack_block ctx.stack ctx.current (fst ctx.curmethod) e
 	else
@@ -173,7 +174,18 @@ let rec gen_call ctx e el =
 		concat ctx "," (gen_value ctx) params;
 		spr ctx ")";
 	| TLocal "__js__", [{ eexpr = TConst (TString code) }] ->
-		spr ctx code
+		spr ctx (String.concat "\n" (ExtString.String.nsplit code "\r\n"))
+	| TLocal "__resources__", [] ->
+		spr ctx "[";		
+		concat ctx "," (fun (name,data) ->
+			spr ctx "{ ";
+			spr ctx "name : ";
+			gen_constant ctx e.epos (TString name);
+			spr ctx ", data : [";
+			concat ctx "," (fun s -> gen_constant ctx e.epos (TString s)) (ExtString.String.nsplit data "\000");
+			spr ctx "]}"
+		) (Hashtbl.fold (fun name data acc -> (name,data) :: acc) ctx.com.resources []);
+		spr ctx "]";
 	| _ ->
 		gen_value ctx e;
 		spr ctx "(";
@@ -705,11 +717,6 @@ let generate com =
 		print ctx "%s = []" ctx.stack.Codegen.stack_exc_var;
 		newline ctx;
 	end;
-	Hashtbl.iter (fun name data ->
-		if String.contains data '\000' then failwith ("Resource " ^ name ^ " contains \\0 characters that can't be used in JavaScript");
-		print ctx "js.Boot.__res[\"%s\"] = \"%s\"" (Ast.s_escape name) (Ast.s_escape data);
-		newline ctx;
-	) com.resources;
 	print ctx "js.Boot.__init()";
 	newline ctx;
 	List.iter (fun e ->

+ 6 - 5
genneko.ml

@@ -216,6 +216,10 @@ and gen_call ctx p e el =
 			this p;
 			array p (List.map (gen_expr ctx) el)
 		]
+	| TLocal "__resources__", [] ->
+		call p (builtin p "array") (Hashtbl.fold (fun name data acc -> 
+			(EObject [("name",gen_constant ctx e.epos (TString name));("data",(EConst (String data),p))],p) :: acc
+		) ctx.com.resources [])
 	| TField ({ eexpr = TConst TSuper; etype = t },f) , _ ->
 		let c = (match follow t with TInst (c,_) -> c | _ -> assert false) in
 		call p (builtin p "call") [
@@ -670,13 +674,10 @@ let gen_package ctx h t =
 	in
 	loop [] (fst (t_path t))
 
-let gen_boot ctx hres =
-	let loop name data acc = (name , gen_constant ctx Ast.null_pos (TString data)) :: acc in
-	let objres = (EObject (Hashtbl.fold loop hres []),null_pos) in
+let gen_boot ctx =
 	(EBlock [
 		EBinop ("=",field null_pos (gen_type_path null_pos (["neko"],"Boot")) "__classes",ident null_pos "@classes"),null_pos;
 		call null_pos (field null_pos (gen_type_path null_pos (["neko"],"Boot")) "__init") [];
-		EBinop ("=",field null_pos (gen_type_path null_pos (["neko"],"Boot")) "__res",objres),null_pos;
 	],null_pos)
 
 let gen_name ctx acc t =
@@ -747,7 +748,7 @@ let generate com libs =
 	let packs = List.concat (List.map (gen_package ctx h) com.types) in
 	let names = List.fold_left (gen_name ctx) [] com.types in
 	let methods = List.rev (List.fold_left (fun acc t -> gen_type ctx t acc) [] com.types) in
-	let boot = gen_boot ctx com.resources in
+	let boot = gen_boot ctx in
 	let inits = List.map (gen_expr ctx) (List.rev ctx.inits) in
 	let vars = List.concat (List.map (gen_static_vars ctx) com.types) in
 	let e = (EBlock (header :: packs @ methods @ boot :: names @ inits @ vars), null_pos) in

+ 16 - 16
genswf8.ml

@@ -882,6 +882,19 @@ and gen_call ctx e el =
 		write ctx APhysEqual;
 	| TLocal "__unprotect__", [{ eexpr = TConst (TString s) }] ->
 		push ctx [VStr (s,false)]
+	| TLocal "__resources__", [] ->
+		let count = ref 0 in
+		Hashtbl.iter (fun name data ->
+			incr count;
+			push ctx [VStr ("name",false);VStr (name,true);VStr ("data",false)];
+			let parts = ExtString.String.nsplit data "\000" in
+			List.iter (gen_big_string ctx) (List.rev parts);
+			init_array ctx (List.length parts);
+			push ctx [VInt 2];
+			write ctx AObject;
+			ctx.stack_size <- ctx.stack_size - 4;
+		) ctx.com.resources;
+		init_array ctx !count
 	| _ , _ ->
 		let nargs = List.length el in
 		List.iter (gen_expr ctx true) (List.rev el);
@@ -1342,7 +1355,7 @@ let gen_type_def ctx t =
 	| TTypeDecl _ ->
 		()
 
-let gen_boot ctx hres =
+let gen_boot ctx =
 	(* r0 = Boot *)
 	getvar ctx (gen_path ctx (["flash"],"Boot") (!extern_boot));
 	write ctx (ASetReg 0);
@@ -1352,20 +1365,7 @@ let gen_boot ctx hres =
 	write ctx AEval;
 	push ctx [VInt 1; VReg 0; VStr ("__init",false)];
 	call ctx VarObj 0;
-	write ctx APop;
-	(* r0.__res = hres *)
-	push ctx [VReg 0; VStr ("__res",false)];
-	let count = ref 0 in
-	Hashtbl.iter (fun name data ->
-		if String.contains data '\000' then failwith ("Resource " ^ name ^ " contains \\0 character than can't be used in Flash");
-		push ctx [VStr (name,true)];
-		gen_big_string ctx data;
-		incr count;
-	) hres;
-	push ctx [VInt (!count)];
-	write ctx AObject;
-	ctx.stack_size <- ctx.stack_size - (!count * 2);
-	write ctx AObjSet
+	write ctx APop
 
 let gen_movieclip ctx m =
 	getvar ctx (gen_path ctx m false);
@@ -1467,7 +1467,7 @@ let generate com =
 	ctx.reg_count <- 0;
 	(* ---- *)
 	List.iter (fun t -> gen_type_def ctx t) com.types;
-	gen_boot ctx com.resources;
+	gen_boot ctx;
 	List.iter (fun m -> gen_movieclip ctx m) ctx.movieclips;
 	let global_try = gen_try ctx in
 	List.iter (gen_expr ctx false) (List.rev ctx.inits);

+ 11 - 21
genswf9.ml

@@ -1123,6 +1123,17 @@ and gen_call ctx retval e el =
 		gen_expr ctx true e;
 		gen_expr ctx true f;
 		write ctx (HOp A3OIn)
+	| TLocal "__resources__", [] ->
+		let count = ref 0 in
+		Hashtbl.iter (fun name data ->
+			incr count;
+			write ctx (HString "name");
+			write ctx (HString name);
+			write ctx (HString "data");
+			write ctx (HString data);
+			write ctx (HObject 2);
+		) ctx.com.resources;
+		write ctx (HArray !count)
 	| TArray ({ eexpr = TLocal "__global__" },{ eexpr = TConst (TString s) }), _ ->
 		(match gen_access ctx e Read with
 		| VGlobal id ->
@@ -1703,23 +1714,6 @@ let generate_type ctx t =
 	| TTypeDecl _ ->
 		None
 
-let generate_resources ctx =
-	write ctx HGetGlobalScope;
-	write ctx (HGetProp (type_path ctx ([],ctx.boot)));
-	let id = type_path ctx (["flash";"utils"],"Dictionary") in
-	write ctx (HFindPropStrict id);
-	write ctx (HConstructProperty (id,0));
-	let r = alloc_reg ctx (KType id) in
-	set_reg ctx r;
-	Hashtbl.iter (fun name data ->
-		write ctx (HReg r.rid);
-		write ctx (HString name);
-		write ctx (HString data);
-		setvar ctx VArray false;
-	) ctx.com.resources;
-	write ctx (HReg r.rid);
-	write ctx (HInitProp (ident "__res"))
-
 let generate_inits ctx types =
 	let f = begin_fun ctx [] t_void [ethis] false null_pos in
 	let slot = ref 0 in
@@ -1770,10 +1764,6 @@ let generate_inits ctx types =
 	write ctx HRetVoid;
 	write ctx (HFunction (finit()));
 	write ctx (HInitProp (ident "init"));
-
-	(* generate resources *)
-	generate_resources ctx;
-
 	write ctx HRetVoid;
 	{
 		hls_method = f();

+ 0 - 52
std/Std.hx

@@ -125,40 +125,6 @@ class Std {
 		#end
 	}
 
-	/**
-		Convert a character code into the corresponding single-char String.
-	**/
-	public static function chr( x : Int ) : String {
-		return String.fromCharCode(x);
-	}
-
-	/**
-		Return the character code of the first character of the String, or null if the String is empty.
-	**/
-	public static function ord( x : String ) : Null<Int> {
-		#if flash
-		if( x == "" )
-			return null;
-		else
-			return x.charCodeAt(0);
-		#elseif neko
-		untyped {
-			var s = __dollar__ssize(x.__s);
-			if( s == 0 )
-				return null;
-			else
-				return __dollar__sget(x.__s,0);
-		}
-		#elseif js
-		if( x == "" )
-			return null;
-		else
-			return x.charCodeAt(0);
-		#else
-		return null;
-		#end
-	}
-
 	/**
 		Return a random integer between 0 included and x excluded.
 	**/
@@ -177,24 +143,6 @@ class Std {
 		#end
 	}
 
-	/**
-		Return the given resource stored using -res, or null if not defined.
-	**/
-	public static function resource( name : String ) : String {
-		return untyped
-		#if as3gen
-		throw "Not supported in AS3";
-		#elseif flash
-		flash.Boot.__res[name];
-		#elseif neko
-		__dollar__objget(neko.Boot.__res,__dollar__hash(name.__s));
-		#elseif js
-		js.Boot.__res[name];
-		#else
-		null;
-		#end
-	}
-
 	/**
 		Initialization the things needed for reflection
 	**/

+ 0 - 4
std/flash9/Boot.hx

@@ -30,10 +30,6 @@ class Boot extends flash.display.MovieClip, implements Dynamic {
 	static var tf : flash.text.TextField;
 	static var lines : Array<String>;
 
-	#if !as3gen
-	static var __res : flash.utils.Dictionary;
-	#end
-
 	public static var skip_constructor = false;
 
 	public function new(?mc:flash.display.MovieClip) {

+ 1 - 1
std/haxe/io/BytesBuffer.hx

@@ -90,7 +90,7 @@ class BytesBuffer {
 	public function getBytes() : Bytes untyped {
 		#if neko
 		var str = StringBuf.__string(b);
-		var bytes =  new Bytes(__dollar__ssize(str),str);
+		var bytes = new Bytes(__dollar__ssize(str),str);
 		#elseif flash9
 		var bytes = new Bytes(b.length,b);
 		b.position = 0;

+ 1 - 1
std/haxe/io/BytesOutput.hx

@@ -118,7 +118,7 @@ class BytesOutput extends Output {
 
 	#end
 
-	public function getBytes() {
+	public function getBytes() : Bytes {
 		#if flash9
 		var bytes = b;
 		b = null;

+ 1 - 1
std/haxe/remoting/SocketProtocol.hx

@@ -144,7 +144,7 @@ class SocketProtocol {
 		o.writeString(msg);
 		o.writeByte(0);
 		#else
-		socket.send(Std.chr(e.c1)+Std.chr(e.c2)+msg);
+		socket.send(String.fromCharCode(e.c1)+String.fromCharCode(e.c2)+msg);
 		#end
 	}
 

+ 20 - 24
std/js/Lib.hx

@@ -48,31 +48,27 @@ class Lib {
 
 	static function __init__() untyped {
 		#if debug
-		__js__('
-			onerror = function(msg,url,line) {
-				var stack = $s.copy();
-				var f = js.Lib.onerror;
-				$s.splice(0,$s.length);
-				if( f == null ) {
-					var i = stack.length;
-					var s = "";
-					while( --i >= 0 )
-						s += "Called from "+stack[i]+"\\n";
-					alert(msg+"\\n\\n"+s);
-					return false;
-				}
-				return f(msg,stack);
-			}
-		');
+__js__('onerror = function(msg,url,line) {
+		var stack = $s.copy();
+		var f = js.Lib.onerror;
+		$s.splice(0,$s.length);
+		if( f == null ) {
+			var i = stack.length;
+			var s = "";
+			while( --i >= 0 )
+				s += "Called from "+stack[i]+"\\n";
+			alert(msg+"\\n\\n"+s);
+			return false;
+		}
+		return f(msg,stack);
+	}');
 		#else
-		__js__('
-			onerror = function(msg,url,line) {
-				var f = js.Lib.onerror;
-				if( f == null )
-					return false;
-				return f(msg,[url+":"+line]);
-			}
-		');
+__js__('onerror = function(msg,url,line) {
+		var f = js.Lib.onerror;
+		if( f == null )
+			return false;
+		return f(msg,[url+":"+line]);
+	}');
 		#end
 	}
 

+ 2 - 1
tests/unit/Test.hx

@@ -168,8 +168,9 @@ class Test #if swf_mark implements mt.Protect #end {
 			new TestIO(),
 			new TestLocals(),
 			new TestSerialize(),
-			new TestRemoting(),
 			new TestMisc(),
+			new TestResource(),
+//			new TestRemoting(),
 		];
 		var current = null;
 		try {

+ 1 - 1
tests/unit/TestIO.hx

@@ -14,8 +14,8 @@ class TestIO extends Test {
 		eq( b.length, 12 );
 		b.set(1,0);
 
-
 		var o = new haxe.io.BytesOutput();
+
 		o.bigEndian = endian;
 		eq(o.bigEndian,endian);
 

+ 4 - 2
tests/unit/TestReflect.hx

@@ -134,8 +134,10 @@ class TestReflect extends Test {
 	}
 
 	function testConv() {
-		eq( Std.chr(65), "A" );
-		eq( Std.ord("A"), 65 );
+		eq( String.fromCharCode(65), "A" );
+		unspec(function() String.fromCharCode(1024));
+		eq( "A".charCodeAt(0), 65 );
+		eq( "".charCodeAt(0), null );
 		eq( Std.int(65), 65 );
 		eq( Std.int(65.456), 65 );
 		eq( Std.int(-65.456), -65 );

+ 47 - 0
tests/unit/TestResource.hx

@@ -0,0 +1,47 @@
+package unit;
+
+class TestResource extends Test {
+
+	static var STR = "Héllo World !";
+
+	function testResources() {
+		var b = haxe.io.Bytes.ofString("Héllo World    !");
+		for( i in 0...b.length )
+			if( b.get(i) == " ".charCodeAt(0) )
+				b.set(i,0);
+		var names = haxe.Resource.listNames();
+		eq( names.length, 2 );
+		if( names[0] == "res1.txt" )
+			eq( names[1], "res2.bin" );
+		else {
+			eq( names[0], "res2.bin" );
+			eq( names[1], "res1.txt" );
+		}
+		eq( haxe.Resource.getString("res1.txt"), STR );
+		#if (neko || flash9)
+		// allow binary strings
+		eq( haxe.Resource.getString("res2.bin"), "Héllo\000World\000\000\000\000!" );
+		#else
+		// cut until first \0
+		eq( haxe.Resource.getString("res2.bin"), "Héllo" );
+		#end
+		eq( haxe.Resource.getBytes("res1.txt").compare(haxe.io.Bytes.ofString(STR)), 0 );
+		eq( haxe.Resource.getBytes("res2.bin").compare(b), 0 );
+	}
+
+	#if neko
+	static function main() {
+		var ch = neko.io.File.write("res1.txt",true);
+		ch.writeString(STR);
+		ch.close();
+		var ch = neko.io.File.write("res2.bin",true);
+		ch.writeString("Héllo");
+		ch.writeByte(0);
+		ch.writeString("World");
+		ch.writeInt31(0);
+		ch.writeString("!");
+		ch.close();
+	}
+	#end
+
+}

+ 1 - 0
tests/unit/res1.txt

@@ -0,0 +1 @@
+Héllo World !

BIN
tests/unit/res2.bin


+ 9 - 1
tests/unit/unit.hxml

@@ -3,6 +3,8 @@
 -main unit.Test
 -debug
 -cp ..
+-resource res1.txt
+-resource res2.bin
 
 --next
 # Flash9
@@ -11,19 +13,25 @@
 -main unit.Test
 -debug
 -cp ..
+-resource res1.txt
+-resource res2.bin
 
 --next
 # JS
 -js unit.js
 unit.Test
 -cp ..
+-resource res1.txt
+-resource res2.bin
 
 --next
 # Neko
 -neko unit.n
 -main unit.Test
--debug
 -cp ..
+-resource res1.txt
+-resource res2.bin
+
 
 --next
 # RemotingServer

+ 14 - 4
tests/unit/unit.hxp

@@ -1,8 +1,17 @@
 <haxe selected="0">
-  <output name="Flash" mode="swf" out="unit8.swf" class="unit.Test" lib="" cmd="http://dev.unit-tests/unit.html" main="True" debug="True">-cp ..</output>
-  <output name="Flash9" mode="swf9" out="unit9.swf" class="unit.Test" lib="" cmd="" main="True" debug="True">-cp ..</output>
-  <output name="JS" mode="js" out="unit.js" class="unit.Test" lib="" cmd="" main="False" debug="False">-cp ..</output>
-  <output name="Neko" mode="neko" out="unit.n" class="unit.Test" lib="" cmd="" main="True" debug="True">-cp ..</output>
+  <output name="Flash" mode="swf" out="unit8.swf" class="unit.Test" lib="" cmd="http://dev.unit-tests/unit.html" main="True" debug="True">-cp ..
+-resource res1.txt
+-resource res2.bin</output>
+  <output name="Flash9" mode="swf9" out="unit9.swf" class="unit.Test" lib="" cmd="" main="True" debug="True">-cp ..
+-resource res1.txt
+-resource res2.bin</output>
+  <output name="JS" mode="js" out="unit.js" class="unit.Test" lib="" cmd="" main="False" debug="False">-cp ..
+-resource res1.txt
+-resource res2.bin</output>
+  <output name="Neko" mode="neko" out="unit.n" class="unit.Test" lib="" cmd="" main="True" debug="False">-cp ..
+-resource res1.txt
+-resource res2.bin
+</output>
   <output name="RemotingServer" mode="neko" out="remoting.n" class="unit.RemotingServer" lib="" cmd="" main="True" debug="False">-cp ..</output>
   <files path="/">
     <file path="MyClass.hx" />
@@ -18,6 +27,7 @@
     <file path="TestMisc.hx" />
     <file path="TestReflect.hx" />
     <file path="TestRemoting.hx" />
+    <file path="TestResource.hx" />
     <file path="TestSerialize.hx" />
   </files>
 </haxe>