Skip to content

Commit

Permalink
fix: Substitute type arguments in 'new' only if prototypes match (#1660)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Feb 3, 2021
1 parent cc37cc4 commit 5981995
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 52 deletions.
2 changes: 2 additions & 0 deletions src/compiler.ts
Expand Up @@ -8442,8 +8442,10 @@ export class Compiler extends DiagnosticEmitter {
if (
!typeArguments &&
(classReference = contextualType.classReference) !== null &&
classReference.prototype == classPrototype &&
classReference.is(CommonFlags.GENERIC)
) {
// e.g. `arr: Array<T> = new Array()`
classInstance = this.resolver.resolveClass(
classPrototype,
classReference.typeArguments,
Expand Down
135 changes: 101 additions & 34 deletions tests/compiler/new.optimized.wat
@@ -1,8 +1,8 @@
(module
(type $i32_=>_i32 (func (param i32) (result i32)))
(type $none_=>_none (func))
(type $i32_i32_=>_none (func (param i32 i32)))
(type $none_=>_i32 (func (result i32)))
(type $i32_=>_i32 (func (param i32) (result i32)))
(type $i32_=>_none (func (param i32)))
(type $i32_i32_i32_=>_none (func (param i32 i32 i32)))
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
Expand All @@ -18,8 +18,8 @@
(data (i32.const 1304) "\01\00\00\00\14\00\00\00~\00l\00i\00b\00/\00r\00t\00.\00t\00s")
(data (i32.const 1372) "<")
(data (i32.const 1384) "\01\00\00\00\1e\00\00\00~\00l\00i\00b\00/\00r\00t\00/\00t\00l\00s\00f\00.\00t\00s")
(data (i32.const 1440) "\06\00\00\00 \00\00\00\00\00\00\00 ")
(data (i32.const 1468) " \00\00\00\00\00\00\00 \00\00\00\00\00\00\00 ")
(data (i32.const 1440) "\07\00\00\00 \00\00\00\00\00\00\00 ")
(data (i32.const 1468) " \00\00\00\00\00\00\00 \00\00\00\00\00\00\00 \00\00\00\00\00\00\00 \00\00\00\04")
(global $new/ref (mut i32) (i32.const 0))
(global $~lib/rt/itcms/total (mut i32) (i32.const 0))
(global $~lib/rt/itcms/threshold (mut i32) (i32.const 1024))
Expand All @@ -33,7 +33,10 @@
(global $~lib/rt/tlsf/ROOT (mut i32) (i32.const 0))
(global $new/gen (mut i32) (i32.const 0))
(global $new/ref2 (mut i32) (i32.const 0))
(global $~lib/memory/__stack_pointer (mut i32) (i32.const 17876))
(global $new/genext (mut i32) (i32.const 0))
(global $new/genext2 (mut i32) (i32.const 0))
(global $new/genext3 (mut i32) (i32.const 0))
(global $~lib/memory/__stack_pointer (mut i32) (i32.const 17884))
(export "memory" (memory $0))
(start $~start)
(func $~lib/rt/itcms/initLazy (param $0 i32) (result i32)
Expand Down Expand Up @@ -66,6 +69,24 @@
local.get $0
call $~lib/rt/itcms/__visit
end
global.get $new/genext
local.tee $0
if
local.get $0
call $~lib/rt/itcms/__visit
end
global.get $new/genext2
local.tee $0
if
local.get $0
call $~lib/rt/itcms/__visit
end
global.get $new/genext3
local.tee $0
if
local.get $0
call $~lib/rt/itcms/__visit
end
i32.const 1248
call $~lib/rt/itcms/__visit
i32.const 1056
Expand Down Expand Up @@ -180,7 +201,7 @@
if
i32.const 0
local.get $0
i32.const 17876
i32.const 17884
i32.lt_u
local.get $0
i32.load offset=8
Expand Down Expand Up @@ -980,7 +1001,7 @@
local.set $0
loop $while-continue|0
local.get $0
i32.const 17876
i32.const 17884
i32.lt_u
if
local.get $0
Expand Down Expand Up @@ -1070,7 +1091,7 @@
unreachable
end
local.get $0
i32.const 17876
i32.const 17884
i32.lt_u
if
local.get $0
Expand All @@ -1093,7 +1114,7 @@
i32.const 4
i32.add
local.tee $1
i32.const 17876
i32.const 17884
i32.ge_u
if
global.get $~lib/rt/tlsf/ROOT
Expand Down Expand Up @@ -1435,28 +1456,31 @@
)
(func $~lib/rt/__visit_members (param $0 i32)
block $invalid
block $new/ns.Ref
block $new/Gen<i32>
block $new/Ref
block $~lib/arraybuffer/ArrayBufferView
block $~lib/string/String
block $~lib/arraybuffer/ArrayBuffer
local.get $0
i32.const 8
i32.sub
i32.load
br_table $~lib/arraybuffer/ArrayBuffer $~lib/string/String $~lib/arraybuffer/ArrayBufferView $new/Ref $new/Gen<i32> $new/ns.Ref $invalid
block $new/GenExt
block $new/ns.Ref
block $new/Gen<i32>
block $new/Ref
block $~lib/arraybuffer/ArrayBufferView
block $~lib/string/String
block $~lib/arraybuffer/ArrayBuffer
local.get $0
i32.const 8
i32.sub
i32.load
br_table $~lib/arraybuffer/ArrayBuffer $~lib/string/String $~lib/arraybuffer/ArrayBufferView $new/Ref $new/Gen<i32> $new/ns.Ref $new/GenExt $invalid
end
return
end
return
end
return
end
local.get $0
i32.load
local.tee $0
if
local.get $0
call $~lib/rt/itcms/__visit
i32.load
local.tee $0
if
local.get $0
call $~lib/rt/itcms/__visit
end
return
end
return
end
Expand Down Expand Up @@ -1498,8 +1522,13 @@
i32.store
local.get $0
global.set $new/ref
i32.const 0
call $new/Gen<i32>#constructor
global.set $new/gen
i32.const 0
call $new/Gen<i32>#constructor
global.set $new/gen
i32.const 0
call $new/Gen<i32>#constructor
local.set $0
global.get $~lib/memory/__stack_pointer
Expand All @@ -1518,14 +1547,20 @@
i32.store
local.get $0
global.set $new/ref2
call $new/GenExt#constructor
global.set $new/genext
call $new/GenExt#constructor
global.set $new/genext2
call $new/GenExt#constructor
global.set $new/genext3
global.get $~lib/memory/__stack_pointer
i32.const 4
i32.add
global.set $~lib/memory/__stack_pointer
)
(func $~stack_check
global.get $~lib/memory/__stack_pointer
i32.const 1492
i32.const 1500
i32.lt_s
if
i32.const 17904
Expand All @@ -1536,6 +1571,35 @@
unreachable
end
)
(func $new/GenExt#constructor (result i32)
(local $0 i32)
global.get $~lib/memory/__stack_pointer
i32.const 8
i32.sub
global.set $~lib/memory/__stack_pointer
call $~stack_check
global.get $~lib/memory/__stack_pointer
i64.const 0
i64.store
global.get $~lib/memory/__stack_pointer
i32.const 6
call $~lib/rt/itcms/__new
local.tee $0
i32.store
global.get $~lib/memory/__stack_pointer
local.get $0
i32.store offset=4
global.get $~lib/memory/__stack_pointer
local.get $0
call $new/Gen<i32>#constructor
local.tee $0
i32.store
global.get $~lib/memory/__stack_pointer
i32.const 8
i32.add
global.set $~lib/memory/__stack_pointer
local.get $0
)
(func $new/Ref#constructor (result i32)
(local $0 i32)
global.get $~lib/memory/__stack_pointer
Expand All @@ -1557,8 +1621,7 @@
global.set $~lib/memory/__stack_pointer
local.get $0
)
(func $new/Gen<i32>#constructor (result i32)
(local $0 i32)
(func $new/Gen<i32>#constructor (param $0 i32) (result i32)
global.get $~lib/memory/__stack_pointer
i32.const 4
i32.sub
Expand All @@ -1567,11 +1630,15 @@
global.get $~lib/memory/__stack_pointer
i32.const 0
i32.store
global.get $~lib/memory/__stack_pointer
i32.const 4
call $~lib/rt/itcms/__new
local.tee $0
i32.store
local.get $0
i32.eqz
if
global.get $~lib/memory/__stack_pointer
i32.const 4
call $~lib/rt/itcms/__new
local.tee $0
i32.store
end
global.get $~lib/memory/__stack_pointer
i32.const 4
i32.add
Expand Down
8 changes: 8 additions & 0 deletions tests/compiler/new.ts
Expand Up @@ -12,6 +12,7 @@ class Gen<T> {
}

var gen: Gen<i32>;
gen = new Gen();
gen = new Gen<i32>();
gen = new Gen<i32>().gen;

Expand All @@ -25,3 +26,10 @@ var ref2: ns.Ref;
ref2 = new ns.Ref();
ref2 = new ns.Ref;
ref2 = new ns.Ref().ref;

class GenExt extends Gen<i32> {
}

var genext = new GenExt();
var genext2: GenExt = new GenExt();
var genext3: Gen<i32> = new GenExt();

0 comments on commit 5981995

Please sign in to comment.