• 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 #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 namespace Uscript {
33 class Parser;
34 class Scanner;
35 class ScriptManagerImpl;
36 
37 class ScriptInterpreter {
38 public:
39     static int32_t ExecuteScript(ScriptManagerImpl *manager, Hpackage::PkgManager::StreamPtr pkgStream);
40 
41     explicit ScriptInterpreter(ScriptManagerImpl *manager);
42     ~ScriptInterpreter();
43 
44     void AddStatement(UScriptStatement *statement);
45     int32_t AddFunction(ScriptFunction *function);
46     bool IsNativeFunction(std::string name);
47     UScriptValuePtr ExecuteNativeFunc(UScriptContextPtr upContext, const std::string &name,
48         ScriptParams *params);
49     UScriptValuePtr ExecuteFunction(UScriptContextPtr context, const std::string &name,
50         ScriptParams *params);
51     UScriptValuePtr FindVariable(UScriptContextPtr local, std::string id);
52     UScriptValuePtr UpdateVariable(UScriptContextPtr local, std::string id, UScriptValuePtr var);
GetInstanceId()53     int32_t GetInstanceId() const
54     {
55         return instanceId_;
56     }
57 
ContextPush(UScriptContextPtr context)58     void ContextPush(UScriptContextPtr context)
59     {
60         contextStack_.push_back(context);
61     }
ContextPop()62     void ContextPop()
63     {
64         contextStack_.pop_back();
65     }
66 
67 private:
68     int32_t LoadScript(Hpackage::PkgManager::StreamPtr pkgStream);
69     int32_t Execute();
70 
71 private:
72     ScriptFunction* FindFunction(const std::string &name);
73     UScriptStatementList* statements_ = nullptr;
74     std::map<std::string, ScriptFunction*> functions_;
75     std::vector<UScriptContextPtr> contextStack_;
76     ScriptManagerImpl* scriptManager_ = nullptr;
77     std::unique_ptr<Parser> parser_;
78     std::unique_ptr<Scanner> scanner_;
79     int32_t instanceId_ = 0;
80 };
81 } // namespace Uscript
82 #endif
83