• 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_context.h"
16 #include <iostream>
17 #include <typeinfo>
18 #include <cmath>
19 #include "script_expression.h"
20 #include "script_interpreter.h"
21 #include "script_utils.h"
22 
23 using namespace std;
24 using namespace Updater;
25 
26 namespace Uscript {
27 static uint32_t g_contextId = 0;
28 
PushParam(int32_t value)29 int32_t UScriptInstructionContext::PushParam(int32_t value)
30 {
31     UScriptValuePtr valuePtr = std::make_shared<IntegerValue>(value);
32     USCRIPT_CHECK(valuePtr != nullptr, return USCRIPT_ERROR_CREATE_OBJ, "Failed to create value");
33     outParam_.push_back(valuePtr);
34     return USCRIPT_SUCCESS;
35 }
36 
PushParam(float value)37 int32_t UScriptInstructionContext::PushParam(float value)
38 {
39     UScriptValuePtr valuePtr = std::make_shared<FloatValue>(value);
40     USCRIPT_CHECK(valuePtr != nullptr, return USCRIPT_ERROR_CREATE_OBJ, "Failed to create value");
41     outParam_.push_back(valuePtr);
42     return USCRIPT_SUCCESS;
43 }
44 
PushParam(const std::string & value)45 int32_t UScriptInstructionContext::PushParam(const std::string& value)
46 {
47     UScriptValuePtr valuePtr = std::make_shared<StringValue>(value);
48     USCRIPT_CHECK(valuePtr != nullptr, return USCRIPT_ERROR_CREATE_OBJ, "Failed to create value");
49     outParam_.push_back(valuePtr);
50     return USCRIPT_SUCCESS;
51 }
52 
GetParamCount()53 int32_t UScriptInstructionContext::GetParamCount()
54 {
55     return innerParam_.size();
56 }
57 
GetParam(int32_t index,int32_t & value)58 int32_t UScriptInstructionContext::GetParam(int32_t index, int32_t &value)
59 {
60     return GetParam<int32_t, IntegerValue>(index, value);
61 }
62 
GetParam(int32_t index,float & value)63 int32_t UScriptInstructionContext::GetParam(int32_t index, float &value)
64 {
65     return GetParam<float, FloatValue>(index, value);
66 }
67 
GetParam(int32_t index,std::string & value)68 int32_t UScriptInstructionContext::GetParam(int32_t index, std::string &value)
69 {
70     return GetParam<std::string, StringValue>(index, value);
71 }
72 
73 template<class T, class TWapper>
GetParam(int32_t index,T & value)74 int32_t UScriptInstructionContext::GetParam(int32_t index, T &value)
75 {
76     if (static_cast<size_t>(index) >= this->innerParam_.size()) {
77         LOG(ERROR) << "Invalid index " << index;
78         return UScriptContext::PARAM_TYPE_INVALID;
79     }
80     // check whether TWapper's type is same with inner param's type
81     TWapper v {T {}};
82     if (innerParam_[index].get()->GetValueType() != v.GetValueType()) {
83         LOG(ERROR) << "Invalid index " << index;
84         return USCRIPT_INVALID_PARAM;
85     }
86     // then perform cast between innerparam and TWapper
87     TWapper* inter = static_cast<TWapper*>(innerParam_[index].get());
88     value = inter->GetValue();
89     return USCRIPT_SUCCESS;
90 }
91 
GetParamType(int32_t index)92 UScriptContext::ParamType UScriptInstructionContext::GetParamType(int32_t index)
93 {
94     USCRIPT_CHECK(static_cast<size_t>(index) < this->innerParam_.size(),
95         return UScriptContext::PARAM_TYPE_INVALID, "Invalid index %d", index);
96     UScriptValue::UScriptValueType type = innerParam_[index]->GetValueType();
97     return (UScriptContext::ParamType)type;
98 }
99 
AddInputParam(UScriptValuePtr value)100 int32_t UScriptInstructionContext::AddInputParam(UScriptValuePtr value)
101 {
102     innerParam_.push_back(value);
103     return USCRIPT_SUCCESS;
104 }
105 
FindVariable(const ScriptInterpreter & inter,std::string id)106 UScriptValuePtr UScriptInterpretContext::FindVariable(const ScriptInterpreter &inter, std::string id)
107 {
108     INTERPRETER_LOGI(inter, this, "FindVariable varName:%s ", id.c_str());
109     if (localVariables_.find(id) != localVariables_.end()) {
110         return localVariables_[id];
111     }
112     return nullptr;
113 }
114 
UScriptInterpretContext(bool top)115 UScriptInterpretContext::UScriptInterpretContext(bool top) : top_(top)
116 {
117     contextId_ = ++g_contextId;
118 }
119 
UpdateVariable(const ScriptInterpreter & inter,std::string id,UScriptValuePtr value)120 void UScriptInterpretContext::UpdateVariable(const ScriptInterpreter &inter, std::string id,
121     UScriptValuePtr value)
122 {
123     INTERPRETER_LOGI(inter, this, " Update varName:%s value: %s", id.c_str(),
124         UScriptValue::ScriptToString(value).c_str());
125     localVariables_[id] = value;
126 }
127 
UpdateVariables(const ScriptInterpreter & inter,UScriptValuePtr value,std::vector<std::string> ids,size_t & startIndex)128 void UScriptInterpretContext::UpdateVariables(const ScriptInterpreter &inter,
129     UScriptValuePtr value,
130     std::vector<std::string> ids,
131     size_t &startIndex)
132 {
133     if (value->GetValueType() != UScriptValue::VALUE_TYPE_LIST) {
134         USCRIPT_CHECK(startIndex < ids.size(), return, "Invalid startIndex %d", startIndex);
135         UpdateVariable(inter, ids[startIndex], value);
136         startIndex++;
137         return;
138     }
139 
140     ReturnValue* values = static_cast<ReturnValue*>(value.get());
141     for (auto out : values->GetValues()) {
142         USCRIPT_CHECK(startIndex < ids.size(), return, "Invalid startIndex %d", startIndex);
143         UpdateVariable(inter, ids[startIndex], out);
144         startIndex++;
145     }
146 }
147 
Computer(int32_t action,UScriptValuePtr value)148 UScriptValuePtr UScriptValue::Computer(int32_t action, UScriptValuePtr value)
149 {
150     return std::make_shared<ErrorValue>(USCRIPT_ERROR_INTERPRET);
151 }
152 
153 #define INTEGER_INTEGER_COMPUTER(op, rightValue) do {                                     \
154     IntegerValue* value = static_cast<IntegerValue *>((rightValue).get());                \
155     if (value == nullptr) {                                                               \
156         USCRIPT_LOGE("Failed to cast ");                                                  \
157     } else {                                                                              \
158         retValue = make_shared<IntegerValue>(this->GetValue() op value->GetValue());      \
159     }                                                                                     \
160 } while (0)
161 
162 #define INTEGER_FLOAT_MATH_COMPUTER(op, rightValue) do {                                  \
163     FloatValue* value = static_cast<FloatValue *>((rightValue).get());                    \
164     if (value == nullptr) {                                                               \
165         USCRIPT_LOGE("Failed to cast ");                                                  \
166     } else {                                                                              \
167         retValue = make_shared<FloatValue>(this->GetValue() op value->GetValue());        \
168     }                                                                                     \
169 } while (0)
170 
171 #define INTEGER_MATH_COMPUTER(op, rightValue) do { \
172     if (rightValue->GetValueType() == UScriptValue::VALUE_TYPE_INTEGER) {                 \
173         INTEGER_INTEGER_COMPUTER(op, (rightValue));                                       \
174     } else if ((rightValue)->GetValueType() == UScriptValue::VALUE_TYPE_FLOAT) {          \
175         INTEGER_FLOAT_MATH_COMPUTER(op, (rightValue));                                    \
176     }                                                                                     \
177 } while (0)
178 
179 #define INTEGER_FLOAT_LOGIC_COMPUTER(op, rightValue) do {                                 \
180     FloatValue* value = static_cast<FloatValue *>((rightValue).get());                    \
181     if (value == nullptr) {                                                               \
182         USCRIPT_LOGE("Failed to cast ");                                                  \
183     } else {                                                                              \
184         retValue = make_shared<IntegerValue>(this->GetValue() op value->GetValue());      \
185     }                                                                                     \
186 } while (0)
187 
188 #define INTEGER_LOGIC_COMPUTER(op, rightValue) do {                                       \
189     if (rightValue->GetValueType() == UScriptValue::VALUE_TYPE_INTEGER) {                 \
190         INTEGER_INTEGER_COMPUTER(op, (rightValue));                                       \
191     } else if ((rightValue)->GetValueType() == UScriptValue::VALUE_TYPE_FLOAT) {          \
192         INTEGER_FLOAT_LOGIC_COMPUTER(op, (rightValue));                                   \
193     }                                                                                     \
194 } while (0)
195 
196 #define INTEGER_INTEGER_MATH_COMPUTER_DIV(rightValue) do {                                \
197     IntegerValue* value = static_cast<IntegerValue *>((rightValue).get());                  \
198     if (value == nullptr || value->GetValue() == 0) {                                     \
199         USCRIPT_LOGE("Failed to cast ");                                                  \
200     } else {                                                                              \
201         retValue = make_shared<IntegerValue>(this->GetValue() / value->GetValue());       \
202     }                                                                                     \
203 } while (0)
204 
205 #define INTEGER_FLOAT_MATH_COMPUTER_DIV(rightValue) do {                                  \
206     FloatValue* value = static_cast<FloatValue *>((rightValue).get());                    \
207     if (value == nullptr || value->GetValue() == 0) {                                     \
208         USCRIPT_LOGE("Failed to cast ");                                                  \
209     } else {                                                                              \
210         retValue = make_shared<FloatValue>(this->GetValue() / value->GetValue());         \
211     }                                                                                     \
212 } while (0)
213 
214 #define INTEGER_MATH_COMPUTER_DIV(rightValue) do {                                        \
215     if (rightValue->GetValueType() == UScriptValue::VALUE_TYPE_INTEGER) {                 \
216         INTEGER_INTEGER_MATH_COMPUTER_DIV((rightValue));                                  \
217     } else if ((rightValue)->GetValueType() == UScriptValue::VALUE_TYPE_FLOAT) {          \
218         INTEGER_FLOAT_MATH_COMPUTER_DIV((rightValue));                                    \
219     }                                                                                     \
220 } while (0)
221 
Computer(int32_t action,UScriptValuePtr value)222 UScriptValuePtr IntegerValue::Computer(int32_t action, UScriptValuePtr value)
223 {
224     UScriptValuePtr rightValue = UScriptValue::GetRightCompluteValue(value);
225     UScriptValuePtr retValue = std::make_shared<ErrorValue>(USCRIPT_ERROR_INTERPRET);
226     USCRIPT_CHECK(rightValue != nullptr, return retValue, "Check param error");
227     switch (action) {
228         case UScriptExpression::ADD_OPERATOR: {
229             INTEGER_MATH_COMPUTER(+, rightValue);
230             break;
231         }
232         case UScriptExpression::SUB_OPERATOR: {
233             INTEGER_MATH_COMPUTER(-, rightValue);
234             break;
235         }
236         case UScriptExpression::MUL_OPERATOR: {
237             INTEGER_MATH_COMPUTER(*, rightValue);
238             break;
239         }
240         case UScriptExpression::DIV_OPERATOR: {
241             INTEGER_MATH_COMPUTER_DIV(rightValue);
242             break;
243         }
244         case UScriptExpression::GT_OPERATOR:
245             INTEGER_LOGIC_COMPUTER(>, rightValue);
246             break;
247         case UScriptExpression::GE_OPERATOR:
248             INTEGER_LOGIC_COMPUTER(>=, rightValue);
249             break;
250         case UScriptExpression::LT_OPERATOR:
251             INTEGER_LOGIC_COMPUTER(<, rightValue);
252             break;
253         case UScriptExpression::LE_OPERATOR:
254             INTEGER_LOGIC_COMPUTER(<=, rightValue);
255             break;
256         case UScriptExpression::EQ_OPERATOR:
257             INTEGER_LOGIC_COMPUTER(==, rightValue);
258             break;
259         case UScriptExpression::NE_OPERATOR:
260             INTEGER_LOGIC_COMPUTER(!=, rightValue);
261             break;
262         case UScriptExpression::AND_OPERATOR:
263             INTEGER_LOGIC_COMPUTER(&&, rightValue);
264             break;
265         case UScriptExpression::OR_OPERATOR:
266             INTEGER_LOGIC_COMPUTER(||, rightValue);
267             break;
268         default:
269             break;
270     }
271     return retValue;
272 }
273 
274 #define FLOAT_INTEGER_COMPUTER(op, rightValue) do {                                          \
275     IntegerValue* value = static_cast<IntegerValue *>((rightValue).get());                   \
276     if (value == nullptr) {                                                                  \
277         USCRIPT_LOGE("Failed to cast ");                                                     \
278     } else {                                                                                 \
279         retValue = make_shared<FloatValue>(this->GetValue() op value->GetValue());           \
280     }                                                                                        \
281 } while (0)
282 
283 #define FLOAT_FLOAT_MATH_COMPUTER(op, rightValue) do {                                       \
284     FloatValue* value = static_cast<FloatValue *>((rightValue).get());                       \
285     if (value == nullptr) {                                                                  \
286         USCRIPT_LOGE("Failed to cast ");                                                     \
287     } else {                                                                                 \
288         retValue = make_shared<FloatValue>(this->GetValue() op value->GetValue());           \
289     }                                                                                        \
290 } while (0)
291 
292 #define FLOAT_MATH_COMPUTER(op, rightValue) do {                                             \
293     if (rightValue->GetValueType() == UScriptValue::VALUE_TYPE_INTEGER) {                    \
294         FLOAT_INTEGER_COMPUTER(op, (rightValue));                                            \
295     } else if ((rightValue)->GetValueType() == UScriptValue::VALUE_TYPE_FLOAT) {             \
296         FLOAT_FLOAT_LOGIC_COMPUTER(op, (rightValue));                                        \
297     }                                                                                        \
298 } while (0)
299 
300 #define FLOAT_FLOAT_LOGIC_COMPUTER(op, rightValue) do {                                      \
301     FloatValue* value = static_cast<FloatValue *>((rightValue).get());                       \
302     if (value == nullptr) {                                                                  \
303         USCRIPT_LOGE("Failed to cast ");                                                     \
304     } else {                                                                                 \
305         retValue = make_shared<IntegerValue>(this->GetValue() op value->GetValue());         \
306     }                                                                                        \
307 } while (0)
308 
309 #define FLOAT_LOGIC_COMPUTER(op, rightValue) do {                                            \
310     if (rightValue->GetValueType() == UScriptValue::VALUE_TYPE_INTEGER) {                    \
311         FLOAT_INTEGER_COMPUTER(op, (rightValue));                                            \
312     } else if ((rightValue)->GetValueType() == UScriptValue::VALUE_TYPE_FLOAT) {             \
313         FLOAT_FLOAT_LOGIC_COMPUTER(op, (rightValue));                                        \
314     }                                                                                        \
315 } while (0)
316 
317 #define FLOAT_INTEGER_MATH_COMPUTER_DIV(rightValue) do {                                     \
318     IntegerValue* value = static_cast<IntegerValue *>((rightValue).get());                     \
319     if (value == nullptr || value->GetValue() == 0) {                                        \
320         USCRIPT_LOGE("Failed to cast ");                                                     \
321     } else {                                                                                 \
322         retValue = make_shared<FloatValue>(this->GetValue() / value->GetValue());            \
323     }                                                                                        \
324 } while (0)
325 
326 #define FLOAT_MATH_COMPUTER_DIV(rightValue) do {                                             \
327     if (rightValue->GetValueType() == UScriptValue::VALUE_TYPE_INTEGER) {                    \
328         FLOAT_INTEGER_MATH_COMPUTER_DIV((rightValue));                                       \
329     } else if ((rightValue)->GetValueType() == UScriptValue::VALUE_TYPE_FLOAT) {             \
330         INTEGER_FLOAT_MATH_COMPUTER_DIV((rightValue));                                       \
331     }                                                                                        \
332 } while (0)
333 
334 
Computer(int32_t action,UScriptValuePtr value)335 UScriptValuePtr FloatValue::Computer(int32_t action, UScriptValuePtr value)
336 {
337     UScriptValuePtr rightValue = UScriptValue::GetRightCompluteValue(value);
338     UScriptValuePtr retValue = std::make_shared<ErrorValue>(USCRIPT_ERROR_INTERPRET);
339     USCRIPT_CHECK(rightValue != nullptr, return retValue, "Check param error");
340     switch (action) {
341         case UScriptExpression::ADD_OPERATOR: {
342             FLOAT_MATH_COMPUTER(+, rightValue);
343             break;
344         }
345         case UScriptExpression::SUB_OPERATOR: {
346             FLOAT_MATH_COMPUTER(-, rightValue);
347             break;
348         }
349         case UScriptExpression::MUL_OPERATOR: {
350             FLOAT_MATH_COMPUTER(*, rightValue);
351             break;
352         }
353         case UScriptExpression::DIV_OPERATOR: {
354             FLOAT_MATH_COMPUTER_DIV(rightValue);
355             break;
356         }
357         case UScriptExpression::GT_OPERATOR:
358             FLOAT_LOGIC_COMPUTER(>, rightValue);
359             break;
360         case UScriptExpression::GE_OPERATOR:
361             FLOAT_LOGIC_COMPUTER(>=, rightValue);
362             break;
363         case UScriptExpression::LT_OPERATOR:
364             FLOAT_LOGIC_COMPUTER(<, rightValue);
365             break;
366         case UScriptExpression::LE_OPERATOR:
367             FLOAT_LOGIC_COMPUTER(<=, rightValue);
368             break;
369         case UScriptExpression::EQ_OPERATOR:
370             return make_shared<IntegerValue>(ComputerEqual(rightValue));
371         case UScriptExpression::NE_OPERATOR:
372             return make_shared<IntegerValue>(!ComputerEqual(rightValue));
373         case UScriptExpression::AND_OPERATOR:
374             FLOAT_LOGIC_COMPUTER(&&, rightValue);
375             break;
376         case UScriptExpression::OR_OPERATOR:
377             FLOAT_LOGIC_COMPUTER(||, rightValue);
378             break;
379         default:
380             break;
381     }
382     return retValue;
383 }
384 
ComputerEqual(const UScriptValuePtr rightValue) const385 bool FloatValue::ComputerEqual(const UScriptValuePtr rightValue) const
386 {
387     if (rightValue->GetValueType() == UScriptValue::VALUE_TYPE_INTEGER) {
388         IntegerValue* value = static_cast<IntegerValue*>(rightValue.get());
389         USCRIPT_CHECK(value != nullptr, return 0, "Failed to cast ");
390         float v2 = value->GetValue();
391         USCRIPT_LOGI("ComputerEqual %f   v2: %f", GetValue(), v2);
392         float diff = GetValue() - v2;
393         diff = abs(diff);
394         return diff < 0.0001f;
395     } else if (rightValue->GetValueType() == UScriptValue::VALUE_TYPE_FLOAT) {
396         FloatValue* value = static_cast<FloatValue*>(rightValue.get());
397         USCRIPT_CHECK(value != nullptr, return 0, "Failed to cast ");
398         float diff = GetValue() - value->GetValue();
399         diff = abs(diff);
400         USCRIPT_LOGI("ComputerEqual %f %f diff: %f", GetValue(), value->GetValue(), diff);
401         return diff < 0.0001f;
402     }
403     return 0;
404 }
405 
ComputerReturn(int32_t action,UScriptValuePtr rightValue,UScriptValuePtr defReturn) const406 UScriptValuePtr StringValue::ComputerReturn(int32_t action, UScriptValuePtr rightValue,
407     UScriptValuePtr defReturn) const
408 {
409     switch (action) {
410         case UScriptExpression::GT_OPERATOR: {
411             return make_shared<IntegerValue>(ComputerLogic(rightValue) > 0);
412         }
413         case UScriptExpression::GE_OPERATOR: {
414             return make_shared<IntegerValue>(ComputerLogic(rightValue) >= 0);
415         }
416         case UScriptExpression::LT_OPERATOR: {
417             return make_shared<IntegerValue>(ComputerLogic(rightValue) < 0);
418         }
419         case UScriptExpression::LE_OPERATOR: {
420             return make_shared<IntegerValue>(ComputerLogic(rightValue) <= 0);
421         }
422         case UScriptExpression::EQ_OPERATOR: {
423             return make_shared<IntegerValue>(ComputerLogic(rightValue) == 0);
424         }
425         case UScriptExpression::NE_OPERATOR: {
426             return make_shared<IntegerValue>(ComputerLogic(rightValue) != 0);
427         }
428         case UScriptExpression::AND_OPERATOR:
429             return defReturn;
430         case UScriptExpression::OR_OPERATOR:
431             return defReturn;
432         default:
433             break;
434     }
435     return std::make_shared<ErrorValue>(USCRIPT_ERROR_INTERPRET);
436 }
437 
Computer(int32_t action,UScriptValuePtr value)438 UScriptValuePtr StringValue::Computer(int32_t action, UScriptValuePtr value)
439 {
440     UScriptValuePtr rightValue = UScriptValue::GetRightCompluteValue(value);
441     UScriptValuePtr defReturn = std::make_shared<ErrorValue>(USCRIPT_ERROR_INTERPRET);
442     USCRIPT_CHECK(rightValue != nullptr, return defReturn, "Check param error");
443 
444     std::string str;
445     if (action == UScriptExpression::ADD_OPERATOR) {
446         if (rightValue->GetValueType() == UScriptValue::VALUE_TYPE_INTEGER) {
447             IntegerValue* integerValue = static_cast<IntegerValue*>(rightValue.get());
448             USCRIPT_CHECK(integerValue != nullptr, return defReturn, "Failed to cast ");
449             str.assign(this->GetValue());
450             return make_shared<StringValue>(str + to_string(integerValue->GetValue()));
451         } else if (rightValue->GetValueType() == UScriptValue::VALUE_TYPE_FLOAT) {
452             FloatValue* floatValue = static_cast<FloatValue*>(rightValue.get());
453             USCRIPT_CHECK(floatValue != nullptr, return defReturn, "Failed to cast ");
454             str.assign(this->GetValue());
455             return make_shared<StringValue>(str + to_string(floatValue->GetValue()));
456         } else {
457             StringValue* stringValue = static_cast<StringValue*>(rightValue.get());
458             USCRIPT_CHECK(stringValue != nullptr, return defReturn, "Failed to cast ");
459             str.assign(this->GetValue());
460             return make_shared<StringValue>(str + stringValue->GetValue());
461         }
462     }
463     if (rightValue->GetValueType() != UScriptValue::VALUE_TYPE_STRING) {
464         return defReturn;
465     }
466 
467     return ComputerReturn(action, rightValue, defReturn);
468 }
469 
ComputerLogic(UScriptValuePtr rightValue) const470 int32_t StringValue::ComputerLogic(UScriptValuePtr rightValue) const
471 {
472     StringValue* value = static_cast<StringValue*>(rightValue.get());
473     USCRIPT_CHECK(value != nullptr, return -1, "Failed to cast ");
474     std::string str;
475     str.assign(this->GetValue());
476     return str.compare(value->GetValue());
477 }
478 
ToString()479 std::string UScriptValue::ToString()
480 {
481     return std::string("null");
482 }
483 
Computer(int32_t action,UScriptValuePtr value)484 UScriptValuePtr ReturnValue::Computer(int32_t action, UScriptValuePtr value)
485 {
486     UScriptValuePtr defReturn = std::make_shared<ErrorValue>(USCRIPT_ERROR_INTERPRET);
487     // 只支持一个返回值时参与数值计算
488     if (values_.size() == 0 || values_.size() > 1) {
489         return defReturn;
490     }
491     return values_[0]->Computer(action, value);
492 }
493 
AddValue(const UScriptValuePtr value)494 void ReturnValue::AddValue(const UScriptValuePtr value)
495 {
496     if (value != nullptr) {
497         values_.push_back(value);
498     }
499 }
500 
AddValues(const std::vector<UScriptValuePtr> values)501 void ReturnValue::AddValues(const std::vector<UScriptValuePtr> values)
502 {
503     for (auto out : values) {
504         values_.push_back(out);
505     }
506 }
507 
GetValues() const508 std::vector<UScriptValuePtr> ReturnValue::GetValues() const
509 {
510     return values_;
511 }
512 
ToString()513 std::string IntegerValue::ToString()
514 {
515     return to_string(value_);
516 }
517 
ToString()518 std::string FloatValue::ToString()
519 {
520     return to_string(value_);
521 }
522 
ToString()523 std::string StringValue::ToString()
524 {
525     return value_;
526 }
527 
ToString()528 std::string ErrorValue::ToString()
529 {
530     return to_string(retCode_);
531 }
532 
ToString()533 std::string ReturnValue::ToString()
534 {
535     std::string str;
536     for (size_t index = 0; index < values_.size(); index++) {
537         if (values_[index]->GetValueType() != VALUE_TYPE_RETURN) {
538             str += " [" + to_string(index) + "] = " + values_[index]->ToString();
539         } else {
540             str += "error type";
541         }
542     }
543     return str;
544 }
545 
ScriptToString(UScriptValuePtr value)546 std::string UScriptValue::ScriptToString(UScriptValuePtr value)
547 {
548     if (value == nullptr) {
549         std::string str("null");
550         return str;
551     }
552     static std::map<int8_t, std::string> typsMaps = {
553         {VALUE_TYPE_INTEGER, "type: Integer "},
554         {VALUE_TYPE_FLOAT, "type: Float "},
555         {VALUE_TYPE_STRING, "type: String "},
556         {VALUE_TYPE_ERROR, "type: Error "},
557         {VALUE_TYPE_LIST, "type: List "},
558         {VALUE_TYPE_RETURN, "type: Return "}
559     };
560     std::string str = typsMaps[value->GetValueType()];
561     return str + value->ToString();
562 }
563 
GetRightCompluteValue(UScriptValuePtr rightValue)564 UScriptValuePtr UScriptValue::GetRightCompluteValue(UScriptValuePtr rightValue)
565 {
566     if (rightValue->GetValueType() == VALUE_TYPE_LIST) {
567         ReturnValue* value = static_cast<ReturnValue*>(rightValue.get());
568         std::vector<UScriptValuePtr> retValues = value->GetValues();
569         if (retValues.size() == 0 || retValues.size() > 1) {
570             return nullptr;
571         }
572         return retValues[0];
573     }
574     return rightValue;
575 }
576 } // namespace Uscript
577