Skip to content

Commit 2cdf2c2

Browse files
committed
remove unused code and fix rescript-lang#2559
1 parent 471d8be commit 2cdf2c2

22 files changed

+236
-148
lines changed

jscomp/core/js_dump.ml

+4-2
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,8 @@ and
471471
else
472472
begin
473473
P.string f L.app_array;
474-
P.paren_group f 1 (fun _ -> arguments cxt f [ e ; E.arr Mutable el])
474+
P.paren_group f 1
475+
(fun _ -> arguments cxt f [ e ; E.array Mutable el])
475476
end)
476477
in
477478
if l > 15 then P.paren_group f 1 action
@@ -880,7 +881,8 @@ and
880881
P.string f L.caml_block;
881882
P.string f L.dot ;
882883
P.string f L.caml_block_create;
883-
P.paren_group f 1 (fun _ -> arguments cxt f [tag; E.arr mutable_flag el])
884+
P.paren_group f 1
885+
(fun _ -> arguments cxt f [tag; E.array mutable_flag el])
884886
end
885887
| Caml_block_tag e ->
886888
P.group f 1 (fun _ ->

jscomp/core/js_exp_make.ml

+45-37
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,42 @@
2929

3030
let no_side_effect = Js_analyzer.no_side_effect_expression
3131

32+
type t = J.expression
33+
3234
type binary_op =
3335
?comment:string ->
34-
J.expression ->
35-
J.expression ->
36-
J.expression
36+
t ->
37+
t ->
38+
t
3739
type unary_op =
3840
?comment:string ->
39-
J.expression ->
40-
J.expression
41+
t ->
42+
t
43+
4144

4245
(*
43-
Remove pure part of the expression
46+
[remove_pure_sub_exp x]
47+
Remove pure part of the expression (minor optimization)
4448
and keep the non-pure part while preserve the semantics
4549
(modulo return value)
50+
It will return None if [x] is pure
4651
*)
47-
let rec extract_non_pure (x : J.expression) =
52+
let rec remove_pure_sub_exp (x : t) : t option =
4853
match x.expression_desc with
4954
| Var _
5055
| Str _
5156
| Number _ -> None (* Can be refined later *)
5257
| Access (a,b) ->
53-
begin match extract_non_pure a , extract_non_pure b with
58+
begin match remove_pure_sub_exp a , remove_pure_sub_exp b with
5459
| None, None -> None
5560
| _, _ -> Some x
5661
end
5762
| Array (xs,_mutable_flag) ->
58-
if List.for_all (fun x -> extract_non_pure x = None) xs then
63+
if List.for_all (fun x -> remove_pure_sub_exp x = None) xs then
5964
None
6065
else Some x
6166
| Seq (a,b) ->
62-
begin match extract_non_pure a , extract_non_pure b with
67+
begin match remove_pure_sub_exp a , remove_pure_sub_exp b with
6368
| None, None -> None
6469
| Some u, Some v ->
6570
Some { x with expression_desc = Seq(u,v)}
@@ -69,20 +74,28 @@ let rec extract_non_pure (x : J.expression) =
6974
end
7075
| _ -> Some x
7176

72-
type t = J.expression
7377

74-
let mk ?comment exp : t =
75-
{expression_desc = exp ; comment }
78+
(* let mk ?comment exp : t =
79+
{expression_desc = exp ; comment } *)
7680

7781
let var ?comment id : t =
7882
{expression_desc = Var (Id id); comment }
7983

84+
let call ?comment ~info e0 args : t =
85+
{expression_desc = Call(e0,args,info); comment }
86+
87+
let flat_call ?comment e0 es : t =
88+
(* TODO: optimization when es is known at compile time
89+
to be an array
90+
*)
91+
{expression_desc = FlatCall (e0,es); comment }
92+
8093
let runtime_var_dot ?comment (x : string) (e1 : string) : J.expression =
8194
{expression_desc =
8295
Var (Qualified(Ext_ident.create_js x,Runtime, Some e1)); comment }
8396

84-
let runtime_var_vid x e1 : J.vident =
85-
Qualified(Ext_ident.create_js x,Runtime, Some e1)
97+
(* let runtime_var_vid x e1 : J.vident =
98+
Qualified(Ext_ident.create_js x,Runtime, Some e1) *)
8699

87100
let ml_var_dot ?comment ( id : Ident.t) e : J.expression =
88101
{expression_desc = Var (Qualified(id, Ml, Some e)); comment }
@@ -97,8 +110,17 @@ let external_var_dot ?comment ~external_name:name ?dot (id : Ident.t) : t =
97110
{expression_desc = Var (Qualified(id, External name, dot)); comment }
98111

99112

100-
let ml_var ?comment (id : Ident.t) : t =
101-
{expression_desc = Var (Qualified (id, Ml, None)); comment}
113+
(* let ml_var ?comment (id : Ident.t) : t =
114+
{expression_desc = Var (Qualified (id, Ml, None)); comment} *)
115+
116+
(* Dot .....................**)
117+
let runtime_call ?comment module_name fn_name args =
118+
call ?comment
119+
~info:Js_call_info.builtin_runtime_call
120+
(runtime_var_dot module_name fn_name) args
121+
122+
let runtime_ref module_name fn_name =
123+
runtime_var_dot module_name fn_name
102124

103125
let str ?(pure=true) ?comment s : t =
104126
{expression_desc = Str (pure,s); comment}
@@ -109,12 +131,13 @@ let unicode ?comment s : t =
109131
let raw_js_code ?comment info s : t =
110132
{expression_desc = Raw_js_code (s,info) ; comment }
111133

134+
(* TODO: could optimize literal *)
112135
let anything_to_string ?comment (e : t) : t =
113136
match e.expression_desc with
114137
| Str _ -> e
115138
| _ -> {expression_desc = Anything_to_string e ; comment}
116139

117-
let arr ?comment mt es : t =
140+
let array ?comment mt es : t =
118141
{expression_desc = Array (es,mt) ; comment}
119142

120143
let sep = " : "
@@ -150,10 +173,10 @@ let make_block ?comment tag tag_info es mutable_flag : t =
150173
(* let uninitialized_object ?comment tag size : t =
151174
{ expression_desc = Caml_uninitialized_obj(tag,size); comment } *)
152175

153-
let uninitialized_array ?comment (e : t) : t =
176+
(* let uninitialized_array ?comment (e : t) : t =
154177
match e.expression_desc with
155-
| Number (Int {i = 0l; _}) -> arr ?comment NA []
156-
| _ -> {comment; expression_desc = Array_of_size e}
178+
| Number (Int {i = 0l; _}) -> array ?comment NA []
179+
| _ -> {comment; expression_desc = Array_of_size e} *)
157180

158181

159182
module L = Literals
@@ -340,23 +363,8 @@ let index_addr ?comment ~yes ~no (e0 : t) e1 : t =
340363
| _ ->
341364
yes ({ expression_desc = Access (e0, int ?comment e1); comment = None} : t)
342365

343-
let call ?comment ~info e0 args : t =
344-
{expression_desc = Call(e0,args,info); comment }
345-
346-
let flat_call ?comment e0 es : t =
347-
(* TODO: optimization when es is known at compile time
348-
to be an array
349-
*)
350-
{expression_desc = FlatCall (e0,es); comment }
351366

352-
(* Dot .....................**)
353-
let runtime_call ?comment module_name fn_name args =
354-
call ?comment
355-
~info:Js_call_info.builtin_runtime_call
356-
(runtime_var_dot module_name fn_name) args
357367

358-
let runtime_ref module_name fn_name =
359-
runtime_var_dot module_name fn_name
360368

361369

362370
(* only used in property access,
@@ -907,7 +915,7 @@ let public_method_call meth_name obj label cache args =
907915
[label;
908916
int cache;
909917
obj ;
910-
arr NA (obj::args)
918+
array NA (obj::args)
911919
]
912920

913921
(* TODO: handle arbitrary length of args ..

jscomp/core/js_exp_make.mli

+40-14
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
]}
5151
*)
5252
type t = J.expression
53-
val extract_non_pure : t -> t option
53+
54+
val remove_pure_sub_exp : t -> t option
5455

