Bladeren bron

replaced @Main class by common.main expression

Nicolas Cannasse 14 jaren geleden
bovenliggende
commit
20f120a43f
10 gewijzigde bestanden met toevoegingen van 73 en 103 verwijderingen
  1. 2 0
      common.ml
  2. 4 4
      doc/CHANGES.txt
  3. 20 42
      genas3.ml
  4. 20 18
      gencpp.ml
  5. 3 1
      genjs.ml
  6. 4 4
      genneko.ml
  7. 8 12
      genphp.ml
  8. 3 0
      genswf8.ml
  9. 2 1
      main.ml
  10. 7 21
      typer.ml

+ 2 - 0
common.ml

@@ -65,6 +65,7 @@ type context = {
 	mutable file : string;
 	mutable flash_version : float;
 	mutable modules : Type.module_def list;
+	mutable main : Type.texpr option;
 	mutable types : Type.module_type list;
 	mutable resources : (string,string) Hashtbl.t;
 	mutable php_front : string option;
@@ -96,6 +97,7 @@ let create v =
 		file = "";
 		types = [];
 		modules = [];
+		main = None;
 		flash_version = 10.;
 		resources = Hashtbl.create 0;
 		php_front = None;

+ 4 - 4
doc/CHANGES.txt

@@ -33,10 +33,10 @@
 	all : lookup unqualified types in all package hierarchy and not only in current package
 	flash : set default flash version to 10 (-swf9 deprecated, use -swf-version 8 for avm1)
 	php : added --php-lib to allow to rename the destination path of the generated lib
-	all : added --dead-code-elimination, removes unused functions from the output (beta feature
-			could not make in the final release)
-	all : added @:keep, forces a class/method to be not descarded when used with 
-	        --dead-code-elimination
+	all : added --dead-code-elimination, removes unused functions from the output
+	     (beta feature could not make in the final release)
+	all : added @:keep to prevent --dead-code-elimination of class/method
+	flash9 : fixed issues with loading a haXe SWF (boot_XXXX class extends flash.Boot)
 
 2010-08-14: 2.06
 	neko : change serializer to be able to handle instances of basic classes from other modules

+ 20 - 42
genas3.ml

@@ -38,7 +38,6 @@ type context = {
 	mutable locals : (string,string) PMap.t;
 	mutable inv_locals : (string,string) PMap.t;
 	mutable local_types : t list;
-	mutable inits : texpr list;
 	mutable constructor_block : bool;
 }
 
@@ -118,7 +117,6 @@ let init infos path =
 		locals = PMap.empty;
 		inv_locals = PMap.empty;
 		local_types = [];
-		inits = [];
 		get_sets = Hashtbl.create 0;
 		constructor_block = false;
 	}
@@ -821,11 +819,6 @@ and gen_value ctx e =
 		)) e.etype e.epos);
 		v()
 
-let generate_boot_init ctx =
-	print ctx "private static function init() : void {";
-	List.iter (gen_expr ctx) ctx.inits;
-	print ctx "}"
-
 let generate_field ctx static f =
 	newline ctx;
 	ctx.in_static <- static;
@@ -853,9 +846,7 @@ let generate_field ctx static f =
 		newline ctx
 	| _ ->
 		let is_getset = (match f.cf_kind with Var { v_read = AccCall _ } | Var { v_write = AccCall _ } -> true | _ -> false) in
-		if ctx.curclass.cl_path = (["flash"],"Boot") && f.cf_name = "init" then
-			generate_boot_init ctx
-		else if ctx.curclass.cl_interface then
+		if ctx.curclass.cl_interface then
 			match follow f.cf_type with
 			| TFun (args,r) ->
 				print ctx "function %s(" f.cf_name;
@@ -971,24 +962,20 @@ let generate_class ctx c =
 	print ctx "}";
 	newline ctx
 
-let generate_main ctx c =
-	ctx.curclass <- c;
+let generate_main ctx inits =
+	ctx.curclass <- { null_class with cl_path = [],"__main__" };
 	let pack = open_block ctx in
-	print ctx "\tpublic class __main__ extends %s {" (s_path ctx true (["flash";"display"],"MovieClip") c.cl_pos);
+	print ctx "\tpublic class __main__ extends %s {" (s_path ctx true (["flash"],"Boot") Ast.null_pos);
 	let cl = open_block ctx in
 	newline ctx;
