1 /*
2 * Copyright (c) 2021 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/jspandafile/scope_info_extractor.h"
17
18 #include "ecmascript/interpreter/frame_handler.h"
19 #include "ecmascript/jspandafile/literal_data_extractor.h"
20 #include "ecmascript/jspandafile/program_object.h"
21 #include "ecmascript/tagged_array-inl.h"
22
23 namespace panda::ecmascript {
GenerateScopeInfo(JSThread * thread,uint16_t scopeId)24 JSTaggedValue ScopeInfoExtractor::GenerateScopeInfo(JSThread *thread, uint16_t scopeId)
25 {
26 EcmaVM *ecmaVm = thread->GetEcmaVM();
27 ObjectFactory *factory = ecmaVm->GetFactory();
28
29 [[maybe_unused]] EcmaHandleScope handleScope(thread);
30 Method *method = FrameHandler(thread).GetMethod();
31 const JSPandaFile *jsPandaFile = method->GetJSPandaFile();
32 JSHandle<ConstantPool> constpool(thread, method->GetConstantPool());
33
34 JSHandle<TaggedArray> elementsLiteral;
35 if (jsPandaFile->IsNewVersion()) {
36 panda_file::File::IndexHeader *indexHeader = constpool->GetIndexHeader();
37 auto pf = jsPandaFile->GetPandaFile();
38 Span<const panda_file::File::EntityId> indexs = pf->GetMethodIndex(indexHeader);
39 panda_file::File::EntityId id = indexs[scopeId];
40 elementsLiteral = LiteralDataExtractor::GetDatasIgnoreType(thread, jsPandaFile, id, constpool);
41 } else {
42 elementsLiteral = LiteralDataExtractor::GetDatasIgnoreType(thread, jsPandaFile, scopeId, constpool);
43 }
44
45 ASSERT(elementsLiteral->GetLength() > 0);
46 size_t length = elementsLiteral->GetLength();
47
48 auto buffer = ecmaVm->GetNativeAreaAllocator()->New<struct ScopeDebugInfo>();
49 auto scopeDebugInfo = static_cast<struct ScopeDebugInfo *>(buffer);
50
51 for (size_t i = 1; i < length; i += 2) { // 2: Each literal buffer contains a pair of key-value.
52 JSTaggedValue val = elementsLiteral->Get(i);
53 ASSERT(val.IsString());
54 CString name = ConvertToString(EcmaString::Cast(val.GetTaggedObject()));
55 int32_t slot = elementsLiteral->Get(i + 1).GetInt();
56 if (scopeDebugInfo == nullptr) {
57 return JSTaggedValue::Hole();
58 }
59 scopeDebugInfo->scopeInfo.emplace(name, slot);
60 }
61
62 JSHandle<JSNativePointer> pointer = factory->NewJSNativePointer(
63 buffer, NativeAreaAllocator::FreeObjectFunc<struct ScopeDebugInfo>, ecmaVm->GetNativeAreaAllocator());
64 return pointer.GetTaggedValue();
65 }
66 } // namespace panda::ecmascript
67