blob: 1a074f2b3e105a4bff3607c0715b9ef6df6d92c7 [file] [log] [blame]
Max Dashoukba5c3fd2021-05-06 11:50:02 -07001/*
Zhomart Mukhamejanov4efd36d2021-04-19 14:22:59 -07002 * 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
Max Dashoukba5c3fd2021-05-06 11:50:02 -070017#include "LuaEngine.h"
18
19#include <utility>
20
21extern "C" {
22#include "lauxlib.h"
23#include "lualib.h"
24}
Zhomart Mukhamejanov4efd36d2021-04-19 14:22:59 -070025
26namespace android {
27namespace automotive {
28namespace telemetry {
Max Dashoukba5c3fd2021-05-06 11:50:02 -070029namespace script_executor {
Zhomart Mukhamejanov4efd36d2021-04-19 14:22:59 -070030
Max Dashouke864dfc2021-05-20 14:19:32 -070031LuaEngine::LuaEngine() {
Max Dashouk2c013db2021-06-30 17:43:54 +000032 // Instantiate Lua environment
Max Dashoukba5c3fd2021-05-06 11:50:02 -070033 mLuaState = luaL_newstate();
34 luaL_openlibs(mLuaState);
Zhomart Mukhamejanov4efd36d2021-04-19 14:22:59 -070035}
36
Max Dashoukba5c3fd2021-05-06 11:50:02 -070037LuaEngine::~LuaEngine() {
38 lua_close(mLuaState);
Zhomart Mukhamejanov4efd36d2021-04-19 14:22:59 -070039}
40
Max Dashouke864dfc2021-05-20 14:19:32 -070041lua_State* LuaEngine::GetLuaState() {
42 return mLuaState;
43}
44
Max Dashouk2c013db2021-06-30 17:43:54 +000045void LuaEngine::ResetListener(ScriptExecutorListener* listener) {
46 mListener.reset(listener);
47}
48
49int 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
68bool 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
81int 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
Max Dashoukba5c3fd2021-05-06 11:50:02 -070092} // namespace script_executor
Zhomart Mukhamejanov4efd36d2021-04-19 14:22:59 -070093} // namespace telemetry
94} // namespace automotive
95} // namespace android