1/* 2 * Copyright (c) 2021-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 16interface I { 17 i(x: int): int { return 0 } 18} 19 20function foo(self: Object, x: int): int { 21 assertEQ( (Type.of(self) as ClassType).getName(), "i32") 22 return x + 11 23} 24 25const foo1: (self: Object, x: int) => int = foo; 26 27class C implements I {} 28 29function main(): void throws { 30 const CType = Type.of(new C()) as ClassType 31 const i32 = Type.of(0) 32 const creator = new ClassTypeCreator("i32").addInterface(CType.getInterface(0)) 33 creator 34 .addMethod( 35 new MethodCreator("i") 36 .addParameter(new ParameterCreator(i32)) 37 .addResult(i32) 38 .addBody(new CallableBodyFunction(foo1 as Object)) 39 ) 40 .addMethod( 41 new MethodCreator("constructor") 42 .addConstructor() 43 .addBody(new CallableBodyDefault()) 44 ) 45 const ty = creator.create() 46 const i = ty.make() as I 47 assertEQ(i.i(19), 30) 48} 49