5556
type binary_op = ?comment:string -> t -> t -> t
5657

@@ -63,31 +64,48 @@ type unary_op = ?comment:string -> t -> t
6364
val ocaml_boolean_under_condition : t -> t
6465

6566

66-
(* val bin : ?comment:string -> J.binop -> t -> t -> t *)
67-
val mk :
68-
?comment:string -> J.expression_desc -> t
67+
68+
(* val mk :
69+
?comment:string -> J.expression_desc -> t *)
6970

7071
val access : binary_op
7172

7273
val string_access : binary_op
7374

7475
val var : ?comment:string -> J.ident -> t
7576

76-
val runtime_var_dot : ?comment:string -> string -> string -> t
77+
(* val runtime_var_dot : ?comment:string -> string -> string -> t *)
7778

78-
val runtime_var_vid : string -> string -> J.vident
79+
(* val runtime_var_vid : string -> string -> J.vident *)
7980

81+
(** [ml_var_dot ocaml_module name]
82+
*)
8083
val ml_var_dot : ?comment:string -> Ident.t -> string -> t
8184

82-
val external_var_dot : ?comment:string -> external_name:string -> ?dot:string -> Ident.t -> t
83-
85+
(** [external_var_dot ~external_name ~dot id]
86+
Used in FFI
87+
*)
88+
val external_var_dot :
89+
?comment:string ->
90+
external_name:string ->
91+
?dot:string ->
92+
Ident.t ->
93+
t
8494

