• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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/method.h"
17 
18 #include "ecmascript/jspandafile/js_pandafile.h"
19 #include "ecmascript/jspandafile/program_object.h"
20 
21 namespace panda::ecmascript {
ParseFunctionName() const22 std::string Method::ParseFunctionName() const
23 {
24     const JSPandaFile *jsPandaFile = GetJSPandaFile();
25     return MethodLiteral::ParseFunctionName(jsPandaFile, GetMethodId());
26 }
27 
GetMethodName() const28 const char *Method::GetMethodName() const
29 {
30     const JSPandaFile *jsPandaFile = GetJSPandaFile();
31     return MethodLiteral::GetMethodName(jsPandaFile, GetMethodId());
32 }
33 
GetMethodName(const JSPandaFile * file) const34 const char *Method::GetMethodName(const JSPandaFile *file) const
35 {
36     return MethodLiteral::GetMethodName(file, GetMethodId());
37 }
38 
GetRecordName() const39 const CString Method::GetRecordName() const
40 {
41     const JSPandaFile *jsPandaFile = GetJSPandaFile();
42     return MethodLiteral::GetRecordName(jsPandaFile, GetMethodId());
43 }
44 
GetCodeSize() const45 uint32_t Method::GetCodeSize() const
46 {
47     const JSPandaFile *jsPandaFile = GetJSPandaFile();
48     return MethodLiteral::GetCodeSize(jsPandaFile, GetMethodId());
49 }
50 
GetJSPandaFile() const51 const JSPandaFile *Method::GetJSPandaFile() const
52 {
53     JSTaggedValue constpool = GetConstantPool();
54     if (constpool.IsUndefined()) {
55         return nullptr;
56     }
57 
58     const ConstantPool *taggedPool = ConstantPool::Cast(constpool.GetTaggedObject());
59     return taggedPool->GetJSPandaFile();
60 }
61 
GetMethodLiteral() const62 MethodLiteral *Method::GetMethodLiteral() const
63 {
64     if (IsAotWithCallField()) {
65         ASSERT(!IsNativeWithCallField());
66         const JSPandaFile *jsPandaFile = GetJSPandaFile();
67         return jsPandaFile->FindMethodLiteral(GetMethodId().GetOffset());
68     }
69     return reinterpret_cast<MethodLiteral *>(GetCodeEntryOrLiteral());
70 }
71 
FindCatchBlock(uint32_t pc) const72 uint32_t Method::FindCatchBlock(uint32_t pc) const
73 {
74     ASSERT(!IsNativeWithCallField());
75     auto *pandaFile = GetJSPandaFile()->GetPandaFile();
76     panda_file::MethodDataAccessor mda(*pandaFile, GetMethodId());
77     panda_file::CodeDataAccessor cda(*pandaFile, mda.GetCodeId().value());
78 
79     uint32_t pcOffset = INVALID_INDEX;
80     cda.EnumerateTryBlocks([&pcOffset, pc](panda_file::CodeDataAccessor::TryBlock &tryBlock) {
81         if ((tryBlock.GetStartPc() <= pc) && ((tryBlock.GetStartPc() + tryBlock.GetLength()) > pc)) {
82             tryBlock.EnumerateCatchBlocks([&](panda_file::CodeDataAccessor::CatchBlock &catchBlock) {
83                 pcOffset = catchBlock.GetHandlerPc();
84                 return false;
85             });
86         }
87         return pcOffset == INVALID_INDEX;
88     });
89     return pcOffset;
90 }
91 
Create(JSThread * thread,const JSPandaFile * jsPandaFile,MethodLiteral * methodLiteral)92 JSHandle<Method> Method::Create(JSThread *thread, const JSPandaFile *jsPandaFile, MethodLiteral *methodLiteral)
93 {
94     EcmaVM *vm = thread->GetEcmaVM();
95     EntityId methodId = methodLiteral->GetMethodId();
96     JSTaggedValue patchVal = vm->GetQuickFixManager()->CheckAndGetPatch(thread, jsPandaFile, methodId);
97     if (!patchVal.IsHole()) {
98         return JSHandle<Method>(thread, patchVal);
99     }
100 
101     JSHandle<Method> method = vm->GetFactory()->NewMethod(methodLiteral);
102     JSHandle<ConstantPool> newConstpool = thread->GetCurrentEcmaContext()->FindOrCreateConstPool(jsPandaFile, methodId);
103     method->SetConstantPool(thread, newConstpool);
104     return method;
105 }
106 } // namespace panda::ecmascript
107