Skip to content

Commit fe983ed

Browse files
committed
Add bs.as support
- we marked bs.as used in label description
1 parent 2411dc8 commit fe983ed

22 files changed

+121282
-120632
lines changed

jscomp/core/bs_conditional_initial.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ let setup_env () =
3737
Clflags.binary_annotations := true;
3838
(* Turn on [-no-alias-deps] by default -- double check *)
3939
Oprint.out_ident := Outcome_printer_ns.out_ident;
40-
40+
Builtin_attributes.check_bs_attributes_inclusion := Record_attributes_check.check_bs_attributes_inclusion;
41+
Lambda.fld_record := Record_attributes_check.fld_record;
42+
Lambda.fld_record_set := Record_attributes_check.fld_record_set;
43+
Lambda.blk_record := Record_attributes_check.blk_record;
4144
#if undefined BS_RELEASE_BUILD then
4245
Printexc.record_backtrace true;
4346
(match Ext_sys.getenv_opt "BS_DEBUG_FILE" with

jscomp/core/js_of_lam_block.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ let field (field_info : Lam_compat.field_dbg_info) e i =
6060
->
6161
E.array_index_by_int ~comment e i
6262
#end
63-
| Fld_record name
63+
| Fld_record {name}
6464
-> E.record_access e name i
6565
| Fld_module name
6666
-> E.module_access e name i

jscomp/core/lam_compat.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ type meth_kind = Lambda.meth_kind
184184

185185
type field_dbg_info = Lambda.field_dbg_info =
186186
| Fld_na
187-
| Fld_record of string
187+
| Fld_record of {name : string; mutable_flag : Asttypes.mutable_flag}
188188
| Fld_module of string
189189
#if OCAML_VERSION =~ ">4.03.0" then
190190
| Fld_record_inline of string
@@ -195,7 +195,7 @@ let str_of_field_info (x : field_dbg_info) : string option =
195195
match x with
196196
| Fld_na
197197
| Fld_tuple -> None
198-
| Fld_record s
198+
| Fld_record {name = s}
199199
| Fld_module s
200200
| Fld_record_inline s
201201
| Fld_record_extension s

jscomp/core/lam_compat.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ type meth_kind = Lambda.meth_kind
8585

8686
type field_dbg_info = Lambda.field_dbg_info =
8787
| Fld_na
88-
| Fld_record of string
88+
| Fld_record of {name : string; mutable_flag : Asttypes.mutable_flag}
8989
| Fld_module of string
9090
#if OCAML_VERSION =~ ">4.03.0" then
9191
| Fld_record_inline of string
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
(* Copyright (C) 2019- 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+
type label = Types.label_description
26+
27+
let fn = (fun (attr : Parsetree.attribute) ->
28+
match attr with
29+
| {txt = "bs.as"}, PStr
30+
[{pstr_desc = Pstr_eval ({pexp_desc = Pexp_constant (Pconst_string(s,_))},_ )}] ->
31+
(* Bs_ast_invariant.mark_used_bs_attribute attr; *)
32+
Some s
33+
| _ -> None
34+
)
35+
36+
let fld_record (lbl : label) =
37+
Lambda.Fld_record
38+
{name = Ext_list.find_def lbl.lbl_attributes fn lbl.lbl_name; mutable_flag = lbl.Types.lbl_mut}
39+
40+
let fld_record_set (lbl : label) =
41+
Lambda.Fld_record_set
42+
(Ext_list.find_def lbl.lbl_attributes fn lbl.lbl_name)
43+
44+
let blk_record fields =
45+
let all_labels_info =
46+
Ext_array.map fields
47+
(fun ((lbl : label),_) ->
48+
Ext_list.find_def lbl.Types.lbl_attributes fn lbl.lbl_name) in
49+
Lambda.Blk_record all_labels_info
50+
51+
let check_bs_attributes_inclusion
52+
(attrs1 : Parsetree.attributes)
53+
(attrs2 : Parsetree.attributes)
54+
lbl_name =
55+
let a = Ext_list.find_def attrs1 fn lbl_name in
56+
let b = Ext_list.find_def attrs2 fn lbl_name in
57+
if a = b then None
58+
else Some (a,b)
59+
60+
61+

jscomp/syntax/bs_ast_invariant.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ let emit_external_warnings : iterator=
117117
#end
118118
| _ -> default_iterator.expr self a
119119
);
120+
label_declaration = (fun self lbl ->
121+
122+
Ext_list.iter lbl.pld_attributes
123+
(fun attr ->
124+
match attr with
125+
| {txt = "bs.as"}, _ -> mark_used_bs_attribute attr
126+
| _ -> ()
127+
);
128+
default_iterator.label_declaration self lbl
129+
);
120130
value_description =
121131
(fun self v ->
122132
match v with

jscomp/test/build.ninja

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ build test/rec_module_test.cmi test/rec_module_test.cmj : cc test/rec_module_tes
496496
build test/rec_value_test.cmi test/rec_value_test.cmj : cc test/rec_value_test.ml | test/mt.cmj $stdlib
497497
build test/record_debug_test.cmi test/record_debug_test.cmj : cc test/record_debug_test.ml | $stdlib
498498
build test/record_extension_test.cmi test/record_extension_test.cmj : cc test/record_extension_test.ml | test/mt.cmj $stdlib
499+
build test/record_name_test.cmi test/record_name_test.cmj : cc test/record_name_test.ml | $stdlib
499500
build test/record_with_test.cmi test/record_with_test.cmj : cc test/record_with_test.ml | test/mt.cmj $stdlib
500501
build test/recursive_module.cmi test/recursive_module.cmj : cc test/recursive_module.ml | test/mt.cmj $stdlib
501502
build test/recursive_module_test.cmi test/recursive_module_test.cmj : cc test/recursive_module_test.ml | test/mt.cmj $stdlib

jscomp/test/record_name_test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
4+
function f(x) {
5+
return {
6+
THIS_IS_NOT_EXPRESSIBLE_IN_BUCKLE: x
7+
};
8+
}
9+
10+
function set(x) {
11+
x.THIS_IS_NOT_EXPRESSIBLE_IN_BUCKLE = 3;
12+
return (x.THIS_IS_NOT_EXPRESSIBLE_IN_BUCKLE << 1);
13+
}
14+
15+
function f2(u) {
16+
return u.x.x.x.y;
17+
}
18+
19+
exports.f = f;
20+
exports.set = set;
21+
exports.f2 = f2;
22+
/* No side effect */

jscomp/test/record_name_test.ml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
(*
3+
To work around unused attribute checking
4+
5+
- we mark it used in ppx stage
6+
we can not mark it in parsing since it won't
7+
works for reason
8+
*)
9+
type t = {
10+
mutable x : int [@bs.as "THIS_IS_NOT_EXPRESSIBLE_IN_BUCKLE"]
11+
(* test key word later *)
12+
}
13+
14+
15+
16+
let f x = { x}
17+
18+
let set x = x.x <- 3 ; x.x * 2
19+
20+
type x = t = private {
21+
mutable x : int [@bs.as "THIS_IS_NOT_EXPRESSIBLE_IN_BUCKLE"]
22+
}
23+
24+
type t0 = { x: t0 ; y : int}
25+
26+
let f2 u =
27+
match u with
28+
| {x = { x = {x={y}}}} -> y
29+
30+

lib/4.06.1/bsb.ml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,13 @@ val find_opt :
796796
('a -> 'b option) ->
797797
'b option
798798

799+
val find_def :
800+
'a list ->
801+
('a -> 'b option) ->
802+
'b ->
803+
'b
799804

805+
800806
val rev_iter :
801807
'a list ->
802808
('a -> unit) ->
@@ -1496,7 +1502,13 @@ let rec find_opt xs p =
14961502
| Some _ as v -> v
14971503
| None -> find_opt l p
14981504

1499-
1505+
let rec find_def xs p def =
1506+
match xs with
1507+
| [] -> def
1508+
| x::l ->
1509+
match p x with
1510+
| Some v -> v
1511+
| None -> find_def l p def
15001512

15011513
let rec split_map l f =
15021514
match l with

0 commit comments

Comments
 (0)