• 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
15var typedArrayConstructors = [
16    Uint8Array,
17    Int8Array,
18    Uint16Array,
19    Int16Array,
20    Uint32Array,
21    Int32Array,
22    Uint8ClampedArray,
23    Float32Array,
24    Float64Array
25];
26
27var set = new Set([1,2,3]);
28
29var foo = {};
30var bar = {};
31var weak_set = new WeakSet();
32weak_set.add(foo);
33weak_set.add(bar);
34
35var string = new String('123');
36
37var map = new Map();
38map.set(0, 'zero');
39map.set(1, 'one');
40
41var weak_map = new WeakMap();
42weak_map.set(foo, 'foo');
43weak_map.set(bar, 'bar');
44
45var string = '123';
46
47for (var constructor of typedArrayConstructors)
48{
49    assert(constructor.from.length === 1);
50
51    try {
52      function f() {constructor.from.call(Array, []);}
53    } catch (e) {
54      assert(e instanceof TypeError);
55    }
56
57    assert(constructor.from(set).toString() === '1,2,3');
58    assert(constructor.from(weak_set).toString() === '');
59
60    if (constructor == Float32Array || constructor == Float64Array)
61    {
62        assert(constructor.from(map).toString() === 'NaN,NaN');
63    }
64    else
65    {
66        assert(constructor.from(map).toString() === '0,0');
67    }
68
69    assert(constructor.from(weak_map).toString() === '');
70    assert(constructor.from(string).toString() === '1,2,3');
71
72    try {
73      function f() {constructor.from.call({}, []);}
74    } catch (e) {
75      assert(e instanceof TypeError);
76    }
77
78    try {
79      function f() {constructor.from.call([], []);}
80    } catch (e) {
81      assert(e instanceof TypeError);
82    }
83
84    try {
85      function f() {constructor.from.call(1, []);}
86    } catch (e) {
87      assert(e instanceof TypeError);
88    }
89
90    try {
91      function f() {constructor.from.call(undefined, []);}
92    } catch (e) {
93      assert(e instanceof TypeError);
94    }
95
96    assert(constructor.from([1,2,3,4]).toString() === '1,2,3,4');
97    assert(constructor.from([12,45]).toString() === '12,45');
98    assert(constructor.from(NaN).toString() === '');
99    assert(constructor.from(Infinity).toString() === '');
100
101    assert(constructor.from([4,5,6], x => x + 1).toString() === '5,6,7');
102    assert(constructor.from([2,4,8], x => x * 2).toString() === '4,8,16');
103
104    try {
105      constructor.from([1,2,3], x => {throw 5});
106      assert(false);
107    } catch (e) {
108      assert(e === 5);
109    }
110
111    try {
112      constructor.from([Symbol.match]);
113      assert(false);
114    } catch (e) {
115      assert(e instanceof TypeError);
116    }
117
118    try {
119      constructor.from([1,1,1], 'foo');
120      assert(false);
121    } catch (e) {
122      assert (e instanceof TypeError);
123    }
124
125    try {
126      function f() {constructor.from(null)}
127    } catch (e) {
128      assert(e instanceof TypeError);
129    }
130
131    try {
132      function f() {constructor.from(undefined);}
133    } catch (e) {
134      assert(e instanceof TypeError);
135    }
136
137    var called = 0;
138    var arr = [1,2,3];
139    var obj = {};
140
141    function testIterator() {
142      called++;
143      return arr[Symbol.iterator]();
144    }
145
146    var getCalled = 0;
147    Object.defineProperty(obj, Symbol.iterator, {
148      get: function() {
149          getCalled++;
150          return testIterator;
151      },
152    });
153
154    assert(constructor.from(obj).toString() === '1,2,3');
155    assert(called === 1);
156    assert(getCalled === 1);
157}
158