@@ -798,19 +798,19 @@ let id s =
798
798
let bo = peek s in
799
799
Lib.Option. map
800
800
(function
801
- | 0 -> `CustomSection
802
- | 1 -> `TypeSection
803
- | 2 -> `ImportSection
804
- | 3 -> `FuncSection
805
- | 4 -> `TableSection
806
- | 5 -> `MemorySection
807
- | 6 -> `GlobalSection
808
- | 7 -> `ExportSection
809
- | 8 -> `StartSection
810
- | 9 -> `ElemSection
811
- | 10 -> `CodeSection
812
- | 11 -> `DataSection
813
- | 12 -> `DataCountSection
801
+ | 0 -> Custom. Custom
802
+ | 1 -> Custom. Type
803
+ | 2 -> Custom. Import
804
+ | 3 -> Custom. Func
805
+ | 4 -> Custom. Table
806
+ | 5 -> Custom. Memory
807
+ | 6 -> Custom. Global
808
+ | 7 -> Custom. Export
809
+ | 8 -> Custom. Start
810
+ | 9 -> Custom. Elem
811
+ | 10 -> Custom. Code
812
+ | 11 -> Custom. Data
813
+ | 12 -> Custom. DataCount
814
814
| _ -> error s (pos s) " malformed section id"
815
815
) bo
816
816
@@ -828,7 +828,7 @@ let section tag f default s =
828
828
let type_ s = at func_type s
829
829
830
830
let type_section s =
831
- section `TypeSection (vec type_) [] s
831
+ section Custom. Type (vec type_) [] s
832
832
833
833
834
834
(* Import section *)
@@ -848,13 +848,13 @@ let import s =
848
848
{module_name; item_name; idesc}
849
849
850
850
let import_section s =
851
- section `ImportSection (vec (at import)) [] s
851
+ section Custom. Import (vec (at import)) [] s
852
852
853
853
854
854
(* Function section *)
855
855
856
856
let func_section s =
857
- section `FuncSection (vec (at var)) [] s
857
+ section Custom. Func (vec (at var)) [] s
858
858
859
859
860
860
(* Table section *)
@@ -864,7 +864,7 @@ let table s =
864
864
{ttype}
865
865
866
866
let table_section s =
867
- section `TableSection (vec (at table)) [] s
867
+ section Custom. Table (vec (at table)) [] s
868
868
869
869
870
870
(* Memory section *)
@@ -874,7 +874,7 @@ let memory s =
874
874
{mtype}
875
875
876
876
let memory_section s =
877
- section `MemorySection (vec (at memory)) [] s
877
+ section Custom. Memory (vec (at memory)) [] s
878
878
879
879
880
880
(* Global section *)
@@ -885,7 +885,7 @@ let global s =
885
885
{gtype; ginit}
886
886
887
887
let global_section s =
888
- section `GlobalSection (vec (at global)) [] s
888
+ section Custom. Global (vec (at global)) [] s
889
889
890
890
891
891
(* Export section *)
@@ -904,7 +904,7 @@ let export s =
904
904
{name; edesc}
905
905
906
906
let export_section s =
907
- section `ExportSection (vec (at export)) [] s
907
+ section Custom. Export (vec (at export)) [] s
908
908
909
909
910
910
(* Start section *)
@@ -914,7 +914,7 @@ let start s =
914
914
{sfunc}
915
915
916
916
let start_section s =
917
- section `StartSection (opt (at start) true ) None s
917
+ section Custom. Start (opt (at start) true ) None s
918
918
919
919
920
920
(* Code section *)
@@ -939,7 +939,7 @@ let code _ s =
939
939
{locals; body; ftype = - 1l @@ no_region}
940
940
941
941
let code_section s =
942
- section `CodeSection (vec (at (sized code))) [] s
942
+ section Custom. Code (vec (at (sized code))) [] s
943
943
944
944
945
945
(* Element section *)
@@ -1012,7 +1012,7 @@ let elem s =
1012
1012
| _ -> error s (pos s - 1 ) " malformed elements segment kind"
1013
1013
1014
1014
let elem_section s =
1015
- section `ElemSection (vec (at elem)) [] s
1015
+ section Custom. Elem (vec (at elem)) [] s
1016
1016
1017
1017
1018
1018
(* Data section *)
@@ -1034,7 +1034,7 @@ let data s =
1034
1034
| _ -> error s (pos s - 1 ) " malformed data segment kind"
1035
1035
1036
1036
let data_section s =
1037
- section `DataSection (vec (at data)) [] s
1037
+ section Custom. Data (vec (at data)) [] s
1038
1038
1039
1039
1040
1040
(* DataCount section *)
@@ -1043,62 +1043,64 @@ let data_count s =
1043
1043
Some (u32 s)
1044
1044
1045
1045
let data_count_section s =
1046
- section `DataCountSection data_count None s
1046
+ section Custom. DataCount data_count None s
1047
1047
1048
1048
1049
1049
(* Custom section *)
1050
1050
1051
- let custom size s =
1051
+ let custom place size s =
1052
1052
let start = pos s in
1053
- let id = name s in
1054
- let bs = get_string (size - (pos s - start)) s in
1055
- Some (id, bs)
1053
+ let name = name s in
1054
+ let content = get_string (size - (pos s - start)) s in
1055
+ Custom. {name; content; place}
1056
1056
1057
- let custom_section s =
1058
- section_with_size `CustomSection custom None s
1057
+ let some_custom place size s =
1058
+ Some (at ( custom place size) s)
1059
1059
1060
- let non_custom_section s =
1061
- match id s with
1062
- | None | Some `CustomSection -> None
1063
- | _ -> skip 1 s; sized skip s; Some ()
1060
+ let custom_section place s =
1061
+ section_with_size Custom. Custom (some_custom place) None s
1064
1062
1065
1063
1066
1064
(* Modules *)
1067
1065
1068
- let rec iterate f s = if f s <> None then iterate f s
1066
+ let rec iterate f s =
1067
+ match f s with
1068
+ | None -> []
1069
+ | Some x -> x :: iterate f s
1069
1070
1070
1071
let magic = 0x6d736100l
1071
1072
1072
1073
let module_ s =
1074
+ let open Custom in
1073
1075
let header = word32 s in
1074
1076
require (header = magic) s 0 " magic header not detected" ;
1075
1077
let version = word32 s in
1076
1078
require (version = Encode. version) s 4 " unknown binary version" ;
1077
- iterate custom_section s;
1079
+ let customs = iterate ( custom_section ( Before Type )) s in
1078
1080
let types = type_section s in
1079
- iterate custom_section s;
1081
+ let customs = customs @ iterate ( custom_section ( After Type )) s in
1080
1082
let imports = import_section s in
1081
- iterate custom_section s;
1083
+ let customs = customs @ iterate ( custom_section ( After Import )) s in
1082
1084
let func_types = func_section s in
1083
- iterate custom_section s;
1085
+ let customs = customs @ iterate ( custom_section ( After Func )) s in
1084
1086
let tables = table_section s in
1085
- iterate custom_section s;
1087
+ let customs = customs @ iterate ( custom_section ( After Table )) s in
1086
1088
let memories = memory_section s in
1087
- iterate custom_section s;
1089
+ let customs = customs @ iterate ( custom_section ( After Memory )) s in
1088
1090
let globals = global_section s in
1089
- iterate custom_section s;
1091
+ let customs = customs @ iterate ( custom_section ( After Global )) s in
1090
1092
let exports = export_section s in
1091
- iterate custom_section s;
1093
+ let customs = customs @ iterate ( custom_section ( After Export )) s in
1092
1094
let start = start_section s in
1093
- iterate custom_section s;
1095
+ let customs = customs @ iterate ( custom_section ( After Start )) s in
1094
1096
let elems = elem_section s in
1095
- iterate custom_section s;
1097
+ let customs = customs @ iterate ( custom_section ( After Elem )) s in
1096
1098
let data_count = data_count_section s in
1097
- iterate custom_section s;
1099
+ let customs = customs @ iterate ( custom_section ( After DataCount )) s in
1098
1100
let func_bodies = code_section s in
1099
- iterate custom_section s;
1101
+ let customs = customs @ iterate ( custom_section ( After Code )) s in
1100
1102
let datas = data_section s in
1101
- iterate custom_section s;
1103
+ let customs = customs @ iterate ( custom_section ( After Data )) s in
1102
1104
require (pos s = len s) s (len s) " unexpected content after last section" ;
1103
1105
require (List. length func_types = List. length func_bodies)
1104
1106
s (len s) " function and code section have inconsistent lengths" ;
@@ -1108,23 +1110,37 @@ let module_ s =
1108
1110
List. for_all Free. (fun f -> (func f).datas = Set. empty) func_bodies)
1109
1111
s (len s) " data count section required" ;
1110
1112
let funcs =
1111
- List. map2 (fun t f -> {f.it with ftype = t} @@ f.at) func_types func_bodies
1112
- in {types; tables; memories; globals; funcs; imports; exports; elems; datas; start}
1113
-
1114
-
1115
- let decode name bs = at module_ (stream name bs)
1116
-
1117
- let all_custom tag s =
1118
- let header = word32 s in
1119
- require (header = magic) s 0 " magic header not detected" ;
1120
- let version = word32 s in
1121
- require (version = Encode. version) s 4 " unknown binary version" ;
1122
- let rec collect () =
1123
- iterate non_custom_section s;
1124
- match custom_section s with
1125
- | None -> []
1126
- | Some (n , s ) when n = tag -> s :: collect ()
1127
- | Some _ -> collect ()
1128
- in collect ()
1129
-
1130
- let decode_custom tag name bs = all_custom tag (stream name bs)
1113
+ List. map2 Source. (fun t f -> {f.it with ftype = t} @@ f.at)
1114
+ func_types func_bodies
1115
+ in
1116
+ {types; tables; memories; globals; funcs; imports; exports; elems; datas; start},
1117
+ customs
1118
+
1119
+
1120
+ let decode_custom m bs custom =
1121
+ let open Source in
1122
+ let Custom. {name; content; place} = custom.it in
1123
+ match Custom. handler name, Custom. handler (Utf8. decode " custom" ) with
1124
+ | Some (module Handler), _ ->
1125
+ let fmt = Handler. decode m bs custom in
1126
+ let module S = struct module Handler = Handler let it = fmt end in
1127
+ [(module S : Custom.Section )]
1128
+ | None , Some (module Handler') ->
1129
+ let fmt = Handler'. decode m bs custom in
1130
+ let module S = struct module Handler = Handler ' let it = fmt end in
1131
+ [(module S : Custom.Section )]
1132
+ | None , None ->
1133
+ if ! Flags. custom_reject then
1134
+ raise (Custom. Code (custom.at,
1135
+ " unknown custom section \" " ^ Utf8. encode name ^ " \" " ))
1136
+ else
1137
+ []
1138
+
1139
+ let decode_with_custom name bs =
1140
+ let m_cs = at module_ (stream name bs) in
1141
+ let open Source in
1142
+ let m', cs = m_cs.it in
1143
+ let m = m' @@ m_cs.at in
1144
+ m, List. flatten (List. map (decode_custom m bs) cs)
1145
+
1146
+ let decode name bs = fst (decode_with_custom name bs)
0 commit comments