Skip to content

Commit c28c6ae

Browse files
authored
Merge pull request #4206 from BuckleScript/more_performant_structure_support
2 parents 305bf23 + 8a1af69 commit c28c6ae

19 files changed

+22608
-22042
lines changed

jscomp/core/bs_conditional_initial.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
(* Clflags.keep_locs := false; *)
2828
let setup_env () =
2929
Env.Persistent_signature.load := Bs_cmi_load.load_cmi;
30+
Typemod.should_hide := Typemod_hide.should_hide;
3031
Clflags.no_std_include := true;
3132
Warnings.parse_options false Bsc_warnings.defaults_w;
3233
Warnings.parse_options true Bsc_warnings.defaults_warn_error;

jscomp/ext/ext_list.ml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,10 @@ let init n f =
364364

365365
let rec rev_append l1 l2 =
366366
match l1 with
367-
[] -> l2
368-
| a :: l -> rev_append l (a :: l2)
367+
| [] -> l2
368+
| [a0] -> a0::l2 (* single element is common *)
369+
| [a0 ; a1] -> a1 :: a0 :: l2
370+
| a0::a1::a2::rest -> rev_append rest (a2::a1::a0::l2)
369371

370372
let rev l = rev_append l []
371373

@@ -442,7 +444,16 @@ let rec rev_map_append l1 l2 f =
442444
let rec flat_map_aux f acc append lx =
443445
match lx with
444446
| [] -> rev_append acc append
445-
| a0::rest -> flat_map_aux f (rev_append (f a0) acc ) append rest
447+
| a0::rest ->
448+
let new_acc =
449+
match f a0 with
450+
| [] -> acc
451+
| [a0] -> a0::acc
452+
| [a0;a1] -> a1::a0::acc
453+
| a0::a1::a2::rest ->
454+
rev_append rest (a2::a1::a0::acc)
455+
in
456+
flat_map_aux f new_acc append rest
446457

447458
let flat_map lx f =
448459
flat_map_aux f [] [] lx

jscomp/syntax/bs_builtin_ppx.ml

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

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

275279
let structure_item_mapper (self : mapper) (str : Parsetree.structure_item) =
276280
match str.pstr_desc with
277-
| Pstr_extension ( ({txt = ("bs.raw"| "raw") ; loc}, payload), _attrs)
278-
->
279-
Ast_exp_handle_external.handle_raw_structure loc payload
280-
| Pstr_extension (({txt = ("bs.debugger.chrome" | "debugger.chrome") ;loc}, payload),_)
281-
->
282-
Location.prerr_warning loc (Preprocessor "this extension can be safely removed");
283-
Ast_structure.dummy_item loc
284281
| Pstr_type (
285-
_rf,
282+
_rf, (* FIXME *)
286283
(_ :: _ as tdcls )) (* [ {ptype_attributes} as tdcl ] *)->
287284
Ast_tdcls.handleTdclsInStru self str tdcls
288285
| Pstr_primitive prim when Ast_attributes.external_needs_to_be_encoded prim.pval_attributes
@@ -341,15 +338,60 @@ let structure_item_mapper (self : mapper) (str : Parsetree.structure_item) =
341338
| _ -> default_mapper.structure_item self str
342339

343340

344-
341+
let rec
342+
structure_mapper (self : mapper) stru =
343+
match stru with
344+
| [] -> []
345+
| item::rest ->
346+
let new_x = self.structure_item self item in
347+
match new_x.pstr_desc with
348+
| Pstr_extension (({txt = ("bs.debugger.chrome" | "debugger.chrome") ;loc}, _),_)
349+
->
350+
Location.prerr_warning loc (Preprocessor "this extension can be safely removed");
351+
(structure_mapper self rest)
352+
| Pstr_extension ( ({txt = ("bs.raw"| "raw") ; loc}, payload), _attrs)
353+
->
354+
Ast_exp_handle_external.handle_raw_structure loc payload :: (structure_mapper self rest)
355+
| Pstr_extension (({txt = "local"; loc}, payload),_)
356+
->
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
383+
| _ ->
384+
new_x :: (structure_mapper self rest)
385+
345386
let unsafe_mapper : mapper =
346387
{ default_mapper with
347388
expr = expr_mapper;
348389
typ = typ_mapper ;
349390
class_type = class_type_mapper;
350391
signature_item = signature_item_mapper ;
351392
value_bindings = Ast_tuple_pattern_flatten.value_bindings_mapper;
352-
structure_item = structure_item_mapper
393+
structure_item = structure_item_mapper;
394+
structure = structure_mapper
353395
}
354396

