package js_of_ocaml-compiler

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file magic_number.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
(* Js_of_ocaml compiler
 * http://www.ocsigen.org/js_of_ocaml/
 *
 * 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 t = string * int

exception Bad_magic_number of string

exception Bad_magic_version of t

let size = 12

let kind_of_string = function
  | "Caml1999X" -> "exe"
  | "Caml1999I" -> "cmi"
  | "Caml1999O" -> "cmo"
  | "Caml1999A" -> "cma"
  | "Caml1999Y" -> "cmx"
  | "Caml1999Z" -> "cmxa"
  | "Caml2007D" -> "cmxs"
  | "Caml2012T" -> "cmt"
  | "Caml1999M" -> "impl"
  | "Caml1999N" -> "intf"
  | _ -> raise Not_found

let of_string s =
  try
    if String.length s <> size then raise Not_found;
    let kind = String.sub s ~pos:0 ~len:9 in
    let v = String.sub s ~pos:9 ~len:3 in
    let _ = kind_of_string kind in
    kind, int_of_string v
  with _ -> raise (Bad_magic_number s)

let kind (s, _) =
  match kind_of_string s with
  | "exe" -> `Exe
  | "cmo" -> `Cmo
  | "cma" -> `Cma
  | other -> `Other other

let to_string (k, v) = Printf.sprintf "%s%03d" k v

let compare (p1, n1) (p2, n2) =
  if not (String.equal p1 p2) then raise Not_found;
  compare n1 n2

let equal a b = compare a b = 0

let current_exe =
  let v =
    match Ocaml_version.v with
    | `V4_04 -> 11
    | `V4_06 -> 11
    | `V4_07 -> 23
    | `V4_08 -> 25
    | `V4_09 -> 26
    | `V4_10 -> 27
    | `V4_11 -> 28
    | `V4_12 -> 29
    | `V4_13 -> 30
  in
  "Caml1999X", v

let current_cmo =
  let v =
    match Ocaml_version.v with
    | `V4_04 -> 11
    | `V4_06 -> 22
    | `V4_07 -> 23
    | `V4_08 -> 25
    | `V4_09 -> 26
    | `V4_10 -> 27
    | `V4_11 -> 28
    | `V4_12 -> 29
    | `V4_13 -> 30
  in
  "Caml1999O", v

let current_cma =
  let v =
    match Ocaml_version.v with
    | `V4_04 -> 12
    | `V4_06 -> 22
    | `V4_07 -> 23
    | `V4_08 -> 25
    | `V4_09 -> 26
    | `V4_10 -> 27
    | `V4_11 -> 28
    | `V4_12 -> 29
    | `V4_13 -> 30
  in
  "Caml1999A", v

let current = function
  | `Exe -> current_exe
  | `Cmo -> current_cmo
  | `Cma -> current_cma
OCaml

Innovation. Community. Security.