Skip to content

Commit

Permalink
[hxb] detect need for local context in nested anons
Browse files Browse the repository at this point in the history
  • Loading branch information
kLabz committed Mar 22, 2024
1 parent c2724bf commit 66491f6
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/compiler/hxb/hxbWriter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ type hxb_writer = {
mutable local_type_parameters : (typed_type_param,unit) IdentityPool.t;
mutable field_stack : unit list;
mutable wrote_local_type_param : bool;
mutable needs_local_context : bool;
unbound_ttp : (typed_type_param,unit) IdentityPool.t;
t_instance_chunk : Chunk.t;
}
Expand Down Expand Up @@ -1006,12 +1007,11 @@ module HxbWriter = struct
write_pos writer v.v_pos

let rec write_anon writer (an : tanon) =
let needs_local_context = ref false in
let write_fields () =
let restore = start_temporary_chunk writer 256 in
let i = ref 0 in
PMap.iter (fun _ cf ->
write_anon_field_ref writer needs_local_context cf;
write_anon_field_ref writer cf;
incr i;
) an.a_fields;
let bytes = restore (fun new_chunk -> Chunk.get_bytes new_chunk) in
Expand All @@ -1035,8 +1035,7 @@ module HxbWriter = struct
assert false
| AbstractStatics _ ->
assert false
end;
!needs_local_context
end

and write_anon_ref writer (an : tanon) =
let pfm = Option.get (writer.anon_id#identify_anon ~strict:true an) in
Expand All @@ -1046,9 +1045,10 @@ module HxbWriter = struct
Chunk.write_uleb128 writer.chunk index
with Not_found ->
let restore = start_temporary_chunk writer 256 in
let needs_local_context = write_anon writer an in
writer.needs_local_context <- false;
write_anon writer an;
let bytes = restore (fun new_chunk -> Chunk.get_bytes new_chunk) in
if needs_local_context then begin
if writer.needs_local_context then begin
let index = Pool.add writer.anons pfm.pfm_path None in
Chunk.write_u8 writer.chunk 1;
Chunk.write_uleb128 writer.chunk index;
Expand All @@ -1059,7 +1059,7 @@ module HxbWriter = struct
Chunk.write_uleb128 writer.chunk index;
end

and write_anon_field_ref writer needs_local_context cf =
and write_anon_field_ref writer cf =
try
let index = HashedIdentityPool.get writer.anon_fields cf.cf_name cf in
Chunk.write_u8 writer.chunk 0;
Expand All @@ -1075,7 +1075,7 @@ module HxbWriter = struct
(* If we access something from the method scope, we have to write the anon field immediately.
This should be fine because in such cases the field cannot be referenced elsewhere. *)
let index = HashedIdentityPool.add writer.anon_fields cf.cf_name cf None in
needs_local_context := true;
writer.needs_local_context <- true;
Chunk.write_u8 writer.chunk 1;
Chunk.write_uleb128 writer.chunk index;
Chunk.write_bytes writer.chunk bytes
Expand Down Expand Up @@ -1565,7 +1565,7 @@ module HxbWriter = struct
| TField(e1,FAnon cf) ->
Chunk.write_u8 writer.chunk 104;
loop e1;
write_anon_field_ref writer (ref false) cf;
write_anon_field_ref writer cf;
true;
| TField(e1,FClosure(Some(c,tl),cf)) ->
Chunk.write_u8 writer.chunk 105;
Expand All @@ -1577,7 +1577,7 @@ module HxbWriter = struct
| TField(e1,FClosure(None,cf)) ->
Chunk.write_u8 writer.chunk 106;
loop e1;
write_anon_field_ref writer (ref false) cf;
write_anon_field_ref writer cf;
true;
| TField(e1,FEnum(en,ef)) ->
Chunk.write_u8 writer.chunk 107;
Expand Down Expand Up @@ -2312,6 +2312,7 @@ let create config warn anon_id =
local_type_parameters = IdentityPool.create ();
field_stack = [];
wrote_local_type_param = false;
needs_local_context = false;
unbound_ttp = IdentityPool.create ();
t_instance_chunk = Chunk.create EOM cp 32;
}
Expand Down

0 comments on commit 66491f6

Please sign in to comment.