1/* 2 * Copyright (c) 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 16function getFuncWithArgsZero(func: (() => void) | (() => Promise<void>)) { 17 return new Promise<void>(async (resolve: (value: PromiseLike<void>) => void) => { 18 await (func as () => Promise<void>)(); 19 const funcStr = Type.of(func as object).getLiteral() 20 const argsCount = getFunctionArgumentsCount(funcStr) 21 const isAsync = checkIsAsyncFunction(funcStr) 22 resolve(Promise.resolve()); 23 }); 24} 25 26function getFunctionArgumentsCount(funcStr: string): number { 27 const regex = new RegExp("^[0123456789]$", "g") 28 let count = "" 29 for(let ch of funcStr) { 30 let str = ch; 31 if(regex.test(str)) { 32 count = str 33 } 34 } 35 if(count.length) { 36 return Number(count) 37 } 38 return 0 39} 40 41function checkIsAsyncFunction(funcStr: string): boolean { 42 const endIndex = funcStr.lastIndexOf("):") 43 const validStr = funcStr.slice(endIndex) 44 if(validStr.indexOf("Promise") !== -1) { 45 return true 46 } else { 47 return false 48 } 49} 50 51async function testAsyncGetFuncWithArgsZeroSafe() { 52 let success = false; 53 try { 54 await getFuncWithArgsZero( async () => {}); 55 success = true; 56 } catch (e) { 57 success = false; 58 } 59 assertEQ(success, true, "getFuncWithArgsZero with async should not throw"); 60} 61 62function testGetFuncWithArgsZeroSafe() { 63 let success = false; 64 try { 65 getFuncWithArgsZero(() => {}); 66 success = true; 67 } catch (e) { 68 success = false; 69 } 70 assertEQ(success, true, "getFuncWithArgsZero should not throw"); 71} 72 73function main(): void { 74 const func1 = (a: number, b: number): number => { 75 return a + b; 76 }; 77 78 const func2 = async (): Promise<void> => {}; 79 80 const func3 = (): number => { 81 return 42; 82 }; 83 84 const func4 = async (x: number, y: number, z: number): Promise<void> => {}; 85 86 const funcStr1: string = Type.of(func1 as object).getLiteral(); 87 const funcStr2: string = Type.of(func2 as object).getLiteral(); 88 const funcStr3: string = Type.of(func3 as object).getLiteral(); 89 const funcStr4: string = Type.of(func4 as object).getLiteral(); 90 91 // Test getLiteral() 92 assertEQ(funcStr1, "(1: std.core.Double, 2: std.core.Double): std.core.Double", "func1 literal check"); 93 assertEQ(funcStr2, "(): std.core.Promise", "func2 literal check"); 94 assertEQ(funcStr3, "(): std.core.Double", "func3 literal check"); 95 assertEQ(funcStr4, "(1: std.core.Double, 2: std.core.Double, 3: std.core.Double): std.core.Promise", "func4 literal check"); 96 97 // Test getFunctionArgumentsCount 98 assertEQ(getFunctionArgumentsCount(funcStr1), 2, "func1 should have 2 arguments"); 99 assertEQ(getFunctionArgumentsCount(funcStr2), 0, "func2 should have 0 arguments"); 100 assertEQ(getFunctionArgumentsCount(funcStr3), 0, "func3 should have 0 arguments"); 101 assertEQ(getFunctionArgumentsCount(funcStr4), 3, "func4 should have 3 arguments"); 102 103 // Test checkIsAsyncFunction 104 assertEQ(checkIsAsyncFunction(funcStr1), false, "func1 should not be async"); 105 assertEQ(checkIsAsyncFunction(funcStr2), true, "func2 should be async"); 106 assertEQ(checkIsAsyncFunction(funcStr3), false, "func3 should not be async"); 107 assertEQ(checkIsAsyncFunction(funcStr4), true, "func4 should be async"); 108 109 // execute getFuncWithArgsZero with async 110 testAsyncGetFuncWithArgsZeroSafe() 111 112 // execute getFuncWithArgsZero with sync 113 testGetFuncWithArgsZeroSafe() 114} 115 116 117