forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs_unsafe_gen.ml
executable file
·102 lines (88 loc) · 3.84 KB
/
js_unsafe_gen.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
#!/usr/bin/env ocaml
let generate_mk arity =
let arity_s = string_of_int arity in
let fmt = format_of_string {|external fn_mk%s : (%s) -> (%s [@bs]) = "#fn_mk" "%s" |} in
if arity = 0 then
let ty = "unit -> 'a0" in
Printf.sprintf fmt arity_s ty ty arity_s
else
let args = Array.to_list (Array.init (arity + 1) (fun i -> "'a" ^ string_of_int i)) in
let ty = String.concat " -> " args in
Printf.sprintf fmt arity_s ty ty arity_s
let generate_call_back_mk arity =
let arity_s = string_of_int (arity + 1) in
let fmt = format_of_string {|external fn_method%s : (%s) -> (%s [@bs.this]) = "#fn_method" "%s" |} in
let args = Array.to_list (Array.init (arity + 1) (fun i -> "'a" ^ string_of_int i)) in
let ty = String.concat " -> " ("'obj":: args) in
Printf.sprintf fmt arity_s ty ty arity_s
let generate_run arity =
let arity_s = string_of_int arity in
let fmt = format_of_string {|external fn_run%s : (%s [@bs]) -> (%s ) = "#fn_run" "%s" |} in
if arity = 0 then
let tya = "unit -> 'a0" in
let tyb = "'a0" in
Printf.sprintf fmt arity_s tya tyb arity_s
else
let args = Array.to_list (Array.init (arity + 1) (fun i -> "'a" ^ string_of_int i)) in
let ty = String.concat " -> " args in
Printf.sprintf fmt arity_s ty ty arity_s
let generate_method_run arity =
let arity_s = string_of_int arity in
let fmt = format_of_string {|external method_run%s : (%s [@bs.meth]) -> (%s ) = "#method_run" "%s" |} in
if arity = 0 then
let tya = "unit -> 'a0" in
let tyb = "'a0" in
Printf.sprintf fmt arity_s tya tyb arity_s
else
let args = Array.to_list (Array.init (arity + 1) (fun i -> "'a" ^ string_of_int i)) in
let ty = String.concat " -> " args in
Printf.sprintf fmt arity_s ty ty arity_s
let prelude = {|
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a 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. *)
(* Generated by scripts/js_unsafe_gen.ml *)
external (!) : 'a Js.t -> 'a = "#unsafe_downgrade"
external debugger : unit -> unit = "#debugger"
external raw_expr : string -> 'a = "#raw_expr"
external raw_stmt : string -> 'a = "#raw_stmt"
external unsafe_downgrade : 'a Js.t -> 'a = "#unsafe_downgrade"
|}
let () =
let arities = Array.to_list (Array.init 10 (fun i -> i)) in
let code =
List.map generate_mk arities @
List.map generate_call_back_mk arities @
List.map generate_run arities @
List.map generate_method_run arities in
print_endline (prelude ^ String.concat "\n" code )
(**
*)
(**
{[
[%bs.obj: < bark : string -> int [@bs.method] >
]}
*)
(* local variables: *)
(* compile-command: "ocaml js_unsafe_gen.ml > ../jscomp/runtime/js_unsafe.ml" *)
(* end: *)