• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef CPP_TELEMETRY_SCRIPT_EXECUTOR_SRC_LUAENGINE_H_
18 #define CPP_TELEMETRY_SCRIPT_EXECUTOR_SRC_LUAENGINE_H_
19 
20 #include "ScriptExecutorListener.h"
21 
22 #include <memory>
23 
24 extern "C" {
25 #include "lua.h"
26 }
27 
28 namespace android {
29 namespace automotive {
30 namespace telemetry {
31 namespace script_executor {
32 
33 // Encapsulates Lua script execution environment.
34 class LuaEngine {
35 public:
36     LuaEngine();
37 
38     virtual ~LuaEngine();
39 
40     // Returns pointer to Lua state object.
41     lua_State* GetLuaState();
42 
43     // Loads Lua script provided as scriptBody string.
44     // Returns 0 if successful. Otherwise returns non-zero Lua error code.
45     int LoadScript(const char* scriptBody);
46 
47     // Pushes a Lua function under provided name into the stack.
48     // Returns true if successful.
49     bool PushFunction(const char* functionName);
50 
51     // Invokes function with the inputs provided in the stack.
52     // Assumes that the script body has been already loaded and successully
53     // compiled and run, and all input arguments, and the function have been
54     // pushed to the stack.
55     // Returns 0 if successful. Otherwise returns non-zero Lua error code.
56     int Run();
57 
58     // Updates stored listener and destroys the previous one.
59     void ResetListener(ScriptExecutorListener* listener);
60 
61 private:
62     lua_State* mLuaState;  // owned
63 
64     std::unique_ptr<ScriptExecutorListener> mListener;
65 };
66 
67 }  // namespace script_executor
68 }  // namespace telemetry
69 }  // namespace automotive
70 }  // namespace android
71 
72 #endif  // CPP_TELEMETRY_SCRIPT_EXECUTOR_SRC_LUAENGINE_H_
73