1 /*
2 * Copyright (c) 2022 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_THROW_EXCEPTION_TEST_H
17 #define ECMASCRIPT_TOOLING_TEST_UTILS_TESTCASES_JS_THROW_EXCEPTION_TEST_H
18
19 #include "test/utils/test_util.h"
20
21 namespace panda::ecmascript::tooling::test {
22 class JsThrowExceptionTest : public TestEvents {
23 public:
JsThrowExceptionTest()24 JsThrowExceptionTest()
25 {
26 breakpoint = [this](const JSPtLocation &location) {
27 ASSERT_TRUE(location.GetMethodId().IsValid());
28 ASSERT_LOCATION_EQ(location, location_);
29 ++breakpointCounter_;
30 std::vector<std::unique_ptr<CallFrame>> callFrames;
31 ASSERT_TRUE(debugger_->GenerateCallFrames(&callFrames));
32 ASSERT_TRUE(callFrames.size() > 0);
33 auto jsLocation = callFrames[0]->GetLocation();
34 ASSERT_TRUE(jsLocation != nullptr);
35 ASSERT_EQ(jsLocation->GetLine(), 28); // 28: breakpoint line
36 ASSERT_EQ(jsLocation->GetColumn(), 0); // 0: breakpoint column
37 TestUtil::SuspendUntilContinue(DebugEvent::BREAKPOINT, location);
38 return true;
39 };
40
41 exception = [this](const JSPtLocation &location) {
42 auto sourceLocation = TestUtil::GetSourceLocation(location, pandaFile_.c_str());
43 ASSERT_EQ(sourceLocation.line, 19); // 19: exception line
44 ASSERT_EQ(sourceLocation.column, 8); // 8: exception column
45 ++exceptionCounter_;
46 std::vector<std::unique_ptr<CallFrame>> callFrames;
47 ASSERT_TRUE(debugger_->GenerateCallFrames(&callFrames));
48 ASSERT_TRUE(callFrames.size() > 0);
49 auto jsLocation = callFrames[0]->GetLocation();
50 ASSERT_TRUE(jsLocation != nullptr);
51 ASSERT_EQ(jsLocation->GetLine(), 19); // 19: exception line
52 ASSERT_EQ(jsLocation->GetColumn(), 8); // 8: exception column
53 ++exceptionCounter_;
54 TestUtil::SuspendUntilContinue(DebugEvent::EXCEPTION, location);
55 return true;
56 };
57
58 loadModule = [this](std::string_view moduleName) {
59 location_ = TestUtil::GetLocation(28, 0, pandaFile_.c_str()); // 28: breakpointer line
60 std::cout<<"vmStart1"<<std::endl;
61 ASSERT_TRUE(location_.GetMethodId().IsValid());
62 std::cout<<"vmStart2"<<std::endl;
63 TestUtil::SuspendUntilContinue(DebugEvent::LOAD_MODULE);
64 ASSERT_EQ(moduleName, pandaFile_);
65 ASSERT_TRUE(debugger_->NotifyScriptParsed(0, pandaFile_));
66 auto condFuncRef = FunctionRef::Undefined(vm_);
67 auto ret = debugInterface_->SetBreakpoint(location_, condFuncRef);
68 ASSERT_TRUE(ret);
69 return true;
70 };
71
72 scenario = [this]() {
73 TestUtil::WaitForLoadModule();
74 TestUtil::Continue();
75 TestUtil::WaitForBreakpoint(location_);
76 TestUtil::Continue();
77 TestUtil::WaitForException();
78 TestUtil::Continue();
79 auto ret = debugInterface_->RemoveBreakpoint(location_);
80 ASSERT_TRUE(ret);
81 ASSERT_EXITED();
82 return true;
83 };
84
85 vmDeath = [this]() {
86 ASSERT_EQ(breakpointCounter_, 1U); // 1: break point counter
87 ASSERT_EQ(exceptionCounter_, 2U); // 2: exception counter
88 return true;
89 };
90 }
91
GetEntryPoint()92 std::pair<std::string, std::string> GetEntryPoint() override
93 {
94 return {pandaFile_, entryPoint_};
95 }
96 ~JsThrowExceptionTest() = default;
97
98 private:
99 std::string pandaFile_ = DEBUGGER_ABC_DIR "throw_exception.abc";
100 std::string entryPoint_ = "_GLOBAL::func_main_0";
101 JSPtLocation location_ {nullptr, JSPtLocation::EntityId(0), 0};
102 size_t breakpointCounter_ = 0;
103 size_t exceptionCounter_ = 0;
104 };
105
GetJsThrowExceptionTest()106 std::unique_ptr<TestEvents> GetJsThrowExceptionTest()
107 {
108 return std::make_unique<JsThrowExceptionTest>();
109 }
110 } // namespace panda::ecmascript::tooling::test
111
112 #endif // ECMASCRIPT_TOOLING_TEST_UTILS_TESTCASES_JS_THROW_EXCEPTION_TEST_H
113