• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 I1 {
17	i(): int { return 0 }
18}
19
20interface I2 {
21	i(x: int): int { return 0 }
22}
23
24class C implements I1, I2 {
25	public i(): int { return 0  }
26	public i(x: int): int { return x }
27}
28
29function getIfaces(): (InterfaceType | undefined)[] {
30	const c = Type.of(new C()) as ClassType
31	let n = c.getInterfacesNum() as int
32	let r = new (InterfaceType | undefined)[n]
33	for (let i = 0; i < n; i++) {
34		r[i] = c.getInterface(i)
35	}
36	return r
37}
38
39function main(): void throws {
40	const ifaces = getIfaces()
41	assertEQ( ifaces.length, 2)
42	const intType = Type.of(0)
43	const I3 = new InterfaceTypeCreator("I3")
44		.addInterface(ifaces[0]!)
45		.addInterface(ifaces[1]!)
46	const CI3 = new ClassTypeCreator("CI3");
47	const i1Body: (self: I1) => int = (self: I1): int => { return 30 }
48	const i2Body: (self: I2, x: int) => int = (self: I2, x: int): int => { return x * x + 3 }
49	CI3
50		.addInterface(I3)
51		.addMethod(
52			new MethodCreator("i")
53				.addResult(intType)
54				.addBody(new CallableBodyFunction(i1Body as Object))
55		)
56		.addMethod(
57			new MethodCreator("i")
58				.addParameter(new ParameterCreator(intType))
59				.addResult(intType)
60				.addBody(new CallableBodyFunction(i2Body as Object))
61		)
62		.addMethod(
63			new MethodCreator("constructor")
64			.addConstructor()
65			.addBody(new CallableBodyDefault())
66		)
67	const ty = CI3.create()
68	const inst = ty.make()
69	assertEQ( (inst as I1).i(), 30)
70	assertEQ( (inst as I2).i(3), 12)
71}
72