summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/basic/object-tostring.js
blob: 5f6204d6dd2b6d53f90f4f71a5e63be79a39e7f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
function doTest(expected, v) {
    assertEq(Object.prototype.toString.call(v), expected)
}

function Dummy() {}

// Basic primitive cases
doTest("[object Undefined]", undefined);
doTest("[object Null]", null);
doTest("[object String]", "");
doTest("[object Number]", 12);
doTest("[object Number]", 12.34);
doTest("[object Boolean]", true);
doTest("[object Symbol]", Symbol());
doTest("[object BigInt]", 1234n);

// Exotic objects with builtinTag
doTest("[object Array]", []);
(function() {
    doTest("[object Arguments]", arguments);
})();
doTest("[object Function]", () => {});
doTest("[object Error]", new Error);
doTest("[object Date]", new Date);
doTest("[object RegExp]", /i/);

// Exotic objects with @@toStringTag
doTest("[object Map]", new Map);
doTest("[object Set]", new Set);
doTest("[object JSON]", JSON);
doTest("[object Math]", Math);

// Normal object cases
doTest("[object Object]", {});
doTest("[object Object]", new Dummy);
doTest("[object Foo]", { [Symbol.toStringTag]: "Foo" });

// Test the receiver of Symbol.toStringTag
let receiver1;
let object1 = {
    __proto__: {
        get [Symbol.toStringTag]() {
            receiver1 = this;
        }
    }
};
doTest("[object Object]", object1);
assertEq(receiver1, object1);

//// BELOW THIS LINE TAMPERS WITH GLOBAL

// Set a toStringTag on the global String.prototype
let receiver2;
Object.defineProperty(String.prototype, Symbol.toStringTag, { get: function() {
    receiver2 = this;
    return "XString";
}});
doTest("[object XString]", "");
assertEq(Object.getPrototypeOf(receiver2), String.prototype);