Chromium Code Reviews| Index: test/mjsunit/getters-on-elements.js |
| diff --git a/test/mjsunit/getters-on-elements.js b/test/mjsunit/getters-on-elements.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e8e1c43641e3b8b32f39d06477c36e65caf312ce |
| --- /dev/null |
| +++ b/test/mjsunit/getters-on-elements.js |
| @@ -0,0 +1,134 @@ |
| +// Copyright 2011 the V8 project authors. All rights reserved. |
| +// Redistribution and use in source and binary forms, with or without |
| +// modification, are permitted provided that the following conditions are |
| +// met: |
| +// |
| +// * Redistributions of source code must retain the above copyright |
| +// notice, this list of conditions and the following disclaimer. |
| +// * Redistributions in binary form must reproduce the above |
| +// copyright notice, this list of conditions and the following |
| +// disclaimer in the documentation and/or other materials provided |
| +// with the distribution. |
| +// * Neither the name of Google Inc. nor the names of its |
| +// contributors may be used to endorse or promote products derived |
| +// from this software without specific prior written permission. |
| +// |
| +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| + |
| +// Flags: --allow-natives-syntax |
| + |
| +// It's nice to run this in Firefox too. |
| +var standalone = false; |
| +if (standalone) { |
| + assertTrue = function(val) { |
| + if (val != true) { |
| + print("FAILURE"); |
| + } |
| + } |
| + |
| + assertFalse = function(val) { |
| + if (val != false) { |
| + print("FAILURE"); |
| + } |
| + } |
| + |
| + optimize = function(name) { } |
| +} else { |
| + optimize = function(name) { |
| + %OptimizeFunctionOnNextCall(name); |
| + } |
| +} |
| + |
| +var calls = 0; |
| +Array.prototype.__defineGetter__(0, function(val) { |
| + calls++; |
| +}); |
| + |
| +function dummy(value) { |
| + try { value; } catch(e) { } |
| +} |
| + |
| +(function() { |
| + function run(array, index) { |
| + var runlength = 10; |
| + calls = 0; |
| + var accumulator = 0; |
| + for (var i = 0; i < runlength; ++i) { |
| + accumulator = accumulator + array[index]; |
| + } |
| + // make sure the computation isn't perceived as redundant. |
| + dummy(accumulator); |
| + return calls == runlength; |
| + } |
| + |
| + // Try "warming up" the ic into a monomorphic state to see if we can trick it |
| + // into ignoring the callback. The object map doesn't have the setter callback. |
|
danno
2013/10/23 12:22:11
80 col
|
| + run({}, 0); |
| + run({}, 0); |
| + run({}, 0); |
| + |
| + // Make sure the setter is called if the element is not defined, |
| + // but is not called if the element is defined. |
| + var typeArray = [1,2.5,true]; |
| + for (var types = 0; types < 3; types++) { |
| + value = typeArray[types]; |
| + for (var i = 0; i < 2; i++) { |
| + assertTrue(run([, value], 0)); // undefined |
| + assertFalse(run([0, value], 0)); // defined. |
| + assertTrue(run([, 3, value], 0)); // undefined |
| + assertTrue(run(new Array(10), 0)); // undefined |
| + // "sneak" an element in with shift. |
| + o = []; o[1] = value; o.shift(1); |
| + assertFalse(run(o, 0)); |
| + optimize(run); |
| + run([], 0); |
| + } |
| + } |
| + |
| + // Make sure we can turn it off. |
| + delete Array.prototype[0]; |
| + assertFalse(run([], 0)); |
| +})(); |
| + |
| +Object.prototype.__defineGetter__(0, function(val) { |
| + calls++; |
| +}); |
| + |
| +(function() { |
| + function run(obj) { |
| + var runlength = 10; |
| + calls = 0; |
| + var accumulator = 0; |
| + for (var i = 0; i < runlength; ++i) { |
| + accumulator = accumulator + obj[0]; |
| + } |
| + // make sure the computation isn't perceived as redundant. |
| + dummy(accumulator); |
| + return calls == runlength; |
| + } |
| + |
| + for (var i = 0; i < 2; i++) { |
| + assertTrue(run({})); |
| + o = { foo: 3 }; |
| + assertTrue(run(o)); |
| + o = {}; o[1] = 3; |
| + assertTrue(run(o)); |
| + o = { 0: 0 }; |
| + assertFalse(run(o)); |
| + delete o[0]; |
| + assertTrue(run(o)); |
| + optimize(run); |
| + run([]); |
| + } |
| +})(); |
| + |