95+
val runtime_call :
96+
?comment:string ->
97+
string -> (* module_name *)
98+
string -> (* fn_name *)
99+
t list -> (* args *)
100+
t
85101

86-
val ml_var : ?comment:string -> Ident.t -> t
102+
val runtime_ref :
103+
string ->
104+
string ->
105+
t
87106

88-
val runtime_call : ?comment:string -> string -> string -> t list -> t
89107
val public_method_call : string -> t -> t -> Int32.t -> t list -> t
90-
val runtime_ref : string -> string -> t
108+
91109

92110
val str :
93111
?pure:bool ->
@@ -224,16 +242,24 @@ val to_json_string : unary_op
224242

225243
val new_ : ?comment:string -> J.expression -> J.expression list -> t
226244

227-
val arr : ?comment:string -> J.mutable_flag -> J.expression list -> t
245+
val array :
246+
?comment:string ->
247+
J.mutable_flag ->
248+
J.expression list ->
249+
t
228250

229251
val make_block :
230252
?comment:string ->
231-
J.expression -> J.tag_info -> J.expression list -> J.mutable_flag -> t
253+
J.expression -> (* tag *)
254+
J.tag_info -> (* tag_info *)
255+
J.expression list ->
256+
J.mutable_flag ->
257+
t
232258

233259
(* val uninitialized_object :
234260
?comment:string -> J.expression -> J.expression -> t *)
235261

236-
val uninitialized_array : unary_op
262+
(* val uninitialized_array : unary_op *)
237263

238264
val seq : binary_op
239265
val fuse_to_seq : t -> t list -> t

jscomp/core/js_of_lam_array.ml

+6-3
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,12 @@ module E = Js_exp_make
5858
let make_array mt (kind : Lambda.array_kind) args =
5959
match kind with
6060
| Pgenarray
61-
| Paddrarray -> E.arr ~comment:"array" mt args
62-
| Pintarray -> E.arr ~comment:"int array" mt args
63-
| Pfloatarray -> E.arr ~comment:"float array" mt args
61+
| Paddrarray ->
62+
E.array ~comment:"array" mt args
63+
| Pintarray ->
64+
E.array ~comment:"int array" mt args
65+
| Pfloatarray ->
66+
E.array ~comment:"float array" mt args
6467

6568
let set_array e e0 e1 =
6669
E.assign (E.access e e0) e1

