PIP Lua Module

This commit is contained in:
2020-07-22 22:20:54 +03:00
parent b018ea9e07
commit 16bbcddf50
28 changed files with 82 additions and 49 deletions

View File

@@ -67,6 +67,7 @@ set(PIP_SRC_OPENCL "lib/opencl")
set(PIP_SRC_IO_UTILS "lib/io_utils") set(PIP_SRC_IO_UTILS "lib/io_utils")
set(PIP_SRC_CONCURRENT "lib/concurrent") set(PIP_SRC_CONCURRENT "lib/concurrent")
set(PIP_SRC_CLOUD "lib/cloud") set(PIP_SRC_CLOUD "lib/cloud")
set(PIP_SRC_LUA "lib/lua")
set(PIP_SRC_DIRS ${PIP_SRC_MAIN} set(PIP_SRC_DIRS ${PIP_SRC_MAIN}
${PIP_SRC_CONSOLE} ${PIP_SRC_CONSOLE}
${PIP_SRC_CRYPT} ${PIP_SRC_CRYPT}
@@ -83,6 +84,7 @@ set(LIBS_MAIN)
set(LIBS_STATUS) set(LIBS_STATUS)
set(HDRS) set(HDRS)
set(PHDRS) set(PHDRS)
set(HDR_DIRS)
if (TESTS) if (TESTS)
include(DownloadGTest) include(DownloadGTest)
@@ -178,7 +180,7 @@ get_filename_component(C_COMPILER "${CMAKE_C_COMPILER}" NAME)
# Sources # Sources
# Main lib # Main lib
set(PIP_FOLDERS "." "core" "containers" "thread" "system" "io_devices" "io_utils" "console" "math" "code" "geo" "resources" "opencl" "crypt" "introspection" "concurrent" "cloud") set(PIP_FOLDERS "." "core" "containers" "thread" "system" "io_devices" "io_utils" "console" "math" "code" "geo" "resources" "opencl" "crypt" "introspection" "concurrent" "cloud" "lua")
include_directories("${PIP_SRC_MAIN}") include_directories("${PIP_SRC_MAIN}")
set(PIP_MAIN_FOLDERS) set(PIP_MAIN_FOLDERS)
foreach(F ${PIP_FOLDERS}) foreach(F ${PIP_FOLDERS})
@@ -215,6 +217,9 @@ gather_src("${PIP_CONCURRENT_TEST}" CPP_CONCURRENT_TEST HDRS PHDRS)
# Cloud lib # Cloud lib
gather_src("${PIP_SRC_CLOUD}" CPP_LIB_CLOUD HDRS PHDRS) gather_src("${PIP_SRC_CLOUD}" CPP_LIB_CLOUD HDRS PHDRS)
# LUA lib
gather_src("${PIP_SRC_LUA}" CPP_LIB_LUA HDRS PHDRS)
if(PIP_FREERTOS) if(PIP_FREERTOS)
add_definitions(-DPIP_FREERTOS) add_definitions(-DPIP_FREERTOS)
set(ICU OFF) set(ICU OFF)
@@ -621,7 +626,11 @@ if (NOT CROSSTOOLS)
if (LUA_FOUND) if (LUA_FOUND)
message(STATUS "Building PIP with Lua support") message(STATUS "Building PIP with Lua support")
add_definitions(-DPIP_LUA) add_definitions(-DPIP_LUA)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/3rd_party/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")
add_library(pip_lua ${PIP_LIB_TYPE} ${CPP_LIB_LUA} ${_RC})
target_link_libraries(pip_lua pip ${LUA_LIBRARIES})
list(APPEND PIP_LIBS_TARGETS pip_lua)
endif() endif()
# Test program # Test program
@@ -629,7 +638,7 @@ if (NOT CROSSTOOLS)
add_executable(pip_test "main.cpp") add_executable(pip_test "main.cpp")
target_link_libraries(pip_test pip) target_link_libraries(pip_test pip)
if (LUA_FOUND) if (LUA_FOUND)
target_link_libraries(pip_test ${LUA_LIBRARIES}) target_link_libraries(pip_test pip_lua ${LUA_LIBRARIES})
endif() endif()
endif() endif()
@@ -662,6 +671,8 @@ if(LIB)
if(MINGW) if(MINGW)
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(DIRECTORY ${HDR_DIRS} DESTINATION ${MINGW_INCLUDE}/pip)
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})
@@ -680,6 +691,7 @@ 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)
install(DIRECTORY ${HDR_DIRS} DESTINATION ${CMAKE_INSTALL_PREFIX}/include/pip)
endif() endif()
install(TARGETS ${PIP_LIBS_TARGETS} DESTINATION ${CMAKE_INSTALL_PREFIX}/lib) install(TARGETS ${PIP_LIBS_TARGETS} DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)
endif() endif()
@@ -695,6 +707,7 @@ 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)
install(DIRECTORY ${HDR_DIRS} DESTINATION include/pip)
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

