ソースを参照

added -D check-js-packages

Nicolas Cannasse 17 年 前
コミット
121c201214
2 ファイル変更8 行追加3 行削除
  1. 1 0
      doc/CHANGES.txt
  2. 7 3
      genjs.ml

+ 1 - 0
doc/CHANGES.txt

@@ -29,6 +29,7 @@
 	flash9 : use target file path for Boot unique ID instead of random number
 	as3 : fixed bug when anonymous field was a reserved identifier
 	flash9 : added flash.Lib.vectorOfArray and vectorConvert for flash10
+	added -D check-js-packages to allow several haxe-generated js files in same page
 
 2008-10-04: 2.01
 	fixed php.Sys

+ 7 - 3
genjs.ml

@@ -24,6 +24,7 @@ type ctx = {
 	buf : Buffer.t;
 	packages : (string list,unit) Hashtbl.t;
 	stack : Codegen.stack_context;
+	check_package : bool;
 	mutable current : tclass;
 	mutable statics : (tclass * string * texpr) list;
 	mutable inits : texpr list;
@@ -587,9 +588,11 @@ let generate_package_create ctx (p,_) =
 			Hashtbl.add ctx.packages (p :: acc) ();
 			(match acc with
 			| [] ->
-				print ctx "%s = {}" p;
-			| _ ->
-				print ctx "%s%s = {}" (String.concat "." (List.rev acc)) (field p));
+				if ctx.check_package then print ctx "try { if( %s == null ) %s = {}; } catch(_) { %s = {}; } " p p p else print ctx "%s = {}" p;
+			| _ -> 
+				let p = String.concat "." (List.rev acc) ^ (field p) in
+		        if ctx.check_package then print ctx "if( !%s ) " p;
+				print ctx "%s = {}" p);
 			newline ctx;
 			loop (p :: acc) l
 	in
@@ -703,6 +706,7 @@ let generate com =
 		com = com;
 		stack = Codegen.stack_init com false;
 		buf = Buffer.create 16000;
+		check_package = Common.defined com "check-js-packages";
 		packages = Hashtbl.create 0;
 		statics = [];
 		inits = [];