package js_of_ocaml-compiler

  1. Overview
  2. Docs
Compiler from OCaml bytecode to JavaScript

Install

Dune Dependency

Authors

Maintainers

Sources

js_of_ocaml-5.7.2.tbz
sha256=d76f0748dbef45b68f5f6b66f1da2d7a462de64f1cd2932aa0740388e667793c
sha512=4d84f20eb60f9a61b82d8bf9d686ad0d44852addc0a3ffc553d124e488796ec2945bf38311922e57eec739a88346be200289055c56b90b3a22ae4354a073b38c

doc/src/js_of_ocaml-compiler/build_info.ml.html

Source file build_info.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
(* Js_of_ocaml compiler
 * http://www.ocsigen.org/js_of_ocaml/
 * Copyright (C) 2022 Hugo Heuzard
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, with linking exception;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *)

open! Stdlib

type kind =
  [ `Runtime
  | `Exe
  | `Cmo
  | `Cma
  | `Unknown
  ]

let all = [ `Runtime; `Exe; `Cmo; `Cma; `Unknown ]

let string_of_kind = function
  | `Runtime -> "runtime"
  | `Exe -> "exe"
  | `Cmo -> "cmo"
  | `Cma -> "cma"
  | `Unknown -> "unknown"

let kind_of_string s =
  match List.find_opt all ~f:(fun k -> String.equal s (string_of_kind k)) with
  | None -> `Unknown
  | Some k -> k

type t = string StringMap.t

let kind t =
  match StringMap.find "kind" t with
  | exception Not_found -> `Unknown
  | s -> kind_of_string s

let create kind =
  let version =
    match Compiler_version.git_version with
    | "" -> Compiler_version.s
    | v -> Printf.sprintf "%s+%s" Compiler_version.s v
  in
  [ "use-js-string", string_of_bool (Config.Flag.use_js_string ())
  ; "effects", string_of_bool (Config.Flag.effects ())
  ; "version", version
  ; "kind", string_of_kind kind
  ]
  |> List.fold_left ~init:StringMap.empty ~f:(fun acc (k, v) -> StringMap.add k v acc)

let with_kind t kind = StringMap.add "kind" (string_of_kind kind) t

let prefix = "//# buildInfo:"

let to_string info =
  let str =
    StringMap.bindings info
    |> List.map ~f:(fun (k, v) -> Printf.sprintf "%s=%s" k v)
    |> String.concat ~sep:", "
  in
  Printf.sprintf "%s%s\n" prefix str

let parse s =
  match String.drop_prefix ~prefix s with
  | None -> None
  | Some suffix ->
      let t =
        suffix
        |> String.split_on_char ~sep:','
        |> List.map ~f:String.trim
        |> List.map ~f:(fun s ->
               match String.lsplit2 ~on:'=' s with
               | None -> s, ""
               | Some (k, v) -> k, v)
        |> List.fold_left ~init:StringMap.empty ~f:(fun acc (k, v) ->
               StringMap.add k v acc)
      in
      Some t

exception
  Incompatible_build_info of
    { key : string
    ; first : (string * string option)
    ; second : (string * string option)
    }

let merge fname1 info1 fname2 info2 =
  if String.equal fname1 fname2
  then
    StringMap.merge
      (fun k v1 v2 ->
        match v1, v2 with
        | Some v1, Some v2 when String.equal v1 v2 -> Some v1
        | Some v1, Some v2 ->
            failwith
              (Printf.sprintf
                 "%s: Duplicated build info with incompatible value. key=%s, v1=%s, v2=%s"
                 fname1
                 k
                 v1
                 v2)
        | Some x, None | None, Some x -> Some x
        | None, None -> assert false)
      info1
      info2
  else
    StringMap.merge
      (fun k v1 v2 ->
        match k, v1, v2 with
        | "kind", v1, v2 ->
            if Option.equal String.equal v1 v2 then v1 else Some (string_of_kind `Unknown)
        | ("effects" | "use-js-string" | "version"), Some v1, Some v2
          when String.equal v1 v2 -> Some v1
        | (("effects" | "use-js-string" | "version") as key), v1, v2 ->
            raise
              (Incompatible_build_info { key; first = fname1, v1; second = fname2, v2 })
        | _, Some v1, Some v2 when String.equal v1 v2 -> Some v1
        (* ignore info that are present on one side only or have a different value *)
        | _, Some _, Some _ -> None
        | _, None, Some _ | _, Some _, None -> None
        | _, None, None -> assert false)
      info1
      info2

let configure t =
  StringMap.iter
    (fun k v ->
      match k with
      | "use-js-string" | "effects" -> Config.Flag.set k (bool_of_string v)
      | _ -> ())
    t
OCaml

Innovation. Community. Security.