Skip to content

Commit f36c73a

Browse files
committed
use NATIVE<T> macro type
1 parent 78965c3 commit f36c73a

File tree

8 files changed

+581
-552
lines changed

8 files changed

+581
-552
lines changed

src/resolver.ts

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ import {
5151
import {
5252
Type,
5353
Signature,
54-
typesToString
54+
typesToString,
55+
TypeKind
5556
} from "./types";
5657

5758
import {
@@ -154,30 +155,29 @@ export class Resolver extends DiagnosticEmitter {
154155
}
155156

156157
// resolve parameters
157-
{
158-
let typeArgumentNodes = typeNode.typeArguments;
159-
if (typeArgumentNodes) {
160-
let numTypeArguments = typeArgumentNodes.length;
161-
let paramTypes = new Array<Type>(numTypeArguments);
162-
for (let i = 0; i < numTypeArguments; ++i) {
163-
let paramType = this.resolveType( // reports
164-
typeArgumentNodes[i],
165-
contextualTypeArguments,
166-
reportMode
167-
);
168-
if (!paramType) return null;
169-
paramTypes[i] = paramType;
170-
}
171-
if (numTypeArguments) { // can't be a placeholder if it has parameters
172-
let instanceKey = typesToString(paramTypes);
173-
if (instanceKey.length) {
174-
localName += "<" + instanceKey + ">";
175-
globalName += "<" + instanceKey + ">";
176-
}
177-
} else if (contextualTypeArguments) {
178-
let placeholderType = contextualTypeArguments.get(globalName);
179-
if (placeholderType) return placeholderType;
158+
var typeArgumentNodes = typeNode.typeArguments;
159+
var typeArguments: Type[] | null = null;
160+
if (typeArgumentNodes) {
161+
let numTypeArguments = typeArgumentNodes.length;
162+
typeArguments = new Array<Type>(numTypeArguments);
163+
for (let i = 0; i < numTypeArguments; ++i) {
164+
let paramType = this.resolveType( // reports
165+
typeArgumentNodes[i],
166+
contextualTypeArguments,
167+
reportMode
168+
);
169+
if (!paramType) return null;
170+
typeArguments[i] = paramType;
171+
}
172+
if (numTypeArguments) { // can't be a placeholder if it has parameters
173+
let instanceKey = typesToString(typeArguments);
174+
if (instanceKey.length) {
175+
localName += "<" + instanceKey + ">";
176+
globalName += "<" + instanceKey + ">";
180177
}
178+
} else if (contextualTypeArguments) {
179+
let placeholderType = contextualTypeArguments.get(globalName);
180+
if (placeholderType) return placeholderType;
181181
}
182182
}
183183

@@ -193,6 +193,36 @@ export class Resolver extends DiagnosticEmitter {
193193
}
194194
}
195195

196+
// check built-in macro types
197+
if (simpleName == "NATIVE") {
198+
if (!(typeArguments && typeArguments.length == 1)) {
199+
if (reportMode == ReportMode.REPORT) {
200+
this.error(
201+
DiagnosticCode.Expected_0_type_arguments_but_got_1,
202+
typeNode.range, "1", (typeArgumentNodes ? typeArgumentNodes.length : 1).toString(10)
203+
);
204+
}
205+
return null;
206+
}
207+
switch (typeArguments[0].kind) {
208+
case TypeKind.I8:
209+
case TypeKind.I16:
210+
case TypeKind.I32: return Type.i32;
211+
case TypeKind.ISIZE: if (!this.program.options.isWasm64) return Type.i32;
212+
case TypeKind.I64: return Type.i64;
213+
case TypeKind.U8:
214+
case TypeKind.U16:
215+
case TypeKind.U32:
216+
case TypeKind.BOOL: return Type.u32;
217+
case TypeKind.USIZE: if (!this.program.options.isWasm64) return Type.u32;
218+
case TypeKind.U64: return Type.u64;
219+
case TypeKind.F32: return Type.f32;
220+
case TypeKind.F64: return Type.f64;
221+
case TypeKind.VOID: return Type.void;
222+
default: assert(false);
223+
}
224+
}
225+
196226
if (reportMode == ReportMode.REPORT) {
197227
this.error(
198228
DiagnosticCode.Cannot_find_name_0,

std/assembly/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,8 @@ declare namespace f64 {
365365
/** Converts A string to an integer. */
366366
export function parseInt(string: string, radix?: i32): f64;
367367
}
368+
/** Macro type evaluating to the underlying native WebAssembly type. */
369+
declare type NATIVE<T> = T;
368370

369371
// User-defined diagnostic macros
370372

std/assembly/internal/typedarray.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@ import {
1212
defaultComparator
1313
} from "./array";
1414

15-
// The internal TypedArray class uses two type parameters for the same reason as `loadUnsafe` and
16-
// `storeUnsafe` in 'internal/arraybuffer.ts'. See the documentation there for details.
17-
1815
/** Typed array base class. Not a global object. */
19-
export abstract class TypedArray<T,TNative = T> {
16+
export abstract class TypedArray<T> {
2017

2118
readonly buffer: ArrayBuffer;
2219
readonly byteOffset: i32;
@@ -50,19 +47,19 @@ export abstract class TypedArray<T,TNative = T> {
5047
}
5148

5249
@operator("[]=")
53-
protected __set(index: i32, value: TNative): void {
50+
protected __set(index: i32, value: NATIVE<T>): void {
5451
if (<u32>index >= <u32>(this.byteLength >>> alignof<T>())) throw new Error("Index out of bounds");
55-
STORE_OFFSET<T,TNative>(this.buffer, index, value, this.byteOffset);
52+
STORE_OFFSET<T,NATIVE<T>>(this.buffer, index, value, this.byteOffset);
5653
}
5754

5855
@inline @operator("{}=")
59-
protected __unchecked_set(index: i32, value: TNative): void {
60-
STORE_OFFSET<T,TNative>(this.buffer, index, value, this.byteOffset);
56+
protected __unchecked_set(index: i32, value: NATIVE<T>): void {
57+
STORE_OFFSET<T,NATIVE<T>>(this.buffer, index, value, this.byteOffset);
6158
}
6259

6360
// copyWithin(target: i32, start: i32, end: i32 = this.length): this
6461

65-
fill(value: TNative, start: i32 = 0, end: i32 = i32.MAX_VALUE): this /* ! */ {
62+
fill(value: NATIVE<T>, start: i32 = 0, end: i32 = i32.MAX_VALUE): this /* ! */ {
6663
var buffer = this.buffer;
6764
var byteOffset = this.byteOffset;
6865
var len = this.length;
@@ -78,7 +75,7 @@ export abstract class TypedArray<T,TNative = T> {
7875
}
7976
} else {
8077
for (; start < end; ++start) {
81-
STORE_OFFSET<T,TNative>(buffer, start, value, byteOffset);
78+
STORE_OFFSET<T,NATIVE<T>>(buffer, start, value, byteOffset);
8279
}
8380
}
8481
return this;

std/assembly/typedarray.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
REDUCE_RIGHT
66
} from "./internal/typedarray";
77

8-
export class Int8Array extends TypedArray<i8,i32> {
8+
export class Int8Array extends TypedArray<i8> {
99
static readonly BYTES_PER_ELEMENT: usize = sizeof<i8>();
1010

1111
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Int8Array {
@@ -27,7 +27,7 @@ export class Int8Array extends TypedArray<i8,i32> {
2727
}
2828
}
2929

30-
export class Uint8Array extends TypedArray<u8,u32> {
30+
export class Uint8Array extends TypedArray<u8> {
3131
static readonly BYTES_PER_ELEMENT: usize = sizeof<u8>();
3232

3333
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Uint8Array {
@@ -63,7 +63,7 @@ export class Uint8ClampedArray extends Uint8Array {
6363
}
6464
}
6565

66-
export class Int16Array extends TypedArray<i16,i32> {
66+
export class Int16Array extends TypedArray<i16> {
6767
static readonly BYTES_PER_ELEMENT: usize = sizeof<i16>();
6868

6969
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Int16Array {
@@ -85,7 +85,7 @@ export class Int16Array extends TypedArray<i16,i32> {
8585
}
8686
}
8787

88-
export class Uint16Array extends TypedArray<u16,u32> {
88+
export class Uint16Array extends TypedArray<u16> {
8989
static readonly BYTES_PER_ELEMENT: usize = sizeof<u16>();
9090

9191
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Uint16Array {

tests/compiler/std/dataview.optimized.wat

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@
339339
end
340340
end
341341
)
342-
(func $~lib/internal/typedarray/TypedArray<u8,u32>#constructor (; 4 ;) (type $FUNCSIG$i) (result i32)
342+
(func $~lib/internal/typedarray/TypedArray<u8>#constructor (; 4 ;) (type $FUNCSIG$i) (result i32)
343343
(local $0 i32)
344344
(local $1 i32)
345345
i32.const 8
@@ -371,15 +371,15 @@
371371
i32.store offset=8
372372
get_local $0
373373
)
374-
(func $~lib/internal/typedarray/TypedArray<u8,u32>#__set (; 5 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32)
374+
(func $~lib/internal/typedarray/TypedArray<u8>#__set (; 5 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32)
375375
get_local $1
376376
get_local $0
377377
i32.load offset=8
378378
i32.ge_u
379379
if
380380
i32.const 0
381381
i32.const 8
382-
i32.const 54
382+
i32.const 51
383383
i32.const 63
384384
call $~lib/env/abort
385385
unreachable
@@ -1092,40 +1092,40 @@
10921092
set_global $~lib/allocator/arena/startOffset
10931093
get_global $~lib/allocator/arena/startOffset
10941094
set_global $~lib/allocator/arena/offset
1095-
call $~lib/internal/typedarray/TypedArray<u8,u32>#constructor
1095+
call $~lib/internal/typedarray/TypedArray<u8>#constructor
10961096
set_global $std/dataview/array
10971097
get_global $std/dataview/array
10981098
i32.const 0
10991099
i32.const 246
1100-
call $~lib/internal/typedarray/TypedArray<u8,u32>#__set
1100+
call $~lib/internal/typedarray/TypedArray<u8>#__set
11011101
get_global $std/dataview/array
11021102
i32.const 1
11031103
i32.const 224
1104-
call $~lib/internal/typedarray/TypedArray<u8,u32>#__set
1104+
call $~lib/internal/typedarray/TypedArray<u8>#__set
11051105
get_global $std/dataview/array
11061106
i32.const 2
11071107
i32.const 88
1108-
call $~lib/internal/typedarray/TypedArray<u8,u32>#__set
1108+
call $~lib/internal/typedarray/TypedArray<u8>#__set
11091109
get_global $std/dataview/array
11101110
i32.const 3
11111111
i32.const 159
1112-
call $~lib/internal/typedarray/TypedArray<u8,u32>#__set
1112+
call $~lib/internal/typedarray/TypedArray<u8>#__set
11131113
get_global $std/dataview/array
11141114
i32.const 4
11151115
i32.const 130
1116-
call $~lib/internal/typedarray/TypedArray<u8,u32>#__set
1116+
call $~lib/internal/typedarray/TypedArray<u8>#__set
11171117
get_global $std/dataview/array
11181118
i32.const 5
11191119
i32.const 101
1120-
call $~lib/internal/typedarray/TypedArray<u8,u32>#__set
1120+
call $~lib/internal/typedarray/TypedArray<u8>#__set
11211121
get_global $std/dataview/array
11221122
i32.const 6
11231123
i32.const 67
1124-
call $~lib/internal/typedarray/TypedArray<u8,u32>#__set
1124+
call $~lib/internal/typedarray/TypedArray<u8>#__set
11251125
get_global $std/dataview/array
11261126
i32.const 7
11271127
i32.const 95
1128-
call $~lib/internal/typedarray/TypedArray<u8,u32>#__set
1128+
call $~lib/internal/typedarray/TypedArray<u8>#__set
11291129
get_global $std/dataview/array
11301130
i32.load
11311131
get_global $std/dataview/array

tests/compiler/std/dataview.untouched.wat

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@
415415
call $~lib/allocator/arena/__memory_allocate
416416
return
417417
)
418-
(func $~lib/internal/typedarray/TypedArray<u8,u32>#constructor (; 6 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
418+
(func $~lib/internal/typedarray/TypedArray<u8>#constructor (; 6 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
419419
(local $2 i32)
420420
(local $3 i32)
421421
(local $4 i32)
@@ -426,7 +426,7 @@
426426
if
427427
i32.const 0
428428
i32.const 8
429-
i32.const 27
429+
i32.const 24
430430
i32.const 34
431431
call $~lib/env/abort
432432
unreachable
@@ -480,7 +480,7 @@
480480
i32.store offset=8
481481
get_local $0
482482
)
483-
(func $~lib/internal/typedarray/TypedArray<u8,u32>#__set (; 7 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32)
483+
(func $~lib/internal/typedarray/TypedArray<u8>#__set (; 7 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32)
484484
(local $3 i32)
485485
(local $4 i32)
486486
get_local $1
@@ -492,7 +492,7 @@
492492
if
493493
i32.const 0
494494
i32.const 8
495-
i32.const 54
495+
i32.const 51
496496
i32.const 63
497497
call $~lib/env/abort
498498
unreachable
@@ -1703,40 +1703,40 @@
17031703
set_global $~lib/allocator/arena/offset
17041704
i32.const 0
17051705
i32.const 8
1706-
call $~lib/internal/typedarray/TypedArray<u8,u32>#constructor
1706+
call $~lib/internal/typedarray/TypedArray<u8>#constructor
17071707
set_global $std/dataview/array
17081708
get_global $std/dataview/array
17091709
i32.const 0
17101710
i32.const 246
1711-
call $~lib/internal/typedarray/TypedArray<u8,u32>#__set
1711+
call $~lib/internal/typedarray/TypedArray<u8>#__set
17121712
get_global $std/dataview/array
17131713
i32.const 1
17141714
i32.const 224
1715-
call $~lib/internal/typedarray/TypedArray<u8,u32>#__set
1715+
call $~lib/internal/typedarray/TypedArray<u8>#__set
17161716
get_global $std/dataview/array
17171717
i32.const 2
17181718
i32.const 88
1719-
call $~lib/internal/typedarray/TypedArray<u8,u32>#__set
1719+
call $~lib/internal/typedarray/TypedArray<u8>#__set
17201720
get_global $std/dataview/array
17211721
i32.const 3
17221722
i32.const 159
1723-
call $~lib/internal/typedarray/TypedArray<u8,u32>#__set
1723+
call $~lib/internal/typedarray/TypedArray<u8>#__set
17241724
get_global $std/dataview/array
17251725
i32.const 4
17261726
i32.const 130
1727-
call $~lib/internal/typedarray/TypedArray<u8,u32>#__set
1727+
call $~lib/internal/typedarray/TypedArray<u8>#__set
17281728
get_global $std/dataview/array
17291729
i32.const 5
17301730
i32.const 101
1731-
call $~lib/internal/typedarray/TypedArray<u8,u32>#__set
1731+
call $~lib/internal/typedarray/TypedArray<u8>#__set
17321732
get_global $std/dataview/array
17331733
i32.const 6
17341734
i32.const 67
1735-
call $~lib/internal/typedarray/TypedArray<u8,u32>#__set
1735+
call $~lib/internal/typedarray/TypedArray<u8>#__set
17361736
get_global $std/dataview/array
17371737
i32.const 7
17381738
i32.const 95
1739-
call $~lib/internal/typedarray/TypedArray<u8,u32>#__set
1739+
call $~lib/internal/typedarray/TypedArray<u8>#__set
17401740
i32.const 0
17411741
get_global $std/dataview/array
17421742
i32.load

0 commit comments

Comments
 (0)