• 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 #ifndef ECMASCRIPT_TOOLING_TEST_UTILS_TESTCASES_JS_SINGLE_STEP_TEST_H
17 #define ECMASCRIPT_TOOLING_TEST_UTILS_TESTCASES_JS_SINGLE_STEP_TEST_H
18 
19 #include "test/utils/test_util.h"
20 
21 namespace panda::ecmascript::tooling::test {
22 class JsSingleStepTest : public TestEvents {
23 public:
JsSingleStepTest()24     JsSingleStepTest()
25     {
26         vmDeath = [this]() {
27             ASSERT_NE(stepCounter_, 0);  // 0: step counter
28             ASSERT_EQ(breakpointCounter_, 2);  // 2: break point counter
29             return true;
30         };
31 
32         loadModule = [this](std::string_view moduleName) {
33             locationStart_ = TestUtil::GetLocation(19, 0, pandaFile_.c_str());  // 19: line number
34             locationEnd_ = TestUtil::GetLocation(22, 0, pandaFile_.c_str());  // 22: line number
35             TestUtil::SuspendUntilContinue(DebugEvent::LOAD_MODULE);
36             ASSERT_EQ(moduleName, pandaFile_);
37             auto condFuncRef = FunctionRef::Undefined(vm_);
38             auto ret = debugInterface_->SetBreakpoint(locationEnd_, condFuncRef);
39             ASSERT_TRUE(ret);
40             return true;
41         };
42 
43         breakpoint = [this](const JSPtLocation &location) {
44             ASSERT_TRUE(location.GetMethodId().IsValid());
45             ASSERT_LOCATION_EQ(location, locationEnd_);
46             // Check what step signalled before breakpoint
47             ASSERT_LOCATION_EQ(location, locationStep_);
48             ASSERT_TRUE(collectSteps_);
49             breakpointCounter_++;
50             // Disable collect steps
51             collectSteps_ = false;
52             return true;
53         };
54 
55         singleStep = [this](const JSPtLocation &location) {
56             ASSERT_TRUE(location.GetMethodId().IsValid());
57             if (!collectSteps_) {
58                 if (locationStart_ == location) {
59                     collectSteps_ = true;
60                 }
61                 return false;
62             }
63 
64             ASSERT_NE(bytecodeOffset_, location.GetBytecodeOffset());
65             locationStep_ = location;
66             stepCounter_++;
67             bytecodeOffset_ = location.GetBytecodeOffset();
68             return false;
69         };
70 
71         scenario = []() {
72             TestUtil::WaitForLoadModule();
73             TestUtil::Continue();
74             return true;
75         };
76     }
77 
GetEntryPoint()78     std::pair<std::string, std::string> GetEntryPoint() override
79     {
80         return {pandaFile_, entryPoint_};
81     }
82 
83 private:
84     std::string pandaFile_ = DEBUGGER_ABC_DIR "sample.abc";
85     std::string entryPoint_ = "_GLOBAL::func_main_0";
86     JSPtLocation locationStart_ {nullptr, JSPtLocation::EntityId(0), 0};
87     JSPtLocation locationEnd_ {nullptr, JSPtLocation::EntityId(0), 0};
88     JSPtLocation locationStep_ {nullptr, JSPtLocation::EntityId(0), 0};
89     int32_t stepCounter_ = 0;
90     int32_t breakpointCounter_ = 0;
91     bool collectSteps_ = false;
92     uint32_t bytecodeOffset_ = std::numeric_limits<uint32_t>::max();
93 };
94 
GetJsSingleStepTest()95 std::unique_ptr<TestEvents> GetJsSingleStepTest()
96 {
97     return std::make_unique<JsSingleStepTest>();
98 }
99 }  // namespace panda::ecmascript::tooling::test
100 
101 #endif  // ECMASCRIPT_TOOLING_TEST_UTILS_TESTCASES_JS_SINGLE_STEP_TEST_H
102