1/* 2 * Copyright (c) 2023-2025 Huawei Device Co., Ltd. 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 16// Test async bridge 17// Check all parameters are passed correctly for different architectures 18 19let called: boolean = false; 20 21function assert_eq(value1: int, value2: int): void { 22 if (value1 == value2) { 23 return; 24 } 25 console.println("Values of type int are not equal: " + value1 + " != " + value2); 26 throw new Error(); 27} 28 29function assert_eq(value1: double, value2: double): void { 30 if (value1 == value2) { 31 return; 32 } 33 console.println("Values of type double are not equal: " + value1 + " != " + value2); 34 throw new Error(); 35} 36 37function prepare(): void { 38 called = false; 39} 40 41// check no-args static function call 42async function foo(): Promise<Object | null> { 43 called = true; 44 return null; 45} 46 47// check function call when all arguments are in registers on x86-64 48async function amd64_small(i1: int, i2: int, i3: int, i4: int, i5: int, f1: double, f2: double, f3: double, f4: double, f5: double, f6: double, f7: double, f8: double): Promise<Object | null> { 49 called = true; 50 assert_eq(i1, 1); 51 assert_eq(i2, 2); 52 assert_eq(i3, 3); 53 assert_eq(i4, 4); 54 assert_eq(i5, 5); 55 assert_eq(f1, 1.0); 56 assert_eq(f2, 2.0); 57 assert_eq(f3, 3.0); 58 assert_eq(f4, 4.0); 59 assert_eq(f5, 5.0); 60 assert_eq(f6, 6.0); 61 assert_eq(f7, 7.0); 62 assert_eq(f8, 8.0); 63 return null; 64} 65 66// check function call when some arguments are passed via the stack on x86-64 67async function amd64_large(i1: int, i2: int, i3: int, i4: int, i5: int, i6: int, f1: double, f2: double, f3: double, f4: double, f5: double, f6: double, f7: double, f8: double, f9: double): Promise<Object | null> { 68 called = true; 69 assert_eq(i1, 1); 70 assert_eq(i2, 2); 71 assert_eq(i3, 3); 72 assert_eq(i4, 4); 73 assert_eq(i5, 5); 74 assert_eq(i6, 6); 75 assert_eq(f1, 1.0); 76 assert_eq(f2, 2.0); 77 assert_eq(f3, 3.0); 78 assert_eq(f4, 4.0); 79 assert_eq(f5, 5.0); 80 assert_eq(f6, 6.0); 81 assert_eq(f7, 7.0); 82 assert_eq(f8, 8.0); 83 assert_eq(f9, 9.0); 84 return null; 85} 86 87// check function call when all arguments are in registers on arm32 88async function arm32_small(i1: int, i2: int, i3: int): Promise<Object | null> { 89 called = true; 90 assert_eq(i1, 1); 91 assert_eq(i2, 2); 92 assert_eq(i3, 3); 93 return null; 94} 95 96// check function call when some arguments are passed via the stack on arm32 97async function arm32_large(i1: int, i2: int, i3: int, i4: int, i5: int): Promise<Object | null> { 98 called = true; 99 assert_eq(i1, 1); 100 assert_eq(i2, 2); 101 assert_eq(i3, 3); 102 assert_eq(i4, 4); 103 assert_eq(i5, 5); 104 return null; 105} 106 107// check function call when all arguments are in registers on arm32hf 108async function arm32hf_small(i1: int, i2: int, i3: int, f1: double, f2: double, f3: double, f4: double, f5: double, f6: double, f7: double, f8: double): Promise<Object | null> { 109 called = true; 110 assert_eq(i1, 1); 111 assert_eq(i2, 2); 112 assert_eq(i3, 3); 113 assert_eq(f1, 1.0); 114 assert_eq(f2, 2.0); 115 assert_eq(f3, 3.0); 116 assert_eq(f4, 4.0); 117 assert_eq(f5, 5.0); 118 assert_eq(f6, 6.0); 119 assert_eq(f7, 7.0); 120 assert_eq(f8, 8.0); 121 return null; 122} 123 124// check function call when some arguments are passed via the stack on arm32hf 125async function arm32hf_large(i1: int, i2: int, i3: int, i4: int, f1: double, f2: double, f3: double, f4: double, f5: double, f6: double, f7: double, f8: double, f9: double): Promise<Object | null> { 126 called = true; 127 assert_eq(i1, 1); 128 assert_eq(i2, 2); 129 assert_eq(i3, 3); 130 assert_eq(i4, 4); 131 assert_eq(f1, 1.0); 132 assert_eq(f2, 2.0); 133 assert_eq(f3, 3.0); 134 assert_eq(f4, 4.0); 135 assert_eq(f5, 5.0); 136 assert_eq(f6, 6.0); 137 assert_eq(f7, 7.0); 138 assert_eq(f8, 8.0); 139 assert_eq(f9, 9.0); 140 return null; 141} 142 143// check function call when all arguments are in registers on arm64 144async function arm64_small(i1: int, i2: int, i3: int, i4: int, i5: int, i6: int, i7: int, f1: double, f2: double, f3: double, f4: double, f5: double, f6: double, f7: double, f8: double): Promise<Object | null> { 145 called = true; 146 assert_eq(i1, 1); 147 assert_eq(i2, 2); 148 assert_eq(i3, 3); 149 assert_eq(i4, 4); 150 assert_eq(i5, 5); 151 assert_eq(i6, 6); 152 assert_eq(i7, 7); 153 assert_eq(f1, 1.0); 154 assert_eq(f2, 2.0); 155 assert_eq(f3, 3.0); 156 assert_eq(f4, 4.0); 157 assert_eq(f5, 5.0); 158 assert_eq(f6, 6.0); 159 assert_eq(f7, 7.0); 160 assert_eq(f8, 8.0); 161 return null; 162} 163 164// check function call when some arguments are passed via the stack on arm64 165async function arm64_large(i1: int, i2: int, i3: int, i4: int, i5: int, i6: int, i7: int, i8: int, f1: double, f2: double, f3: double, f4: double, f5: double, f6: double, f7: double, f8: double, f9: double): Promise<Object | null> { 166 called = true; 167 assert_eq(i1, 1); 168 assert_eq(i2, 2); 169 assert_eq(i3, 3); 170 assert_eq(i4, 4); 171 assert_eq(i5, 5); 172 assert_eq(i6, 6); 173 assert_eq(i7, 7); 174 assert_eq(i8, 8); 175 assert_eq(f1, 1.0); 176 assert_eq(f2, 2.0); 177 assert_eq(f3, 3.0); 178 assert_eq(f4, 4.0); 179 assert_eq(f5, 5.0); 180 assert_eq(f6, 6.0); 181 assert_eq(f7, 7.0); 182 assert_eq(f8, 8.0); 183 assert_eq(f9, 9.0); 184 return null; 185} 186 187async function crasher(): Promise<Int> { 188 return await Promise.resolve(1); 189} 190 191// check async/await in global context 192try { 193 let x = crasher(); 194} catch (e) { 195 assertTrue(e instanceof InvalidCoroutineOperationError); 196} 197 198function main(): void { 199 prepare(); 200 let p = foo(); 201 assertTrue(called); 202 await p; 203 204 prepare(); 205 p = amd64_small(1, 2, 3, 4, 5, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0); 206 assertTrue(called); 207 await p; 208 209 prepare(); 210 p = amd64_large(1, 2, 3, 4, 5, 6, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0); 211 assertTrue(called); 212 await p; 213 214 prepare(); 215 p = arm32_small(1, 2, 3); 216 assertTrue(called); 217 await p; 218 219 prepare(); 220 p = arm32_large(1, 2, 3, 4, 5); 221 assertTrue(called); 222 await p; 223 224 prepare(); 225 p = arm32hf_small(1, 2, 3, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0); 226 assertTrue(called); 227 await p; 228 229 prepare(); 230 p = arm32hf_large(1, 2, 3, 4, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0); 231 assertTrue(called); 232 await p; 233 234 prepare(); 235 p = arm64_small(1, 2, 3, 4, 5, 6, 7, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0); 236 assertTrue(called); 237 await p; 238 239 prepare(); 240 p = arm64_large(1, 2, 3, 4, 5, 6, 7, 8, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0); 241 assertTrue(called); 242 await p; 243} 244