Skip to content

Commit 9203805

Browse files
MaxGraeydcodeIO
authored andcommitted
Add Object.is (#836)
1 parent b6b5c1e commit 9203805

File tree

6 files changed

+1565
-0
lines changed

6 files changed

+1565
-0
lines changed

std/assembly/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,6 +1541,11 @@ declare class Date {
15411541
setTime(value: i64): i64;
15421542
}
15431543

1544+
declare class Object {
1545+
/** The Object.is() method determines whether two values are the same value. */
1546+
static is<T>(value1: T, value2: T): bool;
1547+
}
1548+
15441549
/** Environmental tracing function for debugging purposes. */
15451550
declare function trace(msg: string, n?: i32, a0?: f64, a1?: f64, a2?: f64, a3?: f64, a4?: f64): void;
15461551

std/assembly/object.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export class Object {
2+
static is<T>(value1: T, value2: T): bool {
3+
if (isFloat<T>()) {
4+
if (value1 == value2) {
5+
// 0 === -0, but they are not identical
6+
if (sizeof<T>() == 8) {
7+
// @ts-ignore: typecast
8+
return reinterpret<u64>(value1) == reinterpret<u64>(value2);
9+
} else {
10+
// @ts-ignore: typecast
11+
return reinterpret<u32>(value1) == reinterpret<u32>(value2);
12+
}
13+
}
14+
// NaN !== NaN, but they are identical.
15+
// @ts-ignore: typecast
16+
return bool(i32(isNaN(value1)) & i32(isNaN(value2)));
17+
}
18+
// For references, strings, integers and booleans
19+
return value1 == value2;
20+
}
21+
}

tests/compiler/std/object.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"asc_flags": [
3+
"--runtime none"
4+
]
5+
}

0 commit comments

Comments
 (0)