PILuaProgram

This commit is contained in:
2020-07-22 23:00:31 +03:00
parent 16bbcddf50
commit 6f58388d8d
6 changed files with 142 additions and 12 deletions

View File

@@ -625,6 +625,13 @@ if (NOT CROSSTOOLS)
find_package(Lua QUIET) find_package(Lua QUIET)
if (LUA_FOUND) if (LUA_FOUND)
message(STATUS "Building PIP with Lua support") message(STATUS "Building PIP with Lua support")
import_version(pip_lua pip)
set_deploy_property(pip_lua ${PIP_LIB_TYPE}
LABEL "PIP Lua support"
FULLNAME "${_PIP_DOMAIN}.pip_lua"
COMPANY "${_PIP_COMPANY}"
INFO "Platform-Independent Primitives")
make_rc(pip_lua _RC)
add_definitions(-DPIP_LUA) add_definitions(-DPIP_LUA)
include_directories(${LUA_INCLUDE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/lib/lua/3rd) include_directories(${LUA_INCLUDE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/lib/lua/3rd)
list(APPEND HDR_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/lib/lua/3rd/LuaBridge") list(APPEND HDR_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/lib/lua/3rd/LuaBridge")
@@ -672,7 +679,9 @@ if(LIB)
if (NOT CROSSTOOLS) if (NOT CROSSTOOLS)
install(FILES ${HDRS} DESTINATION ${MINGW_INCLUDE}/pip) install(FILES ${HDRS} DESTINATION ${MINGW_INCLUDE}/pip)
install(FILES ${HDRS} DESTINATION ${MINGW_INCLUDE}/pip) install(FILES ${HDRS} DESTINATION ${MINGW_INCLUDE}/pip)
if(HDR_DIRS)
install(DIRECTORY ${HDR_DIRS} DESTINATION ${MINGW_INCLUDE}/pip) install(DIRECTORY ${HDR_DIRS} DESTINATION ${MINGW_INCLUDE}/pip)
endif()
install(TARGETS ${PIP_LIBS_TARGETS} ARCHIVE DESTINATION ${MINGW_LIB}) install(TARGETS ${PIP_LIBS_TARGETS} ARCHIVE DESTINATION ${MINGW_LIB})
endif() endif()
install(TARGETS ${PIP_LIBS_TARGETS} RUNTIME DESTINATION ${MINGW_BIN}) install(TARGETS ${PIP_LIBS_TARGETS} RUNTIME DESTINATION ${MINGW_BIN})
@@ -691,8 +700,10 @@ if(LIB)
else() else()
if (NOT CROSSTOOLS) if (NOT CROSSTOOLS)
install(FILES ${HDRS} DESTINATION ${CMAKE_INSTALL_PREFIX}/include/pip) install(FILES ${HDRS} DESTINATION ${CMAKE_INSTALL_PREFIX}/include/pip)
if(HDR_DIRS)
install(DIRECTORY ${HDR_DIRS} DESTINATION ${CMAKE_INSTALL_PREFIX}/include/pip) install(DIRECTORY ${HDR_DIRS} DESTINATION ${CMAKE_INSTALL_PREFIX}/include/pip)
endif() endif()
endif()
install(TARGETS ${PIP_LIBS_TARGETS} DESTINATION ${CMAKE_INSTALL_PREFIX}/lib) install(TARGETS ${PIP_LIBS_TARGETS} DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)
endif() endif()
message(STATUS "Install ${PROJECT_NAME} to system \"${CMAKE_INSTALL_PREFIX}\"") message(STATUS "Install ${PROJECT_NAME} to system \"${CMAKE_INSTALL_PREFIX}\"")
@@ -707,7 +718,9 @@ else()
install(TARGETS ${PIP_LIBS_TARGETS} DESTINATION lib) install(TARGETS ${PIP_LIBS_TARGETS} DESTINATION lib)
endif() endif()
install(FILES ${HDRS} DESTINATION include/pip) install(FILES ${HDRS} DESTINATION include/pip)
if(HDR_DIRS)
install(DIRECTORY ${HDR_DIRS} DESTINATION include/pip) install(DIRECTORY ${HDR_DIRS} DESTINATION include/pip)
endif()
message(STATUS "Install ${PROJECT_NAME} to local \"bin\", \"lib\" and \"include\"") message(STATUS "Install ${PROJECT_NAME} to local \"bin\", \"lib\" and \"include\"")
endif() endif()
endif() endif()

View File

@@ -157,7 +157,7 @@ if(__module_Cloud AND __module_IOUtils)
set_target_properties(PIP::Cloud PROPERTIES INTERFACE_LINK_LIBRARIES "PIP::IOUtils") set_target_properties(PIP::Cloud PROPERTIES INTERFACE_LINK_LIBRARIES "PIP::IOUtils")
endif() endif()
if(__module_Lua) if(__module_Lua)
set_target_properties(PIP::Lua PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${LUA_INCLUDE_DIR} INTERFACE_LINK_LIBRARIES "PIP::Lua" ${LUA_LIBRARIES}) set_target_properties(PIP::Lua PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${LUA_INCLUDE_DIR} INTERFACE_LINK_LIBRARIES "PIP" ${LUA_LIBRARIES})
endif() endif()
include(PIPMacros) include(PIPMacros)

53
lib/lua/piluaprogram.cpp Normal file
View File

@@ -0,0 +1,53 @@
/*
PIP - Platform Independent Primitives
PILuaProgram
Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "piluaprogram.h"
PRIVATE_DEFINITION_START(PILuaProgram)
lua_State * lua_state;
PRIVATE_DEFINITION_END(PILuaProgram)
PILuaProgram::PILuaProgram() {
PRIVATE->lua_state = luaL_newstate();
luaL_openlibs(PRIVATE->lua_state);
}
bool PILuaProgram::load(const PIString & script) {
int ret = luaL_dostring(PRIVATE->lua_state, script.dataUTF8());
if (ret != 0) return false;
return true;
}
bool PILuaProgram::prepare() {
return (lua_pcall(PRIVATE->lua_state, 0, 0, 0) == 0);
}
luabridge::LuaRef PILuaProgram::getGlobal(const PIString & name) {
return luabridge::getGlobal(PRIVATE->lua_state, name.dataUTF8());
}
luabridge::Namespace PILuaProgram::getGlobalNamespace() {
return luabridge::getGlobalNamespace(PRIVATE->lua_state);
}

View File

@@ -1,3 +1,25 @@
/*! \file piluaprogram.h
* \brief Lua Program
*/
/*
PIP - Platform Independent Primitives
PILuaProgram
Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PILUAPROGRAM_H #ifndef PILUAPROGRAM_H
#define PILUAPROGRAM_H #define PILUAPROGRAM_H
@@ -6,7 +28,23 @@
class PILuaProgram class PILuaProgram
{ {
public: public:
//! Constructs an empty PILuaProgram, initialize Lua context
PILuaProgram(); PILuaProgram();
//! Load Lua script from PIString
bool load(const PIString & script);
//! Execute script
bool prepare();
//! Get Lua Object or Function
luabridge::LuaRef getGlobal(const PIString & name);
//! Return Lua global namespace
luabridge::Namespace getGlobalNamespace();
private:
PRIVATE_DECLARATION
}; };
#endif // PILUAPROGRAM_H #endif // PILUAPROGRAM_H

View File

@@ -1,3 +1,26 @@
/*! \file pip_lua.h
* \brief PIP Lua bindings
*
* This file declare conversions for PIP types via LuaBridge
*/
/*
PIP - Platform Independent Primitives
PIP Lua bindings
Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PIP_LUA_H #ifndef PIP_LUA_H
#define PIP_LUA_H #define PIP_LUA_H

View File

@@ -4,19 +4,22 @@
static const char * script static const char * script
= "-- script.lua \n" = "-- script.lua \n"
"test()\n"
"testString = \"LuaBridge works ававава!\" \n" "testString = \"LuaBridge works ававава!\" \n"
"number = 42 \n"; "number = 42 \n";
using namespace luabridge; void test() {
piCout << "C function test";
}
int main() { int main() {
lua_State* L = luaL_newstate(); PILuaProgram p;
luaL_dostring(L, script); p.getGlobalNamespace().addFunction("test", test);
luaL_openlibs(L); if (!p.load(PIString::fromUTF8(script))) piCout << "error";
lua_pcall(L, 0, 0, 0); p.prepare();
LuaRef s = getGlobal(L, "testString"); luabridge::LuaRef s = p.getGlobal("testString");
LuaRef n = getGlobal(L, "number"); luabridge::LuaRef n = p.getGlobal("number");
PIString luaString = s.cast<PIString>(); PIString luaString = s.cast<PIString>();
int answer = n.cast<int>(); int answer = n.cast<int>();
piCout << luaString; piCout << luaString;