1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 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 16let arr = Array.from("abcd"); 17print(arr); 18arr = Array.from("abcd"); 19print(arr); 20arr[1] = 'e'; 21print(arr); 22arr = Array.from("abcd"); 23print(arr); 24 25arr = Array.from("01234567890123456789012"); 26print(arr) 27arr = Array.from("方舟") 28print(arr); 29arr = Array.from("方舟") 30print(arr); 31arr = Array.from("") 32print(arr.length) 33arr[0] = 'a' 34arr = Array.from("") 35print(arr.length) 36 37var src = new Uint8Array(10000); 38for(var i = 0; i < 10000; i++) 39{ 40 src[i] = 1; 41} 42arr = Array.from(src); 43print(arr[666]); 44print(arr[999]); 45print(arr.length); 46 47const v1 = new Map(); 48print(Array.from(v1.keys())) 49 50{ 51 let mp=new Map(); 52 let mpIter = mp.entries(); 53 mpIter.__proto__=[1,2,3,4]; 54 let res=Array.from(mpIter); 55 print(res); 56} 57 58{ 59 class MyArray1 extends Array { 60 constructor(...args) { 61 super(...args); 62 return {}; 63 } 64 } 65 let arr1 = MyArray1.from([1,2,3,4]); 66 print(JSON.stringify(arr1)); 67 68 class MyArray2 extends Array { 69 constructor(...args) { 70 super(...args); 71 return new Proxy({}, { 72 get(o, k) { 73 print("get",k); 74 return o[k]; 75 }, 76 set(o, k, v) { 77 print("set",k); 78 return o[k]=v; 79 }, 80 defineProperty(o, k, v) { 81 print("defineProperty",k); 82 return Object.defineProperty(o,k,v); 83 } 84 }); 85 } 86 } 87 let arr2 = MyArray2.from([1,2,3,4]); 88 print(JSON.stringify(arr2)); 89 90 class MyArray3 extends Array { 91 constructor(...args) { 92 super(...args); 93 return new Proxy(this, { 94 get(o, k) { 95 print("get",k); 96 return o[k]; 97 }, 98 set(o, k, v) { 99 print("set",k); 100 return o[k]=v; 101 }, 102 defineProperty(o, k, v) { 103 print("defineProperty",k); 104 return Object.defineProperty(o,k,v); 105 } 106 }); 107 } 108 } 109 let arr3 = MyArray3.from([1,2,3,4]); 110 print(JSON.stringify(arr3)); 111} 112 113{ 114 let arrIterBak = Array.prototype[Symbol.iterator]; 115 let obj = { 116 get length() { 117 print("get length"); 118 return 10; 119 }, 120 set length(x) { 121 print("set length", x); 122 return true; 123 }, 124 get 0() { 125 print('get 0'); 126 return 0; 127 }, 128 get 1() { 129 print('get 1'); 130 return 1; 131 }, 132 get 2() { 133 print('get 2'); 134 return 2; 135 }, 136 get [Symbol.iterator]() { 137 print("get iterator"); 138 return arrIterBak; 139 } 140 } 141 let res = Array.from(obj); 142 print(JSON.stringify(res)); 143} 144 145{ 146 let arr = [1, 2, 3, 4, 5, 6]; 147 Object.defineProperty(arr, 0, { 148 get() { 149 print("get 0"); 150 arr.pop(); 151 return "x"; 152 } 153 }); 154 let res = Array.from(arr); 155 print(JSON.stringify(res)) 156} 157{ 158 let arrIterBak = Array.prototype[Symbol.iterator]; 159 let arr = new Object(1); 160 arr[1] = 1; 161 arr.length = 10; 162 arr[Symbol.iterator] = arrIterBak; 163 print(arr.constructor) 164 let res = Array.from(arr); 165 print(JSON.stringify(res)) 166} 167{ 168 let arrIterBak = Array.prototype[Symbol.iterator]; 169 Number.prototype.__proto__ = { 170 get length() { 171 print("get length"); 172 return 10; 173 }, 174 set length(x) { 175 print("set length", x); 176 return true; 177 }, 178 get 0() { 179 print('get 0'); 180 return 0; 181 }, 182 get 1() { 183 print('get 1'); 184 return 1; 185 }, 186 get 2() { 187 print('get 2'); 188 return 2; 189 }, 190 get [Symbol.iterator]() { 191 print("get iterator"); 192 return arrIterBak; 193 } 194 }; 195 let arr = 1 196 let res = Array.from(arr); 197 print(JSON.stringify(res)) 198} 199 200{ 201 let arr = [1,2,3]; 202 let res = Array.from(arr.values()); 203 print(JSON.stringify(res)); 204} 205 206