• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/object_factory-inl.h"
22 #include "ecmascript/tagged_array-inl.h"
23 
24 namespace panda::ecmascript {
GenerateScopeInfo(JSThread * thread,uint16_t scopeId)25 JSTaggedValue ScopeInfoExtractor::GenerateScopeInfo(JSThread *thread, uint16_t scopeId)
26 {
27     EcmaVM *ecmaVm = thread->GetEcmaVM();
28     ObjectFactory *factory = ecmaVm->GetFactory();
29 
30     [[maybe_unused]] EcmaHandleScope handleScope(thread);
31     Method *method = FrameHandler(thread).GetMethod();
32     const JSPandaFile *jsPandaFile = method->GetJSPandaFile();
33     JSHandle<ConstantPool> constpool(thread, method->GetConstantPool());
34 
35     JSHandle<TaggedArray> elementsLiteral;
36     if (jsPandaFile->IsNewVersion()) {
37         panda_file::File::EntityId id = constpool->GetEntityId(scopeId);
38         elementsLiteral = LiteralDataExtractor::GetDatasIgnoreType(thread, jsPandaFile, id, constpool);
39     } else {
40         elementsLiteral = LiteralDataExtractor::GetDatasIgnoreType(thread, jsPandaFile, scopeId, constpool);
41     }
42 
43     ASSERT(elementsLiteral->GetLength() > 0);
44 
45     auto *scopeDebugInfo = ecmaVm->GetNativeAreaAllocator()->New<ScopeDebugInfo>();
46     if (scopeDebugInfo == nullptr) {
47         return JSTaggedValue::Hole();
48     }
49 
50     size_t length = elementsLiteral->GetLength();
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         scopeDebugInfo->scopeInfo.emplace(name, slot);
57     }
58 
59     JSHandle<JSNativePointer> pointer = factory->NewJSNativePointer(
60         scopeDebugInfo, NativeAreaAllocator::FreeObjectFunc<ScopeDebugInfo>, ecmaVm->GetNativeAreaAllocator());
61     return pointer.GetTaggedValue();
62 }
63 }  // namespace panda::ecmascript
64