Skip to content

Commit da65778

Browse files
authored
Properly resolve namespace siblings (#920)
1 parent 62e7952 commit da65778

File tree

6 files changed

+43
-15
lines changed

6 files changed

+43
-15
lines changed

src/compiler.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,8 @@ export class Compiler extends DiagnosticEmitter {
285285
currentFlow: Flow;
286286
/** Current inline functions stack. */
287287
currentInlineFunctions: Function[] = [];
288-
/** Current enum in compilation. */
289-
currentEnum: Enum | null = null;
288+
/** Current parent element if not a function, i.e. an enum or namespace. */
289+
currentParent: Element | null = null;
290290
/** Current type in compilation. */
291291
currentType: Type = Type.void;
292292
/** Start function statements. */
@@ -987,7 +987,8 @@ export class Compiler extends DiagnosticEmitter {
987987
element.set(CommonFlags.COMPILED);
988988

989989
var module = this.module;
990-
this.currentEnum = element;
990+
var previousParent = this.currentParent;
991+
this.currentParent = element;
991992
var previousValue: EnumValue | null = null;
992993
var previousValueIsMut = false;
993994
var isInline = element.is(CommonFlags.CONST) || element.hasDecorator(DecoratorFlags.INLINE);
@@ -1066,7 +1067,7 @@ export class Compiler extends DiagnosticEmitter {
10661067
previousValue = <EnumValue>val;
10671068
}
10681069
}
1069-
this.currentEnum = null;
1070+
this.currentParent = previousParent;
10701071
return true;
10711072
}
10721073

@@ -1622,9 +1623,6 @@ export class Compiler extends DiagnosticEmitter {
16221623
// === Statements ===============================================================================
16231624

16241625
compileTopLevelStatement(statement: Statement, body: ExpressionRef[]): void {
1625-
if (statement.kind == NodeKind.EXPORTDEFAULT) {
1626-
statement = (<ExportDefaultStatement>statement).declaration;
1627-
}
16281626
switch (statement.kind) {
16291627
case NodeKind.CLASSDECLARATION: {
16301628
let memberStatements = (<ClassDeclaration>statement).members;
@@ -1642,9 +1640,16 @@ export class Compiler extends DiagnosticEmitter {
16421640
break;
16431641
}
16441642
case NodeKind.NAMESPACEDECLARATION: {
1645-
let memberStatements = (<NamespaceDeclaration>statement).members;
1646-
for (let i = 0, k = memberStatements.length; i < k; ++i) {
1647-
this.compileTopLevelStatement(memberStatements[i], body);
1643+
let element = this.program.getElementByDeclaration(<NamespaceDeclaration>statement);
1644+
if (element) {
1645+
// any potentiall merged element
1646+
let previousParent = this.currentParent;
1647+
this.currentParent = element;
1648+
let memberStatements = (<NamespaceDeclaration>statement).members;
1649+
for (let i = 0, k = memberStatements.length; i < k; ++i) {
1650+
this.compileTopLevelStatement(memberStatements[i], body);
1651+
}
1652+
this.currentParent = previousParent;
16481653
}
16491654
break;
16501655
}
@@ -1678,6 +1683,10 @@ export class Compiler extends DiagnosticEmitter {
16781683
}
16791684
break;
16801685
}
1686+
case NodeKind.EXPORTDEFAULT: {
1687+
this.compileTopLevelStatement((<ExportDefaultStatement>statement).declaration, body);
1688+
break;
1689+
}
16811690
case NodeKind.IMPORT: {
16821691
this.compileFileByPath(
16831692
(<ImportStatement>statement).internalPath,
@@ -7209,7 +7218,7 @@ export class Compiler extends DiagnosticEmitter {
72097218
var target = this.resolver.lookupIdentifierExpression( // reports
72107219
expression,
72117220
flow,
7212-
this.currentEnum || actualFunction
7221+
this.currentParent || actualFunction
72137222
);
72147223
if (!target) return module.unreachable();
72157224

tests/compiler/features/simd.optimized.wat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
if
9494
i32.const 0
9595
i32.const 24
96-
i32.const 71
96+
i32.const 70
9797
i32.const 2
9898
call $~lib/builtins/abort
9999
unreachable
@@ -108,7 +108,7 @@
108108
if
109109
i32.const 0
110110
i32.const 24
111-
i32.const 73
111+
i32.const 72
112112
i32.const 13
113113
call $~lib/builtins/abort
114114
unreachable

tests/compiler/features/simd.untouched.wat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@
144144
if
145145
i32.const 0
146146
i32.const 24
147-
i32.const 71
147+
i32.const 70
148148
i32.const 2
149149
call $~lib/builtins/abort
150150
unreachable
@@ -161,7 +161,7 @@
161161
if
162162
i32.const 0
163163
i32.const 24
164-
i32.const 73
164+
i32.const 72
165165
i32.const 13
166166
call $~lib/builtins/abort
167167
unreachable
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
(module
22
(type $FUNCSIG$v (func))
33
(memory $0 0)
4+
(global $namespace/Outer.Inner.anotherVar (mut i32) (i32.const 0))
5+
(global $namespace/Outer.Inner.evenAnotherVar (mut i32) (i32.const 0))
46
(export "memory" (memory $0))
7+
(start $start)
58
(func $start (; 0 ;) (type $FUNCSIG$v)
9+
i32.const 0
10+
global.set $namespace/Outer.Inner.anotherVar
11+
i32.const 1
12+
global.set $namespace/Outer.Inner.evenAnotherVar
13+
)
14+
(func $null (; 1 ;) (type $FUNCSIG$v)
615
nop
716
)
817
)

tests/compiler/namespace.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
namespace Outer {
2+
export var outerVar: i32 = 1;
23
export namespace Inner {
34
export var aVar: i32 = 0;
5+
export var anotherVar: i32 = aVar;
6+
export var evenAnotherVar: i32 = outerVar;
47
export function aFunc(): i32 { return aVar; }
58
export enum anEnum { ONE = 1, TWO = 2 }
69
export const enum aConstEnum { ONE = 1, TWO = 2 }

tests/compiler/namespace.untouched.wat

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
(memory $0 0)
55
(table $0 1 funcref)
66
(elem (i32.const 0) $null)
7+
(global $namespace/Outer.outerVar (mut i32) (i32.const 1))
78
(global $namespace/Outer.Inner.aVar (mut i32) (i32.const 0))
9+
(global $namespace/Outer.Inner.anotherVar (mut i32) (i32.const 0))
10+
(global $namespace/Outer.Inner.evenAnotherVar (mut i32) (i32.const 0))
811
(global $namespace/Outer.Inner.anEnum.ONE i32 (i32.const 1))
912
(global $namespace/Outer.Inner.anEnum.TWO i32 (i32.const 2))
1013
(export "memory" (memory $0))
@@ -16,6 +19,10 @@
1619
i32.const 3
1720
)
1821
(func $start:namespace (; 2 ;) (type $FUNCSIG$v)
22+
global.get $namespace/Outer.Inner.aVar
23+
global.set $namespace/Outer.Inner.anotherVar
24+
global.get $namespace/Outer.outerVar
25+
global.set $namespace/Outer.Inner.evenAnotherVar
1926
global.get $namespace/Outer.Inner.aVar
2027
drop
2128
call $namespace/Outer.Inner.aFunc

0 commit comments

Comments
 (0)