package binaryen

  1. Overview
  2. Docs

Source file type_builder.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
type t

type struct_field = {
  type_ : Type.t;
  packed_type : Packed_type.t;
  mutable_ : bool;
}

(* Source: https://github.com/WebAssembly/binaryen/blob/64ba23996a10e229d46e41eb37736a55af87f79a/src/binaryen-c.h#L3618 *)
type error =
  | SelfSupertype of int
  | InvalidSupertype of int
  | ForwardSupertypeReference of int
  | ForwardChildReference of int

external make : int -> t = "caml_type_builder_create"
external grow : t -> int -> unit = "caml_type_builder_grow"
external size : t -> int = "caml_type_builder_get_size"

external set_signature_type : t -> int -> Type.t -> Type.t -> unit
  = "caml_type_builder_set_signature_type"

external set_struct_type :
  t -> int -> Type.t list -> Packed_type.t list -> bool list -> int -> unit
  = "caml_type_builder_set_struct_type__bytecode"
    "caml_type_builder_set_struct_type"

let set_struct_type builder index fields =
  let split_fields fields =
    List.fold_right
      (fun { type_; packed_type; mutable_ } (types, packed_types, mutables) ->
        (type_ :: types, packed_type :: packed_types, mutable_ :: mutables))
      fields ([], [], [])
  in
  let types, packed_types, mutables = split_fields fields in
  set_struct_type builder index types packed_types mutables (List.length types)

external set_array_type : t -> int -> Type.t -> Packed_type.t -> bool -> unit
  = "caml_type_builder_set_array_type"

external get_temp_heap_type : t -> int -> Heap_type.t
  = "caml_type_builder_get_temp_heap_type"

external get_temp_tuple_type : t -> Type.t list -> Type.t
  = "caml_type_builder_get_temp_tuple_type"

external get_temp_ref_type : t -> Heap_type.t -> bool -> Type.t
  = "caml_type_builder_get_temp_ref_type"

external set_sub_type : t -> int -> Heap_type.t -> unit
  = "caml_type_builder_set_sub_type"

external set_open : t -> int -> unit = "caml_type_builder_set_open"

external create_rec_group : t -> int -> int -> unit
  = "caml_type_builder_create_rec_group"

external build_and_dispose : t -> (Heap_type.t array, int * int) result
  = "caml_type_builder_build_and_dispose"

let build_and_dispose builder =
  match build_and_dispose builder with
  | Ok types -> Ok (Array.to_list types)
  | Error (idx, 0) -> Error (SelfSupertype idx)
  | Error (idx, 1) -> Error (InvalidSupertype idx)
  | Error (idx, 2) -> Error (ForwardSupertypeReference idx)
  | Error (idx, _) -> Error (ForwardChildReference idx)