/
jsUtils.ml
330 lines (305 loc) · 12.8 KB
/
jsUtils.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
(*
Copyright © 2011, 2012 MLstate
This file is part of Opa.
Opa is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.
Opa 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 Affero General Public License for
more details.
You should have received a copy of the GNU Affero General Public License
along with Opa. If not, see <http://www.gnu.org/licenses/>.
*)
module List = BaseList
module J = JsAst
let arguments_ident = JsCons.Ident.native "arguments"
let maybe_globalize_ident local_vars = function
| J.ExprIdent _ as i -> i
| J.Native (`global _,_) as i -> i
| J.Native (`local,j) as i ->
if JsIdentSet.mem i local_vars then i else J.Native (`global true,j)
let collect_local_var local_vars stm =
(* not going inside the functions that are inside expressions on purpose.
* The identifiers defined there are not in scope *)
JsWalk.OnlyStatement.fold
(fun local_vars -> function
| J.Js_function (_,name,_,_)
| J.Js_var (_,name,_) -> JsIdentSet.add name local_vars
| _ -> local_vars) local_vars stm
let collect_local_vars local_vars stms =
List.fold_left collect_local_var local_vars stms
let globalize_native_ident stm =
let rec traverse_stm local_vars =
JsWalk.TStatement.map_nonrec
(aux_stm local_vars) (aux_expr local_vars)
and traverse_expr local_vars =
JsWalk.TExpr.map_nonrec
(aux_expr local_vars) (aux_stm local_vars)
and aux_stm local_vars stm =
match stm with
| J.Js_function (label, name, params, body) ->
let fname = maybe_globalize_ident local_vars name in
let local_vars = JsIdentSet.add arguments_ident local_vars in
let local_vars =
List.fold_left (fun local_vars i ->
JsIdentSet.add i local_vars
) local_vars params
in
let local_vars = collect_local_vars local_vars body in
let stm =
if name == fname then
stm
else
J.Js_function (label,fname,params,body)
in
traverse_stm local_vars stm
| J.Js_var (label, name, o) ->
let fname = maybe_globalize_ident local_vars name in
let stm =
if name == fname then
stm
else
J.Js_var (label,fname, o)
in
traverse_stm local_vars stm
| J.Js_trycatch (label, body, catches, finally) ->
(* We traverse the catch statements by ourselves since
the standard JsWalk fold doesn't take binding variables
into account *)
let fcatches =
List.map (fun (ident, expr, body) ->
let local_vars = JsIdentSet.add ident local_vars in
(ident, expr, traverse_stm local_vars body)
) catches
in
let fbody = traverse_stm local_vars body in
let ffinally = Option.map (traverse_stm local_vars) finally in
J.Js_trycatch (label, fbody, fcatches, ffinally)
| _ ->
traverse_stm local_vars stm
and aux_expr local_vars expr =
match expr with
| J.Je_function (_, _, params, body) ->
let local_vars = JsIdentSet.add arguments_ident local_vars in
let local_vars =
List.fold_left (fun local_vars i ->
JsIdentSet.add i local_vars
) local_vars params
in
let local_vars = collect_local_vars local_vars body in
traverse_expr local_vars expr
| J.Je_ident (label, name) ->
let fname = maybe_globalize_ident local_vars name in
let expr = if name == fname then expr else J.Je_ident (label,fname) in
traverse_expr local_vars expr
| _ ->
traverse_expr local_vars expr
in
aux_stm JsIdentSet.empty stm
let prefix_with (prefix : string) (name : string) : J.expr =
(* When prefixing with identifiers such as [global], we
must not ignore properties inherited from object, otherwise
we lose global functions such as toString. *)
JsCons.Expr.dot ~own_property:false
(JsCons.Expr.native_global prefix) name
let export_to_global_namespace_aux stm =
JsWalk.TStatement.map
(fun stm ->
match stm with
| J.Js_function (_, J.Native (`global _, name), params, body) ->
JsCons.Statement.assign (prefix_with "global" name)
(JsCons.Expr.function_ None params body)
| J.Js_function (_, ((J.ExprIdent _) as i), params, body) ->
let name = JsPrint.string_of_ident i in
JsCons.Statement.assign (prefix_with "global" name)
(JsCons.Expr.function_ None params body)
| J.Js_var (_, J.Native (`global _, name), o) ->
let rhs =
match o with
| Some e -> e
| None -> JsCons.Expr.undefined () in
JsCons.Statement.assign (prefix_with "global" name) rhs
| _ -> stm
)
(fun e ->
match e with
| J.Je_ident (_, J.Native (`global _, "exports")) ->
(* Since exports is not defined in the global scope
when commonjs modules are loaded, we need to replace
it *)
JsCons.Expr.native_global "global"
| J.Je_ident (_, J.Native (`global _, name)) when
name <> "global" &&
name <> "require" (* Hack to avoid require scope problem *) ->
prefix_with "global" name
| _ -> e
) stm
let export_to_global_namespace code =
List.map (fun stm ->
export_to_global_namespace_aux (globalize_native_ident stm)
) code
let export_global_declarations_aux exports stm =
let maybe_export_ident exports ident =
match ident with
| J.Native (`global _, _)
| J.ExprIdent _ ->
JsIdentSet.add ident exports
| _ -> exports in
JsWalk.OnlyStatement.fold (fun exports stm ->
match stm with
| J.Js_function (_, ident, _, _) ->
maybe_export_ident exports ident
| J.Js_var (_, ident, _) ->
maybe_export_ident exports ident
| _ -> exports
) exports stm
let export_global_declarations code =
let code = List.map globalize_native_ident code in
let exports = List.fold_left (fun exports stm ->
export_global_declarations_aux exports stm
) JsIdentSet.empty code in
let exports = JsIdentSet.fold (fun ident exports ->
let export =
JsCons.Statement.assign
(prefix_with "exports" (JsPrint.string_of_ident ident))
(JsCons.Expr.ident ident) in
export :: exports
) exports [] in
code @ exports
let basic_package_json ?(version="0.1.0") name main =
Printf.sprintf (
"{\n" ^^
" \"name\": \"%s\",\n" ^^
" \"version\": \"%s\",\n" ^^
" \"main\": \"%s\"\n" ^^
"}\n"
) name version main
let compares x cmp x0 x1 = if x <> 0 then x else cmp x0 x1
let compare_ident i0 i1 =
match i0, i1 with
| J.ExprIdent i0, J.ExprIdent i1 -> Ident.compare i0 i1
| J.Native (t0, s0), J.Native (t1, s1) ->
compares (Pervasives.compare t0 t1) String.compare s0 s1
| _, _ -> Pervasives.compare i0 i1
let compare_unop = Pervasives.compare
let compare_binop = Pervasives.compare
let rec compare_expr e1 e2 =
match e1, e2 with
| J.Je_this (_), J.Je_this (_) -> 0
| J.Je_ident (_ , i0), J.Je_ident (_ , i1) -> compare_ident i0 i1
| J.Je_array (_ , l0), J.Je_array (_ , l1) -> List.make_compare compare_expr l0 l1
| J.Je_comma (_ , l0, e0), J.Je_comma (_ , l1, e1) ->
compares (List.make_compare compare_expr l0 l1) compare_expr e0 e1
| J.Je_object (_ , l0), J.Je_object (_ , l1) ->
List.make_compare
(fun (f0, e0) (f1, e1) -> compares (String.compare f0 f1) compare_expr e0 e1)
l0 l1
| J.Je_string (_ , s0, b0), J.Je_string (_ , s1, b1) -> Pervasives.compare (s0, b0) (s1, b1)
| J.Je_num (_ , s0), J.Je_num (_ , s1) -> String.compare s0 s1
| J.Je_null (_), J.Je_null (_) -> 0
| J.Je_undefined (_), J.Je_undefined (_) -> 0
| J.Je_bool (_ , b0), J.Je_bool (_ , b1) -> Pervasives.compare b0 b1
| J.Je_regexp (_ , s0 , s0'), J.Je_regexp (_ , s1 , s1') ->
Pervasives.compare (s0, s0') (s1, s1')
| J.Je_function (_ , _i0, p0, b0), J.Je_function (_ , _i1, p1, b1) ->
compares (List.make_compare compare_ident p0 p1) compare_code b0 b1
| J.Je_dot (_ , e0, s0), J.Je_dot (_ , e1, s1) ->
compares (compare_expr e0 e1) String.compare s0 s1
| J.Je_unop (_ , u0, e0), J.Je_unop (_ , u1, e1) ->
compares (compare_unop u0 u1) compare_expr e0 e1
| J.Je_binop (_ , b0, r0, l0), J.Je_binop (_ , b1, r1, l1) ->
compares (compare_binop b0 b1) (compares (compare_expr l0 l1) compare_expr) r0 r1
| J.Je_cond (_ , c0, e0, a0), J.Je_cond (_ , c1, e1, a1) ->
compares (compare_expr c0 c1) (compares (compare_expr a0 a1) compare_expr) e0 e1
| J.Je_call (_ , f0, a0, _), J.Je_call (_ , f1, a1, _) ->
compares (compare_expr f0 f1) (List.make_compare compare_expr) a0 a1
| J.Je_new (_ , e0, a0), J.Je_new (_ , e1, a1) ->
compares (compare_expr e0 e1) (List.make_compare compare_expr) a0 a1
| J.Je_hole (_ , e0), J.Je_hole (_ , e1) ->Pervasives.compare e0 e1
| J.Je_runtime (_ , e0), J.Je_runtime (_ , e1) -> Pervasives.compare e0 e1
| x0, x1 -> Pervasives.compare x0 x1
and compare_statement s0 s1 =
match s0, s1 with
| J.Js_var (_, i0, e0), J.Js_var (_, i1, e1) ->
compares (compare_ident i0 i1) (Option.make_compare compare_expr) e0 e1
| J.Js_function (_, _i0, p0, b0), J.Js_function (_, _i1, p1, b1) ->
compares (List.make_compare compare_ident p0 p1) compare_code b0 b1
| J.Js_return (_, e0), J.Js_return (_, e1) -> (Option.make_compare compare_expr) e0 e1
| J.Js_continue (_, l0), J.Js_continue (_, l1) ->
Option.make_compare String.compare l0 l1
| J.Js_break (_, l0), J.Js_break (_, l1) ->
Option.make_compare String.compare l0 l1
| J.Js_switch (_, e0, c0, d0), J.Js_switch (_, e1, c1, d1) ->
compares (compare_expr e0 e1)
(compares (Option.make_compare compare_statement d0 d1)
(List.make_compare (fun (e0, s0) (e1, s1) ->
compares (compare_expr e0 e1) compare_statement s0 s1))
)
c0 c1
| J.Js_if (_ , c0, e0, a0), J.Js_if (_ , c1, e1, a1) ->
compares (compare_expr c0 c1) (compares (Option.make_compare compare_statement a0 a1) compare_statement) e0 e1
| J.Js_throw (_, e0), J.Js_throw (_, e1) -> compare_expr e0 e1
| J.Js_expr (_, e0), J.Js_expr (_, e1) -> compare_expr e0 e1
| J.Js_trycatch (_, b0, c0, f0), J.Js_trycatch (_, b1, c1, f1) ->
let aux_cmp (i0, e0, s0) (i1, e1, s1) =
compares (compare_ident i0 i1)
(compares (Option.make_compare compare_expr e0 e1) compare_statement) s0 s1
in
compares (compare_statement b0 b1)
(compares (Option.make_compare compare_statement f0 f1) (List.make_compare aux_cmp))
c0 c1
| J.Js_for (_, i0, c0, e0, b0), J.Js_for (_, i1, c1, e1, b1) ->
compares (Option.make_compare compare_expr i0 i1)
(compares
(Option.make_compare compare_expr c0 c1)
(compares (compare_statement b0 b1) (Option.make_compare compare_expr))
) e0 e1
| J.Js_forin (_, lhs0, rhs0, b0), J.Js_forin (_, lhs1, rhs1, b1) ->
compares (compare_expr lhs0 lhs1) (compares (compare_expr rhs0 rhs1) compare_statement) b0 b1
| J.Js_dowhile (_, b0, e0), J.Js_dowhile (_, b1, e1) ->
compares (compare_statement b0 b1) compare_expr e0 e1
| J.Js_while (_, e0, b0), J.Js_while (_, e1, b1) ->
compares (compare_statement b0 b1) compare_expr e0 e1
| J.Js_block (_, b0), J.Js_block (_, b1) ->
compare_code b0 b1
| J.Js_with (_, e0, b0), J.Js_with (_, e1, b1) ->
compares (compare_statement b0 b1) compare_expr e0 e1
| J.Js_label (_, l0, s0), J.Js_label (_, l1, s1) ->
compares (String.compare l0 l1) compare_statement s0 s1
| J.Js_empty _, J.Js_empty _ -> 0
| J.Js_comment (_, _), J.Js_comment (_, _) -> 0
| s0, s1 -> Pervasives.compare s0 s1
and compare_code s0 s1 = List.make_compare compare_statement s0 s1
(* a very conservative approximation of which expressions do observable side
* effects *)
let does_side_effects e =
JsWalk.OnlyExpr.exists
(function
| J.Je_hole _
| J.Je_new _
| J.Je_call (_,_,_,false) -> true
| J.Je_unop (_, ( J.Ju_delete
| J.Ju_add2_pre
| J.Ju_sub2_pre
| J.Ju_add2_post
| J.Ju_sub2_post), _) -> true
| J.Je_binop (_, ( J.Jb_assign
| J.Jb_mul_assign
| J.Jb_div_assign
| J.Jb_mod_assign
| J.Jb_add_assign
| J.Jb_sub_assign
| J.Jb_lsl_assign
| J.Jb_lsr_assign
| J.Jb_asr_assign
| J.Jb_and_assign
| J.Jb_xor_assign
| J.Jb_or_assign ), _, _) -> true
| J.Je_runtime (_, e) -> (
match e with
| JsAstRuntime.SetDistant _ -> true
| JsAstRuntime.TaggedString _ -> false
)
| _ -> false
) e