Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | |
|
arv (Not doing code reviews)
2014/08/20 15:35:53
Use short copyright header with the year 2014
| |
| 2 // Redistribution and use in source and binary forms, with or without | |
| 3 // modification, are permitted provided that the following conditions are | |
| 4 // met: | |
| 5 // | |
| 6 // * Redistributions of source code must retain the above copyright | |
| 7 // notice, this list of conditions and the following disclaimer. | |
| 8 // * Redistributions in binary form must reproduce the above | |
| 9 // copyright notice, this list of conditions and the following | |
| 10 // disclaimer in the documentation and/or other materials provided | |
| 11 // with the distribution. | |
| 12 // * Neither the name of Google Inc. nor the names of its | |
| 13 // contributors may be used to endorse or promote products derived | |
| 14 // from this software without specific prior written permission. | |
| 15 // | |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 27 // | |
| 28 // Flags: --harmony-classes --allow-natives-syntax | |
| 29 | |
| 30 | |
| 31 (function ToMethodSingleClass() { | |
| 32 function f(x) { | |
| 33 var a = [0, 1, 2] | |
| 34 return a[x]; | |
| 35 } | |
| 36 | |
| 37 function ClassD() { } | |
| 38 | |
| 39 assertEquals(1, f(1)); | |
| 40 var g = f.toMethod(ClassD.prototype); | |
| 41 assertEquals(1, g(1)); | |
| 42 assertEquals(undefined, f[%HomeObjectSymbol()]); | |
| 43 assertEquals(ClassD.prototype, g[%HomeObjectSymbol()]); | |
| 44 }()); | |
| 45 | |
| 46 | |
| 47 (function ToMethodClassHierarchy() { | |
| 48 function f(x) { | |
| 49 return function g(y) { x++; return x + y; }; | |
| 50 } | |
| 51 | |
| 52 function Base() {} | |
| 53 function Derived() { } | |
| 54 Derived.prototype = Object.create(Base.prototype); | |
| 55 | |
| 56 var q = f(0); | |
| 57 assertEquals(2, q(1)); | |
| 58 assertEquals(3, q(1)); | |
| 59 var g = q.toMethod(Derived.prototype); | |
| 60 assertFalse(g === q); | |
| 61 assertEquals(4, g(1)); | |
| 62 assertEquals(5, q(1)); | |
| 63 }()); | |
| 64 | |
| 65 | |
| 66 (function ToMethodErrorCases() { | |
| 67 var sFun = Function.prototype.toMethod; | |
| 68 assertThrows(function() { sFun.call({}); }, TypeError); | |
| 69 assertThrows(function() { sFun.call({}, {}); }, TypeError); | |
| 70 function f(){}; | |
| 71 assertThrows(function() { f.toMethod(1); }, TypeError); | |
| 72 }()); | |
| 73 | |
| 74 | |
| 75 (function ToMethodPrototypeChain() { | |
| 76 var o = {}; | |
| 77 var o1 = {}; | |
| 78 function f() { } | |
| 79 | |
| 80 function g() { } | |
| 81 | |
| 82 var fMeth = f.toMethod(o); | |
| 83 assertEquals(o, fMeth[%HomeObjectSymbol()]); | |
| 84 g.__proto__ = fMeth; | |
| 85 assertEquals(undefined, g[%HomeObjectSymbol()]); | |
| 86 var gMeth = g.toMethod(o1); | |
| 87 assertEquals(fMeth, gMeth.__proto__); | |
| 88 assertEquals(o1, gMeth[%HomeObjectSymbol()]); | |
| 89 }()); | |
| 90 | |
| 91 | |
| 92 (function ToMethodBoundFunction() { | |
| 93 var o = {}; | |
| 94 var p = {}; | |
| 95 | |
| 96 | |
| 97 function f(x, y, z, w) { | |
| 98 assertEquals(o, this); | |
| 99 assertEquals(1, x); | |
| 100 assertEquals(2, y); | |
| 101 assertEquals(3, z); | |
| 102 assertEquals(4, w); | |
| 103 return x+y+z+w; | |
| 104 } | |
| 105 | |
| 106 var fBound = f.bind(o, 1, 2, 3); | |
| 107 var fMeth = fBound.toMethod(p); | |
| 108 assertEquals(10, fMeth(4)); | |
| 109 assertEquals(10, fMeth.call(p, 4)); | |
| 110 var fBound1 = fBound.bind(o, 4); | |
| 111 assertEquals(10, fBound1()); | |
| 112 var fMethBound = fMeth.bind(o, 4); | |
| 113 assertEquals(10, fMethBound()); | |
| 114 }()); | |
| 115 | |
| 116 (function TestOptimized() { | |
| 117 function f(o) { | |
| 118 return o.x; | |
| 119 } | |
| 120 var o = {x : 15}; | |
| 121 assertEquals(15, f(o)); | |
| 122 assertEquals(15, f(o)); | |
| 123 %OptimizeFunctionOnNextCall(f); | |
| 124 assertEquals(15, f(o)); | |
| 125 var g = f.toMethod({}); | |
| 126 var o1 = {y : 1024, x : "abc"}; | |
| 127 assertEquals("abc", f(o1)); | |
| 128 assertEquals("abc", g(o1)); | |
| 129 } ()); | |
| 130 | |
| 131 | |
| 132 (function TestPreserveProperties() { | |
|
arv (Not doing code reviews)
2014/08/20 15:35:53
This test is not right.
| |
| 133 function f() { } | |
| 134 f.a = 1; | |
| 135 f.b = "abc"; | |
| 136 var fc = {}; | |
| 137 f.c = fc; | |
| 138 f[0] = "aaa"; | |
| 139 | |
| 140 var m = f.toMethod({}); | |
| 141 assertEquals(1, m.a); | |
| 142 assertEquals("abc", m.b); | |
| 143 assertEquals(fc, m.c); | |
| 144 assertEquals("aaa", m[0]); | |
| 145 | |
| 146 function g() { } | |
| 147 for (var k = 0; k < 10000; k++) { | |
| 148 g[k] = k/10000; | |
| 149 } | |
| 150 var m1 = g.toMethod({}); | |
| 151 for (var k = 0; k < 10000; k++) { | |
| 152 assertEquals(k/10000, m1[k]); | |
| 153 } | |
| 154 | |
| 155 function h() {} | |
| 156 var hProto = {} | |
| 157 h.prototype = hProto; | |
| 158 Object.defineProperty(h, "foo", { get: function() { return 42; } }); | |
| 159 assertEquals(42, h.foo); | |
| 160 var m3 = h.toMethod({}); | |
| 161 assertEquals(42, m3.foo); | |
| 162 assertEquals(hProto, m3.prototype); | |
| 163 }()); | |
| OLD | NEW |