Skip to content

Commit 016d321

Browse files
authored
Merge pull request #4208 from BuckleScript/efficient_let_local
efficient implemenation of let%private
2 parents f4ce3d5 + 87717b9 commit 016d321

File tree

7 files changed

+246
-156
lines changed

7 files changed

+246
-156
lines changed

jscomp/syntax/bs_builtin_ppx.ml

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,6 @@ let signature_item_mapper (self : mapper) (sigi : Parsetree.signature_item) =
270270
)
271271
| _ -> default_mapper.signature_item self sigi
272272

273-
let local_module_name =
274-
let v = ref 0 in
275-
fun () ->
276-
incr v ;
277-
"local_" ^ (string_of_int !v)
278273

279274
let structure_item_mapper (self : mapper) (str : Parsetree.structure_item) =
280275
match str.pstr_desc with
@@ -338,50 +333,68 @@ let structure_item_mapper (self : mapper) (str : Parsetree.structure_item) =
338333
| _ -> default_mapper.structure_item self str
339334

340335

336+
let local_module_name =
337+
let v = ref 0 in
338+
fun () ->
339+
incr v ;
340+
"local_" ^ string_of_int !v
341+
342+
343+
let expand_reverse (stru : Ast_structure.t) (acc : Ast_structure.t) : Ast_structure.t =
344+
if stru = [] then acc
345+
else begin
346+
Ext_list.iter stru Typemod_hide.check;
347+
let local_module_name = local_module_name () in
348+
let last_loc = (List.hd stru).pstr_loc in
349+
let stru = List.rev stru in
350+
let first_loc = (List.hd stru).pstr_loc in
351+
let loc = {first_loc with loc_end = last_loc.loc_end; } in
352+
let open Ast_helper in
353+
Str.module_
354+
~loc
355+
{ pmb_name = {txt = local_module_name; loc};
356+
pmb_expr = {
357+
pmod_desc= Pmod_structure stru;
358+
pmod_loc = loc;
359+
pmod_attributes = [] };
360+
pmb_attributes = Typemod_hide.attrs; pmb_loc = loc} ::
361+
Str.open_ ~loc {
362+
popen_lid = {txt = Lident local_module_name; loc};
363+
popen_override = Override;
364+
popen_loc = loc;
365+
popen_attributes = []
366+
} :: acc
367+
end
368+
369+
341370
let rec
342-
structure_mapper (self : mapper) stru =
371+
structure_mapper (self : mapper) (stru : Ast_structure.t) =
343372
match stru with
344373
| [] -> []
345374
| item::rest ->
346-
let new_x = self.structure_item self item in
347-
match new_x.pstr_desc with
375+
match item.pstr_desc with
348376
| Pstr_extension (({txt = ("bs.debugger.chrome" | "debugger.chrome") ;loc}, _),_)
349377
->
350378
Location.prerr_warning loc (Preprocessor "this extension can be safely removed");
351-
(structure_mapper self rest)
379+
structure_mapper self rest
352380
| Pstr_extension ( ({txt = ("bs.raw"| "raw") ; loc}, payload), _attrs)
353381
->
354-
Ast_exp_handle_external.handle_raw_structure loc payload :: (structure_mapper self rest)
355-
| Pstr_extension (({txt = "local"; loc}, payload),_)
382+
Ast_exp_handle_external.handle_raw_structure loc payload :: structure_mapper self rest
383+
| Pstr_extension (({txt = "private"}, _),_)
356384
->
357-
begin match payload with
358-
| PStr stru ->
359-
(* check no module, no type allowed *)
360-
(* let stru = self.structure self stru in *)
361-
Ext_list.iter stru Typemod_hide.check;
362-
let local_module_name = local_module_name () in
363-
let open Ast_helper in
364-
Str.module_
365-
~loc
366-
{ pmb_name = {txt = local_module_name; loc};
367-
pmb_expr = {
368-
pmod_desc= Pmod_structure stru;
369-
pmod_loc = loc;
370-
pmod_attributes = [] };
371-
pmb_attributes = Typemod_hide.attrs; pmb_loc = loc} ::
372-
Str.open_ ~loc {
373-
popen_lid = {txt = Lident local_module_name; loc};
374-
popen_override = Override;
375-
popen_loc = loc;
376-
popen_attributes = []
377-
} :: structure_mapper self rest
378-
| PSig _
379-
| PTyp _
380-
| PPat _ ->
381-
Location.raise_errorf ~loc "local extension is not support"
382-
end
385+
let rec aux acc (rest : Ast_structure.t) =
386+
match rest with
387+
| {pstr_desc = Pstr_extension (({txt = "private";loc}, payload), _) } :: next ->
388+
begin match payload with
389+
| PStr work ->
390+
aux (Ext_list.rev_map_append work acc (fun x -> self.structure_item self x)) next
391+
| (PSig _ | PTyp _ | PPat _) ->
392+
Location.raise_errorf ~loc "private extension is not support"
393+
end
394+
| _ -> expand_reverse acc (structure_mapper self rest)
395+
in aux [] stru
383396
| _ ->
384-
new_x :: (structure_mapper self rest)
397+
self.structure_item self item :: structure_mapper self rest
385398

