Skip to content

Commit

Permalink
[lua] Add cffi module
Browse files Browse the repository at this point in the history
  • Loading branch information
lampysprites committed May 9, 2024
1 parent 2942aba commit c3cb3a4
Show file tree
Hide file tree
Showing 16 changed files with 761 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,9 @@
[submodule "third_party/tinyxml2"]
path = third_party/tinyxml2
url = https://github.com/aseprite/tinyxml2.git
[submodule "third_party/libffi"]
path = third_party/libffi
url = https://github.com/libffi/libffi.git
[submodule "third_party/cffi-lua"]
path = third_party/cffi-lua
url = https://github.com/q66/cffi-lua
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ option(ENABLE_NEWS "Enable the news in Home tab" on)
option(ENABLE_UPDATER "Enable automatic check for updates" on)
option(ENABLE_SCRIPTING "Compile with scripting support" on)
option(ENABLE_WEBSOCKET "Compile with websocket support" on)
option(ENABLE_FFI "Compile with FFI scripting support" on)
option(ENABLE_TESTS "Compile unit tests" off)
option(ENABLE_BENCHMARKS "Compile benchmarks" off)
option(ENABLE_TRIAL_MODE "Compile the trial version" off)
Expand Down
4 changes: 4 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ if(ENABLE_WEBSOCKET)
add_definitions(-DENABLE_WEBSOCKET)
endif()

if(ENABLE_FFI)
add_definitions(-DENABLE_FFI)
endif()

######################################################################
# Aseprite Libraries (in preferred order to be built)

Expand Down
4 changes: 4 additions & 0 deletions src/app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,10 @@ if(ENABLE_SCRIPTING)
if(ENABLE_WEBSOCKET)
target_link_libraries(app-lib ixwebsocket)
endif()

if(ENABLE_FFI)
target_link_libraries(app-lib cffi-lua)
endif()
endif()

if(ENABLE_UPDATER)
Expand Down
8 changes: 8 additions & 0 deletions src/app/script/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
#include "ui/base.h"
#include "ui/cursor_type.h"
#include "ui/mouse_button.h"
#if ENABLE_FFI
#include <cffi.h>
#endif

#include <fstream>
#include <sstream>
Expand Down Expand Up @@ -519,6 +522,11 @@ Engine::Engine()
register_websocket_class(L);
#endif

// Register FFI module
#if ENABLE_FFI
register_ffi_object(L);
#endif

// Check that we have a clean start (without dirty in the stack)
ASSERT(lua_gettop(L) == top);
}
Expand Down
22 changes: 22 additions & 0 deletions third_party/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,28 @@ if(ENABLE_SCRIPTING)
add_subdirectory(IXWebSocket)
target_include_directories(ixwebsocket PUBLIC)
endif()

if(ENABLE_FFI)
add_subdirectory(ffi-cmake)
# main.cc is not required
add_library(cffi-lua
cffi-lua/src/util.cc
cffi-lua/src/ffilib.cc
cffi-lua/src/parser.cc
cffi-lua/src/ast.cc
cffi-lua/src/lib.cc
cffi-lua/src/ffi.cc
ffi-cmake/cffi.cpp
)
set_property(TARGET cffi-lua PROPERTY POSITION_INDEPENDENT_CODE ON)
target_compile_definitions(cffi-lua PUBLIC FFI_LITTLE_ENDIAN FFI_STATIC_BUILD)
if (WIN32)
target_compile_definitions(cffi-lua PUBLIC FFI_WINDOWS_ABI)
endif()
target_include_directories(cffi-lua PUBLIC cffi-lua/src ffi-cmake/include/ libffi/include
${CMAKE_CURRENT_BINARY_DIR}/ffi-cmake/include/)
target_link_libraries(cffi-lua lua lualib lauxlib ffi_static)
endif()

endif()

Expand Down
1 change: 1 addition & 0 deletions third_party/cffi-lua
Submodule cffi-lua added at 09413f
62 changes: 62 additions & 0 deletions third_party/ffi-cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
cmake_minimum_required(VERSION 2.8.8)
project(libffi C)

set(LIBFFI_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../libffi)
set(LIBFFI_SRC ${LIBFFI_ROOT}/src)
set(LIBFFI_INCLUDE ${LIBFFI_ROOT}/include)

set(SOURCES_LIST
${LIBFFI_SRC}/closures.c
${LIBFFI_SRC}/java_raw_api
${LIBFFI_SRC}/prep_cif.c
${LIBFFI_SRC}/raw_api.c
${LIBFFI_SRC}/tramp.c
${LIBFFI_SRC}/types.c)

if(CMAKE_BUILD_TYPE MATCHES DEBUG)
list(APPEND SOURCES_LIST ${LIBFFI_SRC}/debug.c)
add_definitions(-DFFI_DEBUG)
endif()

include(configure_platform.cmake)
include(configure_options.cmake)

file(COPY ${FFI_CONFIG_FILE} DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
file(COPY ${LIBFFI_SRC}/${TARGETDIR}/ffitarget.h DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/include)

include_directories(${CMAKE_CURRENT_BINARY_DIR}/include)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
include_directories(${LIBFFI_INCLUDE})
include_directories(include)

add_definitions(-DFFI_BUILDING)

add_library(objlib OBJECT ${SOURCES_LIST})
set_property(TARGET objlib PROPERTY POSITION_INDEPENDENT_CODE 1)

add_library(ffi_static STATIC $<TARGET_OBJECTS:objlib>)

if(MSVC)
add_definitions(-DFFI_BUILDING_DLL)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
elseif(CMAKE_SYSTEM_NAME MATCHES Darwin)
set(CMAKE_MACOSX_RPATH 1)
endif()
# add_library(ffi_shared SHARED $<TARGET_OBJECTS:objlib> ${OBJECTS_LIST})

set_target_properties(ffi_static PROPERTIES OUTPUT_NAME ffi)
# set_target_properties(ffi_shared PROPERTIES OUTPUT_NAME ffi)

install(TARGETS ffi_static # ffi_shared
EXPORT ${PROJECT_NAME}Targets
RUNTIME DESTINATION bin
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib)

install(EXPORT ${PROJECT_NAME}Targets
DESTINATION share/${PROJECT_NAME})

install(FILES
${CMAKE_CURRENT_BINARY_DIR}/include/ffi.h
${CMAKE_CURRENT_BINARY_DIR}/include/ffitarget.h
DESTINATION include)
31 changes: 31 additions & 0 deletions third_party/ffi-cmake/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
following files are based on build scripts from am11's PR
which can be found at https://github.com/libffi/libffi/pull/535
- include/fficonfig.h.in
- CMakeLists.txt
- configure_options.cmake
- configure_platform.cmake

LICENSE
=======

libffi - Copyright (c) 1996-2019 Anthony Green, Red Hat, Inc and others.
See source files for details.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
``Software''), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 changes: 11 additions & 0 deletions third_party/ffi-cmake/cffi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <lua.hh>
#include <cffi.h>

void ffi_module_open(lua_State *L);

void register_ffi_object(lua_State *L) {
ffi_module_open(L);
lua_pushvalue(L, -1);
lua_setglobal(L, "ffi");
lua_pop(L, 1);
}
Loading

0 comments on commit c3cb3a4

Please sign in to comment.