Skip to content

Commit 4cc6291

Browse files
committed
Add support for uncurried @this
Both in types in externals and in function definitions, the `@this` annotation now ignores uncurried types. In any case, the processing replaces the type (and the function definition) with some internal representation of method types (and of methods). This is so when switching to uncurried mode, the code does not need changing.
1 parent 611d3a8 commit 4cc6291

8 files changed

+114
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
- Add support for unary uncurried pipe in uncurried mode https://github.com/rescript-lang/rescript-compiler/pull/5804
2020
- Add support for partial application of uncurried functions: with uncurried application one can provide a
2121
subset of the arguments, and return a curried type with the remaining ones https://github.com/rescript-lang/rescript-compiler/pull/5805
22-
- Add support for uncurried externals https://github.com/rescript-lang/rescript-compiler/pull/5815 https://github.com/rescript-lang/rescript-compiler/pull/5819
22+
- Add support for uncurried externals https://github.com/rescript-lang/rescript-compiler/pull/5815 https://github.com/rescript-lang/rescript-compiler/pull/5819 https://github.com/rescript-lang/rescript-compiler/pull/5830
2323
- Parser/Printer: unify uncurried functions of arity 0, and of arity 1 taking unit. There's now only arity 1 in the source language. https://github.com/rescript-lang/rescript-compiler/pull/5825
2424

2525

jscomp/frontend/ast_core_type_class_type.ml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,12 @@ let typ_mapper (self : Bs_ast_mapper.mapper) (ty : Parsetree.core_type) =
122122
match ty with
123123
| {
124124
ptyp_attributes;
125-
ptyp_desc = Ptyp_arrow (label, args, body);
125+
ptyp_desc =
126+
( Ptyp_arrow (label, args, body)
127+
| Ptyp_constr
128+
(* Js.Fn.xx is re-wrapped around only in case Nothing below *)
129+
( { txt = Ldot (Ldot (Lident "Js", "Fn"), _) },
130+
[ { ptyp_desc = Ptyp_arrow (label, args, body) } ] ) );
126131
(* let it go without regard label names,
127132
it will report error later when the label is not empty
128133
*)

jscomp/frontend/bs_builtin_ppx.ml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,17 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
115115
| true, pexp_attributes ->
116116
Ast_bs_open.convertBsErrorFunction e.pexp_loc self pexp_attributes
117117
cases)
118+
| Pexp_record
119+
( [
120+
( { txt = Ldot (Ldot (Lident "Js", "Fn"), _) },
121+
({ pexp_desc = Pexp_fun _; pexp_attributes } as inner_exp) );
122+
],
123+
None )
124+
when match Ast_attributes.process_attributes_rev pexp_attributes with
125+
| Meth_callback _, _ -> true
126+
| _ -> false ->
127+
(* Treat @this (. x, y, z) => ... just like @this (x, y, z) => ... *)
128+
self.expr self inner_exp
118129
| Pexp_fun (label, _, pat, body) -> (
119130
let async = Ast_attributes.has_async_payload e.pexp_attributes <> None in
120131
match Ast_attributes.process_attributes_rev e.pexp_attributes with

jscomp/test/UncurriedExternals.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ var te = (function (prim) {
3434

3535
var tcr = {};
3636

37+
function tsiC(c) {
38+
c.increment = (function (amount) {
39+
var me = this ;
40+
console.log(me);
41+
});
42+
}
43+
44+
function tsiU(c) {
45+
c.increment = (function (amount) {
46+
var me = this ;
47+
console.log(me);
48+
});
49+
}
50+
3751
var StandardNotation = {
3852
dd: dd,
3953
h: h,
@@ -43,7 +57,9 @@ var StandardNotation = {
4357
tg: tg,
4458
tc: tc,
4559
te: te,
46-
tcr: tcr
60+
tcr: tcr,
61+
tsiC: tsiC,
62+
tsiU: tsiU
4763
};
4864

4965
function dd$1(param) {
@@ -79,6 +95,20 @@ var te$1 = (function (prim) {
7995

8096
var tcr$1 = {};
8197

98+
function tsiC$1(c) {
99+
c.increment = (function (amount) {
100+
var me = this ;
101+
console.log(me);
102+
});
103+
}
104+
105+
function tsiU$1(c) {
106+
c.increment = (function (amount) {
107+
var me = this ;
108+
console.log(me);
109+
});
110+
}
111+
82112
exports.StandardNotation = StandardNotation;
83113
exports.dd = dd$1;
84114
exports.h = h$1;
@@ -89,4 +119,6 @@ exports.tg = tg$1;
89119
exports.tc = tc$1;
90120
exports.te = te$1;
91121
exports.tcr = tcr$1;
122+
exports.tsiC = tsiC$1;
123+
exports.tsiU = tsiU$1;
92124
/* h Not a pure module */

jscomp/test/UncurriedExternals.res

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ module StandardNotation = {
2626

2727
@obj external ccreate : (. unit) => string = ""
2828
let tcr = ccreate(.)
29+
30+
type counter
31+
@set external setIncrementC: (counter, @this (counter, int) => unit) => unit = "increment"
32+
let tsiC = c => setIncrementC(c, @this (me, amount) => Js.log(me))
33+
@set external setIncrementU: (. counter, @this (. counter, int) => unit) => unit = "increment"
34+
let tsiU = c => setIncrementU(. c, @this (. me, amount) => Js.log(me))
2935
}
3036

3137
@@uncurried
@@ -57,3 +63,9 @@ let te = toException(Not_found)
5763

5864
@obj external ucreate : unit => string = ""
5965
let tcr = ucreate()
66+
67+
type counter
68+
@set external setIncrementC: (. counter, @this (. counter, int) => unit) => unit = "increment"
69+
let tsiC = c => setIncrementC(. c, @this (. me, amount) => Js.log(. me))
70+
@set external setIncrementU: (counter, @this (counter, int) => unit) => unit = "increment"
71+
let tsiU = c => setIncrementU(c, @this (me, amount) => Js.log(. me))

lib/4.06.1/unstable/js_compiler.ml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146937,7 +146937,12 @@ let typ_mapper (self : Bs_ast_mapper.mapper) (ty : Parsetree.core_type) =
146937146937
match ty with
146938146938
| {
146939146939
ptyp_attributes;
146940-
ptyp_desc = Ptyp_arrow (label, args, body);
146940+
ptyp_desc =
146941+
( Ptyp_arrow (label, args, body)
146942+
| Ptyp_constr
146943+
(* Js.Fn.xx is re-wrapped around only in case Nothing below *)
146944+
( { txt = Ldot (Ldot (Lident "Js", "Fn"), _) },
146945+
[ { ptyp_desc = Ptyp_arrow (label, args, body) } ] ) );
146941146946
(* let it go without regard label names,
146942146947
it will report error later when the label is not empty
146943146948
*)
@@ -152110,6 +152115,17 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
152110152115
| true, pexp_attributes ->
152111152116
Ast_bs_open.convertBsErrorFunction e.pexp_loc self pexp_attributes
152112152117
cases)
152118+
| Pexp_record
152119+
( [
152120+
( { txt = Ldot (Ldot (Lident "Js", "Fn"), _) },
152121+
({ pexp_desc = Pexp_fun _; pexp_attributes } as inner_exp) );
152122+
],
152123+
None )
152124+
when match Ast_attributes.process_attributes_rev pexp_attributes with
152125+
| Meth_callback _, _ -> true
152126+
| _ -> false ->
152127+
(* Treat @this (. x, y, z) => ... just like @this (x, y, z) => ... *)
152128+
self.expr self inner_exp
152113152129
| Pexp_fun (label, _, pat, body) -> (
152114152130
let async = Ast_attributes.has_async_payload e.pexp_attributes <> None in
152115152131
match Ast_attributes.process_attributes_rev e.pexp_attributes with

lib/4.06.1/unstable/js_playground_compiler.ml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146937,7 +146937,12 @@ let typ_mapper (self : Bs_ast_mapper.mapper) (ty : Parsetree.core_type) =
146937146937
match ty with
146938146938
| {
146939146939
ptyp_attributes;
146940-
ptyp_desc = Ptyp_arrow (label, args, body);
146940+
ptyp_desc =
146941+
( Ptyp_arrow (label, args, body)
146942+
| Ptyp_constr
146943+
(* Js.Fn.xx is re-wrapped around only in case Nothing below *)
146944+
( { txt = Ldot (Ldot (Lident "Js", "Fn"), _) },
146945+
[ { ptyp_desc = Ptyp_arrow (label, args, body) } ] ) );
146941146946
(* let it go without regard label names,
146942146947
it will report error later when the label is not empty
146943146948
*)
@@ -152110,6 +152115,17 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
152110152115
| true, pexp_attributes ->
152111152116
Ast_bs_open.convertBsErrorFunction e.pexp_loc self pexp_attributes
152112152117
cases)
152118+
| Pexp_record
152119+
( [
152120+
( { txt = Ldot (Ldot (Lident "Js", "Fn"), _) },
152121+
({ pexp_desc = Pexp_fun _; pexp_attributes } as inner_exp) );
152122+
],
152123+
None )
152124+
when match Ast_attributes.process_attributes_rev pexp_attributes with
152125+
| Meth_callback _, _ -> true
152126+
| _ -> false ->
152127+
(* Treat @this (. x, y, z) => ... just like @this (x, y, z) => ... *)
152128+
self.expr self inner_exp
152113152129
| Pexp_fun (label, _, pat, body) -> (
152114152130
let async = Ast_attributes.has_async_payload e.pexp_attributes <> None in
152115152131
match Ast_attributes.process_attributes_rev e.pexp_attributes with

lib/4.06.1/whole_compiler.ml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157221,7 +157221,12 @@ let typ_mapper (self : Bs_ast_mapper.mapper) (ty : Parsetree.core_type) =
157221157221
match ty with
157222157222
| {
157223157223
ptyp_attributes;
157224-
ptyp_desc = Ptyp_arrow (label, args, body);
157224+
ptyp_desc =
157225+
( Ptyp_arrow (label, args, body)
157226+
| Ptyp_constr
157227+
(* Js.Fn.xx is re-wrapped around only in case Nothing below *)
157228+
( { txt = Ldot (Ldot (Lident "Js", "Fn"), _) },
157229+
[ { ptyp_desc = Ptyp_arrow (label, args, body) } ] ) );
157225157230
(* let it go without regard label names,
157226157231
it will report error later when the label is not empty
157227157232
*)
@@ -162394,6 +162399,17 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
162394162399
| true, pexp_attributes ->
162395162400
Ast_bs_open.convertBsErrorFunction e.pexp_loc self pexp_attributes
162396162401
cases)
162402+
| Pexp_record
162403+
( [
162404+
( { txt = Ldot (Ldot (Lident "Js", "Fn"), _) },
162405+
({ pexp_desc = Pexp_fun _; pexp_attributes } as inner_exp) );
162406+
],
162407+
None )
162408+
when match Ast_attributes.process_attributes_rev pexp_attributes with
162409+
| Meth_callback _, _ -> true
162410+
| _ -> false ->
162411+
(* Treat @this (. x, y, z) => ... just like @this (x, y, z) => ... *)
162412+
self.expr self inner_exp
162397162413
| Pexp_fun (label, _, pat, body) -> (
162398162414
let async = Ast_attributes.has_async_payload e.pexp_attributes <> None in
162399162415
match Ast_attributes.process_attributes_rev e.pexp_attributes with

0 commit comments

Comments
 (0)