Explorar el Código

write blocks less awkwardly

also don't read arrays only to turn them into lists
Simon Krajewski hace 1 año
padre
commit
37e5b3746e
Se han modificado 2 ficheros con 15 adiciones y 17 borrados
  1. 3 9
      src/compiler/hxb/hxbReader.ml
  2. 12 8
      src/compiler/hxb/hxbWriter.ml

+ 3 - 9
src/compiler/hxb/hxbReader.ml

@@ -163,8 +163,7 @@ class hxb_reader
 
 	method read_list : 'a . (unit -> 'a) -> 'a list = fun f ->
 		let l = self#read_uleb128 in
-		let a = Array.init l (fun _ -> f ()) in
-		Array.to_list a
+		List.init l (fun _ -> f ())
 
 	method read_option : 'a . (unit -> 'a) -> 'a option = fun f ->
 		match self#read_u8 with
@@ -972,13 +971,8 @@ class hxb_reader
 						let l = IO.read_byte ch in
 						let el = List.init l (fun _ -> loop ()) in
 						TBlock el;
-					| 37 ->
-						let l = IO.read_ui16 ch in
-						let el = List.init l (fun _ -> loop ()) in
-						TBlock el;
-					| 38 ->
-						let l = IO.read_i32 ch in
-						let el = List.init l (fun _ -> loop ()) in
+					| 39 ->
+						let el = self#read_list loop in
 						TBlock el;
 
 					(* function 50-59 *)

+ 12 - 8
src/compiler/hxb/hxbWriter.ml

@@ -299,7 +299,7 @@ class pos_writer
 		chunk#write_leb128 p.pmax;
 
 	method write_pos (offset : int) (p : pos) =
-		if p.pfile <> p_file then begin
+		if p.pfile != p_file then begin
 			(* File changed, write full pos *)
 			chunk#write_u8 (4 + offset);
 			self#do_write_pos p;
@@ -1269,7 +1269,14 @@ class hxb_writer
 			| TBlock [] ->
 				chunk#write_u8 30;
 			| TBlock el ->
-				let l = List.length el in
+				let restore = self#start_temporary_chunk in
+				let i = ref 0 in
+				List.iter (fun e ->
+					incr i;
+					loop e;
+				) el;
+				let bytes = restore (fun new_chunk -> new_chunk#get_bytes) in
+				let l = !i in
 				begin match l with
 				| 1 -> chunk#write_u8 31;
 				| 2 -> chunk#write_u8 32;
@@ -1280,15 +1287,12 @@ class hxb_writer
 					if l <= 0xFF then begin
 						chunk#write_u8 36;
 						chunk#write_u8 l;
-					end else if l < 0xFFFF then begin
-						chunk#write_u8 37;
-						chunk#write_ui16 l;
 					end else begin
-						chunk#write_u8 38;
-						chunk#write_i32 l;
+						chunk#write_u8 39;
+						chunk#write_uleb128 l;
 					end;
 				end;
-				List.iter loop el
+				chunk#write_bytes bytes;
 			(* function 50-59 *)
 			| TFunction tf ->
 				chunk#write_u8 50;