1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma_5/extensions/watch-setter-become-setter.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,44 @@ 1.4 +/* 1.5 + * Any copyright is dedicated to the Public Domain. 1.6 + * http://creativecommons.org/licenses/publicdomain/ 1.7 + */ 1.8 + 1.9 +/* Create an object with a JavaScript setter. */ 1.10 +var firstSetterCount; 1.11 +var o = { w:2, set x(v) { firstSetterCount++; } }; 1.12 + 1.13 +/* 1.14 + * Put the object in dictionary mode, so that JSObject::putProperty will 1.15 + * mutate its shapes instead of creating new ones. 1.16 + */ 1.17 +delete o.w; 1.18 + 1.19 +/* A stock watcher function. */ 1.20 +var watcherCount; 1.21 +function watcher(id, oldval, newval) { watcherCount++; return newval; } 1.22 + 1.23 +/* 1.24 + * Place a watchpoint on the property. The property's shape now has the 1.25 + * watchpoint setter, with the original setter saved in the watchpoint 1.26 + * structure. 1.27 + */ 1.28 +o.watch('x', watcher); 1.29 + 1.30 +/* 1.31 + * Replace the setter with a new setter. The shape should get updated to 1.32 + * refer to the new setter, and then the watchpoint setter should be 1.33 + * re-established. 1.34 + */ 1.35 +var secondSetterCount; 1.36 +Object.defineProperty(o, 'x', { set: function () { secondSetterCount++ } }); 1.37 + 1.38 +/* 1.39 + * Assign to the property. This should trigger the watchpoint and the new setter. 1.40 + */ 1.41 +watcherCount = firstSetterCount = secondSetterCount = 0; 1.42 +o.x = 3; 1.43 +assertEq(watcherCount, 1); 1.44 +assertEq(firstSetterCount, 0); 1.45 +assertEq(secondSetterCount, 1); 1.46 + 1.47 +reportCompare(true, true);