Skip to content

Commit da9c32c

Browse files
committed
Tackle the case of ===
1 parent b942d61 commit da9c32c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1053
-1297
lines changed

src/compiler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3935,8 +3935,8 @@ export class Compiler extends DiagnosticEmitter {
39353935
leftExpr = this.compileExpression(left, contextualType);
39363936
leftType = this.currentType;
39373937

3938-
// check operator overload
3939-
if (operator == Token.EQUALS_EQUALS && this.currentType.is(TypeFlags.REFERENCE)) {
3938+
// check operator overload
3939+
if (this.currentType.is(TypeFlags.REFERENCE)) {
39403940
let classReference = leftType.classReference;
39413941
if (classReference) {
39423942
let overload = classReference.lookupOverload(OperatorKind.EQ);

std/assembly/array.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function ensureSize(array: usize, minSize: usize, alignLog2: u32): void {
1616
let newCapacity = minSize << alignLog2;
1717
let newData = __realloc(oldData, newCapacity); // keeps RC
1818
memory.fill(newData + oldCapacity, 0, newCapacity - oldCapacity);
19-
if (newData !== oldData) { // oldData has been free'd
19+
if (changetype<usize>(newData) != changetype<usize>(oldData)) { // oldData has been free'd
2020
store<usize>(array, newData, offsetof<ArrayBufferView>("buffer"));
2121
store<usize>(array, newData, offsetof<ArrayBufferView>("dataStart"));
2222
}
@@ -38,7 +38,10 @@ export class Array<T> extends ArrayBufferView {
3838
private length_: i32;
3939

4040
static isArray<U>(value: U): bool {
41-
return isReference<U>() ? builtin_isArray(value) && value !== null : false;
41+
if (isReference<U>()) {
42+
return value instanceof Array && changetype<usize>(value) != 0;
43+
}
44+
return false;
4245
}
4346

4447
static create<T>(capacity: i32 = 0): Array<T> {
@@ -213,7 +216,7 @@ export class Array<T> extends ArrayBufferView {
213216

214217
concat(other: Array<T>): Array<T> {
215218
var thisLen = this.length_;
216-
var otherLen = select(0, other.length_, other === null);
219+
var otherLen = select(0, other.length_, changetype<usize>(other) == 0);
217220
var outLen = thisLen + otherLen;
218221
if (<u32>outLen > <u32>BLOCK_MAXSIZE >>> alignof<T>()) throw new Error(E_INVALIDLENGTH);
219222
var out = changetype<Array<T>>(__allocArray(outLen, alignof<T>(), idof<Array<T>>())); // retains
@@ -472,11 +475,7 @@ export class Array<T> extends ArrayBufferView {
472475
if (isBoolean<T>()) return joinBooleanArray(dataStart, length, separator);
473476
if (isInteger<T>()) return joinIntegerArray<T>(dataStart, length, separator);
474477
if (isFloat<T>()) return joinFloatArray<T>(dataStart, length, separator);
475-
476-
if (ASC_SHRINK_LEVEL < 1) {
477-
if (isString<T>()) return joinStringArray(dataStart, length, separator);
478-
}
479-
// For rest objects and arrays use general join routine
478+
if (isString<T>()) return joinStringArray(dataStart, length, separator);
480479
if (isReference<T>()) return joinReferenceArray<T>(dataStart, length, separator);
481480
ERROR("unspported element type");
482481
return <string>unreachable();

std/assembly/arraybuffer.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,23 @@ export abstract class ArrayBufferView {
3232
@sealed export class ArrayBuffer {
3333

3434
static isView<T>(value: T): bool {
35-
if (isNullable<T>()) {
36-
if (value === null) return false;
35+
if (isReference<T>()) {
36+
if (isNullable<T>()) {
37+
if (!changetype<usize>(value)) return false;
38+
}
39+
if (value instanceof Int8Array) return true;
40+
if (value instanceof Uint8Array) return true;
41+
if (value instanceof Uint8ClampedArray) return true;
42+
if (value instanceof Int16Array) return true;
43+
if (value instanceof Uint16Array) return true;
44+
if (value instanceof Int32Array) return true;
45+
if (value instanceof Uint32Array) return true;
46+
if (value instanceof Int64Array) return true;
47+
if (value instanceof Uint64Array) return true;
48+
if (value instanceof Float32Array) return true;
49+
if (value instanceof Float64Array) return true;
50+
if (value instanceof DataView) return true;
3751
}
38-
if (value instanceof Int8Array) return true;
39-
if (value instanceof Uint8Array) return true;
40-
if (value instanceof Uint8ClampedArray) return true;
41-
if (value instanceof Int16Array) return true;
42-
if (value instanceof Uint16Array) return true;
43-
if (value instanceof Int32Array) return true;
44-
if (value instanceof Uint32Array) return true;
45-
if (value instanceof Int64Array) return true;
46-
if (value instanceof Uint64Array) return true;
47-
if (value instanceof Float32Array) return true;
48-
if (value instanceof Float64Array) return true;
49-
if (value instanceof DataView) return true;
5052
return false;
5153
}
5254

std/assembly/map.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export class Map<K,V> {
102102
}
103103

104104
has(key: K): bool {
105-
return this.find(key, HASH<K>(key)) !== null;
105+
return changetype<usize>(this.find(key, HASH<K>(key))) != 0;
106106
}
107107

108108
@operator("[]")

std/assembly/set.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export class Set<T> {
9898

9999
@operator("[]")
100100
has(key: T): bool {
101-
return this.find(key, HASH<T>(key)) !== null;
101+
return changetype<usize>(this.find(key, HASH<T>(key))) != 0;
102102
}
103103

104104
add(key: T): this {

std/assembly/string.ts

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,10 @@ import { idof } from "./builtins";
6161
}
6262

6363
@operator("+") private static __concat(left: String, right: String): String {
64-
return select<String>(left, changetype<String>("null"), left !== null).concat(right);
64+
return select<String>(left, changetype<String>("null"), changetype<usize>(left) != 0).concat(right);
6565
}
6666

6767
concat(other: String): String {
68-
if (other === null) other = changetype<String>("null");
6968
var thisSize: isize = this.length << 1;
7069
var otherSize: isize = other.length << 1;
7170
var outSize: usize = thisSize + otherSize;
@@ -77,7 +76,6 @@ import { idof } from "./builtins";
7776
}
7877

7978
endsWith(search: String, end: i32 = String.MAX_LENGTH): bool {
80-
if (search === null) return false;
8179
end = min(max(end, 0), this.length);
8280
var searchLength = <isize>search.length;
8381
var searchStart = <isize>end - searchLength;
@@ -87,17 +85,17 @@ import { idof } from "./builtins";
8785
}
8886

8987
@operator("==") private static __eq(left: String | null, right: String | null): bool {
90-
if (left === right) return true;
91-
if (left === null || right === null) return false;
92-
var leftLength = left.length;
93-
if (leftLength != right.length) return false;
88+
if (changetype<usize>(left) == changetype<usize>(right)) return true;
89+
if (!changetype<usize>(left) || !changetype<usize>(right)) return false;
90+
var leftLength = changetype<String>(left).length;
91+
if (leftLength != changetype<String>(right).length) return false;
9492
// @ts-ignore: string <-> String
9593
return !compareImpl(left, 0, right, 0, leftLength);
9694
}
9795

9896
@operator.prefix("!")
9997
private static __not(str: String | null): bool {
100-
return str === null || !str.length;
98+
return !changetype<usize>(str) || !changetype<String>(str).length;
10199
}
102100

103101
@operator("!=")
@@ -106,30 +104,37 @@ import { idof } from "./builtins";
106104
}
107105

108106
@operator(">") private static __gt(left: String | null, right: String | null): bool {
109-
if (left === right || left === null || right === null) return false;
110-
var leftLength = left.length;
107+
if (
108+
changetype<usize>(left) == changetype<usize>(right) ||
109+
!changetype<usize>(left) ||
110+
!changetype<usize>(right)
111+
) return false;
112+
var leftLength = changetype<String>(left).length;
111113
if (!leftLength) return false;
112-
var rightLength = right.length;
114+
var rightLength = changetype<String>(right).length;
113115
if (!rightLength) return true;
114116
// @ts-ignore: string <-> String
115117
return compareImpl(left, 0, right, 0, min(leftLength, rightLength)) > 0;
116118
}
117119

118-
@operator(">=") private static __gte(left: String, right: String): bool {
120+
@operator(">=") private static __gte(left: String | null, right: String | null): bool {
119121
return !this.__lt(left, right);
120122
}
121123

122-
@operator("<") private static __lt(left: String, right: String): bool {
123-
if (left === right || left === null || right === null) return false;
124-
var rightLength = right.length;
124+
@operator("<") private static __lt(left: String | null, right: String | null): bool {
125+
if (
126+
changetype<usize>(left) == changetype<usize>(right) ||
127+
!changetype<usize>(left) ||
128+
!changetype<usize>(right)
129+
) return false;
130+
var rightLength = changetype<String>(right).length;
125131
if (!rightLength) return false;
126-
var leftLength = left.length;
132+
var leftLength = changetype<String>(left).length;
127133
if (!leftLength) return true;
128-
// @ts-ignore: string <-> String
129-
return compareImpl(left, 0, right, 0, min(leftLength, rightLength)) < 0;
134+
return compareImpl(changetype<string>(left), 0, changetype<string>(right), 0, min(leftLength, rightLength)) < 0;
130135
}
131136

132-
@operator("<=") private static __lte(left: String, right: String): bool {
137+
@operator("<=") private static __lte(left: String | null, right: String | null): bool {
133138
return !this.__gt(left, right);
134139
}
135140

@@ -165,7 +170,7 @@ import { idof } from "./builtins";
165170

166171
// TODO: implement full locale comparison with locales and Collator options
167172
localeCompare(other: String): i32 {
168-
if (other === this) return 0; // compare pointers
173+
if (changetype<usize>(other) == changetype<usize>(this)) return 0;
169174
var len: isize = this.length;
170175
var otherLen: isize = other.length;
171176
if (otherLen != len) return select(1, -1, len > otherLen);
@@ -175,7 +180,6 @@ import { idof } from "./builtins";
175180
}
176181

177182
startsWith(search: String, start: i32 = 0): bool {
178-
if (search === null) search = changetype<String>("null");
179183
var len = <isize>this.length;
180184
var searchStart = min(max(<isize>start, 0), len);
181185
var searchLength = <isize>search.length;
@@ -445,9 +449,9 @@ import { idof } from "./builtins";
445449

446450
split(separator: String | null = null, limit: i32 = i32.MAX_VALUE): String[] {
447451
if (!limit) return changetype<Array<String>>(__allocArray(0, alignof<String>(), idof<Array<String>>())); // retains
448-
if (separator === null) return [this];
452+
if (!changetype<usize>(separator)) return [this];
449453
var length: isize = this.length;
450-
var sepLen: isize = separator.length;
454+
var sepLen: isize = changetype<String>(separator).length;
451455
if (limit < 0) limit = i32.MAX_VALUE;
452456
if (!sepLen) {
453457
if (!length) return changetype<Array<String>>(__allocArray(0, alignof<String>(), idof<Array<String>>())); // retains
@@ -471,7 +475,7 @@ import { idof } from "./builtins";
471475
}
472476
var result = changetype<Array<String>>(__allocArray(0, alignof<String>(), idof<Array<String>>())); // retains
473477
var end = 0, start = 0, i = 0;
474-
while (~(end = this.indexOf(separator, start))) {
478+
while (~(end = this.indexOf(changetype<String>(separator), start))) {
475479
let len = end - start;
476480
if (len > 0) {
477481
let out = __alloc(<usize>len << 1, idof<String>());

std/assembly/symbol.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ var nextId: usize = 12; // Symbol.unscopables + 1
6767
static readonly unscopables: symbol = changetype<symbol>(11);
6868

6969
static for(key: string): symbol {
70-
if (!stringToId) { stringToId = new Map(); idToString = new Map(); }
70+
if (!stringToId) {
71+
stringToId = new Map();
72+
idToString = new Map();
73+
}
7174
else if (stringToId.has(key)) return changetype<symbol>(stringToId.get(key));
7275
var id = nextId++;
7376
if (!id) unreachable(); // out of ids
@@ -77,7 +80,7 @@ var nextId: usize = 12; // Symbol.unscopables + 1
7780
}
7881

7982
static keyFor(sym: symbol): string | null {
80-
return idToString !== null && idToString.has(changetype<usize>(sym))
83+
return idToString != null && idToString.has(changetype<usize>(sym))
8184
? idToString.get(changetype<usize>(sym))
8285
: null;
8386
}
@@ -98,7 +101,7 @@ var nextId: usize = 12; // Symbol.unscopables + 1
98101
case 10: { str = "toStringTag"; break; }
99102
case 11: { str = "unscopables"; break; }
100103
default: {
101-
if (idToString !== null && idToString.has(id)) str = idToString.get(id);
104+
if (idToString != null && idToString.has(id)) str = idToString.get(id);
102105
break;
103106
}
104107
}

std/assembly/util/hash.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ function hash64(key: u64): u32 {
6363
return v;
6464
}
6565

66-
function hashStr(key: string): u32 {
66+
function hashStr(key: string | null): u32 {
6767
var v = FNV_OFFSET;
68-
if (key !== null) {
69-
for (let i: usize = 0, k: usize = key.length << 1; i < k; ++i) {
68+
if (changetype<usize>(key)) {
69+
for (let i: usize = 0, k: usize = changetype<string>(key).length << 1; i < k; ++i) {
7070
v = (v ^ <u32>load<u8>(changetype<usize>(key) + i)) * FNV_PRIME;
7171
}
7272
}

std/assembly/util/memory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export function memcpy(dest: usize, src: usize, n: usize): void { // see: musl/s
144144
// @ts-ignore: decorator
145145
@inline
146146
export function memmove(dest: usize, src: usize, n: usize): void { // see: musl/src/string/memmove.c
147-
if (dest === src) return;
147+
if (dest == src) return;
148148
if (ASC_SHRINK_LEVEL < 1) {
149149
if (src + n <= dest || dest + n <= src) {
150150
memcpy(dest, src, n);

std/assembly/util/sort.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ export function COMPARATOR<T>(): (a: T, b: T) => i32 {
2929
}
3030
} else if (isString<T>()) {
3131
return (a: T, b: T): i32 => {
32-
if (a === b || a === null || b === null) return 0;
32+
if (
33+
changetype<usize>(a) == changetype<usize>(b) ||
34+
!changetype<usize>(a) ||
35+
!changetype<usize>(b)
36+
) return 0;
3337
var alen = changetype<string>(a).length;
3438
var blen = changetype<string>(b).length;
3539
if (!alen && !blen) return 0;

0 commit comments

Comments
 (0)