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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/js1_8_5/extensions/clone-complex-object.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,313 @@
     1.4 +// |reftest| skip-if(!xulRuntime.shell)
     1.5 +// -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     1.6 +// Any copyright is dedicated to the Public Domain.
     1.7 +// http://creativecommons.org/licenses/publicdomain/
     1.8 +
     1.9 +// Set of properties on a cloned object that are legitimately non-enumerable,
    1.10 +// grouped by object type.
    1.11 +var non_enumerable = { 'Array': [ 'length' ],
    1.12 +                       'String': [ 'length' ] };
    1.13 +
    1.14 +// Set of properties on a cloned object that are legitimately non-configurable,
    1.15 +// grouped by object type. The property name '0' stands in for any indexed
    1.16 +// property.
    1.17 +var non_configurable = { 'String': [ 0 ],
    1.18 +                         '(typed array)': [ 0 ] };
    1.19 +
    1.20 +// Set of properties on a cloned object that are legitimately non-writable,
    1.21 +// grouped by object type. The property name '0' stands in for any indexed
    1.22 +// property.
    1.23 +var non_writable = { 'String': [ 0 ] };
    1.24 +
    1.25 +function classOf(obj) {
    1.26 +    var classString = Object.prototype.toString.call(obj);
    1.27 +    var [ all, classname ] = classString.match(/\[object (\w+)/);
    1.28 +    return classname;
    1.29 +}
    1.30 +
    1.31 +function isIndex(p) {
    1.32 +    var u = p >>> 0;
    1.33 +    return ("" + u == p && u != 0xffffffff);
    1.34 +}
    1.35 +
    1.36 +function notIndex(p) {
    1.37 +    return !isIndex(p);
    1.38 +}
    1.39 +
    1.40 +function tableContains(table, cls, prop) {
    1.41 +    if (isIndex(prop))
    1.42 +        prop = 0;
    1.43 +    if (cls.match(/\wArray$/))
    1.44 +        cls = "(typed array)";
    1.45 +    var exceptionalProps = table[cls] || [];
    1.46 +    return exceptionalProps.indexOf(prop) != -1;
    1.47 +}
    1.48 +
    1.49 +function shouldBeConfigurable(cls, prop) {
    1.50 +    return !tableContains(non_configurable, cls, prop);
    1.51 +}
    1.52 +
    1.53 +function shouldBeWritable(cls, prop) {
    1.54 +    return !tableContains(non_writable, cls, prop);
    1.55 +}
    1.56 +
    1.57 +function ownProperties(obj) {
    1.58 +    return Object.getOwnPropertyNames(obj).
    1.59 +        map(function (p) { return [p, Object.getOwnPropertyDescriptor(obj, p)]; });
    1.60 +}
    1.61 +
    1.62 +function isCloneable(pair) {
    1.63 +    return typeof pair[0] === 'string' && pair[1].enumerable;
    1.64 +}
    1.65 +
    1.66 +function compareProperties(a, b, stack, path) {
    1.67 +    var ca = classOf(a);
    1.68 +
    1.69 +    // 'b', the original object, may have non-enumerable or XMLName properties;
    1.70 +    // ignore them. 'a', the clone, should not have any non-enumerable
    1.71 +    // properties (except .length, if it's an Array or String) or XMLName
    1.72 +    // properties.
    1.73 +    var pb = ownProperties(b).filter(isCloneable);
    1.74 +    var pa = ownProperties(a);
    1.75 +    for (var i = 0; i < pa.length; i++) {
    1.76 +        var propname = pa[i][0];
    1.77 +        assertEq(typeof propname, "string", "clone should not have E4X properties " + path);
    1.78 +        if (!pa[i][1].enumerable) {
    1.79 +            if (tableContains(non_enumerable, ca, propname)) {
    1.80 +                // remove it so that the comparisons below will work
    1.81 +                pa.splice(i, 1);
    1.82 +                i--;
    1.83 +            } else {
    1.84 +                throw new Error("non-enumerable clone property " + uneval(pa[i][0]) + " " + path);
    1.85 +            }
    1.86 +        }
    1.87 +    }
    1.88 +
    1.89 +    // Check that, apart from properties whose names are array indexes,
    1.90 +    // the enumerable properties appear in the same order.
    1.91 +    var aNames = pa.map(function (pair) { return pair[1]; }).filter(notIndex);
    1.92 +    var bNames = pa.map(function (pair) { return pair[1]; }).filter(notIndex);
    1.93 +    assertEq(aNames.join(","), bNames.join(","), path);
    1.94 +
    1.95 +    // Check that the lists are the same when including array indexes.
    1.96 +    function byName(a, b) { a = a[0]; b = b[0]; return a < b ? -1 : a === b ? 0 : 1; }
    1.97 +    pa.sort(byName);
    1.98 +    pb.sort(byName);
    1.99 +    assertEq(pa.length, pb.length, "should see the same number of properties " + path);
   1.100 +    for (var i = 0; i < pa.length; i++) {
   1.101 +        var aName = pa[i][0];
   1.102 +        var bName = pb[i][0];
   1.103 +        assertEq(aName, bName, path);
   1.104 +
   1.105 +        var path2 = isIndex(aName) ? path + "[" + aName + "]" : path + "." + aName;
   1.106 +        var da = pa[i][1];
   1.107 +        var db = pb[i][1];
   1.108 +        assertEq(da.configurable, shouldBeConfigurable(ca, aName), path2);
   1.109 +        assertEq(da.writable, shouldBeWritable(ca, aName), path2);
   1.110 +        assertEq("value" in da, true, path2);
   1.111 +        var va = da.value;
   1.112 +        var vb = b[pb[i][0]];
   1.113 +        stack.push([va, vb, path2]);
   1.114 +    }
   1.115 +}
   1.116 +
   1.117 +function isClone(a, b) {
   1.118 +    var stack = [[a, b, 'obj']];
   1.119 +    var memory = new WeakMap();
   1.120 +    var rmemory = new WeakMap();
   1.121 +
   1.122 +    while (stack.length > 0) {
   1.123 +        var pair = stack.pop();
   1.124 +        var x = pair[0], y = pair[1], path = pair[2];
   1.125 +        if (typeof x !== "object" || x === null) {
   1.126 +            // x is primitive.
   1.127 +            assertEq(x, y, "equal primitives");
   1.128 +        } else if (x instanceof Date) {
   1.129 +            assertEq(x.getTime(), y.getTime(), "equal times for cloned Dates");
   1.130 +        } else if (memory.has(x)) {
   1.131 +            // x is an object we have seen before in a.
   1.132 +            assertEq(y, memory.get(x), "repeated object the same");
   1.133 +            assertEq(rmemory.get(y), x, "repeated object's clone already seen");
   1.134 +        } else {
   1.135 +            // x is an object we have not seen before.
   1.136 +	    // Check that we have not seen y before either.
   1.137 +            assertEq(rmemory.has(y), false);
   1.138 +
   1.139 +            var xcls = classOf(x);
   1.140 +            var ycls = classOf(y);
   1.141 +            assertEq(xcls, ycls, "same [[Class]]");
   1.142 +
   1.143 +            // clone objects should have the default prototype of the class
   1.144 +            assertEq(Object.getPrototypeOf(x), this[xcls].prototype);
   1.145 +
   1.146 +            compareProperties(x, y, stack, path);
   1.147 +
   1.148 +            // Record that we have seen this pair of objects.
   1.149 +            memory.set(x, y);
   1.150 +            rmemory.set(y, x);
   1.151 +        }
   1.152 +    }
   1.153 +    return true;
   1.154 +}
   1.155 +
   1.156 +function check(val) {
   1.157 +    var clone = deserialize(serialize(val));
   1.158 +    assertEq(isClone(val, clone), true);
   1.159 +    return clone;
   1.160 +}
   1.161 +
   1.162 +// Various recursive objects
   1.163 +
   1.164 +// Recursive array.
   1.165 +var a = [];
   1.166 +a[0] = a;
   1.167 +check(a);
   1.168 +
   1.169 +// Recursive Object.
   1.170 +var b = {};
   1.171 +b.next = b;
   1.172 +check(b);
   1.173 +
   1.174 +// Mutually recursive objects.
   1.175 +var a = [];
   1.176 +var b = {};
   1.177 +var c = {};
   1.178 +a[0] = b;
   1.179 +a[1] = b;
   1.180 +a[2] = b;
   1.181 +b.next = a;
   1.182 +check(a);
   1.183 +check(b);
   1.184 +
   1.185 +// A date
   1.186 +check(new Date);
   1.187 +
   1.188 +// A recursive object that is very large.
   1.189 +a = [];
   1.190 +b = a;
   1.191 +for (var i = 0; i < 10000; i++) {
   1.192 +    b[0] = {};
   1.193 +    b[1] = [];
   1.194 +    b = b[1];
   1.195 +}
   1.196 +b[0] = {owner: a};
   1.197 +b[1] = [];
   1.198 +check(a);
   1.199 +
   1.200 +// Date objects should not be identical even if representing the same date
   1.201 +var ar = [ new Date(1000), new Date(1000) ];
   1.202 +var clone = check(ar);
   1.203 +assertEq(clone[0] === clone[1], false);
   1.204 +
   1.205 +// Identity preservation for various types of objects
   1.206 +
   1.207 +function checkSimpleIdentity(v)
   1.208 +{
   1.209 +    a = check([ v, v ]);
   1.210 +    assertEq(a[0] === a[1], true);
   1.211 +    return a;
   1.212 +}
   1.213 +
   1.214 +var v = new Boolean(true);
   1.215 +checkSimpleIdentity(v);
   1.216 +
   1.217 +v = new Number(17);
   1.218 +checkSimpleIdentity(v);
   1.219 +
   1.220 +v = new String("yo");
   1.221 +checkSimpleIdentity(v);
   1.222 +
   1.223 +v = "fish";
   1.224 +checkSimpleIdentity(v);
   1.225 +
   1.226 +v = new Int8Array([ 10, 20 ]);
   1.227 +checkSimpleIdentity(v);
   1.228 +
   1.229 +v = new ArrayBuffer(7);
   1.230 +checkSimpleIdentity(v);
   1.231 +
   1.232 +v = new Date(1000);
   1.233 +b = [ v, v, { 'date': v } ];
   1.234 +clone = check(b);
   1.235 +assertEq(clone[0] === clone[1], true);
   1.236 +assertEq(clone[0], clone[2]['date']);
   1.237 +assertEq(clone[0] === v, false);
   1.238 +
   1.239 +// Reduced and modified from postMessage_structured_clone test
   1.240 +let foo = { };
   1.241 +let baz = { };
   1.242 +let obj = { 'foo': foo,
   1.243 +            'bar': { 'foo': foo },
   1.244 +            'expando': { 'expando': baz },
   1.245 +            'baz': baz };
   1.246 +check(obj);
   1.247 +
   1.248 +for (var obj of new getTestContent)
   1.249 +    check(obj);
   1.250 +
   1.251 +// Stolen wholesale from postMessage_structured_clone_helper.js
   1.252 +function getTestContent()
   1.253 +{
   1.254 +  yield "hello";
   1.255 +  yield 2+3;
   1.256 +  yield 12;
   1.257 +  yield null;
   1.258 +  yield "complex" + "string";
   1.259 +  yield new Object();
   1.260 +  yield new Date(1306113544);
   1.261 +  yield [1, 2, 3, 4, 5];
   1.262 +  let obj = new Object();
   1.263 +  obj.foo = 3;
   1.264 +  obj.bar = "hi";
   1.265 +  obj.baz = new Date(1306113544);
   1.266 +  obj.boo = obj;
   1.267 +  yield obj;
   1.268 +
   1.269 +  let recursiveobj = new Object();
   1.270 +  recursiveobj.a = recursiveobj;
   1.271 +  recursiveobj.foo = new Object();
   1.272 +  recursiveobj.foo.bar = "bar";
   1.273 +  recursiveobj.foo.backref = recursiveobj;
   1.274 +  recursiveobj.foo.baz = 84;
   1.275 +  recursiveobj.foo.backref2 = recursiveobj;
   1.276 +  recursiveobj.bar = new Object();
   1.277 +  recursiveobj.bar.foo = "foo";
   1.278 +  recursiveobj.bar.backref = recursiveobj;
   1.279 +  recursiveobj.bar.baz = new Date(1306113544);
   1.280 +  recursiveobj.bar.backref2 = recursiveobj;
   1.281 +  recursiveobj.expando = recursiveobj;
   1.282 +  yield recursiveobj;
   1.283 +
   1.284 +  let obj = new Object();
   1.285 +  obj.expando1 = 1;
   1.286 +  obj.foo = new Object();
   1.287 +  obj.foo.bar = 2;
   1.288 +  obj.bar = new Object();
   1.289 +  obj.bar.foo = obj.foo;
   1.290 +  obj.expando = new Object();
   1.291 +  obj.expando.expando = new Object();
   1.292 +  obj.expando.expando.obj = obj;
   1.293 +  obj.expando2 = 4;
   1.294 +  obj.baz = obj.expando.expando;
   1.295 +  obj.blah = obj.bar;
   1.296 +  obj.foo.baz = obj.blah;
   1.297 +  obj.foo.blah = obj.blah;
   1.298 +  yield obj;
   1.299 +
   1.300 +  let diamond = new Object();
   1.301 +  let obj = new Object();
   1.302 +  obj.foo = "foo";
   1.303 +  obj.bar = 92;
   1.304 +  obj.backref = diamond;
   1.305 +  diamond.ref1 = obj;
   1.306 +  diamond.ref2 = obj;
   1.307 +  yield diamond;
   1.308 +
   1.309 +  let doubleref = new Object();
   1.310 +  let obj = new Object();
   1.311 +  doubleref.ref1 = obj;
   1.312 +  doubleref.ref2 = obj;
   1.313 +  yield doubleref;
   1.314 +}
   1.315 +
   1.316 +reportCompare(0, 0, 'ok');

mercurial