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 USCRIPT_CHECK(funcContext != nullptr, return std::make_shared<ErrorValue>(USCRIPT_ERROR_CREATE_OBJ),
37 "[interpreter-%d] Fail to create context for function %s", inter.GetInstanceId(), functionName_.c_str());
38
39 if (inputParams == nullptr || params_ == nullptr) {
40 USCRIPT_CHECK(!(params_ != nullptr || inputParams != nullptr),
41 return std::make_shared<ErrorValue>(USCRIPT_ERROR_INTERPRET),
42 "[interpreter-%d] ScriptFunction::Execute param not match %s",
43 inter.GetInstanceId(), functionName_.c_str());
44 } else {
45 USCRIPT_CHECK(params_->GetParams().size() == params_->GetParams().size(),
46 return std::make_shared<ErrorValue>(USCRIPT_ERROR_INTERPRET),
47 "[interpreter-%d] ScriptFunction::Execute param not match %s",
48 inter.GetInstanceId(), functionName_.c_str());
49
50 size_t index = 0;
51 std::vector<std::string> paramNames = GetParamNames(inter, context);
52 for (auto expression : inputParams->GetParams()) {
53 UScriptValuePtr var = expression->Execute(inter, context);
54 USCRIPT_CHECK(!(var == nullptr || var->GetValueType() == UScriptValue::VALUE_TYPE_ERROR),
55 return std::make_shared<ErrorValue>(USCRIPT_NOTEXIST_INSTRUCTION),
56 "[interpreter-%d] ScriptFunction::Execute fail to computer param %s",
57 inter.GetInstanceId(), functionName_.c_str());
58 USCRIPT_CHECK(index < paramNames.size(), return std::make_shared<ErrorValue>(USCRIPT_NOTEXIST_INSTRUCTION),
59 "[interpreter-%d] ScriptFunction::Execute invalid index %zu param %s",
60 inter.GetInstanceId(), index, functionName_.c_str());
61 funcContext->UpdateVariables(inter, var, paramNames, index);
62 }
63 }
64 UScriptStatementResult result = statements_->Execute(inter, funcContext);
65 INTERPRETER_LOGI(inter, context, "ScriptFunction execute %s result %s", functionName_.c_str(),
66 UScriptStatementResult::ScriptToString(&result).c_str());
67 return result.GetResultValue();
68 }
69
GetParamNames(ScriptInterpreter & inter,UScriptContextPtr context) const70 std::vector<std::string> ScriptFunction::GetParamNames(ScriptInterpreter &inter,
71 UScriptContextPtr context) const
72 {
73 int32_t ret;
74 std::vector<std::string> names;
75 for (auto expression : params_->GetParams()) {
76 std::string varName;
77 ret = IdentifierExpression::GetIdentifierName(expression, varName);
78 INTERPRETER_CHECK(inter, context, ret == USCRIPT_SUCCESS, return names, "Fail to get param name %s",
79 functionName_.c_str());
80 names.push_back(varName);
81 }
82 return names;
83 }
84 } // namespace uscript
85