Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1 | // Copyright 2015 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 | function getStrictF() { |
| 6 | 'use strict'; |
| 7 | return function f() {}; |
| 8 | } |
| 9 | |
| 10 | |
| 11 | function getSloppyF() { |
| 12 | return function f() {}; |
| 13 | } |
| 14 | |
| 15 | |
| 16 | function test(testFunction) { |
| 17 | testFunction(getStrictF()); |
| 18 | testFunction(getSloppyF()); |
| 19 | } |
| 20 | |
| 21 | |
| 22 | function testDescriptor(f) { |
| 23 | var descr = Object.getOwnPropertyDescriptor(f, 'name'); |
| 24 | assertTrue(descr.configurable); |
| 25 | assertFalse(descr.enumerable); |
| 26 | assertEquals('f', descr.value); |
| 27 | assertFalse(descr.writable); |
| 28 | } |
| 29 | test(testDescriptor); |
| 30 | |
| 31 | |
| 32 | function testSet(f) { |
| 33 | f.name = 'g'; |
| 34 | assertEquals('f', f.name); |
| 35 | } |
| 36 | test(testSet); |
| 37 | |
| 38 | |
| 39 | function testSetStrict(f) { |
| 40 | 'use strict'; |
| 41 | assertThrows(function() { |
| 42 | f.name = 'g'; |
| 43 | }, TypeError); |
| 44 | } |
| 45 | test(testSetStrict); |
| 46 | |
| 47 | |
| 48 | function testReconfigureAsDataProperty(f) { |
| 49 | Object.defineProperty(f, 'name', { |
| 50 | value: 'g', |
| 51 | }); |
| 52 | assertEquals('g', f.name); |
| 53 | Object.defineProperty(f, 'name', { |
| 54 | writable: true |
| 55 | }); |
| 56 | f.name = 'h'; |
| 57 | assertEquals('h', f.name); |
| 58 | |
| 59 | f.name = 42; |
| 60 | assertEquals(42, f.name); |
| 61 | } |
| 62 | test(testReconfigureAsDataProperty); |
| 63 | |
| 64 | |
| 65 | function testReconfigureAsAccessorProperty(f) { |
| 66 | var name = 'g'; |
| 67 | Object.defineProperty(f, 'name', { |
| 68 | get: function() { return name; }, |
| 69 | set: function(v) { name = v; } |
| 70 | }); |
| 71 | assertEquals('g', f.name); |
| 72 | f.name = 'h'; |
| 73 | assertEquals('h', f.name); |
| 74 | } |
| 75 | test(testReconfigureAsAccessorProperty); |
| 76 | |
| 77 | |
| 78 | function testFunctionToString(f) { |
| 79 | Object.defineProperty(f, 'name', { |
| 80 | value: {toString: function() { assertUnreachable(); }}, |
| 81 | }); |
| 82 | assertEquals('function f() {}', f.toString()); |
| 83 | } |
| 84 | test(testFunctionToString); |
| 85 | |
| 86 | |
| 87 | (function testSetOnInstance() { |
| 88 | // This needs to come before testDelete below |
| 89 | assertTrue(Function.prototype.hasOwnProperty('name')); |
| 90 | |
| 91 | function f() {} |
| 92 | delete f.name; |
| 93 | assertEquals('', f.name); |
| 94 | |
| 95 | f.name = 42; |
| 96 | assertEquals('', f.name); // non writable prototype property. |
| 97 | assertFalse(f.hasOwnProperty('name')); |
| 98 | |
| 99 | Object.defineProperty(Function.prototype, 'name', {writable: true}); |
| 100 | |
| 101 | f.name = 123; |
| 102 | assertTrue(f.hasOwnProperty('name')); |
| 103 | assertEquals(123, f.name); |
| 104 | })(); |
| 105 | |
| 106 | |
| 107 | (function testDelete() { |
| 108 | function f() {} |
| 109 | assertTrue(delete f.name); |
| 110 | assertFalse(f.hasOwnProperty('name')); |
| 111 | assertEquals('', f.name); |
| 112 | |
| 113 | assertTrue(delete Function.prototype.name); |
| 114 | assertEquals(undefined, f.name); |
| 115 | })(); |