浏览代码

[server] clean up native library handling

Simon Krajewski 3 年之前
父节点
当前提交
8a57730245
共有 5 个文件被更改,包括 51 次插入53 次删除
  1. 0 1
      src/compiler/compilationContext.ml
  2. 8 14
      src/compiler/compiler.ml
  3. 4 10
      src/compiler/haxe.ml
  4. 1 1
      src/compiler/server.ml
  5. 38 27
      src/context/commonCache.ml

+ 0 - 1
src/compiler/compilationContext.ml

@@ -35,7 +35,6 @@ type communication = {
 
 and compilation_context = {
 	com : Common.context;
-	mutable on_exit : (unit -> unit) list;
 	mutable messages : Common.compiler_message list;
 	mutable has_next : bool;
 	mutable has_error : bool;

+ 8 - 14
src/compiler/compiler.ml

@@ -72,16 +72,9 @@ let initialize_target ctx com actx =
 				actx.classes <- (Path.parse_path "cpp.cppia.HostClasses" ) :: actx.classes;
 			"cpp"
 		| Cs ->
-			ctx.on_exit <- (fun () ->
-				com.native_libs.net_libs <- [];
-			) :: ctx.on_exit;
 			Dotnet.before_generate com;
 			add_std "cs"; "cs"
 		| Java ->
-			ctx.on_exit <- (fun () ->
-				List.iter (fun java_lib -> java_lib#close) com.native_libs.java_libs;
-				com.native_libs.java_libs <- [];
-			) :: ctx.on_exit;
 			Java.before_generate com;
 			if defined com Define.Jvm then begin
 				add_std "jvm";
@@ -553,10 +546,14 @@ let compile ctx actx =
 	end
 
 let finalize ctx =
-	List.iter (fun f ->
-		f();
-	) ctx.on_exit;
-	ctx.comm.flush ctx
+	ctx.comm.flush ctx;
+	(* In server mode any open libs are closed by the lib_build_task. In offline mode
+	   we should do it here to be safe. *)
+	if not ctx.comm.is_server then begin
+		List.iter (fun lib -> lib#close) ctx.com.native_libs.java_libs;
+		List.iter (fun lib -> lib#close) ctx.com.native_libs.net_libs;
+		List.iter (fun lib -> lib#close) ctx.com.native_libs.swf_libs;
+	end
 
 open DisplayTypes
 
@@ -710,8 +707,6 @@ let compile_ctx server_api comm ctx =
 	let run ctx =
 		server_api.before_anything ctx;
 		setup_common_context ctx;
-		(* TODO: deal with this a bit better *)
-		if not comm.is_server then Common.raw_define ctx.com "haxe.noNativeLibsCache";
 		compile_safe ctx (fun () ->
 			let actx = Args.parse_args ctx.com in
 			begin match actx.server_mode with
@@ -757,7 +752,6 @@ let compile_ctx server_api comm ctx =
 
 let create_context compilation_step comm cs params = {
 	com = Common.create compilation_step cs version params;
-	on_exit = [];
 	messages = [];
 	has_next = false;
 	has_error = false;

+ 4 - 10
src/compiler/haxe.ml

@@ -47,14 +47,8 @@ let other = Timer.timer ["other"];;
 Sys.catch_break true;
 
 let args = List.tl (Array.to_list Sys.argv) in
-(try
-	let server = Sys.getenv "HAXE_COMPILATION_SERVER" in
-	let host, port = (try ExtString.String.split server ":" with _ -> "127.0.0.1", server) in
-	Server.do_connect host (try int_of_string port with _ -> failwith "Invalid HAXE_COMPILATION_SERVER port") args
-with Not_found ->
-	set_binary_mode_out stdout true;
-	set_binary_mode_out stderr true;
-	let sctx = ServerCompilationContext.create false in
-	Server.process sctx (Communication.create_stdio ()) args;
-);
+set_binary_mode_out stdout true;
+set_binary_mode_out stderr true;
+let sctx = ServerCompilationContext.create false in
+Server.process sctx (Communication.create_stdio ()) args;
 other()

+ 1 - 1
src/compiler/server.ml

@@ -215,7 +215,7 @@ module Communication = struct
 					maybe_cache_context sctx ctx.com;
 			)
 		);
-		is_server = false;
+		is_server = true;
 	}
 end
 

+ 38 - 27
src/context/commonCache.ml

@@ -2,6 +2,28 @@ open Globals
 open Common
 open Type
 
+class lib_build_task cs file ftime lib = object(self)
+	inherit CompilationCache.server_task ["build_lib";lib#get_name] 60
+
+	method private execute =
+		(* Created lookup and eagerly read each known type. *)
+		lib#load;
+		let h = Hashtbl.create 0 in
+		List.iter (fun path ->
+			if not (Hashtbl.mem h path) then begin
+				let p = { pfile = file ^ " @ " ^ Globals.s_type_path path; pmin = 0; pmax = 0; } in
+				try begin match lib#build path p with
+				| Some r -> Hashtbl.add h path r
+				| None -> ()
+				end with _ ->
+					()
+			end
+		) lib#list_modules;
+		lib#close;
+		(* Save and set up lookup. *)
+		cs#add_native_lib file h ftime;
+end
+
 let handle_native_lib com lib =
 	com.native_libs.all_libs <- lib#get_file_path :: com.native_libs.all_libs;
 	com.load_extern_type <- com.load_extern_type @ [lib#get_file_path,lib#build];
@@ -9,41 +31,30 @@ let handle_native_lib com lib =
 		let cs = com.cs in
 		let init () =
 			let file = lib#get_file_path in
-			let key = file in
 			let ftime = file_time file in
-			begin match cs#get_native_lib key with
+			begin match cs#get_native_lib file with
 			| Some lib when ftime <= lib.c_nl_mtime ->
 				(* Cached lib is good, set up lookup into cached files. *)
-				lib.c_nl_files;
+				Some lib.c_nl_files;
 			| _ ->
-				(* Cached lib is outdated or doesn't exist yet, read library. *)
-				lib#load;
-				(* Created lookup and eagerly read each known type. *)
-				let h = Hashtbl.create 0 in
-				List.iter (fun path ->
-					if not (Hashtbl.mem h path) then begin
-						let p = { pfile = file ^ " @ " ^ Globals.s_type_path path; pmin = 0; pmax = 0; } in
-						try begin match lib#build path p with
-						| Some r -> Hashtbl.add h path r
-						| None -> ()
-						end with _ ->
-							()
-					end
-				) lib#list_modules;
-				(* Save and set up lookup. *)
-				cs#add_native_lib key h ftime;
-				h;
+				(* Cached lib is outdated or doesn't exist yet, register build task. *)
+				cs#add_task (new lib_build_task cs file ftime lib);
+				None
 			end;
 		in
 		(fun () ->
 			let lut = init() in
-			let build path p =
-				try Some (Hashtbl.find lut path)
-				with Not_found -> None
-			in
-			com.load_extern_type <- List.map (fun (name,f) ->
-				name,if name = lib#get_file_path then build else f
-			) com.load_extern_type
+			match lut with
+			| Some lut ->
+				let build path p =
+					try Some (Hashtbl.find lut path)
+					with Not_found -> None
+				in
+				com.load_extern_type <- List.map (fun (name,f) ->
+					name,if name = lib#get_file_path then build else f
+				) com.load_extern_type
+			| None ->
+				lib#load
 		)
 	end else
 		(* Offline mode, just read library as usual. *)