summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/extensions/clone-object.js
blob: 56a2166bec603a747a18c986a327cc735966b6d2 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// |reftest| skip-if(!xulRuntime.shell)
// -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
// Any copyright is dedicated to the Public Domain.
// http://creativecommons.org/licenses/publicdomain/

function test() {
    var check = clone_object_check;

    check({});
    check([]);
    check({x: 0});
    check({x: 0.7, p: "forty-two", y: null, z: undefined});
    check(Array.prototype);
    check(Object.prototype);

    // before and after
    var b, a;

    // Slow array.
    b = [, 1, 2, 3];
    b.expando = true;
    b[5] = 5;
    b[0] = 0;
    b[4] = 4;
    delete b[2];
    check(b);

    // Check cloning properties other than basic data properties. (check()
    // asserts that the properties of the clone are configurable, writable,
    // enumerable data properties.)
    b = {};
    Object.defineProperties(b, {
        x: {enumerable: true, get: function () { return 12479; }},
        y: {enumerable: true, configurable: true, writable: false, value: 0},
        z: {enumerable: true, configurable: false, writable: true, value: 0},
        hidden: {enumerable:false, value: 1334}});
    check(b);

    // Check corner cases involving property names.
    b = {"-1": -1,
         0xffffffff: null,
         0x100000000: null,
         "": 0,
         "\xff\x7f\u7fff\uffff\ufeff\ufffe": 1, // random unicode id
         "\ud800 \udbff \udc00 \udfff": 2}; // busted surrogate pairs
    check(b);

    b = [];
    b[-1] = -1;
    b[0xffffffff] = null;
    b[0x100000000] = null;
    b[""] = 0;
    b["\xff\x7f\u7fff\uffff\ufeff\ufffe"] = 1;
    b["\ud800 \udbff \udc00 \udfff"] = 2;
    check(b);

    // Check that array's .length property is cloned.
    b = Array(5);
    assertEq(b.length, 5);
    a = check(b);
    assertEq(a.length, 5);

    b = Array(0);
    b[1] = "ok";
    a = check(b);
    assertEq(a.length, 2);

    // Check that prototypes are not cloned, per spec.
    b = Object.create({x:1});
    b.y = 2;
    b.z = 3;
    check(b);

    // Check that cloning does not separate merge points in the tree.
    var same = {};
    b = {one: same, two: same};
    a = check(b);
    assertEq(a.one === a.two, true);

    b = [same, same];
    a = check(b);
    assertEq(a[0] === a[1], true);

    /*
      XXX TODO spin this out into its own test
    // This fails quickly with an OOM error. An exception would be nicer.
    function Infinitree() {
        return { get left() { return new Infinitree; },
                 get right() { return new Infinitree; }};
    }
    var threw = false;
    try {
        serialize(new Infinitree);
    } catch (exc) {
        threw = true;
    }
    assertEq(threw, true);
    */

    // Clone an array with holes.
    check([0, 1, 2, , 4, 5, 6]);

    // Array holes should not take up space.
    b = [];
    b[255] = 1;
    check(b);
    assertEq(serialize(b).clonebuffer.length < 255, true);

    // Check that trailing holes in an array are preserved.
    b = [1,2,3,,];
    assertEq(b.length, 4);
    a = check(b);
    assertEq(a.length, 4);
    assertEq(a.toString(), "1,2,3,");

    b = [1,2,3,,,];
    assertEq(b.length, 5);
    a = check(b);
    assertEq(a.length, 5);
    assertEq(a.toString(), "1,2,3,,");

    // Self-modifying object.
    // This should never read through to b's prototype.
    b = Object.create({y: 2}, 
                      {x: {enumerable: true,
                           configurable: true,
                           get: function() { if (this.hasOwnProperty("y")) delete this.y; return 1; }},
                       y: {enumerable: true,
                           configurable: true,
                           writable: true,
                           value: 3}});
    check(b, "selfModifyingObject");

    // Ignore properties with object-ids.
    var uri = "http://example.net";
    b = {x: 1, y: 2};
    Object.defineProperty(b, Array(uri, "x"), {enumerable: true, value: 3});
    Object.defineProperty(b, Array(uri, "y"), {enumerable: true, value: 5});
    check(b);
}

test();
reportCompare(0, 0, 'ok');