Skip to content

Commit c7bac75

Browse files
author
Jesse Chen
committed
fix: top level statement compile within namespace
1 parent c22c078 commit c7bac75

File tree

7 files changed

+40
-11
lines changed

7 files changed

+40
-11
lines changed

src/parser.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,7 @@ export class Parser extends DiagnosticEmitter {
398398
tn.range(declareStart, declareEnd), "declare"
399399
); // recoverable
400400
}
401-
if (!namespace) {
402-
statement = this.parseStatement(tn, true);
403-
} // TODO: else?
401+
statement = this.parseStatement(tn, true);
404402
}
405403
break;
406404
}

src/program.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2773,7 +2773,7 @@ export class Program extends DiagnosticEmitter {
27732773
this.initializeVariables(<VariableStatement>member, original);
27742774
break;
27752775
}
2776-
default: assert(false); // namespace member expected
2776+
default: break; // namespace could have statement
27772777
}
27782778
}
27792779
if (original != element) copyMembers(original, element); // keep original parent

tests/compiler/namespace.debug.wat

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
(module
22
(type $none_=>_i32 (func (result i32)))
33
(type $none_=>_none (func))
4+
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
5+
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
6+
(global $namespace/outVariable (mut i32) (i32.const 0))
47
(global $namespace/Outer.outerVar (mut i32) (i32.const 1))
58
(global $namespace/Outer.Inner.aVar (mut i32) (i32.const 0))
69
(global $namespace/Outer.Inner.anotherVar (mut i32) (i32.const 0))
710
(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))
10-
(global $~lib/memory/__data_end i32 (i32.const 8))
11-
(global $~lib/memory/__stack_pointer (mut i32) (i32.const 32776))
12-
(global $~lib/memory/__heap_base i32 (i32.const 32776))
13-
(memory $0 0)
13+
(global $~lib/memory/__data_end i32 (i32.const 60))
14+
(global $~lib/memory/__stack_pointer (mut i32) (i32.const 32828))
15+
(global $~lib/memory/__heap_base i32 (i32.const 32828))
16+
(memory $0 1)
17+
(data $0 (i32.const 12) ",\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00\18\00\00\00n\00a\00m\00e\00s\00p\00a\00c\00e\00.\00t\00s\00\00\00\00\00")
1418
(table $0 1 1 funcref)
1519
(elem $0 (i32.const 1))
1620
(export "memory" (memory $0))
@@ -24,10 +28,24 @@
2428
return
2529
)
2630
(func $start:namespace
31+
i32.const 42
32+
global.set $namespace/outVariable
2733
global.get $namespace/Outer.Inner.aVar
2834
global.set $namespace/Outer.Inner.anotherVar
2935
global.get $namespace/Outer.outerVar
3036
global.set $namespace/Outer.Inner.evenAnotherVar
37+
global.get $namespace/outVariable
38+
i32.const 42
39+
i32.eq
40+
i32.eqz
41+
if
42+
i32.const 0
43+
i32.const 32
44+
i32.const 16
45+
i32.const 1
46+
call $~lib/builtins/abort
47+
unreachable
48+
end
3149
global.get $namespace/Outer.Inner.aVar
3250
drop
3351
call $namespace/Outer.Inner.aFunc

tests/compiler/namespace.release.wat

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
(module
2-
(memory $0 0)
2+
(memory $0 1)
3+
(data $0 (i32.const 1036) ",")
4+
(data $0.1 (i32.const 1048) "\02\00\00\00\18\00\00\00n\00a\00m\00e\00s\00p\00a\00c\00e\00.\00t\00s")
35
(export "memory" (memory $0))
46
)

tests/compiler/namespace.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
let outVariable: i32 = 0;
12
namespace Outer {
3+
outVariable = 42;
24
export var outerVar: i32 = 1;
35
export namespace Inner {
46
export var aVar: i32 = 0;
@@ -7,9 +9,11 @@ namespace Outer {
79
export function aFunc(): i32 { return aVar; }
810
export enum anEnum { ONE = 1, TWO = 2 }
911
export const enum aConstEnum { ONE = 1, TWO = 2 }
12+
export function assertOutVariable(): void { assert(outVariable == 42); }
1013
}
1114
}
1215

16+
assert(outVariable == 42);
1317
Outer.Inner.aVar;
1418
Outer.Inner.aFunc();
1519
Outer.Inner.anEnum.ONE;

tests/parser/namespace.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
let outsideVariable:i32 = 0;
12
declare namespace A {
3+
outsideVariable = 42;
24
namespace B {
35
export namespace C {
46
var aVar: i32;
@@ -13,4 +15,5 @@ declare namespace A {
1315
var aVar: i32;
1416
}
1517
}
18+
return; // // ERROR 1108: A 'return' statement can only be used within a function body.
1619
}

tests/parser/namespace.ts.fixture.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
let outsideVariable: i32 = 0;
12
declare namespace A {
3+
outsideVariable = 42;
24
namespace B {
35
export namespace C {
46
var aVar: i32;
@@ -13,6 +15,8 @@ declare namespace A {
1315
var aVar: i32;
1416
}
1517
}
18+
return;
1619
}
17-
// ERROR 1039: "Initializers are not allowed in ambient contexts." in namespace.ts(6,32+1)
18-
// ERROR 1183: "An implementation cannot be declared in ambient contexts." in namespace.ts(8,37+1)
20+
// ERROR 1039: "Initializers are not allowed in ambient contexts." in namespace.ts(8,32+1)
21+
// ERROR 1183: "An implementation cannot be declared in ambient contexts." in namespace.ts(10,37+1)
22+
// ERROR 1108: "A 'return' statement can only be used within a function body." in namespace.ts(18,3+6)

0 commit comments

Comments
 (0)