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