Skip to content

Commit e918130

Browse files
committed
Use @async attribute instead of an external.
1 parent a4d248d commit e918130

27 files changed

+123
-93
lines changed

example-async/lib/js/src/AA.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example-async/src/AA.res

+51-45
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
// Boilerplate currently handwritten
2-
external async0: Js.Fn.arity0<'r> => Js.Fn.arity0<Js.Promise.t<'r>> = "?async"
3-
external async1: Js.Fn.arity1<'a1 => 'r> => Js.Fn.arity1<'a1 => Js.Promise.t<'r>> = "?async"
4-
external async2: Js.Fn.arity2<('a1, 'a2) => 'r> => Js.Fn.arity2<('a1, 'a2) => Js.Promise.t<'r>> =
5-
"?async"
6-
72
external await: Js.Promise.t<'a> => 'a = "?await"
83

94
type testable = (. unit) => Js.Promise.t<unit>
@@ -17,20 +12,24 @@ let addTest1 = (t, x) => tests->Js.Array2.push((. ()) => t(. x))->ignore
1712
//
1813
// Basic tests
1914

20-
let foo = async2((. x, y) => x + y)
15+
let foo = @async (. x, y) => x + y
2116

22-
let bar = async1((. ff) => {
23-
let a = await(ff(. 3, 4))
24-
let b = await(foo(. 5, 6))
25-
a + b
26-
})
17+
let bar =
18+
@async
19+
(. ff) => {
20+
let a = await(ff(. 3, 4))
21+
let b = await(foo(. 5, 6))
22+
a + b
23+
}
2724

28-
let baz = async0((. ()) => await(bar(. foo)))
25+
let baz = @async (. ()) => await(bar(. foo))
2926

30-
let testBaz: testable = async0((. ()) => {
31-
let n = await(baz(.))
32-
Js.log2("baz returned", n)
33-
})
27+
let testBaz: testable =
28+
@async
29+
(. ()) => {
30+
let n = await(baz(.))
31+
Js.log2("baz returned", n)
32+
}
3433

3534
testBaz->addTest
3635

@@ -40,18 +39,19 @@ testBaz->addTest
4039

4140
exception E(int)
4241

43-
let e1: testable = async0((. ()) => raise(E(1000)))
44-
let e2: testable = async0((. ()) => Js.Exn.raiseError("Some JS error"))
45-
let e3: testable = async0((. ()) => await(e1(.)))
46-
let e4: testable = async0((. ()) => await(e2(.)))
42+
let e1: testable = @async (. ()) => raise(E(1000))
43+
let e2: testable = @async (. ()) => Js.Exn.raiseError("Some JS error")
44+
let e3: testable = @async (. ()) => await(e1(.))
45+
let e4: testable = @async (. ()) => await(e2(.))
4746
let e5: testable = %raw(`function() { return Promise.reject(new Error('fail')) }`)
4847

49-
let testTryCatch = async1((. fn) =>
50-
try await(fn(.)) catch {
51-
| E(n) => Js.log2("testTryCatch: E", n)
52-
| JsError(_) => Js.log("testTryCatch: JsError")
53-
}
54-
)
48+
let testTryCatch =
49+
@async
50+
(. fn) =>
51+
try await(fn(.)) catch {
52+
| E(n) => Js.log2("testTryCatch: E", n)
53+
| JsError(_) => Js.log("testTryCatch: JsError")
54+
}
5555

5656
testTryCatch->addTest1(e1)
5757
testTryCatch->addTest1(e2)
@@ -63,28 +63,32 @@ testTryCatch->addTest1(e5)
6363
//
6464
// Check for nested promise
6565

66-
let singlePromise = async1((. x) => x + 1)
66+
let singlePromise = @async (. x) => x + 1
6767

