forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs_internal_gen.js
135 lines (119 loc) · 3.44 KB
/
js_internal_gen.js
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
var assert = require("assert");
function initArray(n) {
var res = new Array(n);
for (var i = 0; i < n; ++i) {
res[i] = i;
}
return res;
}
function gen_normal(arity) {
if (arity === 0) {
return "unit -> 'a0";
} else {
return initArray(arity + 1)
.map(x => `'a${x}`)
.join(" -> ");
}
}
function gen_bs(arity) {
if (arity === 0) {
return "([`Arity_0], 'a0) fn";
} else {
return `([\`Arity_${arity} of ( ${initArray(arity)
.map(x => `'a${x}`)
.join(" * ")} )], 'a${arity}) fn`;
}
}
function gen_bs_this(arity) {
assert.notEqual(arity, 0);
return `([\`Arity_${arity} of ( ${initArray(arity)
.map(x => `'a${x}`)
.join(" * ")} )], 'a${arity} ) meth_callback`;
}
function gen_bs_meth(arity) {
if (arity === 0) {
return `([\`Arity_0], 'a0) meth`;
}
return `([\`Arity_${arity} of ( ${initArray(arity)
.map(x => `'a${x}`)
.join(" * ")} )], 'a${arity}) meth`;
}
function generate_mk(arity) {
return `external fn_mk${arity} : (${gen_normal(arity)}) -> (${gen_bs(
arity
)}) = "#fn_mk" "${arity}" `;
}
function generate_call_back_mk(arity) {
var arity = arity + 1;
return `external fn_method${arity} : (${gen_normal(arity)}) -> (${gen_bs_this(
arity
)}) = "#fn_method" "${arity}" `;
}
function generate_run(arity) {
if (arity === 0) {
return `external fn_run0 : (([\`Arity_0], 'a0) fn) -> 'a0 = "#fn_run" "0" `;
}
return `external fn_run${arity} : (${gen_bs(arity)}) -> (${gen_normal(
arity
)} ) = "#fn_run" "${arity}" `;
}
function generate_method_run(arity) {
if (arity === 0) {
return `external method_run0 : (([\`Arity_0], 'a0) meth) -> 'a0 = "#method_run" "0" `;
}
return `external method_run${arity} : (${gen_bs_meth(
arity
)}) -> (${gen_normal(arity)} ) = "#method_run" "${arity}" `;
}
var prelude = `
(* Generated by scripts/js_unsafe_gen.ml *)
type (-'obj, +'a) meth_callback
type (-'arg, + 'result) meth
type (-'arg, + 'result) fn
(** Js uncurried function *)
external (!) : 'a t -> 'a = "#unsafe_downgrade"
external debugger : unit -> unit = "#debugger"
external raw_expr : string -> 'a = "#raw_expr"
external raw_stmt : string -> 'a = "#raw_stmt"
external raw_function: string -> 'a = "#raw_function"
external unsafe_downgrade : 'a t -> 'a = "#unsafe_downgrade"
`.split("\n");
function genAll(prefix) {
var arities = initArray(10);
var code = `${prefix} ${Array.prototype
.concat(
prelude,
arities.map(generate_mk),
arities.map(generate_call_back_mk),
arities.map(generate_run),
arities.map(generate_method_run)
)
.map(x => " " + x)
.join("\n")}
end`;
// console.log(arities, arities.map(x=>generate_call_back_mk(x)))
return code;
}
var fs = require("fs");
var path = require("path");
if (require.main === module) {
// fs.writeFileSync(path.join(__dirname, '..', 'jscomp', 'runtime', 'js_unsafe.ml'), genAll(), 'utf8')
var jsMlFile = path.join(__dirname, "..", "jscomp", "runtime", "js.ml");
var jsMliFile = path.join(__dirname, "..", "jscomp", "runtime", "js.mli");
var replacement = genAll("module Internal = struct");
var jsCode = fs.readFileSync(jsMlFile, "utf8");
fs.writeFileSync(
jsMlFile,
jsCode.replace(/module Internal = struct([^])*?end/m, replacement),
"utf8"
);
jsCode = fs.readFileSync(jsMliFile, "utf8");
fs.writeFileSync(
jsMliFile,
jsCode.replace(
/module Internal : sig([^])*?end/m,
genAll("module Internal : sig")
),
"utf8"
);
}