1/* 2 * Copyright (c) 2023-2024 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 assert_true(value: boolean): void { 38 if (!value) { 39 return; 40 } 41 console.println("Value is not true"); 42 throw new Error(); 43} 44 45function prepare(): void { 46 called = false; 47} 48 49// check no-args static function call 50async function foo(): Promise<Object | null> { 51 called = true; 52 return null; 53} 54 55// check function call when all arguments are in registers on x86-64 56async 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> { 57 called = true; 58 assert_eq(i1, 1); 59 assert_eq(i2, 2); 60 assert_eq(i3, 3); 61 assert_eq(i4, 4); 62 assert_eq(i5, 5); 63 assert_eq(f1, 1.0); 64 assert_eq(f2, 2.0); 65 assert_eq(f3, 3.0); 66 assert_eq(f4, 4.0); 67 assert_eq(f5, 5.0); 68 assert_eq(f6, 6.0); 69 assert_eq(f7, 7.0); 70 assert_eq(f8, 8.0); 71 return null; 72} 73 74// check function call when some arguments are passed via the stack on x86-64 75async 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> { 76 called = true; 77 assert_eq(i1, 1); 78 assert_eq(i2, 2); 79 assert_eq(i3, 3); 80 assert_eq(i4, 4); 81 assert_eq(i5, 5); 82 assert_eq(i6, 6); 83 assert_eq(f1, 1.0); 84 assert_eq(f2, 2.0); 85 assert_eq(f3, 3.0); 86 assert_eq(f4, 4.0); 87 assert_eq(f5, 5.0); 88 assert_eq(f6, 6.0); 89 assert_eq(f7, 7.0); 90 assert_eq(f8, 8.0); 91 assert_eq(f9, 9.0); 92 return null; 93} 94 95// check function call when all arguments are in registers on arm32 96async function arm32_small(i1: int, i2: int, i3: int): Promise<Object | null> { 97 called = true; 98 assert_eq(i1, 1); 99 assert_eq(i2, 2); 100 assert_eq(i3, 3); 101 return null; 102} 103 104// check function call when some arguments are passed via the stack on arm32 105async function arm32_large(i1: int, i2: int, i3: int, i4: int, i5: int): Promise<Object | null> { 106 called = true; 107 assert_eq(i1, 1); 108 assert_eq(i2, 2); 109 assert_eq(i3, 3); 110 assert_eq(i4, 4); 111 assert_eq(i5, 5); 112 return null; 113} 114 115// check function call when all arguments are in registers on arm32hf 116async 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> { 117 called = true; 118 assert_eq(i1, 1); 119 assert_eq(i2, 2); 120 assert_eq(i3, 3); 121 assert_eq(f1, 1.0); 122 assert_eq(f2, 2.0); 123 assert_eq(f3, 3.0); 124 assert_eq(f4, 4.0); 125 assert_eq(f5, 5.0); 126 assert_eq(f6, 6.0); 127 assert_eq(f7, 7.0); 128 assert_eq(f8, 8.0); 129 return null; 130} 131 132// check function call when some arguments are passed via the stack on arm32hf 133async 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> { 134 called = true; 135 assert_eq(i1, 1); 136 assert_eq(i2, 2); 137 assert_eq(i3, 3); 138 assert_eq(i4, 4); 139 assert_eq(f1, 1.0); 140 assert_eq(f2, 2.0); 141 assert_eq(f3, 3.0); 142 assert_eq(f4, 4.0); 143 assert_eq(f5, 5.0); 144 assert_eq(f6, 6.0); 145 assert_eq(f7, 7.0); 146 assert_eq(f8, 8.0); 147 assert_eq(f9, 9.0); 148 return null; 149} 150 151// check function call when all arguments are in registers on arm64 152async 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> { 153 called = true; 154 assert_eq(i1, 1); 155 assert_eq(i2, 2); 156 assert_eq(i3, 3); 157 assert_eq(i4, 4); 158 assert_eq(i5, 5); 159 assert_eq(i6, 6); 160 assert_eq(i7, 7); 161 assert_eq(f1, 1.0); 162 assert_eq(f2, 2.0); 163 assert_eq(f3, 3.0); 164 assert_eq(f4, 4.0); 165 assert_eq(f5, 5.0); 166 assert_eq(f6, 6.0); 167 assert_eq(f7, 7.0); 168 assert_eq(f8, 8.0); 169 return null; 170} 171 172// check function call when some arguments are passed via the stack on arm64 173async 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> { 174 called = true; 175 assert_eq(i1, 1); 176 assert_eq(i2, 2); 177 assert_eq(i3, 3); 178 assert_eq(i4, 4); 179 assert_eq(i5, 5); 180 assert_eq(i6, 6); 181 assert_eq(i7, 7); 182 assert_eq(i8, 8); 183 assert_eq(f1, 1.0); 184 assert_eq(f2, 2.0); 185 assert_eq(f3, 3.0); 186 assert_eq(f4, 4.0); 187 assert_eq(f5, 5.0); 188 assert_eq(f6, 6.0); 189 assert_eq(f7, 7.0); 190 assert_eq(f8, 8.0); 191 assert_eq(f9, 9.0); 192 return null; 193} 194 195function main(): void { 196 prepare(); 197 foo(); 198 assert(called); 199 200 prepare(); 201 amd64_small(1, 2, 3, 4, 5, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0); 202 assert(called); 203 204 prepare(); 205 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); 206 assert(called); 207 208 prepare(); 209 arm32_small(1, 2, 3); 210 assert(called); 211 212 prepare(); 213 arm32_large(1, 2, 3, 4, 5); 214 assert(called); 215 216 prepare(); 217 arm32hf_small(1, 2, 3, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0); 218 assert(called); 219 220 prepare(); 221 arm32hf_large(1, 2, 3, 4, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0); 222 assert(called); 223 224 prepare(); 225 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); 226 assert(called); 227 228 prepare(); 229 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); 230 assert(called); 231} 232