Make 'instanceof' behave like TS if the lhs is nullable

This commit is contained in:
dcodeIO
2018-06-09 02:01:45 +02:00
parent 7478c8a0d3
commit 47f2e0950a
8 changed files with 220 additions and 54 deletions

View File

@ -4,6 +4,7 @@
(type $Fi (func (param f64) (result i32)))
(type $v (func))
(import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32)))
(global $instanceof/an (mut i32) (i32.const 0))
(memory $0 1)
(data (i32.const 8) "\0d\00\00\00i\00n\00s\00t\00a\00n\00c\00e\00o\00f\00.\00t\00s")
(export "memory" (memory $0))
@ -14,7 +15,10 @@
(func $instanceof/isI32<f64> (; 2 ;) (type $Fi) (param $0 f64) (result i32)
(i32.const 0)
)
(func $start (; 3 ;) (type $v)
(func $instanceof/isI32<u32> (; 3 ;) (type $ii) (param $0 i32) (result i32)
(i32.const 0)
)
(func $start (; 4 ;) (type $v)
(if
(i32.eqz
(call $instanceof/isI32<i32>
@ -45,5 +49,48 @@
(unreachable)
)
)
(if
(call $instanceof/isI32<u32>
(i32.const 0)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 40)
(i32.const 0)
)
(unreachable)
)
)
(if
(get_global $instanceof/an)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 43)
(i32.const 0)
)
(unreachable)
)
)
(set_global $instanceof/an
(i32.const 1)
)
(if
(i32.eqz
(get_global $instanceof/an)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 46)
(i32.const 0)
)
(unreachable)
)
)
)
)

View File

@ -37,13 +37,14 @@ function isI32<T>(v: T): bool {
assert( isI32(0));
assert(!isI32(0.0));
assert(!isI32(<u32>0)); // signedness is relevant
// TODO: what about nullables?
// var an: A | null;
// var bn: B | null;
//
// assert(an instanceof A);
// assert(bn instanceof A);
//
// assert(!(an instanceof B));
// assert(bn instanceof B);
var an: A | null = null;
assert(!(an instanceof A)); // TS: null is not an instance of A
assert( an instanceof A | null); // AS: null is an instance of A | null
an = changetype<A | null>(1);
assert( an instanceof A); // TS: non-null is an instance of A
assert( an instanceof A | null); // AS: non-null is an instance of A | null
// TODO: keep track of nullability during flows, so this becomes precomputable:
// assert(an !== null && an instanceof A);

View File

@ -8,6 +8,7 @@
(global $instanceof/b (mut i32) (i32.const 0))
(global $instanceof/i (mut i32) (i32.const 0))
(global $instanceof/f (mut f32) (f32.const 0))
(global $instanceof/an (mut i32) (i32.const 0))
(global $HEAP_BASE i32 (i32.const 40))
(memory $0 1)
(data (i32.const 8) "\0d\00\00\00i\00n\00s\00t\00a\00n\00c\00e\00o\00f\00.\00t\00s\00")
@ -23,7 +24,12 @@
(i32.const 0)
)
)
(func $start (; 3 ;) (type $v)
(func $instanceof/isI32<u32> (; 3 ;) (type $ii) (param $0 i32) (result i32)
(return
(i32.const 0)
)
)
(func $start (; 4 ;) (type $v)
(if
(i32.eqz
(i32.const 1)
@ -304,5 +310,90 @@
(unreachable)
)
)
(if
(i32.eqz
(i32.eqz
(call $instanceof/isI32<u32>
(i32.const 0)
)
)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 40)
(i32.const 0)
)
(unreachable)
)
)
(if
(i32.eqz
(i32.eqz
(i32.ne
(get_global $instanceof/an)
(i32.const 0)
)
)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 43)
(i32.const 0)
)
(unreachable)
)
)
(if
(i32.eqz
(i32.const 1)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 44)
(i32.const 0)
)
(unreachable)
)
)
(set_global $instanceof/an
(i32.const 1)
)
(if
(i32.eqz
(i32.ne
(get_global $instanceof/an)
(i32.const 0)
)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 46)
(i32.const 0)
)
(unreachable)
)
)
(if
(i32.eqz
(i32.const 1)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 47)
(i32.const 0)
)
(unreachable)
)
)
)
)