• 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 */
15
16var builtin_objects = [
17  Array,
18  ArrayBuffer,
19  Boolean,
20  DataView,
21  Date,
22  Error,
23  EvalError,
24  Function,
25  Map,
26  Number,
27  Object,
28  Promise,
29  RangeError,
30  ReferenceError,
31  RegExp,
32  Set,
33  String,
34  Symbol,
35  SyntaxError,
36  TypeError,
37  URIError,
38  WeakMap,
39  WeakSet,
40];
41
42var builtin_typedArrays = [
43  Float32Array,
44  Float64Array,
45  Int16Array,
46  Int32Array,
47  Int8Array,
48  Uint16Array,
49  Uint32Array,
50  Uint8Array,
51  Uint8ClampedArray,
52];
53
54
55(function () {
56  /* each length property is configurable */
57  var desc;
58
59  for (obj of builtin_objects) {
60    desc = Object.getOwnPropertyDescriptor(obj, 'length');
61    assert(desc.writable === false);
62    assert(desc.enumerable === false);
63    assert(desc.configurable === true);
64  }
65
66  for (ta of builtin_typedArrays) {
67    desc = Object.getOwnPropertyDescriptor(ta, 'length');
68    assert(desc.writable === false);
69    assert(desc.enumerable === false);
70    assert(desc.configurable === true);
71  }
72})();
73
74(function () {
75  /* each length property can be deleted */
76  for (obj of builtin_objects) {
77    assert(obj.hasOwnProperty('length') === true);
78    assert(delete obj.length);
79    assert(obj.hasOwnProperty('length') === false);
80  }
81
82  for (ta of builtin_typedArrays) {
83    assert(ta.hasOwnProperty('length') === true);
84    assert(delete ta.length);
85    assert(ta.hasOwnProperty('length') === false);
86  }
87})();
88
89(function () {
90  /* test length property of builtin function */
91  for (obj of builtin_objects) {
92    var property_names = Object.getOwnPropertyNames(obj);
93    for (var name of property_names) {
94      if (typeof obj[name] == 'function') {
95        var func = obj[name];
96        var desc = Object.getOwnPropertyDescriptor(func, 'length');
97        assert(desc.writable === false);
98        assert(desc.enumerable === false);
99        assert(desc.configurable === true);
100
101        assert(func.hasOwnProperty('length') === true);
102        assert(delete func.length);
103        assert(func.hasOwnProperty('length') === false);
104      }
105    }
106  }
107})();
108
109(function () {
110  /* test length property of function objects */
111  var normal_func = function () {};
112  var arrow_func = () => {};
113  var bound_func = normal_func.bind({});
114  var nested_bound_func = arrow_func.bind().bind(1);
115
116  var functions = [normal_func, arrow_func, bound_func, nested_bound_func];
117
118  for (func of functions) {
119    var desc = Object.getOwnPropertyDescriptor(func, 'length');
120    assert(desc.writable === false);
121    assert(desc.enumerable === false);
122    assert(desc.configurable === true);
123  }
124
125  for (func of functions) {
126    assert(func.hasOwnProperty('length') === true);
127    assert(delete func.length);
128    assert(func.hasOwnProperty('length') === false);
129  }
130})();
131
132(function() {
133  /* changing the length of f affects the bound function */
134  function f(a,b,c) {}
135  Object.defineProperty(f, "length", { value: 30 });
136  var g = f.bind(1,2)
137  assert(g.length === 29);
138})();
139
140(function() {
141  /* changing the length of f does not affect the bound function */
142  function f(a,b,c) {}
143  var g = f.bind(1,2)
144  Object.defineProperty(f, "length", { value: 30 });
145  assert(g.length === 2);
146})();
147