Skip to content

Properly resolve namespace siblings #920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@ export class Compiler extends DiagnosticEmitter {
currentFlow: Flow;
/** Current inline functions stack. */
currentInlineFunctions: Function[] = [];
/** Current enum in compilation. */
currentEnum: Enum | null = null;
/** Current parent element if not a function, i.e. an enum or namespace. */
currentParent: Element | null = null;
/** Current type in compilation. */
currentType: Type = Type.void;
/** Start function statements. */
Expand Down Expand Up @@ -987,7 +987,8 @@ export class Compiler extends DiagnosticEmitter {
element.set(CommonFlags.COMPILED);

var module = this.module;
this.currentEnum = element;
var previousParent = this.currentParent;
this.currentParent = element;
var previousValue: EnumValue | null = null;
var previousValueIsMut = false;
var isInline = element.is(CommonFlags.CONST) || element.hasDecorator(DecoratorFlags.INLINE);
Expand Down Expand Up @@ -1066,7 +1067,7 @@ export class Compiler extends DiagnosticEmitter {
previousValue = <EnumValue>val;
}
}
this.currentEnum = null;
this.currentParent = previousParent;
return true;
}

Expand Down Expand Up @@ -1622,9 +1623,6 @@ export class Compiler extends DiagnosticEmitter {
// === Statements ===============================================================================

compileTopLevelStatement(statement: Statement, body: ExpressionRef[]): void {
if (statement.kind == NodeKind.EXPORTDEFAULT) {
statement = (<ExportDefaultStatement>statement).declaration;
}
switch (statement.kind) {
case NodeKind.CLASSDECLARATION: {
let memberStatements = (<ClassDeclaration>statement).members;
Expand All @@ -1642,9 +1640,16 @@ export class Compiler extends DiagnosticEmitter {
break;
}
case NodeKind.NAMESPACEDECLARATION: {
let memberStatements = (<NamespaceDeclaration>statement).members;
for (let i = 0, k = memberStatements.length; i < k; ++i) {
this.compileTopLevelStatement(memberStatements[i], body);
let element = this.program.getElementByDeclaration(<NamespaceDeclaration>statement);
if (element) {
// any potentiall merged element
let previousParent = this.currentParent;
this.currentParent = element;
let memberStatements = (<NamespaceDeclaration>statement).members;
for (let i = 0, k = memberStatements.length; i < k; ++i) {
this.compileTopLevelStatement(memberStatements[i], body);
}
this.currentParent = previousParent;
}
break;
}
Expand Down Expand Up @@ -1678,6 +1683,10 @@ export class Compiler extends DiagnosticEmitter {
}
break;
}
case NodeKind.EXPORTDEFAULT: {
this.compileTopLevelStatement((<ExportDefaultStatement>statement).declaration, body);
break;
}
case NodeKind.IMPORT: {
this.compileFileByPath(
(<ImportStatement>statement).internalPath,
Expand Down Expand Up @@ -7209,7 +7218,7 @@ export class Compiler extends DiagnosticEmitter {
var target = this.resolver.lookupIdentifierExpression( // reports
expression,
flow,
this.currentEnum || actualFunction
this.currentParent || actualFunction
);
if (!target) return module.unreachable();

Expand Down
4 changes: 2 additions & 2 deletions tests/compiler/features/simd.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
if
i32.const 0
i32.const 24
i32.const 71
i32.const 70
i32.const 2
call $~lib/builtins/abort
unreachable
Expand All @@ -108,7 +108,7 @@
if
i32.const 0
i32.const 24
i32.const 73
i32.const 72
i32.const 13
call $~lib/builtins/abort
unreachable
Expand Down
4 changes: 2 additions & 2 deletions tests/compiler/features/simd.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
if
i32.const 0
i32.const 24
i32.const 71
i32.const 70
i32.const 2
call $~lib/builtins/abort
unreachable
Expand All @@ -161,7 +161,7 @@
if
i32.const 0
i32.const 24
i32.const 73
i32.const 72
i32.const 13
call $~lib/builtins/abort
unreachable
Expand Down
9 changes: 9 additions & 0 deletions tests/compiler/namespace.optimized.wat
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
(module
(type $FUNCSIG$v (func))
(memory $0 0)
(global $namespace/Outer.Inner.anotherVar (mut i32) (i32.const 0))
(global $namespace/Outer.Inner.evenAnotherVar (mut i32) (i32.const 0))
(export "memory" (memory $0))
(start $start)
(func $start (; 0 ;) (type $FUNCSIG$v)
i32.const 0
global.set $namespace/Outer.Inner.anotherVar
i32.const 1
global.set $namespace/Outer.Inner.evenAnotherVar
)
(func $null (; 1 ;) (type $FUNCSIG$v)
nop
)
)
3 changes: 3 additions & 0 deletions tests/compiler/namespace.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
namespace Outer {
export var outerVar: i32 = 1;
export namespace Inner {
export var aVar: i32 = 0;
export var anotherVar: i32 = aVar;
export var evenAnotherVar: i32 = outerVar;
export function aFunc(): i32 { return aVar; }
export enum anEnum { ONE = 1, TWO = 2 }
export const enum aConstEnum { ONE = 1, TWO = 2 }
Expand Down
7 changes: 7 additions & 0 deletions tests/compiler/namespace.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
(memory $0 0)
(table $0 1 funcref)
(elem (i32.const 0) $null)
(global $namespace/Outer.outerVar (mut i32) (i32.const 1))
(global $namespace/Outer.Inner.aVar (mut i32) (i32.const 0))
(global $namespace/Outer.Inner.anotherVar (mut i32) (i32.const 0))
(global $namespace/Outer.Inner.evenAnotherVar (mut i32) (i32.const 0))
(global $namespace/Outer.Inner.anEnum.ONE i32 (i32.const 1))
(global $namespace/Outer.Inner.anEnum.TWO i32 (i32.const 2))
(export "memory" (memory $0))
Expand All @@ -16,6 +19,10 @@
i32.const 3
)
(func $start:namespace (; 2 ;) (type $FUNCSIG$v)
global.get $namespace/Outer.Inner.aVar
global.set $namespace/Outer.Inner.anotherVar
global.get $namespace/Outer.outerVar
global.set $namespace/Outer.Inner.evenAnotherVar
global.get $namespace/Outer.Inner.aVar
drop
call $namespace/Outer.Inner.aFunc
Expand Down