Add fake hash and use it to compare functions and class instances

This commit is contained in:
Andrew Morris
2023-07-26 17:27:03 +10:00
parent b683e47bc5
commit 69663190ee
6 changed files with 119 additions and 2 deletions

View File

@@ -0,0 +1,12 @@
// //! test_output("foo")
export default () => {
class X {
foo = "foo";
}
return new X().foo;
};
// deno-lint-ignore no-unused-vars
class X {}

View File

@@ -0,0 +1,31 @@
//! test_output(false)
// Should be: true
// When functions (and therefore classes) have the same source (and references), they should compare
// equal due to the content hash system. This isn't implemented yet - the content hash is just faked
// using its location in the bytecode, which means you only get equal comparison when the functions
// are referentially equal (inconsistent with value semantics).
export default () => {
const a = new classes[0](1, 2);
const b = new classes[1](1, 2);
return a === b;
};
const classes = [
class Point {
constructor(public x: number, public y: number) {}
lenSq() {
return this.x ** 2 + this.y ** 2;
}
},
class Point {
constructor(public x: number, public y: number) {}
lenSq() {
return this.x ** 2 + this.y ** 2;
}
},
];

View File

@@ -0,0 +1,22 @@
//! test_output([true,true,false,true])
export default () => {
const a = new Point(1, 2);
const b = new Point(1, 2);
const c = new Point(1, 3);
return [
a.lenSq === b.lenSq,
a === b,
a === c,
c === c,
];
};
class Point {
constructor(public x: number, public y: number) {}
lenSq() {
return this.x ** 2 + this.y ** 2;
}
}