1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 20:37:35 +00:00

LibJS: Make (most) String.prototype functions generic

I.e. they don't require the |this| value to be a string object and
"can be transferred to other kinds of objects for use as a method" as
the spec describes it.
This commit is contained in:
Linus Groh 2020-04-29 16:40:05 +01:00 committed by Andreas Kling
parent 4bdb6daac5
commit cfdb7b8806
2 changed files with 126 additions and 89 deletions

View file

@ -0,0 +1,51 @@
load("test-common.js");
try {
const genericStringPrototypeFunctions = [
"charAt",
"repeat",
"startsWith",
"indexOf",
"toLowerCase",
"toUpperCase",
"padStart",
"padEnd",
"trim",
"trimStart",
"trimEnd",
"concat",
"substring",
"includes",
];
genericStringPrototypeFunctions.forEach(name => {
String.prototype[name].call({ toString: () => "hello friends" });
String.prototype[name].call({ toString: () => 123 });
String.prototype[name].call({ toString: () => undefined });
assertThrowsError(() => {
String.prototype[name].call({ toString: () => new String() });
}, {
error: TypeError,
message: "Cannot convert object to string"
});
assertThrowsError(() => {
String.prototype[name].call({ toString: () => [] });
}, {
error: TypeError,
message: "Cannot convert object to string"
});
assertThrowsError(() => {
String.prototype[name].call({ toString: () => ({}) });
}, {
error: TypeError,
message: "Cannot convert object to string"
});
});
console.log("PASS");
} catch (err) {
console.log("FAIL: " + err);
}