Skip to content

Commit 327b986

Browse files
cristianocnojaf
andauthored
Add support for async methods. (#7702)
* Add support for async methods. Fixes #7681 * Add changelog --------- Co-authored-by: nojaf <[email protected]>
1 parent 0e5d216 commit 327b986

File tree

9 files changed

+39
-7
lines changed

9 files changed

+39
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
- Fix rewatch not recompiling on changes under windows. https://github.com/rescript-lang/rescript/pull/7690
4141
- Fix locations of regex literals. https://github.com/rescript-lang/rescript/pull/7683
4242
- Fix async React component compilation. https://github.com/rescript-lang/rescript/pull/7704
43+
- Fix @this with async keyword. https://github.com/rescript-lang/rescript/pull/7702
4344

4445
# 12.0.0-beta.2
4546

compiler/core/js_exp_make.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ let ocaml_fun ?comment ?immutable_mask ?directive ~return_unit ~async
254254
comment;
255255
}
256256

257-
let method_ ?comment ?immutable_mask ~return_unit params body : t =
257+
let method_ ?comment ?immutable_mask ~async ~return_unit params body : t =
258258
let len = List.length params in
259259
{
260260
expression_desc =
@@ -265,7 +265,7 @@ let method_ ?comment ?immutable_mask ~return_unit params body : t =
265265
body;
266266
env = Js_fun_env.make ?immutable_mask len;
267267
return_unit;
268-
async = false;
268+
async;
269269
directive = None;
270270
};
271271
comment;

compiler/core/js_exp_make.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ val ocaml_fun :
111111
val method_ :
112112
?comment:string ->
113113
?immutable_mask:bool array ->
114+
async:bool ->
114115
return_unit:bool ->
115116
J.ident list ->
116117
J.block ->

compiler/core/lam_compile.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,9 +1623,9 @@ let compile output_prefix =
16231623
| {primitive = Pjs_unsafe_downgrade _; args} -> assert false
16241624
| {primitive = Pjs_fn_method; args = args_lambda} -> (
16251625
match args_lambda with
1626-
| [Lfunction {params; body; attr = {return_unit}}] ->
1626+
| [Lfunction {params; body; attr = {return_unit; async}}] ->
16271627
Js_output.output_of_block_and_expression lambda_cxt.continuation []
1628-
(E.method_ params ~return_unit
1628+
(E.method_ ~async ~return_unit params
16291629
(* Invariant: jmp_table can not across function boundary,
16301630
here we share env
16311631
*)

compiler/frontend/ast_uncurry_gen.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
open Ast_helper
2626

2727
(* Handling `fun [@this]` used in `object [@bs] end` *)
28-
let to_method_callback loc (self : Bs_ast_mapper.mapper) label
28+
let to_method_callback ~async loc (self : Bs_ast_mapper.mapper) label
2929
(self_pat : Parsetree.pattern) body : Parsetree.expression_desc =
3030
let self_pat = self.pat self self_pat in
3131
(match Ast_pat.is_single_variable_pattern_conservative self_pat with
@@ -50,7 +50,9 @@ let to_method_callback loc (self : Bs_ast_mapper.mapper) label
5050
let arity = List.length rev_extra_args in
5151
let body =
5252
match body.pexp_desc with
53-
| Pexp_fun f -> {body with pexp_desc = Pexp_fun {f with arity = Some arity}}
53+
| Pexp_fun f ->
54+
Ast_async.make_function_async ~async
55+
{body with pexp_desc = Pexp_fun {f with arity = Some arity; async}}
5456
| _ -> body
5557
in
5658
let arity_s = string_of_int arity in

compiler/frontend/ast_uncurry_gen.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

2525
val to_method_callback :
26+
async:bool ->
2627
Location.t ->
2728
Bs_ast_mapper.mapper ->
2829
Asttypes.arg_label ->

compiler/frontend/bs_builtin_ppx.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
111111
{
112112
e with
113113
pexp_desc =
114-
Ast_uncurry_gen.to_method_callback e.pexp_loc self label pat body;
114+
Ast_uncurry_gen.to_method_callback ~async e.pexp_loc self label pat
115+
body;
115116
pexp_attributes;
116117
})
117118
| Pexp_apply _ -> Ast_exp_apply.app_exp_mapper e self

tests/tests/src/UncurriedExternals.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,20 @@ function methodWithAsync(param) {
6969
return (async arg => $$this + arg | 0)(param);
7070
}
7171

72+
let p1 = {
73+
watch: async function (name, ev) {
74+
let pc = this ;
75+
console.log(pc);
76+
}
77+
};
78+
79+
let AsyncMethod = {
80+
p1: p1
81+
};
82+
7283
export {
7384
StandardNotation,
7485
methodWithAsync,
86+
AsyncMethod,
7587
}
7688
/* h Not a pure module */

tests/tests/src/UncurriedExternals.res

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,17 @@ module StandardNotation = {
3636
}
3737

3838
let methodWithAsync = @this this => async arg => this + arg
39+
40+
module AsyncMethod = {
41+
type pluginContext
42+
type changeEvent
43+
44+
type p = {watch: @this (pluginContext, string, changeEvent) => promise<unit>}
45+
46+
let p1 = {
47+
watch: @this
48+
async (pc, name, ev) => {
49+
Console.log(pc)
50+
},
51+
}
52+
}

0 commit comments

Comments
 (0)