Support 'this' in static functions, fixes #45; Fix propagation of 'ambient' flag

This commit is contained in:
dcodeIO
2018-03-20 12:02:05 +01:00
parent fea8e65a41
commit 2c0ddf4f80
12 changed files with 163 additions and 86 deletions

View File

@ -4,9 +4,9 @@ declare const externalConstant: i32;
externalFunction();
assert(externalConstant == 1);
namespace my {
export declare function externalFunction(): void;
export declare const externalConstant: i32;
declare namespace my {
function externalFunction(): void;
const externalConstant: i32;
}
my.externalFunction();

View File

@ -0,0 +1,31 @@
(module
(type $i (func (result i32)))
(type $iiiiv (func (param i32 i32 i32 i32)))
(type $v (func))
(import "env" "abort" (func $abort (param i32 i32 i32 i32)))
(global $static-this/Foo.bar (mut i32) (i32.const 42))
(memory $0 1)
(data (i32.const 4) "\0e\00\00\00s\00t\00a\00t\00i\00c\00-\00t\00h\00i\00s\00.\00t\00s")
(export "memory" (memory $0))
(start $start)
(func $static-this/Foo.getBar (; 1 ;) (type $i) (result i32)
(get_global $static-this/Foo.bar)
)
(func $start (; 2 ;) (type $v)
(if
(i32.ne
(call $static-this/Foo.getBar)
(i32.const 42)
)
(block
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 8)
(i32.const 0)
)
(unreachable)
)
)
)
)

View File

@ -0,0 +1,8 @@
class Foo {
static bar: i32 = 42;
static getBar(): i32 {
return this.bar;
}
}
assert(Foo.getBar() == 42);

View File

@ -0,0 +1,36 @@
(module
(type $i (func (result i32)))
(type $iiiiv (func (param i32 i32 i32 i32)))
(type $v (func))
(import "env" "abort" (func $abort (param i32 i32 i32 i32)))
(global $static-this/Foo.bar (mut i32) (i32.const 42))
(global $HEAP_BASE i32 (i32.const 36))
(memory $0 1)
(data (i32.const 4) "\0e\00\00\00s\00t\00a\00t\00i\00c\00-\00t\00h\00i\00s\00.\00t\00s\00")
(export "memory" (memory $0))
(start $start)
(func $static-this/Foo.getBar (; 1 ;) (type $i) (result i32)
(return
(get_global $static-this/Foo.bar)
)
)
(func $start (; 2 ;) (type $v)
(if
(i32.eqz
(i32.eq
(call $static-this/Foo.getBar)
(i32.const 42)
)
)
(block
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 8)
(i32.const 0)
)
(unreachable)
)
)
)
)

View File

@ -2,10 +2,15 @@ declare namespace A {
namespace B {
export namespace C {
var aVar: i32;
const aConst: i32 = 0;
function aFunc(): void {}
const aConst: i32;
const aConstInvalid: i32 = 0; // 1039: Initializers are not allowed in ambient contexts.
function aFunc(): void;
function aFuncInvalid(): void {} // 1183: An implementation cannot be declared in ambient contexts.
enum AnEnum {}
class AClass {}
}
namespace D {
var aVar: i32;
}
}
}

View File

@ -2,10 +2,17 @@ declare namespace A {
namespace B {
export namespace C {
var aVar: i32;
const aConst: i32 = 0;
function aFunc(): void {}
const aConst: i32;
const aConstInvalid: i32 = 0;
function aFunc(): void;
function aFuncInvalid(): void {}
enum AnEnum {}
class AClass {}
}
namespace D {
var aVar: i32;
}
}
}
// ERROR 1039: "Initializers are not allowed in ambient contexts." in namespace.ts:6:31
// ERROR 1183: "An implementation cannot be declared in ambient contexts." in namespace.ts:8:36