Skip to content

Commit 50cc24f

Browse files
committed
lift the function restriction
1 parent f1e7900 commit 50cc24f

12 files changed

+469
-305
lines changed

jscomp/all.depend

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ syntax/external_process.cmx : common/lam_methname.cmx \
221221
syntax/ast_core_type.cmx syntax/ast_comb.cmx syntax/ast_attributes.cmx \
222222
syntax/external_process.cmi
223223
syntax/ast_derive_abstract.cmx : syntax/external_process.cmx \
224+
syntax/external_ffi_types.cmx syntax/external_arg_spec.cmx \
224225
ext/ext_list.cmx syntax/ast_literal.cmx syntax/ast_derive_util.cmx \
225226
syntax/ast_core_type.cmx syntax/ast_attributes.cmx \
226227
syntax/ast_derive_abstract.cmi

jscomp/bsb/bsb_templates.ml

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -549,15 +549,12 @@ let root = OCamlRes.Res.([
549549
\ \"You've clicked this \" ++ string_of_int(self.state.count) ++ \" times(s)\";\n\
550550
\ <div>\n\
551551
\ <button onClick=(_event => self.send(Click))>\n\
552-
\ (ReasonReact.stringToElement(message))\n\
552+
\ (ReasonReact.string(message))\n\
553553
\ </button>\n\
554554
\ <button onClick=(_event => self.send(Toggle))>\n\
555-
\ (ReasonReact.stringToElement(\"Toggle greeting\"))\n\
555+
\ (ReasonReact.string(\"Toggle greeting\"))\n\
556556
\ </button>\n\
557-
\ (\n\
558-
\ self.state.show ?\n\
559-
\ ReasonReact.stringToElement(greeting) : ReasonReact.nullElement\n\
560-
\ )\n\
557+
\ (self.state.show ? ReasonReact.string(greeting) : ReasonReact.null)\n\
561558
\ </div>;\n\
562559
\ },\n\
563560
};\n\
@@ -583,7 +580,7 @@ let root = OCamlRes.Res.([
583580
\ ...component,\n\
584581
\ render: self =>\n\
585582
\ <div onClick=(self.handle(handleClick))>\n\
586-
\ (ReasonReact.stringToElement(message))\n\
583+
\ (ReasonReact.string(message))\n\
587584
\ </div>,\n\
588585
};\n\
589586
")]) ;
@@ -598,6 +595,7 @@ let root = OCamlRes.Res.([
598595
# in another tab\n\
599596
npm run webpack\n\
600597
```\n\
598+
\n\
601599
After you see the webpack compilation succeed (the `npm run webpack` step), open up `src/index.html` (**no server needed!**). Then modify whichever `.re` file in `src` and refresh the page to see the changes.\n\
602600
\n\
603601
**For more elaborate ReasonReact examples**, please see https://github.com/reasonml-community/reason-react-example\n\
@@ -633,7 +631,7 @@ let root = OCamlRes.Res.([
633631
\ \"dependencies\": {\n\
634632
\ \"react\": \"^16.2.0\",\n\
635633
\ \"react-dom\": \"^16.2.0\",\n\
636-
\ \"reason-react\": \">=0.3.4\"\n\
634+
\ \"reason-react\": \">=0.4.0\"\n\
637635
\ },\n\
638636
\ \"devDependencies\": {\n\
639637
\ \"bs-platform\": \"^${bsb:bs-version}\",\n\
@@ -665,10 +663,7 @@ let root = OCamlRes.Res.([
665663
\ \"bs-dependencies\": [\n\
666664
\ \"reason-react\"\n\
667665
\ ],\n\
668-
\ \"refmt\": 3,\n\
669-
\ \"warnings\": {\n\
670-
\ \"error\": \"+5\"\n\
671-
\ }\n\
666+
\ \"refmt\": 3\n\
672667
}\n\
673668
") ;
674669
File (".gitignore",

jscomp/syntax/ast_attributes.ml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,20 @@ let bs_obj : attr
358358
let bs_get : attr
359359
= {txt = "bs.get"; loc = locg}, Ast_payload.empty
360360

361+
let bs_get_arity : attr
362+
= {txt = "internal.arity"; loc = locg},
363+
PStr
364+
[{pstr_desc =
365+
Pstr_eval (
366+
{pexp_desc =
367+
Pexp_constant
368+
(Const_int 1);
369+
pexp_loc = locg;
370+
pexp_attributes = []
371+
},[])
372+
; pstr_loc = locg}]
373+
374+
361375
let bs_set : attr
362376
= {txt = "bs.set"; loc = locg}, Ast_payload.empty
363377

jscomp/syntax/ast_attributes.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,6 @@ val bs_obj : attr
8585

8686

8787
val bs_get : attr
88+
val bs_get_arity : attr
8889
val bs_set : attr
8990
val bs_return_undefined : attr

jscomp/syntax/ast_derive_abstract.ml

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -34,31 +34,15 @@ let handle_config (config : Parsetree.expression option) =
3434
U.invalid_config config
3535
| None -> ()
3636

37-
(* see #2337
38-
TODO: relax it to allow (int -> int [@bs])
39-
*)
40-
let rec checkNotFunciton (ty : Parsetree.core_type) =
41-
match ty.ptyp_desc with
42-
| Ptyp_poly (_,ty) -> checkNotFunciton ty
43-
| Ptyp_alias (ty,_) -> checkNotFunciton ty
44-
| Ptyp_arrow _ ->
45-
Location.raise_errorf
46-
~loc:ty.ptyp_loc
47-
"syntactic function type is not allowed when working with abstract bs.deriving, create a named type as work around"
48-
| Ptyp_any
49-
| Ptyp_var _
50-
| Ptyp_tuple _
51-
| Ptyp_constr _
52-
| Ptyp_object _
53-
| Ptyp_class _
54-
| Ptyp_variant _
55-
| Ptyp_package _
56-
| Ptyp_extension _ -> ()
5737

5838

5939
let get_optional_attrs =
6040
[Ast_attributes.bs_get; Ast_attributes.bs_return_undefined]
61-
let get_attrs = [ Ast_attributes.bs_get ]
41+
(** For this attributes, its type was wrapped as an option,
42+
so we can still reuse existing frame work
43+
*)
44+
45+
let get_attrs = [ Ast_attributes.bs_get_arity]
6246
let set_attrs = [Ast_attributes.bs_set]
6347
let handleTdcl (tdcl : Parsetree.type_declaration) =
6448
let core_type = U.core_type_of_type_declaration tdcl in
@@ -88,31 +72,38 @@ let handleTdcl (tdcl : Parsetree.type_declaration) =
8872
pld_loc
8973
}:
9074
Parsetree.label_declaration) (acc, maker, labels) ->
91-
let () = checkNotFunciton pld_type in
92-
(* TODO: explain why *)
93-
let prim, newLabel =
75+
let prim_as_name, newLabel =
9476
match Ast_attributes.iter_process_bs_string_as pld_attributes with
9577
| None ->
96-
[label_name], pld_name
78+
label_name, pld_name
9779
| Some new_name ->
98-
[new_name], {pld_name with txt = new_name}
80+
new_name, {pld_name with txt = new_name}
9981
in
100-
let is_option = Ast_attributes.has_bs_optional pld_attributes in
101-
let maker, getter_type =
102-
if is_option then
82+
let prim = [prim_as_name] in
83+
let is_optional = Ast_attributes.has_bs_optional pld_attributes in
84+
let maker, getter_declaration =
85+
if is_optional then
10386
let optional_type = Ast_core_type.lift_option_type pld_type in
104-
Ast_core_type.opt_arrow pld_loc label_name optional_type maker,
105-
Typ.arrow ~loc "" core_type optional_type
87+
(Ast_core_type.opt_arrow pld_loc label_name optional_type maker,
88+
Val.mk pld_name
89+
~attrs:get_optional_attrs ~prim
90+
(Typ.arrow ~loc "" core_type optional_type)
91+
)
10692
else
10793
Typ.arrow ~loc:pld_loc label_name pld_type maker,
108-
Typ.arrow ~loc "" core_type pld_type
94+
Val.mk pld_name ~attrs:get_attrs
95+
~prim:(
96+
["" ; (* Not needed actually*)
97+
External_ffi_types.to_string
98+
(Ffi_bs (
99+
[{arg_type = Nothing; arg_label = External_arg_spec.empty_label}],
100+
Return_identity,
101+
Js_get {js_get_name = prim_as_name; js_get_scopes = []}
102+
))] )
103+
(Typ.arrow ~loc "" core_type pld_type)
109104
in
110105
let acc =
111-
Val.mk pld_name
112-
~attrs:(
113-
if is_option then get_optional_attrs
114-
else get_attrs)
115-
~prim getter_type :: acc in
106+
getter_declaration :: acc in
116107
let is_current_field_mutable = pld_mutable = Mutable in
117108
let acc =
118109
if is_current_field_mutable then
@@ -130,7 +121,7 @@ let handleTdcl (tdcl : Parsetree.type_declaration) =
130121
else acc in
131122
acc,
132123
maker,
133-
(is_option, newLabel)::labels
124+
(is_optional, newLabel)::labels
134125
) label_declarations
135126
([],
136127
(if has_optional_field then

jscomp/test/bs_abstract_test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
var Curry = require("../../lib/js/curry.js");
34

45
var v = {
56
hd: 3,
@@ -15,5 +16,40 @@ var f = {
1516
y: "x"
1617
};
1718

19+
function uf(u) {
20+
return Curry._1(u.y0, 1);
21+
}
22+
23+
function uf1(u) {
24+
return Curry._1(u.y1, 1);
25+
}
26+
27+
function uf2(u) {
28+
return Curry._2(u.y1, 1, 2);
29+
}
30+
31+
function uff(f) {
32+
return f.yyyy(1);
33+
}
34+
35+
function uff2(f) {
36+
return f.yyyy1(1, 2);
37+
}
38+
39+
function uff3(f) {
40+
var match = f.yyyy2;
41+
if (match !== undefined) {
42+
return Curry._1(match, 0);
43+
} else {
44+
return 0;
45+
}
46+
}
47+
1848
exports.f = f;
49+
exports.uf = uf;
50+
exports.uf1 = uf1;
51+
exports.uf2 = uf2;
52+
exports.uff = uff;
53+
exports.uff2 = uff2;
54+
exports.uff3 = uff3;
1955
/* Not a pure module */

jscomp/test/bs_abstract_test.ml

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,33 @@ and x = {
2020
} [@@bs.deriving abstract]
2121

2222

23-
let f = x ~k:(fun[@bs] x y -> x = y) ~y:"x"
23+
let f = x ~k:(fun[@bs] x y -> x = y) ~y:"x"
24+
25+
type u = {
26+
x : int ;
27+
y0 : int -> int;
28+
y1 : int -> int -> int
29+
} [@@bs.deriving abstract]
30+
31+
32+
let uf u = u |. y0 1
33+
let uf1 u = u |. y1 1
34+
let uf2 u = u |. y1 1 2
35+
36+
type u1 = {
37+
x : int;
38+
yyyy : (int -> int [@bs]);
39+
yyyy1 : (int -> int -> int [@bs]);
40+
yyyy2 : int -> int [@bs.optional]
41+
} [@@bs.deriving abstract]
42+
43+
let uff f =
44+
(f |. yyyy) 1 [@bs]
45+
46+
let uff2 f =
47+
(f |. yyyy1) 1 2 [@bs]
48+
49+
let uff3 f =
50+
match f |. yyyy2 with
51+
| None -> 0
52+
| Some x -> x 0

jscomp/test/bs_abstract_test.mli

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,14 @@ type 'a linked_list = private
1414
} [@@bs.deriving abstract]
1515

1616

17-
val f : x
17+
val f : x
18+
type u
19+
val uf : u -> int
20+
val uf1 : u -> int -> int
21+
val uf2 : u -> int
22+
23+
type u1
24+
val uff : u1 -> int
25+
val uff2 : u1 -> int
26+
27+
val uff3 : u1 -> int

lib/bsb.ml

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13492,15 +13492,12 @@ let root = OCamlRes.Res.([
1349213492
\ \"You've clicked this \" ++ string_of_int(self.state.count) ++ \" times(s)\";\n\
1349313493
\ <div>\n\
1349413494
\ <button onClick=(_event => self.send(Click))>\n\
13495-
\ (ReasonReact.stringToElement(message))\n\
13495+
\ (ReasonReact.string(message))\n\
1349613496
\ </button>\n\
1349713497
\ <button onClick=(_event => self.send(Toggle))>\n\
13498-
\ (ReasonReact.stringToElement(\"Toggle greeting\"))\n\
13498+
\ (ReasonReact.string(\"Toggle greeting\"))\n\
1349913499
\ </button>\n\
13500-
\ (\n\
13501-
\ self.state.show ?\n\
13502-
\ ReasonReact.stringToElement(greeting) : ReasonReact.nullElement\n\
13503-
\ )\n\
13500+
\ (self.state.show ? ReasonReact.string(greeting) : ReasonReact.null)\n\
1350413501
\ </div>;\n\
1350513502
\ },\n\
1350613503
};\n\
@@ -13526,7 +13523,7 @@ let root = OCamlRes.Res.([
1352613523
\ ...component,\n\
1352713524
\ render: self =>\n\
1352813525
\ <div onClick=(self.handle(handleClick))>\n\
13529-
\ (ReasonReact.stringToElement(message))\n\
13526+
\ (ReasonReact.string(message))\n\
1353013527
\ </div>,\n\
1353113528
};\n\
1353213529
")]) ;
@@ -13541,6 +13538,7 @@ let root = OCamlRes.Res.([
1354113538
# in another tab\n\
1354213539
npm run webpack\n\
1354313540
```\n\
13541+
\n\
1354413542
After you see the webpack compilation succeed (the `npm run webpack` step), open up `src/index.html` (**no server needed!**). Then modify whichever `.re` file in `src` and refresh the page to see the changes.\n\
1354513543
\n\
1354613544
**For more elaborate ReasonReact examples**, please see https://github.com/reasonml-community/reason-react-example\n\
@@ -13576,7 +13574,7 @@ let root = OCamlRes.Res.([
1357613574
\ \"dependencies\": {\n\
1357713575
\ \"react\": \"^16.2.0\",\n\
1357813576
\ \"react-dom\": \"^16.2.0\",\n\
13579-
\ \"reason-react\": \">=0.3.4\"\n\
13577+
\ \"reason-react\": \">=0.4.0\"\n\
1358013578
\ },\n\
1358113579
\ \"devDependencies\": {\n\
1358213580
\ \"bs-platform\": \"^${bsb:bs-version}\",\n\
@@ -13608,10 +13606,7 @@ let root = OCamlRes.Res.([
1360813606
\ \"bs-dependencies\": [\n\
1360913607
\ \"reason-react\"\n\
1361013608
\ ],\n\
13611-
\ \"refmt\": 3,\n\
13612-
\ \"warnings\": {\n\
13613-
\ \"error\": \"+5\"\n\
13614-
\ }\n\
13609+
\ \"refmt\": 3\n\
1361513610
}\n\
1361613611
") ;
1361713612
File (".gitignore",

0 commit comments

Comments
 (0)