Skip to content

Commit 5351bb3

Browse files
committed
Support @uncurry externals with uncurried types.
So existing externals can be used without change in uncurried mode, without giving an error. That said, the `@uncurry` annotation becomes redundant in those cases.
1 parent 2fb38ce commit 5351bb3

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

jscomp/frontend/ast_core_type.ml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,16 @@ let get_uncurry_arity (ty : t) =
128128
| { ptyp_desc = Ptyp_arrow _ } -> Some (get_uncurry_arity_aux rest 1)
129129
| _ -> Some 0)
130130
| Ptyp_arrow (_, _, rest) -> Some (get_uncurry_arity_aux rest 1)
131+
| _ when Ast_uncurried.typeIsUncurriedFun ty ->
132+
let arity, _ = Ast_uncurried.typeExtractUncurriedFun ty in
133+
Some arity
131134
| _ -> None
132135

133136
let get_curry_arity (ty : t) =
134137
match ty.ptyp_desc with
135-
| Ptyp_constr ({ txt = Lident "function$" }, [ t; _ ]) ->
136-
get_uncurry_arity_aux t 0
138+
| _ when Ast_uncurried.typeIsUncurriedFun ty ->
139+
let arity, _ = Ast_uncurried.typeExtractUncurriedFun ty in
140+
arity
137141
| _ -> get_uncurry_arity_aux ty 0
138142

139143
(* add hoc for bs.send.pipe *)

jscomp/test/UncurriedExternals.js

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

3+
var React = require("react");
34

45
function dd(param) {
56
throw {
@@ -46,6 +47,14 @@ function tsiU(c) {
4647
});
4748
}
4849

50+
var match = React.useState(function () {
51+
return 3;
52+
});
53+
54+
var StandardNotation_get = match[0];
55+
56+
var StandardNotation_set = match[1];
57+
4958
var StandardNotation = {
5059
dd: dd,
5160
h: h,
@@ -57,7 +66,9 @@ var StandardNotation = {
5766
te: te,
5867
tcr: tcr,
5968
tsiC: tsiC,
60-
tsiU: tsiU
69+
tsiU: tsiU,
70+
get: StandardNotation_get,
71+
set: StandardNotation_set
6172
};
6273

6374
function dd$1(param) {
@@ -105,6 +116,14 @@ function tsiU$1(c) {
105116
});
106117
}
107118

119+
var match$1 = React.useState(function (param) {
120+
return 3;
121+
});
122+
123+
var get = match$1[0];
124+
125+
var set = match$1[1];
126+
108127
exports.StandardNotation = StandardNotation;
109128
exports.dd = dd$1;
110129
exports.h = h$1;
@@ -117,4 +136,6 @@ exports.te = te$1;
117136
exports.tcr = tcr$1;
118137
exports.tsiC = tsiC$1;
119138
exports.tsiU = tsiU$1;
139+
exports.get = get;
140+
exports.set = set;
120141
/* h Not a pure module */

jscomp/test/UncurriedExternals.res

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,19 @@ module StandardNotation = {
2424
external toException: (. exn) => exn = "%identity"
2525
let te = toException(. Not_found)
2626

27-
@obj external ccreate : (. unit) => string = ""
27+
@obj external ccreate: (. unit) => string = ""
2828
let tcr = ccreate(.)
2929

3030
type counter
3131
@set external setIncrementC: (counter, @this (counter, int) => unit) => unit = "increment"
3232
let tsiC = c => setIncrementC(c, @this (me, amount) => Js.log(me))
3333
@set external setIncrementU: (. counter, @this (. counter, int) => unit) => unit = "increment"
34-
let tsiU = c => setIncrementU(. c, @this (. me, amount) => Js.log(me))
34+
let tsiU = c => setIncrementU(.c, @this (. me, amount) => Js.log(me))
35+
36+
@module("react")
37+
external useState: (@uncurry (unit => 'state)) => ('state, ('state => 'state) => unit) =
38+
"useState"
39+
let (get, set) = useState(() => 3)
3540
}
3641

3742
@@uncurried
@@ -61,11 +66,15 @@ let tc = copy("abc")
6166
external toException: exn => exn = "%identity"
6267
let te = toException(Not_found)
6368

64-
@obj external ucreate : unit => string = ""
69+
@obj external ucreate: unit => string = ""
6570
let tcr = ucreate()
6671

6772
type counter
6873
@set external setIncrementC: (. counter, @this (. counter, int) => unit) => unit = "increment"
69-
let tsiC = c => setIncrementC(. c, @this (. me, amount) => Js.log(. me))
74+
let tsiC = c => setIncrementC(.c, @this (. me, amount) => Js.log(. me))
7075
@set external setIncrementU: (counter, @this (counter, int) => unit) => unit = "increment"
7176
let tsiU = c => setIncrementU(c, @this (me, amount) => Js.log(. me))
77+
78+
@module("react")
79+
external useState: (@uncurry (unit => 'state)) => ('state, ('state => 'state) => unit) = "useState"
80+
let (get, set) = useState(() => 3)

0 commit comments

Comments
 (0)