1// Copyright 2014 the V8 project authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4// 5// Flags: --harmony-classes --allow-natives-syntax 6 7 8(function TestSingleClass() { 9 function f(x) { 10 var a = [0, 1, 2] 11 return a[x]; 12 } 13 14 function ClassD() { } 15 16 assertEquals(1, f(1)); 17 var g = f.toMethod(ClassD.prototype); 18 assertEquals(1, g(1)); 19 assertEquals(undefined, f[%HomeObjectSymbol()]); 20 assertEquals(ClassD.prototype, g[%HomeObjectSymbol()]); 21}()); 22 23 24(function TestClassHierarchy() { 25 function f(x) { 26 return function g(y) { x++; return x + y; }; 27 } 28 29 function Base() {} 30 function Derived() { } 31 Derived.prototype = Object.create(Base.prototype); 32 33 var q = f(0); 34 assertEquals(2, q(1)); 35 assertEquals(3, q(1)); 36 var g = q.toMethod(Derived.prototype); 37 assertFalse(g === q); 38 assertEquals(4, g(1)); 39 assertEquals(5, q(1)); 40}()); 41 42 43(function TestErrorCases() { 44 var sFun = Function.prototype.toMethod; 45 assertThrows(function() { sFun.call({}); }, TypeError); 46 assertThrows(function() { sFun.call({}, {}); }, TypeError); 47 function f(){}; 48 assertThrows(function() { f.toMethod(1); }, TypeError); 49}()); 50 51 52(function TestPrototypeChain() { 53 var o = {}; 54 var o1 = {}; 55 function f() { } 56 57 function g() { } 58 59 var fMeth = f.toMethod(o); 60 assertEquals(o, fMeth[%HomeObjectSymbol()]); 61 g.__proto__ = fMeth; 62 assertEquals(undefined, g[%HomeObjectSymbol()]); 63 var gMeth = g.toMethod(o1); 64 assertEquals(fMeth, gMeth.__proto__); 65 assertEquals(o, fMeth[%HomeObjectSymbol()]); 66 assertEquals(o1, gMeth[%HomeObjectSymbol()]); 67}()); 68 69 70(function TestBoundFunction() { 71 var o = {}; 72 var p = {}; 73 74 75 function f(x, y, z, w) { 76 assertEquals(o, this); 77 assertEquals(1, x); 78 assertEquals(2, y); 79 assertEquals(3, z); 80 assertEquals(4, w); 81 return x+y+z+w; 82 } 83 84 var fBound = f.bind(o, 1, 2, 3); 85 var fMeth = fBound.toMethod(p); 86 assertEquals(10, fMeth(4)); 87 assertEquals(10, fMeth.call(p, 4)); 88 var fBound1 = fBound.bind(o, 4); 89 assertEquals(10, fBound1()); 90 var fMethBound = fMeth.bind(o, 4); 91 assertEquals(10, fMethBound()); 92}()); 93 94(function TestOptimized() { 95 function f(o) { 96 return o.x; 97 } 98 var o = {x : 15}; 99 assertEquals(15, f(o)); 100 assertEquals(15, f(o)); 101 %OptimizeFunctionOnNextCall(f); 102 assertEquals(15, f(o)); 103 var g = f.toMethod({}); 104 var o1 = {y : 1024, x : "abc"}; 105 assertEquals("abc", f(o1)); 106 assertEquals("abc", g(o1)); 107} ()); 108 109(function TestExtensibility() { 110 function f() {} 111 Object.preventExtensions(f); 112 assertFalse(Object.isExtensible(f)); 113 var m = f.toMethod({}); 114 assertTrue(Object.isExtensible(m)); 115}()); 116