68-
let nestedPromise = async1((. x) => {
69-
let resolve = x => [Js.Promise.resolve(x)]
70-
let _result = singlePromise(. x + 1)->resolve
71-
32
72-
})
68+
let nestedPromise =
69+
@async
70+
(. x) => {
71+
let resolve = x => [Js.Promise.resolve(x)]
72+
let _result = singlePromise(. x + 1)->resolve
73+
32
74+
}
7375

7476
//
7577
//
7678
// Test error handling in fetch
7779

7880
let explainError: unknown => string = %raw(`(e)=>e.toString()`)
7981

80-
let testFetch = async1((. url) => {
81-
switch await(Fetch.fetch(url)) {
82-
| response =>
83-
let status = response->Fetch.Response.status
84-
Js.log2("Fetch returned status:", status)
85-
| exception JsError(e) => Js.log2("Fetch returned an error:", e->explainError)
82+
let testFetch =
83+
@async
84+
(. url) => {
85+
switch await(Fetch.fetch(url)) {
86+
| response =>
87+
let status = response->Fetch.Response.status
88+
Js.log2("Fetch returned status:", status)
89+
| exception JsError(e) => Js.log2("Fetch returned an error:", e->explainError)
90+
}
8691
}
87-
})
8892

8993
testFetch->addTest1("https://www.google.com/sdkjdkghdsg")
9094
testFetch->addTest1("https://www.google.comsdkjdkghdsg")
@@ -93,12 +97,14 @@ testFetch->addTest1("https://www.google.comsdkjdkghdsg")
9397
//
9498
// Run tests
9599

96-
let runAllTests = async0((. ()) => {
97-
for i in 0 to Array.length(tests) - 1 {
98-
await(tests[i](.))
100+
let runAllTests =
101+
@async
102+
(. ()) => {
103+
for i in 0 to Array.length(tests) - 1 {
104+
await(tests[i](.))
105+
}
106+
// Note: this is no good, as await is inside a closure
107+
// tests->Js.Array2.forEach(test => await(test(.)))
99108
}
100-
// Note: this is no good, as await is inside a closure
101-
// tests->Js.Array2.forEach(test => await(test(.)))
102-
})
103109

104110
runAllTests(.)->ignore

jscomp/core/j.ml

+3-2
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,11 @@ and expression_desc =
142142
*)
143143
| New of expression * expression list option (* TODO: option remove *)
144144
| Var of vident
145-
| Fun of bool * ident list * block * Js_fun_env.t * bool
145+
| Fun of bool * ident list * block * Js_fun_env.t * bool * bool
146146
(* The first parameter by default is false,
147147
it will be true when it's a method
148-
The last pararemter [true] return unit
148+
The second-last pararemter [true] return unit
149+
The last pararemter [true] means async
149150
*)
150151
| Str of {delim: string option; txt: string}
151152
(* A string is UTF-8 encoded, and may contain

jscomp/core/js_analyzer.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ let free_variables (stats : idents_stats) =
5353
expression =
5454
(fun self exp ->
5555
match exp.expression_desc with
56-
| Fun (_, _, _, env, _)
56+
| Fun (_, _, _, env, _, _)
5757
(* a optimization to avoid walking into funciton again
5858
if it's already comuted
5959
*) ->

jscomp/core/js_dump.ml

+12-10
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ let rec try_optimize_curry cxt f len function_id =
297297
Curry_gen.pp_optimize_curry f len;
298298
P.paren_group f 1 (fun _ -> expression ~level:1 cxt f function_id)
299299

300-
and pp_function ~return_unit ~is_method cxt (f : P.t) ~fn_state
300+
and pp_function ~return_unit ~async ~is_method cxt (f : P.t) ~fn_state
301301
(l : Ident.t list) (b : J.block) (env : Js_fun_env.t) : cxt =
302302
match b with
303303
| [
@@ -415,7 +415,9 @@ and pp_function ~return_unit ~is_method cxt (f : P.t) ~fn_state
415415
->
416416
P.string f "async ";
417417
b
418-
| _ -> b
418+
| _ ->
419+
let () = if async then P.string f "async " in
420+
b
419421
in
420422
P.string f L.function_;
421423
P.space f;
@@ -540,10 +542,10 @@ and expression_desc cxt ~(level : int) f x : cxt =
540542
let cxt = expression ~level:0 cxt f e1 in
541543
comma_sp f;
542544
expression ~level:0 cxt f e2)
543-
| Fun (is_method, l, b, env, return_unit) ->
545+
| Fun (is_method, l, b, env, return_unit, async) ->
544546
(* TODO: dump for comments *)
545547
pp_function ~is_method cxt f ~fn_state:default_fn_exp_state l b env
546-
~return_unit
548+
~return_unit ~async
547549
(* TODO:
548550
when [e] is [Js_raw_code] with arity
549551
print it in a more precise way
@@ -564,10 +566,10 @@ and expression_desc cxt ~(level : int) f x : cxt =
564566
| [
565567
{
566568
expression_desc =
567-
Fun (is_method, l, b, env, return_unit);
569+
Fun (is_method, l, b, env, return_unit, async);
568570
};
569571
] ->
570-
pp_function ~is_method ~return_unit cxt f
572+
pp_function ~is_method ~return_unit ~async cxt f
571573
~fn_state:(No_name { single_arg = true })
572574
l b env
573575
| _ -> arguments cxt f el)
@@ -912,8 +914,8 @@ and variable_declaration top cxt f (variable : J.variable_declaration) : cxt =
912914
statement_desc top cxt f (J.Exp e)
913915
| _ -> (
914916
match e.expression_desc with
915-
| Fun (is_method, params, b, env, return_unit) ->
916-
pp_function ~is_method cxt f ~return_unit
917+
| Fun (is_method, params, b, env, return_unit, async) ->
918+
pp_function ~is_method cxt f ~return_unit ~async
917919
~fn_state:(if top then Name_top name else Name_non_top name)
918920
params b env
919921
| _ ->
@@ -1139,9 +1141,9 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
11391141
cxt
11401142
| Return e -> (
11411143
match e.expression_desc with
1142-
| Fun (is_method, l, b, env, return_unit) ->
1144+
| Fun (is_method, l, b, env, return_unit, async) ->
11431145
let cxt =
1144-
pp_function ~return_unit ~is_method cxt f ~fn_state:Is_return l b
1146+
pp_function ~return_unit ~is_method ~async cxt f ~fn_state:Is_return l b
11451147
env
11461148
in
11471149
semi f;

jscomp/core/js_exp_make.ml

+5-5
Original file line numberDiff line numberDiff line change
@@ -197,20 +197,20 @@ let unit : t = { expression_desc = Undefined; comment = None }
197197
[Js_fun_env.empty] is a mutable state ..
198198
*)
199199

200-
let ocaml_fun ?comment ?immutable_mask ~return_unit params block : t =
200+
let ocaml_fun ?comment ?immutable_mask ~return_unit ~async params block : t =
201201
let len = List.length params in
202202
{
203203
expression_desc =
204204
Fun
205-
(false, params, block, Js_fun_env.make ?immutable_mask len, return_unit);
205+
(false, params, block, Js_fun_env.make ?immutable_mask len, return_unit, async);
206206
comment;
207207
}
208208

209209
let method_ ?comment ?immutable_mask ~return_unit params block : t =
210210
let len = List.length params in
211211
{
212212
expression_desc =
213-
Fun (true, params, block, Js_fun_env.make ?immutable_mask len, return_unit);
213+
Fun (true, params, block, Js_fun_env.make ?immutable_mask len, return_unit, false);
214214
comment;
215215
}
216216

@@ -484,7 +484,7 @@ let bytes_length ?comment (e : t) : t =
484484

485485
let function_length ?comment (e : t) : t =
486486
match e.expression_desc with
487-
| Fun (b, params, _, _, _) ->
487+
| Fun (b, params, _, _, _, _) ->
488488
let params_length = List.length params in
489489
int ?comment
490490
(Int32.of_int (if b then params_length - 1 else params_length))
@@ -1158,7 +1158,7 @@ let of_block ?comment ?e block : t =
11581158
Ext_list.append block
11591159
[ { J.statement_desc = Return e; comment } ]),
11601160
Js_fun_env.make 0,
1161-
return_unit );
1161+
return_unit, (* async *) false);
11621162
}
11631163
[]
11641164

