| 1 |
function getLogString(obj) { |
| 2 |
let log = getWatchtowerLog(); |
| 3 |
return log.map(item => { |
| 4 |
assertEq(item.object, obj); |
| 5 |
if (typeof item.extra === "symbol") { |
| 6 |
item.extra = "<symbol>"; |
| 7 |
} |
| 8 |
return item.kind + (item.extra ? ": " + item.extra : ""); |
| 9 |
}).join("\n"); |
| 10 |
} |
| 11 |
|
| 12 |
function testBasic() { |
| 13 |
let o = {}; |
| 14 |
addWatchtowerTarget(o); |
| 15 |
|
| 16 |
o.x = 1; |
| 17 |
o.y = 2; |
| 18 |
o[Symbol()] = 3; |
| 19 |
Object.defineProperty(o, "foo", {enumerable: false, configurable: false, value: 1}); |
| 20 |
Object.defineProperty(o, "getter", {get() { return 123; }}); |
| 21 |
o.__proto__ = {}; |
| 22 |
o.__proto__ = null; |
| 23 |
o[12345] = 1; // Sparse elements are included for now. |
| 24 |
delete o.x; |
| 25 |
Object.defineProperty(o, "y", {writable: false}); |
| 26 |
Object.defineProperty(o, "x", {value: 0}); |
| 27 |
Object.seal(o); |
| 28 |
Object.freeze(o); |
| 29 |
|
| 30 |
let log = getLogString(o); |
| 31 |
assertEq(log, |
| 32 |
`add-prop: x |
| 33 |
add-prop: y |
| 34 |
add-prop: <symbol> |
| 35 |
add-prop: foo |
| 36 |
add-prop: getter |
| 37 |
proto-change |
| 38 |
proto-change |
| 39 |
add-prop: 12345 |
| 40 |
remove-prop: x |
| 41 |
change-prop-flags: y |
| 42 |
add-prop: x |
| 43 |
freeze-or-seal |
| 44 |
freeze-or-seal`); |
| 45 |
} |
| 46 |
for (var i = 0; i < 20; i++) { |
| 47 |
testBasic(); |
| 48 |
} |
| 49 |
|
| 50 |
// Object.assign edge case. |
| 51 |
function testAssign() { |
| 52 |
let o = {}; |
| 53 |
o.x = 1; |
| 54 |
delete o.x; |
| 55 |
let from = {x: 1, y: 2, z: 3, a: 4}; |
| 56 |
addWatchtowerTarget(o); |
| 57 |
addWatchtowerTarget(from); |
| 58 |
Object.assign(o, from); |
| 59 |
let log = getLogString(o); |
| 60 |
assertEq(log, "add-prop: x\nadd-prop: y\nadd-prop: z\nadd-prop: a"); |
| 61 |
} |
| 62 |
testAssign(); |
| 63 |
|
| 64 |
function testJit() { |
| 65 |
for (var i = 0; i < 20; i++) { |
| 66 |
let o = {}; |
| 67 |
addWatchtowerTarget(o); |
| 68 |
o.x = 1; |
| 69 |
o.y = 2; |
| 70 |
let log = getLogString(o); |
| 71 |
assertEq(log, "add-prop: x\nadd-prop: y"); |
| 72 |
} |
| 73 |
} |
| 74 |
testJit(); |
| 75 |
|
| 76 |
// array.length is a custom data property. |
| 77 |
function testCustomDataProp() { |
| 78 |
let o = []; |
| 79 |
addWatchtowerTarget(o); |
| 80 |
|
| 81 |
Object.defineProperty(o, "length", {writable: false}); |
| 82 |
let log = getLogString(o); |
| 83 |
assertEq(log, "change-prop-flags: length"); |
| 84 |
} |
| 85 |
for (var i = 0; i < 20; i++) { |
| 86 |
testCustomDataProp(); |
| 87 |
} |