• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright JS Foundation and other contributors, http://js.foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/* This test checks async modifiers (nothing else). */
16
17function check_promise(p, value)
18{
19  assert(p instanceof Promise)
20
21  p.then(function(v) {
22    assert(v === value)
23  })
24}
25
26/* Async functions */
27
28async function f(a) {
29  return a
30}
31
32check_promise(f(1), 1)
33
34f = async function (a) { return a }
35check_promise(f(2), 2)
36
37f = (async function (a) { return a })
38check_promise(f(3), 3)
39
40f = [async function (a) { return a }]
41check_promise(f[0](4), 4)
42
43/* These four are parser tests. */
44async => {}
45async async => {}
46(async => {})
47(async async => {})
48
49f = async => async;
50assert(f(5) === 5)
51
52f = async async => async;
53check_promise(f(6), 6)
54
55f = (async => async)
56assert(f(7) === 7)
57
58f = (async async => async)
59check_promise(f(8), 8)
60
61f = [async => async]
62assert(f[0](9) === 9)
63
64f = [async async => async]
65check_promise(f[0](10), 10)
66
67f = async (a, b) => a + b;
68check_promise(f(10, 1), 11)
69
70f = (async (a, b) => a + b);
71check_promise(f(10, 2), 12)
72
73f = [async (a, b) => a + b];
74check_promise(f[0](10, 3), 13)
75
76f = true ? async () => 14 : 0;
77check_promise(f(), 14)
78
79f = (1, async async => async)
80check_promise(f(15), 15)
81
82/* Functions contain async references */
83
84function f1() {
85  var async = 1;
86
87  /* The arrow function after the newline should be ignored. */
88  var v1 = async
89  async => async
90
91  /* The function statement after the newline should not be an async function. */
92  var v2 = async
93  function g() { return 2 }
94
95  async
96  function h() { return 3 }
97
98  assert(v1 === 1)
99  assert(v2 === 1)
100  assert(g() === 2)
101  assert(h() === 3)
102}
103f1();
104
105function f2() {
106  var async = 1;
107
108  function g() { async = 2; }
109  g();
110
111  assert(async == 2);
112}
113f2();
114
115function f3() {
116  var v = 3;
117  var async = () => v = 4;
118
119  function g() { async(); }
120  g();
121
122  assert(v === 4);
123}
124f3();
125
126function f4() {
127  var v = 5;
128  var async = (a, b) => v = a + b;
129
130  function g() { async(((v)), ((v))); }
131  g();
132
133  assert(v === 10);
134}
135f4();
136
137function f5() {
138  var v = 0;
139  var async = (a, b) => v = a + b;
140
141  function g() { async((async(1,2)), ((async(3,4)))); }
142  g();
143
144  assert(v === 10);
145}
146f5();
147