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 #ifndef USCRIPT_INTERPRETER_H 16 #define USCRIPT_INTERPRETER_H 17 18 #include "script_context.h" 19 #include "script_expression.h" 20 #include "script_function.h" 21 #include "script_manager_impl.h" 22 #include "script_param.h" 23 #include "script_statement.h" 24 25 #define INTERPRETER_LOGE(inter, context, format, ...) \ 26 Logger(Updater::INFO, (__FILE_NAME__), (__LINE__), \ 27 "[INTERPRETER %d-%d]"#format, (inter).GetInstanceId(), (context)->GetContextId(), ##__VA_ARGS__) 28 #define INTERPRETER_LOGI(inter, context, format, ...) \ 29 Logger(Updater::INFO, (__FILE_NAME__), (__LINE__), \ 30 "[INTERPRETER %d-%d]"#format, (inter).GetInstanceId(), (context)->GetContextId(), ##__VA_ARGS__) 31 32 #define INTERPRETER_CHECK(inter, context, ret, statement, ...) \ 33 if (!(ret)) { \ 34 INTERPRETER_LOGE(inter, context, __VA_ARGS__); \ 35 statement; \ 36 } 37 38 namespace Uscript { 39 class Parser; 40 class Scanner; 41 class ScriptManagerImpl; 42 43 class ScriptInterpreter { 44 public: 45 static int32_t ExecuteScript(ScriptManagerImpl *manager, Hpackage::PkgManager::StreamPtr pkgStream); 46 47 explicit ScriptInterpreter(ScriptManagerImpl *manager); 48 ~ScriptInterpreter(); 49 50 void AddStatement(UScriptStatement *statement); 51 int32_t AddFunction(ScriptFunction *function); 52 bool IsNativeFunction(std::string name); 53 UScriptValuePtr ExecuteNativeFunc(UScriptContextPtr upContext, const std::string &name, 54 ScriptParams *params); 55 UScriptValuePtr ExecuteFunction(UScriptContextPtr context, const std::string &name, 56 ScriptParams *params); 57 UScriptValuePtr FindVariable(UScriptContextPtr local, std::string id); 58 UScriptValuePtr UpdateVariable(UScriptContextPtr local, std::string id, UScriptValuePtr var); GetInstanceId()59 int32_t GetInstanceId() const 60 { 61 return instanceId_; 62 } 63 ContextPush(UScriptContextPtr context)64 void ContextPush(UScriptContextPtr context) 65 { 66 contextStack_.push_back(context); 67 } ContextPop()68 void ContextPop() 69 { 70 contextStack_.pop_back(); 71 } 72 73 private: 74 int32_t LoadScript(Hpackage::PkgManager::StreamPtr pkgStream); 75 int32_t Execute(); 76 77 private: 78 ScriptFunction* FindFunction(const std::string &name); 79 UScriptStatementList* statements_ = nullptr; 80 std::map<std::string, ScriptFunction*> functions_; 81 std::vector<UScriptContextPtr> contextStack_; 82 ScriptManagerImpl* scriptManager_ = nullptr; 83 std::unique_ptr<Parser> parser_; 84 std::unique_ptr<Scanner> scanner_; 85 int32_t instanceId_ = 0; 86 }; 87 } // namespace Uscript 88 #endif 89