• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #ifndef ECMASCRIPT_TOOLING_TEST_UTILS_TESTCASES_JS_LOCAL_VARIABLE_SCOPE_TEST_H
17 #define ECMASCRIPT_TOOLING_TEST_UTILS_TESTCASES_JS_LOCAL_VARIABLE_SCOPE_TEST_H
18 
19 #include "test/utils/test_util.h"
20 
21 namespace panda::ecmascript::tooling::test {
22 class JsLocalVariableScopeTest : public TestEvents {
23 public:
JsLocalVariableScopeTest()24     JsLocalVariableScopeTest()
25     {
26         breakpoint = [this](const JSPtLocation &location) {
27             ASSERT_TRUE(location.GetMethodId().IsValid());
28             ASSERT_LOCATION_EQ(location, location_);
29             ++breakpointCounter_;
30             debugger_->NotifyPaused(location, PauseReason::INSTRUMENTATION);
31             TestUtil::SuspendUntilContinue(DebugEvent::BREAKPOINT, location);
32             return true;
33         };
34 
35         loadModule = [this](std::string_view moduleName) {
36             std::string pandaFile = DEBUGGER_ABC_DIR "local_variable_scope.abc";
37             std::string sourceFile = DEBUGGER_JS_DIR "local_variable_scope.js";
38             static_cast<JsLocalVariableScopeTestChannel *>(channel_)->Initial(vm_, runtime_);
39             runtime_->Enable();
40             // 37: breakpointer line
41             location_ = TestUtil::GetLocation(sourceFile.c_str(), 37, 0, pandaFile.c_str());
42             ASSERT_TRUE(location_.GetMethodId().IsValid());
43             TestUtil::SuspendUntilContinue(DebugEvent::LOAD_MODULE);
44             ASSERT_EQ(moduleName, pandaFile);
45             ASSERT_TRUE(debugger_->NotifyScriptParsed(0, pandaFile));
46             auto condFuncRef = FunctionRef::Undefined(vm_);
47             auto ret = debugInterface_->SetBreakpoint(location_, condFuncRef);
48             ASSERT_TRUE(ret);
49             return true;
50         };
51 
52         scenario = [this]() {
53             TestUtil::WaitForLoadModule();
54             TestUtil::Continue();
55             TestUtil::WaitForBreakpoint(location_);
56             TestUtil::Continue();
57             auto ret = debugInterface_->RemoveBreakpoint(location_);
58             ASSERT_TRUE(ret);
59             ASSERT_EXITED();
60             return true;
61         };
62 
63         vmDeath = [this]() {
64             ASSERT_EQ(breakpointCounter_, 1U);
65             return true;
66         };
67 
68         channel_ = new JsLocalVariableScopeTestChannel();
69     }
70 
GetEntryPoint()71     std::pair<std::string, std::string> GetEntryPoint() override
72     {
73         std::string pandaFile = DEBUGGER_ABC_DIR "local_variable_scope.abc";
74         return {pandaFile, entryPoint_};
75     }
76 
~JsLocalVariableScopeTest()77     ~JsLocalVariableScopeTest()
78     {
79         delete channel_;
80         channel_ = nullptr;
81     }
82 private:
83     class JsLocalVariableScopeTestChannel : public TestChannel {
84     public:
85         JsLocalVariableScopeTestChannel() = default;
86         ~JsLocalVariableScopeTestChannel() = default;
87 
Initial(const EcmaVM * vm,RuntimeImpl * runtime)88         void Initial(const EcmaVM *vm, RuntimeImpl *runtime)
89         {
90             vm_ = vm;
91             runtime_ = runtime;
92         }
93 
SendNotification(const PtBaseEvents & events)94         void SendNotification(const PtBaseEvents &events) override
95         {
96             const static std::vector<std::function<bool(const PtBaseEvents &events)>> eventList = {
97                 [](const PtBaseEvents &events) -> bool {
98                     std::string sourceFile = DEBUGGER_JS_DIR "local_variable_scope.js";
99                     auto parsed = static_cast<const ScriptParsed *>(&events);
100                     std::string str = parsed->ToJson()->Stringify();
101 
102                     ASSERT_EQ(parsed->GetName(), "Debugger.scriptParsed");
103                     ASSERT_EQ(parsed->GetUrl(), sourceFile);
104                     return true;
105                 },
106                 [this](const PtBaseEvents &events) -> bool {
107                     auto paused = static_cast<const Paused *>(&events);
108                     std::string str = paused->ToJson()->Stringify();
109 
110                     ASSERT_EQ(paused->GetName(), "Debugger.paused");
111                     auto frame = paused->GetCallFrames()->at(0).get();
112                     auto scopes = frame->GetScopeChain();
113                     for (uint32_t i = 0; i < scopes->size(); i++) {
114                         auto scope = scopes->at(i).get();
115                         if (scope->GetType() != Scope::Type::Local()) {
116                             continue;
117                         }
118                         auto localScopeId = scope->GetObject()->GetObjectId();
119                         GetPropertiesParams params;
120                         params.SetObjectId(localScopeId).SetOwnProperties(true);
121                         std::vector<std::unique_ptr<PropertyDescriptor>> outPropertyDesc;
122                         runtime_->GetProperties(params, &outPropertyDesc, {}, {}, {});
123                         std::map<std::string, std::string> variables;
124 
125                         for (const auto &property : outPropertyDesc) {
126                             auto value = property->GetValue();
127                             auto name = property->GetName();
128                             ASSERT_TRUE(truthGroundMap_.find(name) != truthGroundMap_.end());
129                             extractLocalVariable(name, value, variables);
130                             ASSERT_EQ(variables[name], truthGroundMap_.at(name));
131                         }
132                         ASSERT_EQ(variables.size(), truthGroundMap_.size());
133                     }
134                     return true;
135                 }
136             };
137 
138             ASSERT_TRUE(eventList[index_++](events));
139         }
140     private:
141         NO_COPY_SEMANTIC(JsLocalVariableScopeTestChannel);
142         NO_MOVE_SEMANTIC(JsLocalVariableScopeTestChannel);
143 
extractLocalVariable(std::string name,RemoteObject * value,std::map<std::string,std::string> & variables)144         void extractLocalVariable(std::string name, RemoteObject *value, std::map<std::string, std::string> &variables)
145         {
146             std::string variableValue = "undefined";
147             if (value->HasUnserializableValue()) {
148                 variableValue = value->GetUnserializableValue();
149             }
150             if (variables.find(name) == variables.end()) {
151                 variables.emplace(name, variableValue);
152             }
153         }
154 
155         const std::map<std::string, std::string> truthGroundMap_ = {
156             {"a", "1"},
157             {"b", "2"},
158             {"e", "6"},
159             {"f", "7"},
160             {"innerTest", "function innerTest( { [js code] }"},
161             {"j", "12"},
162             {"scopeTest", "function scopeTest( { [js code] }"},
163             {"n", "undefined"},
164             {"p", "undefined"},
165             {"q", "undefined"}
166         };
167 
168         int32_t index_ {0};
169         const EcmaVM *vm_ {nullptr};
170         RuntimeImpl *runtime_ {nullptr};
171     };
172     std::string entryPoint_ = "_GLOBAL::func_main_0";
173     JSPtLocation location_ {nullptr, JSPtLocation::EntityId(0), 0};
174     size_t breakpointCounter_ = 0;
175 };
176 
GetJsLocalVariableScopeTest()177 std::unique_ptr<TestEvents> GetJsLocalVariableScopeTest()
178 {
179     return std::make_unique<JsLocalVariableScopeTest>();
180 }
181 } // namespace panda::ecmascript::tooling::test
182 
183 #endif  // ECMASCRIPT_TOOLING_TEST_UTILS_TESTCASES_JS_LOCAL_VARIABLE_SCOPE_TEST_H
184