js/src/tests/js1_8_5/extensions/clone-object.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // |reftest| skip-if(!xulRuntime.shell)
michael@0 2 // -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 3 // Any copyright is dedicated to the Public Domain.
michael@0 4 // http://creativecommons.org/licenses/publicdomain/
michael@0 5
michael@0 6 // Assert that cloning b does the right thing as far as we can tell.
michael@0 7 // Caveat: getters in b must produce the same value each time they're
michael@0 8 // called. We may call them several times.
michael@0 9 //
michael@0 10 // If desc is provided, then the very first thing we do to b is clone it.
michael@0 11 // (The self-modifying object test counts on this.)
michael@0 12 //
michael@0 13 function check(b, desc) {
michael@0 14 function classOf(obj) {
michael@0 15 return Object.prototype.toString.call(obj);
michael@0 16 }
michael@0 17
michael@0 18 function ownProperties(obj) {
michael@0 19 return Object.getOwnPropertyNames(obj).
michael@0 20 map(function (p) { return [p, Object.getOwnPropertyDescriptor(obj, p)]; });
michael@0 21 }
michael@0 22
michael@0 23 function isCloneable(pair) {
michael@0 24 return typeof pair[0] === 'string' && pair[1].enumerable;
michael@0 25 }
michael@0 26
michael@0 27 function notIndex(p) {
michael@0 28 var u = p >>> 0;
michael@0 29 return !("" + u == p && u != 0xffffffff);
michael@0 30 }
michael@0 31
michael@0 32 function assertIsCloneOf(a, b, path) {
michael@0 33 assertEq(a === b, false);
michael@0 34
michael@0 35 var ca = classOf(a);
michael@0 36 assertEq(ca, classOf(b), path);
michael@0 37
michael@0 38 assertEq(Object.getPrototypeOf(a),
michael@0 39 ca == "[object Object]" ? Object.prototype : Array.prototype,
michael@0 40 path);
michael@0 41
michael@0 42 // 'b', the original object, may have non-enumerable or XMLName
michael@0 43 // properties; ignore them. 'a', the clone, should not have any
michael@0 44 // non-enumerable properties (except .length, if it's an Array) or
michael@0 45 // XMLName properties.
michael@0 46 var pb = ownProperties(b).filter(isCloneable);
michael@0 47 var pa = ownProperties(a);
michael@0 48 for (var i = 0; i < pa.length; i++) {
michael@0 49 assertEq(typeof pa[i][0], "string", "clone should not have E4X properties " + path);
michael@0 50 if (!pa[i][1].enumerable) {
michael@0 51 if (Array.isArray(a) && pa[i][0] == "length") {
michael@0 52 // remove it so that the comparisons below will work
michael@0 53 pa.splice(i, 1);
michael@0 54 i--;
michael@0 55 } else {
michael@0 56 throw new Error("non-enumerable clone property " + uneval(pa[i][0]) + " " + path);
michael@0 57 }
michael@0 58 }
michael@0 59 }
michael@0 60
michael@0 61 // Check that, apart from properties whose names are array indexes,
michael@0 62 // the enumerable properties appear in the same order.
michael@0 63 var aNames = pa.map(function (pair) { return pair[1]; }).filter(notIndex);
michael@0 64 var bNames = pa.map(function (pair) { return pair[1]; }).filter(notIndex);
michael@0 65 assertEq(aNames.join(","), bNames.join(","), path);
michael@0 66
michael@0 67 // Check that the lists are the same when including array indexes.
michael@0 68 function byName(a, b) { a = a[0]; b = b[0]; return a < b ? -1 : a === b ? 0 : 1; }
michael@0 69 pa.sort(byName);
michael@0 70 pb.sort(byName);
michael@0 71 assertEq(pa.length, pb.length, "should see the same number of properties " + path);
michael@0 72 for (var i = 0; i < pa.length; i++) {
michael@0 73 var aName = pa[i][0];
michael@0 74 var bName = pb[i][0];
michael@0 75 assertEq(aName, bName, path);
michael@0 76
michael@0 77 var path2 = path + "." + aName;
michael@0 78 var da = pa[i][1];
michael@0 79 var db = pb[i][1];
michael@0 80 assertEq(da.configurable, true, path2);
michael@0 81 assertEq(da.writable, true, path2);
michael@0 82 assertEq("value" in da, true, path2);
michael@0 83 var va = da.value;
michael@0 84 var vb = b[pb[i][0]];
michael@0 85 if (typeof va === "object" && va !== null)
michael@0 86 queue.push([va, vb, path2]);
michael@0 87 else
michael@0 88 assertEq(va, vb, path2);
michael@0 89 }
michael@0 90 }
michael@0 91
michael@0 92 var banner = "while testing clone of " + (desc || uneval(b));
michael@0 93 var a = deserialize(serialize(b));
michael@0 94 var queue = [[a, b, banner]];
michael@0 95 while (queue.length) {
michael@0 96 var triple = queue.shift();
michael@0 97 assertIsCloneOf(triple[0], triple[1], triple[2]);
michael@0 98 }
michael@0 99
michael@0 100 return a; // for further testing
michael@0 101 }
michael@0 102
michael@0 103 function test() {
michael@0 104 check({});
michael@0 105 check([]);
michael@0 106 check({x: 0});
michael@0 107 check({x: 0.7, p: "forty-two", y: null, z: undefined});
michael@0 108 check(Array.prototype);
michael@0 109 check(Object.prototype);
michael@0 110
michael@0 111 // before and after
michael@0 112 var b, a;
michael@0 113
michael@0 114 // Slow array.
michael@0 115 b = [, 1, 2, 3];
michael@0 116 b.expando = true;
michael@0 117 b[5] = 5;
michael@0 118 b[0] = 0;
michael@0 119 b[4] = 4;
michael@0 120 delete b[2];
michael@0 121 check(b);
michael@0 122
michael@0 123 // Check cloning properties other than basic data properties. (check()
michael@0 124 // asserts that the properties of the clone are configurable, writable,
michael@0 125 // enumerable data properties.)
michael@0 126 b = {};
michael@0 127 Object.defineProperties(b, {
michael@0 128 x: {enumerable: true, get: function () { return 12479; }},
michael@0 129 y: {enumerable: true, configurable: true, writable: false, value: 0},
michael@0 130 z: {enumerable: true, configurable: false, writable: true, value: 0},
michael@0 131 hidden: {enumerable:false, value: 1334}});
michael@0 132 check(b);
michael@0 133
michael@0 134 // Check corner cases involving property names.
michael@0 135 b = {"-1": -1,
michael@0 136 0xffffffff: null,
michael@0 137 0x100000000: null,
michael@0 138 "": 0,
michael@0 139 "\xff\x7f\u7fff\uffff\ufeff\ufffe": 1, // random unicode id
michael@0 140 "\ud800 \udbff \udc00 \udfff": 2}; // busted surrogate pairs
michael@0 141 check(b);
michael@0 142
michael@0 143 b = [];
michael@0 144 b[-1] = -1;
michael@0 145 b[0xffffffff] = null;
michael@0 146 b[0x100000000] = null;
michael@0 147 b[""] = 0;
michael@0 148 b["\xff\x7f\u7fff\uffff\ufeff\ufffe"] = 1;
michael@0 149 b["\ud800 \udbff \udc00 \udfff"] = 2;
michael@0 150 check(b);
michael@0 151
michael@0 152 // An array's .length property is not enumerable, so it is not cloned.
michael@0 153 b = Array(5);
michael@0 154 assertEq(b.length, 5);
michael@0 155 a = check(b);
michael@0 156 assertEq(a.length, 0);
michael@0 157
michael@0 158 b[1] = "ok";
michael@0 159 a = check(b);
michael@0 160 assertEq(a.length, 2);
michael@0 161
michael@0 162 // Check that prototypes are not cloned, per spec.
michael@0 163 b = Object.create({x:1});
michael@0 164 b.y = 2;
michael@0 165 b.z = 3;
michael@0 166 check(b);
michael@0 167
michael@0 168 // Check that cloning does not separate merge points in the tree.
michael@0 169 var same = {};
michael@0 170 b = {one: same, two: same};
michael@0 171 a = check(b);
michael@0 172 assertEq(a.one === a.two, true);
michael@0 173
michael@0 174 b = [same, same];
michael@0 175 a = check(b);
michael@0 176 assertEq(a[0] === a[1], true);
michael@0 177
michael@0 178 // Try cloning a deep object. Don't fail with "too much recursion".
michael@0 179 b = {};
michael@0 180 var current = b;
michael@0 181 for (var i = 0; i < 10000; i++) {
michael@0 182 var next = {};
michael@0 183 current['x' + i] = next;
michael@0 184 current = next;
michael@0 185 }
michael@0 186 check(b, "deepObject"); // takes 2 seconds :-\
michael@0 187
michael@0 188 /*
michael@0 189 XXX TODO spin this out into its own test
michael@0 190 // This fails quickly with an OOM error. An exception would be nicer.
michael@0 191 function Infinitree() {
michael@0 192 return { get left() { return new Infinitree; },
michael@0 193 get right() { return new Infinitree; }};
michael@0 194 }
michael@0 195 var threw = false;
michael@0 196 try {
michael@0 197 serialize(new Infinitree);
michael@0 198 } catch (exc) {
michael@0 199 threw = true;
michael@0 200 }
michael@0 201 assertEq(threw, true);
michael@0 202 */
michael@0 203
michael@0 204 // Clone an array with holes.
michael@0 205 check([0, 1, 2, , 4, 5, 6]);
michael@0 206
michael@0 207 // Array holes should not take up space.
michael@0 208 b = [];
michael@0 209 b[255] = 1;
michael@0 210 check(b);
michael@0 211 assertEq(serialize(b).clonebuffer.length < 255, true);
michael@0 212
michael@0 213 // Self-modifying object.
michael@0 214 // This should never read through to b's prototype.
michael@0 215 b = Object.create({y: 2},
michael@0 216 {x: {enumerable: true,
michael@0 217 configurable: true,
michael@0 218 get: function() { if (this.hasOwnProperty("y")) delete this.y; return 1; }},
michael@0 219 y: {enumerable: true,
michael@0 220 configurable: true,
michael@0 221 writable: true,
michael@0 222 value: 3}});
michael@0 223 check(b, "selfModifyingObject");
michael@0 224
michael@0 225 // Ignore properties with object-ids.
michael@0 226 var uri = "http://example.net";
michael@0 227 b = {x: 1, y: 2};
michael@0 228 Object.defineProperty(b, Array(uri, "x"), {enumerable: true, value: 3});
michael@0 229 Object.defineProperty(b, Array(uri, "y"), {enumerable: true, value: 5});
michael@0 230 check(b);
michael@0 231 }
michael@0 232
michael@0 233 test();
michael@0 234 reportCompare(0, 0, 'ok');

mercurial