mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 23:47:45 +00:00
LibJS: Add symbol objects
This commit adds the following classes: SymbolObject, SymbolConstructor, SymbolPrototype, and Symbol. This commit does not introduce any new functionality to the Object class, so they cannot be used as property keys in objects.
This commit is contained in:
parent
b5b08fba92
commit
4ced126704
21 changed files with 819 additions and 3 deletions
30
Libraries/LibJS/Tests/Symbol.for.js
Normal file
30
Libraries/LibJS/Tests/Symbol.for.js
Normal file
|
@ -0,0 +1,30 @@
|
|||
load("test-common.js")
|
||||
|
||||
try {
|
||||
const localSym = Symbol("foo");
|
||||
const globalSym = Symbol.for("foo");
|
||||
|
||||
assert(localSym !== globalSym);
|
||||
assert(localSym !== Symbol("foo"));
|
||||
assert(globalSym !== Symbol("foo"));
|
||||
assert(globalSym === Symbol.for("foo"));
|
||||
assert(localSym.toString() === "Symbol(foo)");
|
||||
assert(globalSym.toString() === "Symbol(foo)");
|
||||
|
||||
assert(Symbol.for(1).description === "1");
|
||||
assert(Symbol.for(true).description === "true");
|
||||
assert(Symbol.for({}).description === "[object Object]");
|
||||
assert(Symbol.for().description === "undefined");
|
||||
assert(Symbol.for(null).description === "null");
|
||||
|
||||
assertThrowsError(() => {
|
||||
Symbol.for(Symbol());
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Can't convert symbol to string",
|
||||
});
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
26
Libraries/LibJS/Tests/Symbol.js
Normal file
26
Libraries/LibJS/Tests/Symbol.js
Normal file
|
@ -0,0 +1,26 @@
|
|||
load("test-common.js")
|
||||
|
||||
try {
|
||||
const s1 = Symbol("foo");
|
||||
const s2 = Symbol("foo");
|
||||
|
||||
assert(s1 !== s2);
|
||||
assert(s1.description === "foo");
|
||||
assert(s2.description === "foo");
|
||||
|
||||
s1.description = "bar";
|
||||
assert(s1.description === "foo");
|
||||
|
||||
assert(typeof s1 === "symbol");
|
||||
|
||||
assertThrowsError(() => {
|
||||
Symbol(Symbol('foo'));
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Can't convert symbol to string"
|
||||
})
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
31
Libraries/LibJS/Tests/Symbol.keyFor.js
Normal file
31
Libraries/LibJS/Tests/Symbol.keyFor.js
Normal file
|
@ -0,0 +1,31 @@
|
|||
load("test-common.js")
|
||||
|
||||
try {
|
||||
const localSym = Symbol("foo");
|
||||
const globalSym = Symbol.for("foo");
|
||||
|
||||
assert(Symbol.keyFor(localSym) === undefined);
|
||||
assert(Symbol.keyFor(globalSym) === "foo");
|
||||
|
||||
const testThrows = (value, str) => {
|
||||
assertThrowsError(() => {
|
||||
Symbol.keyFor(value);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: str + " is not a symbol",
|
||||
});
|
||||
};
|
||||
|
||||
testThrows(1, "1");
|
||||
testThrows(null, "null");
|
||||
testThrows(undefined, "undefined");
|
||||
testThrows([], "[object Array]");
|
||||
testThrows({}, "[object Object]");
|
||||
testThrows(true, "true");
|
||||
testThrows("foobar", "foobar");
|
||||
testThrows(function(){}, "[object ScriptFunction]"); // FIXME: Better function stringification
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
28
Libraries/LibJS/Tests/Symbol.prototype.toString.js
Normal file
28
Libraries/LibJS/Tests/Symbol.prototype.toString.js
Normal file
|
@ -0,0 +1,28 @@
|
|||
load("test-common.js")
|
||||
|
||||
try {
|
||||
const s1 = Symbol("foo");
|
||||
const s2 = Symbol.for("bar");
|
||||
|
||||
assert(s1.toString() === "Symbol(foo)");
|
||||
assert(s2.toString() === "Symbol(bar)");
|
||||
|
||||
assertThrowsError(() => {
|
||||
s1 + "";
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Can't convert symbol to string",
|
||||
});
|
||||
|
||||
// FIXME: Uncomment when this doesn't assert
|
||||
// assertThrowsError(() => {
|
||||
// s1 + 1;
|
||||
// }, {
|
||||
// error: TypeError,
|
||||
// message: "Can't convert symbol to number",
|
||||
// });
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
22
Libraries/LibJS/Tests/Symbol.prototype.valueOf.js
Normal file
22
Libraries/LibJS/Tests/Symbol.prototype.valueOf.js
Normal file
|
@ -0,0 +1,22 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
let local = Symbol('foo');
|
||||
let global = Symbol.for('foo');
|
||||
assert(local.valueOf() === local);
|
||||
assert(global.valueOf() === global);
|
||||
|
||||
assert(Symbol.prototype.valueOf.call(local) === local);
|
||||
assert(Symbol.prototype.valueOf.call(global) === global);
|
||||
|
||||
assertThrowsError(() => {
|
||||
Symbol.prototype.valueOf.call("foo");
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "object must be of type Symbol"
|
||||
});
|
||||
|
||||
console.log("PASS");
|
||||
} catch (err) {
|
||||
console.log("FAIL: " + err);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue