• 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_basicinstruction.h"
16 #include <unistd.h>
17 #include "script_utils.h"
18 
19 using namespace Uscript;
20 using namespace std;
21 
22 namespace BasicInstruction {
Execute(Uscript::UScriptEnv & env,Uscript::UScriptContext & context)23 int32_t UScriptInstructionAbort::Execute(Uscript::UScriptEnv &env, Uscript::UScriptContext &context)
24 {
25     int32_t result = 1;
26     int32_t ret = context.GetParam(0, result);
27     USCRIPT_CHECK(ret == USCRIPT_SUCCESS, return ret, "Failed to get param");
28     return ((result == 0) ? USCRIPT_ABOART : USCRIPT_SUCCESS);
29 }
30 
Execute(Uscript::UScriptEnv & env,Uscript::UScriptContext & context)31 int32_t UScriptInstructionAssert::Execute(Uscript::UScriptEnv &env, Uscript::UScriptContext &context)
32 {
33     int32_t result = 1;
34     int32_t ret = context.GetParam(0, result);
35     USCRIPT_CHECK(ret == USCRIPT_SUCCESS, return ret, "Failed to get param");
36     return ((result == 0) ? USCRIPT_ASSERT : USCRIPT_SUCCESS);
37 }
38 
Execute(Uscript::UScriptEnv & env,Uscript::UScriptContext & context)39 int32_t UScriptInstructionSleep::Execute(Uscript::UScriptEnv &env, Uscript::UScriptContext &context)
40 {
41     int32_t seconds = 1;
42     int32_t ret = context.GetParam(0, seconds);
43     USCRIPT_CHECK(ret == USCRIPT_SUCCESS, return ret, "Failed to get param");
44     sleep(seconds);
45     return USCRIPT_SUCCESS;
46 }
47 
Execute(Uscript::UScriptEnv & env,Uscript::UScriptContext & context)48 int32_t UScriptInstructionConcat::Execute(Uscript::UScriptEnv &env, Uscript::UScriptContext &context)
49 {
50     int32_t ret = 0;
51     std::string str;
52     ret = context.GetParam(0, str);
53     USCRIPT_CHECK(ret == USCRIPT_SUCCESS, return ret, "Failed to get param");
54 
55     for (int32_t i = 1; i < context.GetParamCount(); i++) {
56         switch (context.GetParamType(i)) {
57             case UScriptContext::PARAM_TYPE_INTEGER: {
58                 int32_t v;
59                 ret = context.GetParam(i, v);
60                 USCRIPT_CHECK(ret == USCRIPT_SUCCESS, return ret, "Failed to get param");
61                 str.append(to_string(v));
62                 break;
63             }
64             case UScriptContext::PARAM_TYPE_FLOAT: {
65                 float v;
66                 ret = context.GetParam(i, v);
67                 USCRIPT_CHECK(ret == USCRIPT_SUCCESS, return ret, "Failed to get param");
68                 str.append(to_string(v));
69                 break;
70             }
71             case UScriptContext::PARAM_TYPE_STRING: {
72                 std::string v;
73                 ret = context.GetParam(i, v);
74                 USCRIPT_CHECK(ret == USCRIPT_SUCCESS, return ret, "Failed to get param");
75                 str.append(v);
76                 break;
77             }
78             default:
79                 break;
80         }
81     }
82     context.PushParam(str);
83     return USCRIPT_SUCCESS;
84 }
85 
Execute(Uscript::UScriptEnv & env,Uscript::UScriptContext & context)86 int32_t UScriptInstructionIsSubString::Execute(Uscript::UScriptEnv &env, Uscript::UScriptContext &context)
87 {
88     std::string str;
89     std::string subStr;
90     int32_t ret = context.GetParam(0, str);
91     USCRIPT_CHECK(ret == USCRIPT_SUCCESS, return ret, "Failed to get param");
92     ret = context.GetParam(1, subStr);
93     USCRIPT_CHECK(ret == USCRIPT_SUCCESS, return ret, "Failed to get param");
94     string::size_type last = str.find(subStr);
95     if (last == string::npos) {
96         context.PushParam(0);
97     } else {
98         context.PushParam(1);
99     }
100     return USCRIPT_SUCCESS;
101 }
102 
Execute(Uscript::UScriptEnv & env,Uscript::UScriptContext & context)103 int32_t UScriptInstructionStdout::Execute(Uscript::UScriptEnv &env, Uscript::UScriptContext &context)
104 {
105     int32_t ret;
106     for (int32_t i = 0; i < context.GetParamCount(); i++) {
107         if (context.GetParamType(i) == UScriptContext::PARAM_TYPE_INTEGER) {
108             int32_t v;
109             ret = context.GetParam(i, v);
110             USCRIPT_CHECK(ret == USCRIPT_SUCCESS, return ret, "Failed to get param");
111             std::cout << v << "  ";
112         } else if (context.GetParamType(i) == UScriptContext::PARAM_TYPE_FLOAT) {
113             float v;
114             ret = context.GetParam(i, v);
115             USCRIPT_CHECK(ret == USCRIPT_SUCCESS, return ret, "Failed to get param");
116             std::cout << v << "  ";
117         } else if (context.GetParamType(i) == UScriptContext::PARAM_TYPE_STRING) {
118             std::string v;
119             ret = context.GetParam(i, v);
120             USCRIPT_CHECK(ret == USCRIPT_SUCCESS, return ret, "Failed to get param");
121             std::cout << v << "  ";
122         }
123     }
124     std::cout << std::endl;
125     return USCRIPT_SUCCESS;
126 }
127 } // namespace BasicInstruction
128