jscomp/core/js_exp_make.mli

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ val ocaml_fun :
8888
?comment:string ->
8989
?immutable_mask:bool array ->
9090
return_unit:bool ->
91+
async:bool ->
9192
J.ident list ->
9293
J.block ->
9394
t

jscomp/core/js_fold.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class fold =
146146
| Var _x0 ->
147147
let _self = _self#vident _x0 in
148148
_self
149-
| Fun (_x0, _x1, _x2, _x3, _x4) ->
149+
| Fun (_x0, _x1, _x2, _x3, _x4, _x5) ->
150150
let _self = list (fun _self -> _self#ident) _self _x1 in
151151
let _self = _self#block _x2 in
152152
_self

jscomp/core/js_pass_scope.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ let record_scope_pass =
137137
expression =
138138
(fun self state x ->
139139
match x.expression_desc with
140-
| Fun (_method_, params, block, env, _return_unit) ->
140+
| Fun (_method_, params, block, env, _return_unit, _async) ->
141141
(* Function is the only place to introduce a new scope in
142142
ES5
143143
TODO: check

jscomp/core/js_pass_tailcall_inline.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ let subst (export_set : Set_ident.t) stats =
165165
Some
166166
{
167167
expression_desc =
168-
Fun (false, params, block, env, _return_unit);
168+
Fun (false, params, block, env, _return_unit, _async);
169169
comment = _;
170170
};
171171
(*TODO: don't inline method tail call yet,
@@ -200,7 +200,7 @@ let subst (export_set : Set_ident.t) stats =
200200
Call
201201
( {
202202
expression_desc =
203-
Fun (false, params, block, env, _return_unit);
203+
Fun (false, params, block, env, _return_unit, _async);
204204
},
205205
args,
206206
_info );

jscomp/core/js_record_fold.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ let expression_desc : 'a. ('a, expression_desc) fn =
152152
| Var _x0 ->
153153
let st = _self.vident _self st _x0 in
154154
st
155-
| Fun (_x0, _x1, _x2, _x3, _x4) ->
155+
| Fun (_x0, _x1, _x2, _x3, _x4, _x5) ->
156156
let st = list _self.ident _self st _x1 in
157157
let st = _self.block _self st _x2 in
158158
st

jscomp/core/js_record_iter.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ let expression_desc : expression_desc fn =
118118
_self.expression _self _x0;
119119
option (fun _self arg -> list _self.expression _self arg) _self _x1
120120
| Var _x0 -> _self.vident _self _x0
121-
| Fun (_x0, _x1, _x2, _x3, _x4) ->
121+
| Fun (_x0, _x1, _x2, _x3, _x4, _x5) ->
122122
list _self.ident _self _x1;
123123
_self.block _self _x2
124124
| Str _ -> ()

jscomp/core/js_record_map.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,10 @@ let expression_desc : expression_desc fn =
150150
| Var _x0 ->
151151
let _x0 = _self.vident _self _x0 in
152152
Var _x0
153-
| Fun (_x0, _x1, _x2, _x3, _x4) ->
153+
| Fun (_x0, _x1, _x2, _x3, _x4, _x5) ->
154154
let _x1 = list _self.ident _self _x1 in
155155
let _x2 = _self.block _self _x2 in
156-
Fun (_x0, _x1, _x2, _x3, _x4)
156+
Fun (_x0, _x1, _x2, _x3, _x4, _x5)
157157
| Str _ as v -> v
158158
| Raw_js_code _ as v -> v
159159
| Array (_x0, _x1) ->

0 commit comments

Comments
 (0)