1/* 2 * Copyright (c) 2025 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// test objectLiteral transition 17function test() { 18 let obj = {a: 1, b: 2}; 19 obj.c = 3; 20 print(obj.c); 21} 22test(); 23print(ArkTools.isAOTCompiled(test)) 24print(ArkTools.isAOTDeoptimized(test)) 25 26// test whether the ObjectLiteralHClassCache is right 27function test2() { 28 let obj0 = ArkTools.createNapiObject(); 29 let d = {x: 3, y: 4}; 30 function foo() { 31 obj0.x = 1; 32 print(obj0.x); 33 } 34 foo(); 35 if (ArkTools.isAOTCompiled(foo)) { 36 d = {x: 1, y: 2}; 37 } 38 print(d.x); 39 print(d.y); 40} 41test2(); 42print(ArkTools.isAOTCompiled(test2)) 43print(ArkTools.isAOTDeoptimized(test2)) 44 45// test object literal with same literal length 46function test3() { 47 let obj = {name: 1}; 48 function foo(obj) { 49 print(obj.name); 50 } 51 foo(obj); 52 if (ArkTools.isAOTCompiled(test)) { 53 let obj2 = {name: 2}; 54 foo(obj2); 55 } 56 function test() { 57 obj.a = 1; 58 obj.b = 2; 59 obj.c = 3; 60 obj.d = 4; 61 } 62 test(); 63} 64test3(); 65print(ArkTools.isAOTCompiled(test3)) 66print(ArkTools.isAOTDeoptimized(test3)) 67 68// test object literal has more or less properties 69function test4() { 70 let obj = {name: 1}; 71 obj.x = 1; 72 obj.y = 2; 73 obj.z = 3; 74 // an object literal has more properties. 75 let obj1 = {key: 1}; 76 obj1.x = 1; 77 obj1.y = 2; 78 obj1.z = 3; 79 obj1.a = 4; 80 // an object literal has less properties. 81 let obj2 = {value: 1}; 82 obj2.x = 1; 83 print(obj.name); 84 print(obj1.a); 85 print(obj2.x); 86} 87test4(); 88print(ArkTools.isAOTCompiled(test4)) 89print(ArkTools.isAOTDeoptimized(test4)) 90 91// test AOT in-prop is hole 92function test5() { 93 function Base() {} 94 Base.prototype = { 95 // constructor is hole 96 set constructor(_) { 97 } 98 }; 99 new Base(); 100} 101test5(); 102print(ArkTools.isAOTCompiled(test5)) 103 104// test AOT CreateObjectWithBuffer 105function CreateBigObjectBuffer() { 106 let obj = {name1: 1, key1: 2}; 107 obj.a0 = 0; 108 obj.a1 = 1; 109 obj.a2 = 2; 110 obj.a3 = 3; 111 obj.a4 = 4; 112 obj.a5 = 5; 113 obj.a6 = 6; 114 obj.a7 = 7; 115 obj.a8 = 8; 116 obj.a9 = 0; 117 obj.a10 = 10; 118 obj.a11 = 11; 119} 120print(ArkTools.isAOTCompiled(CreateBigObjectBuffer)) 121print(ArkTools.isAOTDeoptimized(CreateBigObjectBuffer)) 122 123function test6() { 124 CreateBigObjectBuffer(); 125 if (ArkTools.isAOTCompiled(CreateBigObjectBuffer)) { 126 let obj2 = {name2: 1, key2: 2}; 127 obj2.key = 2; 128 } 129} 130test6(); 131print(ArkTools.isAOTCompiled(test6)) 132print(ArkTools.isAOTDeoptimized(test6)) 133 134// test literal length is zero 135function test7() { 136 var x = { 137 0: 0, 138 1: 1 139 }; 140 x.a = 2; 141 var y = { 142 2: 2 143 }; 144 y.a = 1; 145 y.b = 2; 146 print(x.a); 147 print(x.b); 148 print(y.a); 149 print(y.b); 150} 151test7(); 152print(ArkTools.isAOTCompiled(test7)) 153print(ArkTools.isAOTDeoptimized(test7)) 154