Browse Source

optimize this.field

Simon Krajewski 1 year ago
parent
commit
af48042698
2 changed files with 25 additions and 3 deletions
  1. 15 2
      src/compiler/hxb/hxbReader.ml
  2. 10 1
      src/compiler/hxb/hxbWriter.ml

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

@@ -27,12 +27,14 @@ type field_reader_context = {
 	t_pool : Type.t Array.t;
 	pos : pos ref;
 	vars : tvar Array.t;
+	mutable tthis : Type.t;
 }
 
 let create_field_reader_context p ts vars = {
 	t_pool = ts;
 	pos = ref p;
 	vars = vars;
+	tthis = t_dynamic;
 }
 
 type hxb_reader_result =
@@ -970,7 +972,9 @@ class hxb_reader
 				match IO.read_byte ch with
 					(* values 0-19 *)
 					| 0 -> TConst TNull
-					| 1 -> TConst TThis
+					| 1 ->
+						fctx.tthis <- t;
+						TConst TThis
 					| 2 -> TConst TSuper
 					| 3 -> TConst (TBool false)
 					| 4 -> TConst (TBool true)
@@ -1158,11 +1162,20 @@ class hxb_reader
 						TField(e1,FDynamic s)
 
 					| 110 ->
-						let p = self#read_pos in
+						read_relpos ();
+						let p = !(fctx.pos) in
 						let c = self#read_class_ref in
 						let cf = self#read_field_ref in
 						let e1 = Texpr.Builder.make_static_this c p in
 						TField(e1,FStatic(c,cf))
+					| 111 ->
+						read_relpos ();
+						let p = !(fctx.pos) in
+						let c = self#read_class_ref in
+						let tl = self#read_types in
+						let cf = self#read_field_ref in
+						let ethis = mk (TConst TThis) fctx.tthis p in
+						TField(ethis,FInstance(c,tl,cf))
 
 					(* module types 120-139 *)
 					| 120 -> TTypeExpr (TClassDecl self#read_class_ref)

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

@@ -405,6 +405,7 @@ type field_writer_context = {
 	t_pool : (bytes,bytes) pool;
 	t_rings : t_rings;
 	pos_writer : pos_writer;
+	mutable texpr_this : texpr option;
 	vars : (int,tvar) pool;
 }
 
@@ -412,6 +413,7 @@ let create_field_writer_context pos_writer = {
 	t_pool = new pool;
 	t_rings = new t_rings 5;
 	pos_writer = pos_writer;
+	texpr_this = None;
 	vars = new pool;
 }
 
@@ -1255,6 +1257,7 @@ class hxb_writer
 				| TNull ->
 					self#write_texpr_byte 0;
 				| TThis ->
+					fctx.texpr_this <- Some e;
 					self#write_texpr_byte 1;
 				| TSuper ->
 					self#write_texpr_byte 2;
@@ -1413,6 +1416,12 @@ class hxb_writer
 				in
 				self#write_enum_field_ref en ef;
 				chunk#write_uleb128 i;
+			| TField({eexpr = TConst TThis; epos = p1},FInstance(c,tl,cf)) when fctx.texpr_this <> None ->
+				self#write_texpr_byte 111;
+				fctx.pos_writer#write_pos chunk true 0 p1;
+				self#write_class_ref c;
+				self#write_types tl;
+				self#write_field_ref c CfrMember cf;
 			| TField(e1,FInstance(c,tl,cf)) ->
 				self#write_texpr_byte 102;
 				loop e1;
@@ -1421,7 +1430,7 @@ class hxb_writer
 				self#write_field_ref c CfrMember cf;
 			| TField({eexpr = TTypeExpr (TClassDecl c'); epos = p1},FStatic(c,cf)) when c == c' ->
 				self#write_texpr_byte 110;
-				self#write_pos p1;
+				fctx.pos_writer#write_pos chunk true 0 p1;
 				self#write_class_ref c;
 				self#write_field_ref c CfrStatic cf;
 			| TField(e1,FStatic(c,cf)) ->