Skip to content

Commit 83839cd

Browse files
committed
Add test for structurally interface
1 parent 2c4fb36 commit 83839cd

File tree

4 files changed

+149
-57
lines changed

4 files changed

+149
-57
lines changed

src/compiler.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,10 +1327,9 @@ export class Compiler extends DiagnosticEmitter {
13271327
instance.identifierNode.range
13281328
);
13291329
}
1330-
13311330
instance.set(CommonFlags.MODULE_IMPORT);
13321331
mangleImportName(instance, instance.declaration); // TODO: check for duplicates
1333-
1332+
13341333
// create the import
13351334
module.addFunctionImport(
13361335
instance.internalName,
@@ -1464,7 +1463,7 @@ export class Compiler extends DiagnosticEmitter {
14641463
contextualTypeArguments: Map<string,Type> | null = null,
14651464
alternativeReportNode: Node | null = null
14661465
): void {
1467-
// TODO Compile functions to use
1466+
// TODO Compile functions to use
14681467
// this.error(
14691468
// DiagnosticCode.Operation_not_supported,
14701469
// declaration.range
@@ -9191,10 +9190,11 @@ export class Compiler extends DiagnosticEmitter {
91919190
for (let index: i32 = 0; index < iFuncs.length; index++) {
91929191
const [funcID, classes] = iFuncs[index];
91939192
// Compile the interface methods with index
9194-
for (const iFunc of interfaceMethods.get(funcID)!) {
9193+
for (const iFunc of interfaceMethods.get(funcID)!) {
91959194
iFunc.finalize(module, this.compileInterfaceMethod(iFunc, index));
91969195
}
91979196
const innerBlock = relooper.addBlock(module.nop());
9197+
// Add brach for method
91989198
relooper.addBranchForSwitch(first, innerBlock, [index]);
91999199
for (const [classID, func] of classes.entries()) {
92009200
const methodCase = relooper.addBlock(module.return(module.i32(func.functionTableIndex)));
@@ -9211,7 +9211,7 @@ export class Compiler extends DiagnosticEmitter {
92119211
null,
92129212
[relooper.renderAndDispose(first, 0), module.i32(0)],
92139213
Type.u32.toNativeType()
9214-
);;
9214+
);
92159215
const hardCoded = this.module.i32(1);
92169216
this.module.addFunction("~virtual", typeRef, null, body);
92179217

@@ -9243,7 +9243,7 @@ export class Compiler extends DiagnosticEmitter {
92439243
NativeType.I32
92449244
);
92459245
// module.removeFunction(member.internalName);
9246-
9246+
92479247
const callIndirect = module.call_indirect(
92489248
callVirtual,
92499249
func.localsByIndex.map<number>(local =>
@@ -9255,7 +9255,7 @@ export class Compiler extends DiagnosticEmitter {
92559255
func.signature.thisType
92569256
)
92579257
);
9258-
9258+
92599259
const body = module.block(
92609260
null,
92619261
[callIndirect],

tests/compiler/interface.optimized.wat

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
(type $FUNCSIG$viiii (func (param i32 i32 i32 i32)))
55
(type $FUNCSIG$iiii (func (param i32 i32 i32) (result i32)))
66
(type $FUNCSIG$v (func))
7-
(type $FUNCSIG$i (func (result i32)))
7+
(type $FUNCSIG$ii (func (param i32) (result i32)))
88
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
99
(memory $0 1)
1010
(data (i32.const 8) "\18\00\00\00\01\00\00\00\01\00\00\00\18\00\00\00i\00n\00t\00e\00r\00f\00a\00c\00e\00.\00t\00s")
11-
(table $0 3 funcref)
12-
(elem (i32.const 0) $null $interface/AFoo#foo $interface/AFoo#faa)
11+
(table $0 5 funcref)
12+
(elem (i32.const 0) $null $interface/AFoo#foo $interface/AFoo#foo $interface/AFoo#faa $interface/AFoo#faa)
1313
(global $~lib/rt/stub/startOffset (mut i32) (i32.const 0))
1414
(global $~lib/rt/stub/offset (mut i32) (i32.const 0))
1515
(global $interface/aFoo (mut i32) (i32.const 0))
16+
(global $interface/sFoo (mut i32) (i32.const 0))
1617
(export "memory" (memory $0))
1718
(start $start)
1819
(func $~lib/rt/stub/maybeGrowMemory (; 1 ;) (type $FUNCSIG$vi) (param $0 i32)
@@ -57,32 +58,32 @@
5758
local.get $0
5859
global.set $~lib/rt/stub/offset
5960
)
60-
(func $~lib/rt/stub/__alloc (; 2 ;) (type $FUNCSIG$i) (result i32)
61-
(local $0 i32)
61+
(func $~lib/rt/stub/__alloc (; 2 ;) (type $FUNCSIG$ii) (param $0 i32) (result i32)
6262
(local $1 i32)
63+
(local $2 i32)
6364
global.get $~lib/rt/stub/offset
6465
i32.const 16
6566
i32.add
66-
local.tee $1
67+
local.tee $2
6768
i32.const 16
6869
i32.add
6970
call $~lib/rt/stub/maybeGrowMemory
70-
local.get $1
71+
local.get $2
7172
i32.const 16
7273
i32.sub
73-
local.tee $0
74+
local.tee $1
7475
i32.const 16
7576
i32.store
76-
local.get $0
77+
local.get $1
7778
i32.const -1
7879
i32.store offset=4
80+
local.get $1
7981
local.get $0
80-
i32.const 3
8182
i32.store offset=8
82-
local.get $0
83+
local.get $1
8384
i32.const 4
8485
i32.store offset=12
85-
local.get $1
86+
local.get $2
8687
)
8788
(func $interface/passAnInterface (; 3 ;) (type $FUNCSIG$vi) (param $0 i32)
8889
local.get $0
@@ -99,7 +100,7 @@
99100
if
100101
i32.const 0
101102
i32.const 24
102-
i32.const 23
103+
i32.const 37
103104
i32.const 2
104105
call $~lib/builtins/abort
105106
unreachable
@@ -119,7 +120,7 @@
119120
if
120121
i32.const 0
121122
i32.const 24
122-
i32.const 24
123+
i32.const 38
123124
i32.const 2
124125
call $~lib/builtins/abort
125126
unreachable
@@ -131,14 +132,24 @@
131132
global.set $~lib/rt/stub/startOffset
132133
i32.const 48
133134
global.set $~lib/rt/stub/offset
135+
i32.const 3
134136
call $~lib/rt/stub/__alloc
135137
local.tee $0
136138
i32.const 41
137139
i32.store
138140
local.get $0
139141
global.set $interface/aFoo
142+
i32.const 4
143+
call $~lib/rt/stub/__alloc
144+
local.tee $0
145+
i32.const 41
146+
i32.store
147+
local.get $0
148+
global.set $interface/sFoo
140149
global.get $interface/aFoo
141150
call $interface/passAnInterface
151+
global.get $interface/sFoo
152+
call $interface/passAnInterface
142153
)
143154
(func $interface/AFoo#foo (; 5 ;) (type $FUNCSIG$iii) (param $0 i32) (param $1 i32) (result i32)
144155
local.get $0
@@ -153,28 +164,50 @@
153164
)
154165
(func $~virtual (; 7 ;) (type $FUNCSIG$iii) (param $0 i32) (param $1 i32) (result i32)
155166
block $switch$1$default
156-
block $switch$1$case$5
167+
block $switch$1$case$6
157168
block $switch$1$case$3
158169
local.get $0
159-
br_table $switch$1$case$3 $switch$1$case$5 $switch$1$default
170+
br_table $switch$1$case$3 $switch$1$case$6 $switch$1$default
160171
end
161172
local.get $1
162173
i32.const 3
163-
i32.ne
174+
i32.eq
164175
if
176+
i32.const 1
177+
return
178+
else
179+
local.get $1
180+
i32.const 4
181+
i32.eq
182+
if
183+
i32.const 2
184+
return
185+
else
186+
unreachable
187+
end
165188
unreachable
166189
end
167-
i32.const 1
168-
return
190+
unreachable
169191
end
170192
local.get $1
171193
i32.const 3
172-
i32.ne
194+
i32.eq
173195
if
196+
i32.const 3
197+
return
198+
else
199+
local.get $1
200+
i32.const 4
201+
i32.eq
202+
if
203+
i32.const 4
204+
return
205+
else
206+
unreachable
207+
end
174208
unreachable
175209
end
176-
i32.const 2
177-
return
210+
unreachable
178211
end
179212
unreachable
180213
)

tests/compiler/interface.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,26 @@ class AFoo implements IFoo {
1717

1818
}
1919

20-
let aFoo = new AFoo();
20+
class StructurallyImplementsIFoo {
21+
i: i32 = 41;
22+
23+
foo(i: i32): i32 {
24+
return this.i + i;
25+
}
26+
27+
faa(i: i32, i2: i32): i32 {
28+
return i + i2;
29+
}
30+
}
31+
32+
const aFoo = new AFoo();
33+
const sFoo = new StructurallyImplementsIFoo();
34+
2135

2236
function passAnInterface(foo: IFoo): void {
2337
assert(foo.foo(1) == 42);
2438
assert(foo.faa(1,3) == 4);
2539
}
2640

27-
passAnInterface(aFoo);
41+
passAnInterface(aFoo);
42+
passAnInterface(sFoo);

0 commit comments

Comments
 (0)