Skip to content

Commit 128cb7e

Browse files
committed
1 parent bdb2cd2 commit 128cb7e

File tree

10 files changed

+319
-170
lines changed

10 files changed

+319
-170
lines changed

jscomp/all.depend

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,10 @@ syntax/external_process.cmx : common/lam_methname.cmx \
229229
syntax/ast_attributes.cmx syntax/external_process.cmi
230230
syntax/ast_derive_abstract.cmx : syntax/external_process.cmx \
231231
syntax/external_ffi_types.cmx syntax/external_arg_spec.cmx \
232-
ext/ext_list.cmx syntax/ast_literal.cmx syntax/ast_derive_util.cmx \
233-
syntax/ast_core_type.cmx syntax/ast_compatible.cmx \
234-
syntax/ast_attributes.cmx syntax/ast_derive_abstract.cmi
232+
ext/ext_list.cmx syntax/ast_payload.cmx syntax/ast_literal.cmx \
233+
syntax/ast_derive_util.cmx syntax/ast_core_type.cmx \
234+
syntax/ast_compatible.cmx syntax/ast_attributes.cmx \
235+
syntax/ast_derive_abstract.cmi
235236
syntax/ast_derive_dyn.cmx : ext/ext_list.cmx syntax/bs_syntaxerr.cmx \
236237
syntax/ast_structure.cmx syntax/ast_derive_util.cmx syntax/ast_derive.cmx \
237238
syntax/ast_compatible.cmx syntax/ast_attributes.cmx \
@@ -251,7 +252,7 @@ syntax/ast_util.cmx : ext/literals.cmx syntax/external_process.cmx \
251252
syntax/ast_core_type.cmx syntax/ast_compatible.cmx syntax/ast_comb.cmx \
252253
syntax/ast_attributes.cmx syntax/ast_util.cmi
253254
syntax/ast_tdcls.cmx : ext/ext_list.cmx syntax/bs_ast_mapper.cmx \
254-
syntax/ast_structure.cmx syntax/ast_signature.cmx syntax/ast_payload.cmx \
255+
syntax/ast_structure.cmx syntax/ast_signature.cmx \
255256
syntax/ast_derive_abstract.cmx syntax/ast_derive.cmx \
256257
syntax/ast_compatible.cmx syntax/ast_attributes.cmx syntax/ast_tdcls.cmi
257258
syntax/ast_primitive.cmx : syntax/external_process.cmx \
@@ -309,7 +310,7 @@ syntax/ast_polyvar.cmi : syntax/external_arg_spec.cmi
309310
syntax/external_ffi_types.cmi : syntax/external_arg_spec.cmi
310311
syntax/external_process.cmi : common/bs_loc.cmi syntax/ast_core_type.cmi \
311312
syntax/ast_attributes.cmi
312-
syntax/ast_derive_abstract.cmi :
313+
syntax/ast_derive_abstract.cmi : syntax/ast_payload.cmi
313314
syntax/ast_derive_dyn.cmi :
314315
syntax/ast_derive_projector.cmi :
315316
syntax/ast_derive_js_mapper.cmi :

jscomp/syntax/ast_derive_abstract.ml

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,20 @@ module U = Ast_derive_util
2828
open Ast_helper
2929
type tdcls = Parsetree.type_declaration list
3030

