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 #include "ecmascript/compiler/aot_constantpool_patcher.h"
17
18 namespace panda::ecmascript {
SetPrototypeForTransitions(JSThread * thread,JSHClass * hclass,JSTaggedValue proto)19 void AotConstantpoolPatcher::SetPrototypeForTransitions(JSThread *thread, JSHClass *hclass, JSTaggedValue proto)
20 {
21 std::queue<JSHClass *> backHClass;
22 backHClass.push(hclass);
23 while (!backHClass.empty()) {
24 JSHClass *current = backHClass.front();
25 backHClass.pop();
26 hclass->SetProto(thread, proto);
27
28 auto transitions = current->GetTransitions();
29 if (transitions.IsUndefined()) {
30 continue;
31 }
32 if (transitions.IsWeak()) {
33 auto cache = transitions.GetTaggedWeakRef();
34 backHClass.push(JSHClass::Cast(cache));
35 continue;
36 }
37 ASSERT(transitions.IsTaggedArray());
38 TransitionsDictionary *dict = TransitionsDictionary::Cast(transitions.GetTaggedObject());
39 dict->IterateEntryValue([&backHClass] (JSHClass *cache) {
40 backHClass.push(JSHClass::Cast(cache));
41 });
42 }
43 }
44
SetObjectFunctionFromConstPool(JSThread * thread,JSHandle<ConstantPool> newConstPool)45 void AotConstantpoolPatcher::SetObjectFunctionFromConstPool(JSThread *thread, JSHandle<ConstantPool> newConstPool)
46 {
47 auto aotHCInfo = newConstPool->GetAotHClassInfo();
48 if (!aotHCInfo.IsTaggedArray()) {
49 return;
50 }
51 auto aotHCInfoArray = TaggedArray::Cast(aotHCInfo);
52 if (aotHCInfoArray->GetLength() <= 0) {
53 return;
54 }
55 JSTaggedValue ihc = aotHCInfoArray->Get(0);
56 if (!ihc.IsJSHClass()) {
57 return;
58 }
59 JSHandle<JSHClass> objectFunctionIHC(thread, ihc);
60 if (!objectFunctionIHC->IsOnlyJSObject()) {
61 LOG_ECMA(FATAL) << "Napi Object must be JSObject.";
62 UNREACHABLE();
63 return;
64 }
65 auto env = thread->GetCurrentEcmaContext()->GetGlobalEnv();
66 if (env->GetObjectFunctionTsNapiClass()->IsUndefined()) {
67 JSHandle<JSHClass> objectFunctionNapiClass(env->GetObjectFunctionNapiClass());
68 objectFunctionIHC->SetPrototype(thread, objectFunctionNapiClass->GetProto());
69 JSHClass::EnableProtoChangeMarker(thread, objectFunctionIHC);
70 SetPrototypeForTransitions(thread, *objectFunctionIHC, objectFunctionNapiClass->GetProto());
71 env->SetObjectFunctionTsNapiClass(thread, objectFunctionIHC.GetTaggedValue());
72 }
73 }
74 } // namespace panda::ecmascript::kungfu
75