1425bb815Sopenharmony_ci/* Copyright JS Foundation and other contributors, http://js.foundation 2425bb815Sopenharmony_ci * 3425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4425bb815Sopenharmony_ci * you may not use this file except in compliance with the License. 5425bb815Sopenharmony_ci * You may obtain a copy of the License at 6425bb815Sopenharmony_ci * 7425bb815Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8425bb815Sopenharmony_ci * 9425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS 11425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12425bb815Sopenharmony_ci * See the License for the specific language governing permissions and 13425bb815Sopenharmony_ci * limitations under the License. 14425bb815Sopenharmony_ci */ 15425bb815Sopenharmony_ci 16425bb815Sopenharmony_ci function isInstanceofArray (instance) { 17425bb815Sopenharmony_ci assert (instance instanceof C); 18425bb815Sopenharmony_ci assert (instance instanceof B); 19425bb815Sopenharmony_ci assert (instance instanceof A); 20425bb815Sopenharmony_ci assert (instance instanceof Array); 21425bb815Sopenharmony_ci } 22425bb815Sopenharmony_ci 23425bb815Sopenharmony_ci class A extends Array { 24425bb815Sopenharmony_ci f () { 25425bb815Sopenharmony_ci return 5; 26425bb815Sopenharmony_ci } 27425bb815Sopenharmony_ci } 28425bb815Sopenharmony_ci 29425bb815Sopenharmony_ci class B extends A { 30425bb815Sopenharmony_ci g () { 31425bb815Sopenharmony_ci return eval ("eval ('super.f ()')"); 32425bb815Sopenharmony_ci } 33425bb815Sopenharmony_ci } 34425bb815Sopenharmony_ci 35425bb815Sopenharmony_ci class C extends B { 36425bb815Sopenharmony_ci h () { 37425bb815Sopenharmony_ci return eval ('super.g ()'); 38425bb815Sopenharmony_ci } 39425bb815Sopenharmony_ci } 40425bb815Sopenharmony_ci 41425bb815Sopenharmony_ci var c = new C (1, 2, 3, 4, 5, 6); 42425bb815Sopenharmony_ci 43425bb815Sopenharmony_ci isInstanceofArray (c); 44425bb815Sopenharmony_ci c.push (7); 45425bb815Sopenharmony_ci assert (c.length === 7); 46425bb815Sopenharmony_ci assert (c.f () === 5); 47425bb815Sopenharmony_ci assert (c.g () === 5); 48425bb815Sopenharmony_ci assert (c.h () === 5); 49425bb815Sopenharmony_ci 50425bb815Sopenharmony_ci // Test built-in Array prototype methods 51425bb815Sopenharmony_ci var mapped = c.map ((x) => x * 2); 52425bb815Sopenharmony_ci isInstanceofArray (mapped); 53425bb815Sopenharmony_ci 54425bb815Sopenharmony_ci for (var i = 0; i < mapped.length; i++) { 55425bb815Sopenharmony_ci assert (mapped[i] == c[i] * 2); 56425bb815Sopenharmony_ci } 57425bb815Sopenharmony_ci 58425bb815Sopenharmony_ci var concated = c.concat (c); 59425bb815Sopenharmony_ci isInstanceofArray (concated); 60425bb815Sopenharmony_ci 61425bb815Sopenharmony_ci for (var i = 0; i < concated.length; i++) { 62425bb815Sopenharmony_ci assert (concated[i] == c[i % (concated.length / 2)]); 63425bb815Sopenharmony_ci } 64425bb815Sopenharmony_ci 65425bb815Sopenharmony_ci var sliced = c.slice (c); 66425bb815Sopenharmony_ci isInstanceofArray (sliced); 67425bb815Sopenharmony_ci 68425bb815Sopenharmony_ci for (var i = 0; i < sliced.length; i++) { 69425bb815Sopenharmony_ci assert (sliced[i] == c[i]); 70425bb815Sopenharmony_ci } 71425bb815Sopenharmony_ci 72425bb815Sopenharmony_ci var filtered = c.filter ((x) => x > 100); 73425bb815Sopenharmony_ci isInstanceofArray (sliced); 74425bb815Sopenharmony_ci assert (filtered.length === 0); 75425bb815Sopenharmony_ci 76425bb815Sopenharmony_ci var spliced = c.splice (c.length - 1); 77425bb815Sopenharmony_ci isInstanceofArray (spliced); 78425bb815Sopenharmony_ci assert (spliced.length === 1); 79425bb815Sopenharmony_ci assert (spliced[0] === 7); 80425bb815Sopenharmony_ci 81425bb815Sopenharmony_ci c.constructor = 5; 82425bb815Sopenharmony_ci 83425bb815Sopenharmony_ci try { 84425bb815Sopenharmony_ci mapped = c.map ((x) => x * 2); 85425bb815Sopenharmony_ci assert (false); 86425bb815Sopenharmony_ci } catch (e) { 87425bb815Sopenharmony_ci assert (e instanceof TypeError); 88425bb815Sopenharmony_ci } 89425bb815Sopenharmony_ci 90425bb815Sopenharmony_ci try { 91425bb815Sopenharmony_ci concated = c.concat (c); 92425bb815Sopenharmony_ci assert (false); 93425bb815Sopenharmony_ci } catch (e) { 94425bb815Sopenharmony_ci assert (e instanceof TypeError); 95425bb815Sopenharmony_ci } 96425bb815Sopenharmony_ci 97425bb815Sopenharmony_ci try { 98425bb815Sopenharmony_ci sliced = c.slice (c); 99425bb815Sopenharmony_ci assert (false); 100425bb815Sopenharmony_ci } catch (e) { 101425bb815Sopenharmony_ci assert (e instanceof TypeError); 102425bb815Sopenharmony_ci } 103425bb815Sopenharmony_ci 104425bb815Sopenharmony_ci try { 105425bb815Sopenharmony_ci filtered = c.filter ((x) => x > 100); 106425bb815Sopenharmony_ci assert (false); 107425bb815Sopenharmony_ci } catch (e) { 108425bb815Sopenharmony_ci assert (e instanceof TypeError); 109425bb815Sopenharmony_ci } 110425bb815Sopenharmony_ci 111425bb815Sopenharmony_ci try { 112425bb815Sopenharmony_ci spliced = c.splice (0); 113425bb815Sopenharmony_ci assert (false); 114425bb815Sopenharmony_ci } catch (e) { 115425bb815Sopenharmony_ci assert (e instanceof TypeError); 116425bb815Sopenharmony_ci } 117