1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 22:47:44 +00:00

LibJS: Make Object.prototype.toString() fully spec compliant

- Fix evaluation order: IsArray(O) should always be called and before
  Get(O, @@toStringTag), previously it was the other way around and
  IsArray would only be called if @@toStringTag is not a string
- Add missing exception checks to both function calls
- Add missing builtin tag for arguments object

Also, while we're here:
- Update variable names to match spec
- Add spec step comments
This commit is contained in:
Linus Groh 2021-07-05 18:31:56 +01:00
parent e1906d74b8
commit 339ccba354
5 changed files with 87 additions and 36 deletions

View file

@ -3,20 +3,29 @@ test("length", () => {
});
test("result for various object types", () => {
const oToString = o => Object.prototype.toString.call(o);
const arrayProxy = new Proxy([], {});
const customToStringTag = {
[Symbol.toStringTag]: "Foo",
};
const arguments = (function () {
return arguments;
})();
expect(oToString(undefined)).toBe("[object Undefined]");
expect(oToString(null)).toBe("[object Null]");
expect(oToString([])).toBe("[object Array]");
expect(oToString(function () {})).toBe("[object Function]");
expect(oToString(new Error())).toBe("[object Error]");
expect(oToString(new TypeError())).toBe("[object Error]");
expect(oToString(new AggregateError([]))).toBe("[object Error]");
expect(oToString(new Boolean())).toBe("[object Boolean]");
expect(oToString(new Number())).toBe("[object Number]");
expect(oToString(new Date())).toBe("[object Date]");
expect(oToString(new RegExp())).toBe("[object RegExp]");
expect(oToString({})).toBe("[object Object]");
expect(Object.prototype.toString.call(undefined)).toBe("[object Undefined]");
expect(Object.prototype.toString.call(null)).toBe("[object Null]");
expect(Object.prototype.toString.call([])).toBe("[object Array]");
expect(Object.prototype.toString.call(arguments)).toBe("[object Arguments]");
expect(Object.prototype.toString.call(function () {})).toBe("[object Function]");
expect(Object.prototype.toString.call(new Error())).toBe("[object Error]");
expect(Object.prototype.toString.call(new TypeError())).toBe("[object Error]");
expect(Object.prototype.toString.call(new AggregateError([]))).toBe("[object Error]");
expect(Object.prototype.toString.call(new Boolean())).toBe("[object Boolean]");
expect(Object.prototype.toString.call(new Number())).toBe("[object Number]");
expect(Object.prototype.toString.call(new Date())).toBe("[object Date]");
expect(Object.prototype.toString.call(new RegExp())).toBe("[object RegExp]");
expect(Object.prototype.toString.call({})).toBe("[object Object]");
expect(Object.prototype.toString.call(arrayProxy)).toBe("[object Array]");
expect(Object.prototype.toString.call(customToStringTag)).toBe("[object Foo]");
expect(globalThis.toString()).toBe("[object Object]");
});