Skip to content

Commit a011d58

Browse files
committed
introducing optimized iterator pattern
1 parent 11fd9e0 commit a011d58

File tree

9 files changed

+261
-238
lines changed

9 files changed

+261
-238
lines changed

jscomp/core/j.ml

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,38 +39,38 @@
3939
*)
4040

4141

42+
type mutable_flag = Js_op.mutable_flag
4243

43-
type label = string
44+
type binop = Js_op.binop
4445

45-
and binop = Js_op.binop
46+
type int_op = Js_op.int_op
4647

47-
and int_op = Js_op.int_op
48-
49-
and kind = Js_op.kind
48+
type kind = Js_op.kind
5049

51-
and property = Js_op.property
50+
type property = Js_op.property
5251

53-
and number = Js_op.number
52+
type number = Js_op.number
5453

55-
and mutable_flag = Js_op.mutable_flag
54+
type ident_info = Js_op.ident_info
5655

57-
and ident_info = Js_op.ident_info
56+
type exports = Js_op.exports
5857

59-
and exports = Js_op.exports
58+
type tag_info = Js_op.tag_info
6059

61-
and tag_info = Js_op.tag_info
60+
type property_name = Js_op.property_name
61+
62+
type label = string
6263

6364
and required_modules = module_id list
6465

65-
66-
66+
and ident = Ident.t (* we override `method ident` *)
6767

6868
(** object literal, if key is ident, in this case, it might be renamed by
6969
Google Closure optimizer,
7070
currently we always use quote
7171
*)
72-
and property_name = Js_op.property_name
73-
and ident = Ident.t
72+
73+
7474
and module_id = {
7575
id : ident; kind : Js_op.kind
7676
}
@@ -350,3 +350,4 @@ and deps_program =
350350
modules : required_modules ;
351351
side_effect : string option (* None: no, Some reason *)
352352
}
353+
[@@deriving]

jscomp/core/js_fold.ml

Lines changed: 38 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11