355397

jscomp/syntax/typemod_hide.ml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
(* Copyright (C) 2020 Authors of BuckleScript
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
25+
26+
let should_hide ( x : Typedtree.module_binding) =
27+
match x.mb_attributes with
28+
| [] -> false
29+
| ({txt = "internal.local";_},_) :: rest -> true
30+
| _ :: rest ->
31+
Ext_list.exists rest (fun (x,_) -> x.txt = "internal.local")
32+
33+
let attrs : Parsetree.attributes =
34+
[{txt = "internal.local";loc = Location.none}, PStr []]
35+
36+
let check (x : Parsetree.structure_item) =
37+
match x.pstr_desc with
38+
| Pstr_eval _
39+
| Pstr_value _
40+
| Pstr_primitive _
41+
| Pstr_typext _
42+
| Pstr_exception _
43+
-> ()
44+
| _ ->
45+
Location.raise_errorf ~loc:x.pstr_loc
46+
"the structure is not supported in local extension"

jscomp/test/build.ninja

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ build test/int_hashtbl_test.cmi test/int_hashtbl_test.cmj : cc test/int_hashtbl_
364364
build test/int_map.cmi test/int_map.cmj : cc test/int_map.ml | $stdlib
365365
build test/int_overflow_test.cmi test/int_overflow_test.cmj : cc test/int_overflow_test.ml | test/mt.cmj $stdlib
366366
build test/int_switch_test.cmi test/int_switch_test.cmj : cc test/int_switch_test.ml | test/mt.cmj $stdlib
367+
build test/internal_unused_test.cmi test/internal_unused_test.cmj : cc test/internal_unused_test.ml | $stdlib
367368
build test/io_test.cmi test/io_test.cmj : cc test/io_test.ml | $stdlib
368369
build test/js_array_test.cmi test/js_array_test.cmj : cc test/js_array_test.ml | test/mt.cmj $stdlib
369370
build test/js_bool_test.cmi test/js_bool_test.cmj : cc test/js_bool_test.ml | test/mt.cmj $stdlib

jscomp/test/internal_unused_test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
var Caml_exceptions = require("../../lib/js/caml_exceptions.js");
4+
5+
console.log(3);
6+
7+
var A = Caml_exceptions.create("Internal_unused_test.P1.A");
8+
9+
function f(param) {
10+
throw A;
11+
}
12+
13+
var c = 5;
14+
15+
function h(a, b) {
16+
return a + b | 0;
17+
}
18+
19+
console.log(c);
20+
21+
console.log(h(1, 2));
22+
23+
exports.f = f;
24+
/* Not a pure module */

jscomp/test/internal_unused_test.ml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
[@@@warning "A"]
2+
[@@@warnerror "a"]
3+
;; module [@internal.local] P0 = struct
4+
let a = 3 in
5+
Js.log a ; a + 2
6+
end
7+
open! P0
8+
9+
;; module [@internal.local] P1 = struct
10+
exception A
11+
let _a = 2
12+
end
13+
open! P1
14+
15+
let f () = raise A
16+
17+
let%local b = 3
18+
19+
let%local c = b + 2
20+
21+
[%%local
22+
let d = c
23+
let f = d
24+
let h = fun[@bs] a b -> a + b
25+
]
26+
27+
28+
29+
;; Js.log f
30+
31+
;; Js.log (h 1 2 [@bs])
32+
33+
(* [%%debugger.chrome] *)

lib/4.06.1/bsb.ml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,8 +1286,10 @@ let init n f =
12861286

12871287
let rec rev_append l1 l2 =
12881288
match l1 with
1289-
[] -> l2
1290-
| a :: l -> rev_append l (a :: l2)
1289+
| [] -> l2
1290+
| [a0] -> a0::l2 (* single element is common *)
1291+
| [a0 ; a1] -> a1 :: a0 :: l2
1292+
| a0::a1::a2::rest -> rev_append rest (a2::a1::a0::l2)
12911293

12921294
let rev l = rev_append l []
12931295

@@ -1364,7 +1366,16 @@ let rec rev_map_append l1 l2 f =
13641366
let rec flat_map_aux f acc append lx =
13651367
match lx with
13661368
| [] -> rev_append acc append
1367-
| a0::rest -> flat_map_aux f (rev_append (f a0) acc ) append rest
1369+
| a0::rest ->
1370+
let new_acc =
1371+
match f a0 with
1372+
| [] -> acc
1373+
| [a0] -> a0::acc
1374+
| [a0;a1] -> a1::a0::acc
1375+
| a0::a1::a2::rest ->
1376+
rev_append rest (a2::a1::a0::acc)
1377+
in
1378+
flat_map_aux f new_acc append rest
13681379

13691380
let flat_map lx f =
13701381
flat_map_aux f [] [] lx

lib/4.06.1/bsb_helper.ml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,8 +1278,10 @@ let init n f =
12781278

12791279
let rec rev_append l1 l2 =
12801280
match l1 with
1281-
[] -> l2
1282-
| a :: l -> rev_append l (a :: l2)
1281+
| [] -> l2
1282+
| [a0] -> a0::l2 (* single element is common *)
1283+
| [a0 ; a1] -> a1 :: a0 :: l2
1284+
| a0::a1::a2::rest -> rev_append rest (a2::a1::a0::l2)
12831285

12841286
let rev l = rev_append l []
12851287

@@ -1356,7 +1358,16 @@ let rec rev_map_append l1 l2 f =
13561358
let rec flat_map_aux f acc append lx =
13571359
match lx with
13581360
| [] -> rev_append acc append
1359-
| a0::rest -> flat_map_aux f (rev_append (f a0) acc ) append rest
1361+
| a0::rest ->
1362+
let new_acc =
1363+
match f a0 with
1364+
| [] -> acc
1365+
| [a0] -> a0::acc
1366+
| [a0;a1] -> a1::a0::acc
1367+
| a0::a1::a2::rest ->
1368+
rev_append rest (a2::a1::a0::acc)
1369+
in
1370+
flat_map_aux f new_acc append rest
13601371

13611372
let flat_map lx f =
13621373
flat_map_aux f [] [] lx

lib/4.06.1/unstable/all_ounit_tests.ml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5963,8 +5963,10 @@ let init n f =
59635963

59645964
let rec rev_append l1 l2 =
59655965
match l1 with
5966-
[] -> l2
5967-
| a :: l -> rev_append l (a :: l2)
5966+
| [] -> l2
5967+
| [a0] -> a0::l2 (* single element is common *)
5968+
| [a0 ; a1] -> a1 :: a0 :: l2
5969+
| a0::a1::a2::rest -> rev_append rest (a2::a1::a0::l2)
59685970

59695971
let rev l = rev_append l []
59705972

@@ -6041,7 +6043,16 @@ let rec rev_map_append l1 l2 f =
60416043
let rec flat_map_aux f acc append lx =
60426044
match lx with
60436045
| [] -> rev_append acc append
6044-
| a0::rest -> flat_map_aux f (rev_append (f a0) acc ) append rest
6046+
| a0::rest ->
6047+
let new_acc =
6048+
match f a0 with
6049+
| [] -> acc
6050+
| [a0] -> a0::acc
6051+
| [a0;a1] -> a1::a0::acc
6052+
| a0::a1::a2::rest ->
6053+
rev_append rest (a2::a1::a0::acc)
6054+
in
6055+
flat_map_aux f new_acc append rest
60456056

60466057
let flat_map lx f =
60476058
flat_map_aux f [] [] lx

0 commit comments

Comments
 (0)