Skip to content

Commit 999d661

Browse files
authored
[interpreter] Functionality for encoding custom sections (WebAssembly#1295)
1 parent 37d6b39 commit 999d661

File tree

6 files changed

+614
-574
lines changed

6 files changed

+614
-574
lines changed

interpreter/binary/decode.ml

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,12 @@ let value_type s =
154154
| Some n when n > 0x70 -> NumType (num_type s)
155155
| _ -> RefType (ref_type s)
156156

157-
let stack_type s = vec value_type s
157+
let result_type s = vec value_type s
158158
let func_type s =
159159
match vs7 s with
160160
| -0x20 ->
161-
let ins = stack_type s in
162-
let out = stack_type s in
161+
let ins = result_type s in
162+
let out = result_type s in
163163
FuncType (ins, out)
164164
| _ -> error s (pos s - 1) "malformed function type"
165165

@@ -759,21 +759,28 @@ let data_count_section s =
759759

760760
let custom size s =
761761
let start = pos s in
762-
let _id = name s in
763-
skip (size - (pos s - start)) s;
764-
true
762+
let id = name s in
763+
let bs = get_string (size - (pos s - start)) s in
764+
Some (id, bs)
765765

766766
let custom_section s =
767-
section_with_size `CustomSection custom false s
767+
section_with_size `CustomSection custom None s
768+
769+
let non_custom_section s =
770+
match id s with
771+
| None | Some `CustomSection -> None
772+
| _ -> skip 1 s; sized skip s; Some ()
768773

769774

770775
(* Modules *)
771776

772-
let rec iterate f s = if f s then iterate f s
777+
let rec iterate f s = if f s <> None then iterate f s
778+
779+
let magic = 0x6d736100l
773780

774781
let module_ s =
775-
let magic = u32 s in
776-
require (magic = 0x6d736100l) s 0 "magic header not detected";
782+
let header = u32 s in
783+
require (header = magic) s 0 "magic header not detected";
777784
let version = u32 s in
778785
require (version = Encode.version) s 4 "unknown binary version";
779786
iterate custom_section s;
@@ -816,3 +823,18 @@ let module_ s =
816823

817824

818825
let decode name bs = at module_ (stream name bs)
826+
827+
let all_custom tag s =
828+
let header = u32 s in
829+
require (header = magic) s 0 "magic header not detected";
830+
let version = u32 s in
831+
require (version = Encode.version) s 4 "unknown binary version";
832+
let rec collect () =
833+
iterate non_custom_section s;
834+
match custom_section s with
835+
| None -> []
836+
| Some (n, s) when n = tag -> s :: collect ()
837+
| Some _ -> collect ()
838+
in collect ()
839+
840+
let decode_custom tag name bs = all_custom tag (stream name bs)

interpreter/binary/decode.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
exception Code of Source.region * string
22

33
val decode : string -> string -> Ast.module_ (* raises Code *)
4+
5+
val decode_custom : Ast.name -> string -> string -> string list (* raises Code *)

0 commit comments

Comments
 (0)