@@ -69,10 +69,26 @@ let write_char_indice buf c idx =
6969 Buffer. add_char buf c;
7070 write_indice buf idx
7171
72- let write_reftype buf ht =
73- match ht with
74- | Text. Func_ht -> Buffer. add_char buf '\x70'
75- | Extern_ht -> Buffer. add_char buf '\x6F'
72+ let write_reftype buf nullable ht =
73+ match nullable with
74+ | Text. Null -> begin
75+ match ht with
76+ | Text. Extern_ht -> Buffer. add_char buf '\x6F'
77+ | Func_ht -> Buffer. add_char buf '\x70'
78+ | TypeOf (Raw id ) -> write_char_indice buf '\x63' id
79+ | TypeOf _ -> assert false
80+ end
81+ | No_null -> begin
82+ Buffer. add_char buf '\x64' ;
83+ match ht with
84+ | Text. Func_ht -> Buffer. add_char buf '\x70'
85+ | Extern_ht -> Buffer. add_char buf '\x6F'
86+ | TypeOf (Raw id ) -> write_indice buf id
87+ | TypeOf _ -> assert false
88+ end
89+ (* TODO: TypeOf (Text id) Unreachable because there are no text ids in binary
90+ format, the proper way to do it is by redefining ref_type and heap_type for
91+ the binary format but it requires lots of changes. *)
7692
7793let get_char_valtype = function
7894 | Text. Num_type I32 -> '\x7F'
@@ -159,12 +175,12 @@ let write_memory_import buf
159175 write_limits buf limits
160176
161177let write_table_import buf
162- ({ modul_name; name; typ = limits , (_nullable , heaptype ); _ } :
178+ ({ modul_name; name; typ = limits , (nullable , heaptype ); _ } :
163179 Text.Table.Type. t Origin. imported ) =
164180 write_string buf modul_name;
165181 write_string buf name;
166182 Buffer. add_char buf '\x01' ;
167- write_reftype buf heaptype;
183+ write_reftype buf nullable heaptype;
168184 write_limits buf limits
169185
170186let write_func_import buf
@@ -441,7 +457,7 @@ let rec write_instr buf instr =
441457 | I64_extend32_s -> add_char '\xC4'
442458 | Ref_null rt ->
443459 add_char '\xD0' ;
444- write_reftype buf rt
460+ write_reftype buf Text. Null rt
445461 | Ref_is_null -> add_char '\xD1'
446462 | Ref_func idx -> write_char_indice buf '\xD2' idx
447463 | I_trunc_sat_f (S32, S32, S) -> write_fc buf 0
@@ -509,16 +525,16 @@ and write_expr buf expr ~end_op_code =
509525 let end_op_code = Option. value end_op_code ~default: '\x0B' in
510526 Buffer. add_char buf end_op_code
511527
512- let write_table buf { Table. typ = limits , (_nullable , heaptype ); init; _ } =
528+ let write_table buf { Table. typ = limits , (nullable , heaptype ); init; _ } =
513529 match init with
514530 | Some e ->
515531 Buffer. add_char buf '\x40' ;
516532 Buffer. add_char buf '\x00' ;
517- write_reftype buf heaptype;
533+ write_reftype buf nullable heaptype;
518534 write_limits buf limits;
519535 write_expr buf e ~end_op_code: None
520536 | None ->
521- write_reftype buf heaptype;
537+ write_reftype buf nullable heaptype;
522538 write_limits buf limits
523539
524540let write_export buf cid ({ name; id } : Binary.Export.t ) =
@@ -559,7 +575,7 @@ let write_locals buf locals =
559575 Buffer. add_char buf char )
560576 compressed
561577
562- let write_element buf ({ typ = _ , ht ; init; mode; _ } : Elem.t ) =
578+ let write_element buf ({ typ = nullable , ht ; init; mode; _ } : Elem.t ) =
563579 let write_init buf init =
564580 let is_ref_func = ref true in
565581 encode_vector_list buf init (fun buf expr ->
@@ -581,7 +597,7 @@ let write_element buf ({ typ = _, ht; init; mode; _ } : Elem.t) =
581597 end
582598 else begin
583599 write_u32_of_int buf 5 ;
584- write_reftype buf ht;
600+ write_reftype buf nullable ht;
585601 Buffer. add_buffer buf elem_buf
586602 end
587603 | Declarative ->
@@ -594,7 +610,7 @@ let write_element buf ({ typ = _, ht; init; mode; _ } : Elem.t) =
594610 end
595611 else begin
596612 write_u32_of_int buf 7 ;
597- write_reftype buf ht;
613+ write_reftype buf nullable ht;
598614 Buffer. add_buffer buf elem_buf
599615 end
600616 | Active (Some 0 , expr ) ->
@@ -617,7 +633,7 @@ let write_element buf ({ typ = _, ht; init; mode; _ } : Elem.t) =
617633 write_u32_of_int buf 6 ;
618634 write_indice buf i;
619635 write_expr buf expr ~end_op_code: None ;
620- write_reftype buf ht;
636+ write_reftype buf nullable ht;
621637 Buffer. add_buffer buf elem_buf
622638 end
623639 | _ -> assert false
0 commit comments