• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright JS Foundation and other contributors, http://js.foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15function assertNameExists(func) {
16  assert(func.hasOwnProperty('name') === true);
17}
18function assertNameNotExists(func) {
19  assert(func.hasOwnProperty('name') === false);
20  assert(Function.prototype.name === '');
21  assert(func.name === '');
22}
23
24function assertConfigurableOnlyMethod(func) {
25  let desc = Object.getOwnPropertyDescriptor(func, 'name');
26  assert(desc.configurable === true);
27  assert(desc.writable === false);
28  assert(desc.enumerable === false);
29
30  delete func.name;
31  assertNameNotExists(func);
32
33  Object.defineProperty(func, 'name', {value: 'newName', configurable: true});
34  assert (Object.getOwnPropertyDescriptor(func, 'name').value === 'newName');
35  assertNameExists(func);
36
37  delete func.name;
38  assertNameNotExists(func);
39  Object.defineProperty(func, 'name', desc);
40}
41
42function assertConfigurableOnlyAccessor(func, name, method) {
43  let accessor = Object.getOwnPropertyDescriptor(func, name)[method];
44  assertConfigurableOnlyMethod(accessor);
45}
46
47function assertMethodName(func, name, functionName = name) {
48  assertNameExists(func);
49  assertConfigurableOnlyMethod(func)
50  assert(Object.getOwnPropertyDescriptor(func, 'name').value === functionName)
51}
52
53function assertGetterName(obj, name, functionName = name) {
54  assertConfigurableOnlyAccessor(obj, name, 'get');
55  assert(Object.getOwnPropertyDescriptor(obj, name).get['name'] === 'get ' + functionName)
56}
57
58function assertSetterName(obj, name, functionName = name) {
59  assertConfigurableOnlyAccessor(obj, name, 'set');
60  assert(Object.getOwnPropertyDescriptor(obj, name).set['name'] === 'set ' + functionName)
61}
62
63var func1 = function () {};
64assertMethodName(func1, 'func1');
65
66var func2 = function bar() {};
67assertMethodName(func2, 'bar');
68
69var func3 = (function () {}).prototype.constructor;
70assert(typeof func3 === 'function');
71assertNameNotExists(func3);
72
73var func4;
74func4 = function () {}
75assertMethodName(func4, 'func4');
76
77var func5;
78func5 = function bar () {}
79assertMethodName(func5, 'bar');
80
81var func6;
82(func6) = function () {}
83assertNameNotExists(func6);
84
85var func7;
86(func7) = function bar () {}
87assertMethodName(func7, 'bar');
88
89let emptySymbolMethod = Symbol();
90let namedSymbolMethod = Symbol('foo');
91let emptySymbolGetter = Symbol();
92let namedSymbolGetter = Symbol('foo');
93let emptySymbolSetter = Symbol();
94let namedSymbolSetter = Symbol('foo');
95
96var o = {
97  func1() {},
98  func2: function () {},
99  func3: function bar() {},
100  func4: () => {},
101  func5: class {},
102  func6: class A {},
103  func7: class name { static name () {} },
104  ['func' + '8']() {},
105  ['func' + '9']: function () {},
106  ['func' + '10']: function bar() {},
107  ['func' + '11']: () => {},
108  ['func' + '12']: class {},
109  ['func' + '13']: class A {},
110  ['func' + '14']: class name { static name () {} },
111  get func15() {},
112  get ['func' + '16']() {},
113  set func17(a) {},
114  set ['func' + '18'](a) {},
115  [emptySymbolMethod]() {},
116  [namedSymbolMethod]() {},
117  get [emptySymbolGetter]() {},
118  get [namedSymbolGetter]() {},
119  set [emptySymbolSetter](a) {},
120  set [namedSymbolSetter](a) {},
121}
122
123assertMethodName(o.func1, 'func1');
124assertMethodName(o.func2, 'func2');
125assertMethodName(o.func3, 'bar');
126assertMethodName(o.func4, 'func4');
127assertMethodName(o.func5, 'func5');
128assertMethodName(o.func6, 'A');
129assert(typeof o.func7 === 'function');
130
131assertMethodName(o.func8, 'func8');
132assertMethodName(o.func9, 'func9');
133assertMethodName(o.func10, 'bar');
134assertMethodName(o.func11, 'func11');
135assertMethodName(o.func12, 'func12');
136assertMethodName(o.func13, 'A');
137assert(typeof o.func14 === 'function');
138
139assertGetterName(o, 'func15');
140assertGetterName(o, 'func16');
141assertSetterName(o, 'func17');
142assertSetterName(o, 'func17');
143
144assertMethodName(o[emptySymbolMethod], '');
145assertMethodName(o[namedSymbolMethod], '[foo]');
146assertGetterName(o, emptySymbolGetter, '');
147assertGetterName(o, namedSymbolGetter, '[foo]');
148assertSetterName(o, emptySymbolSetter, '');
149assertSetterName(o, namedSymbolSetter, '[foo]');
150
151class A  {
152  constructor () {}
153  func1() {}
154  get func2() {}
155  set func3(a) {}
156
157  static func4() {}
158  static get func5() {}
159  static set func6(a) {}
160
161  ['func' + '7']() {}
162  get ['func' + '8']() {}
163  set ['func' + '9'](a) {}
164
165  static ['func' + '10']() {}
166  static get ['func' + '11']() {}
167  static set ['func' + '12'](a) {}
168
169  [emptySymbolMethod]() {}
170  [namedSymbolMethod]() {}
171  get [emptySymbolGetter]() {}
172  get [namedSymbolGetter]() {}
173  set [emptySymbolSetter](a) {}
174  set [namedSymbolSetter](a) {}
175
176  static [emptySymbolMethod]() {}
177  static [namedSymbolMethod]() {}
178  static get [emptySymbolGetter]() {}
179  static get [namedSymbolGetter]() {}
180  static set [emptySymbolSetter](a) {}
181  static set [namedSymbolSetter](a) {}
182}
183
184assertMethodName(A.prototype.func1, 'func1');
185assertGetterName(A.prototype, 'func2');
186assertSetterName(A.prototype, 'func3');
187
188assertMethodName(A.func4, 'func4');
189assertGetterName(A, 'func5');
190assertSetterName(A, 'func6');
191
192assertMethodName(A.prototype.func7, 'func7');
193assertGetterName(A.prototype, 'func8');
194assertSetterName(A.prototype, 'func9');
195
196assertMethodName(A.func10, 'func10');
197assertGetterName(A, 'func11');
198assertSetterName(A, 'func12');
199
200assertMethodName(A[emptySymbolMethod], '');
201assertMethodName(A[namedSymbolMethod], '[foo]');
202assertGetterName(A, emptySymbolGetter, '');
203assertGetterName(A, namedSymbolGetter, '[foo]');
204assertSetterName(A, emptySymbolSetter, '');
205assertSetterName(A, namedSymbolSetter, '[foo]');
206
207assertMethodName(A.prototype[emptySymbolMethod], '');
208assertMethodName(A.prototype[namedSymbolMethod], '[foo]');
209assertGetterName(A.prototype, emptySymbolGetter, '');
210assertGetterName(A.prototype, namedSymbolGetter, '[foo]');
211assertSetterName(A.prototype, emptySymbolSetter, '');
212assertSetterName(A.prototype, namedSymbolSetter, '[foo]');
213
214class B  {
215  func1() {}
216  get func2() {}
217  set func3(a) {}
218
219  static func4() {}
220  static get func5() {}
221  static set func6(a) {}
222
223  ['func' + '7']() {}
224  get ['func' + '8']() {}
225  set ['func' + '9'](a) {}
226
227  static ['func' + '10']() {}
228  static get ['func' + '11']() {}
229  static set ['func' + '12'](a) {}
230
231  [emptySymbolMethod]() {}
232  [namedSymbolMethod]() {}
233  get [emptySymbolGetter]() {}
234  get [namedSymbolGetter]() {}
235  set [emptySymbolSetter](a) {}
236  set [namedSymbolSetter](a) {}
237
238  static [emptySymbolMethod]() {}
239  static [namedSymbolMethod]() {}
240  static get [emptySymbolGetter]() {}
241  static get [namedSymbolGetter]() {}
242  static set [emptySymbolSetter](a) {}
243  static set [namedSymbolSetter](a) {}
244}
245
246assertMethodName(B.prototype.func1, 'func1');
247assertGetterName(B.prototype, 'func2');
248assertSetterName(B.prototype, 'func3');
249
250assertMethodName(B.func4, 'func4');
251assertGetterName(B, 'func5');
252assertSetterName(B, 'func6');
253
254assertMethodName(B.prototype.func7, 'func7');
255assertGetterName(B.prototype, 'func8');
256assertSetterName(B.prototype, 'func9');
257
258assertMethodName(B.func10, 'func10');
259assertGetterName(B, 'func11');
260assertSetterName(B, 'func12');
261
262assertMethodName(B[emptySymbolMethod], '');
263assertMethodName(B[namedSymbolMethod], '[foo]');
264assertGetterName(B, emptySymbolGetter, '');
265assertGetterName(B, namedSymbolGetter, '[foo]');
266assertSetterName(B, emptySymbolSetter, '');
267assertSetterName(B, namedSymbolSetter, '[foo]');
268
269assertMethodName(B.prototype[emptySymbolMethod], '');
270assertMethodName(B.prototype[namedSymbolMethod], '[foo]');
271assertGetterName(B.prototype, emptySymbolGetter, '');
272assertGetterName(B.prototype, namedSymbolGetter, '[foo]');
273assertSetterName(B.prototype, emptySymbolSetter, '');
274assertSetterName(B.prototype, namedSymbolSetter, '[foo]');
275
276let names = ['push', 'pop', 'reduce', 'reduceRight'];
277
278for (let n of names) {
279  assert(Array.prototype[n].name === n);
280}
281
282assert(Array.prototype[Symbol.iterator].name === 'values');
283assert(Array.prototype.values.name === 'values');
284assert(Object.getOwnPropertyDescriptor(Array, Symbol.species).get.name === 'get [Symbol.species]');
285assert(Object.getOwnPropertyDescriptor(String.prototype, Symbol.iterator).value.name === '[Symbol.iterator]');
286assert(Object.getOwnPropertyDescriptor(Object.prototype, '__proto__').get.name === 'get __proto__');
287assert(Object.getOwnPropertyDescriptor(Object.prototype, '__proto__').set.name === 'set __proto__');
288
289let arFunc;
290let array = [];
291array['original'] = array;
292array['original'][arFunc = ()=>{ }]=function(){}
293assertNameNotExists(array[arFunc]);
294
295var o = { 0 : class {} };
296
297assertMethodName(o['0'], '0');
298