1 /**
2 * Copyright (c) 2024 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 "evaluation/evaluation_engine.h"
17
18 #include "include/tooling/pt_thread.h"
19
20 namespace ark::tooling::inspector {
EvaluateExpression(uint32_t frameNumber,const ExpressionWrapper & bytecode,Method ** method)21 Expected<std::pair<TypedValue, ObjectHeader *>, Error> PtThreadEvaluationEngine::EvaluateExpression(
22 uint32_t frameNumber, const ExpressionWrapper &bytecode, Method **method)
23 {
24 ASSERT(!IsEvaluating());
25 ASSERT(method != nullptr);
26
27 VRegValue result;
28 std::optional<Error> err;
29
30 evaluating_ = true;
31 if (*method != nullptr) {
32 err = debugger_->EvaluateExpression(PtThread(thread_), frameNumber, *method, &result);
33 } else {
34 err = debugger_->EvaluateExpression(PtThread(thread_), frameNumber, bytecode, method, &result);
35 }
36 evaluating_ = false;
37
38 if (err) {
39 return Unexpected(*err);
40 }
41 ASSERT(*method != nullptr);
42
43 auto *exception = thread_->GetException();
44 // Current implementation must clear any occurred exceptions
45 if (exception != nullptr) {
46 thread_->SetException(nullptr);
47 }
48
49 return std::make_pair(result.ToTypedValue((*method)->GetReturnType().GetId()), exception);
50 }
51 } // namespace ark::tooling::inspector
52