jscomp/core/lam_compile.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ and compile_recursive_let ~all_bindings
304304
and need to be declared first
305305
*)
306306
Js_output.of_block (
307-
S.define ~kind:Variable id (E.arr Mutable []) ::
307+
S.define ~kind:Variable id (E.array Mutable []) ::
308308
(List.mapi (fun i (x : Lam.t) ->
309309
match x with
310310
| Lvar lid
@@ -978,8 +978,8 @@ and
978978
| ReturnFalse , {block = []; value = Some out1},
979979
{block = []; value = Some out2} ->
980980
begin
981-
match Js_exp_make.extract_non_pure out1 ,
982-
Js_exp_make.extract_non_pure out2 with
981+
match Js_exp_make.remove_pure_sub_exp out1 ,
982+
Js_exp_make.remove_pure_sub_exp out2 with
983983
| None, None -> Js_output.make (Ext_list.append b [ S.exp e])
984984
(* FIX #1762 *)
985985
| Some out1, Some out2 ->

jscomp/core/lam_dispatch_primitive.ml

+4-4
Original file line numberDiff line numberDiff line change
@@ -428,15 +428,15 @@ let translate loc (prim_name : string)
428428
| _ -> assert false
429429
end
430430
| "caml_create_string" ->
431+
(* Bytes.create *)
431432
(* Note that for invalid range, JS raise an Exception RangeError,
432433
here in OCaml it's [Invalid_argument], we have to preserve this semantics.
433434
Also, it's creating a [bytes] which is a js array actually.
434435
*)
435436
begin match args with
436-
| [{expression_desc = Number (Int {i; _}); _} as v]
437-
when i >= 0l ->
438-
E.uninitialized_array v
439-
(* TODO: inline and spits out a warning when i is negative *)
437+
| [{expression_desc = Number (Int {i = 0l; _}); _}]
438+
->
439+
E.array NA []
440440
| _ ->
441441
call Js_runtime_modules.string
442442
end

jscomp/runtime/caml_string.ml

+7-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,13 @@ let caml_string_get s i=
5454
let caml_create_string len : bytes =
5555
(* Node raise [RangeError] exception *)
5656
if len < 0 then raise (Invalid_argument "String.create")
57-
else new_uninitialized len
57+
else
58+
let result = new_uninitialized len in
59+
for i = 0 to len - 1 do
60+
unsafe_set result i '\000'
61+
done ;
62+
result
63+
5864

5965

6066

jscomp/test/bytes_split_gpr_743_test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function eq(loc, param) {
2828
return /* () */0;
2929
}
3030

31-
var b = new Array(3);
31+
var b = Caml_string.caml_create_string(3);
3232

3333
b[0] = /* "a" */97;
3434

@@ -47,7 +47,7 @@ eq("File \"bytes_split_gpr_743_test.ml\", line 17, characters 5-12", /* tuple */
4747
res
4848
]);
4949

50-
var b$1 = new Array(3);
50+
var b$1 = Caml_string.caml_create_string(3);
5151

5252
b$1[0] = /* "a" */97;
5353

jscomp/test/ocaml_parsetree_test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10393,7 +10393,7 @@ var keyword_table = create_hashtable(149, /* :: */[
1039310393
]
1039410394
]);
1039510395

10396-
var initial_string_buffer = new Array(256);
10396+
var initial_string_buffer = Caml_string.caml_create_string(256);
1039710397

1039810398
var string_buff = [initial_string_buffer];
1039910399

jscomp/test/ocaml_re_test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1941,8 +1941,8 @@ function colorize(c, regexp) {
19411941
}
19421942

19431943
function flatten_cmap(cm) {
1944-
var c = new Array(256);
1945-
var col_repr = new Array(256);
1944+
var c = Caml_string.caml_create_string(256);
1945+
var col_repr = Caml_string.caml_create_string(256);
19461946
var v = 0;
19471947
c[0] = /* "\000" */0;
19481948
col_repr[0] = /* "\000" */0;

0 commit comments

Comments
 (0)