386399
let unsafe_mapper : mapper =
387400
{ default_mapper with

jscomp/test/internal_unused_test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,28 @@ function h(a, b) {
1616
return a + b | 0;
1717
}
1818

19+
var h1 = 2;
20+
21+
var h2 = h1 + 1 | 0;
22+
23+
var h4 = 2;
24+
25+
var h5 = h4 + 1 | 0;
26+
27+
var b = 5;
28+
29+
var N = {
30+
b: b
31+
};
32+
33+
console.log(h5);
34+
35+
console.log(h2);
36+
1937
console.log(c);
2038

2139
console.log(h(1, 2));
2240

2341
exports.f = f;
42+
exports.N = N;
2443
/* Not a pure module */

jscomp/test/internal_unused_test.ml

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,36 @@ open! P1
1414

1515
let f () = raise A
1616

17-
let%local b = 3
17+
let%private b = 3
1818

19-
let%local c = b + 2
19+
let%private c = b + 2
2020

21-
[%%local
21+
[%%private
2222
let d = c
2323
let f = d
2424
let h = fun[@bs] a b -> a + b
2525
]
2626

2727

28+
let%private h0 = 1
29+
30+
let%private h1 = h0 + 1
31+
32+
let%private h2 = h1 + 1
33+
34+
[%%private
35+
let h3 = 1
36+
let h4 = h3 + 1
37+
let h5 = h4 + 1
38+
]
39+
40+
module N = struct
41+
let %private a = 3
42+
let b = a + 2
43+
end
44+
45+
;; Js.log h5
46+
;; Js.log h2
2847

2948
;; Js.log f
3049

lib/4.06.1/unstable/js_compiler.ml

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -315455,11 +315455,6 @@ let signature_item_mapper (self : mapper) (sigi : Parsetree.signature_item) =
315455315455
)
315456315456
| _ -> default_mapper.signature_item self sigi
315457315457

315458-
let local_module_name =
315459-
let v = ref 0 in
315460-
fun () ->
315461-
incr v ;
315462-
"local_" ^ (string_of_int !v)
315463315458

315464315459
let structure_item_mapper (self : mapper) (str : Parsetree.structure_item) =
315465315460
match str.pstr_desc with
@@ -315523,50 +315518,68 @@ let structure_item_mapper (self : mapper) (str : Parsetree.structure_item) =
315523315518
| _ -> default_mapper.structure_item self str
315524315519

315525315520

315521+
let local_module_name =
315522+
let v = ref 0 in
315523+
fun () ->
315524+
incr v ;
315525+
"local_" ^ string_of_int !v
315526+
315527+
315528+
let expand_reverse (stru : Ast_structure.t) (acc : Ast_structure.t) : Ast_structure.t =
315529+
if stru = [] then acc
315530+
else begin
315531+
Ext_list.iter stru Typemod_hide.check;
315532+
let local_module_name = local_module_name () in
315533+
let last_loc = (List.hd stru).pstr_loc in
315534+
let stru = List.rev stru in
315535+
let first_loc = (List.hd stru).pstr_loc in
315536+
let loc = {first_loc with loc_end = last_loc.loc_end; } in
315537+
let open Ast_helper in
315538+
Str.module_
315539+
~loc
315540+
{ pmb_name = {txt = local_module_name; loc};
315541+
pmb_expr = {
315542+
pmod_desc= Pmod_structure stru;
315543+
pmod_loc = loc;
315544+
pmod_attributes = [] };
315545+
pmb_attributes = Typemod_hide.attrs; pmb_loc = loc} ::
315546+
Str.open_ ~loc {
315547+
popen_lid = {txt = Lident local_module_name; loc};
315548+
popen_override = Override;
315549+
popen_loc = loc;
315550+
popen_attributes = []
315551+
} :: acc
315552+
end
315553+
315554+
315526315555
let rec
315527-
structure_mapper (self : mapper) stru =
315556+
structure_mapper (self : mapper) (stru : Ast_structure.t) =
315528315557
match stru with
315529315558
| [] -> []
315530315559
| item::rest ->
315531-
let new_x = self.structure_item self item in
315532-
match new_x.pstr_desc with
315560+
match item.pstr_desc with
315533315561
| Pstr_extension (({txt = ("bs.debugger.chrome" | "debugger.chrome") ;loc}, _),_)
315534315562
->
315535315563
Location.prerr_warning loc (Preprocessor "this extension can be safely removed");
315536-
(structure_mapper self rest)
315564+
structure_mapper self rest
315537315565
| Pstr_extension ( ({txt = ("bs.raw"| "raw") ; loc}, payload), _attrs)
315538315566
->
315539-
Ast_exp_handle_external.handle_raw_structure loc payload :: (structure_mapper self rest)
315540-
| Pstr_extension (({txt = "local"; loc}, payload),_)
315567+
Ast_exp_handle_external.handle_raw_structure loc payload :: structure_mapper self rest
315568+
| Pstr_extension (({txt = "private"}, _),_)
315541315569
->
315542-
begin match payload with
315543-
| PStr stru ->
315544-
(* check no module, no type allowed *)
315545-
(* let stru = self.structure self stru in *)
315546-
Ext_list.iter stru Typemod_hide.check;
315547-
let local_module_name = local_module_name () in
315548-
let open Ast_helper in
315549-
Str.module_
315550-
~loc
315551-
{ pmb_name = {txt = local_module_name; loc};
315552-
pmb_expr = {
315553-
pmod_desc= Pmod_structure stru;
315554-
pmod_loc = loc;
315555-
pmod_attributes = [] };
315556-
pmb_attributes = Typemod_hide.attrs; pmb_loc = loc} ::
315557-
Str.open_ ~loc {
315558-
popen_lid = {txt = Lident local_module_name; loc};
315559-
popen_override = Override;
315560-
popen_loc = loc;
315561-
popen_attributes = []
315562-
} :: structure_mapper self rest
315563-
| PSig _
315564-
| PTyp _
315565-
| PPat _ ->
315566-
Location.raise_errorf ~loc "local extension is not support"
315567-
end
315570+
let rec aux acc (rest : Ast_structure.t) =
315571+
match rest with
315572+
| {pstr_desc = Pstr_extension (({txt = "private";loc}, payload), _) } :: next ->
315573+
begin match payload with
315574+
| PStr work ->
315575+
aux (Ext_list.rev_map_append work acc (fun x -> self.structure_item self x)) next
315576+
| (PSig _ | PTyp _ | PPat _) ->
315577+
Location.raise_errorf ~loc "private extension is not support"
315578+
end
315579+
| _ -> expand_reverse acc (structure_mapper self rest)
315580+
in aux [] stru
315568315581
| _ ->
315569-
new_x :: (structure_mapper self rest)
315582+
self.structure_item self item :: structure_mapper self rest
315570315583

315571315584
let unsafe_mapper : mapper =
315572315585
{ default_mapper with

0 commit comments

Comments
 (0)