• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         size_t index = 0;
46         std::vector<std::string> paramNames = GetParamNames(inter, context);
47         for (auto expression : inputParams->GetParams()) {
48             UScriptValuePtr var = expression->Execute(inter, context);
49             USCRIPT_CHECK(!(var == nullptr || var->GetValueType() == UScriptValue::VALUE_TYPE_ERROR),
50                 return std::make_shared<ErrorValue>(USCRIPT_NOTEXIST_INSTRUCTION),
51                 "[interpreter-%d] ScriptFunction::Execute fail to computer param %s",
52                 inter.GetInstanceId(), functionName_.c_str());
53             USCRIPT_CHECK(index < paramNames.size(), return std::make_shared<ErrorValue>(USCRIPT_NOTEXIST_INSTRUCTION),
54                 "[interpreter-%d] ScriptFunction::Execute invalid index %zu param %s",
55                 inter.GetInstanceId(), index, functionName_.c_str());
56             funcContext->UpdateVariables(inter, var, paramNames, index);
57         }
58     }
59     UScriptStatementResult result = statements_->Execute(inter, funcContext);
60     INTERPRETER_LOGI(inter, context, "ScriptFunction execute %s result %s", functionName_.c_str(),
61         UScriptStatementResult::ScriptToString(&result).c_str());
62     return result.GetResultValue();
63 }
64 
GetParamNames(const ScriptInterpreter & inter,UScriptContextPtr context) const65 std::vector<std::string> ScriptFunction::GetParamNames(const ScriptInterpreter &inter,
66     UScriptContextPtr context) const
67 {
68     int32_t ret;
69     std::vector<std::string> names;
70     for (auto expression : params_->GetParams()) {
71         std::string varName;
72         ret = IdentifierExpression::GetIdentifierName(expression, varName);
73         if (ret != USCRIPT_SUCCESS) {
74             INTERPRETER_LOGE(inter, context, "Fail to get param name %s", functionName_.c_str());
75             return names;
76         }
77         names.push_back(varName);
78     }
79     return names;
80 }
81 } // namespace Uscript
82