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 #include "script_function.h"
16 #include "script_context.h"
17 #include "script_interpreter.h"
18 #include "script_manager.h"
19 #include "script_param.h"
20 #include "script_utils.h"
21
22 using namespace std;
23
24 namespace Uscript {
~ScriptFunction()25 ScriptFunction::~ScriptFunction()
26 {
27 delete params_;
28 delete statements_;
29 }
30
Execute(ScriptInterpreter & inter,UScriptContextPtr context,ScriptParams * inputParams)31 UScriptValuePtr ScriptFunction::Execute(ScriptInterpreter &inter,
32 UScriptContextPtr context, ScriptParams *inputParams)
33 {
34 INTERPRETER_LOGI(inter, context, "ScriptFunction execute %s", functionName_.c_str());
35 UScriptContextPtr funcContext = std::make_shared<UScriptInterpretContext>();
36 if (funcContext == nullptr) {
37 USCRIPT_LOGE("[interpreter-%d] Fail to create context for function %s",
38 inter.GetInstanceId(), functionName_.c_str());
39 return std::make_shared<ErrorValue>(USCRIPT_ERROR_CREATE_OBJ);
40 }
41
42 if (inputParams == nullptr || params_ == nullptr) {
43 if (params_ != nullptr || inputParams != nullptr) {
44 USCRIPT_LOGE("[interpreter-%d] ScriptFunction::Execute param not match %s",
45 inter.GetInstanceId(), functionName_.c_str());
46 return std::make_shared<ErrorValue>(USCRIPT_ERROR_INTERPRET);
47 }
48 } else {
49 if (params_->GetParams().size() != inputParams->GetParams().size()) {
50 USCRIPT_LOGE("[interpreter-%d] ScriptFunction::Execute param not match %s",
51 inter.GetInstanceId(), functionName_.c_str());
52 return std::make_shared<ErrorValue>(USCRIPT_ERROR_INTERPRET);
53 }
54
55 size_t index = 0;
56 std::vector<std::string> paramNames = GetParamNames(inter, context);
57 for (auto expression : inputParams->GetParams()) {
58 UScriptValuePtr var = expression->Execute(inter, context);
59 if (var == nullptr || var->GetValueType() == UScriptValue::VALUE_TYPE_ERROR) {
60 USCRIPT_LOGE("[interpreter-%d] ScriptFunction::Execute fail to computer param %s",
61 inter.GetInstanceId(), functionName_.c_str());
62 return std::make_shared<ErrorValue>(USCRIPT_NOTEXIST_INSTRUCTION);
63 }
64 if (index >= paramNames.size()) {
65 USCRIPT_LOGE("[interpreter-%d] ScriptFunction::Execute invalid index %zu param %s",
66 inter.GetInstanceId(), index, functionName_.c_str());
67 return std::make_shared<ErrorValue>(USCRIPT_NOTEXIST_INSTRUCTION);
68 }
69 funcContext->UpdateVariables(inter, var, paramNames, index);
70 }
71 }
72 UScriptStatementResult result = statements_->Execute(inter, funcContext);
73 INTERPRETER_LOGI(inter, context, "ScriptFunction execute %s result %s", functionName_.c_str(),
74 UScriptStatementResult::ScriptToString(&result).c_str());
75 return result.GetResultValue();
76 }
77
GetParamNames(ScriptInterpreter & inter,UScriptContextPtr context) const78 std::vector<std::string> ScriptFunction::GetParamNames(ScriptInterpreter &inter,
79 UScriptContextPtr context) const
80 {
81 int32_t ret;
82 std::vector<std::string> names;
83 for (auto expression : params_->GetParams()) {
84 std::string varName;
85 ret = IdentifierExpression::GetIdentifierName(expression, varName);
86 if (ret != USCRIPT_SUCCESS) {
87 INTERPRETER_LOGE(inter, context, "Fail to get param name %s", functionName_.c_str());
88 return names;
89 }
90 names.push_back(varName);
91 }
92 return names;
93 }
94 } // namespace Uscript
95