diff --git a/CMakeLists.txt b/CMakeLists.txt index 145b35c3..aecd32ae 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -625,6 +625,13 @@ if (NOT CROSSTOOLS) find_package(Lua QUIET) if (LUA_FOUND) 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) include_directories(${LUA_INCLUDE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/lib/lua/3rd) list(APPEND HDR_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/lib/lua/3rd/LuaBridge") @@ -672,7 +679,9 @@ if(LIB) if (NOT CROSSTOOLS) install(FILES ${HDRS} DESTINATION ${MINGW_INCLUDE}/pip) install(FILES ${HDRS} DESTINATION ${MINGW_INCLUDE}/pip) - install(DIRECTORY ${HDR_DIRS} DESTINATION ${MINGW_INCLUDE}/pip) + if(HDR_DIRS) + install(DIRECTORY ${HDR_DIRS} DESTINATION ${MINGW_INCLUDE}/pip) + endif() install(TARGETS ${PIP_LIBS_TARGETS} ARCHIVE DESTINATION ${MINGW_LIB}) endif() install(TARGETS ${PIP_LIBS_TARGETS} RUNTIME DESTINATION ${MINGW_BIN}) @@ -691,7 +700,9 @@ if(LIB) else() if (NOT CROSSTOOLS) install(FILES ${HDRS} DESTINATION ${CMAKE_INSTALL_PREFIX}/include/pip) - install(DIRECTORY ${HDR_DIRS} DESTINATION ${CMAKE_INSTALL_PREFIX}/include/pip) + if(HDR_DIRS) + install(DIRECTORY ${HDR_DIRS} DESTINATION ${CMAKE_INSTALL_PREFIX}/include/pip) + endif() endif() install(TARGETS ${PIP_LIBS_TARGETS} DESTINATION ${CMAKE_INSTALL_PREFIX}/lib) endif() @@ -707,7 +718,9 @@ else() install(TARGETS ${PIP_LIBS_TARGETS} DESTINATION lib) endif() install(FILES ${HDRS} DESTINATION include/pip) - install(DIRECTORY ${HDR_DIRS} DESTINATION include/pip) + if(HDR_DIRS) + install(DIRECTORY ${HDR_DIRS} DESTINATION include/pip) + endif() message(STATUS "Install ${PROJECT_NAME} to local \"bin\", \"lib\" and \"include\"") endif() endif() diff --git a/cmake/FindPIP.cmake b/cmake/FindPIP.cmake index aa662695..910d72bd 100644 --- a/cmake/FindPIP.cmake +++ b/cmake/FindPIP.cmake @@ -156,8 +156,8 @@ endif() if(__module_Cloud AND __module_IOUtils) set_target_properties(PIP::Cloud PROPERTIES INTERFACE_LINK_LIBRARIES "PIP::IOUtils") endif() -if(__module_Lua ) - set_target_properties(PIP::Lua PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${LUA_INCLUDE_DIR} INTERFACE_LINK_LIBRARIES "PIP::Lua" ${LUA_LIBRARIES}) +if(__module_Lua) + set_target_properties(PIP::Lua PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${LUA_INCLUDE_DIR} INTERFACE_LINK_LIBRARIES "PIP" ${LUA_LIBRARIES}) endif() include(PIPMacros) diff --git a/lib/lua/piluaprogram.cpp b/lib/lua/piluaprogram.cpp new file mode 100644 index 00000000..93603e86 --- /dev/null +++ b/lib/lua/piluaprogram.cpp @@ -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 . +*/ +#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); +} + diff --git a/lib/main/lua/piluaprogram.h b/lib/main/lua/piluaprogram.h index ef80bbf7..a21b075a 100644 --- a/lib/main/lua/piluaprogram.h +++ b/lib/main/lua/piluaprogram.h @@ -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 . +*/ + #ifndef PILUAPROGRAM_H #define PILUAPROGRAM_H @@ -6,7 +28,23 @@ class PILuaProgram { public: + //! Constructs an empty PILuaProgram, initialize Lua context 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 diff --git a/lib/main/lua/pip_lua.h b/lib/main/lua/pip_lua.h index 8830d081..56a1a9a5 100644 --- a/lib/main/lua/pip_lua.h +++ b/lib/main/lua/pip_lua.h @@ -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 . +*/ #ifndef PIP_LUA_H #define PIP_LUA_H diff --git a/main.cpp b/main.cpp index 5b32984f..d78cedcf 100644 --- a/main.cpp +++ b/main.cpp @@ -4,19 +4,22 @@ static const char * script = "-- script.lua \n" + "test()\n" "testString = \"LuaBridge works ававава!\" \n" "number = 42 \n"; -using namespace luabridge; +void test() { + piCout << "C function test"; +} int main() { - lua_State* L = luaL_newstate(); - luaL_dostring(L, script); - luaL_openlibs(L); - lua_pcall(L, 0, 0, 0); - LuaRef s = getGlobal(L, "testString"); - LuaRef n = getGlobal(L, "number"); + PILuaProgram p; + p.getGlobalNamespace().addFunction("test", test); + if (!p.load(PIString::fromUTF8(script))) piCout << "error"; + p.prepare(); + luabridge::LuaRef s = p.getGlobal("testString"); + luabridge::LuaRef n = p.getGlobal("number"); PIString luaString = s.cast(); int answer = n.cast(); piCout << luaString;