1fb299fa2Sopenharmony_ci/* 2fb299fa2Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License. 5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at 6fb299fa2Sopenharmony_ci * 7fb299fa2Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fb299fa2Sopenharmony_ci * 9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and 13fb299fa2Sopenharmony_ci * limitations under the License. 14fb299fa2Sopenharmony_ci */ 15fb299fa2Sopenharmony_ci#include "script_statement.h" 16fb299fa2Sopenharmony_ci#include "script_context.h" 17fb299fa2Sopenharmony_ci#include "script_expression.h" 18fb299fa2Sopenharmony_ci#include "script_interpreter.h" 19fb299fa2Sopenharmony_ci#include "script_utils.h" 20fb299fa2Sopenharmony_ci 21fb299fa2Sopenharmony_ciusing namespace std; 22fb299fa2Sopenharmony_ci 23fb299fa2Sopenharmony_cinamespace Uscript { 24fb299fa2Sopenharmony_civoid UScriptStatementResult::UpdateStatementResult(UScriptValuePtr value) 25fb299fa2Sopenharmony_ci{ 26fb299fa2Sopenharmony_ci if (value == nullptr) { 27fb299fa2Sopenharmony_ci USCRIPT_LOGE("Invalid value"); 28fb299fa2Sopenharmony_ci SetResultType(UScriptStatementResult::STATEMENT_RESULT_TYPE_ERROR); 29fb299fa2Sopenharmony_ci SetError(USCRIPT_INVALID_PARAM); 30fb299fa2Sopenharmony_ci return; 31fb299fa2Sopenharmony_ci } 32fb299fa2Sopenharmony_ci switch (value->GetValueType()) { 33fb299fa2Sopenharmony_ci case UScriptValue::VALUE_TYPE_INTEGER: 34fb299fa2Sopenharmony_ci /* fallthrough */ 35fb299fa2Sopenharmony_ci case UScriptValue::VALUE_TYPE_FLOAT: 36fb299fa2Sopenharmony_ci /* fallthrough */ 37fb299fa2Sopenharmony_ci case UScriptValue::VALUE_TYPE_STRING: 38fb299fa2Sopenharmony_ci SetResultType(UScriptStatementResult::STATEMENT_RESULT_TYPE_NORMAL); 39fb299fa2Sopenharmony_ci SetResultValue(value); 40fb299fa2Sopenharmony_ci break; 41fb299fa2Sopenharmony_ci case UScriptValue::VALUE_TYPE_ERROR: 42fb299fa2Sopenharmony_ci SetResultType(UScriptStatementResult::STATEMENT_RESULT_TYPE_ERROR); 43fb299fa2Sopenharmony_ci SetError(USCRIPT_ERROR_INTERPRET); 44fb299fa2Sopenharmony_ci if (value->GetValueType() == UScriptValue::VALUE_TYPE_ERROR) { 45fb299fa2Sopenharmony_ci SetError(((ErrorValue*)value.get())->GetValue()); 46fb299fa2Sopenharmony_ci } 47fb299fa2Sopenharmony_ci break; 48fb299fa2Sopenharmony_ci case UScriptValue::VALUE_TYPE_LIST: 49fb299fa2Sopenharmony_ci SetResultType(UScriptStatementResult::STATEMENT_RESULT_TYPE_NORMAL); 50fb299fa2Sopenharmony_ci SetResultValue(value); 51fb299fa2Sopenharmony_ci break; 52fb299fa2Sopenharmony_ci case UScriptValue::VALUE_TYPE_RETURN: 53fb299fa2Sopenharmony_ci SetResultType(UScriptStatementResult::STATEMENT_RESULT_TYPE_RTN); 54fb299fa2Sopenharmony_ci SetResultValue(value); 55fb299fa2Sopenharmony_ci break; 56fb299fa2Sopenharmony_ci default: 57fb299fa2Sopenharmony_ci SetResultType(UScriptStatementResult::STATEMENT_RESULT_TYPE_ERROR); 58fb299fa2Sopenharmony_ci SetError(USCRIPT_INVALID_SCRIPT); 59fb299fa2Sopenharmony_ci break; 60fb299fa2Sopenharmony_ci } 61fb299fa2Sopenharmony_ci return; 62fb299fa2Sopenharmony_ci} 63fb299fa2Sopenharmony_ci 64fb299fa2Sopenharmony_cistd::string UScriptStatementResult::ScriptToString(UScriptStatementResult *result) 65fb299fa2Sopenharmony_ci{ 66fb299fa2Sopenharmony_ci std::string str; 67fb299fa2Sopenharmony_ci if (result == nullptr) { 68fb299fa2Sopenharmony_ci USCRIPT_LOGE("null value"); 69fb299fa2Sopenharmony_ci return str; 70fb299fa2Sopenharmony_ci } 71fb299fa2Sopenharmony_ci 72fb299fa2Sopenharmony_ci str.append("type: " + to_string(result->GetResultType())); 73fb299fa2Sopenharmony_ci if (result->GetResultType() == UScriptStatementResult::STATEMENT_RESULT_TYPE_ERROR) { 74fb299fa2Sopenharmony_ci str.append(" errorCode : " + std::to_string(result->GetError())); 75fb299fa2Sopenharmony_ci } else { 76fb299fa2Sopenharmony_ci str.append(" value : " + UScriptValue::ScriptToString(result->GetResultValue())); 77fb299fa2Sopenharmony_ci } 78fb299fa2Sopenharmony_ci return str; 79fb299fa2Sopenharmony_ci} 80fb299fa2Sopenharmony_ci 81fb299fa2Sopenharmony_ciUScriptStatement* UScriptStatement::CreateStatement(UScriptStatement::StatementType type) 82fb299fa2Sopenharmony_ci{ 83fb299fa2Sopenharmony_ci return new UScriptStatementCtrl(type); 84fb299fa2Sopenharmony_ci} 85fb299fa2Sopenharmony_ci 86fb299fa2Sopenharmony_ci// ExpressionStatement 87fb299fa2Sopenharmony_ciUScriptStatement* UScriptStatement::CreateExpressionStatement(UScriptExpression *expression) 88fb299fa2Sopenharmony_ci{ 89fb299fa2Sopenharmony_ci return new UScriptExpressionStatement(expression); 90fb299fa2Sopenharmony_ci} 91fb299fa2Sopenharmony_ci 92fb299fa2Sopenharmony_ci// IFStatement 93fb299fa2Sopenharmony_ciUScriptStatement* UScriptStatement::CreateIfStatement(UScriptExpression *condition, 94fb299fa2Sopenharmony_ci UScriptStatementList *list1, 95fb299fa2Sopenharmony_ci UScriptStatementList *list2, 96fb299fa2Sopenharmony_ci UScriptStatement *nextIfState) 97fb299fa2Sopenharmony_ci{ 98fb299fa2Sopenharmony_ci auto ifStatement = new(std::nothrow) UScriptIfStatement(condition, list1); 99fb299fa2Sopenharmony_ci if (ifStatement == nullptr) { 100fb299fa2Sopenharmony_ci USCRIPT_LOGE("Create if statement failed "); 101fb299fa2Sopenharmony_ci return nullptr; 102fb299fa2Sopenharmony_ci } 103fb299fa2Sopenharmony_ci ifStatement->AddFalseStatementList(list2); 104fb299fa2Sopenharmony_ci ifStatement->AddNextStatement(reinterpret_cast<UScriptIfStatement*>(nextIfState)); 105fb299fa2Sopenharmony_ci return ifStatement; 106fb299fa2Sopenharmony_ci} 107fb299fa2Sopenharmony_ci 108fb299fa2Sopenharmony_ci// FORStatement 109fb299fa2Sopenharmony_ciUScriptStatement* UScriptStatement::CreateForStatement(UScriptExpression *before, 110fb299fa2Sopenharmony_ci UScriptExpression *condition, 111fb299fa2Sopenharmony_ci UScriptExpression *after, 112fb299fa2Sopenharmony_ci UScriptStatementList *list) 113fb299fa2Sopenharmony_ci{ 114fb299fa2Sopenharmony_ci return new UScriptForStatement(before, condition, after, list); 115fb299fa2Sopenharmony_ci} 116fb299fa2Sopenharmony_ci 117fb299fa2Sopenharmony_ciUScriptStatement* UScriptStatement::CreateWhileStatement(UScriptExpression *condition, 118fb299fa2Sopenharmony_ci UScriptStatementList *list) 119fb299fa2Sopenharmony_ci{ 120fb299fa2Sopenharmony_ci return new UScriptWhileStatement(condition, list); 121fb299fa2Sopenharmony_ci} 122fb299fa2Sopenharmony_ci 123fb299fa2Sopenharmony_ciUScriptStatementList* UScriptStatementList::CreateInstance(UScriptStatement *statement) 124fb299fa2Sopenharmony_ci{ 125fb299fa2Sopenharmony_ci auto list = new(std::nothrow) UScriptStatementList(); 126fb299fa2Sopenharmony_ci if (list == nullptr) { 127fb299fa2Sopenharmony_ci USCRIPT_LOGE("Failed to create statement list "); 128fb299fa2Sopenharmony_ci return nullptr; 129fb299fa2Sopenharmony_ci } 130fb299fa2Sopenharmony_ci list->AddScriptStatement(statement); 131fb299fa2Sopenharmony_ci return list; 132fb299fa2Sopenharmony_ci} 133fb299fa2Sopenharmony_ci 134fb299fa2Sopenharmony_ciUScriptStatementList::~UScriptStatementList() 135fb299fa2Sopenharmony_ci{ 136fb299fa2Sopenharmony_ci for (auto iter = statements_.begin(); iter != statements_.end();) { 137fb299fa2Sopenharmony_ci delete *iter; 138fb299fa2Sopenharmony_ci iter = statements_.erase(iter); 139fb299fa2Sopenharmony_ci } 140fb299fa2Sopenharmony_ci statements_.clear(); 141fb299fa2Sopenharmony_ci} 142fb299fa2Sopenharmony_ci 143fb299fa2Sopenharmony_civoid UScriptStatementList::AddScriptStatement(UScriptStatement *statement) 144fb299fa2Sopenharmony_ci{ 145fb299fa2Sopenharmony_ci statements_.push_back(statement); 146fb299fa2Sopenharmony_ci} 147fb299fa2Sopenharmony_ci 148fb299fa2Sopenharmony_ciUScriptStatementResult UScriptStatementCtrl::Execute(ScriptInterpreter &interpreter, UScriptContextPtr context) 149fb299fa2Sopenharmony_ci{ 150fb299fa2Sopenharmony_ci UScriptStatementResult result; 151fb299fa2Sopenharmony_ci switch (GetType()) { 152fb299fa2Sopenharmony_ci case STATEMENT_TYPE_BREAK: 153fb299fa2Sopenharmony_ci result.SetResultType(UScriptStatementResult::STATEMENT_RESULT_TYPE_BREAK); 154fb299fa2Sopenharmony_ci break; 155fb299fa2Sopenharmony_ci case STATEMENT_TYPE_RTN: 156fb299fa2Sopenharmony_ci result.SetResultType(UScriptStatementResult::STATEMENT_RESULT_TYPE_RTN); 157fb299fa2Sopenharmony_ci break; 158fb299fa2Sopenharmony_ci case STATEMENT_TYPE_CONTINUE: 159fb299fa2Sopenharmony_ci result.SetResultType(UScriptStatementResult::STATEMENT_RESULT_TYPE_CONTINUE); 160fb299fa2Sopenharmony_ci break; 161fb299fa2Sopenharmony_ci default: 162fb299fa2Sopenharmony_ci result.SetResultType(UScriptStatementResult::STATEMENT_RESULT_TYPE_ERROR); 163fb299fa2Sopenharmony_ci result.SetError(USCRIPT_INVALID_STATEMENT); 164fb299fa2Sopenharmony_ci break; 165fb299fa2Sopenharmony_ci } 166fb299fa2Sopenharmony_ci INTERPRETER_LOGI(interpreter, context, "UScriptStatementList::statement result :%s", 167fb299fa2Sopenharmony_ci UScriptStatementResult::ScriptToString(&result).c_str()); 168fb299fa2Sopenharmony_ci return result; 169fb299fa2Sopenharmony_ci} 170fb299fa2Sopenharmony_ci 171fb299fa2Sopenharmony_ciUScriptStatementResult UScriptExpressionStatement::Execute(ScriptInterpreter &interpreter, 172fb299fa2Sopenharmony_ci UScriptContextPtr context) 173fb299fa2Sopenharmony_ci{ 174fb299fa2Sopenharmony_ci UScriptStatementResult result(UScriptStatementResult::STATEMENT_RESULT_TYPE_NORMAL, nullptr); 175fb299fa2Sopenharmony_ci INTERPRETER_LOGD(interpreter, context, "UScriptExpressionStatement::statement "); 176fb299fa2Sopenharmony_ci UScriptValuePtr value = expression_->Execute(interpreter, context); 177fb299fa2Sopenharmony_ci result.UpdateStatementResult(value); 178fb299fa2Sopenharmony_ci INTERPRETER_LOGD(interpreter, context, "UScriptExpressionStatement::Execute result: %s", 179fb299fa2Sopenharmony_ci UScriptStatementResult::ScriptToString(&result).c_str()); 180fb299fa2Sopenharmony_ci return result; 181fb299fa2Sopenharmony_ci} 182fb299fa2Sopenharmony_ci 183fb299fa2Sopenharmony_ciUScriptStatementResult UScriptForStatement::Execute(ScriptInterpreter &interpreter, UScriptContextPtr context) 184fb299fa2Sopenharmony_ci{ 185fb299fa2Sopenharmony_ci INTERPRETER_LOGI(interpreter, context, "UScriptForStatement::statement "); 186fb299fa2Sopenharmony_ci UScriptStatementResult result(UScriptStatementResult::STATEMENT_RESULT_TYPE_NORMAL, nullptr); 187fb299fa2Sopenharmony_ci if (before_ != nullptr) { 188fb299fa2Sopenharmony_ci INTERPRETER_LOGE(interpreter, context, "Execute before"); 189fb299fa2Sopenharmony_ci before_->Execute(interpreter, context); 190fb299fa2Sopenharmony_ci } 191fb299fa2Sopenharmony_ci 192fb299fa2Sopenharmony_ci while (1) { 193fb299fa2Sopenharmony_ci if (condition_ != nullptr) { 194fb299fa2Sopenharmony_ci UScriptValuePtr v = condition_->Execute(interpreter, context); 195fb299fa2Sopenharmony_ci if (v == nullptr || v->GetValueType() == UScriptValue::VALUE_TYPE_ERROR) { 196fb299fa2Sopenharmony_ci INTERPRETER_LOGE(interpreter, context, "Execute for condition failed: %s", 197fb299fa2Sopenharmony_ci UScriptValue::ScriptToString(v).c_str()); 198fb299fa2Sopenharmony_ci result.SetResultType(UScriptStatementResult::STATEMENT_RESULT_TYPE_ERROR); 199fb299fa2Sopenharmony_ci result.SetError(USCRIPT_INVALID_PARAM); 200fb299fa2Sopenharmony_ci return result; 201fb299fa2Sopenharmony_ci } 202fb299fa2Sopenharmony_ci if (!v->IsTrue()) { 203fb299fa2Sopenharmony_ci break; 204fb299fa2Sopenharmony_ci } 205fb299fa2Sopenharmony_ci } 206fb299fa2Sopenharmony_ci UScriptStatementResult centerResult = statements_->Execute(interpreter, context); 207fb299fa2Sopenharmony_ci INTERPRETER_LOGI(interpreter, context, "Execute statements result %s ", 208fb299fa2Sopenharmony_ci UScriptStatementResult::ScriptToString(¢erResult).c_str()); 209fb299fa2Sopenharmony_ci if (centerResult.GetResultType() == UScriptStatementResult::STATEMENT_RESULT_TYPE_BREAK) { 210fb299fa2Sopenharmony_ci break; 211fb299fa2Sopenharmony_ci } 212fb299fa2Sopenharmony_ci if (centerResult.GetResultType() == UScriptStatementResult::STATEMENT_RESULT_TYPE_RTN || 213fb299fa2Sopenharmony_ci centerResult.GetResultType() == UScriptStatementResult::STATEMENT_RESULT_TYPE_ERROR) { 214fb299fa2Sopenharmony_ci return centerResult; 215fb299fa2Sopenharmony_ci } 216fb299fa2Sopenharmony_ci 217fb299fa2Sopenharmony_ci if (after_ != nullptr) { 218fb299fa2Sopenharmony_ci INTERPRETER_LOGI(interpreter, context, "Execute after"); 219fb299fa2Sopenharmony_ci after_->Execute(interpreter, context); 220fb299fa2Sopenharmony_ci } 221fb299fa2Sopenharmony_ci } 222fb299fa2Sopenharmony_ci return result; 223fb299fa2Sopenharmony_ci} 224fb299fa2Sopenharmony_ci 225fb299fa2Sopenharmony_ciUScriptStatementResult UScriptWhileStatement::Execute(ScriptInterpreter &interpreter, UScriptContextPtr context) 226fb299fa2Sopenharmony_ci{ 227fb299fa2Sopenharmony_ci INTERPRETER_LOGI(interpreter, context, "UScriptStatementResult::statement "); 228fb299fa2Sopenharmony_ci UScriptStatementResult result(UScriptStatementResult::STATEMENT_RESULT_TYPE_NORMAL, nullptr); 229fb299fa2Sopenharmony_ci while (1) { 230fb299fa2Sopenharmony_ci if (condition_ != nullptr) { 231fb299fa2Sopenharmony_ci UScriptValuePtr v = condition_->Execute(interpreter, context); 232fb299fa2Sopenharmony_ci if (v == nullptr || v->GetValueType() == UScriptValue::VALUE_TYPE_ERROR) { 233fb299fa2Sopenharmony_ci INTERPRETER_LOGE(interpreter, context, "Execute while condition failed: %s", 234fb299fa2Sopenharmony_ci UScriptValue::ScriptToString(v).c_str()); 235fb299fa2Sopenharmony_ci result.SetResultType(UScriptStatementResult::STATEMENT_RESULT_TYPE_ERROR); 236fb299fa2Sopenharmony_ci result.SetError(USCRIPT_INVALID_PARAM); 237fb299fa2Sopenharmony_ci return result; 238fb299fa2Sopenharmony_ci } 239fb299fa2Sopenharmony_ci if (!v->IsTrue()) { 240fb299fa2Sopenharmony_ci break; 241fb299fa2Sopenharmony_ci } 242fb299fa2Sopenharmony_ci } 243fb299fa2Sopenharmony_ci UScriptStatementResult centerResult = statements_->Execute(interpreter, context); 244fb299fa2Sopenharmony_ci INTERPRETER_LOGI(interpreter, context, "Execute statements result %s ", 245fb299fa2Sopenharmony_ci UScriptStatementResult::ScriptToString(¢erResult).c_str()); 246fb299fa2Sopenharmony_ci if (centerResult.GetResultType() == UScriptStatementResult::STATEMENT_RESULT_TYPE_BREAK) { 247fb299fa2Sopenharmony_ci break; 248fb299fa2Sopenharmony_ci } 249fb299fa2Sopenharmony_ci if (centerResult.GetResultType() == UScriptStatementResult::STATEMENT_RESULT_TYPE_CONTINUE) { 250fb299fa2Sopenharmony_ci continue; 251fb299fa2Sopenharmony_ci } 252fb299fa2Sopenharmony_ci if (centerResult.GetResultType() == UScriptStatementResult::STATEMENT_RESULT_TYPE_RTN || 253fb299fa2Sopenharmony_ci centerResult.GetResultType() == UScriptStatementResult::STATEMENT_RESULT_TYPE_ERROR) { 254fb299fa2Sopenharmony_ci return centerResult; 255fb299fa2Sopenharmony_ci } 256fb299fa2Sopenharmony_ci } 257fb299fa2Sopenharmony_ci return result; 258fb299fa2Sopenharmony_ci} 259fb299fa2Sopenharmony_ci 260fb299fa2Sopenharmony_ciUScriptStatementResult UScriptIfStatement::Execute(ScriptInterpreter &interpreter, UScriptContextPtr context) 261fb299fa2Sopenharmony_ci{ 262fb299fa2Sopenharmony_ci UScriptStatementResult result(UScriptStatementResult::STATEMENT_RESULT_TYPE_NORMAL, nullptr); 263fb299fa2Sopenharmony_ci UScriptValuePtr v = expression_->Execute(interpreter, context); 264fb299fa2Sopenharmony_ci if (v == nullptr || v->GetValueType() == UScriptValue::VALUE_TYPE_ERROR) { 265fb299fa2Sopenharmony_ci INTERPRETER_LOGE(interpreter, context, "Execute for condition failed: %s", 266fb299fa2Sopenharmony_ci UScriptValue::ScriptToString(v).c_str()); 267fb299fa2Sopenharmony_ci result.SetResultType(UScriptStatementResult::STATEMENT_RESULT_TYPE_ERROR); 268fb299fa2Sopenharmony_ci result.SetError(USCRIPT_INVALID_PARAM); 269fb299fa2Sopenharmony_ci return result; 270fb299fa2Sopenharmony_ci } 271fb299fa2Sopenharmony_ci 272fb299fa2Sopenharmony_ci if (v->IsTrue()) { 273fb299fa2Sopenharmony_ci if (trueStatements_ == nullptr) { 274fb299fa2Sopenharmony_ci return result; 275fb299fa2Sopenharmony_ci } 276fb299fa2Sopenharmony_ci UScriptContextPtr local = std::make_shared<UScriptInterpretContext>(); 277fb299fa2Sopenharmony_ci return trueStatements_->Execute(interpreter, local); 278fb299fa2Sopenharmony_ci } else if (falseStatements_ != nullptr) { 279fb299fa2Sopenharmony_ci UScriptContextPtr local = std::make_shared<UScriptInterpretContext>(); 280fb299fa2Sopenharmony_ci return falseStatements_->Execute(interpreter, local); 281fb299fa2Sopenharmony_ci } else if (nextStatement_ != nullptr) { 282fb299fa2Sopenharmony_ci return nextStatement_->Execute(interpreter, context); 283fb299fa2Sopenharmony_ci } 284fb299fa2Sopenharmony_ci return result; 285fb299fa2Sopenharmony_ci} 286fb299fa2Sopenharmony_ci 287fb299fa2Sopenharmony_ciUScriptStatementResult UScriptStatementList::Execute(ScriptInterpreter &interpreter, UScriptContextPtr context) 288fb299fa2Sopenharmony_ci{ 289fb299fa2Sopenharmony_ci INTERPRETER_LOGD(interpreter, context, "UScriptStatementList::Execute "); 290fb299fa2Sopenharmony_ci interpreter.ContextPush(context); 291fb299fa2Sopenharmony_ci UScriptStatementResult result(UScriptStatementResult::STATEMENT_RESULT_TYPE_NORMAL, nullptr); 292fb299fa2Sopenharmony_ci for (auto statement : statements_) { 293fb299fa2Sopenharmony_ci result = statement->Execute(interpreter, context); 294fb299fa2Sopenharmony_ci if (result.GetResultType() == UScriptStatementResult::STATEMENT_RESULT_TYPE_BREAK) { 295fb299fa2Sopenharmony_ci break; 296fb299fa2Sopenharmony_ci } else if (result.GetResultType() == UScriptStatementResult::STATEMENT_RESULT_TYPE_CONTINUE) { 297fb299fa2Sopenharmony_ci break; 298fb299fa2Sopenharmony_ci } else if (result.GetResultType() == UScriptStatementResult::STATEMENT_RESULT_TYPE_RTN) { 299fb299fa2Sopenharmony_ci break; 300fb299fa2Sopenharmony_ci } else if (result.GetResultType() == UScriptStatementResult::STATEMENT_RESULT_TYPE_ERROR) { 301fb299fa2Sopenharmony_ci break; 302fb299fa2Sopenharmony_ci } 303fb299fa2Sopenharmony_ci } 304fb299fa2Sopenharmony_ci interpreter.ContextPop(); 305fb299fa2Sopenharmony_ci INTERPRETER_LOGD(interpreter, context, "UScriptStatementList finish %s", 306fb299fa2Sopenharmony_ci UScriptStatementResult::ScriptToString(&result).c_str()); 307fb299fa2Sopenharmony_ci return result; 308fb299fa2Sopenharmony_ci} 309fb299fa2Sopenharmony_ci 310fb299fa2Sopenharmony_ciUScriptExpressionStatement::~UScriptExpressionStatement() 311fb299fa2Sopenharmony_ci{ 312fb299fa2Sopenharmony_ci delete expression_; 313fb299fa2Sopenharmony_ci} 314fb299fa2Sopenharmony_ci 315fb299fa2Sopenharmony_ciUScriptIfStatement::~UScriptIfStatement() 316fb299fa2Sopenharmony_ci{ 317fb299fa2Sopenharmony_ci delete expression_; 318fb299fa2Sopenharmony_ci delete trueStatements_; 319fb299fa2Sopenharmony_ci delete falseStatements_; 320fb299fa2Sopenharmony_ci delete nextStatement_; 321fb299fa2Sopenharmony_ci} 322fb299fa2Sopenharmony_ci 323fb299fa2Sopenharmony_ciUScriptForStatement::~UScriptForStatement() 324fb299fa2Sopenharmony_ci{ 325fb299fa2Sopenharmony_ci delete before_; 326fb299fa2Sopenharmony_ci delete condition_; 327fb299fa2Sopenharmony_ci delete after_; 328fb299fa2Sopenharmony_ci delete statements_; 329fb299fa2Sopenharmony_ci} 330fb299fa2Sopenharmony_ci 331fb299fa2Sopenharmony_ciUScriptWhileStatement::~UScriptWhileStatement() 332fb299fa2Sopenharmony_ci{ 333fb299fa2Sopenharmony_ci delete condition_; 334fb299fa2Sopenharmony_ci delete statements_; 335fb299fa2Sopenharmony_ci} 336fb299fa2Sopenharmony_ci 337fb299fa2Sopenharmony_ci 338fb299fa2Sopenharmony_ciUScriptReturnStatement::~UScriptReturnStatement() 339fb299fa2Sopenharmony_ci{ 340fb299fa2Sopenharmony_ci delete params_; 341fb299fa2Sopenharmony_ci} 342fb299fa2Sopenharmony_ci 343fb299fa2Sopenharmony_ciUScriptReturnStatement* UScriptReturnStatement::CreateStatement(ScriptParams *params) 344fb299fa2Sopenharmony_ci{ 345fb299fa2Sopenharmony_ci auto statement = new(std::nothrow) UScriptReturnStatement(); 346fb299fa2Sopenharmony_ci if (statement == nullptr) { 347fb299fa2Sopenharmony_ci USCRIPT_LOGE("Create statement failed"); 348fb299fa2Sopenharmony_ci return nullptr; 349fb299fa2Sopenharmony_ci } 350fb299fa2Sopenharmony_ci if (params != nullptr) { 351fb299fa2Sopenharmony_ci statement->AddParams(params); 352fb299fa2Sopenharmony_ci } 353fb299fa2Sopenharmony_ci return statement; 354fb299fa2Sopenharmony_ci} 355fb299fa2Sopenharmony_ci 356fb299fa2Sopenharmony_ciUScriptStatementResult UScriptReturnStatement::Execute(ScriptInterpreter &interpreter, UScriptContextPtr context) 357fb299fa2Sopenharmony_ci{ 358fb299fa2Sopenharmony_ci UScriptStatementResult result(UScriptStatementResult::STATEMENT_RESULT_TYPE_RTN, nullptr); 359fb299fa2Sopenharmony_ci if (params_ == nullptr) { 360fb299fa2Sopenharmony_ci USCRIPT_LOGE("Invalid parm"); 361fb299fa2Sopenharmony_ci return result; 362fb299fa2Sopenharmony_ci } 363fb299fa2Sopenharmony_ci 364fb299fa2Sopenharmony_ci std::shared_ptr<ReturnValue> retValue = std::make_shared<ReturnValue>(); 365fb299fa2Sopenharmony_ci if (retValue == nullptr) { 366fb299fa2Sopenharmony_ci USCRIPT_LOGE("Create ret value failed"); 367fb299fa2Sopenharmony_ci return result; 368fb299fa2Sopenharmony_ci } 369fb299fa2Sopenharmony_ci for (auto id : params_->GetParams()) { 370fb299fa2Sopenharmony_ci UScriptValuePtr var = id->Execute(interpreter, context); 371fb299fa2Sopenharmony_ci INTERPRETER_LOGI(interpreter, context, "params result: %s", UScriptValue::ScriptToString(var).c_str()); 372fb299fa2Sopenharmony_ci if (var->GetValueType() == UScriptValue::VALUE_TYPE_LIST) { 373fb299fa2Sopenharmony_ci retValue->AddValues(((ReturnValue*)var.get())->GetValues()); 374fb299fa2Sopenharmony_ci } else { 375fb299fa2Sopenharmony_ci retValue->AddValue(var); 376fb299fa2Sopenharmony_ci } 377fb299fa2Sopenharmony_ci } 378fb299fa2Sopenharmony_ci result.SetResultType(UScriptStatementResult::STATEMENT_RESULT_TYPE_RTN); 379fb299fa2Sopenharmony_ci result.SetResultValue(retValue); 380fb299fa2Sopenharmony_ci return result; 381fb299fa2Sopenharmony_ci} 382fb299fa2Sopenharmony_ci} // namespace Uscript 383