瀏覽代碼

demo how continuations could work

Simon Krajewski 1 年之前
父節點
當前提交
b164e3b52e
共有 2 個文件被更改,包括 24 次插入8 次删除
  1. 9 1
      src/compiler/hxb/hxbAbstractReader.ml
  2. 15 7
      src/compiler/hxb/hxbReader.ml

+ 9 - 1
src/compiler/hxb/hxbAbstractReader.ml

@@ -1,10 +1,18 @@
 open Globals
 open HxbReaderApi
+open HxbReader
 
 class virtual hxb_abstract_reader = object(self)
 	inherit hxb_reader_api
 
 	method read_hxb (input : IO.input) =
 		let reader = new HxbReader.hxb_reader in
-		reader#read (self :> hxb_reader_api) input
+		let result = reader#read (self :> hxb_reader_api) HHDR input in
+		let rec loop result = match result with
+			| FullModule m ->
+				m
+			| HeaderOnly(m,cont) ->
+				loop (cont (self :> hxb_reader_api) HEND)
+		in
+		loop result
 end

+ 15 - 7
src/compiler/hxb/hxbReader.ml

@@ -35,6 +35,12 @@ let create_field_reader_context p vars = {
 	vars = vars;
 }
 
+type hxb_reader_result =
+	| FullModule of module_def
+	| HeaderOnly of module_def * hxb_continuation (* HHDR *)
+
+and hxb_continuation = hxb_reader_api -> chunk_kind -> hxb_reader_result
+
 class hxb_reader
 
 = object(self)
@@ -1602,15 +1608,15 @@ class hxb_reader
 		tmonos <- Array.init (self#read_uleb128) (fun _ -> mk_mono());
 		api#make_module path file
 
-	method read (api : hxb_reader_api) (file_ch : IO.input) =
+	method read (api : hxb_reader_api) (stop : chunk_kind) (file_ch : IO.input) =
 		if (Bytes.to_string (IO.nread file_ch 3)) <> "hxb" then
 			raise (HxbFailure "magic");
 		let version = IO.read_byte file_ch in
 		if version <> hxb_version then
 			raise (HxbFailure (Printf.sprintf "version mismatch: hxb version %i, reader version %i" version hxb_version));
-		self#continue api file_ch
+		self#continue file_ch api stop
 
-	method continue (new_api : hxb_reader_api) (file_ch : IO.input) =
+	method continue (file_ch : IO.input) (new_api : hxb_reader_api) stop =
 		api <- new_api;
 		let rec loop () =
 			ch <- file_ch;
@@ -1618,7 +1624,7 @@ class hxb_reader
 			ch <- IO.input_bytes data;
 			match chunk with
 			| HEND ->
-				()
+				FullModule current_module
 			| STRI ->
 				string_pool <- self#read_string_pool;
 				loop()
@@ -1627,7 +1633,10 @@ class hxb_reader
 				loop()
 			| HHDR ->
 				current_module <- self#read_hhdr;
-				loop()
+				if stop = HHDR then
+					HeaderOnly(current_module,self#continue file_ch)
+				else
+					loop()
 			| ANFR ->
 				self#read_anfr;
 				loop()
@@ -1675,6 +1684,5 @@ class hxb_reader
 				self#read_efld;
 				loop()
 		in
-		loop();
-		current_module
+		loop()
 end