22
open J
3-
class virtual fold =
3+
let [@inline] unknown o _ = o
4+
class fold =
45
object ((o : 'self_type))
5-
method unknown : 'a. 'a -> 'self_type = fun _ -> o
6-
method string : string -> 'self_type = fun _ -> o
76
method option :
87
'a. ('self_type -> 'a -> 'self_type) -> 'a option -> 'self_type =
98
fun _f_a -> function | None -> o | Some _x -> let o = _f_a o _x in o
@@ -13,37 +12,24 @@
1312
function
1413
| [] -> o
1514
| _x :: _x_i1 -> let o = _f_a o _x in let o = o#list _f_a _x_i1 in o
16-
method int32 : int32 -> 'self_type = fun _ -> o
17-
method int : int -> 'self_type = fun _ -> o
18-
method bool : bool -> 'self_type = fun _ -> o
19-
method label : label -> 'self_type = o#string
20-
method binop : binop -> 'self_type = o#unknown
21-
method int_op : int_op -> 'self_type = o#unknown
22-
method kind : kind -> 'self_type = o#unknown
23-
method property : property -> 'self_type = o#unknown
24-
method number : number -> 'self_type = o#unknown
25-
method mutable_flag : mutable_flag -> 'self_type = o#unknown
26-
method ident_info : ident_info -> 'self_type = o#unknown
27-
method exports : exports -> 'self_type = o#unknown
28-
method tag_info : tag_info -> 'self_type = o#unknown
15+
method label : label -> 'self_type = unknown o
2916
method required_modules : required_modules -> 'self_type = o#list (fun o -> o#module_id)
30-
method property_name : property_name -> 'self_type = o#unknown
31-
method ident : ident -> 'self_type = o#unknown
17+
method ident : ident -> 'self_type = unknown o
3218
method module_id : module_id -> 'self_type = fun { id = _x0;kind = _x1} -> let o = o#ident _x0 in
33-
let o = o#unknown _x1 in o
19+
let o = unknown o _x1 in o
3420
method vident : vident -> 'self_type = function
3521
| Id ( _x0) ->
3622
let o = o#ident _x0 in
3723
o
3824
|Qualified ( _x0,_x1) ->
3925
let o = o#module_id _x0 in
40-
let o = o#option (fun o -> o#string) _x1 in
26+
let o = o#option (fun o -> unknown o) _x1 in
4127
o
4228
method exception_ident : exception_ident -> 'self_type = o#ident
4329
method for_ident : for_ident -> 'self_type = o#ident
44-
method for_direction : for_direction -> 'self_type = o#unknown
45-
method property_map : property_map -> 'self_type = o#list (fun o -> fun ( _x0,_x1) -> let o = o#property_name _x0 in let o = o#expression _x1 in o)
46-
method length_object : length_object -> 'self_type = o#unknown
30+
method for_direction : for_direction -> 'self_type = unknown o
31+
method property_map : property_map -> 'self_type = o#list (fun o -> fun ( _x0,_x1) -> let o = unknown o _x0 in let o = o#expression _x1 in o)
32+
method length_object : length_object -> 'self_type = unknown o
4733
method expression_desc : expression_desc -> 'self_type = function
4834
| Length ( _x0,_x1) ->
4935
let o = o#expression _x0 in
@@ -63,7 +49,7 @@ let o = o#expression _x0 in
6349
let o = o#expression _x1 in
6450
o
6551
|Bool ( _x0) ->
66-
let o = o#bool _x0 in
52+
let o = unknown o _x0 in
6753
o
6854
|Typeof ( _x0) ->
6955
let o = o#expression _x0 in
@@ -81,7 +67,7 @@ let o = o#expression _x1 in
8167
let o = o#expression _x2 in
8268
o
8369
|Bin ( _x0,_x1,_x2) ->
84-
let o = o#binop _x0 in
70+
let o = unknown o _x0 in
8571
let o = o#expression _x1 in
8672
let o = o#expression _x2 in
8773
o
@@ -92,7 +78,7 @@ let o = o#expression _x1 in
9278
|Call ( _x0,_x1,_x2) ->
9379
let o = o#expression _x0 in
9480
let o = o#list (fun o -> o#expression) _x1 in
95-
let o = o#unknown _x2 in
81+
let o = unknown o _x2 in
9682
o
9783
|String_index ( _x0,_x1) ->
9884
let o = o#expression _x0 in
@@ -104,8 +90,8 @@ let o = o#expression _x1 in
10490
o
10591
|Static_index ( _x0,_x1,_x2) ->
10692
let o = o#expression _x0 in
107-
let o = o#string _x1 in
108-
let o = o#option (fun o -> o#int32) _x2 in
93+
let o = unknown o _x1 in
94+
let o = o#option (fun o -> unknown o) _x2 in
10995
o
11096
|New ( _x0,_x1) ->
11197
let o = o#expression _x0 in
@@ -115,40 +101,40 @@ let o = o#option (fun o -> o#list (fun o -> o#expression)) _x1 in
115101
let o = o#vident _x0 in
116102
o
117103
|Fun ( _x0,_x1,_x2,_x3) ->
118-
let o = o#bool _x0 in
104+
let o = unknown o _x0 in
119105
let o = o#list (fun o -> o#ident) _x1 in
120106
let o = o#block _x2 in
121-
let o = o#unknown _x3 in
107+
let o = unknown o _x3 in
122108
o
123109
|Str ( _x0,_x1) ->
124-
let o = o#bool _x0 in
125-
let o = o#string _x1 in
110+
let o = unknown o _x0 in
111+
let o = unknown o _x1 in
126112
o
127113
|Unicode ( _x0) ->
128-
let o = o#string _x0 in
114+
let o = unknown o _x0 in
129115
o
130116
|Raw_js_code ( _x0) ->
131-
let o = o#unknown _x0 in
117+
let o = unknown o _x0 in
132118
o
133119
|Array ( _x0,_x1) ->
134120
let o = o#list (fun o -> o#expression) _x0 in
135-
let o = o#mutable_flag _x1 in
121+
let o = unknown o _x1 in
136122
o
137123
|Optional_block ( _x0,_x1) ->
138124
let o = o#expression _x0 in
139-
let o = o#bool _x1 in
125+
let o = unknown o _x1 in
140126
o
141127
|Caml_block ( _x0,_x1,_x2,_x3) ->
142128
let o = o#list (fun o -> o#expression) _x0 in
143-
let o = o#mutable_flag _x1 in
129+
let o = unknown o _x1 in
144130
let o = o#expression _x2 in
145-
let o = o#tag_info _x3 in
131+
let o = unknown o _x3 in
146132
o
147133
|Caml_block_tag ( _x0) ->
148134
let o = o#expression _x0 in
149135
o
150136
|Number ( _x0) ->
151-
let o = o#number _x0 in
137+
let o = unknown o _x0 in
152138
o
153139
|Object ( _x0) ->
154140
let o = o#property_map _x0 in
@@ -176,15 +162,15 @@ let o = o#block _x2 in
176162
let o = o#option (fun o -> o#label) _x0 in
177163
let o = o#expression _x1 in
178164
let o = o#block _x2 in
179-
let o = o#unknown _x3 in
165+
let o = unknown o _x3 in
180166
o
181167
|ForRange ( _x0,_x1,_x2,_x3,_x4,_x5) ->
182168
let o = o#option (fun o -> o#for_ident_expression) _x0 in
183169
let o = o#finish_ident_expression _x1 in
184170
let o = o#for_ident _x2 in
185171
let o = o#for_direction _x3 in
186172
let o = o#block _x4 in
187-
let o = o#unknown _x5 in
173+
let o = unknown o _x5 in
188174
o
189175
|Continue ( _x0) ->
190176
let o = o#label _x0 in
@@ -213,24 +199,24 @@ let o = o#option (fun o -> o#block) _x2 in
213199
o
214200
|Debugger -> o
215201
method expression : expression -> 'self_type = fun { expression_desc = _x0;comment = _x1} -> let o = o#expression_desc _x0 in
216-
let o = o#option (fun o -> o#string) _x1 in o
202+
let o = o#option (fun o -> unknown o) _x1 in o
217203
method statement : statement -> 'self_type = fun { statement_desc = _x0;comment = _x1} -> let o = o#statement_desc _x0 in
218-
let o = o#option (fun o -> o#string) _x1 in o
204+
let o = o#option (fun o -> unknown o) _x1 in o
219205
method variable_declaration : variable_declaration -> 'self_type = fun { ident = _x0;value = _x1;property = _x2;ident_info = _x3} -> let o = o#ident _x0 in
220206
let o = o#option (fun o -> o#expression) _x1 in
221-
let o = o#property _x2 in
222-
let o = o#ident_info _x3 in o
223-
method string_clause : string_clause -> 'self_type = fun ( _x0,_x1) -> let o = o#string _x0 in let o = o#case_clause _x1 in o
224-
method int_clause : int_clause -> 'self_type = fun ( _x0,_x1) -> let o = o#int _x0 in let o = o#case_clause _x1 in o
207+
let o = unknown o _x2 in
208+
let o = unknown o _x3 in o
209+
method string_clause : string_clause -> 'self_type = fun ( _x0,_x1) -> let o = unknown o _x0 in let o = o#case_clause _x1 in o
210+
method int_clause : int_clause -> 'self_type = fun ( _x0,_x1) -> let o = unknown o _x0 in let o = o#case_clause _x1 in o
225211
method case_clause : case_clause -> 'self_type = fun { switch_body = _x0;should_break = _x1;comment = _x2} -> let o = o#block _x0 in
226-
let o = o#bool _x1 in
227-
let o = o#option (fun o -> o#string) _x2 in o
212+
let o = unknown o _x1 in
213+
let o = o#option (fun o -> unknown o) _x2 in o
228214
method block : block -> 'self_type = o#list (fun o -> o#statement)
229215
method program : program -> 'self_type = fun { block = _x0;exports = _x1;export_set = _x2} -> let o = o#block _x0 in
230-
let o = o#exports _x1 in
231-
let o = o#unknown _x2 in o
216+
let o = unknown o _x1 in
217+
let o = unknown o _x2 in o
232218
method deps_program : deps_program -> 'self_type = fun { program = _x0;modules = _x1;side_effect = _x2} -> let o = o#program _x0 in
233219
let o = o#required_modules _x1 in
234-
let o = o#option (fun o -> o#string) _x2 in o
220+
let o = o#option (fun o -> unknown o) _x2 in o
235221
end
236222

0 commit comments

Comments
 (0)