Skip to content

Commit 55dfb16

Browse files
committed
remove high order types in visitor, ensure the evaluation order
1 parent b7aa403 commit 55dfb16

File tree

7 files changed

+202
-175
lines changed

7 files changed

+202
-175
lines changed

jscomp/core/js_fold.ml

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44
let [@inline] option sub self = fun v ->
55
match v with
66
| None -> self
7-
| Some x -> sub x
7+
| Some x -> sub self x
8+
let rec list (sub : 'self_type -> 'a -> 'self_type) self = fun v ->
9+
match v with
10+
| [] -> self
11+
| x::xs ->
12+
let self = sub self x in
13+
list sub self xs
814
class fold =
915
object ((_self : 'self_type))
1016
method list :
@@ -14,7 +20,7 @@
1420
| [] -> _self
1521
| _x :: _x_i1 -> let _self = _f_a _self _x in let _self = _self#list _f_a _x_i1 in _self
1622
method label : label -> 'self_type = unknown _self
17-
method required_modules : required_modules -> 'self_type = _self#list (fun _self -> _self#module_id)
23+
method required_modules : required_modules -> 'self_type = list (fun _self -> _self#module_id) _self
1824
method ident : ident -> 'self_type = unknown _self
1925
method module_id : module_id -> 'self_type = fun { id = _x0;kind = _x1} -> let _self = _self#ident _x0 in _self
2026
method vident : vident -> 'self_type = function
@@ -27,7 +33,7 @@ let _self = _self#module_id _x0 in
2733
method exception_ident : exception_ident -> 'self_type = _self#ident
2834
method for_ident : for_ident -> 'self_type = _self#ident
2935
method for_direction : for_direction -> 'self_type = unknown _self
30-
method property_map : property_map -> 'self_type = _self#list (fun _self -> fun ( _x0,_x1) -> let _self = _self#expression _x1 in _self)
36+
method property_map : property_map -> 'self_type = list (fun _self -> fun ( _x0,_x1) -> let _self = _self#expression _x1 in _self) _self
3137
method length_object : length_object -> 'self_type = unknown _self
3238
method expression_desc : expression_desc -> 'self_type = function
3339
| Length ( _x0,_x1) ->
@@ -73,7 +79,7 @@ let _self = _self#expression _x1 in
7379
_self
7480
|Call ( _x0,_x1,_x2) ->
7581
let _self = _self#expression _x0 in
76-
let _self = _self#list (fun _self -> _self#expression) _x1 in
82+
let _self = list (fun _self -> _self#expression) _self _x1 in
7783
_self
7884
|String_index ( _x0,_x1) ->
7985
let _self = _self#expression _x0 in
@@ -88,26 +94,26 @@ let _self = _self#expression _x0 in
8894
_self
8995
|New ( _x0,_x1) ->
9096
let _self = _self#expression _x0 in
91-
let _self = option (_self#list (fun _self -> _self#expression)) _self _x1 in
97+
let _self = option (fun _self -> list (fun _self -> _self#expression) _self) _self _x1 in
9298
_self
9399
|Var ( _x0) ->
94100
let _self = _self#vident _x0 in
95101
_self
96102
|Fun ( _x0,_x1,_x2,_x3) ->
97-
let _self = _self#list (fun _self -> _self#ident) _x1 in
103+
let _self = list (fun _self -> _self#ident) _self _x1 in
98104
let _self = _self#block _x2 in
99105
_self
100106
|Str _ -> _self
101107
|Unicode _ -> _self
102108
|Raw_js_code _ -> _self
103109
|Array ( _x0,_x1) ->
104-
let _self = _self#list (fun _self -> _self#expression) _x0 in
110+
let _self = list (fun _self -> _self#expression) _self _x0 in
105111
_self
106112
|Optional_block ( _x0,_x1) ->
107113
let _self = _self#expression _x0 in
108114
_self
109115
|Caml_block ( _x0,_x1,_x2,_x3) ->
110-
let _self = _self#list (fun _self -> _self#expression) _x0 in
116+
let _self = list (fun _self -> _self#expression) _self _x0 in
111117
let _self = _self#expression _x2 in
112118
_self
113119
|Caml_block_tag ( _x0) ->
@@ -137,12 +143,12 @@ let _self = _self#block _x1 in
137143
let _self = _self#block _x2 in
138144
_self
139145
|While ( _x0,_x1,_x2,_x3) ->
140-
let _self = option (_self#label) _self _x0 in
146+
let _self = option (fun _self -> _self#label) _self _x0 in
141147
let _self = _self#expression _x1 in
142148
let _self = _self#block _x2 in
143149
_self
144150
|ForRange ( _x0,_x1,_x2,_x3,_x4,_x5) ->
145-
let _self = option (_self#for_ident_expression) _self _x0 in
151+
let _self = option (fun _self -> _self#for_ident_expression) _self _x0 in
146152
let _self = _self#finish_ident_expression _x1 in
147153
let _self = _self#for_ident _x2 in
148154
let _self = _self#for_direction _x3 in
@@ -157,31 +163,31 @@ let _self = _self#expression _x0 in
157163
_self
158164
|Int_switch ( _x0,_x1,_x2) ->
159165
let _self = _self#expression _x0 in
160-
let _self = _self#list (fun _self -> _self#int_clause) _x1 in
161-
let _self = option (_self#block) _self _x2 in
166+
let _self = list (fun _self -> _self#int_clause) _self _x1 in
167+
let _self = option (fun _self -> _self#block) _self _x2 in
162168
_self
163169
|String_switch ( _x0,_x1,_x2) ->
164170
let _self = _self#expression _x0 in
165-
let _self = _self#list (fun _self -> _self#string_clause) _x1 in
166-
let _self = option (_self#block) _self _x2 in
171+
let _self = list (fun _self -> _self#string_clause) _self _x1 in
172+
let _self = option (fun _self -> _self#block) _self _x2 in
167173
_self
168174
|Throw ( _x0) ->
169175
let _self = _self#expression _x0 in
170176
_self
171177
|Try ( _x0,_x1,_x2) ->
172178
let _self = _self#block _x0 in
173-
let _self = option (fun ( _x0,_x1) -> let _self = _self#exception_ident _x0 in let _self = _self#block _x1 in _self) _self _x1 in
174-
let _self = option (_self#block) _self _x2 in
179+
let _self = option (fun _self -> fun ( _x0,_x1) -> let _self = _self#exception_ident _x0 in let _self = _self#block _x1 in _self) _self _x1 in
180+
let _self = option (fun _self -> _self#block) _self _x2 in
175181
_self
176182
|Debugger -> _self
177183
method expression : expression -> 'self_type = fun { expression_desc = _x0;comment = _x1} -> let _self = _self#expression_desc _x0 in _self
178184
method statement : statement -> 'self_type = fun { statement_desc = _x0;comment = _x1} -> let _self = _self#statement_desc _x0 in _self
179185
method variable_declaration : variable_declaration -> 'self_type = fun { ident = _x0;value = _x1;property = _x2;ident_info = _x3} -> let _self = _self#ident _x0 in
180-
let _self = option (_self#expression) _self _x1 in _self
186+
let _self = option (fun _self -> _self#expression) _self _x1 in _self
181187
method string_clause : string_clause -> 'self_type = fun ( _x0,_x1) -> let _self = _self#case_clause _x1 in _self
182188
method int_clause : int_clause -> 'self_type = fun ( _x0,_x1) -> let _self = _self#case_clause _x1 in _self
183189
method case_clause : case_clause -> 'self_type = fun { switch_body = _x0;should_break = _x1;comment = _x2} -> let _self = _self#block _x0 in _self
184-
method block : block -> 'self_type = _self#list (fun _self -> _self#statement)
190+
method block : block -> 'self_type = list (fun _self -> _self#statement) _self
185191
method program : program -> 'self_type = fun { block = _x0;exports = _x1;export_set = _x2} -> let _self = _self#block _x0 in _self
186192
method deps_program : deps_program -> 'self_type = fun { program = _x0;modules = _x1;side_effect = _x2} -> let _self = _self#program _x0 in
187193
let _self = _self#required_modules _x1 in _self

jscomp/core/js_map.ml

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11

22
open J
3-
let unknown : 'a. 'a -> 'a = fun x -> x
4-
let option sub = fun v ->
3+
let [@inline] unknown : 'a. 'a -> 'a = fun x -> x
4+
let [@inline] option sub = fun v ->
55
match v with
66
| None -> None
77
| Some v -> Some (sub v)
8+
let rec list sub = fun v ->
9+
match v with
10+
| [] -> []
11+
| x::xs ->
12+
let v = sub x in
13+
v :: list sub xs
14+
(* Note we need add [v] to enforce the evaluation order
15+
it indeed cause different semantis here
16+
*)
817
class map = object
918
((_self : 'self_type))
10-
method list :
11-
'a 'a_out. ('self_type -> 'a -> 'a_out) -> 'a list -> 'a_out list =
12-
fun _f_a ->
13-
function
14-
| [] -> []
15-
| _x :: _x_i1 ->
16-
let _x = _f_a _self _x in
17-
let _x_i1 = _self#list _f_a _x_i1 in _x :: _x_i1
1819
method label : label -> label = unknown
19-
method required_modules : required_modules -> required_modules = _self#list (fun _self -> _self#module_id)
20+
method required_modules : required_modules -> required_modules = list (_self#module_id)
2021
method ident : ident -> ident = unknown
2122
method module_id : module_id -> module_id = fun { id = _x0;kind = _x1} -> let _x0 = _self#ident _x0 in {id = _x0;kind = _x1}
2223
method vident : vident -> vident = function
@@ -29,7 +30,7 @@ Qualified ( _x0,_x1)
2930
method exception_ident : exception_ident -> exception_ident = _self#ident
3031
method for_ident : for_ident -> for_ident = _self#ident
3132
method for_direction : for_direction -> for_direction = unknown
32-
method property_map : property_map -> property_map = _self#list (fun _self -> fun ( _x0,_x1) -> let _x1 = _self#expression _x1 in _x0,_x1)
33+
method property_map : property_map -> property_map = list (fun ( _x0,_x1) -> let _x1 = _self#expression _x1 in _x0,_x1)
3334
method length_object : length_object -> length_object = unknown
3435
method expression_desc : expression_desc -> expression_desc = function
3536
| Length ( _x0,_x1) ->
@@ -75,7 +76,7 @@ let _x1 = _self#expression _x1 in
7576
FlatCall ( _x0,_x1)
7677
|Call ( _x0,_x1,_x2) ->
7778
let _x0 = _self#expression _x0 in
78-
let _x1 = _self#list (fun _self -> _self#expression) _x1 in
79+
let _x1 = list (_self#expression) _x1 in
7980
Call ( _x0,_x1,_x2)
8081
|String_index ( _x0,_x1) ->
8182
let _x0 = _self#expression _x0 in
@@ -90,26 +91,26 @@ let _x0 = _self#expression _x0 in
9091
Static_index ( _x0,_x1,_x2)
9192
|New ( _x0,_x1) ->
9293
let _x0 = _self#expression _x0 in
93-
let _x1 = option (_self#list (fun _self -> _self#expression)) _x1 in
94+
let _x1 = option (list (_self#expression)) _x1 in
9495
New ( _x0,_x1)
9596
|Var ( _x0) ->
9697
let _x0 = _self#vident _x0 in
9798
Var ( _x0)
9899
|Fun ( _x0,_x1,_x2,_x3) ->
99-
let _x1 = _self#list (fun _self -> _self#ident) _x1 in
100+
let _x1 = list (_self#ident) _x1 in
100101
let _x2 = _self#block _x2 in
101102
Fun ( _x0,_x1,_x2,_x3)
102103
|Str _ as v -> v
103104
|Unicode _ as v -> v
104105
|Raw_js_code _ as v -> v
105106
|Array ( _x0,_x1) ->
106-
let _x0 = _self#list (fun _self -> _self#expression) _x0 in
107+
let _x0 = list (_self#expression) _x0 in
107108
Array ( _x0,_x1)
108109
|Optional_block ( _x0,_x1) ->
109110
let _x0 = _self#expression _x0 in
110111
Optional_block ( _x0,_x1)
111112
|Caml_block ( _x0,_x1,_x2,_x3) ->
112-
let _x0 = _self#list (fun _self -> _self#expression) _x0 in
113+
let _x0 = list (_self#expression) _x0 in
113114
let _x2 = _self#expression _x2 in
114115
Caml_block ( _x0,_x1,_x2,_x3)
115116
|Caml_block_tag ( _x0) ->
@@ -159,12 +160,12 @@ let _x0 = _self#expression _x0 in
159160
Return ( _x0)
160161
|Int_switch ( _x0,_x1,_x2) ->
161162
let _x0 = _self#expression _x0 in
162-
let _x1 = _self#list (fun _self -> _self#int_clause) _x1 in
163+
let _x1 = list (_self#int_clause) _x1 in
163164
let _x2 = option (_self#block) _x2 in
164165
Int_switch ( _x0,_x1,_x2)
165166
|String_switch ( _x0,_x1,_x2) ->
166167
let _x0 = _self#expression _x0 in
167-
let _x1 = _self#list (fun _self -> _self#string_clause) _x1 in
168+
let _x1 = list (_self#string_clause) _x1 in
168169
let _x2 = option (_self#block) _x2 in
169170
String_switch ( _x0,_x1,_x2)
170171
|Throw ( _x0) ->
@@ -183,7 +184,7 @@ let _x1 = option (_self#expression) _x1 in {ident = _x0;value = _x1;property =
183184
method string_clause : string_clause -> string_clause = fun ( _x0,_x1) -> let _x1 = _self#case_clause _x1 in _x0,_x1
184185
method int_clause : int_clause -> int_clause = fun ( _x0,_x1) -> let _x1 = _self#case_clause _x1 in _x0,_x1
185186
method case_clause : case_clause -> case_clause = fun { switch_body = _x0;should_break = _x1;comment = _x2} -> let _x0 = _self#block _x0 in {switch_body = _x0;should_break = _x1;comment = _x2}
186-
method block : block -> block = _self#list (fun _self -> _self#statement)
187+
method block : block -> block = list (_self#statement)
187188
method program : program -> program = fun { block = _x0;exports = _x1;export_set = _x2} -> let _x0 = _self#block _x0 in {block = _x0;exports = _x1;export_set = _x2}
188189
method deps_program : deps_program -> deps_program = fun { program = _x0;modules = _x1;side_effect = _x2} -> let _x0 = _self#program _x0 in
189190
let _x1 = _self#required_modules _x1 in {program = _x0;modules = _x1;side_effect = _x2}

0 commit comments

Comments
 (0)