@@ -10,6 +10,7 @@ Also create imported targets:
* PIP::IOUtils * PIP::IOUtils
* PIP::Concurrent * PIP::Concurrent
* PIP::Cloud * PIP::Cloud
* PIP::Lua
These targets include directories and depends on These targets include directories and depends on
main library main library
@@ -66,6 +67,7 @@ find_library(PIP_COMPRESS_LIBRARY pip_compress${_pip_suffix} HINTS ${_PIP_LIBDIR
find_library(PIP_IO_UTILS_LIBRARY pip_io_utils${_pip_suffix} HINTS ${_PIP_LIBDIR}) find_library(PIP_IO_UTILS_LIBRARY pip_io_utils${_pip_suffix} HINTS ${_PIP_LIBDIR})
find_library(PIP_CONCURRENT_LIBRARY pip_concurrent${_pip_suffix} HINTS ${_PIP_LIBDIR}) find_library(PIP_CONCURRENT_LIBRARY pip_concurrent${_pip_suffix} HINTS ${_PIP_LIBDIR})
find_library(PIP_CLOUD_LIBRARY pip_cloud HINTS${_pip_suffix} ${_PIP_LIBDIR}) find_library(PIP_CLOUD_LIBRARY pip_cloud HINTS${_pip_suffix} ${_PIP_LIBDIR})
find_library(PIP_LUA_LIBRARY pip_lua HINTS${_pip_suffix} ${_PIP_LIBDIR})
find_file(PIP_H_INCLUDE "pip.h" HINTS ${_PIP_INCDIR} $ENV{SMSDK_DIR}/include/pip) find_file(PIP_H_INCLUDE "pip.h" HINTS ${_PIP_INCDIR} $ENV{SMSDK_DIR}/include/pip)
get_filename_component(PIP_INCLUDES ${PIP_H_INCLUDE} PATH) get_filename_component(PIP_INCLUDES ${PIP_H_INCLUDE} PATH)
set(__ext "") set(__ext "")
@@ -94,6 +96,12 @@ if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
list(APPEND PIP_LIBRARY ${_PIP_ADD_LIBS_}) list(APPEND PIP_LIBRARY ${_PIP_ADD_LIBS_})
endif() endif()
endif() endif()
if(PIP_LUA_LIBRARY)
if(MINGW)
set(LUA_INCLUDE_DIR ${MINGW_INCLUDE})
endif()
find_package(Lua QUIET)
endif()
if(NOT PIP_VERSION) if(NOT PIP_VERSION)
include(CheckSymbolExists) include(CheckSymbolExists)
@@ -117,7 +125,7 @@ if(PIP_FIND_VERSION VERSION_GREATER PIP_VERSION)
message(FATAL_ERROR "PIP version ${PIP_VERSION} is available, but ${PIP_FIND_VERSION} requested!") message(FATAL_ERROR "PIP version ${PIP_VERSION} is available, but ${PIP_FIND_VERSION} requested!")
endif() endif()
set(__modules "USB;Crypt;Console;FFTW;Compress;IOUtils;Concurrent;Cloud") set(__modules "USB;Crypt;Console;FFTW;Compress;IOUtils;Concurrent;Cloud;Lua")
set(__module_USB "${PIP_USB_LIBRARY}" ) set(__module_USB "${PIP_USB_LIBRARY}" )
set(__module_Console "${PIP_CONSOLE_LIBRARY}" ) set(__module_Console "${PIP_CONSOLE_LIBRARY}" )
set(__module_Crypt "${PIP_CRYPT_LIBRARY}" ) set(__module_Crypt "${PIP_CRYPT_LIBRARY}" )
@@ -126,6 +134,7 @@ set(__module_Compress "${PIP_COMPRESS_LIBRARY}" )
set(__module_IOUtils "${PIP_IO_UTILS_LIBRARY}" ) set(__module_IOUtils "${PIP_IO_UTILS_LIBRARY}" )
set(__module_Concurrent "${PIP_CONCURRENT_LIBRARY}") set(__module_Concurrent "${PIP_CONCURRENT_LIBRARY}")
set(__module_Cloud "${PIP_CLOUD_LIBRARY}" ) set(__module_Cloud "${PIP_CLOUD_LIBRARY}" )
set(__module_Lua "${PIP_LUA_LIBRARY}" )
if((NOT TARGET PIP) AND PIP_LIBRARY) if((NOT TARGET PIP) AND PIP_LIBRARY)
add_library(PIP UNKNOWN IMPORTED) add_library(PIP UNKNOWN IMPORTED)
set_target_properties(PIP PROPERTIES set_target_properties(PIP PROPERTIES
@@ -147,6 +156,9 @@ endif()
if(__module_Cloud AND __module_IOUtils) 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 )
set_target_properties(PIP::Lua PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${LUA_INCLUDE_DIR} INTERFACE_LINK_LIBRARIES "PIP::Lua" ${LUA_LIBRARIES})
endif()
include(PIPMacros) include(PIPMacros)

View File

@@ -0,0 +1,12 @@
#ifndef PILUAPROGRAM_H
#define PILUAPROGRAM_H
#include "pip_lua.h"
class PILuaProgram
{
public:
PILuaProgram();
};
#endif // PILUAPROGRAM_H

39
lib/main/lua/pip_lua.h Normal file
View File

@@ -0,0 +1,39 @@
#ifndef PIP_LUA_H
#define PIP_LUA_H
extern "C" {
# include "lua.h"
# include "lauxlib.h"
# include "lualib.h"
}
#include <LuaBridge/LuaBridge.h>
#include "pistring.h"
namespace luabridge {
template <>
struct Stack <PIString> {
static void push (lua_State* L, PIString const& str) {
lua_pushstring(L, str.dataUTF8());
}
static PIString get (lua_State* L, int index) {
if (lua_type(L, index) == LUA_TSTRING) {
const char* str = lua_tostring(L, index);
return PIString::fromUTF8(str);
}
lua_pushvalue(L, index);
const char* str = lua_tostring(L, -1);
PIString string = PIString::fromUTF8(str);
lua_pop(L, 1);
return string;
}
static bool isInstance (lua_State* L, int index) {
return lua_type(L, index) == LUA_TSTRING;
}
};
}
#endif // PIP_LUA_H

View File

@@ -1,55 +1,12 @@
#include "pip.h" #include "pip.h"
#ifdef PIP_LUA #ifdef PIP_LUA
extern "C" { #include "piluaprogram.h"
# include "lua.h"
# include "lauxlib.h"
# include "lualib.h"
}
#include <LuaBridge/LuaBridge.h>
#include "pistring_std.h"
static const char * script static const char * script
= "-- script.lua \n" = "-- script.lua \n"
"testString = \"LuaBridge works!\" \n" "testString = \"LuaBridge works ававава!\" \n"
"number = 42 \n"; "number = 42 \n";
namespace luabridge {
template <>
struct Stack <PIString>
{
static void push (lua_State* L, PIString const& str)
{
lua_pushstring(L, str.dataAscii());
}
static PIString get (lua_State* L, int index)
{
size_t len;
if (lua_type (L, index) == LUA_TSTRING)
{
const char* str = lua_tolstring(L, index, &len);
return PIString(str, len);
}
// Lua reference manual:
// If the value is a number, then lua_tolstring also changes the actual value in the stack to a string.
// (This change confuses lua_next when lua_tolstring is applied to keys during a table traversal.)
lua_pushvalue (L, index);
const char* str = lua_tolstring(L, -1, &len);
PIString string (str, len);
lua_pop (L, 1); // Pop the temporary string
return string;
}
static bool isInstance (lua_State* L, int index)
{
return lua_type (L, index) == LUA_TSTRING;
}
};
}
using namespace luabridge; using namespace luabridge;