package js_of_ocaml-compiler

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

Install

Dune Dependency

Authors

Maintainers

Sources

js_of_ocaml-6.1.1.tbz
sha256=d31da41aae61c02aaa8b4d504e4e93705233d303cd81a07bb4ac5eedb03d6014
sha512=207c6c80447a79fa13e5dbd6baf2837ec91b9d81952328ae91c3496ca30f095fce49faa09af8125602f1ae6f2f6e0c391178454dcb39b9907b5a3712bad3fcee

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

Source file config.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
(* Js_of_ocaml compiler
 * http://www.ocsigen.org/js_of_ocaml/
 * Copyright (C) 2013 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

module Flag = struct
  let optims = ref []

  let available () = List.map ~f:fst !optims

  let o ~name ~default =
    let state =
      match List.string_assoc name !optims with
      | Some x -> x
      | None ->
          let state = ref default in
          optims := (name, state) :: !optims;
          state
    in
    fun () -> !state

  let find s =
    match List.string_assoc s !optims with
    | Some x -> !x
    | None -> failwith (Printf.sprintf "The option named %S doesn't exist" s)

  let set s b =
    match List.string_assoc s !optims with
    | Some s -> s := b
    | None -> failwith (Printf.sprintf "The option named %S doesn't exist" s)

  let disable s =
    match List.string_assoc s !optims with
    | Some s -> s := false
    | None -> failwith (Printf.sprintf "The option named %S doesn't exist" s)

  let enable s =
    match List.string_assoc s !optims with
    | Some s -> s := true
    | None -> failwith (Printf.sprintf "The option named %S doesn't exist" s)

  let pretty = o ~name:"pretty" ~default:false

  let stable_var = o ~name:"stable_var" ~default:false

  let debuginfo = o ~name:"debuginfo" ~default:false

  let deadcode = o ~name:"deadcode" ~default:true

  let globaldeadcode = o ~name:"globaldeadcode" ~default:true

  let shortvar = o ~name:"shortvar" ~default:true

  let compact = o ~name:"compact" ~default:true

  let optcall = o ~name:"optcall" ~default:true

  let inline = o ~name:"inline" ~default:true

  let effects = o ~name:"effects" ~default:false

  let staticeval = o ~name:"staticeval" ~default:true

  let share_constant = o ~name:"share" ~default:true

  let strictmode = o ~name:"strict" ~default:true

  let debugger = o ~name:"debugger" ~default:true

  let genprim = o ~name:"genprim" ~default:true

  let excwrap = o ~name:"excwrap" ~default:true

  let improved_stacktrace = o ~name:"with-js-error" ~default:false

  let inline_callgen = o ~name:"callgen" ~default:false

  let safe_string = o ~name:"safestring" ~default:true

  let use_js_string = o ~name:"use-js-string" ~default:true

  let check_magic = o ~name:"check-magic-number" ~default:true

  let compact_vardecl = o ~name:"vardecl" ~default:false

  let header = o ~name:"header" ~default:true

  let auto_link = o ~name:"auto-link" ~default:true

  let es6 = o ~name:"es6" ~default:false

  let load_shapes_auto = o ~name:"load-shapes-auto" ~default:false
end

module Param = struct
  let int default =
    ( default
    , int_of_string
    , fun s ->
        try
          ignore (int_of_string s : int);
          Ok ()
        with _ -> Error "expecting an integer" )

  let enum : (string * 'a) list -> _ = function
    | (_, v) :: _ as l ->
        ( v
        , (fun x ->
            match List.string_assoc x l with
            | Some x -> x
            | None -> assert false)
        , fun x ->
            if List.exists ~f:(fun (y, _) -> String.equal x y) l
            then Ok ()
            else
              Error
                (Printf.sprintf
                   "expecting one of %s"
                   (String.concat ~sep:", " (List.map l ~f:fst))) )
    | _ -> assert false

  let params : (string * _) list ref = ref []

  let p ~name ~desc (default, convert, valid) =
    assert (Option.is_none (List.string_assoc name !params));
    let state = ref default in
    let set : string -> unit =
     fun v ->
      try state := convert v
      with _ -> failwith (Printf.sprintf "malformed option %s=%s." name v)
    in
    params := (name, (set, desc, valid)) :: !params;
    fun () -> !state

  let set s v =
    match List.string_assoc s !params with
    | Some (f, _, _) -> f v
    | None -> failwith (Printf.sprintf "The option named %S doesn't exist" s)

  let all () = List.map !params ~f:(fun (n, (_, d, valid)) -> n, d, valid)

  (* V8 "optimize" switches with less than 128 case.
     60 seams to perform well. *)
  let switch_max_case =
    p ~name:"switch_size" ~desc:"set the maximum number of case in a switch" (int 60)

  let inlining_limit =
    p ~name:"inlining-limit" ~desc:"set the size limit for inlining" (int 150)

  let tailcall_max_depth =
    p
      ~name:"tc_depth"
      ~desc:"set the maximum number of recursive tailcalls defore returning a trampoline"
      (int 50)

  let constant_max_depth =
    p
      ~name:"cst_depth"
      ~desc:"set the maximum depth of generated literal JavaScript values"
      (int 10)

  type tc =
    | TcNone
    | TcTrampoline

  let tc_equal (a : tc) b = Poly.equal a b

  (* | TcWhile *)

  let tc_default = TcTrampoline

  let _tc_all =
    tc_default
    :: List.filter [ TcNone; TcTrampoline ] ~f:(fun x -> not (tc_equal tc_default x))

  let tailcall_optim =
    p
      ~name:"tc"
      ~desc:"Set tailcall optimisation"
      (enum [ "trampoline", TcTrampoline (* default *); "none", TcNone ])

  let lambda_lifting_threshold =
    (* When we reach this depth, we start looking for functions to be lifted *)
    p
      ~name:"lifting-threshold"
      ~desc:"Set threshold for lifting deeply nested functions"
      (int 50)

  let lambda_lifting_baseline =
    (* Level at which functions are lifted *)
    p
      ~name:"lifting-baseline"
      ~desc:"Set baseline for lifting deeply nested functions"
      (int 1)
end

(****)

let target_ : [ `JavaScript | `Wasm | `None ] ref = ref `None

let target () =
  match !target_ with
  | `None -> failwith "target was not set"
  | (`JavaScript | `Wasm) as t -> t

let set_target (t : [ `JavaScript | `Wasm ]) =
  (match t with
  | `JavaScript -> Targetint.set_num_bits 32
  | `Wasm -> Targetint.set_num_bits 31);
  target_ := (t :> [ `JavaScript | `Wasm | `None ])

type effects_backend =
  [ `Disabled
  | `Cps
  | `Double_translation
  | `Jspi
  ]

let effects_ : [< `None | effects_backend ] ref = ref `None

let effects () =
  match !effects_ with
  | `None -> failwith "effects was not set"
  | (`Jspi | `Cps | `Disabled | `Double_translation) as b -> b

let set_effects_backend (backend : effects_backend) =
  effects_ := (backend :> [ `None | effects_backend ])
OCaml

Innovation. Community. Security.