Ver Fonte

make api an argument of the read function

This is a prerequisite for making the reader reentrant, which should now be possible.
Simon Krajewski há 1 ano atrás
pai
commit
adecf064c7

+ 3 - 3
src/compiler/hxb/hxbAbstractReader.ml

@@ -1,10 +1,10 @@
 open Globals
 open HxbReaderApi
 
-class virtual hxb_abstract_reader (p : pos) = object(self)
+class virtual hxb_abstract_reader = object(self)
 	inherit hxb_reader_api
 
 	method read_hxb (input : IO.input) =
-		let reader = new HxbReader.hxb_reader (self :> hxb_reader_api) in
-		reader#read input true p
+		let reader = new HxbReader.hxb_reader in
+		reader#read (self :> hxb_reader_api) input
 end

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

@@ -3,6 +3,7 @@ open Ast
 open Type
 open HxbData
 open HxbShared
+open HxbReaderApi
 
 (* Debug utils *)
 let no_color = false
@@ -35,9 +36,9 @@ let create_field_reader_context p vars = {
 }
 
 class hxb_reader
-	(api : HxbReaderApi.hxb_reader_api)
-= object(self)
 
+= object(self)
+	val mutable api = Obj.magic ""
 	val mutable current_module = null_module
 	val mutable current_type = None
 	val mutable current_field = null_field
@@ -1601,13 +1602,16 @@ class hxb_reader
 		tmonos <- Array.init (self#read_uleb128) (fun _ -> mk_mono());
 		api#make_module path file
 
-	method read (file_ch : IO.input) (debug : bool) (p : pos) =
+	method read (api : hxb_reader_api) (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
 
+	method continue (new_api : hxb_reader_api) (file_ch : IO.input) =
+		api <- new_api;
 		let rec loop () =
 			ch <- file_ch;
 			let (chunk,data) = self#read_chunk in

+ 1 - 1
src/compiler/hxb/hxbWriter.ml

@@ -300,7 +300,7 @@ let create_field_writer_context pos_writer = {
 	vars = new pool;
 }
 
-class ['a] hxb_writer
+class hxb_writer
 	(display_source_at : Globals.pos -> unit)
 	(anon_id : Type.t Tanon_identification.tanon_identification)
 = object(self)

+ 3 - 4
src/compiler/server.ml

@@ -390,9 +390,8 @@ let check_module sctx ctx m_path m_extra p =
 class hxb_reader_api_server
 	(ctx : Typecore.typer)
 	(cc : context_cache)
-	(p : pos)
 = object(self)
-	inherit HxbAbstractReader.hxb_abstract_reader p
+	inherit HxbAbstractReader.hxb_abstract_reader
 
 	method make_module (path : path) (file : string) =
 		let mc = cc#get_hxb_module path in
@@ -482,7 +481,7 @@ let rec add_modules sctx ctx (m : module_def) (from_binary : bool) (p : pos) =
 							| GoodModule m ->
 								m
 							| BinaryModule mc ->
-								(new hxb_reader_api_server ctx cc p)#read_hxb (IO.input_bytes mc.mc_bytes);
+								(new hxb_reader_api_server ctx cc)#read_hxb (IO.input_bytes mc.mc_bytes);
 							| NoModule ->
 								failwith (Printf.sprintf "Unexpectedly could not find module %s as a dependency of %s" (s_type_path mpath) (s_type_path m0.m_path))
 							| BadModule reason ->
@@ -550,7 +549,7 @@ and type_module sctx (ctx:Typecore.typer) mpath p =
 			   checking dependencies. This means that the actual decoding never has any reason to fail. *)
 			begin match check_module sctx ctx mpath mc.mc_extra p with
 				| None ->
-					let m = (new hxb_reader_api_server ctx cc p)#read_hxb (IO.input_bytes mc.mc_bytes) in
+					let m = (new hxb_reader_api_server ctx cc)#read_hxb (IO.input_bytes mc.mc_bytes) in
 					add_modules true m;
 				| Some reason ->
 					skip mpath reason

+ 2 - 3
src/context/display/displayJson.ml

@@ -107,9 +107,8 @@ end
 class hxb_reader_api_com
 	(com : Common.context)
 	(cc : CompilationCache.context_cache)
-	(p : pos)
 = object(self)
-	inherit HxbAbstractReader.hxb_abstract_reader p
+	inherit HxbAbstractReader.hxb_abstract_reader
 
 	method make_module (path : path) (file : string) =
 		let mc = cc#get_hxb_module path in
@@ -143,7 +142,7 @@ class hxb_reader_api_com
 end
 
 let find_module com cc path p =
-	(new hxb_reader_api_com com cc p)#find_module path
+	(new hxb_reader_api_com com cc)#find_module path
 
 type handler_context = {
 	com : Common.context;

+ 1 - 1
src/typing/typeloadModule.ml

@@ -782,7 +782,7 @@ class hxb_reader_api_typeload
 	(load_module : typer -> path -> pos -> module_def)
 	(p : pos)
 = object(self)
-	inherit HxbAbstractReader.hxb_abstract_reader p
+	inherit HxbAbstractReader.hxb_abstract_reader
 
 	method make_module (path : path) (file : string) =
 		let m = ModuleLevel.make_module ctx path file p in