Skip to content

Add Object.is #836

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions std/assembly/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1541,6 +1541,11 @@ declare class Date {
setTime(value: i64): i64;
}

declare class Object {
/** The Object.is() method determines whether two values are the same value. */
static is<T>(value1: T, value2: T): bool;
}

/** Environmental tracing function for debugging purposes. */
declare function trace(msg: string, n?: i32, a0?: f64, a1?: f64, a2?: f64, a3?: f64, a4?: f64): void;

Expand Down
21 changes: 21 additions & 0 deletions std/assembly/object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export class Object {
static is<T>(value1: T, value2: T): bool {
if (isFloat<T>()) {
if (value1 == value2) {
// 0 === -0, but they are not identical
if (sizeof<T>() == 8) {
// @ts-ignore: typecast
return reinterpret<u64>(value1) == reinterpret<u64>(value2);
} else {
// @ts-ignore: typecast
return reinterpret<u32>(value1) == reinterpret<u32>(value2);
}
}
// NaN !== NaN, but they are identical.
// @ts-ignore: typecast
return bool(i32(isNaN(value1)) & i32(isNaN(value2)));
}
// For references, strings, integers and booleans
return value1 == value2;
}
}
5 changes: 5 additions & 0 deletions tests/compiler/std/object.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"asc_flags": [
"--runtime none"
]
}
Loading