31+
type abstractKind =
32+
| Not_abstract
33+
| Light_abstract
34+
| Complex_abstract
35+
3136
let isAbstract (xs :Ast_payload.action list) =
3237
match xs with
33-
| [{loc; txt = "abstract"}, None] ->
34-
true
38+
| [{loc; txt = "abstract"},
39+
(None
40+
)] ->
41+
Complex_abstract
42+
| [{loc; txt = "abstract"},
43+
Some {pexp_desc = Pexp_ident {txt = Lident "light"}}
44+
] -> Light_abstract
3545
| [{loc; txt = "abstract"}, Some _ ]
3646
->
3747
Location.raise_errorf ~loc "invalid config for abstract"
@@ -43,7 +53,7 @@ let isAbstract (xs :Ast_payload.action list) =
4353
"bs.deriving abstract does not work with any other deriving"
4454
| _ -> ()
4555
) ;
46-
false
56+
Not_abstract
4757
(* let handle_config (config : Parsetree.expression option) =
4858
match config with
4959
| Some config ->
@@ -69,7 +79,11 @@ let deprecated name =
6979
("use " ^ name ^ "Get instead")
7080

7181

72-
let handleTdcl (tdcl : Parsetree.type_declaration) =
82+
let handleTdcl
83+
light
84+
(tdcl : Parsetree.type_declaration)
85+
: Parsetree.type_declaration * Parsetree.value_description list
86+
=
7387
let core_type = U.core_type_of_type_declaration tdcl in
7488
let loc = tdcl.ptype_loc in
7589
let type_name = tdcl.ptype_name.txt in
@@ -123,24 +137,27 @@ let handleTdcl (tdcl : Parsetree.type_declaration) =
123137
#end
124138

125139
maker,
126-
let aux b pld_name =
140+
let aux light deprec pld_name : Parsetree.value_description =
127141
(Val.mk ~loc:pld_loc
128-
(if b then pld_name else
142+
(if light then pld_name else
129143
{pld_name with txt = pld_name.txt ^ "Get"})
130-
~attrs:(if b then deprecated (pld_name.Asttypes.txt) :: get_optional_attrs
144+
~attrs:(if deprec then deprecated (pld_name.Asttypes.txt) :: get_optional_attrs
131145
else get_optional_attrs) ~prim
132146
(Ast_compatible.arrow ~loc core_type optional_type)
133147
) in
134-
aux true pld_name :: aux false pld_name :: acc )
148+
if not light then
149+
aux true true pld_name :: aux false false pld_name :: acc
150+
else aux true false pld_name :: acc
151+
)
135152
else
136153
Ast_compatible.label_arrow ~loc:pld_loc label_name pld_type maker,
137154
(
138-
let aux b pld_name =
139-
Val.mk ~loc:pld_loc
140-
(if b then pld_name else
141-
{pld_name with txt = pld_name.txt ^ "Get"}
142-
) ~attrs:(if b then deprecated pld_name.Asttypes.txt :: get_attrs else get_attrs)
143-
~prim:(
155+
let aux light deprec pld_name =
156+
Val.mk ~loc:pld_loc
157+
(if light then pld_name else
158+
{pld_name with txt = pld_name.txt ^ "Get"}
159+
) ~attrs:(if deprec then deprecated pld_name.Asttypes.txt :: get_attrs else get_attrs)
160+
~prim:(
144161
["" ; (* Not needed actually*)
145162
External_ffi_types.to_string
146163
(Ffi_bs (
@@ -150,7 +167,10 @@ let handleTdcl (tdcl : Parsetree.type_declaration) =
150167
))] )
151168
(Ast_compatible.arrow ~loc core_type pld_type)
152169
in
153-
aux true pld_name ::aux false pld_name :: acc )
170+
if not light then
171+
aux true true pld_name ::aux false false pld_name :: acc
172+
else aux true false pld_name :: acc
173+
)
154174
in
155175
let is_current_field_mutable = pld_mutable = Mutable in
156176
let acc =
@@ -194,21 +214,21 @@ let handleTdcl (tdcl : Parsetree.type_declaration) =
194214
(* U.notApplicable tdcl.ptype_loc derivingName; *)
195215
tdcl, []
196216

197-
let handleTdclsInStr tdcls =
217+
let handleTdclsInStr ~light tdcls =
198218
let tdcls, code =
199219
Ext_list.fold_right tdcls ([],[]) (fun tdcl (tdcls, sts) ->
200-
match handleTdcl tdcl with
220+
match handleTdcl light tdcl with
201221
ntdcl, value_descriptions ->
202222
ntdcl::tdcls,
203223
Ext_list.map_append value_descriptions sts (fun x -> Str.primitive x)
204224
) in
205225
Ast_compatible.rec_type_str tdcls :: code
206226
(* still need perform transformation for non-abstract type*)
207227

208-
let handleTdclsInSig tdcls =
228+
let handleTdclsInSig ~light tdcls =
209229
let tdcls, code =
210230
Ext_list.fold_right tdcls ([],[]) (fun tdcl (tdcls, sts) ->
211-
match handleTdcl tdcl with
231+
match handleTdcl light tdcl with
212232
ntdcl, value_descriptions ->
213233
ntdcl::tdcls,
214234
Ext_list.map_append value_descriptions sts (fun x -> Sig.value x)

jscomp/syntax/ast_derive_abstract.mli

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,23 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25-
25+
type abstractKind =
26+
| Not_abstract
27+
| Light_abstract
28+
| Complex_abstract
29+
2630
val isAbstract :
27-
Ast_payload.action list -> bool
31+
Ast_payload.action list -> abstractKind
2832
(** if only [abstract] happens [true]
2933
if [abstract] does not appear [false]
3034
if [abstract] happens with other, raise exception
3135
*)
3236

3337

3438
val handleTdclsInStr :
39+
light:bool ->
3540
Parsetree.type_declaration list -> Parsetree.structure
3641

3742
val handleTdclsInSig:
43+
light:bool ->
3844
Parsetree.type_declaration list -> Parsetree.signature

jscomp/syntax/ast_tdcls.ml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ let handleTdclsInSigi
5757
let loc = sigi.psig_loc in
5858
let originalTdclsNewAttrs = newTdcls tdcls newAttrs in (* remove the processed attr*)
5959
let newTdclsNewAttrs = self.type_declaration_list self originalTdclsNewAttrs in
60-
if Ast_derive_abstract.isAbstract actions then
61-
let codes = Ast_derive_abstract.handleTdclsInSig originalTdclsNewAttrs in
60+
let kind = Ast_derive_abstract.isAbstract actions in
61+
if kind <> Not_abstract then
62+
let codes = Ast_derive_abstract.handleTdclsInSig ~light:(kind = Light_abstract) originalTdclsNewAttrs in
6263
Ast_signature.fuseAll ~loc
6364
(
6465
Sig.include_ ~loc
@@ -102,8 +103,10 @@ let handleTdclsInStru
102103
let newStr : Parsetree.structure_item =
103104
Ast_compatible.rec_type_str ~loc (self.type_declaration_list self originalTdclsNewAttrs)
104105
in
105-
if Ast_derive_abstract.isAbstract actions then
106-
let codes = Ast_derive_abstract.handleTdclsInStr originalTdclsNewAttrs in
106+
let kind = Ast_derive_abstract.isAbstract actions in
107+
if kind <> Not_abstract then
108+
let codes =
109+
Ast_derive_abstract.handleTdclsInStr ~light:(kind = Light_abstract) originalTdclsNewAttrs in
107110
(* use [tdcls2] avoid nonterminating *)
108111
Ast_structure.fuseAll ~loc
109112
(

jscomp/test/bs_abstract_test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,16 @@ function uff3(f) {
4545
}
4646
}
4747

48+
function fx(v) {
49+
return v.x;
50+
}
51+
4852
exports.f = f;
4953
exports.uf = uf;
5054
exports.uf1 = uf1;
5155
exports.uf2 = uf2;
5256
exports.uff = uff;
5357
exports.uff2 = uff2;
5458
exports.uff3 = uff3;
59+
exports.fx = fx;
5560
/* Not a pure module */

jscomp/test/bs_abstract_test.ml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,16 @@ let uff2 f =
5252
let uff3 f =
5353
match f |. yyyy2Get with
5454
| None -> 0
55-
| Some x -> x 0
55+
| Some x -> x 0
56+
57+
58+
59+
type u3 = {
60+
x : int;
61+
yyyy : (int -> int [@bs]);
62+
yyyy1 : (int -> int -> int [@bs]);
63+
yyyy2 : int -> int [@bs.optional]
64+
} [@@bs.deriving { abstract = light} ]
65+
66+
67+
let fx v = v |. x

jscomp/test/bs_abstract_test.mli

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,7 @@ type u1
2424
val uff : u1 -> int
2525
val uff2 : u1 -> int
2626

27-
val uff3 : u1 -> int
27+
val uff3 : u1 -> int
28+
29+
type u3
30+
val fx : u3 -> int

0 commit comments

Comments
 (0)