-	(match c.cl_ordered_statics with
-	| [{ cf_expr = Some e }] ->
-		spr ctx "public function __main__() {";
-		let f = open_block ctx in
-		newline ctx;
-		print ctx "new %s(this)" (s_path ctx true (["flash"],"Boot") c.cl_pos);
-		newline ctx;
-		gen_value ctx e;
-		f();
-		newline ctx;
-		spr ctx "}";
-	| _ -> assert false);
+	spr ctx "public function __main__() {";
+	let fl = open_block ctx in
+	newline ctx;
+	spr ctx "super()";
+	List.iter (fun e -> newline ctx; gen_expr ctx e) inits;
+	fl();
+	newline ctx;
+	print ctx "}";
 	cl();
 	newline ctx;
 	print ctx "}";
@@ -1064,7 +1051,6 @@ let generate com =
 	let ctx = init infos ([],"enum") in
 	generate_base_enum ctx;
 	close ctx;
-	let boot = ref None in
 	let inits = ref [] in
 	List.iter (fun t ->
 		match t with
@@ -1078,17 +1064,10 @@ let generate com =
 			| Some e -> inits := e :: !inits);
 			if c.cl_extern then
 				()
-			else (match c.cl_path with
-			| [], "@Main" ->
-				let ctx = init infos ([],"__main__") in
-				generate_main ctx c;
-				close ctx;
-			| ["flash"], "Boot" ->
-				boot := Some c;
-			| _ ->
+			else
 				let ctx = init infos c.cl_path in
 				generate_class ctx c;
-				close ctx)
+				close ctx
 		| TEnumDecl e ->
 			let pack,name = e.e_path in
 			let e = { e with e_path = (pack,protect name) } in
@@ -1101,10 +1080,9 @@ let generate com =
 		| TTypeDecl t ->
 			()
 	) com.types;
-	match !boot with
-	| None -> assert false
-	| Some c ->
-		let ctx = init infos c.cl_path in
-		ctx.inits <- List.rev !inits;
-		generate_class ctx c;
-		close ctx
+	(match com.main with
+	| None -> ()
+	| Some e -> inits := e :: !inits);
+	let ctx = init infos ([],"__main__") in
+	generate_main ctx (List.rev !inits);
+	close ctx

+ 20 - 18
gencpp.ml

@@ -2889,24 +2889,18 @@ let generate common_ctx =
 			let name =  class_text class_def.cl_path in
 			(if debug then print_endline ("	ignore generic class " ^ name))
 		| TClassDecl class_def ->
-			(match class_def.cl_path with
-			| [], "@Main" ->
-				main_deps := find_referenced_types common_ctx (TClassDecl class_def) super_deps constructor_deps false;
-				generate_main common_ctx member_types super_deps class_def !boot_classes !init_classes;
-			| _ ->
-				let name =  class_text class_def.cl_path in
-				let is_internal = is_internal_class class_def.cl_path in
-				if (is_internal) then
-					( if debug then print_endline (" internal class " ^ name ))
-				else begin
-					if (not class_def.cl_interface) then
-						boot_classes := class_def.cl_path ::  !boot_classes;
-					if (has_init_field class_def) then
-						init_classes := class_def.cl_path ::  !init_classes;
-					let deps = generate_class_files common_ctx member_types super_deps constructor_deps class_def in
-					exe_classes := (class_def.cl_path, deps)  ::  !exe_classes;
-				end
-			)
+			let name =  class_text class_def.cl_path in
+			let is_internal = is_internal_class class_def.cl_path in
+			if (is_internal) then
+				( if debug then print_endline (" internal class " ^ name ))
+			else begin
+				if (not class_def.cl_interface) then
+					boot_classes := class_def.cl_path ::  !boot_classes;
+				if (has_init_field class_def) then
+					init_classes := class_def.cl_path ::  !init_classes;
+				let deps = generate_class_files common_ctx member_types super_deps constructor_deps class_def in
+				exe_classes := (class_def.cl_path, deps)  ::  !exe_classes;
+			end
 		| TEnumDecl enum_def ->
 			let name =  class_text enum_def.e_path in
 			let is_internal = is_internal_class enum_def.e_path in
@@ -2924,6 +2918,14 @@ let generate common_ctx =
 		);
 	) common_ctx.types;
 
