1425bb815Sopenharmony_ci// Copyright JS Foundation and other contributors, http://js.foundation 2425bb815Sopenharmony_ci// 3425bb815Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License"); 4425bb815Sopenharmony_ci// you may not use this file except in compliance with the License. 5425bb815Sopenharmony_ci// You may obtain a copy of the License at 6425bb815Sopenharmony_ci// 7425bb815Sopenharmony_ci// http://www.apache.org/licenses/LICENSE-2.0 8425bb815Sopenharmony_ci// 9425bb815Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software 10425bb815Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS 11425bb815Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12425bb815Sopenharmony_ci// See the License for the specific language governing permissions and 13425bb815Sopenharmony_ci// limitations under the License. 14425bb815Sopenharmony_ci 15425bb815Sopenharmony_ci/* This test checks async modifiers (nothing else). */ 16425bb815Sopenharmony_ci 17425bb815Sopenharmony_cifunction check_promise(p, value) 18425bb815Sopenharmony_ci{ 19425bb815Sopenharmony_ci assert(p instanceof Promise) 20425bb815Sopenharmony_ci 21425bb815Sopenharmony_ci p.then(function(v) { 22425bb815Sopenharmony_ci assert(v === value) 23425bb815Sopenharmony_ci }) 24425bb815Sopenharmony_ci} 25425bb815Sopenharmony_ci 26425bb815Sopenharmony_ci/* Async functions */ 27425bb815Sopenharmony_ci 28425bb815Sopenharmony_ciasync function f(a) { 29425bb815Sopenharmony_ci return a 30425bb815Sopenharmony_ci} 31425bb815Sopenharmony_ci 32425bb815Sopenharmony_cicheck_promise(f(1), 1) 33425bb815Sopenharmony_ci 34425bb815Sopenharmony_cif = async function (a) { return a } 35425bb815Sopenharmony_cicheck_promise(f(2), 2) 36425bb815Sopenharmony_ci 37425bb815Sopenharmony_cif = (async function (a) { return a }) 38425bb815Sopenharmony_cicheck_promise(f(3), 3) 39425bb815Sopenharmony_ci 40425bb815Sopenharmony_cif = [async function (a) { return a }] 41425bb815Sopenharmony_cicheck_promise(f[0](4), 4) 42425bb815Sopenharmony_ci 43425bb815Sopenharmony_ci/* These four are parser tests. */ 44425bb815Sopenharmony_ciasync => {} 45425bb815Sopenharmony_ciasync async => {} 46425bb815Sopenharmony_ci(async => {}) 47425bb815Sopenharmony_ci(async async => {}) 48425bb815Sopenharmony_ci 49425bb815Sopenharmony_cif = async => async; 50425bb815Sopenharmony_ciassert(f(5) === 5) 51425bb815Sopenharmony_ci 52425bb815Sopenharmony_cif = async async => async; 53425bb815Sopenharmony_cicheck_promise(f(6), 6) 54425bb815Sopenharmony_ci 55425bb815Sopenharmony_cif = (async => async) 56425bb815Sopenharmony_ciassert(f(7) === 7) 57425bb815Sopenharmony_ci 58425bb815Sopenharmony_cif = (async async => async) 59425bb815Sopenharmony_cicheck_promise(f(8), 8) 60425bb815Sopenharmony_ci 61425bb815Sopenharmony_cif = [async => async] 62425bb815Sopenharmony_ciassert(f[0](9) === 9) 63425bb815Sopenharmony_ci 64425bb815Sopenharmony_cif = [async async => async] 65425bb815Sopenharmony_cicheck_promise(f[0](10), 10) 66425bb815Sopenharmony_ci 67425bb815Sopenharmony_cif = async (a, b) => a + b; 68425bb815Sopenharmony_cicheck_promise(f(10, 1), 11) 69425bb815Sopenharmony_ci 70425bb815Sopenharmony_cif = (async (a, b) => a + b); 71425bb815Sopenharmony_cicheck_promise(f(10, 2), 12) 72425bb815Sopenharmony_ci 73425bb815Sopenharmony_cif = [async (a, b) => a + b]; 74425bb815Sopenharmony_cicheck_promise(f[0](10, 3), 13) 75425bb815Sopenharmony_ci 76425bb815Sopenharmony_cif = true ? async () => 14 : 0; 77425bb815Sopenharmony_cicheck_promise(f(), 14) 78425bb815Sopenharmony_ci 79425bb815Sopenharmony_cif = (1, async async => async) 80425bb815Sopenharmony_cicheck_promise(f(15), 15) 81425bb815Sopenharmony_ci 82425bb815Sopenharmony_ci/* Functions contain async references */ 83425bb815Sopenharmony_ci 84425bb815Sopenharmony_cifunction f1() { 85425bb815Sopenharmony_ci var async = 1; 86425bb815Sopenharmony_ci 87425bb815Sopenharmony_ci /* The arrow function after the newline should be ignored. */ 88425bb815Sopenharmony_ci var v1 = async 89425bb815Sopenharmony_ci async => async 90425bb815Sopenharmony_ci 91425bb815Sopenharmony_ci /* The function statement after the newline should not be an async function. */ 92425bb815Sopenharmony_ci var v2 = async 93425bb815Sopenharmony_ci function g() { return 2 } 94425bb815Sopenharmony_ci 95425bb815Sopenharmony_ci async 96425bb815Sopenharmony_ci function h() { return 3 } 97425bb815Sopenharmony_ci 98425bb815Sopenharmony_ci assert(v1 === 1) 99425bb815Sopenharmony_ci assert(v2 === 1) 100425bb815Sopenharmony_ci assert(g() === 2) 101425bb815Sopenharmony_ci assert(h() === 3) 102425bb815Sopenharmony_ci} 103425bb815Sopenharmony_cif1(); 104425bb815Sopenharmony_ci 105425bb815Sopenharmony_cifunction f2() { 106425bb815Sopenharmony_ci var async = 1; 107425bb815Sopenharmony_ci 108425bb815Sopenharmony_ci function g() { async = 2; } 109425bb815Sopenharmony_ci g(); 110425bb815Sopenharmony_ci 111425bb815Sopenharmony_ci assert(async == 2); 112425bb815Sopenharmony_ci} 113425bb815Sopenharmony_cif2(); 114425bb815Sopenharmony_ci 115425bb815Sopenharmony_cifunction f3() { 116425bb815Sopenharmony_ci var v = 3; 117425bb815Sopenharmony_ci var async = () => v = 4; 118425bb815Sopenharmony_ci 119425bb815Sopenharmony_ci function g() { async(); } 120425bb815Sopenharmony_ci g(); 121425bb815Sopenharmony_ci 122425bb815Sopenharmony_ci assert(v === 4); 123425bb815Sopenharmony_ci} 124425bb815Sopenharmony_cif3(); 125425bb815Sopenharmony_ci 126425bb815Sopenharmony_cifunction f4() { 127425bb815Sopenharmony_ci var v = 5; 128425bb815Sopenharmony_ci var async = (a, b) => v = a + b; 129425bb815Sopenharmony_ci 130425bb815Sopenharmony_ci function g() { async(((v)), ((v))); } 131425bb815Sopenharmony_ci g(); 132425bb815Sopenharmony_ci 133425bb815Sopenharmony_ci assert(v === 10); 134425bb815Sopenharmony_ci} 135425bb815Sopenharmony_cif4(); 136425bb815Sopenharmony_ci 137425bb815Sopenharmony_cifunction f5() { 138425bb815Sopenharmony_ci var v = 0; 139425bb815Sopenharmony_ci var async = (a, b) => v = a + b; 140425bb815Sopenharmony_ci 141425bb815Sopenharmony_ci function g() { async((async(1,2)), ((async(3,4)))); } 142425bb815Sopenharmony_ci g(); 143425bb815Sopenharmony_ci 144425bb815Sopenharmony_ci assert(v === 10); 145425bb815Sopenharmony_ci} 146425bb815Sopenharmony_cif5(); 147