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