+	(match common_ctx.main with
+	| None -> ()
+	| Some e ->
+		let main_field = { cf_name = "__main__"; cf_type = t_dynamic; cf_expr = Some e; cf_public = true; cf_meta = []; cf_doc = None; cf_kind = Var { v_read = AccNormal; v_write = AccNormal; }; cf_params = [] } in
+		let class_def = { null_class with cl_path = ([],"@Main"); cl_ordered_statics = [main_field] } in
+		main_deps := find_referenced_types common_ctx (TClassDecl class_def) super_deps constructor_deps false;
+		generate_main common_ctx member_types super_deps class_def !boot_classes !init_classes);
+
 	write_resources common_ctx;
 
 

+ 3 - 1
genjs.ml

@@ -37,7 +37,6 @@ type ctx = {
 }
 
 let s_path ctx = function
-	| ([],"@Main") -> "$Main"
 	| ([],p) -> 
 		(match ctx.namespace with
 		| None -> p
@@ -768,6 +767,9 @@ let generate com =
 		newline ctx;
 	) (List.rev ctx.inits);
 	List.iter (generate_static ctx) (List.rev ctx.statics);
+	(match com.main with
+	| None -> ()
+	| Some e -> gen_expr ctx e);
 	let ch = open_out_bin com.file in
 	output_string ch (Buffer.contents ctx.buf);
 	close_out ch;

+ 4 - 4
genneko.ml

@@ -642,7 +642,7 @@ let gen_type ctx t acc =
 		(match c.cl_init with
 		| None -> ()
 		| Some e -> ctx.inits <- (c,e) :: ctx.inits);
-		if c.cl_extern || c.cl_path = ([],"@Main") then
+		if c.cl_extern then
 			acc
 		else
 			gen_class ctx c :: acc
@@ -722,7 +722,7 @@ let gen_name ctx acc t =
 		) in
 		setname :: setconstrs :: meta @ acc
 	| TClassDecl c ->
-		if c.cl_extern || c.cl_path = ([],"@Main") then
+		if c.cl_extern then
 			acc
 		else
 			let p = pos ctx c.cl_pos in
@@ -783,7 +783,6 @@ let header() =
 	in
 	let inits = [
 		"@classes",call p (builtin p "new") [null p];
-		"@Main",call p (builtin p "new") [null p];
 		"@enum_to_string",func [] (call p (fields ["neko";"Boot";"__enum_str"]) [this p]);
 		"@serialize",func [] (call p (fields ["neko";"Boot";"__serialize"]) [this p]);
 		"@tag_serialize",func [] (call p (fields ["neko";"Boot";"__tagserialize"]) [this p]);
@@ -820,7 +819,8 @@ let generate com libs =
 	let t = Common.timer "neko generation" in
 	let libs = (ENeko (generate_libs_init libs) , { psource = "<header>"; pline = 1; }) in	
 	let el = build ctx com.types in
-	let e = (EBlock ((header()) @ libs :: el), null_pos) in
+	let emain = (match com.main with None -> [] | Some e -> [gen_expr ctx e]) in
+	let e = (EBlock ((header()) @ libs :: el @ emain), null_pos) in
 	let neko_file = (try Filename.chop_extension com.file with _ -> com.file) ^ ".neko" in
 	let ch = IO.output_channel (open_out_bin neko_file) in
 	let source = Common.defined com "neko_source" in

+ 8 - 12
genphp.ml

@@ -1861,7 +1861,7 @@ let generate_class ctx c =
 	print ctx "}"
 	
 	
-let createmain com c =
+let createmain com e =
 	let filename = match com.php_front with None -> "index.php" | Some n -> n in
 	let ctx = {
 		com = com;
@@ -1900,10 +1900,7 @@ let createmain com c =
 	newline ctx;
 	newline ctx;
 	spr ctx ("require_once dirname(__FILE__).'/" ^ ctx.lib_path ^ "/php/Boot.class.php';\n\n");
-	(match c.cl_ordered_statics with
-	| [{ cf_expr = Some e }] ->
-		gen_value ctx e;
-	| _ -> assert false);
+	gen_value ctx e;
 	newline ctx;
 	spr ctx "\n?>";
 	close ctx
@@ -2042,10 +2039,7 @@ let generate com =
 					gen_expr ctx e;
 					close ctx;
 					);
-			end else (match c.cl_path with
-			| [], "@Main" ->
-				createmain com c;
-			| _ ->
+			end else
 				let ctx = init com php_lib_path c.cl_path (if c.cl_interface then 2 else 0) in
 				ctx.extern_classes_with_init <- !extern_classes_with_init;
 				ctx.all_dynamic_methods <- !all_dynamic_methods;
@@ -2071,9 +2065,8 @@ let generate com =
 						generate_inline_method ctx c h;
 						loop ctx.inline_methods
 				in
-				loop ctx.inline_methods;
-				
-				close ctx);
+				loop ctx.inline_methods;				
+				close ctx
 		| TEnumDecl e ->
 			if e.e_extern then
 				()
@@ -2084,6 +2077,9 @@ let generate com =
 		| TTypeDecl t ->
 			());
 	) com.types;
