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 16/* 17 * @tc.name:primitiveic 18 * @tc.desc:test primitiveic 19 * @tc.type: FUNC 20 */ 21 22// for number_ic 23const numObj1 = 10928; 24const numObj2 = 123.456; 25const numObj3 = new Number(42); 26for (let i = 0; i < 100; i++) { 27 let res1 = numObj1.toString(); 28 let res2 = numObj2.toPrecision(4); 29 let res3 = numObj3.valueOf(); 30} 31 32// for number_ic & string_ic hasAccessor 33{ 34 function foo() { return -4096;} 35 Object.defineProperty(String.prototype, "a", {get:foo}); 36 Object.defineProperty(Number.prototype, "b", {get:foo}); 37 for (let i = 0; i < 50; i++) 38 { 39 const num = 123456; 40 const str = "normallize"; 41 num.a; 42 str.b; 43 } 44 Object.defineProperty(Number.prototype, "c", {value:"123"}); 45 for (let i = 0; i < 50; i++) 46 { 47 const num = 123456; 48 const str = "normallize"; 49 num.a; 50 str.b; 51 } 52} 53 54// for number_ic & string_ic notHasAccessor 55{ 56 Object.defineProperty(String.prototype, "d", {value:"456"}); 57 Object.defineProperty(Number.prototype, "e", {value:"789"}); 58 for (let i = 0; i < 50; i++) 59 { 60 const num = 123456; 61 const str = "normallize"; 62 num.d; 63 str.e; 64 } 65} 66 67for(let i=0;i<2;i++){ 68 let obj={}; 69 let x=obj.__proto__; 70 Object.freeze(x); 71} 72 73function f(a, b) { 74 a.name; 75} 76 77for (let i = 0; i < 100; i++) { 78 f(Number, 1); 79 f(120, 1); 80 f(Number, 1); 81} 82 83function f(a, b) { 84 a.valueOf(); 85} 86 87for (let i = 0; i < 100; i++) { 88 f(Number.prototype, 1); 89} 90for (let i = 0; i < 100; i++) { 91 f(120, 1); 92} 93 94const testProtoIcSuccessAssert = []; 95const testProtoIcFailAssert = [ 96 "SendableFloat64Array", 97 "SendableFloat32Array", 98 "SendableInt32Array", 99 "SendableInt16Array", 100 "SendableInt8Array", 101 "SendableUint32Array", 102 "SendableUint16Array", 103 "SendableUint8Array", 104 "SendableUint8ClampedArray" 105]; 106let testProtoIcSuccess = []; 107let testProtoIcFail = []; 108 109function testProtoIc(ctor) { 110 for (let i = 0; i < 100; i++) { }; 111 let obj = new ctor(100); 112 try { 113 let obj1 = { 114 __proto__: obj, 115 }; 116 let obj2 = { 117 __proto__: obj1, 118 }; 119 let obj3 = { 120 __proto__: obj2, 121 }; 122 testProtoIcSuccess.push(ctor.name); 123 } catch (err) { 124 testProtoIcFail.push(ctor.name); 125 } 126} 127 128[ 129 SendableFloat64Array, 130 SendableFloat32Array, 131 SendableInt32Array, 132 SendableInt16Array, 133 SendableInt8Array, 134 SendableUint32Array, 135 SendableUint16Array, 136 SendableUint8Array, 137 SendableUint8ClampedArray, 138].forEach((ctor) => { 139 testProtoIc(ctor); 140}); 141 142assert_equal(testProtoIcSuccess,testProtoIcSuccessAssert) 143assert_equal(testProtoIcFail,testProtoIcFailAssert) 144 145function f(){return 1}; 146Object.defineProperty(this,"g",{ 147 get:f, 148 set:f, 149}) 150for(let i=0;i<2;i++){ 151 assert_equal(g,1) 152} 153 154function func1(o, v) { 155 let res; 156 for (let i = 0; i < 100; i++) { 157 res=o.x; 158 if (res != v) { 159 } 160 } 161 return res; 162} 163{ 164 let pro = { 165 get x() { 166 return 1; 167 } 168 } 169 let o = { 170 __proto__: pro 171 }; 172 o[102500] = 1; 173 o["test"] = "test"; 174 assert_equal(func1(o, 1),1); 175 Object.defineProperty(o, "x", { value: 2 }); 176 assert_equal(func1(o, 2),2); 177} 178 179{ 180 let pro = { 181 get x() { 182 return 1; 183 } 184 } 185 let pro2 = { 186 __proto__: pro 187 }; 188 let o = { 189 __proto__: pro2 190 }; 191 pro2[102500] = 1; 192 pro2["test"] = "test"; 193 assert_equal(func1(o, 1),1); 194 Object.defineProperty(pro2, "x", { value: 2 }); 195 func1(o, 2); 196} 197 198{ 199 function getNumber(o) { 200 let res; 201 for (let i = 0; i < 100; i++) { 202 res=o.Number; 203 } 204 return res; 205 } 206 let pro = globalThis 207 let pro2 = { 208 __proto__: pro 209 }; 210 let o = { 211 __proto__: pro2 212 }; 213 pro2[102500] = 1; 214 pro2["test"] = "test"; 215 for (let i = 0; i < 2; i++) { 216 assert_equal(getNumber(o).toString(),'function Number() { [native code] }') 217 } 218 Object.defineProperty(o, "Number", { value: 2 }); 219 220 for (let i = 0; i < 2; i++) { 221 assert_equal(getNumber(o),2) 222 } 223} 224 225test_end();