Skip to content

Commit

Permalink
add custom section to binary module
Browse files Browse the repository at this point in the history
  • Loading branch information
Laplace-Demon committed Jul 31, 2024
1 parent f718d8b commit 57b700e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 27 deletions.
6 changes: 6 additions & 0 deletions src/ast/binary.ml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ type elem =
; mode : elem_mode
}

type custom =
| Uninterpreted of (string, Sexp.t) Either.t
| Contract of binary Contract.t

type modul =
{ id : string option
; types : binary rec_type Named.t
Expand All @@ -60,6 +64,7 @@ type modul =
; data : data Named.t
; exports : exports
; start : int option
; custom : custom list
}

let empty_modul =
Expand All @@ -73,4 +78,5 @@ let empty_modul =
; data = Named.empty
; exports = { global = []; mem = []; table = []; func = [] }
; start = None
; custom = []
}
3 changes: 2 additions & 1 deletion src/ast/binary_to_text.ml
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ let from_exports (exports : Binary.exports) : Text.module_field list =
let from_start = function None -> [] | Some n -> [ MStart (Raw n) ]

let modul
{ Binary.id; types; global; table; mem; func; elem; data; start; exports } =
{ Binary.id; types; global; table; mem; func; elem; data; start; exports; _ }
=
let fields =
from_types types @ from_global global @ from_table table @ from_mem mem
@ from_func func @ from_elem elem @ from_data data @ from_exports exports
Expand Down
59 changes: 33 additions & 26 deletions src/parser/binary_parser.ml
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ let parse_many_custom_section input =

let sections_iterate (input : Input.t) =
(* Custom *)
let* _custom_sections, input = parse_many_custom_section input in
let* custom_sections, input = parse_many_custom_section input in

(* Type *)
let* type_section, input =
Expand All @@ -936,44 +936,44 @@ let sections_iterate (input : Input.t) =
let type_section = Array.of_list type_section in

(* Custom *)
let* _custom_sections', input = parse_many_custom_section input in
let _custom_sections = _custom_sections @ _custom_sections' in
let* custom_sections', input = parse_many_custom_section input in
let custom_sections = custom_sections @ custom_sections' in

(* Imports *)
let* import_section, input =
section_parse input ~expected_id:'\x02' [] (vector_no_id read_import)
in

(* Custom *)
let* _custom_sections', input = parse_many_custom_section input in
let _custom_sections = _custom_sections @ _custom_sections' in
let* custom_sections', input = parse_many_custom_section input in
let custom_sections = custom_sections @ custom_sections' in

(* Function *)
let* function_section, input =
section_parse input ~expected_id:'\x03' [] (vector_no_id read_U32)
in

(* Custom *)
let* _custom_sections', input = parse_many_custom_section input in
let _custom_sections = _custom_sections @ _custom_sections' in
let* custom_sections', input = parse_many_custom_section input in
let custom_sections = custom_sections @ custom_sections' in

(* Tables *)
let* table_section, input =
section_parse input ~expected_id:'\x04' [] (vector_no_id read_table)
in

(* Custom *)
let* _custom_sections', input = parse_many_custom_section input in
let _custom_sections = _custom_sections @ _custom_sections' in
let* custom_sections', input = parse_many_custom_section input in
let custom_sections = custom_sections @ custom_sections' in

(* Memory *)
let* memory_section, input =
section_parse input ~expected_id:'\x05' [] (vector_no_id read_memory)
in

(* Custom *)
let* _custom_sections', input = parse_many_custom_section input in
let _custom_sections = _custom_sections @ _custom_sections' in
let* custom_sections', input = parse_many_custom_section input in
let custom_sections = custom_sections @ custom_sections' in

(* Globals *)
let* global_section, input =
Expand All @@ -982,17 +982,17 @@ let sections_iterate (input : Input.t) =
in

(* Custom *)
let* _custom_sections', input = parse_many_custom_section input in
let _custom_sections = _custom_sections @ _custom_sections' in
let* custom_sections', input = parse_many_custom_section input in
let custom_sections = custom_sections @ custom_sections' in

(* Exports *)
let* export_section, input =
section_parse input ~expected_id:'\x07' [] (vector_no_id read_export)
in

(* Custom *)
let* _custom_sections', input = parse_many_custom_section input in
let _custom_sections = _custom_sections @ _custom_sections' in
let* custom_sections', input = parse_many_custom_section input in
let custom_sections = custom_sections @ custom_sections' in

(* Start *)
let* start_section, input =
Expand All @@ -1002,8 +1002,8 @@ let sections_iterate (input : Input.t) =
in

(* Custom *)
let* _custom_sections', input = parse_many_custom_section input in
let _custom_sections = _custom_sections @ _custom_sections' in
let* custom_sections', input = parse_many_custom_section input in
let custom_sections = custom_sections @ custom_sections' in

(* Elements *)
let* element_section, input =
Expand All @@ -1012,8 +1012,8 @@ let sections_iterate (input : Input.t) =
in

(* Custom *)
let* _custom_sections', input = parse_many_custom_section input in
let _custom_sections = _custom_sections @ _custom_sections' in
let* custom_sections', input = parse_many_custom_section input in
let custom_sections = custom_sections @ custom_sections' in

(* Data_count *)
let* data_count_section, input =
Expand All @@ -1023,8 +1023,8 @@ let sections_iterate (input : Input.t) =
in

(* Custom *)
let* _custom_sections', input = parse_many_custom_section input in
let _custom_sections = _custom_sections @ _custom_sections' in
let* custom_sections', input = parse_many_custom_section input in
let custom_sections = custom_sections @ custom_sections' in

(* Code *)
let* code_section, input =
Expand All @@ -1039,8 +1039,8 @@ let sections_iterate (input : Input.t) =
in

(* Custom *)
let* _custom_sections', input = parse_many_custom_section input in
let _custom_sections = _custom_sections @ _custom_sections' in
let* custom_sections', input = parse_many_custom_section input in
let custom_sections = custom_sections @ custom_sections' in

(* Data *)
let+ data_section, input =
Expand All @@ -1066,9 +1066,8 @@ let sections_iterate (input : Input.t) =
in

(* Custom *)
(* TODO: actually use the various custom sections *)
let* _custom_sections', input = parse_many_custom_section input in
let _custom_sections = _custom_sections @ _custom_sections' in
let* custom_sections', input = parse_many_custom_section input in
let custom_sections = custom_sections @ custom_sections' in

let+ () =
if not @@ Input.is_empty input then Error (`Msg "malformed section id")
Expand Down Expand Up @@ -1210,6 +1209,13 @@ let sections_iterate (input : Input.t) =
}
in

(* Custom *)
let custom =
List.filter_map
(Option.map (fun x -> Uninterpreted (Either.Left x)))
custom_sections
in

{ id = None
; types
; global
Expand All @@ -1220,6 +1226,7 @@ let sections_iterate (input : Input.t) =
; start = start_section
; data
; exports
; custom
}

let from_string content =
Expand Down
1 change: 1 addition & 0 deletions src/text_to_binary/rewrite.ml
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ let modul (modul : Assigned.t) : Binary.modul Result.t =
; exports
; func
; start
; custom = []
}
in
modul

0 comments on commit 57b700e

Please sign in to comment.