Index: test/mjsunit/harmony/toMethod.js |
diff --git a/test/mjsunit/harmony/toMethod.js b/test/mjsunit/harmony/toMethod.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..7e738290215c53ed9d9a47671f6bacc7f1cd0c03 |
--- /dev/null |
+++ b/test/mjsunit/harmony/toMethod.js |
@@ -0,0 +1,163 @@ |
+// 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
|
+// 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: --harmony-classes --allow-natives-syntax |
+ |
+ |
+(function ToMethodSingleClass() { |
+ function f(x) { |
+ var a = [0, 1, 2] |
+ return a[x]; |
+ } |
+ |
+ function ClassD() { } |
+ |
+ assertEquals(1, f(1)); |
+ var g = f.toMethod(ClassD.prototype); |
+ assertEquals(1, g(1)); |
+ assertEquals(undefined, f[%HomeObjectSymbol()]); |
+ assertEquals(ClassD.prototype, g[%HomeObjectSymbol()]); |
+}()); |
+ |
+ |
+(function ToMethodClassHierarchy() { |
+ function f(x) { |
+ return function g(y) { x++; return x + y; }; |
+ } |
+ |
+ function Base() {} |
+ function Derived() { } |
+ Derived.prototype = Object.create(Base.prototype); |
+ |
+ var q = f(0); |
+ assertEquals(2, q(1)); |
+ assertEquals(3, q(1)); |
+ var g = q.toMethod(Derived.prototype); |
+ assertFalse(g === q); |
+ assertEquals(4, g(1)); |
+ assertEquals(5, q(1)); |
+}()); |
+ |
+ |
+(function ToMethodErrorCases() { |
+ var sFun = Function.prototype.toMethod; |
+ assertThrows(function() { sFun.call({}); }, TypeError); |
+ assertThrows(function() { sFun.call({}, {}); }, TypeError); |
+ function f(){}; |
+ assertThrows(function() { f.toMethod(1); }, TypeError); |
+}()); |
+ |
+ |
+(function ToMethodPrototypeChain() { |
+ var o = {}; |
+ var o1 = {}; |
+ function f() { } |
+ |
+ function g() { } |
+ |
+ var fMeth = f.toMethod(o); |
+ assertEquals(o, fMeth[%HomeObjectSymbol()]); |
+ g.__proto__ = fMeth; |
+ assertEquals(undefined, g[%HomeObjectSymbol()]); |
+ var gMeth = g.toMethod(o1); |
+ assertEquals(fMeth, gMeth.__proto__); |
+ assertEquals(o1, gMeth[%HomeObjectSymbol()]); |
+}()); |
+ |
+ |
+(function ToMethodBoundFunction() { |
+ var o = {}; |
+ var p = {}; |
+ |
+ |
+ function f(x, y, z, w) { |
+ assertEquals(o, this); |
+ assertEquals(1, x); |
+ assertEquals(2, y); |
+ assertEquals(3, z); |
+ assertEquals(4, w); |
+ return x+y+z+w; |
+ } |
+ |
+ var fBound = f.bind(o, 1, 2, 3); |
+ var fMeth = fBound.toMethod(p); |
+ assertEquals(10, fMeth(4)); |
+ assertEquals(10, fMeth.call(p, 4)); |
+ var fBound1 = fBound.bind(o, 4); |
+ assertEquals(10, fBound1()); |
+ var fMethBound = fMeth.bind(o, 4); |
+ assertEquals(10, fMethBound()); |
+}()); |
+ |
+(function TestOptimized() { |
+ function f(o) { |
+ return o.x; |
+ } |
+ var o = {x : 15}; |
+ assertEquals(15, f(o)); |
+ assertEquals(15, f(o)); |
+ %OptimizeFunctionOnNextCall(f); |
+ assertEquals(15, f(o)); |
+ var g = f.toMethod({}); |
+ var o1 = {y : 1024, x : "abc"}; |
+ assertEquals("abc", f(o1)); |
+ assertEquals("abc", g(o1)); |
+} ()); |
+ |
+ |
+(function TestPreserveProperties() { |
arv (Not doing code reviews)
2014/08/20 15:35:53
This test is not right.
|
+ function f() { } |
+ f.a = 1; |
+ f.b = "abc"; |
+ var fc = {}; |
+ f.c = fc; |
+ f[0] = "aaa"; |
+ |
+ var m = f.toMethod({}); |
+ assertEquals(1, m.a); |
+ assertEquals("abc", m.b); |
+ assertEquals(fc, m.c); |
+ assertEquals("aaa", m[0]); |
+ |
+ function g() { } |
+ for (var k = 0; k < 10000; k++) { |
+ g[k] = k/10000; |
+ } |
+ var m1 = g.toMethod({}); |
+ for (var k = 0; k < 10000; k++) { |
+ assertEquals(k/10000, m1[k]); |
+ } |
+ |
+ function h() {} |
+ var hProto = {} |
+ h.prototype = hProto; |
+ Object.defineProperty(h, "foo", { get: function() { return 42; } }); |
+ assertEquals(42, h.foo); |
+ var m3 = h.toMethod({}); |
+ assertEquals(42, m3.foo); |
+ assertEquals(hProto, m3.prototype); |
+}()); |