PILuaProgram
This commit is contained in:
@@ -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)
|
||||
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,8 +700,10 @@ if(LIB)
|
||||
else()
|
||||
if (NOT CROSSTOOLS)
|
||||
install(FILES ${HDRS} 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()
|
||||
message(STATUS "Install ${PROJECT_NAME} to system \"${CMAKE_INSTALL_PREFIX}\"")
|
||||
@@ -707,7 +718,9 @@ else()
|
||||
install(TARGETS ${PIP_LIBS_TARGETS} DESTINATION lib)
|
||||
endif()
|
||||
install(FILES ${HDRS} 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()
|
||||
|
||||
@@ -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)
|
||||
|
||||
53
lib/lua/piluaprogram.cpp
Normal file
53
lib/lua/piluaprogram.cpp
Normal 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);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
#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
|
||||
|
||||
@@ -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
|
||||
#define PIP_LUA_H
|
||||
|
||||
|
||||
17
main.cpp
17
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<PIString>();
|
||||
int answer = n.cast<int>();
|
||||
piCout << luaString;
|
||||
|
||||
Reference in New Issue
Block a user