Skip to content

Commit c9f0ba5

Browse files
committed
more specific
1 parent ccb4478 commit c9f0ba5

File tree

4 files changed

+125
-18
lines changed

4 files changed

+125
-18
lines changed

src/parser.ts

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3266,24 +3266,55 @@ export class Parser extends DiagnosticEmitter {
32663266

32673267
// NewExpression
32683268
if (token == Token.NEW) {
3269-
operand = this.parseExpression(tn, Precedence.GROUPING);
3270-
if (!operand) return null;
3271-
if (operand.kind == NodeKind.CALL) {
3272-
return Node.createNewExpression(
3273-
(<CallExpression>operand).expression,
3274-
(<CallExpression>operand).typeArguments,
3275-
(<CallExpression>operand).arguments,
3276-
tn.range(startPos, tn.pos)
3277-
);
3278-
} else if (operand.kind == NodeKind.IDENTIFIER) { // new Class;
3279-
return Node.createNewExpression(
3280-
operand, null, [],
3281-
tn.range(startPos, tn.pos)
3269+
if (tn.skipIdentifier(IdentifierHandling.DEFAULT)) {
3270+
operand = Node.createIdentifierExpression(
3271+
tn.readIdentifier(),
3272+
tn.range()
32823273
);
3274+
while (tn.skip(Token.DOT)) {
3275+
if (tn.skipIdentifier(IdentifierHandling.PREFER)) {
3276+
operand = Node.createPropertyAccessExpression(
3277+
operand,
3278+
Node.createIdentifierExpression(
3279+
tn.readIdentifier(),
3280+
tn.range()
3281+
),
3282+
tn.range(operand.range.start, tn.pos)
3283+
);
3284+
} else {
3285+
this.error(
3286+
DiagnosticCode.Identifier_expected,
3287+
tn.range()
3288+
);
3289+
return null;
3290+
}
3291+
}
3292+
operand = this.maybeParseCallExpression(tn, operand);
3293+
if (operand.kind == NodeKind.CALL) {
3294+
return Node.createNewExpression(
3295+
(<CallExpression>operand).expression,
3296+
(<CallExpression>operand).typeArguments,
3297+
(<CallExpression>operand).arguments,
3298+
tn.range(startPos, tn.pos)
3299+
);
3300+
} else if (
3301+
operand.kind == NodeKind.IDENTIFIER ||
3302+
operand.kind == NodeKind.PROPERTYACCESS
3303+
) {
3304+
return Node.createNewExpression(
3305+
operand, null, [],
3306+
tn.range(startPos, tn.pos)
3307+
);
3308+
} else {
3309+
this.error(
3310+
DiagnosticCode.This_expression_is_not_constructable,
3311+
operand.range
3312+
);
3313+
}
32833314
} else {
32843315
this.error(
3285-
DiagnosticCode.This_expression_is_not_constructable,
3286-
operand.range
3316+
DiagnosticCode.Identifier_expected,
3317+
tn.range()
32873318
);
32883319
}
32893320
return null;

tests/compiler/new.optimized.wat

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
(global $~lib/rt/stub/startOffset (mut i32) (i32.const 0))
88
(global $~lib/rt/stub/offset (mut i32) (i32.const 0))
99
(global $new/gen (mut i32) (i32.const 0))
10+
(global $new/ref2 (mut i32) (i32.const 0))
1011
(export "memory" (memory $0))
1112
(start $start)
1213
(func $~lib/rt/stub/maybeGrowMemory (; 0 ;) (type $FUNCSIG$vi) (param $0 i32)
@@ -98,6 +99,15 @@
9899
i32.const 4
99100
call $~lib/rt/stub/__alloc
100101
global.set $new/gen
102+
i32.const 5
103+
call $~lib/rt/stub/__alloc
104+
global.set $new/ref2
105+
i32.const 5
106+
call $~lib/rt/stub/__alloc
107+
global.set $new/ref2
108+
i32.const 5
109+
call $~lib/rt/stub/__alloc
110+
global.set $new/ref2
101111
)
102112
(func $start (; 3 ;) (type $FUNCSIG$v)
103113
call $start:new

tests/compiler/new.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,14 @@ class Gen<T> {
1414
var gen: Gen<i32>;
1515
gen = new Gen<i32>();
1616
gen = new Gen<i32>().gen;
17+
18+
namespace ns {
19+
export class Ref {
20+
get ref(): Ref { return this; }
21+
}
22+
}
23+
24+
var ref2: ns.Ref;
25+
ref2 = new ns.Ref();
26+
ref2 = new ns.Ref;
27+
ref2 = new ns.Ref().ref;

tests/compiler/new.untouched.wat

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
(global $~lib/rt/stub/startOffset (mut i32) (i32.const 0))
1111
(global $~lib/rt/stub/offset (mut i32) (i32.const 0))
1212
(global $new/gen (mut i32) (i32.const 0))
13+
(global $new/ref2 (mut i32) (i32.const 0))
1314
(global $~lib/heap/__heap_base i32 (i32.const 8))
1415
(export "memory" (memory $0))
1516
(start $start)
@@ -158,13 +159,31 @@
158159
local.get $0
159160
call $~lib/rt/stub/__retain
160161
)
161-
(func $start:new (; 8 ;) (type $FUNCSIG$v)
162+
(func $new/ns.Ref#constructor (; 8 ;) (type $FUNCSIG$ii) (param $0 i32) (result i32)
163+
local.get $0
164+
i32.eqz
165+
if
166+
i32.const 0
167+
i32.const 5
168+
call $~lib/rt/stub/__alloc
169+
call $~lib/rt/stub/__retain
170+
local.set $0
171+
end
172+
local.get $0
173+
)
174+
(func $new/ns.Ref#get:ref (; 9 ;) (type $FUNCSIG$ii) (param $0 i32) (result i32)
175+
local.get $0
176+
call $~lib/rt/stub/__retain
177+
)
178+
(func $start:new (; 10 ;) (type $FUNCSIG$v)
162179
(local $0 i32)
163180
(local $1 i32)
164181
(local $2 i32)
165182
(local $3 i32)
166183
(local $4 i32)
167184
(local $5 i32)
185+
(local $6 i32)
186+
(local $7 i32)
168187
global.get $~lib/heap/__heap_base
169188
i32.const 15
170189
i32.add
@@ -232,6 +251,38 @@
232251
end
233252
local.get $4
234253
global.set $new/gen
254+
i32.const 0
255+
call $new/ns.Ref#constructor
256+
local.set $5
257+
global.get $new/ref2
258+
call $~lib/rt/stub/__release
259+
local.get $5
260+
global.set $new/ref2
261+
i32.const 0
262+
call $new/ns.Ref#constructor
263+
local.set $4
264+
global.get $new/ref2
265+
call $~lib/rt/stub/__release
266+
local.get $4
267+
global.set $new/ref2
268+
i32.const 0
269+
call $new/ns.Ref#constructor
270+
local.tee $4
271+
call $new/ns.Ref#get:ref
272+
local.tee $5
273+
local.tee $6
274+
global.get $new/ref2
275+
local.tee $7
276+
i32.ne
277+
if
278+
local.get $6
279+
call $~lib/rt/stub/__retain
280+
drop
281+
local.get $7
282+
call $~lib/rt/stub/__release
283+
end
284+
local.get $6
285+
global.set $new/ref2
235286
local.get $0
236287
call $~lib/rt/stub/__release
237288
local.get $1
@@ -240,10 +291,14 @@
240291
call $~lib/rt/stub/__release
241292
local.get $3
242293
call $~lib/rt/stub/__release
294+
local.get $4
295+
call $~lib/rt/stub/__release
296+
local.get $5
297+
call $~lib/rt/stub/__release
243298
)
244-
(func $start (; 9 ;) (type $FUNCSIG$v)
299+
(func $start (; 11 ;) (type $FUNCSIG$v)
245300
call $start:new
246301
)
247-
(func $null (; 10 ;) (type $FUNCSIG$v)
302+
(func $null (; 12 ;) (type $FUNCSIG$v)
248303
)
249304
)

0 commit comments

Comments
 (0)