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 #include "LuaEngine.h"
18
19 #include <utility>
20
21 extern "C" {
22 #include "lauxlib.h"
23 #include "lualib.h"
24 }
25
26 namespace android {
27 namespace automotive {
28 namespace telemetry {
29 namespace script_executor {
30
LuaEngine()31 LuaEngine::LuaEngine() {
32 // Instantiate Lua environment
33 mLuaState = luaL_newstate();
34 luaL_openlibs(mLuaState);
35 }
36
~LuaEngine()37 LuaEngine::~LuaEngine() {
38 lua_close(mLuaState);
39 }
40
GetLuaState()41 lua_State* LuaEngine::GetLuaState() {
42 return mLuaState;
43 }
44
ResetListener(ScriptExecutorListener * listener)45 void LuaEngine::ResetListener(ScriptExecutorListener* listener) {
46 mListener.reset(listener);
47 }
48
LoadScript(const char * scriptBody)49 int LuaEngine::LoadScript(const char* scriptBody) {
50 // As the first step in Lua script execution we want to load
51 // the body of the script into Lua stack and have it processed by Lua
52 // to catch any errors.
53 // More on luaL_dostring: https://www.lua.org/manual/5.3/manual.html#lual_dostring
54 // If error, pushes the error object into the stack.
55 const auto status = luaL_dostring(mLuaState, scriptBody);
56 if (status) {
57 // Removes error object from the stack.
58 // Lua stack must be properly maintained due to its limited size,
59 // ~20 elements and its critical function because all interaction with
60 // Lua happens via the stack.
61 // Starting read about Lua stack: https://www.lua.org/pil/24.2.html
62 // TODO(b/192284232): add test case to trigger this.
63 lua_pop(mLuaState, 1);
64 }
65 return status;
66 }
67
PushFunction(const char * functionName)68 bool LuaEngine::PushFunction(const char* functionName) {
69 // Interaction between native code and Lua happens via Lua stack.
70 // In such model, a caller first pushes the name of the function
71 // that needs to be called, followed by the function's input
72 // arguments, one input value pushed at a time.
73 // More info: https://www.lua.org/pil/24.2.html
74 lua_getglobal(mLuaState, functionName);
75 const auto status = lua_isfunction(mLuaState, /*idx= */ -1);
76 // TODO(b/192284785): add test case for wrong function name in Lua.
77 if (status == 0) lua_pop(mLuaState, 1);
78 return status;
79 }
80
Run()81 int LuaEngine::Run() {
82 // Performs blocking call of the provided Lua function. Assumes all
83 // input arguments are in the Lua stack as well in proper order.
84 // On how to call Lua functions: https://www.lua.org/pil/25.2.html
85 // Doc on lua_pcall: https://www.lua.org/manual/5.3/manual.html#lua_pcall
86 // TODO(b/189241508): Once we implement publishedData parsing, nargs should
87 // change from 1 to 2.
88 // TODO(b/192284612): add test case for failed call.
89 return lua_pcall(mLuaState, /* nargs= */ 1, /* nresults= */ 0, /*errfunc= */ 0);
90 }
91
92 } // namespace script_executor
93 } // namespace telemetry
94 } // namespace automotive
95 } // namespace android
96