+	(match com.main with
+	| None -> ()
+	| Some e -> createmain com e);
 	Hashtbl.iter (fun name data ->
 		write_resource com.file name data
 	) com.resources;

+ 3 - 0
genswf8.ml

@@ -1552,6 +1552,9 @@ let generate com =
 	List.iter (gen_expr ctx false) (List.rev ctx.inits);
 	let global_try = gen_try ctx in
 	List.iter (gen_class_static_init ctx) (List.rev ctx.statics);
+	(match com.main with
+	| None -> ()
+	| Some e -> gen_expr ctx false e);
 	ctx.static_init <- false;
 	let end_try = global_try() in
 	(* flash.Boot.__trace(exc) *)

+ 2 - 1
main.ml

@@ -582,7 +582,8 @@ try
 		t();
 		if !has_error then do_exit();
 		if !no_output then com.platform <- Cross;
-		let types, modules = Typer.generate ctx com.main_class (!excludes) in
+		let main, types, modules = Typer.generate ctx com.main_class (!excludes) in
+		com.main <- main;
 		com.types <- types;
 		com.modules <- modules;
 		com.lines <- Lexer.build_line_index();

+ 7 - 21
typer.ml

@@ -1787,8 +1787,8 @@ let generate ctx main excludes =
 
 	in
 	Hashtbl.iter (fun _ m -> modules := m :: !modules; List.iter loop m.mtypes) ctx.g.modules;
-	(match main with
-	| None -> ()
+	let main = (match main with
+	| None -> None
 	| Some cl ->
 		let t = Typeload.load_type_def ctx null_pos { tpackage = fst cl; tname = snd cl; tparams = []; tsub = None } in
 		let ft, r = (match t with
@@ -1804,24 +1804,10 @@ let generate ctx main excludes =
 			with
 				Not_found -> error ("Invalid -main : " ^ s_type_path cl ^ " does not have static function main") null_pos
 		) in
-		let path = ([],"@Main") in
 		let emain = type_type ctx cl null_pos in
-		let c = mk_class path null_pos in
-		let f = {
-			cf_name = "init";
-			cf_type = r;
-			cf_public = false;
-			cf_kind = Var { v_read = AccNormal; v_write = AccNormal };
-			cf_doc = None;
-			cf_meta = no_meta;
-			cf_params = [];
-			cf_expr = Some (mk (TCall (mk (TField (emain,"main")) ft null_pos,[])) r null_pos);
-		} in
-		c.cl_statics <- PMap.add "init" f c.cl_statics;
-		c.cl_ordered_statics <- f :: c.cl_ordered_statics;
-		types := TClassDecl c :: !types
-	);
-	List.rev !types, List.rev !modules
+		Some (mk (TCall (mk (TField (emain,"main")) ft null_pos,[])) r null_pos);
+	) in
+	main, List.rev !types, List.rev !modules
 
 (* ---------------------------------------------------------------------- *)
 (* MACROS *)
@@ -1936,7 +1922,7 @@ let load_macro ctx cpath f p =
 			ignore(Typeload.load_module ctx2 (["haxe";"macro"],"Expr") p);
 			ignore(Typeload.load_module ctx2 (["haxe";"macro"],"Type") p);
 			finalize ctx2;
-			let types, _ = generate ctx2 None [] in
+			let _, types, _ = generate ctx2 None [] in
 			Interp.add_types mctx types;
 			Interp.init mctx;
 			ctx2
@@ -1952,7 +1938,7 @@ let load_macro ctx cpath f p =
 	let in_macro = ctx.in_macro in
 	if not in_macro then begin
 		finalize ctx2;
-		let types, modules = generate ctx2 None [] in
+		let _, types, modules = generate ctx2 None [] in
 		ctx2.com.types <- types;
 		ctx2.com.Common.modules <- modules;
 		Interp.add_types mctx types;