object-assign-plain-cache.js (1717B)
1 // The cache lookup must happen after ensuring there are no dense elements. 2 function testNewDenseElement() { 3 var from = {x: 1, y: 2, z: 3}; 4 5 for (var i = 0; i < 10; i++) { 6 if (i === 6) { 7 from[0] = 1; 8 } 9 var to = Object.assign({}, from); 10 if (i >= 6) { 11 assertEq(JSON.stringify(to), '{"0":1,"x":1,"y":2,"z":3}'); 12 } else { 13 assertEq(JSON.stringify(to), '{"x":1,"y":2,"z":3}'); 14 } 15 } 16 } 17 testNewDenseElement(); 18 19 // The cache lookup must happen after ensuring there are non-writable 20 // properties on the proto chain. 21 function testProtoNonWritable() { 22 var proto = {x: 1}; 23 var from = {x: 1, y: 2, z: 3}; 24 25 for (var i = 0; i < 10; i++) { 26 if (i === 6) { 27 Object.freeze(proto); 28 } 29 30 var to = Object.create(proto); 31 var ex = null; 32 try { 33 Object.assign(to, from); 34 } catch (e) { 35 ex = e; 36 } 37 38 assertEq(ex instanceof TypeError, i > 5); 39 40 if (i <= 5) { 41 assertEq(JSON.stringify(to), '{"x":1,"y":2,"z":3}'); 42 } else { 43 assertEq(JSON.stringify(to), '{}'); 44 } 45 } 46 } 47 testProtoNonWritable(); 48 49 function testDictionary1() { 50 var from = {a: 1, b: 2, c: 3}; 51 delete from.a; 52 for (var i = 0; i < 10; i++) { 53 var to = Object.assign({}, from); 54 assertEq(JSON.stringify(to), '{"b":2,"c":3}'); 55 } 56 } 57 testDictionary1(); 58 59 function testDictionary2() { 60 var from = {a: 1, b: 2, c: 3}; 61 delete from.a; 62 from.a = 4; 63 for (var i = 0; i < 10; i++) { 64 var to = Object.assign({}, from); 65 assertEq(JSON.stringify(to), '{"b":2,"c":3,"a":4}'); 66 } 67 } 68 testDictionary2();