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 runtime_->Enable();
60 // 28: breakpointer line
61 location_ = TestUtil::GetLocation(sourceFile_.c_str(), 28, 0, pandaFile_.c_str());
62 std::cout<<"vmStart1"<<std::endl;
63 ASSERT_TRUE(location_.GetMethodId().IsValid());
64 std::cout<<"vmStart2"<<std::endl;
65 TestUtil::SuspendUntilContinue(DebugEvent::LOAD_MODULE);
66 ASSERT_EQ(moduleName, pandaFile_);
67 ASSERT_TRUE(debugger_->NotifyScriptParsed(0, pandaFile_));
68 auto condFuncRef = FunctionRef::Undefined(vm_);
69 auto ret = debugInterface_->SetBreakpoint(location_, condFuncRef);
70 ASSERT_TRUE(ret);
71 return true;
72 };
73
74 scenario = [this]() {
75 TestUtil::WaitForLoadModule();
76 TestUtil::Continue();
77 TestUtil::WaitForBreakpoint(location_);
78 TestUtil::Continue();
79 TestUtil::WaitForException();
80 TestUtil::Continue();
81 auto ret = debugInterface_->RemoveBreakpoint(location_);
82 ASSERT_TRUE(ret);
83 ASSERT_EXITED();
84 return true;
85 };
86
87 vmDeath = [this]() {
88 ASSERT_EQ(breakpointCounter_, 1U); // 1: break point counter
89 ASSERT_EQ(exceptionCounter_, 2U); // 2: exception counter
90 return true;
91 };
92 }
93
GetEntryPoint()94 std::pair<std::string, std::string> GetEntryPoint() override
95 {
96 return {pandaFile_, entryPoint_};
97 }
98 ~JsThrowExceptionTest() = default;
99
100 private:
101 const std::string testFile = "throw_exception";
102 std::string pandaFile_ = DEBUGGER_ABC_DIR + testFile + ".abc";
103 std::string sourceFile_ = DEBUGGER_JS_DIR + testFile + ".js";
104 std::string entryPoint_ = "_GLOBAL::func_main_0";
105 JSPtLocation location_ {nullptr, JSPtLocation::EntityId(0), 0};
106 size_t breakpointCounter_ = 0;
107 size_t exceptionCounter_ = 0;
108 };
109
GetJsThrowExceptionTest()110 std::unique_ptr<TestEvents> GetJsThrowExceptionTest()
111 {
112 return std::make_unique<JsThrowExceptionTest>();
113 }
114 } // namespace panda::ecmascript::tooling::test
115
116 #endif // ECMASCRIPT_TOOLING_TEST_UTILS_TESTCASES_JS_THROW_EXCEPTION_TEST_H
117