diff --git a/.clang-format b/.clang-format index 706391ba..e8daa25a 100644 --- a/.clang-format +++ b/.clang-format @@ -1,7 +1,7 @@ -# # # # sol2 +# # # # sol3 # The MIT License (MIT) # -# Copyright (c) 2013-2018 Rapptz, ThePhD, and contributors +# Copyright (c) 2013-2019 Rapptz, ThePhD, and contributors # # 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 @@ -25,7 +25,7 @@ BasedOnStyle: WebKit IndentWidth: 5 TabWidth: 5 ContinuationIndentWidth: 5 -UseTab: Always +UseTab: ForIndentation # Namespaces NamespaceIndentation: All @@ -33,7 +33,7 @@ CompactNamespaces: true FixNamespaceComments: true # Overall Alignment -ColumnLimit: 0 +ColumnLimit: 160 AlignAfterOpenBracket: DontAlign # uses ContinuationIndentWidth for this instead AccessModifierOffset: -5 # do not push public: or private: around #AlignConsecutiveAssignments: true # affects more than what's expected: do not use @@ -59,7 +59,7 @@ AlwaysBreakAfterReturnType: None BreakConstructorInitializers: BeforeComma ConstructorInitializerIndentWidth: 0 ConstructorInitializerAllOnOneLineOrOnePerLine: true -BinPackArguments: true +BinPackArguments: false BinPackParameters: true # Classes diff --git a/.dockerignore b/.dockerignore index 37341578..b12c08b8 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,7 +1,7 @@ # # # # sol2 # The MIT License (MIT) # -# Copyright (c) 2013-2018 Rapptz, ThePhD, and contributors +# Copyright (c) 2013-2019 Rapptz, ThePhD, and contributors # # 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 diff --git a/.gitignore b/.gitignore index 89711ffd..981bbece 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ -# # # # sol2 +# # # # sol3 # The MIT License (MIT) # -# Copyright (c) 2013-2018 Rapptz, ThePhD, and contributors +# Copyright (c) 2013-2019 Rapptz, ThePhD, and contributors # # 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 @@ -72,15 +72,6 @@ main.pdb *.files *.includes -# Scratchpad Files -m.lua -main.lua -catch_mock.hpp -main_aux.cpp -main.hpp -main2.cpp -main.cpp - # Local Lua Testbeds lua-5.4.0-cxx/ lua-5.4.0/ @@ -98,7 +89,6 @@ lua-5.1.5/ LuaJIT-2.1.0/ luajit-2.0.5/ luajit-2.0.3/ -include/ liblua.a lib/liblua5.2.a @@ -110,6 +100,9 @@ docs/build/ *.sublime-project +# MacOS Garbage +.DS_Store + # Windows Crap desktop.ini *.db @@ -123,10 +116,10 @@ cmake-build-relwithdebinfo/ cmake-build-release/ /subprojects/* !/subprojects/*.wrap -LuaJIT-2.1/ -LuaJIT-2.0/ -lua-5.4/ -lua-5.3-cxx/ lua-5.3/ -lua-5.2/ +lua-5.3-cxx/ +lua-5.4/ +LuaJIT-2.0/ +LuaJIT-2.1/ lua-5.1/ +lua-5.2/ diff --git a/.travis.yml b/.travis.yml index cdbb3521..922db524 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ -# # # # sol2 +# # # # sol3 # The MIT License (MIT) # -# Copyright (c) 2013-2018 Rapptz, ThePhD, and contributors +# Copyright (c) 2013-2019 Rapptz, ThePhD, and contributors # # 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 @@ -35,8 +35,7 @@ before_install: script: - export SOL2_DIR=${TRAVIS_BUILD_DIR} - sudo docker image pull thephd/sol2:gcc-${GCC_VERSION}_llvm-${LLVM_VERSION} -- sudo docker run --rm --interactive --tty --name "sol2.test" -v "${SOL2_DIR}:/root/sol2" --env "CI=${CI}" --env "PLATFORM=${PLATFORM}" --env "LUA_VERSION=${LUA_VERSION}" --env "GCC_VERSION=${GCC_VERSION}" --env "LLVM_VERSION=${LLVM_VERSION}" thephd/sol2:gcc-${GCC_VERSION}_llvm-${LLVM_VERSION} -- sudo docker rmi $(sudo docker image ls -q) +- sudo docker run --rm --interactive --tty --name "sol2.test" -v "${SOL2_DIR}:/root/sol2" --env "SOL2_CI=${SOL2_CI}" --env "SOL2_PLATFORM=${SOL2_PLATFORM}" --env "SOL2_LUA_VERSION=${SOL2_LUA_VERSION}" --env "SOL2_TEST_SINGLE=${SOL2_TEST_SINGLE}" --env "SOL2_TEST_INTEROP=${SOL2_TEST_INTEROP}" --env "GCC_VERSION=${GCC_VERSION}" --env "LLVM_VERSION=${LLVM_VERSION}" thephd/sol2:gcc-${GCC_VERSION}_llvm-${LLVM_VERSION} matrix: fast_finish: true @@ -44,116 +43,109 @@ matrix: - os: osx include: - # GCC 4.9.x, 5.x, 6.x, 7.x + # LLVM 5.0.x -> 8.0.x - env: - - LUA_VERSION=5.3.4 + - SOL2_LUA_VERSION=5.3.5 + LLVM_VERSION=5.0.2 + SOL2_PLATFORM=x64 + SOL2_CI=true + + - env: + - SOL2_LUA_VERSION=5.3.5 + LLVM_VERSION=6.0.1 + SOL2_PLATFORM=x64 + SOL2_CI=true + + - env: + - SOL2_LUA_VERSION=5.3.5 + LLVM_VERSION=7.0.1 + SOL2_PLATFORM=x64 + SOL2_CI=true + + - env: + - SOL2_LUA_VERSION=5.3.5 + LLVM_VERSION=8.0.0 + SOL2_PLATFORM=x64 + SOL2_CI=true + + # GCC 7.x, 8.x + - env: + - SOL2_LUA_VERSION=5.3.5 GCC_VERSION=7 - PLATFORM=i686 - CI=true + SOL2_PLATFORM=x64 + SOL2_CI=true - env: - - LUA_VERSION=luajit-2.0.5 - GCC_VERSION=7 - PLATFORM=i686 - CI=true + - SOL2_LUA_VERSION=5.3.5 + GCC_VERSION=8 + SOL2_PLATFORM=x64 + SOL2_CI=true + # Lua Versions 5.2.4, 5.1.5, and LuaJIT - env: - - LUA_VERSION=luajit-2.1.0-beta3 - GCC_VERSION=7 - PLATFORM=i686 - CI=true - - - env: - - LUA_VERSION=5.3.4 - GCC_VERSION=4.9 - PLATFORM=x64 - CI=true - - - env: - - LUA_VERSION=5.3.4 - GCC_VERSION=5 - CI=true - PLATFORM=x64 - - - env: - - LUA_VERSION=5.3.4 - GCC_VERSION=6 - PLATFORM=x64 - CI=true - - - env: - - LUA_VERSION=5.3.4 - GCC_VERSION=7 - PLATFORM=x64 - CI=true - - # LLVM 3.6.x -> 5.0.x - - env: - - LUA_VERSION=5.3.4 - LLVM_VERSION=3.6.2 - PLATFORM=x64 - CI=true - - - env: - - LUA_VERSION=5.3.4 - LLVM_VERSION=3.7.1 - PLATFORM=x64 - CI=true - - - env: - - LUA_VERSION=5.3.4 - LLVM_VERSION=3.8.1 - PLATFORM=x64 - CI=true - - - env: - - LUA_VERSION=5.3.4 - LLVM_VERSION=3.9.1 - PLATFORM=x64 - CI=true - - - env: - - LUA_VERSION=5.3.4 - LLVM_VERSION=4.0.1 - PLATFORM=x64 - CI=true - - - env: - - LUA_VERSION=5.3.4 - LLVM_VERSION=5.0.1 - PLATFORM=x64 - CI=true - - # Lua Versions 5.2.4, 5.1.5, and LuaJIT (as well as x86) - - env: - - LUA_VERSION=5.2.4 - GCC_VERSION=7 - PLATFORM=x64 - CI=true + - SOL2_LUA_VERSION=5.1.5 + GCC_VERSION=8 + SOL2_PLATFORM=x64 + SOL2_CI=true - env: - - LUA_VERSION=5.1.5 - GCC_VERSION=7 - PLATFORM=x64 - CI=true + - SOL2_LUA_VERSION=5.2.4 + GCC_VERSION=8 + SOL2_PLATFORM=x64 + SOL2_CI=true - env: - - LUA_VERSION=luajit-2.0.4 - GCC_VERSION=7 - PLATFORM=x64 - CI=true + - SOL2_LUA_VERSION=luajit-2.0.4 + GCC_VERSION=8 + SOL2_PLATFORM=x64 + SOL2_CI=true - env: - - LUA_VERSION=luajit-2.0.5 - GCC_VERSION=7 - PLATFORM=x64 - CI=true + - SOL2_LUA_VERSION=luajit-2.0.5 + GCC_VERSION=8 + SOL2_PLATFORM=x64 + SOL2_CI=true + + # x86 builds + - env: + - SOL2_LUA_VERSION=5.3.5 + GCC_VERSION=8 + SOL2_PLATFORM=i686 + SOL2_CI=true + + - env: + - SOL2_LUA_VERSION=luajit-2.0.5 + GCC_VERSION=8 + SOL2_PLATFORM=i686 + SOL2_CI=true + + - env: + - SOL2_LUA_VERSION=luajit-2.1.0-beta3 + GCC_VERSION=8 + SOL2_PLATFORM=i686 + SOL2_CI=true + + # Test Single, Interop, Etc. + - env: + - SOL2_LUA_VERSION=5.3.5 + SOL2_TEST_SINGLE=true + SOL2_TEST_INTEROP=true + LLVM_VERSION=8.0.0 + SOL2_PLATFORM=x64 + SOL2_CI=true + + - env: + - SOL2_LUA_VERSION=5.3.5 + SOL2_TEST_SINGLE=true + SOL2_TEST_INTEROP=true + GCC_VERSION=8 + SOL2_PLATFORM=x64 + SOL2_CI=true - os: osx - osx_image: xcode9.0 + osx_image: xcode10.1 env: - - LUA_VERSION=lua-5.3.4 - LLVM_VERSION=5.0.1 + - SOL2_LUA_VERSION=lua-5.3.5 before_install: - chmod +x ./scripts/run.osx.sh - chmod +x ./scripts/preparation.osx.sh @@ -162,10 +154,20 @@ matrix: - ./scripts/run.osx.sh - os: osx - osx_image: xcode9.1 + osx_image: xcode9.4 env: - - LUA_VERSION=lua-5.3.4 - LLVM_VERSION=5.0.1 + - SOL2_LUA_VERSION=lua-5.3.5 + before_install: + - chmod +x ./scripts/run.osx.sh + - chmod +x ./scripts/preparation.osx.sh + - ./scripts/preparation.osx.sh + script: + - ./scripts/run.osx.sh + + - os: osx + osx_image: xcode9.3 + env: + - SOL2_LUA_VERSION=lua-5.3.5 before_install: - chmod +x ./scripts/run.osx.sh - chmod +x ./scripts/preparation.osx.sh @@ -176,8 +178,7 @@ matrix: - os: osx osx_image: xcode9.2 env: - - LUA_VERSION=lua-5.3.4 - LLVM_VERSION=5.0.1 + - SOL2_LUA_VERSION=lua-5.3.5 before_install: - chmod +x ./scripts/run.osx.sh - chmod +x ./scripts/preparation.osx.sh @@ -188,7 +189,7 @@ matrix: notifications: webhooks: urls: - - https://webhooks.gitter.im/e/b864d553270a069d26c8 + - http://skyhook.glitch.me/api/webhooks/427786348108185611/y2nTmghqltv1SKX2DclEMEKyZtRcwKFlwfZpB_mL1A0nZTVS5MSfumqDKq30-gvWgeL5/travis on_success: change on_failure: always on_start: always diff --git a/CMakeLists.txt b/CMakeLists.txt index ad557768..961b39d6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ -# # # # sol2 +# # # # sol3 # The MIT License (MIT) # -# Copyright (c) 2013-2018 Rapptz, ThePhD, and contributors +# Copyright (c) 2013-2019 Rapptz, ThePhD, and contributors # # 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 @@ -20,82 +20,15 @@ # 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. -# # # # sol2 +# # # # sol3 # # # Required minimum version statement cmake_minimum_required(VERSION 3.5.0) # # # project declaration -project(sol2 VERSION 2.20.0 LANGUAGES CXX C) +project(sol2 VERSION 3.0.0 LANGUAGES CXX C) include(GNUInstallDirs) -# # # General Project Requirements -# Set general standard requirements here -set(CMAKE_CXX_STANDARD 14) -set(CMAKE_CXX_STANDARD_REQUIRED ON) -# Features a C++ compiler must have to be used to compile sol2 -# This list is not *complete* as CMake does not support features for -# all of the advanced features utilized. -set(CXX_FEATURES - cxx_auto_type - cxx_constexpr - cxx_decltype - cxx_decltype_auto - cxx_default_function_template_args - cxx_final - cxx_lambdas - cxx_noexcept - cxx_nullptr - cxx_override - cxx_range_for - cxx_return_type_deduction - cxx_right_angle_brackets - cxx_static_assert - cxx_strong_enums - cxx_variadic_macros - cxx_variadic_templates) - -# # # -if (PLATFORM MATCHES "i686" OR PLATFORM STREQUAL "x86") - set(IS_X86 TRUE) -elseif (PLATFORM MATCHES "ARM64") - set(IS_ARM64 TRUE) - set(IS_X64 TRUE) -elseif (PLATFORM MATCHES "ARM") - set(IS_ARM TRUE) -elseif (PLATFORM MATCHES "x86_64" OR PLATFORM STREQUAL "x64") - set(IS_X64 TRUE) -else() - set(IS_X64 TRUE) -endif() - -# # # General project flags -if (MSVC) - add_definitions(/DUNICODE /D_UNICODE /D_CRT_SECURE_NO_WARNINGS /D_CRT_SECURE_NO_DEPRECATE) - # Warning level, exceptions - add_compile_options(/W4 /EHsc) - add_compile_options("$<$:/MDd>" - "$<$:/MD>" - "$<$:/MD>" - "$<$:/MD>") - if (NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang") - add_compile_options(/MP) - endif() -else() - add_compile_options(-Wno-unknown-warning -Wno-unknown-warning-option -Wall -Wextra -Wpedantic -pedantic -pedantic-errors) -endif() - -# # # General project output locations -if (IS_X86 OR CMAKE_SIZEOF_VOID_P EQUAL 4) - set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/x86/lib") - set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/x86/bin") - set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/x86/bin") -else() - set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/x64/lib") - set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/x64/bin") - set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/x64/bin") -endif() - # # # Modules # # Include modules useful to the project, whether locally made in our own cmake DIRECTORY # # our from the standard cmake libraries @@ -109,44 +42,78 @@ include(CMakePackageConfigHelpers) # # # Configuration # # Cached defines, strings, paths and options -set(LUA_VERSION "5.3.4" CACHE STRING "The version of Lua needed. Can be 5.1, 5.2, 5.3, LuaJIT, or a more specific 3-part version number for a specifc Lua (e.g., 5.3.4 or luajit-2.0.5)") -set(BUILD_LUA TRUE CACHE BOOL "Always build Lua, do not search for it in the system") -set(PLATFORM "x64" CACHE STRING "Target platform to compile for when building binaries (x86, x64)") -option(CI "Whether or not we are in continguous integration mode" OFF) -option(TESTS "Enable build of tests" OFF) -option(EXAMPLES "Enable build of examples" OFF) -option(INTEROP_EXAMPLES "Enable build of interop examples" OFF) -option(DYNAMIC_LOADING_EXAMPLES "Enable build of interop examples" OFF) -option(SINGLE "Enable build of single header files" OFF) -option(DOCS "Enable build of documentation" OFF) +set(SOL2_LUA_VERSION "5.3.5" CACHE STRING "The version of Lua needed. Can be 5.1, 5.2, 5.3, 5.4, LuaJIT, or a more specific 3-part version number for a specifc Lua (e.g., 5.3.4 or luajit-2.0.5)") +set(SOL2_BUILD_LUA TRUE CACHE BOOL "Always build Lua, do not search for it in the system") +set(SOL2_PLATFORM "x64" CACHE STRING "Target platform to compile for when building binaries (x86, x64)") +option(SOL2_CI "Whether or not we are in continguous integration mode" OFF) +option(SOL2_TESTS "Enable build of tests" OFF) +option(SOL2_EXAMPLES "Enable build of examples" OFF) +option(SOL2_INTEROP_EXAMPLES "Enable build of interop examples" OFF) +option(SOL2_DYNAMIC_LOADING_EXAMPLES "Enable build of interop examples" OFF) +option(SOL2_GENERATE_SINGLE "Enable generation and build of single header files" OFF) +option(SOL2_SINGLE "Enable use of prepackaged single header files" OFF) +option(SOL2_DOCS "Enable build of documentation" OFF) # Single tests and examples tests will only be turned on if both SINGLE and TESTS are defined -CMAKE_DEPENDENT_OPTION(TESTS_SINGLE "Enable build of tests using the generated single headers" ON - "SINGLE;TESTS" OFF) -CMAKE_DEPENDENT_OPTION(EXAMPLES_SINGLE "Enable build of examples using the generated single headers" OFF - "SINGLE;EXAMPLES" OFF) -CMAKE_DEPENDENT_OPTION(INTEROP_EXAMPLES_SINGLE "Enable build of interop examples using the generated single headers" OFF - "SINGLE;INTEROP_EXAMPLES" OFF) -CMAKE_DEPENDENT_OPTION(DYNAMIC_LOADING_EXAMPLES_SINGLE "Enable build of dynamic loading examples using the generated single headers" OFF - "SINGLE;DYNAMIC_LOADING_EXAMPLES" OFF) -CMAKE_DEPENDENT_OPTION(TESTS_EXAMPLES "Enable build of examples as tests" ON - "EXAMPLES" OFF) -CMAKE_DEPENDENT_OPTION(TESTS_INTEROP_EXAMPLES "Enable build of interop examples as tests" ON - "INTEROP_EXAMPLES" OFF) -CMAKE_DEPENDENT_OPTION(TESTS_DYNAMIC_LOADING_EXAMPLES "Enable build of dynamic loading examples as tests" ON - "DYNAMIC_LOADING_EXAMPLES" OFF) +CMAKE_DEPENDENT_OPTION(SOL2_TESTS_SINGLE "Enable build of tests using the premade single headers" ON + "SOL2_SINGLE;SOL2_TESTS" OFF) +CMAKE_DEPENDENT_OPTION(SOL2_TESTS_SINGLE_GENERATED "Enable build of tests using the generated single headers" ON + "SOL2_GENERATE_SINGLE;SOL2_TESTS" OFF) +CMAKE_DEPENDENT_OPTION(SOL2_EXAMPLES_SINGLE "Enable build of examples using the generated single headers" OFF + "SOL2_SINGLE;SOL2_EXAMPLES" OFF) +CMAKE_DEPENDENT_OPTION(SOL2_EXAMPLES_SINGLE_GENERATED "Enable build of examples using the premade single headers" OFF + "SOL2_GENERATE_SINGLE;SOL2_EXAMPLES" OFF) +CMAKE_DEPENDENT_OPTION(SOL2_INTEROP_EXAMPLES_SINGLE "Enable build of interop examples using the generated single headers" OFF + "SOL2_SINGLE;SOL2_INTEROP_EXAMPLES" OFF) +CMAKE_DEPENDENT_OPTION(SOL2_INTEROP_EXAMPLES_SINGLE_GENERATED "Enable build of interop examples using the generated single headers" OFF + "SOL2_GENERATE_SINGLE;SOL2_INTEROP_EXAMPLES" OFF) +CMAKE_DEPENDENT_OPTION(SOL2_DYNAMIC_LOADING_EXAMPLES_SINGLE "Enable build of dynamic loading examples using the generated single headers" OFF + "SOL2_SINGLE;SOL2_DYNAMIC_LOADING_EXAMPLES" OFF) +CMAKE_DEPENDENT_OPTION(SOL2_DYNAMIC_LOADING_EXAMPLES_SINGLE_GENERATED "Enable build of dynamic loading examples using the generated single headers" OFF + "SOL2_GENERATE_SINGLE;SOL2_DYNAMIC_LOADING_EXAMPLES" OFF) +CMAKE_DEPENDENT_OPTION(SOL2_TESTS_EXAMPLES "Enable build of examples as tests" ON + "SOL2_EXAMPLES" OFF) +CMAKE_DEPENDENT_OPTION(SOL2_TESTS_INTEROP_EXAMPLES "Enable build of interop examples as tests" ON + "SOL2_INTEROP_EXAMPLES" OFF) +CMAKE_DEPENDENT_OPTION(SOL2_TESTS_DYNAMIC_LOADING_EXAMPLES "Enable build of dynamic loading examples as tests" ON + "SOL2_DYNAMIC_LOADING_EXAMPLES" OFF) +CMAKE_DEPENDENT_OPTION(BUILD_LUA_AS_DLL "Build Lua as a DLL" ON + "SOL2_BUILD_LUA" OFF) +# # # Platform +# Detect x86 and x64 stuff +if (SOL2_PLATFORM MATCHES "i686" OR SOL2_PLATFORM STREQUAL "x86") + set(IS_X86 TRUE) +elseif (SOL2_PLATFORM MATCHES "ARM64") + set(IS_ARM64 TRUE) + set(IS_X64 TRUE) +elseif (SOL2_PLATFORM MATCHES "ARM") + set(IS_ARM TRUE) +elseif (SOL2_PLATFORM MATCHES "x86_64" OR SOL2_PLATFORM STREQUAL "x64") + set(IS_X64 TRUE) +else() + set(IS_X64 TRUE) +endif() + + +# # # sol2 Source Groups +# # Sources everyone is going to need +# Header files +file(GLOB SOL2_HEADER_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/include/sol*.hpp) +source_group(sol2 FILES ${SOL2_HEADER_SOURCES}) + # # # sol2 Library # # Add a target for sol2's library to be included by external users add_library(sol2 INTERFACE) add_library(sol2::sol2 ALIAS sol2) +target_sources(sol2 INTERFACE ${SOL2_SINGLE_HEADER_SOURCES}) set_target_properties(sol2 PROPERTIES EXPORT_NAME sol2::sol2) target_include_directories(sol2 INTERFACE - $ - $) + $ + $) # # Version configurations configure_package_config_file( @@ -168,62 +135,26 @@ install(EXPORT sol2 FILE sol2-targets.cmake DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/sol2") -install(DIRECTORY sol - DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") - -install(FILES sol.hpp - DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") +install(DIRECTORY include/ + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/sol/include") install(FILES "${CMAKE_CURRENT_BINARY_DIR}/cmake/sol2-config.cmake" "${CMAKE_CURRENT_BINARY_DIR}/cmake/sol2-config-version.cmake" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/sol2") -# # # Source Groups -# # Sources everyone is going to need -# Header files -file(GLOB SOL2_HEADER_SOURCES sol*.hpp) -source_group(headers FILES ${SOL2_HEADER_SOURCES}) -# single header files -file(GLOB SOL2_SINGLE_HEADER_SOURCES single/sol/sol_forward.hpp single/sol/sol.hpp) -source_group(headers FILES ${SOL2_SINGLE_HEADER_SOURCES}) - # # # Single header target # Find Python3 for single header / forward header generation -find_package(PythonInterp 3) -set(SOL2_SINGLE_HEADER_FOUND FALSE) -set(SOL2_SINGLE_FOUND FALSE) -set(SOL2_DOCS_FOUND FALSE) -if (PYTHONINTERP_FOUND) - if (SINGLE) - set(SOL2_SINGLE_FOUND TRUE) - add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/single/sol.hpp" "${CMAKE_CURRENT_BINARY_DIR}/single/sol_forward.hpp" - COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/single" - COMMAND ${PYTHON_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/single.py" --output "${CMAKE_CURRENT_BINARY_DIR}/single/sol.hpp") - add_custom_target(sol2_single_header ALL - DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/single/sol.hpp" "${CMAKE_CURRENT_BINARY_DIR}/single/sol_forward.hpp") - add_library(sol2_single INTERFACE) - add_library(sol2::sol2_single ALIAS sol2_single) - set_target_properties(sol2_single - PROPERTIES - EXPORT_NAME sol2::sol2_single - INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_BINARY_DIR}/single") - add_dependencies(sol2_single sol2_single_header) - install(FILES "${CMAKE_CURRENT_BINARY_DIR}/single/sol.hpp" "${CMAKE_CURRENT_BINARY_DIR}/single/sol_forward.hpp" - DESTINATION include/sol/single/sol) - endif() - if (DOCS) - set(SOL2_DOCS_FOUND TRUE) - add_custom_command(OUTPUT docs - COMMAND ${CMAKE_COMMAND} -E copy_directory "${CMAKE_CURRENT_SOURCE_DIR}/docs" docs - COMMAND make -C docs html) - add_custom_target(documentation ALL DEPENDS docs) - install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/docs/build/html" DESTINATION "${CMAKE_INSTALL_DOCDIR}") - endif() -else() - if (SINGLE) - message(STATUS "sol2 single_header cannot be generated as python 3 has not been found.") - endif() +if (SOL2_GENERATE_SINGLE OR SOL2_SINGLE) + message(STATUS "sol2 adding single...") + add_subdirectory(single) +endif() + +# # # documentation +# Generates the docs +if (SOL2_DOCS) + message(STATUS "sol2 adding docs...") + add_subdirectory(docs) endif() # pkg-config support, except on Windows @@ -236,23 +167,23 @@ if(NOT WIN32 OR NOT CMAKE_HOST_SYSTEM_NAME MATCHES Windows) install(FILES "${CMAKE_CURRENT_BINARY_DIR}/sol2.pc" DESTINATION "${PKGCONFIG_INSTALL_DIR}") endif() -if (CI) +if (SOL2_CI) message(STATUS "sol2 Contiguous Integration is on") endif() -if (EXAMPLES OR TESTS_EXAMPLES OR EXAMPLES_SINGLE OR INTEROP_EXAMPLES OR TESTS_INTEROP_EXAMPLES OR INTEROP_EXAMPLES_SINGLE OR DYNAMIC_LOADING_EXAMPLES OR TESTS_DYNAMIC_LOADING_EXAMPLES OR DYNAMIC_LOADING_EXAMPLES_SINGLE) +if (SOL2_EXAMPLES OR SOL2_TESTS_EXAMPLES OR SOL2_EXAMPLES_SINGLE OR SOL2_INTEROP_EXAMPLES OR SOL2_TESTS_INTEROP_EXAMPLES OR SOL2_INTEROP_EXAMPLES_SINGLE OR SOL2_DYNAMIC_LOADING_EXAMPLES OR SOL2_TESTS_DYNAMIC_LOADING_EXAMPLES OR SOL2_DYNAMIC_LOADING_EXAMPLES_SINGLE) set(DO_EXAMPLES TRUE) else() set(DO_EXAMPLES FALSE) endif() -if (TESTS OR TESTS_SINGLE) +if (SOL2_TESTS OR SOL2_TESTS_SINGLE) set(DO_TESTS TRUE) else() set(DO_TESTS FALSE) endif() -if (DO_TESTS OR TESTS_EXAMPLES OR TESTS_INTEROP_EXAMPLES OR TESTS_DYNAMIC_LOADING_EXAMPLES) +if (DO_TESTS OR SOL2_TESTS_EXAMPLES OR SOL2_TESTS_INTEROP_EXAMPLES OR SOL2_TESTS_DYNAMIC_LOADING_EXAMPLES) set(ENABLE_TESTING TRUE) else() set(ENABLE_TESTING FALSE) @@ -260,6 +191,17 @@ endif() # # # Tests, Examples and other CI suites that come with sol2 if (DO_TESTS OR DO_EXAMPLES) + # # # General project output locations + if (IS_X86 OR CMAKE_SIZEOF_VOID_P EQUAL 4) + set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/x86/lib") + set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/x86/bin") + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/x86/bin") + else() + set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/x64/lib") + set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/x64/bin") + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/x64/bin") + endif() + # # # Libraries # Here, we pull in all the necessary libraries for building examples and tests # Find threading library @@ -272,31 +214,71 @@ if (DO_TESTS OR DO_EXAMPLES) endif() set(CMAKE_THREAD_PREFER_PTHREAD TRUE) set(THREADS_PREFER_PTHREAD_FLAG TRUE) + else() + string(REGEX REPLACE "/W[0-4]" "" CMAKE_C_FLAGS ${CMAKE_C_FLAGS}) + string(REGEX REPLACE "/W[0-4]" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}) endif() find_package(Threads REQUIRED) - string(TOLOWER ${LUA_VERSION} NORMALIZED_LUA_VERSION) + string(TOLOWER ${SOL2_LUA_VERSION} NORMALIZED_LUA_VERSION) # Find way to get Lua: build if requested, or attempt to build if no matching version is found - if (BUILD_LUA) - find_package(LuaBuild REQUIRED COMPONENTS ${LUA_VERSION}) - elseif (NOT LUA_VERSION) + if (SOL2_BUILD_LUA) + find_package(LuaBuild REQUIRED COMPONENTS ${SOL2_LUA_VERSION}) + elseif (NOT SOL2_LUA_VERSION) find_package(LuaBuild REQUIRED) else() if (NORMALIZED_LUA_VERSION MATCHES "5.1") + set(CREATE_LUALIB_TARGET TRUE) find_package(Lua 5.1 EXACT REQUIRED) elseif(NORMALIZED_LUA_VERSION MATCHES "5.2") + set(CREATE_LUALIB_TARGET TRUE) find_package(Lua 5.2 EXACT REQUIRED) elseif(NORMALIZED_LUA_VERSION MATCHES "5.3") + set(CREATE_LUALIB_TARGET TRUE) find_package(Lua 5.3 EXACT REQUIRED) + elseif(NORMALIZED_LUA_VERSION MATCHES "5.4") + set(CREATE_LUALIB_TARGET TRUE) + find_package(Lua 5.4 EXACT REQUIRED) elseif(NORMALIZED_LUA_VERSION MATCHES "luajit") + set(CREATE_LUALIB_TARGET TRUE) find_package(LuaJIT REQUIRED) else() - find_package(LuaBuild ${LUA_VERSION} REQUIRED) + find_package(LuaBuild ${SOL2_LUA_VERSION} REQUIRED) endif() endif() + if (CREATE_LUALIB_TARGET AND LUA_FOUND) + set(lualib lua_imported_lib_${SOL2_LUA_VERSION}) + foreach(lua_search_lib ${LUA_LIBRARIES}) + get_filename_component(lsl_fname ${lua_search_lib} NAME) + if (lsl_fname MATCHES "lua" OR lsl_fname MATCHES "Lua" OR lsl_fname MATCHES "LUA") + if (lsl_fname MATCHES "\.so|\.dylib|\.dll") + set(lualibtype SHARED) + set(lualiblocation ${lua_search_lib}) + else() + set(lualibtype STATIC) + set(lualiblocation ${lua_search_lib}) + endif() + else() + set(LUA_SEARCH_DEPENDENCY_LIBS ${LUA_SEARCH_DEPENDENCY_LIBS} "${lua_search_lib}") + endif() + endforeach() + add_library(${lualib} ${lualibtype} IMPORTED) + set_target_properties(${lualib} + PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES ${LUA_INCLUDE_DIR} + INTERFACE_LINK_LIBRARIES ${LUA_SEARCH_DEPENDENCY_LIBS} + IMPORTED_LINK_INTERFACE_LANGUAGES C + IMPORTED_LOCATION ${lualiblocation}) + if (CMAKE_DL_LIBS) + set_property(TARGET ${lualib} + APPEND PROPERTY INTERFACE_LINK_LIBRARIES ${CMAKE_DL_LIBS}) + endif() + set(LUA_LIBRARIES ${lualib}) + endif() + if (NOT LUA_FOUND AND NOT LUABUILD_FOUND) - message(FATAL_ERROR "sol2 Lua \"${LUA_VERSION}\" not found and could not be targeted for building") + message(FATAL_ERROR "sol2 Lua \"${SOL2_LUA_VERSION}\" not found and could not be targeted for building") endif() # # Enable test harness for regular, example or single tests diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index b8f2f177..8793f37c 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -1,18 +1,26 @@ -# Donators! ♥ +# 🎉 Donators! ♥ 🎉 Thank you to all patrons, donators and contributors who help keep sol2 amazing. -Robert Salvet -Ορφέας Ζαφείρης - 2x Donations! -Michael Wallar -Johannes Schultz -Dailidzionak Ilya +- Robert Salvet +- Ορφέας Ζαφείρης - 2x Donations! +- Michael Wallar +- Johannes Schultz +- Dailidzionak Ilya +- BECKMANN & EGLE Industrieelektronik GmbH [bue.de](https://www.bue.de/) -# Patrons +# 🎉 Patrons! ♥ 🎉 Beyond just a one-time donation, patrons make a continued commitment to help keep sol2 supported and bug-free. Thank you for your patronage! Here are the supporters that wanted to be featured as sol2 contributors. -Michael Caisse -Joshua Fisher +- Michael Caisse +- Joshua Fisher +- Ορφέας Ζαφείρης + +# Company Patrons / Supporters # + +Companies who sign up for a long-term support contract or patronage are listed here! They really push forward what's possible with sol2 (and the newer v3)! Please reach out to phdofthehouse@gmail.com if you are interested in a custom solution or a long-term support contract that goes beyond the current release's needs! + +- Intrepid Control Systems [intrepidcs.com](https://www.intrepidcs.com/) diff --git a/Dockerfile b/Dockerfile index e304171c..1d9925d3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,8 +20,8 @@ # 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. -# Start from the ubuntu:xenial image -FROM ubuntu:xenial +# Start from the ubuntu:bionic image +FROM ubuntu:bionic # owner LABEL author="ThePhD " LABEL maintainer="ThePhD " @@ -48,13 +48,17 @@ VOLUME /root/sol2 # # This is ordered like this so making multiple of these # # containers is more or less identical up to this point # Command line arguments, with default values -ARG CI=true +ARG SOL2_PLATFORM=x64 +ARG SOL2_LUA_VERSION=x64 +ARG SOL2_LUA_VERSION=5.3.5 +ARG SOL2_TEST_SINGLE=false +ARG SOL2_TEST_INTEROP=false +ARG SOL2_CI=true ARG GCC_VERSION ARG LLVM_VERSION -ARG PLATFORM=x64 # Potential environment variables -ENV CI=${CI} PLATFORM=${PLATFORM} GCC_VERSION=${GCC_VERSION} LLVM_VERSION=${LLVM_VERSION} SOL2_DIR=/root/sol2 +ENV SOL2_LUA_VERSION=${SOL2_LUA_VERSION} SOL2_TEST_SINGLE=${SOL2_TEST_SINGLE} SOL2_TEST_INTEROP=${SOL2_TEST_INTEROP} SOL2_CI=${SOL2_CI} SOL2_PLATFORM=${SOL2_PLATFORM} GCC_VERSION=${GCC_VERSION} LLVM_VERSION=${LLVM_VERSION} SOL2_DIR=/root/sol2 RUN ["/usr/bin/env", "zsh", "-e", "/root/sol2-scripts/preparation.linux.sh"] diff --git a/LICENSE.txt b/LICENSE.txt index 86d0851d..d4b16a7e 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2013-2018 Rapptz, ThePhD, and contributors +Copyright (c) 2013-2019 Rapptz, ThePhD, and contributors 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 diff --git a/appveyor.yml b/appveyor.yml index 10b5ca48..a00e6d94 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,7 +1,7 @@ -# # # # sol2 +# # # # sol3 # The MIT License (MIT) # -# Copyright (c) 2013-2018 Rapptz, ThePhD, and contributors +# Copyright (c) 2013-2019 Rapptz, ThePhD, and contributors # # 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 @@ -44,53 +44,63 @@ platform: environment: matrix: - # apparently, I can't quite make LLVM work right now... - #- LUA_VERSION: 5.3.4 - # LLVM_VERSION: 4.0.0 - - LUA_VERSION: 5.3.4 - MINGW_VERSION: 7.2.0 - - LUA_VERSION: 5.3.4 - MINGW_VERSION: 6.3.0 - - LUA_VERSION: luajit-2.0.5 - MINGW_VERSION: 6.3.0 - - LUA_VERSION: luajit-2.1.0-beta3 - MINGW_VERSION: 6.3.0 - - LUA_VERSION: 5.3.4 - - LUA_VERSION: 5.2.4 - - LUA_VERSION: 5.1.5 - - LUA_VERSION: luajit-2.0.5 - - LUA_VERSION: luajit-2.1.0-beta3 + - SOL2_LUA_VERSION: luajit-2.0.5 + SOL2_BUILD_NINJA: 1 + SOL2_NO_2015: 1 + - SOL2_LUA_VERSION: luajit-2.1.0-beta3 + SOL2_BUILD_NINJA: 1 + SOL2_NO_2015: 1 + - SOL2_LUA_VERSION: 5.3.5 + SOL2_BUILD_NINJA: 1 + MINGW_VERSION: 7.3.0 + - SOL2_LUA_VERSION: 5.3.5 + SOL2_BUILD_NINJA: 1 + MINGW_VERSION: 8.1.0 + - SOL2_LUA_VERSION: luajit-2.0.5 + SOL2_BUILD_NINJA: 1 + MINGW_VERSION: 8.1.0 + - SOL2_LUA_VERSION: luajit-2.1.0-beta3 + SOL2_BUILD_NINJA: 1 + MINGW_VERSION: 8.1.0 + - SOL2_LUA_VERSION: 5.3.5 + SOL2_NO_2015: 1 + - SOL2_LUA_VERSION: 5.2.4 + SOL2_NO_2015: 1 + - SOL2_LUA_VERSION: 5.1.5 + SOL2_NO_2015: 1 matrix: + #fast_finish: true allow_failures: # 32-bit builds are temperamental with exceptions - platform: x86 - # Visual Studio 2015 builds are allowed to fail, since I have no intention of dealing with bad old builds - - image: Visual Studio 2015 - # LLVM is experimental as all get-out - - LLVM_VERSION: 4.0.0 + - SOL2_LUA_VERSION: luajit-2.0.5 + - SOL2_LUA_VERSION: luajit-2.1.0-beta3 exclude: # Necessary: MinGW doesn't exist on VS 2017 images - # Also does not have MinGW x64 for 5.3.0 - image: Visual Studio 2017 - MINGW_VERSION: 6.3.0 - # LLVM exists in all images, and we only want the VS 2017 x64 versions + MINGW_VERSION: 7.3.0 + - image: Visual Studio 2017 + MINGW_VERSION: 8.1.0 + # There are no recent i686 compilers on the VS2015/2017 images + - MINGW_VERSION: 7.3.0 + platform: x86 + - MINGW_VERSION: 8.1.0 + platform: x86 + # Get rid of Visual Studio 2015 builds (unsupported) - image: Visual Studio 2015 - LLVM_VERSION: 4.0.0 + SOL2_NO_2015: 1 - platform: x86 - LLVM_VERSION: 4.0.0 - # Get rid of x86 builds + SOL2_LUA_VERSION: 5.2.4 - platform: x86 - LUA_VERSION: 5.2.4 + SOL2_LUA_VERSION: 5.1.5 + # Get rid of x86 builds for non-latest (no reason to redo that work) - platform: x86 - LUA_VERSION: 5.1.5 + SOL2_LUA_VERSION: 5.3.5 - platform: x86 - MINGW_VERSION: 7.2.0 - # Get rid of redundant Visual Studio 2015 builds - - image: Visual Studio 2015 - LUA_VERSION: 5.1.5 - - image: Visual Studio 2015 - LUA_VERSION: 5.2.4 + SOL2_LUA_VERSION: 5.2.4 + - platform: x86 + SOL2_LUA_VERSION: 5.1.5 init: # # Ninja @@ -109,10 +119,8 @@ init: - set python_path=C:\Python36 - set mingw_path= - set llvm_path= -- if "%MINGW_VERSION%"=="5.3.0" (set mingw_path=C:\mingw-w64\i686-5.3.0-posix-dwarf-rt_v4-rev0\mingw32\bin) -- if "%MINGW_VERSION%"=="6.3.0" (if "%PLATFORM%"=="x64" (set mingw_path=C:\mingw-w64\x86_64-6.3.0-posix-seh-rt_v5-rev1\mingw64\bin) else ( set mingw_path=C:\mingw-w64\i686-6.3.0-posix-dwarf-rt_v5-rev1\mingw32\bin)) -- if "%MINGW_VERSION%"=="7.2.0" (if "%PLATFORM%"=="x64" (set mingw_path=C:\mingw-w64\x86_64-7.2.0-posix-seh-rt_v5-rev1\mingw64\bin) else ( set mingw_path=C:\mingw-w64\i686-7.2.0-posix-seh-rt_v5-rev1\mingw32\bin)) -- if "%LLVM_VERSION%"=="4.0.0" (set llvm_path=C:\Program Files\LLVM\bin) +- if "%MINGW_VERSION%"=="7.3.0" (if "%PLATFORM%"=="x64" (set mingw_path=C:\mingw-w64\x86_64-7.3.0-posix-seh-rt_v5-rev0\mingw64\bin) else ( set mingw_path=C:\mingw-w64\i686-7.3.0-posix-seh-rt_v5-rev0\mingw32\bin)) +- if "%MINGW_VERSION%"=="8.1.0" (if "%PLATFORM%"=="x64" (set mingw_path=C:\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin) else ( set mingw_path=C:\mingw-w64\i686-8.1.0-posix-seh-rt_v6-rev0\mingw32\bin)) - if "%PLATFORM%"=="x64" (set python_path=C:\Python36-x64) - set PATH=%python_path%;%PATH% - set PATH=%mingw_path%;%PATH% @@ -121,53 +129,46 @@ init: - echo PATH=%PATH% # # Generators and CMake arguments # configure the generator appropriately -- set arch= -- set parallelism= -- set logger= -- set build_type= +- set CMAKE_GENERATOR=Ninja +- set arch= x86 +- set parallelism= - set build_compiler= -- set lua_build_type=OFF -- set vcvars_script="C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64 - if "%PLATFORM%"=="x64" (set arch= Win64) -- if "%APPVEYOR_BUILD_WORKER_IMAGE%"=="Visual Studio 2017" (set CMAKE_GENERATOR=Visual Studio 15 2017%arch%&&set parallelism=/maxcpucount&&set logger=/verbosity:quiet /logger:"C:\Program Files\AppVeyor\BuildAgent\Appveyor.MSBuildLogger.dll"&&set vcvars_script="C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat") -- if "%APPVEYOR_BUILD_WORKER_IMAGE%"=="Visual Studio 2015" (set CMAKE_GENERATOR=Visual Studio 14 2015%arch%&&set parallelism=/maxcpucount&&set logger=/verbosity:quiet /logger:"C:\Program Files\AppVeyor\BuildAgent\Appveyor.MSBuildLogger.dll") -- if "%MINGW_VERSION%"=="5.3.0" (set CMAKE_GENERATOR=Ninja&&set parallelism= &&set logger= &&set build_type=-DCMAKE_BUILD_TYPE=Release&&set build_compiler=-DCMAKE_CXX_COMPILER=g++.exe -DCMAKE_C_COMPILER=gcc.exe) -- if "%MINGW_VERSION%"=="6.3.0" (set CMAKE_GENERATOR=Ninja&&set parallelism= &&set logger= &&set build_type=-DCMAKE_BUILD_TYPE=Release&&set build_compiler=-DCMAKE_CXX_COMPILER=g++.exe -DCMAKE_C_COMPILER=gcc.exe) -- if "%MINGW_VERSION%"=="7.2.0" (set CMAKE_GENERATOR=Ninja&&set parallelism= &&set logger= &&set build_type=-DCMAKE_BUILD_TYPE=Release&&set build_compiler=-DCMAKE_CXX_COMPILER=g++.exe -DCMAKE_C_COMPILER=gcc.exe) -- if "%LLVM_VERSION%"=="4.0.0" (set CMAKE_GENERATOR=Ninja&&set parallelism= &&set logger= &&set build_type=-DCMAKE_BUILD_TYPE=Release&&set build_compiler=-DCMAKE_CXX_COMPILER=clang-cl.exe -DCMAKE_C_COMPILER=clang-cl.exe) +- if "%APPVEYOR_BUILD_WORKER_IMAGE%"=="Visual Studio 2017" (set CMAKE_GENERATOR=Visual Studio 15 2017%arch%) +- if "%APPVEYOR_BUILD_WORKER_IMAGE%"=="Visual Studio 2015" (set CMAKE_GENERATOR=Visual Studio 14 2015%arch%) +- if "%SOL2_BUILD_NINJA%"=="1" (set CMAKE_GENERATOR=Ninja) else (set parallelism=/maxcpucount) +- if "%SOL2_BUILD_NINJA%"=="1" (set build_compiler=-DCMAKE_CXX_COMPILER=cl.exe -DCMAKE_C_COMPILER=cl.exe) +- set vcvars_script="C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64 +- if "%PLATFORM%"=="x86" (set vcvars_script="C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x86) +- if "%MINGW_VERSION%"=="7.3.0" (set CMAKE_GENERATOR=Ninja&&set build_compiler=-DCMAKE_CXX_COMPILER=g++.exe -DCMAKE_C_COMPILER=gcc.exe) +- if "%MINGW_VERSION%"=="8.1.0" (set CMAKE_GENERATOR=Ninja&&set build_compiler=-DCMAKE_CXX_COMPILER=g++.exe -DCMAKE_C_COMPILER=gcc.exe) # # Last printouts # print out generator information -- echo Appveyor Image is %APPVEYOR_BUILD_WORKER_IMAGE% (vcvars script is %vcvars_script%) -- echo cmake generator is %CMAKE_GENERATOR% using build_type=%build_type% and build_compiler=%build_compiler% with special flags logger=%logger% parallelism=%parallelism% +- echo Appveyor Image is %APPVEYOR_BUILD_WORKER_IMAGE% +- echo vcvars_script is %vcvars_script% +- echo cmake generator is %CMAKE_GENERATOR% +- echo build_compiler is %build_compiler% +- echo parallelism is %parallelism% # print out useful tool information - ninja --version - cmake --version -- if DEFINED LLVM_VERSION (clang-cl.exe -v) -- if DEFINED MINGW_VERSION (g++.exe --version) -# We need to use CMAKE_BUILD_TYPE=Release since there are no "configuration" -# toolsets for Ninja or Makefiles as far as cmake is concerned, so -# the --config / -C switches on builds do nothing...! before_build: -- if DEFINED LLVM_VERSION (call %vcvars_script% && cd C:\projects\sol2) - md build-sol2 - cd build-sol2 -- cmake .. -G "%CMAKE_GENERATOR%" %build_type% %build_compiler% -DLUA_VERSION="%LUA_VERSION%" -DCI=ON -DBUILD_LUA=ON -DBUILD_LUA_AS_DLL=%lua_build_type% -DTESTS=ON -DEXAMPLES=ON -DSINGLE=ON -DTESTS_EXAMPLES=ON -DEXAMPLES_SINGLE=ON -DTESTS_SINGLE=ON +- echo %vcvars_script% +- cd && %vcvars_script% +- cmake .. -G "%CMAKE_GENERATOR%" %build_compiler% -DCMAKE_BUILD_TYPE=Release "-DSOL2_LUA_VERSION=%SOL2_LUA_VERSION%" -DSOL2_PLATFORM=%PLATFORM% -DSOL2_CI=ON -DSOL2_BUILD_LUA=ON -DBUILD_LUA_AS_DLL=ON -DSOL2_TESTS=ON -DSOL2_EXAMPLES=ON -DSOL2_TESTS_EXAMPLES=ON -# We do not build the debug versions because the compiler -# generates too much debug info for MinGW to handle -# TODO: fix the damn compilation space and time already build_script: -- if NOT "%build_type%"=="-DCMAKE_BUILD_TYPE=Release" (cmake --build . --config Debug -- %parallelism% %logger%) -- cmake --build . --config Release -- %parallelism% %logger% +- cmake --build . --config Release -- %parallelism% test_script: -- if NOT "%build_type%"=="-DCMAKE_BUILD_TYPE=Release" (ctest -C Debug --output-on-failure) - ctest -C Release --output-on-failure notifications: - provider: Webhook - url: https://webhooks.gitter.im/e/1af10e654a918bef7f1e + url: http://skyhook.glitch.me/api/webhooks/427786348108185611/y2nTmghqltv1SKX2DclEMEKyZtRcwKFlwfZpB_mL1A0nZTVS5MSfumqDKq30-gvWgeL5/appveyor method: POST on_build_success: true on_build_failure: true diff --git a/cmake/Modules/Common/Core.cmake b/cmake/Modules/Common/Core.cmake index d50f152d..61686a58 100644 --- a/cmake/Modules/Common/Core.cmake +++ b/cmake/Modules/Common/Core.cmake @@ -1,7 +1,7 @@ # # # # sol2 # The MIT License (MIT) # -# Copyright (c) 2013-2018 Rapptz, ThePhD, and contributors +# Copyright (c) 2013-2019 Rapptz, ThePhD, and contributors # # 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 diff --git a/cmake/Modules/FindCatch.cmake b/cmake/Modules/FindCatch.cmake index 3931db11..d4c2c225 100644 --- a/cmake/Modules/FindCatch.cmake +++ b/cmake/Modules/FindCatch.cmake @@ -1,7 +1,7 @@ -# # # # sol2 +# # # # sol3 # The MIT License (MIT) # -# Copyright (c) 2013-2018 Rapptz, ThePhD, and contributors +# Copyright (c) 2013-2019 Rapptz, ThePhD, and contributors # # 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 diff --git a/cmake/Modules/FindKaguyaBuild.cmake b/cmake/Modules/FindKaguyaBuild.cmake index fd8e5e68..dbef9451 100644 --- a/cmake/Modules/FindKaguyaBuild.cmake +++ b/cmake/Modules/FindKaguyaBuild.cmake @@ -1,8 +1,8 @@ -# # # # sol2 +# # # # sol3 # The MIT License (MIT) # -# Copyright (c) 2013-2018 Rapptz, ThePhD, and contributors +# Copyright (c) 2013-2019 Rapptz, ThePhD, and contributors # # 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 diff --git a/cmake/Modules/FindLua.cmake b/cmake/Modules/FindLua.cmake deleted file mode 100644 index 1e498a86..00000000 --- a/cmake/Modules/FindLua.cmake +++ /dev/null @@ -1,233 +0,0 @@ -# Distributed under the OSI-approved BSD 3-Clause License. See accompanying -# file Copyright.txt or https://cmake.org/licensing for details. - -#.rst: -# FindLua -# ------- -# -# -# -# Locate Lua library. This module defines -# -# :: -# -# LUA_FOUND - if false, do not try to link to Lua -# LUA_LIBRARIES - both lua and lualib -# LUA_INCLUDE_DIR - where to find lua.h -# LUA_LIBRARY_DIR - Dir(s) where Lua libraries are found -# LUA_VERSION_STRING - the version of Lua found -# LUA_VERSION_MAJOR - the major version of Lua -# LUA_VERSION_MINOR - the minor version of Lua -# LUA_VERSION_PATCH - the patch version of Lua -# -# -# -# Note that the expected include convention is -# -# :: -# -# #include "lua.h" -# -# and not -# -# :: -# -# #include -# -# This is because, the lua location is not standardized and may exist in -# locations other than lua/ - -unset(_lua_include_subdirs) -unset(_lua_append_versions) -unset(_lua_library_names) - - -include(${CMAKE_CURRENT_LIST_DIR}/FindLua/set_version_vars.cmake) -include(${CMAKE_CURRENT_LIST_DIR}/FindLua/version_check.cmake) - -# # this is a function only to have all the variables inside go away automatically -# function(_lua_set_version_vars) -# set(LUA_VERSIONS5 5.3 5.2 5.1 5.0) - -# if (Lua_FIND_VERSION_EXACT) -# if (Lua_FIND_VERSION_COUNT GREATER 1) -# set(_lua_append_versions ${Lua_FIND_VERSION_MAJOR}.${Lua_FIND_VERSION_MINOR}) -# endif () -# elseif (Lua_FIND_VERSION) -# # once there is a different major version supported this should become a loop -# if (NOT Lua_FIND_VERSION_MAJOR GREATER 5) -# if (Lua_FIND_VERSION_COUNT EQUAL 1) -# set(_lua_append_versions ${LUA_VERSIONS5}) -# else () -# foreach (subver IN LISTS LUA_VERSIONS5) -# if (NOT subver VERSION_LESS ${Lua_FIND_VERSION}) -# list(APPEND _lua_append_versions ${subver}) -# endif () -# endforeach () -# endif () -# endif () -# else () -# # once there is a different major version supported this should become a loop -# set(_lua_append_versions ${LUA_VERSIONS5}) -# endif () - -# list(APPEND _lua_include_subdirs "include/lua" "include") - -# foreach (ver IN LISTS _lua_append_versions) -# string(REGEX MATCH "^([0-9]+)\\.([0-9]+)$" _ver "${ver}") -# list(APPEND _lua_include_subdirs -# include/lua${CMAKE_MATCH_1}${CMAKE_MATCH_2} -# include/lua${CMAKE_MATCH_1}.${CMAKE_MATCH_2} -# include/lua-${CMAKE_MATCH_1}.${CMAKE_MATCH_2} -# ) -# list(APPEND _lua_library_names -# lua${CMAKE_MATCH_1}${CMAKE_MATCH_2} -# lua${CMAKE_MATCH_1}.${CMAKE_MATCH_2} -# lua.${CMAKE_MATCH_1}.${CMAKE_MATCH_2} -# lua-${CMAKE_MATCH_1}.${CMAKE_MATCH_2} -# ) -# endforeach () - -# set(_lua_include_subdirs "${_lua_include_subdirs}" PARENT_SCOPE) -# set(_lua_append_versions "${_lua_append_versions}" PARENT_SCOPE) -# endfunction(_lua_set_version_vars) - -# function(_lua_check_header_version _hdr_file) -# # At least 5.[012] have different ways to express the version -# # so all of them need to be tested. Lua 5.2 defines LUA_VERSION -# # and LUA_RELEASE as joined by the C preprocessor, so avoid those. -# file(STRINGS "${_hdr_file}" lua_version_strings -# REGEX "^#define[ \t]+LUA_(RELEASE[ \t]+\"Lua [0-9]|VERSION([ \t]+\"Lua [0-9]|_[MR])).*") - -# string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MAJOR[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_MAJOR ";${lua_version_strings};") -# if (LUA_VERSION_MAJOR MATCHES "^[0-9]+$") -# string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MINOR[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_MINOR ";${lua_version_strings};") -# string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_RELEASE[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_PATCH ";${lua_version_strings};") -# set(LUA_VERSION_STRING "${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}.${LUA_VERSION_PATCH}") -# else () -# string(REGEX REPLACE ".*;#define[ \t]+LUA_RELEASE[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" LUA_VERSION_STRING ";${lua_version_strings};") -# if (NOT LUA_VERSION_STRING MATCHES "^[0-9.]+$") -# string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" LUA_VERSION_STRING ";${lua_version_strings};") -# endif () -# string(REGEX REPLACE "^([0-9]+)\\.[0-9.]*$" "\\1" LUA_VERSION_MAJOR "${LUA_VERSION_STRING}") -# string(REGEX REPLACE "^[0-9]+\\.([0-9]+)[0-9.]*$" "\\1" LUA_VERSION_MINOR "${LUA_VERSION_STRING}") -# string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]).*" "\\1" LUA_VERSION_PATCH "${LUA_VERSION_STRING}") -# endif () -# foreach (ver IN LISTS _lua_append_versions) -# if (ver STREQUAL "${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}") -# set(LUA_VERSION_MAJOR ${LUA_VERSION_MAJOR} PARENT_SCOPE) -# set(LUA_VERSION_MINOR ${LUA_VERSION_MINOR} PARENT_SCOPE) -# set(LUA_VERSION_PATCH ${LUA_VERSION_PATCH} PARENT_SCOPE) -# set(LUA_VERSION_STRING ${LUA_VERSION_STRING} PARENT_SCOPE) -# return() -# endif () -# endforeach () -# endfunction(_lua_check_header_version) - -_lua_set_version_vars(lua "") - -find_path(LUA_INCLUDE_DIR lua.h - HINTS - ENV LUA_DIR - PATH_SUFFIXES ${_lua_include_subdirs} include - PATHS - ${LUA_DIR} - ~/Library/Frameworks - /Library/Frameworks - /sw # Fink - /opt/local # DarwinPorts - /opt/csw # Blastwave - /opt - /usr - /usr/local # Homebrew -) - -if (LUA_INCLUDE_DIR AND EXISTS "${LUA_INCLUDE_DIR}/lua.h") - _lua_check_header_version("${LUA_INCLUDE_DIR}/lua.h" "LUA") -endif () - -if (NOT LUA_VERSION_STRING) - foreach (subdir IN LISTS _lua_include_subdirs) - unset(LUA_INCLUDE_PREFIX CACHE) - find_path(LUA_INCLUDE_PREFIX ${subdir}/lua.h - HINTS - ENV LUA_DIR - PATHS - ${LUA_DIR} - ~/Library/Frameworks - /Library/Frameworks - /sw # Fink - /opt/local # DarwinPorts - /opt/csw # Blastwave - /opt - /usr - /usr/local - ) - if (LUA_INCLUDE_PREFIX) - _lua_check_header_version("${LUA_INCLUDE_PREFIX}/${subdir}/lua.h") - if (LUA_VERSION_STRING) - set(LUA_INCLUDE_DIR "${LUA_INCLUDE_PREFIX}/${subdir}") - break() - endif () - endif () - endforeach () -endif () -unset(_lua_include_subdirs) -unset(_lua_append_versions) - -find_library(LUA_LIBRARY - NAMES ${_lua_library_names} lua - HINTS - ENV LUA_DIR - PATH_SUFFIXES lib bin - PATHS - ${LUA_DIR} - ~/Library/Frameworks - /Library/Frameworks - /sw - /opt/local - /opt/csw - /opt - /usr - /usr/local - - # From the include_dir - ${LUA_INCLUDE_DIR}/../lib -) -unset(_lua_library_names) - -if (LUA_LIBRARY) - # include the math library for Unix - if (UNIX AND NOT APPLE AND NOT BEOS) - find_library(LUA_MATH_LIBRARY m) - set(LUA_LIBRARIES "${LUA_LIBRARY};${LUA_MATH_LIBRARY}") - - # include dl library for statically-linked Lua library - get_filename_component(LUA_LIB_EXT ${LUA_LIBRARY} EXT) - if (LUA_LIB_EXT STREQUAL CMAKE_STATIC_LIBRARY_SUFFIX) - list(APPEND LUA_LIBRARIES ${CMAKE_DL_LIBS}) - endif () - - # For Windows and Mac, don't need to explicitly include the math library - else () - set(LUA_LIBRARIES "${LUA_LIBRARY}") - endif () - - set(LUA_LIBRARY_DIR ) - foreach (lib ${LUA_LIBRARIES}) - get_filename_component(lib_dir ${lib} DIRECTORY CACHE) - list(APPEND LUA_LIBRARY_DIR ${lib_dir}) - endforeach () - list(REMOVE_DUPLICATES LUA_LIBRARY_DIR) -endif () - -include(FindPackageHandleStandardArgs) - -# handle the QUIETLY and REQUIRED arguments and set LUA_FOUND to TRUE if -# all listed variables are TRUE -FIND_PACKAGE_HANDLE_STANDARD_ARGS(Lua - FOUND_VAR Lua_FOUND - REQUIRED_VARS LUA_LIBRARIES LUA_LIBRARY_DIR LUA_INCLUDE_DIR - VERSION_VAR LUA_VERSION_STRING) - -mark_as_advanced(LUA_INCLUDE_DIR LUA_LIBRARY LUA_LIBRARY_DIR LUA_MATH_LIBRARY) diff --git a/cmake/Modules/FindLua/set_version_vars.cmake b/cmake/Modules/FindLua/set_version_vars.cmake index fad0fcca..314265af 100644 --- a/cmake/Modules/FindLua/set_version_vars.cmake +++ b/cmake/Modules/FindLua/set_version_vars.cmake @@ -1,7 +1,7 @@ # # # # sol2 # The MIT License (MIT) # -# Copyright (c) 2013-2018 Rapptz, ThePhD, and contributors +# Copyright (c) 2013-2019 Rapptz, ThePhD, and contributors # # 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 diff --git a/cmake/Modules/FindLuaBridgeBuild.cmake b/cmake/Modules/FindLuaBridgeBuild.cmake index 75209527..ffe0a7e0 100644 --- a/cmake/Modules/FindLuaBridgeBuild.cmake +++ b/cmake/Modules/FindLuaBridgeBuild.cmake @@ -1,7 +1,7 @@ -# # # # sol2 +# # # # sol3 # The MIT License (MIT) # -# Copyright (c) 2013-2018 Rapptz, ThePhD, and contributors +# Copyright (c) 2013-2019 Rapptz, ThePhD, and contributors # # 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 diff --git a/cmake/Modules/FindLuaBuild.cmake b/cmake/Modules/FindLuaBuild.cmake index 1ef99653..6ed229a7 100644 --- a/cmake/Modules/FindLuaBuild.cmake +++ b/cmake/Modules/FindLuaBuild.cmake @@ -1,7 +1,7 @@ -# # # # sol2 +# # # # sol3 # The MIT License (MIT) # -# Copyright (c) 2013-2018 Rapptz, ThePhD, and contributors +# Copyright (c) 2013-2019 Rapptz, ThePhD, and contributors # # 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 @@ -107,7 +107,7 @@ if (LuaBuild_FIND_VERSION) endif() endif() if (NOT LUA_VERSION) - set(LUA_VERSION 5.3.4) + set(LUA_VERSION 5.3.5) endif() find_lua_build(${LUA_VERSION}) unset(find_lua_build) @@ -116,5 +116,5 @@ unset(find_lua_build) # all listed variables are TRUE FIND_PACKAGE_HANDLE_STANDARD_ARGS(LuaBuild FOUND_VAR LUABUILD_FOUND - REQUIRED_VARS LUA_LIBRARIES LUA_INTERPRETER LUA_INCLUDE_DIRS + REQUIRED_VARS LUA_LIBRARIES LUA_INCLUDE_DIRS VERSION_VAR LUA_VERSION_STRING) diff --git a/cmake/Modules/FindLuaBuild/LuaJIT.cmake b/cmake/Modules/FindLuaBuild/LuaJIT.cmake index 6584acf3..920e5806 100644 --- a/cmake/Modules/FindLuaBuild/LuaJIT.cmake +++ b/cmake/Modules/FindLuaBuild/LuaJIT.cmake @@ -1,7 +1,7 @@ # # # # sol2 # The MIT License (MIT) # -# Copyright (c) 2013-2018 Rapptz, ThePhD, and contributors +# Copyright (c) 2013-2019 Rapptz, ThePhD, and contributors # # 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 @@ -145,6 +145,7 @@ endif() set(LUA_JIT_SOURCE_DIR "${LUA_BUILD_TOPLEVEL}/src") set(LUA_JIT_INSTALL_DIR "${LUA_BUILD_TOPLEVEL}/install") +set(LUA_JIT_INCLUDE_DIRS "${LUA_JIT_SOURCE_DIR}") file(MAKE_DIRECTORY ${LUA_JIT_SOURCE_DIR}) file(MAKE_DIRECTORY ${LUA_JIT_INSTALL_DIR}) @@ -164,11 +165,40 @@ set(LUA_JIT_EXE_FILE "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${LUA_JIT_EXE_FILENAME}" if (MSVC) # Visual C++ is predicated off running msvcbuild.bat # which requires a Visual Studio Command Prompt - if (BUILD_LUA_AS_DLL) - set(LUA_JIT_BUILD_COMMAND BUILD_COMMAND cd src && msvcbuild.bat) - else() - set(LUA_JIT_BUILD_COMMAND BUILD_COMMAND cd src && msvcbuild.bat static) + # make sure to find the right one + find_file(VCVARS_ALL_BAT NAMES vcvarsall.bat + HINTS "C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Auxiliary/Build" + "C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Auxiliary" + "C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC" + "C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Auxiliary/Build" + "C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Auxiliary" + "C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC" + "C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/VC/Auxiliary/Build" + "C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/VC/Auxiliary" + "C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/VC" + + "C:/Program Files/Microsoft Visual Studio/2017/Community/VC/Auxiliary/Build" + "C:/Program Files/Microsoft Visual Studio/2017/Community/VC/Auxiliary" + "C:/Program Files/Microsoft Visual Studio/2017/Community/VC" + "C:/Program Files/Microsoft Visual Studio/2017/Professional/VC/Auxiliary/Build" + "C:/Program Files/Microsoft Visual Studio/2017/Professional/VC/Auxiliary" + "C:/Program Files/Microsoft Visual Studio/2017/Professional/VC" + "C:/Program Files/Microsoft Visual Studio/2017/Enterprise/VC/Auxiliary/Build" + "C:/Program Files/Microsoft Visual Studio/2017/Enterprise/VC/Auxiliary" + "C:/Program Files/Microsoft Visual Studio/2017/Enterprise/VC") + if (VCVARS_ALL_BAT MATCHES "VCVARS_ALL_BAT-NOTFOUND") + MESSAGE(FATAL_ERROR "Cannot find 'vcvarsall.bat' file or similar needed to build LuaJIT ${LUA_VERSION} on Windows") endif() + if (CMAKE_SIZEOF_VOID_P LESS_EQUAL 4) + set(LUA_JIT_MAKE_COMMAND "${VCVARS_ALL_BAT}" x86) + else() + set(LUA_JIT_MAKE_COMMAND "${VCVARS_ALL_BAT}" x64) + endif() + set(LUA_JIT_MAKE_COMMAND ${LUA_JIT_MAKE_COMMAND} && cd src && msvcbuild.bat) + if (NOT BUILD_LUA_AS_DLL) + set(LUA_JIT_MAKE_COMMAND ${LUA_JIT_MAKE_COMMAND} static) + endif() + set(LUA_JIT_PREBUILT_LIB "lua51.lib") set(LUA_JIT_PREBUILT_IMP_LIB "lua51.lib") set(LUA_JIT_PREBUILT_DLL "lua51.dll") @@ -189,13 +219,12 @@ else () list(APPEND LUA_JIT_MAKE_BUILD_MODIFICATIONS "LUAJIT_A=${LUA_JIT_LIB_FILENAME}") set(LUA_JIT_MAKE_CFLAGS_MODIFICATIONS "") set(LUA_JIT_MAKE_HOST_CFLAGS_MODIFICATIONS "") - set(LUA_JIT_MAKE_TARGET_CFLAGS_MODIFICATIONS "") + set(LUA_JIT_MAKE_TARGET_CFLAGS_MODIFICATIONS "-fPIC") if (BUILD_LUA_AS_DLL) list(APPEND LUA_JIT_MAKE_BUILD_MODIFICATIONS "LUAJIT_SO=${LUA_JIT_DLL_FILENAME}" "TARGET_SONAME=${LUA_JIT_DLL_FILENAME}" "TARGET_DYLIBNAME=${LUA_JIT_DLL_FILENAME}" "TARGET_DLLNAME=${LUA_JIT_DLL_FILENAME}") list(APPEND LUA_JIT_MAKE_BUILD_MODIFICATIONS "BUILDMODE=dynamic") else() list(APPEND LUA_JIT_MAKE_BUILD_MODIFICATIONS "BUILDMODE=static") - set(LUA_JIT_MAKE_TARGET_CFLAGS_MODIFICATIONS "${LUA_JIT_MAKE_TARGET_CFLAGS_MODIFICATIONS} -fPIC") endif() if (IS_X86) list(APPEND LUA_JIT_MAKE_BUILD_MODIFICATIONS "CC=${CMAKE_C_COMPILER} -m32") @@ -217,10 +246,11 @@ else () list(APPEND LUA_JIT_MAKE_BUILD_MODIFICATIONS "CFLAGS=${LUA_JIT_MAKE_CFLAGS_MODIFICATIONS}") list(APPEND LUA_JIT_MAKE_BUILD_MODIFICATIONS "TARGET_CFLAGS=${LUA_JIT_MAKE_TARGET_CFLAGS_MODIFICATIONS}") list(APPEND LUA_JIT_MAKE_BUILD_MODIFICATIONS "HOST_CFLAGS=${LUA_JIT_MAKE_HOST_CFLAGS_MODIFICATIONS}") - - set(LUA_JIT_BUILD_COMMAND BUILD_COMMAND "${MAKE_PROGRAM}" ${LUA_JIT_MAKE_BUILD_MODIFICATIONS}) + set(LUA_JIT_MAKE_COMMAND "${MAKE_PROGRAM}" ${LUA_JIT_MAKE_BUILD_MODIFICATIONS}) endif() +set(LUA_JIT_BUILD_COMMAND BUILD_COMMAND ${LUA_JIT_MAKE_COMMAND}) + set(lualib luajit_lib_${LUA_JIT_VERSION}) set(luainterpreter luajit_${LUA_JIT_VERSION}) @@ -247,15 +277,20 @@ elseif(LUA_JIT_NORMALIZED_LUA_VERSION MATCHES "latest") set(LUA_JIT_PULL_LATEST TRUE) endif() -set(LUA_JIT_BYPRODUCTS "${LUA_JIT_SOURCE_LUA_LIB}" "${LUA_JIT_SOURCE_LUA_LIB_EXP}" - "${LUA_JIT_SOURCE_LUA_DLL}" "${LUA_JIT_SOURCE_LUA_INTERPRETER}") +set(LUA_JIT_BYPRODUCTS "${LUA_JIT_SOURCE_LUA_DLL}" "${LUA_JIT_SOURCE_LUA_INTERPRETER}") +set(LUA_JIT_INSTALL_BYPRODUCTS "${LUA_JIT_DESTINATION_LUA_DLL}" "${LUA_JIT_DESTINATION_LUA_INTERPRETER}") -set(LUA_JIT_INSTALL_BYPRODUCTS "${LUA_JIT_DESTINATION_LUA_LIB}" "${LUA_JIT_DESTINATION_LUA_LIB_EXP}" - "${LUA_JIT_DESTINATION_LUA_DLL}" "${LUA_JIT_DESTINATION_LUA_INTERPRETER}") +if (BUILD_LUA_AS_DLL AND MSVC) + set(LUA_JIT_BYPRODUCTS ${LUA_JIT_BYPRODUCTS} "${LUA_JIT_SOURCE_LUA_LIB_EXP}") + set(LUA_JIT_INSTALL_BYPRODUCTS ${LUA_JIT_INSTALL_BYPRODUCTS} "${LUA_JIT_DESTINATION_LUA_LIB_EXP}") +endif() -if (CMAKE_IMPORT_LIBRARY_SUFFIX) - set(LUA_JIT_BYPRODUCTS "${LUA_JIT_BYPRODUCTS}" "${LUA_JIT_SOURCE_LUA_IMP_LIB}") +if (CMAKE_IMPORT_LIBRARY_SUFFIX AND BUILD_LUA_AS_DLL) + set(LUA_JIT_BYPRODUCTS ${LUA_JIT_BYPRODUCTS} "${LUA_JIT_SOURCE_LUA_IMP_LIB}") set(LUA_JIT_INSTALL_BYPRODUCTS "${LUA_JIT_INSTALL_BYPRODUCTS}" "${LUA_JIT_DESTINATION_LUA_IMP_LIB}") +else() + set(LUA_JIT_BYPRODUCTS ${LUA_JIT_BYPRODUCTS} "${LUA_JIT_SOURCE_LUA_LIB}") + set(LUA_JIT_INSTALL_BYPRODUCTS ${LUA_JIT_INSTALL_BYPRODUCTS} "${LUA_JIT_DESTINATION_LUA_LIB}") endif() # # Post-Build moving steps for necessary items @@ -264,20 +299,37 @@ set(LUA_JIT_POSTBUILD_COMMENTS "Executable - Moving \"${LUA_JIT_SOURCE_LUA_INTER set(LUA_JIT_POSTBUILD_COMMANDS COMMAND "${CMAKE_COMMAND}" -E copy "${LUA_JIT_SOURCE_LUA_INTERPRETER}" "${LUA_JIT_DESTINATION_LUA_INTERPRETER}") if (BUILD_LUA_AS_DLL) if (MSVC) - set(LUA_JIT_POSTBUILD_COMMENTS "${LUA_JIT_POSTBUILD_COMMENTS}\nImport Library - Moving \"${LUA_JIT_SOURCE_LUA_IMP_LIB}\" to \"${LUA_JIT_DESTINATION_LUA_IMP_LIB}\"...") + set(LUA_JIT_POSTBUILD_COMMENTS "${LUA_JIT_POSTBUILD_COMMENTS} | Import Library - Moving \"${LUA_JIT_SOURCE_LUA_IMP_LIB}\" to \"${LUA_JIT_DESTINATION_LUA_IMP_LIB}\"...") set(LUA_JIT_POSTBUILD_COMMANDS ${LUA_JIT_POSTBUILD_COMMANDS} COMMAND "${CMAKE_COMMAND}" -E copy "${LUA_JIT_SOURCE_LUA_IMP_LIB}" "${LUA_JIT_DESTINATION_LUA_IMP_LIB}") - set(LUA_JIT_POSTBUILD_COMMENTS "${LUA_JIT_POSTBUILD_COMMENTS}\nLibrary - Moving \"${LUA_JIT_SOURCE_LUA_LIB_EXP}\" to \"${LUA_JIT_DESTINATION_LUA_LIB_EXP}\"...") + set(LUA_JIT_POSTBUILD_COMMENTS "${LUA_JIT_POSTBUILD_COMMENTS} | Library - Moving \"${LUA_JIT_SOURCE_LUA_LIB_EXP}\" to \"${LUA_JIT_DESTINATION_LUA_LIB_EXP}\"...") set(LUA_JIT_POSTBUILD_COMMANDS ${LUA_JIT_POSTBUILD_COMMANDS} && "${CMAKE_COMMAND}" -E copy "${LUA_JIT_SOURCE_LUA_LIB_EXP}" "${LUA_JIT_DESTINATION_LUA_LIB_EXP}") endif() - set(LUA_JIT_POSTBUILD_COMMENTS "${LUA_JIT_POSTBUILD_COMMENTS}\nDynamic Library - Moving \"${LUA_JIT_SOURCE_LUA_DLL}\" to \"${LUA_JIT_DESTINATION_LUA_DLL}\"...") + set(LUA_JIT_POSTBUILD_COMMENTS "${LUA_JIT_POSTBUILD_COMMENTS} | Dynamic Library - Moving \"${LUA_JIT_SOURCE_LUA_DLL}\" to \"${LUA_JIT_DESTINATION_LUA_DLL}\"...") set(LUA_JIT_POSTBUILD_COMMANDS ${LUA_JIT_POSTBUILD_COMMANDS} COMMAND "${CMAKE_COMMAND}" -E copy "${LUA_JIT_SOURCE_LUA_DLL}" "${LUA_JIT_DESTINATION_LUA_DLL}") else() - set(LUA_JIT_POSTBUILD_COMMENTS "${LUA_JIT_POSTBUILD_COMMENTS}\nLibrary - Moving \"${LUA_JIT_SOURCE_LUA_LIB}\" to \"${LUA_JIT_DESTINATION_LUA_LIB}\"...") + set(LUA_JIT_POSTBUILD_COMMENTS "${LUA_JIT_POSTBUILD_COMMENTS} | Library - Moving \"${LUA_JIT_SOURCE_LUA_LIB}\" to \"${LUA_JIT_DESTINATION_LUA_LIB}\"...") set(LUA_JIT_POSTBUILD_COMMANDS ${LUA_JIT_POSTBUILD_COMMANDS} COMMAND "${CMAKE_COMMAND}" -E copy "${LUA_JIT_SOURCE_LUA_LIB}" "${LUA_JIT_DESTINATION_LUA_LIB}") endif() -if (LUA_JIT_GIT_COMMIT OR LUA_JIT_PULL_LATEST) +if (LUA_LOCAL_DIR) + file(COPY "${LUA_LOCAL_DIR}/" + DESTINATION "${LUA_BUILD_TOPLEVEL}" + FILES_MATCHING REGEX ".*" + ) + add_custom_command(OUTPUT ${LUA_JIT_BYPRODUCTS} + COMMAND ${LUA_JIT_MAKE_COMMAND} + WORKING_DIRECTORY "${LUA_BUILD_TOPLEVEL}" + DEPENDS "${LUA_BUILD_TOPLEVEL}/Makefile" "${LUA_BUILD_TOPLEVEL}/src/msvcbuild.bat" + COMMENT "Building LuaJIT ${LUA_JIT_VERSION}..." + ) + add_custom_target(LUA_JIT-move + ${LUA_JIT_POSTBUILD_COMMANDS} + COMMENT ${LUA_JIT_POSTBUILD_COMMENTS} + BYPRODUCTS ${LUA_JIT_INSTALL_BYPRODUCTS} + DEPENDS ${LUA_JIT_BYPRODUCTS} + ) +elseif (LUA_JIT_GIT_COMMIT OR LUA_JIT_PULL_LATEST) if (LUA_JIT_PULL_LATEST) MESSAGE(STATUS "Latest LuaJIT has been requested: pulling from git...") elseif (LUA_JIT_GIT_COMMIT) @@ -304,7 +356,7 @@ if (LUA_JIT_GIT_COMMIT OR LUA_JIT_PULL_LATEST) else() ExternalProject_Add(LUA_JIT BUILD_IN_SOURCE TRUE - BUILD_ALWAYS FALSE + BUILD_ALWAYS TRUE # LuaJIT does not offer a TLS/SSL port TLS_VERIFY FALSE PREFIX "${LUA_BUILD_TOPLEVEL}" @@ -323,58 +375,46 @@ else() BUILD_BYPRODUCTS ${LUA_JIT_BYPRODUCTS}) endif() -# # MAYBE?: -# Add additional post-build step to move all necessary headers/lua files -# for now, we just point directly to the `src` directory... - -add_custom_command(TARGET LUA_JIT - POST_BUILD - ${LUA_JIT_POSTBUILD_COMMANDS} - COMMENT ${LUA_JIT_POSTBUILD_COMMENTS} - BYPRODUCTS ${LUA_JIT_INSTALL_BYPRODUCTS}) +if (NOT LUA_LOCAL_DIR) + ExternalProject_Add_Step(LUA_JIT move + ALWAYS TRUE + ${LUA_JIT_POSTBUILD_COMMANDS} + WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" + COMMENT ${LUA_JIT_POSTBUILD_COMMENTS} + DEPENDEES build + DEPENDS ${LUA_JIT_BYPRODUCTS} + BYPRODUCTS ${LUA_JIT_INSTALL_BYPRODUCTS}) + ExternalProject_Add_StepTargets(LUA_JIT move) +endif() # # Lua Library -add_library(${lualib} ${LUA_BUILD_LIBRARY_TYPE} IMPORTED) -# make sure the library we export really does depend on Lua JIT's external project -add_dependencies(${lualib} LUA_JIT) -# Configure properties +add_library(${lualib} INTERFACE) +add_dependencies(${lualib} LUA_JIT-move) +target_include_directories(${lualib} + INTERFACE "${LUA_JIT_SOURCE_DIR}") if (BUILD_LUA_AS_DLL) if (MSVC) - set_target_properties(${lualib} - PROPERTIES - IMPORTED_IMPLIB "${LUA_JIT_DESTINATION_LUA_IMP_LIB}") + target_link_libraries(${lualib} + INTERFACE "${LUA_JIT_DESTINATION_LUA_LIB}") + else() + target_link_libraries(${lualib} + INTERFACE "${LUA_JIT_DESTINATION_LUA_DLL}") endif() - set_target_properties(${lualib} - PROPERTIES - IMPORTED_LOCATION "${LUA_JIT_DESTINATION_LUA_DLL}") -else () - set_target_properties(${lualib} - PROPERTIES - IMPORTED_LOCATION "${LUA_JIT_DESTINATION_LUA_LIB}") +else() + target_link_libraries(${lualib} + INTERFACE "${LUA_JIT_DESTINATION_LUA_LIB}") endif() -set_target_properties(${lualib} - PROPERTIES - IMPORTED_LINK_INTERFACE_LANGUAGES C - INTERFACE_INCLUDE_DIRECTORIES "${LUA_JIT_SOURCE_DIR}") + if (CMAKE_DL_LIBS) - set_target_properties(${lualib} - PROPERTIES - INTERFACE_LINK_LIBRARIES ${CMAKE_DL_LIBS}) + target_link_libraries(${lualib} + INTERFACE ${CMAKE_DL_LIBS}) endif() if (XCODE) target_compile_options(${lualib} - PUBLIC -pagezero_size 10000 -image_base 100000000) + INTERFACE -pagezero_size 10000 -image_base 100000000) endif () - -# # Lua Executable -add_executable(${luainterpreter} IMPORTED) -# Add location pointing to executable -set_target_properties(${luainterpreter} - PROPERTIES - IMPORTED_LOCATION "${LUA_JIT_DESTINATION_LUA_INTERPRETER}") -add_dependencies(${luainterpreter} LUA_JIT) - + # # set externally-visible target indicator set(LUA_LIBRARIES ${lualib}) -set(LUA_INTERPRETER ${luainterpreter}) -set(LUA_INCLUDE_DIRS "${LUA_JIT_SOURCE_DIR}") +set(LUA_INTERPRETER "") +set(LUA_INCLUDE_DIRS "${LUA_JIT_INCLUDE_DIRS}") diff --git a/cmake/Modules/FindLuaBuild/LuaVanilla.cmake b/cmake/Modules/FindLuaBuild/LuaVanilla.cmake index 4dcb1185..50f84c62 100644 --- a/cmake/Modules/FindLuaBuild/LuaVanilla.cmake +++ b/cmake/Modules/FindLuaBuild/LuaVanilla.cmake @@ -1,7 +1,7 @@ # # # # sol2 # The MIT License (MIT) # -# Copyright (c) 2013-2018 Rapptz, ThePhD, and contributors +# Copyright (c) 2013-2019 Rapptz, ThePhD, and contributors # # 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 @@ -34,10 +34,17 @@ include(Common/Core) # Latest versions for specific sub-versions of Lua set(LUA_VANILLA_5.1_LATEST_VERSION 5.1.5) set(LUA_VANILLA_5.2_LATEST_VERSION 5.2.4) -set(LUA_VANILLA_5.3_LATEST_VERSION 5.3.4) +set(LUA_VANILLA_5.3_LATEST_VERSION 5.3.5) +set(LUA_VANILLA_5.4_LATEST_VERSION 5.4.0-work1) # exact version, coming from CI: pull directly from Lua and use external project to build # list of known md5 / sha1: must update when there are changes +set(LUA_VANILLA_MD5_5.4.0-work2 3cdf2a4eb84dde6b6aaf5d2d1de07be9) +set(LUA_VANILLA_SHA1_5.4.0-work2 e8484e61c5c338e3ec2f75dbe0f6703d079fecf9) +set(LUA_VANILLA_MD5_5.4.0-work1 0ff232b8658884155a43cf72212edbd9) +set(LUA_VANILLA_SHA1_5.4.0-work1 a8193b14ed3869917d1102cb0418cf9dfb0d9baf) +set(LUA_VANILLA_MD5_5.3.5 4f4b4f323fd3514a68e0ab3da8ce3455) +set(LUA_VANILLA_SHA1_5.3.5 112eb10ff04d1b4c9898e121d6bdf54a81482447) set(LUA_VANILLA_MD5_5.3.4 53a9c68bcc0eda58bdc2095ad5cdfc63) set(LUA_VANILLA_SHA1_5.3.4 79790cfd40e09ba796b01a571d4d63b52b1cd950) set(LUA_VANILLA_MD5_5.3.3 703f75caa4fdf4a911c1a72e67a27498) @@ -118,6 +125,8 @@ elseif (LUA_VERSION MATCHES "([0-9]+)\\.([0-9]+)") set(LUA_VANILLA_VERSION ${LUA_VANILLA_5.2_LATEST_VERSION}) elseif (${CMAKE_MATCH_2} EQUAL 3) set(LUA_VANILLA_VERSION ${LUA_VANILLA_5.3_LATEST_VERSION}) + elseif (${CMAKE_MATCH_2} EQUAL 4) + set(LUA_VANILLA_VERSION ${LUA_VANILLA_5.4_LATEST_VERSION}) else() # default to whatever the first two # numbers happen to be, plus build 0 @@ -156,7 +165,6 @@ endif() # # Potential compiler variables if (MSVC) - set(LUA_VANILLA_LUA_LUAC_COMPILER_OPTIONS "/W1") if (BUILD_LUA_AS_DLL) set(LUA_VANILLA_DLL_DEFINE LUA_BUILD_AS_DLL) else() @@ -169,6 +177,7 @@ endif() # # Source files for natural build, if we have to go that far # retrieve source files +set(LUA_VANILLA_DOWNLOAD_URL https://www.lua.org/ftp/lua-${LUA_VANILLA_VERSION}.tar.gz) if (LUA_VANILLA_VERSION MATCHES "^5\\.1") set(LUA_VANILLA_LIB_SOURCES lapi.c lcode.c ldebug.c ldo.c ldump.c lfunc.c lgc.c llex.c lmem.c lobject.c lopcodes.c lparser.c lstate.c @@ -187,10 +196,7 @@ elseif (LUA_VANILLA_VERSION MATCHES "^5\\.2") set(LUA_VANILLA_LUA_SOURCES lua.c ) set(LUA_VANILLA_LUAC_SOURCES luac.c ) set(LUA_VANILLA_GENERATE_LUA_HPP false) -else() - if (NOT LUA_VANILLA_VERSION MATCHES "^5\\.3") - message(STATUS "Using the Lua 5.3 sources list for a version of Lua that is not 5.3: may result in an incomplete build or errors later") - endif() +elseif (LUA_VANILLA_VERSION MATCHES "^5\\.3") set(LUA_VANILLA_LIB_SOURCES lapi.c lcode.c lctype.c ldebug.c ldo.c ldump.c lfunc.c lgc.c llex.c lmem.c lobject.c lopcodes.c lparser.c lstate.c lstring.c ltable.c ltm.c lundump.c lvm.c lzio.c lauxlib.c @@ -199,6 +205,28 @@ else() set(LUA_VANILLA_LUA_SOURCES lua.c ) set(LUA_VANILLA_LUAC_SOURCES luac.c ) set(LUA_VANILLA_GENERATE_LUA_HPP false) +elseif (LUA_VANILLA_VERSION MATCHES "^5\\.4") + if (LUA_VANILLA_VERSION MATCHES "work") + set(LUA_VANILLA_DOWNLOAD_URL https://www.lua.org/work/lua-${LUA_VANILLA_VERSION}.tar.gz) + endif() + set(LUA_VANILLA_LIB_SOURCES lapi.c lauxlib.c lbaselib.c lcode.c lcorolib.c + lctype.c ldblib.c ldebug.c ldo.c ldump.c lfunc.c lgc.c linit.c liolib.c + llex.c lmathlib.c lmem.c loadlib.c lobject.c lopcodes.c loslib.c + lparser.c lstate.c lstring.c lstrlib.c ltable.c ltablib.c ltm.c lundump.c + lutf8lib.c lvm.c lzio.c) + set(LUA_VANILLA_LUA_SOURCES lua.c ) + set(LUA_VANILLA_LUAC_SOURCES luac.c ) + set(LUA_VANILLA_GENERATE_LUA_HPP false) +else() + MESSAGE(WARNING "Using Lua 5.4.0-work1 file list for ${LUA_VERSION} version") + set(LUA_VANILLA_LIB_SOURCES lapi.c lauxlib.c lbaselib.c lcode.c lcorolib.c + lctype.c ldblib.c ldebug.c ldo.c ldump.c lfunc.c lgc.c linit.c liolib.c + llex.c lmathlib.c lmem.c loadlib.c lobject.c lopcodes.c loslib.c + lparser.c lstate.c lstring.c lstrlib.c ltable.c ltablib.c ltm.c lundump.c + lutf8lib.c lvm.c lzio.c) + set(LUA_VANILLA_LUA_SOURCES lua.c ) + set(LUA_VANILLA_LUAC_SOURCES luac.c ) + set(LUA_VANILLA_GENERATE_LUA_HPP false) endif() set(LUA_VANILLA_SOURCE_DIR "${LUA_BUILD_TOPLEVEL}/src") @@ -207,32 +235,38 @@ prepend(LUA_VANILLA_LUA_SOURCES "${LUA_VANILLA_SOURCE_DIR}/" ${LUA_VANILLA_LUA_S prepend(LUA_VANILLA_LUAC_SOURCES "${LUA_VANILLA_SOURCE_DIR}/" ${LUA_VANILLA_LUAC_SOURCES}) # download, just for the sake of download + extract -# have to use 2 different commands just to have an empty command -# that results in nothing being run -# TODO: talk to smarter CMake people...? +# or pull from local folder +if (LUA_LOCAL_DIR) + file(COPY "${LUA_LOCAL_DIR}/src" + DESTINATION "${LUA_BUILD_TOPLEVEL}") + file(COPY "${LUA_LOCAL_DIR}/include" + DESTINATION "${LUA_BUILD_TOPLEVEL}") + add_custom_target(LUA_VANILLA + DEPENDS "${LUA_VANILLA_LIB_SOURCES}" "${LUA_VANILLA_LUA_SOURCES}" "${LUA_VANILLA_LUAC_SOURCES}") + set(LUA_VANILLA_INCLUDE_DIRS ${LUA_VANILLA_INCLUDE_DIRS} "${LUA_VANILLA_SOURCE_DIR}" "${LUA_BUILD_TOPLEVEL}/include") +else() + ExternalProject_Add(LUA_VANILLA + BUILD_IN_SOURCE TRUE + BUILD_ALWAYS FALSE + TLS_VERIFY TRUE + PREFIX ${LUA_BUILD_TOPLEVEL} + SOURCE_DIR ${LUA_BUILD_TOPLEVEL} + DOWNLOAD_DIR ${LUA_BUILD_TOPLEVEL} + TMP_DIR "${LUA_BUILD_TOPLEVEL}-tmp" + STAMP_DIR "${LUA_BUILD_TOPLEVEL}-stamp" + INSTALL_DIR "${LUA_BUILD_INSTALL_DIR}" + URL ${LUA_VANILLA_DOWNLOAD_URL} + URL_MD5 ${LUA_VANILLA_MD5} + URL_HASH SHA1=${LUA_VANILLA_SHA1} + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND "" + TEST_COMMAND "" + BUILD_BYPRODUCTS "${LUA_VANILLA_LIB_SOURCES}" "${LUA_VANILLA_LUA_SOURCES}" "${LUA_VANILLA_LUAC_SOURCES}") -ExternalProject_Add(LUA_VANILLA - BUILD_IN_SOURCE TRUE - BUILD_ALWAYS FALSE - TLS_VERIFY TRUE - PREFIX ${LUA_BUILD_TOPLEVEL} - SOURCE_DIR ${LUA_BUILD_TOPLEVEL} - DOWNLOAD_DIR ${LUA_BUILD_TOPLEVEL} - TMP_DIR "${LUA_BUILD_TOPLEVEL}-tmp" - STAMP_DIR "${LUA_BUILD_TOPLEVEL}-stamp" - INSTALL_DIR "${LUA_BUILD_INSTALL_DIR}" - URL https://www.lua.org/ftp/lua-${LUA_VANILLA_VERSION}.tar.gz - URL_MD5 ${LUA_VANILLA_MD5} - URL_HASH SHA1=${LUA_VANILLA_SHA1} - CONFIGURE_COMMAND "" - BUILD_COMMAND "" - INSTALL_COMMAND "" - TEST_COMMAND "" - BUILD_BYPRODUCTS "${LUA_VANILLA_LIB_SOURCES}" "${LUA_VANILLA_LUA_SOURCES}" "${LUA_VANILLA_LUAC_SOURCES}") - -# make a quick lua.hpp for 5.1 targets that don't have it -if (LUA_VANILLA_GENERATE_LUA_HPP) - set(LUA_VANILLA_LUA_HPP_CONTENT "// lua.hpp + # make a quick lua.hpp for 5.1 targets that don't have it + if (LUA_VANILLA_GENERATE_LUA_HPP) + set(LUA_VANILLA_LUA_HPP_CONTENT "// lua.hpp // Lua header files for C++ // <> not supplied automatically because Lua also compiles as C++ @@ -242,19 +276,22 @@ extern \"C\" { #include \"lauxlib.h\" } ") - set(LUA_VANILLA_SOURCE_LUA_HPP "${LUA_BUILD_TOPLEVEL}-tmp/lua.hpp") - set(LUA_VANILLA_DESTINATION_LUA_HPP "${LUA_VANILLA_SOURCE_DIR}/lua.hpp") - file(WRITE "${LUA_VANILLA_SOURCE_LUA_HPP}" "${LUA_VANILLA_LUA_HPP_CONTENT}") - file(TO_NATIVE_PATH "${LUA_VANILLA_SOURCE_LUA_HPP}" LUA_VANILLA_SOURCE_LUA_HPP) - file(TO_NATIVE_PATH "${LUA_VANILLA_DESTINATION_LUA_HPP}" LUA_VANILLA_DESTINATION_LUA_HPP) - ExternalProject_Add_Step(LUA_VANILLA - prebuild - # after download, before build - DEPENDEES download - DEPENDERS build - BYPRODUCTS "${LUA_VANILLA_DESTINATION_LUA_HPP}" - COMMENT "Moving \"${LUA_VANILLA_SOURCE_LUA_HPP}\" to \"${LUA_VANILLA_DESTINATION_LUA_HPP}\"..." - COMMAND "${CMAKE_COMMAND}" -E copy "${LUA_VANILLA_SOURCE_LUA_HPP}" "${LUA_VANILLA_DESTINATION_LUA_HPP}") + set(LUA_VANILLA_SOURCE_LUA_HPP "${LUA_BUILD_TOPLEVEL}-tmp/lua.hpp") + set(LUA_VANILLA_DESTINATION_LUA_HPP "${LUA_VANILLA_SOURCE_DIR}/lua.hpp") + file(WRITE "${LUA_VANILLA_SOURCE_LUA_HPP}" "${LUA_VANILLA_LUA_HPP_CONTENT}") + file(TO_NATIVE_PATH "${LUA_VANILLA_SOURCE_LUA_HPP}" LUA_VANILLA_SOURCE_LUA_HPP) + file(TO_NATIVE_PATH "${LUA_VANILLA_DESTINATION_LUA_HPP}" LUA_VANILLA_DESTINATION_LUA_HPP) + ExternalProject_Add_Step(LUA_VANILLA + prebuild + # after download, before build + DEPENDEES download + DEPENDERS build + BYPRODUCTS "${LUA_VANILLA_DESTINATION_LUA_HPP}" + COMMENT "Moving \"${LUA_VANILLA_SOURCE_LUA_HPP}\" to \"${LUA_VANILLA_DESTINATION_LUA_HPP}\"..." + COMMAND "${CMAKE_COMMAND}" -E copy "${LUA_VANILLA_SOURCE_LUA_HPP}" "${LUA_VANILLA_DESTINATION_LUA_HPP}") + endif() + + set(LUA_VANILLA_INCLUDE_DIRS ${LUA_VANILLA_SOURCE_DIR}) endif() # # Target names @@ -279,13 +316,12 @@ set_target_properties(${liblua} C_STANDARD 99 C_EXTENSIONS TRUE POSITION_INDEPENDENT_CODE TRUE - INCLUDE_DIRECTORIES ${LUA_VANILLA_SOURCE_DIR} OUTPUT_NAME ${LUA_BUILD_LIBNAME} RUNTIME_OUTPUT_NAME ${LUA_BUILD_LIBNAME} LIBRARY_OUTPUT_NAME ${LUA_BUILD_LIBNAME} ARCHIVE_OUTPUT_NAME ${LUA_BUILD_LIBNAME}) target_include_directories(${liblua} - PUBLIC ${LUA_VANILLA_SOURCE_DIR}) + PUBLIC "${LUA_VANILLA_INCLUDE_DIRS}") target_compile_definitions(${liblua} PUBLIC LUA_COMPAT_ALL ${LUA_VANILLA_DLL_DEFINE}) if (MSVC) @@ -306,10 +342,10 @@ target_compile_options(${liblua} PRIVATE ${LUA_VANILLA_LUALIB_COMPILER_OPTIONS}) add_dependencies(${liblua} LUA_VANILLA) if (CMAKE_DL_LIBS) - target_link_libraries(${liblua} ${CMAKE_DL_LIBS}) + target_link_libraries(${liblua} PRIVATE ${CMAKE_DL_LIBS}) endif() if (UNIX) - target_link_libraries(${liblua} m) + target_link_libraries(${liblua} PRIVATE m) endif() # we don't really need this section... @@ -323,7 +359,7 @@ set_target_properties(${luainterpreter} C_EXTENSIONS TRUE OUTPUT_NAME lua-${LUA_VANILLA_VERSION}) target_include_directories(${luainterpreter} - PRIVATE ${LUA_VANILLA_SOURCE_DIR}) + PRIVATE "${LUA_VANILLA_INCLUDE_DIRS}") target_compile_definitions(${luainterpreter} PUBLIC LUA_COMPAT_ALL ${LUA_VANILLA_DLL_DEFINE} PRIVATE LUA_COMPAT_ALL ${LUA_VANILLA_DLL_DEFINE}) @@ -341,14 +377,12 @@ else() target_compile_definitions(${luainterpreter} PRIVATE LUA_USE_LINUX) endif() -target_compile_options(${luainterpreter} - PRIVATE ${LUA_VANILLA_LUA_LUAC_COMPILER_OPTIONS}) -target_link_libraries(${luainterpreter} ${liblua}) +target_link_libraries(${luainterpreter} PRIVATE ${liblua}) if (CMAKE_DL_LIBS) - target_link_libraries(${luainterpreter} ${CMAKE_DL_LIBS}) + target_link_libraries(${luainterpreter} PRIVATE ${CMAKE_DL_LIBS}) endif() if (UNIX) - target_link_libraries(${luainterpreter} m readline) + target_link_libraries(${luainterpreter} PRIVATE m readline) endif() # LuaC Compiler @@ -365,9 +399,7 @@ set_target_properties(${luacompiler} C_EXTENSIONS TRUE OUTPUT_NAME luac-${LUA_VANILLA_VERSION}) target_include_directories(${luacompiler} - PRIVATE ${LUA_VANILLA_SOURCE_DIR}) -target_compile_options(${luacompiler} - PRIVATE ${LUA_VANILLA_LUA_LUAC_COMPILER_OPTIONS}) + PRIVATE "${LUA_VANILLA_INCLUDE_DIRS}") target_compile_definitions(${luacompiler} PUBLIC LUA_COMPAT_ALL ${LUA_VANILLA_DLL_DEFINE} PRIVATE LUA_COMPAT_ALL ${LUA_VANILLA_DLL_DEFINE}) @@ -385,13 +417,13 @@ else() target_compile_definitions(${luacompiler} PRIVATE LUA_USE_LINUX) endif() -target_link_libraries(${luacompiler} ${liblua}) +target_link_libraries(${luacompiler} PRIVATE ${liblua}) if (CMAKE_DL_LIBS) - target_link_libraries(${luacompiler} ${CMAKE_DL_LIBS}) + target_link_libraries(${luacompiler} PRIVATE ${CMAKE_DL_LIBS}) endif() if (UNIX) # TODO: make readline optional? - target_link_libraries(${luacompiler} m readline) + target_link_libraries(${luacompiler} PRIVATE m readline) endif() # set externally-visible target indicator diff --git a/cmake/Modules/FindLuaJIT.cmake b/cmake/Modules/FindLuaJIT.cmake deleted file mode 100644 index 8ef3dbdc..00000000 --- a/cmake/Modules/FindLuaJIT.cmake +++ /dev/null @@ -1,173 +0,0 @@ -# Distributed under the OSI-approved BSD 3-Clause License. See accompanying -# file Copyright.txt or https://cmake.org/licensing for details. - -#.rst: -# FindLuaJIT -# ------- -# -# -# -# Locate LuaJIT library. This module defines -# -# :: -# -# LUAJIT_FOUND - if false, do not try to link to LuaJIT -# LUAJIT_LIBRARIES - both lua and lualib -# LUAJIT_INCLUDE_DIR - where to find lua.h and luajit.h -# LUAJIT_VERSION_STRING - the version of LuaJIT found -# LUAJIT_VERSION_MAJOR - the major version of LuaJIT -# LUAJIT_VERSION_MINOR - the minor version of LuaJIT -# LUAJIT_VERSION_PATCH - the patch version of LuaJIT -# LUAJIT_LUA_VERSION_STRING - the version of Lua the found LuaJIT is compatible with -# -# -# -# Note that the expected include convention is -# -# :: -# -# #include "lua.h" -# -# and not -# -# :: -# -# #include -# -# This is because, the lua location is not standardized and may exist in -# locations other than lua/ - -unset(_luajit_include_subdirs) -unset(_luajit_append_versions) -unset(_luajit_library_names) - -include(${CMAKE_CURRENT_LIST_DIR}/FindLua/set_version_vars.cmake) -include(${CMAKE_CURRENT_LIST_DIR}/FindLua/version_check.cmake) - -_lua_set_version_vars(luajit "jit") - -find_path(LUAJIT_INCLUDE_DIR luajit.h - HINTS - ENV LUAJIT_DIR - PATH_SUFFIXES ${_luajit_include_subdirs} include/luajit include - PATHS - ${LUAJIT_DIR} - ~/Library/Frameworks - /Library/Frameworks - /sw # Fink - /opt/local # DarwinPorts - /opt/csw # Blastwave - /opt - /usr - /usr/local # Homebrew -) - -if (LUAJIT_INCLUDE_DIR AND EXISTS "${LUAJIT_INCLUDE_DIR}/lua.h") - _lua_check_header_version("${LUAJIT_INCLUDE_DIR}/lua.h" "LUAJIT") -endif () - -if (NOT LUAJIT_VERSION_STRING) - foreach (subdir IN LISTS _luajit_include_subdirs) - unset(LUAJIT_INCLUDE_PREFIX CACHE) - find_path(LUAJIT_INCLUDE_PREFIX ${subdir}/lua.h - HINTS - ENV LUA_DIR - PATHS - ${LUA_DIR} - ~/Library/Frameworks - /Library/Frameworks - /sw # Fink - /opt/local # DarwinPorts - /opt/csw # Blastwave - /opt - /usr - /usr/local # Homebrew - ) - if (LUAJIT_INCLUDE_PREFIX) - _lua_check_header_version("${LUAJIT_INCLUDE_PREFIX}/${subdir}/lua.h" "LUAJIT") - if (LUAJIT_VERSION_STRING) - set(LUAJIT_INCLUDE_DIR "${LUAJIT_INCLUDE_PREFIX}/${subdir}") - break() - endif () - endif () - endforeach () -endif () -unset(_luajit_include_subdirs) -unset(_luajit_append_versions) - -if (LUAJIT_INCLUDE_DIR AND EXISTS "${LUAJIT_INCLUDE_DIR}/luajit.h") - # LuaJIT defines two preprocessor macros: - # LUA_VERSION -> version string with lua version in it - # LUA_VERSION_NUM -> numeric representation, i.e. 20003 for 2.0.3 - # This just parses the LUAJIT_VERSION macro and extracts the version. - file(STRINGS "${LUAJIT_INCLUDE_DIR}/luajit.h" version_strings - REGEX "^#define[ \t]+LUAJIT_VERSION?[ \t]+(\"LuaJIT [0-9\\.]+(-(beta|alpha)[0-9]*)?\").*") - - string(REGEX REPLACE ".*;#define[ \t]+LUAJIT_VERSION[ \t]+\"LuaJIT ([0-9\\.]+(-(beta|alpha)[0-9]*)?)\"[ \t]*;.*" "\\1" LUAJIT_VERSION_STRING_SHORT ";${version_strings};") - string(REGEX REPLACE ".*;([0-9]+\\.[0-9]+\\.[0-9]+(-(beta|alpha)[0-9]*)?);.*" "\\1" luajit_version_num ";${LUAJIT_VERSION_STRING_SHORT};") - - string(REGEX REPLACE "^([0-9]+)\\.[0-9]+\\.(-(beta|alpha)[0-9]*)?$" "\\1" LUAJIT_VERSION_MAJOR "${luajit_version_num}") - string(REGEX REPLACE "^[0-9]+\\.([0-9]+)\\.[0-9](-(beta|alpha)[0-9]*)?$" "\\1" LUAJIT_VERSION_MINOR "${luajit_version_num}") - string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]+(-(beta|alpha)[0-9]*)?)$" "\\1" LUAJIT_VERSION_PATCH "${luajit_version_num}") - - # We can also set the LUAJIT_LUA_VERSION_* fields that are found by FindLua. - # We do this as LuaJIT claims full compatibility with a certain LUA version. - _lua_check_header_version("${LUAJIT_INCLUDE_DIR}/lua.h" "LUAJIT_LUA_") - - set(LUAJIT_VERSION_STRING "${LUAJIT_LUA_VERSION_STRING} (${LUAJIT_VERSION_STRING_SHORT})") -endif() - -find_library(LUAJIT_LIBRARY - NAMES ${_luajit_library_names} luajit lua - HINTS - ENV LUAJIT_DIR - PATH_SUFFIXES lib - PATHS - ${LUAJIT_DIR} - ~/Library/Frameworks - /Library/Frameworks - /sw - /opt/local - /opt/csw - /opt - /usr - /usr/local # Homebrew -) -unset(_luajit_library_names) - -if (LUAJIT_LIBRARY) - # include the math library for Unix - if (UNIX AND NOT APPLE AND NOT BEOS) - find_library(LUAJIT_MATH_LIBRARY m) - set(LUAJIT_LIBRARIES "${LUAJIT_LIBRARY};${LUAJIT_MATH_LIBRARY}") - # For Windows and Mac, don't need to explicitly include the math library - else () - set(LUAJIT_LIBRARIES "${LUAJIT_LIBRARY}") - endif () - - set(LUAJIT_LIBRARY_DIR ) - foreach (lib ${LUAJIT_LIBRARIES}) - get_filename_component(lib_dir ${lib} DIRECTORY CACHE) - list(APPEND LUAJIT_LIBRARY_DIR ${lib_dir}) - endforeach () - list(REMOVE_DUPLICATES LUAJIT_LIBRARY_DIR) -endif () - -if(APPLE) - # Not sure why this is true, but its mentioned here: - # http://luajit.org/install.html#embed - set(LUAJIT_LINK_FLAGS "-pagezero_size 10000 -image_base 100000000") -else() - set(LUAJIT_LINK_FLAGS "") -endif() - -include(FindPackageHandleStandardArgs) -# handle the QUIETLY and REQUIRED arguments and set LuaJIT_FOUND to TRUE if -# all listed variables are TRUE -FIND_PACKAGE_HANDLE_STANDARD_ARGS(LuaJIT - FOUND_VAR LuaJIT_FOUND - REQUIRED_VARS LUAJIT_LIBRARIES LUAJIT_INCLUDE_DIR LUAJIT_LIBRARY_DIR - VERSION_VAR LUAJIT_VERSION_STRING) - -mark_as_advanced(LUAJIT_INCLUDE_DIR LUAJIT_LIBRARY LUAJIT_LIBRARY_DIR LUAJIT_MATH_LIBRARY LUAJIT_LINK_FLAGS) - diff --git a/cmake/Modules/FindLuwraBuild.cmake b/cmake/Modules/FindLuwraBuild.cmake index 97e13711..243e1365 100644 --- a/cmake/Modules/FindLuwraBuild.cmake +++ b/cmake/Modules/FindLuwraBuild.cmake @@ -1,7 +1,7 @@ -# # # # sol2 +# # # # sol3 # The MIT License (MIT) # -# Copyright (c) 2013-2018 Rapptz, ThePhD, and contributors +# Copyright (c) 2013-2019 Rapptz, ThePhD, and contributors # # 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 diff --git a/cmake/Modules/FindToLuappBuild.cmake b/cmake/Modules/FindToLuappBuild.cmake index 63bd581e..c88cfa2c 100644 --- a/cmake/Modules/FindToLuappBuild.cmake +++ b/cmake/Modules/FindToLuappBuild.cmake @@ -1,7 +1,7 @@ -# # # # sol2 +# # # # sol3 # The MIT License (MIT) # -# Copyright (c) 2013-2018 Rapptz, ThePhD, and contributors +# Copyright (c) 2013-2019 Rapptz, ThePhD, and contributors # # 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 @@ -57,7 +57,7 @@ ExternalProject_Add(TOLUAPP_BUILD_SOURCE TEST_COMMAND "" BUILD_BYPRODUCTS "${toluapp_sources}") -set(toluapp_lib toluapp_lib_5.2.4) +set(toluapp_lib toluapp_lib_${toluapp_version}) add_library(${toluapp_lib} SHARED ${toluapp_sources}) add_dependencies(${toluapp_lib} TOLUAPP_BUILD_SOURCE) set_target_properties(${toluapp_lib} PROPERTIES @@ -70,7 +70,7 @@ if (MSVC) target_compile_options(${toluapp_lib} PRIVATE /W1) target_compile_definitions(${toluapp_lib} - PRIVATE TOLUA_API=__declspec(dllexport)) + PRIVATE "TOLUA_API=__declspec(dllexport)") else() target_compile_options(${toluapp_lib} PRIVATE -w diff --git a/cmake/sol2-config.cmake.in b/cmake/sol2-config.cmake.in index e43edddf..70c448d2 100644 --- a/cmake/sol2-config.cmake.in +++ b/cmake/sol2-config.cmake.in @@ -1,7 +1,7 @@ -# # # # sol2 +# # # # sol3 # The MIT License (MIT) # -# Copyright (c) 2013-2018 Rapptz, ThePhD, and contributors +# Copyright (c) 2013-2019 Rapptz, ThePhD, and contributors # # 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 diff --git a/docs/CMakeLists.txt b/docs/CMakeLists.txt new file mode 100644 index 00000000..57011ffe --- /dev/null +++ b/docs/CMakeLists.txt @@ -0,0 +1,43 @@ +# # # # sol3 +# The MIT License (MIT) +# +# Copyright (c) 2013-2019 Rapptz, ThePhD, and contributors +# +# 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. + +# # # # sol3, documentation generation +# # # Required minimum version statement +cmake_minimum_required(VERSION 3.5.0) + +find_package(PythonInterp 3) + +if (NOT PYTHONINTERP_FOUND) + message(FATAL_ERROR "sol2 documentation cannot be generated as python 3 has not been found: install or set the python 3 interpreter for the docs to find it and be sure to pip install sphinx") +endif() + +find_program(sol2_make_executable make make.exe mingw32-make mingw32-make.exe) +if(NOT sol2_make_executable) + message(FATAL_ERROR "could not find a suitable make executable to build Sphinx documentation") +endif() + +add_custom_command(OUTPUT docs_invisible_file_always_generate + USES_TERMINAL + COMMAND "${sol2_make_executable}" -C "${CMAKE_CURRENT_SOURCE_DIR}" html "BUILDDIR=${CMAKE_CURRENT_BINARY_DIR}") +add_custom_target(docs + DEPENDS docs_invisible_file_always_generate) +install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/docs/html" DESTINATION "${CMAKE_INSTALL_DOCDIR}") diff --git a/docs/source/api/as_args.rst b/docs/source/api/as_args.rst index 9e158dad..9ad93bd8 100644 --- a/docs/source/api/as_args.rst +++ b/docs/source/api/as_args.rst @@ -15,7 +15,7 @@ as_args ``sol::as_args`` is a function that that takes an iterable and turns it into multiple arguments to a function call. It forwards its arguments, and is meant to be used as shown below: -.. literalinclude:: ../../../examples/args_from_container.cpp +.. literalinclude:: ../../../examples/source/args_from_container.cpp :caption: args_from_container.cpp :linenos: diff --git a/docs/source/api/as_function.rst b/docs/source/api/as_function.rst index 13afc36f..65cff856 100644 --- a/docs/source/api/as_function.rst +++ b/docs/source/api/as_function.rst @@ -12,7 +12,7 @@ This function serves the purpose of ensuring that a callable struct (like a lamb This class can also make it so usertypes bind variable types as functions to for usertype bindings. -.. literalinclude:: ../../../examples/docs/as_function.cpp +.. literalinclude:: ../../../examples/source/docs/as_function.cpp :linenos: @@ -20,5 +20,5 @@ Note that if you actually want a userdata, but you want it to be callable, you s Here's an example of binding a variable as a function to a usertype: -.. literalinclude:: ../../../examples/docs/as_function_usertype_member_variable.cpp +.. literalinclude:: ../../../examples/source/docs/as_function_usertype_member_variable.cpp :linenos: diff --git a/docs/source/api/as_returns.rst b/docs/source/api/as_returns.rst index 8f38fee5..66cb3940 100644 --- a/docs/source/api/as_returns.rst +++ b/docs/source/api/as_returns.rst @@ -15,5 +15,5 @@ as_returns This allows you to wrap up a source that has ``begin`` and ``end`` iterator-returning functions on it and return it as multiple results into Lua. To have more control over the returns, use :doc:`sol::variadic_results`. -.. literalinclude:: ../../../examples/as_returns.cpp +.. literalinclude:: ../../../examples/source/as_returns.cpp :linenos: diff --git a/docs/source/api/as_table.rst b/docs/source/api/as_table.rst index 2b9ed510..5fa0456a 100644 --- a/docs/source/api/as_table.rst +++ b/docs/source/api/as_table.rst @@ -13,7 +13,7 @@ as_table This function serves the purpose of ensuring that an object is pushed -- if possible -- like a table into Lua. The container passed here can be a pointer, a reference, a ``std::reference_wrapper`` around a container, or just a plain container value. It must have a begin/end function, and if it has a ``std::pair`` as its ``value_type``, it will be pushed as a dictionary. Otherwise, it's pushed as a sequence. -.. literalinclude:: ../../../examples/docs/as_table_ipairs.cpp +.. literalinclude:: ../../../examples/source/docs/as_table_ipairs.cpp :linenos: Note that any caveats with Lua tables apply the moment it is serialized, and the data cannot be gotten out back out in C++ as a C++ type. You can deserialize the Lua table into something explicitly using the ``sol::as_table_t`` marker for your get and conversion operations using Sol. At that point, the returned type is deserialized **from** a table, meaning you cannot reference any kind of C++ data directly as you do with regular userdata/usertypes. *All C++ type information is lost upon serialization into Lua.* diff --git a/docs/source/api/c_call.rst b/docs/source/api/c_call.rst index cc637b99..44356cfe 100644 --- a/docs/source/api/c_call.rst +++ b/docs/source/api/c_call.rst @@ -23,7 +23,7 @@ It is advisable for the user to consider making a macro to do the necessary ``de Here's an example below of various ways to use ``sol::c_call``: -.. literalinclude:: ../../../examples/c_call.cpp +.. literalinclude:: ../../../examples/source/c_call.cpp :linenos: .. _one similar to this: http://stackoverflow.com/a/5628222/5280922 diff --git a/docs/source/api/environment.rst b/docs/source/api/environment.rst index d8bcb33c..6ccb8825 100644 --- a/docs/source/api/environment.rst +++ b/docs/source/api/environment.rst @@ -18,7 +18,7 @@ This type is passed to :ref:`sol::state(_view)::script/do_x`. -The second and third unique constructors take a special empty type that serves as a key to trigger this constructor and serves no other purpose, ``sol::env_t``. The shortcut value so you don't have to create one is called ``sol::env_key``. It is used like ``sol::environment my_env(sol::env_key, some_object);``. It will extract the environment out of whatever the second argument is that may or may not have an environment. If it does not have an environment, the constructor will complete but the object will have ``env.valid() == false``, since it will reference Lua's ``nil``. +The second and third unique constructors take a special empty type that serves as a key to trigger this constructor and serves no other purpose, ``sol::env_key_t``. The shortcut value so you don't have to create one is called ``sol::env_key``. It is used like ``sol::environment my_env(sol::env_key, some_object);``. It will extract the environment out of whatever the second argument is that may or may not have an environment. If it does not have an environment, the constructor will complete but the object will have ``env.valid() == false``, since it will reference Lua's ``nil``. .. code-block:: cpp diff --git a/docs/source/api/function.rst b/docs/source/api/function.rst index dbd49ef9..8b77187b 100644 --- a/docs/source/api/function.rst +++ b/docs/source/api/function.rst @@ -22,14 +22,14 @@ Function is a correct-assuming version of :doc:`protected_function` to set/get a metatable. Lua metatables are powerful ways to override default behavior of objects for various kinds of operators, among other things. Here is an entirely complete example, showing getting and working with a :doc:`usertype`'s metatable defined by Sol: -.. literalinclude:: ../../../examples/metatable_key_low_level.cpp +.. literalinclude:: ../../../examples/source/metatable_key_low_level.cpp :caption: messing with metatables :linenos: diff --git a/docs/source/api/overload.rst b/docs/source/api/overload.rst index 8817aab8..5a7f5375 100644 --- a/docs/source/api/overload.rst +++ b/docs/source/api/overload.rst @@ -32,19 +32,19 @@ Its use is simple: wherever you can pass a function type to Lua, whether its on The functions can be any kind of function / function object (lambda). Given these functions and struct: -.. literalinclude:: ../../../examples/overloading_with_members.cpp +.. literalinclude:: ../../../examples/source/overloading_with_members.cpp :linenos: :lines: 1-27 You then use it just like you would for any other part of the api: -.. literalinclude:: ../../../examples/overloading_with_members.cpp +.. literalinclude:: ../../../examples/source/overloading_with_members.cpp :linenos: :lines: 29-45 Doing the following in Lua will call the specific overloads chosen, and their associated functions: -.. literalinclude:: ../../../examples/overloading_with_members.cpp +.. literalinclude:: ../../../examples/source/overloading_with_members.cpp :linenos: :lines: 47- diff --git a/docs/source/api/property.rst b/docs/source/api/property.rst index f944f595..7141bc9e 100644 --- a/docs/source/api/property.rst +++ b/docs/source/api/property.rst @@ -13,6 +13,6 @@ property These set of functions create a type which allows a setter and getter pair (or a single getter, or a single setter) to be used to create a variable that is either read-write, read-only, or write-only. When used during :doc:`usertype` construction, it will create a variable that uses the setter/getter member function specified. -.. literalinclude:: ../../../examples/property.cpp +.. literalinclude:: ../../../examples/source/property.cpp :linenos: diff --git a/docs/source/api/protect.rst b/docs/source/api/protect.rst index 72a8c06f..7788bd6b 100644 --- a/docs/source/api/protect.rst +++ b/docs/source/api/protect.rst @@ -10,5 +10,5 @@ protect ``sol::protect( my_func )`` allows you to protect a function call or member variable call when it is being set to Lua. It can be used with usertypes or when just setting a function into Sol. Below is an example that demonstrates that a call that would normally not error without :doc:`Safety features turned on<../safety>` that instead errors and makes the Lua safety-call wrapper ``pcall`` fail: -.. literalinclude:: ../../../examples/protect.cpp +.. literalinclude:: ../../../examples/source/protect.cpp :linenos: diff --git a/docs/source/api/protected_function.rst b/docs/source/api/protected_function.rst index 5edf668e..eee0855b 100644 --- a/docs/source/api/protected_function.rst +++ b/docs/source/api/protected_function.rst @@ -11,13 +11,13 @@ Inspired by a request from `starwing`_ in the :doc:`old sol repository<../origin When called without the return types being specified by either a ``sol::types<...>`` list or a ``call( ... )`` template type list, it generates a :doc:`protected_function_result` class that gets implicitly converted to the requested return type. For example: -.. literalinclude:: ../../../examples/error_handler.cpp +.. literalinclude:: ../../../examples/source/error_handler.cpp :linenos: :lines: 10-28 The following C++ code will call this function from this file and retrieve the return value, unless an error occurs, in which case you can bind an error handling function like so: -.. literalinclude:: ../../../examples/error_handler.cpp +.. literalinclude:: ../../../examples/source/error_handler.cpp :linenos: :lines: 1-6,30-66 @@ -26,7 +26,7 @@ This code is much more long-winded than its :doc:`function` counterpar Alternatively, with a bad or good function call, you can use ``sol::optional`` to check if the call succeeded or failed: -.. literalinclude:: ../../../examples/error_handler.cpp +.. literalinclude:: ../../../examples/source/error_handler.cpp :linenos: :lines: 67- diff --git a/docs/source/api/proxy.rst b/docs/source/api/proxy.rst index 03f01369..14e1fef1 100644 --- a/docs/source/api/proxy.rst +++ b/docs/source/api/proxy.rst @@ -27,19 +27,19 @@ proxy ``proxy`` is returned by lookups into :doc:`sol::table` and table-like entities. Because it is templated on key and table type, it would be hard to spell: you can capture it using the word ``auto`` if you feel like you need to carry it around for some reason before using it. ``proxy`` evaluates its arguments lazily, when you finally call ``get`` or ``set`` on it. Here are some examples given the following lua script: -.. literalinclude:: ../../../examples/table_proxy.cpp +.. literalinclude:: ../../../examples/source/table_proxy.cpp :linenos: :lines: 11-15 After loading that file in or putting it in a string and reading the string directly in lua (see :doc:`state`), you can start kicking around with it in C++ like so: -.. literalinclude:: ../../../examples/table_proxy.cpp +.. literalinclude:: ../../../examples/source/table_proxy.cpp :linenos: :lines: 1-8,18-40 We don't recommend using ``proxy`` lazy evaluation the above to be used across classes or between function: it's more of something you can do to save a reference to a value you like, call a script or run a lua function, and then get it afterwards. You can also set functions (and function objects) this way, and retrieve them as well: -.. literalinclude:: ../../../examples/table_proxy.cpp +.. literalinclude:: ../../../examples/source/table_proxy.cpp :linenos: :lines: 41- diff --git a/docs/source/api/readonly.rst b/docs/source/api/readonly.rst index 2dedc11b..99ff5aba 100644 --- a/docs/source/api/readonly.rst +++ b/docs/source/api/readonly.rst @@ -14,7 +14,7 @@ The goal of read-only is to protect a variable set on a usertype or a function. If you are looking to make a read-only table, you need to go through a bit of a complicated song and dance by overriding the ``__index`` metamethod. Here's a complete example on the way to do that using ``sol``: -.. literalinclude:: ../../../examples/read_only.cpp +.. literalinclude:: ../../../examples/source/read_only.cpp :caption: read_only.cpp :linenos: diff --git a/docs/source/api/stack.rst b/docs/source/api/stack.rst index cfa4b179..d9695d82 100644 --- a/docs/source/api/stack.rst +++ b/docs/source/api/stack.rst @@ -241,7 +241,7 @@ This is an SFINAE-friendly struct that is meant to expose static function ``get` :name: pusher template - struct pusher { + struct unqualified_pusher { template static int push ( lua_State* L, T&&, ... ) { // can optionally take more than just 1 argument diff --git a/docs/source/api/stack_reference.rst b/docs/source/api/stack_reference.rst index a72f6e85..98c341bf 100644 --- a/docs/source/api/stack_reference.rst +++ b/docs/source/api/stack_reference.rst @@ -17,7 +17,7 @@ This type is particular to working with the stack. It does not push the function Furthermore, if you know you have a function in the right place alongside proper arguments on top of it, you can use the ``sol::stack_count`` structure and give its constructor the number of arguments off the top that you want to call your pre-prepared function with: -.. literalinclude:: ../../../examples/stack_aligned_function.cpp +.. literalinclude:: ../../../examples/source/stack_aligned_function.cpp :caption: stack_aligned_function.cpp :linenos: :name: stack-aligned-function-example diff --git a/docs/source/api/state.rst b/docs/source/api/state.rst index a58789a3..fc8cbff9 100644 --- a/docs/source/api/state.rst +++ b/docs/source/api/state.rst @@ -100,7 +100,7 @@ If your script returns a value, you can capture it from the returned :ref:`sol:: To handle errors when using the second overload, provide a callable function/object that takes a ``lua_State*`` as its first argument and a ``sol::protected_function_result`` as its second argument. ``sol::script_default_on_error`` and ``sol::script_pass_on_error`` are 2 functions provided by sol that will either generate a traceback error to return / throw (if throwing is allowed); or, pass the error on through and return it to the user (respectively). An example of having your: -.. literalinclude:: ../../../examples/docs/state_script_safe.cpp +.. literalinclude:: ../../../examples/source/docs/state_script_safe.cpp :linenos: :name: state-script-safe diff --git a/docs/source/api/this_environment.rst b/docs/source/api/this_environment.rst index 9fd7c0d4..f08f4a81 100644 --- a/docs/source/api/this_environment.rst +++ b/docs/source/api/this_environment.rst @@ -9,7 +9,7 @@ Sometimes in C++ it's useful to know where a Lua call is coming from and what :d :linenos: #define SOL_CHECK_ARGUMENTS - #include + #include #include diff --git a/docs/source/api/this_state.rst b/docs/source/api/this_state.rst index c0779058..46eec7b8 100644 --- a/docs/source/api/this_state.rst +++ b/docs/source/api/this_state.rst @@ -9,5 +9,5 @@ this_state This class is a transparent type that is meant to be gotten in functions to get the current lua state a bound function or usertype method is being called from. It does not actually retrieve anything from lua nor does it increment the argument count, making it "invisible" to function calls in lua and calls through ``std::function<...>`` and :doc:`sol::function` on this type. It can be put in any position in the argument list of a function: -.. literalinclude:: ../../../examples/this_state.cpp +.. literalinclude:: ../../../examples/source/this_state.cpp :linenos: diff --git a/docs/source/api/tie.rst b/docs/source/api/tie.rst index df9151cd..23fd09f0 100644 --- a/docs/source/api/tie.rst +++ b/docs/source/api/tie.rst @@ -5,7 +5,7 @@ tie `std::tie()`_ does not work well with :doc:`sol::function`'s ``sol::function_result`` returns. Use ``sol::tie`` instead. Because they're both named `tie`, you'll need to be explicit when you use Sol's by naming it with the namespace (``sol::tie``), even with a ``using namespace sol;``. Here's an example: -.. literalinclude:: ../../../examples/tie.cpp +.. literalinclude:: ../../../examples/source/tie.cpp :linenos: .. _std::tie(): http://en.cppreference.com/w/cpp/utility/tuple/tie diff --git a/docs/source/api/usertype.rst b/docs/source/api/usertype.rst index 9f0a4f99..01608886 100644 --- a/docs/source/api/usertype.rst +++ b/docs/source/api/usertype.rst @@ -199,7 +199,7 @@ runtime functions You can add functions at runtime **to the whole class** (not to individual objects). Set a name under the metatable name you bound using ``new_usertype`` to an object. For example: -.. literalinclude:: ../../../examples/docs/runtime_extension.cpp +.. literalinclude:: ../../../examples/source/docs/runtime_extension.cpp :caption: runtime_extension.cpp :name: runtime-extension-example :linenos: @@ -232,7 +232,7 @@ Register the base classes explicitly. Always specify your bases if you plan to retrieve a base class using the Sol abstraction directly and not casting yourself. -.. literalinclude:: ../../../examples/docs/inheritance.cpp +.. literalinclude:: ../../../examples/source/docs/inheritance.cpp :caption: inheritance.cpp :name: inheritance-example :linenos: diff --git a/docs/source/api/var.rst b/docs/source/api/var.rst index cf8df970..0c6d442f 100644 --- a/docs/source/api/var.rst +++ b/docs/source/api/var.rst @@ -5,5 +5,5 @@ var The sole purpose of this tagging type is to work with :doc:`usertypes` to provide ``my_class.my_static_var`` access, and to also provide reference-based access as well. -.. literalinclude:: ../../../examples/usertype_var.cpp +.. literalinclude:: ../../../examples/source/usertype_var.cpp :linenos: diff --git a/docs/source/api/variadic_args.rst b/docs/source/api/variadic_args.rst index 7d230665..b7c2d3df 100644 --- a/docs/source/api/variadic_args.rst +++ b/docs/source/api/variadic_args.rst @@ -11,15 +11,15 @@ This class is meant to represent every single argument at its current index and ``variadic_args`` also has ``begin()`` and ``end()`` functions that return (almost) random-acess iterators. These return a proxy type that can be implicitly converted to a type you want, much like the :doc:`table proxy type`. -.. literalinclude:: ../../../examples/variadic_args.cpp +.. literalinclude:: ../../../examples/source/variadic_args.cpp :linenos: You can also "save" arguments and the like later, by stuffing them into a ``std::vector`` or something similar that serializes them into the registry. Below is an example of saving all of the arguments provided by ``sol::variadic_args`` in a lambda capture variable called ``args``. -.. literalinclude:: ../../../examples/variadic_args_storage.cpp +.. literalinclude:: ../../../examples/source/variadic_args_storage.cpp :linenos: Finally, note that you can use ``sol::variadic_args`` constructor to "offset"/"shift over" the arguments being viewed: -.. literalinclude:: ../../../examples/variadic_args_shifted.cpp +.. literalinclude:: ../../../examples/source/variadic_args_shifted.cpp :linenos: diff --git a/docs/source/conf.py b/docs/source/conf.py index 795d96a4..852bb4bd 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -55,9 +55,9 @@ author = 'ThePhD' # built documents. # # The short X.Y version. -version = '2.20' +version = '3.0' # The full version, including alpha/beta/rc tags. -release = '2.20.5' +release = '3.0.0' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/docs/source/containers.rst b/docs/source/containers.rst index 41a85cb4..cea8d73d 100644 --- a/docs/source/containers.rst +++ b/docs/source/containers.rst @@ -217,7 +217,7 @@ a complete example Here's a complete working example of it working for Lua 5.3 and Lua 5.2, and how you can retrieve out the container in all versions: -.. literalinclude:: ../../examples/containers.cpp +.. literalinclude:: ../../examples/source/containers.cpp :name: containers-example :linenos: @@ -236,7 +236,7 @@ There are also other ways to iterate over key/values, but they can be difficult If you can't upgrade, use the "member" function ``my_container:pairs()`` in Lua to perform iteration: -.. literalinclude:: ../../examples/container_with_pairs.cpp +.. literalinclude:: ../../examples/source/container_with_pairs.cpp :name: containers-pairs-example :linenos: diff --git a/docs/source/exceptions.rst b/docs/source/exceptions.rst index 93e15416..870dc0f3 100644 --- a/docs/source/exceptions.rst +++ b/docs/source/exceptions.rst @@ -9,7 +9,7 @@ If you turn this off, the default `at_panic`_ function :doc:`state` s To make this not be the case, you can set a panic function directly with ``lua_atpanic( lua, my_panic_function );`` or when you create the ``sol::state`` with ``sol::state lua(my_panic_function);``. Here's an example ``my_panic_function`` you can have that prints out its errors: -.. literalinclude:: ../../examples/docs/my_panic.cpp +.. literalinclude:: ../../examples/source/docs/my_panic.cpp :caption: typical panic function :name: typical-panic-function :linenos: @@ -26,7 +26,7 @@ various sol and lua handlers Lua comes with two kind of built-in handlers that sol provides easy opt-ins for. One is the ``panic`` function, as :ref:`demonstrated above`. Another is the ``pcall`` error handler, used with :doc:`sol::protected_function`. It is any function that takes a single argument. The single argument is the error type being passed around: in Lua, this is a single string message: -.. literalinclude:: ../../examples/protected_functions.cpp +.. literalinclude:: ../../examples/source/protected_functions.cpp :caption: regular error handling :name: regular-error-handling :linenos: @@ -34,7 +34,7 @@ Lua comes with two kind of built-in handlers that sol provides easy opt-ins for. The other handler is specific to sol2. If you open a ``sol::state``, or open the default state handlers for your ``lua_State*`` (see :ref:`sol::state's automatic handlers` for more details), there is a ``sol::exception_handler_function`` type. It allows you to register a function in the event that an exception happens that bubbles out of your functions. The function requires that you push 1 item onto the stack that will be used with a call to `lua_error`_ -.. literalinclude:: ../../examples/exception_handler.cpp +.. literalinclude:: ../../examples/source/exception_handler.cpp :caption: exception handling :name: exception-handling :linenos: diff --git a/docs/source/functions.rst b/docs/source/functions.rst index 5bb2ea32..892986f3 100644 --- a/docs/source/functions.rst +++ b/docs/source/functions.rst @@ -42,7 +42,7 @@ To be explicit about wanting a struct to be interpreted as a function, use ``my_ Furthermore, it is important to know that lambdas without a specified return type (and a non-const, non-reference-qualified ``auto``) will decay return values. To capture or return references explicitly, use ``decltype(auto)`` or specify the return type **exactly** as desired: -.. literalinclude:: ../../examples/docs/references_in_lambdas.cpp +.. literalinclude:: ../../examples/source/docs/references_in_lambdas.cpp :name: refereces-in-lambdas-example :linenos: diff --git a/docs/source/index.rst b/docs/source/index.rst index 2dd6f1ec..eab9d860 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -57,11 +57,11 @@ the basics: The code below *and* more examples can be found in the `examples directory`_ -.. literalinclude:: ../../examples/docs/simple_functions.cpp +.. literalinclude:: ../../examples/source/docs/simple_functions.cpp :name: simple-functions-example :linenos: -.. literalinclude:: ../../examples/docs/simple_structs.cpp +.. literalinclude:: ../../examples/source/docs/simple_structs.cpp :name: simple-structs-example :linenos: diff --git a/docs/source/threading.rst b/docs/source/threading.rst index fde5c3b1..bbf4c0c4 100644 --- a/docs/source/threading.rst +++ b/docs/source/threading.rst @@ -34,6 +34,6 @@ You can mitigate some of the pressure of using coroutines and threading by using Here's an example of explicit state transferring below: -.. literalinclude:: ../../examples/docs/state_transfer.cpp +.. literalinclude:: ../../examples/source/docs/state_transfer.cpp :name: state-transfer :linenos: diff --git a/docs/source/tutorial/all-the-things.rst b/docs/source/tutorial/all-the-things.rst index 686ce2af..3656546d 100644 --- a/docs/source/tutorial/all-the-things.rst +++ b/docs/source/tutorial/all-the-things.rst @@ -14,11 +14,11 @@ These are all the things. Use your browser's search to find things you want. asserts / prerequisites ----------------------- -You'll need to ``#include ``/``#include "sol.hpp"`` somewhere in your code. Sol is header-only, so you don't need to compile anything. However, **Lua must be compiled and available**. See the :doc:`getting started tutorial` for more details. +You'll need to ``#include ``/``#include "sol.hpp"`` somewhere in your code. Sol is header-only, so you don't need to compile anything. However, **Lua must be compiled and available**. See the :doc:`getting started tutorial` for more details. The implementation for ``assert.hpp`` with ``c_assert`` looks like so: -.. literalinclude:: ../../../examples/assert.hpp +.. literalinclude:: ../../../examples/source/assert.hpp :linenos: :lines: 1-3, 19- @@ -27,7 +27,7 @@ This is the assert used in the quick code below. opening a state --------------- -.. literalinclude:: ../../../examples/tutorials/quick_n_dirty/opening_a_state.cpp +.. literalinclude:: ../../../examples/source/tutorials/quick_n_dirty/opening_a_state.cpp :linenos: @@ -39,7 +39,7 @@ using sol2 on a lua_State\* For your system/game that already has Lua or uses an in-house or pre-rolled Lua system (LuaBridge, kaguya, Luwra, etc.), but you'd still like sol2 and nice things: -.. literalinclude:: ../../../examples/tutorials/quick_n_dirty/opening_state_on_raw_lua.cpp +.. literalinclude:: ../../../examples/source/tutorials/quick_n_dirty/opening_state_on_raw_lua.cpp :linenos: .. _running-lua-code: @@ -47,13 +47,13 @@ For your system/game that already has Lua or uses an in-house or pre-rolled Lua running lua code ---------------- -.. literalinclude:: ../../../examples/tutorials/quick_n_dirty/running_lua_code.cpp +.. literalinclude:: ../../../examples/source/tutorials/quick_n_dirty/running_lua_code.cpp :linenos: :lines: 1-10, 16-26 To run Lua code but have an error handler in case things go wrong: -.. literalinclude:: ../../../examples/tutorials/quick_n_dirty/running_lua_code.cpp +.. literalinclude:: ../../../examples/source/tutorials/quick_n_dirty/running_lua_code.cpp :linenos: :lines: 28-39,47- @@ -68,46 +68,49 @@ You can use the individual load and function call operator to load, check, and t This is ONLY if you need some sort of fine-grained control: for 99% of cases, :ref:`running lua code` is preferred and avoids pitfalls in not understanding the difference between script/load and needing to run a chunk after loading it. -.. literalinclude:: ../../../examples/tutorials/quick_n_dirty/running_lua_code_low_level.cpp +.. literalinclude:: ../../../examples/source/tutorials/quick_n_dirty/running_lua_code_low_level.cpp :linenos: :lines: 1-10, 16-40, 47-49 +You can also `develop custom loaders`_ that pull from things that are not strings or files. + + set and get variables --------------------- You can set/get everything using table-like syntax. -.. literalinclude:: ../../../examples/tutorials/quick_n_dirty/set_and_get_variables.cpp +.. literalinclude:: ../../../examples/source/tutorials/quick_n_dirty/set_and_get_variables.cpp :linenos: :lines: 1-19 Equivalent to loading lua values like so: -.. literalinclude:: ../../../examples/tutorials/quick_n_dirty/set_and_get_variables.cpp +.. literalinclude:: ../../../examples/source/tutorials/quick_n_dirty/set_and_get_variables.cpp :linenos: :lines: 22-34 You can show they are equivalent: -.. literalinclude:: ../../../examples/tutorials/quick_n_dirty/set_and_get_variables.cpp +.. literalinclude:: ../../../examples/source/tutorials/quick_n_dirty/set_and_get_variables.cpp :linenos: :lines: 36-44 Retrieve these variables using this syntax: -.. literalinclude:: ../../../examples/tutorials/quick_n_dirty/set_and_get_variables.cpp +.. literalinclude:: ../../../examples/source/tutorials/quick_n_dirty/set_and_get_variables.cpp :linenos: :lines: 45-64 Retrieve Lua types using ``object`` and other ``sol::`` types. -.. literalinclude:: ../../../examples/tutorials/quick_n_dirty/set_and_get_variables.cpp +.. literalinclude:: ../../../examples/source/tutorials/quick_n_dirty/set_and_get_variables.cpp :linenos: :lines: 66- You can erase things by setting it to ``nullptr`` or ``sol::lua_nil``. -.. literalinclude:: ../../../examples/tutorials/quick_n_dirty/set_and_get_variables_exists.cpp +.. literalinclude:: ../../../examples/source/tutorials/quick_n_dirty/set_and_get_variables_exists.cpp :linenos: Note that if its a :doc:`userdata/usertype<../api/usertype>` for a C++ type, the destructor will run only when the garbage collector deems it appropriate to destroy the memory. If you are relying on the destructor being run when its set to ``sol::lua_nil``, you're probably committing a mistake. @@ -117,13 +120,13 @@ tables Tables can be manipulated using accessor-syntax. Note that :doc:`sol::state<../api/state>` is a table and all the methods shown here work with ``sol::state``, too. -.. literalinclude:: ../../../examples/tutorials/quick_n_dirty/tables_and_nesting.cpp +.. literalinclude:: ../../../examples/source/tutorials/quick_n_dirty/tables_and_nesting.cpp :linenos: :lines: 1-34 If you're going deep, be safe: -.. literalinclude:: ../../../examples/tutorials/quick_n_dirty/tables_and_nesting.cpp +.. literalinclude:: ../../../examples/source/tutorials/quick_n_dirty/tables_and_nesting.cpp :linenos: :lines: 35- @@ -132,13 +135,13 @@ make tables There are many ways to make a table. Here's an easy way for simple ones: -.. literalinclude:: ../../../examples/tutorials/quick_n_dirty/make_tables.cpp +.. literalinclude:: ../../../examples/source/tutorials/quick_n_dirty/make_tables.cpp :linenos: :lines: 1-21 Equivalent Lua code, and check that they're equivalent: -.. literalinclude:: ../../../examples/tutorials/quick_n_dirty/make_tables.cpp +.. literalinclude:: ../../../examples/source/tutorials/quick_n_dirty/make_tables.cpp :linenos: :lines: 22- @@ -152,7 +155,7 @@ functions They're easy to use, from Lua and from C++: -.. literalinclude:: ../../../examples/tutorials/quick_n_dirty/functions_easy.cpp +.. literalinclude:: ../../../examples/source/tutorials/quick_n_dirty/functions_easy.cpp :linenos: :lines: 1- @@ -160,13 +163,13 @@ If you need to protect against errors and parser problems and you're not ready t You can bind member variables as functions too, as well as all KINDS of function-like things: -.. literalinclude:: ../../../examples/tutorials/quick_n_dirty/functions_all.cpp +.. literalinclude:: ../../../examples/source/tutorials/quick_n_dirty/functions_all.cpp :linenos: :lines: 1-50 The lua code to call these things is: -.. literalinclude:: ../../../examples/tutorials/quick_n_dirty/functions_all.cpp +.. literalinclude:: ../../../examples/source/tutorials/quick_n_dirty/functions_all.cpp :linenos: :lines: 51- @@ -178,7 +181,7 @@ self call You can pass the ``self`` argument through C++ to emulate 'member function' calls in Lua. -.. literalinclude:: ../../../examples/tutorials/quick_n_dirty/self_call.cpp +.. literalinclude:: ../../../examples/source/tutorials/quick_n_dirty/self_call.cpp :linenos: :lines: 1- @@ -186,7 +189,7 @@ You can pass the ``self`` argument through C++ to emulate 'member function' call multiple returns from lua ------------------------- -.. literalinclude:: ../../../examples/tutorials/quick_n_dirty/multiple_returns_from_lua.cpp +.. literalinclude:: ../../../examples/source/tutorials/quick_n_dirty/multiple_returns_from_lua.cpp :linenos: :lines: 1- @@ -194,7 +197,7 @@ multiple returns from lua multiple returns to lua ----------------------- -.. literalinclude:: ../../../examples/tutorials/quick_n_dirty/multiple_returns_to_lua.cpp +.. literalinclude:: ../../../examples/source/tutorials/quick_n_dirty/multiple_returns_to_lua.cpp :linenos: :lines: 1- @@ -214,7 +217,7 @@ Everything that is not a: Is set as a :doc:`userdata + usertype<../api/usertype>`. -.. literalinclude:: ../../../examples/tutorials/quick_n_dirty/userdata.cpp +.. literalinclude:: ../../../examples/source/tutorials/quick_n_dirty/userdata.cpp :linenos: :lines: 1-57,97- @@ -222,13 +225,13 @@ Is set as a :doc:`userdata + usertype<../api/usertype>`. If you want it to refer to something, whose memory you know won't die in C++ while it is used/exists in Lua, do the following: -.. literalinclude:: ../../../examples/tutorials/quick_n_dirty/userdata_memory_reference.cpp +.. literalinclude:: ../../../examples/source/tutorials/quick_n_dirty/userdata_memory_reference.cpp :linenos: :lines: 1-45 You can retrieve the userdata in the same way as everything else. Importantly, note that you can change the data of usertype variables and it will affect things in lua if you get a pointer or a reference: -.. literalinclude:: ../../../examples/tutorials/quick_n_dirty/userdata_memory_reference.cpp +.. literalinclude:: ../../../examples/source/tutorials/quick_n_dirty/userdata_memory_reference.cpp :linenos: :lines: 46- @@ -246,7 +249,7 @@ namespacing You can emulate namespacing by having a table and giving it the namespace names you want before registering enums or usertypes: -.. literalinclude:: ../../../examples/tutorials/quick_n_dirty/namespacing.cpp +.. literalinclude:: ../../../examples/source/tutorials/quick_n_dirty/namespacing.cpp :linenos: :lines: 1- @@ -271,6 +274,7 @@ Some more things you can do/read about: * :doc:`stack references<../api/stack_reference>` to have zero-overhead Sol abstractions while not copying to the Lua registry. * :doc:`resolve<../api/resolve>` overloads in case you have overloaded functions; a cleaner casting utility. You must use this to emulate default parameters. +.. _develop custom loaders: https://github.com/ThePhD/sol2/blob/develop/examples/custom_reader.cpp .. _basic example: https://github.com/ThePhD/sol2/blob/develop/examples/usertype.cpp .. _special functions example: https://github.com/ThePhD/sol2/blob/develop/examples/usertype_special_functions.cpp .. _initializers example: https://github.com/ThePhD/sol2/blob/develop/examples/usertype_initializers.cpp diff --git a/docs/source/tutorial/customization.rst b/docs/source/tutorial/customization.rst index 5140cedd..f9aac2e8 100644 --- a/docs/source/tutorial/customization.rst +++ b/docs/source/tutorial/customization.rst @@ -1,21 +1,21 @@ adding your own types ===================== -Sometimes, overriding Sol to make it handle certain ``struct``'s and ``class``'es as something other than just userdata is desirable. The way to do this is to take advantage of the 4 customization points for Sol. These are ``sol::lua_size``, ``sol::stack::pusher``, ``sol::stack::getter``, ``sol::stack::checker``. +Sometimes, overriding Sol to make it handle certain ``struct``'s and ``class``'es as something other than just userdata is desirable. The way to do this is to take advantage of the 4 customization points for Sol. These are ``sol::lua_size``, ``sol::stack::unqualified_pusher``, ``sol::stack::getter``, ``sol::stack::checker``. These are template class/structs, so you'll override them using a technique C++ calls *class/struct specialization*. Below is an example of a struct that gets broken apart into 2 pieces when going in the C++ --> Lua direction, and then pulled back into a struct when going in the Lua --> C++: -.. literalinclude:: ../../../examples/customization.cpp +.. literalinclude:: ../../../examples/source/customization_multiple.cpp :name: customization-overriding :linenos: - :lines: 1-72 + :lines: 1-52 This is the base formula that you can follow to extend to your own classes. Using it in the rest of the library should then be seamless: -.. literalinclude:: ../../../examples/customization.cpp +.. literalinclude:: ../../../examples/source/customization_multiple.cpp :name: customization-overriding-use :linenos: - :lines: 73- + :lines: 52- And that's it! diff --git a/docs/source/tutorial/cxx-in-lua.rst b/docs/source/tutorial/cxx-in-lua.rst index fe4f0731..9ebdf7d6 100644 --- a/docs/source/tutorial/cxx-in-lua.rst +++ b/docs/source/tutorial/cxx-in-lua.rst @@ -93,7 +93,7 @@ To do this, you bind things using the ``new_usertype`` and ``set_usertype`` meth .. code-block:: cpp :caption: player_script.cpp - #include + #include int main () { sol::state lua; diff --git a/docs/source/tutorial/functions.rst b/docs/source/tutorial/functions.rst index 75afac7b..c6c0e31a 100644 --- a/docs/source/tutorial/functions.rst +++ b/docs/source/tutorial/functions.rst @@ -13,7 +13,7 @@ Given a C++ function, you can drop it into Sol in several equivalent ways, worki :caption: Registering C++ functions :name: writing-functions - #include + #include std::string my_function( int a, std::string b ) { // Create a string with the letter 'D' "a" times, diff --git a/docs/source/tutorial/getting-started.rst b/docs/source/tutorial/getting-started.rst index 570de472..1cf8079d 100644 --- a/docs/source/tutorial/getting-started.rst +++ b/docs/source/tutorial/getting-started.rst @@ -16,7 +16,7 @@ When you're ready, try compiling this short snippet: :caption: test.cpp: the first snippet :name: the-first-snippet - #include // or #include "sol.hpp", whichever suits your needs + #include // or #include "sol.hpp", whichever suits your needs int main (int argc, char* argv[]) { @@ -47,7 +47,7 @@ The second line opens a single lua-provided library, "base". There are several o :caption: test.cpp: the first snippet :name: the-second-snippet - #include + #include int main (int argc, char* argv[]) { diff --git a/docs/source/tutorial/variables.rst b/docs/source/tutorial/variables.rst index 1222643b..af2eaea0 100644 --- a/docs/source/tutorial/variables.rst +++ b/docs/source/tutorial/variables.rst @@ -8,28 +8,28 @@ reading Given this lua file that gets loaded into sol: -.. literalinclude:: ../../../examples/tutorials/variables_demo.cpp +.. literalinclude:: ../../../examples/source/tutorials/variables_demo.cpp :linenos: :lines: 15-18 You can interact with the Lua Virtual Machine like so: -.. literalinclude:: ../../../examples/tutorials/variables_demo.cpp +.. literalinclude:: ../../../examples/source/tutorials/variables_demo.cpp :linenos: :lines: 1-10, 12-12, 20-24, 70- From this example, you can see that there's many ways to pull out the varaibles you want. For example, to determine if a nested variable exists or not, you can use ``auto`` to capture the value of a ``table[key]`` lookup, and then use the ``.valid()`` method: -.. literalinclude:: ../../../examples/tutorials/variables_demo.cpp +.. literalinclude:: ../../../examples/source/tutorials/variables_demo.cpp :linenos: :lines: 1-10, 12-12, 34-43, 70- This comes in handy when you want to check if a nested variable exists. You can also check if a toplevel variable is present or not by using ``sol::optional``, which also checks if A) the keys you're going into exist and B) the type you're trying to get is of a specific type: -.. literalinclude:: ../../../examples/tutorials/variables_demo.cpp +.. literalinclude:: ../../../examples/source/tutorials/variables_demo.cpp :linenos: :caption: optional lookup :lines: 1-10, 12-12, 43-58, 70- @@ -37,7 +37,7 @@ This comes in handy when you want to check if a nested variable exists. You can This can come in handy when, even in optimized or release modes, you still want the safety of checking. You can also use the `get_or` methods to, if a certain value may be present but you just want to default the value to something else: -.. literalinclude:: ../../../examples/tutorials/variables_demo.cpp +.. literalinclude:: ../../../examples/source/tutorials/variables_demo.cpp :linenos: :caption: optional lookup :lines: 1-10, 12-12, 60- @@ -51,19 +51,19 @@ writing Writing gets a lot simpler. Even without scripting a file or a string, you can read and write variables into lua as you please: -.. literalinclude:: ../../../examples/tutorials/write_variables_demo.cpp +.. literalinclude:: ../../../examples/source/tutorials/write_variables_demo.cpp :linenos: :name: writing-variables-demo This example pretty much sums up what can be done. Note that the syntax ``lua["non_existing_key_1"] = 1`` will make that variable, but if you tunnel too deep without first creating a table, the Lua API will panic (e.g., ``lua["does_not_exist"]["b"] = 20`` will trigger a panic). You can also be lazy with reading / writing values: -.. literalinclude:: ../../../examples/tutorials/lazy_demo.cpp +.. literalinclude:: ../../../examples/source/tutorials/lazy_demo.cpp :linenos: Finally, it's possible to erase a reference/variable by setting it to ``nil``, using the constant ``sol::nil`` in C++: -.. literalinclude:: ../../../examples/tutorials/erase_demo.cpp +.. literalinclude:: ../../../examples/source/tutorials/erase_demo.cpp :linenos: diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index f4bb3d19..2ac93f1e 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,7 +1,7 @@ # # # # sol2 # The MIT License (MIT) # -# Copyright (c) 2013-2018 Rapptz, ThePhD, and contributors +# Copyright (c) 2013-2019 Rapptz, ThePhD, and contributors # # 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 @@ -22,82 +22,110 @@ # # # sol2 Examples -if (DYNAMIC_LOADING_EXAMPLES) +if (SOL2_DYNAMIC_LOADING_EXAMPLES OR SOL2_DYNAMIC_LOADING_EXAMPLES_SINGLE OR SOL2_DYNAMIC_LOADING_EXAMPLES_SINGLE_GENERATED) # # require_from_dll example # just add the subdirectory add_subdirectory(require_dll_example) -endif(DYNAMIC_LOADING_EXAMPLES) +endif() -if (INTEROP_EXAMPLES) +if (SOL2_INTEROP_EXAMPLES OR SOL2_INTEROP_EXAMPLES_SINGLE OR SOL2_INTEROP_EXAMPLES_SINGLE_GENERATED) # # interop examples add_subdirectory(interop/kaguya) add_subdirectory(interop/tolua) add_subdirectory(interop/LuaBridge) add_subdirectory(interop/luwra) -endif(INTEROP_EXAMPLES) +endif() + +# # Utility assert.hpp "library" +add_library(sol2_assert INTERFACE) +add_library(sol2::assert ALIAS sol2_assert) +set_target_properties(sol2_assert + PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/include") +if (SOL2_CI) + target_compile_definitions(sol2_assert + INTERFACE SOL2_CI) +endif() # # single-source compilable examples -file(GLOB EXAMPLES_SRC *.cpp tutorials/quick_n_dirty/*.cpp docs/*.cpp) +file(GLOB EXAMPLES_SRC source/*.cpp source/tutorials/quick_n_dirty/*.cpp source/docs/*.cpp) source_group(examples FILES ${EXAMPLES_SRC}) -function (MAKE_EXAMPLE example_source_file is_single) +function (MAKE_EXAMPLE example_source_file example_suffix target_sol) + get_filename_component(example_name ${example_source_file} NAME_WE) file(RELATIVE_PATH example_source_file_relative ${CMAKE_SOURCE_DIR} ${example_source_file}) get_filename_component(example_output_relative_dir ${example_source_file_relative} DIRECTORY) file(TO_CMAKE_PATH "${example_output_relative_dir}" example_output_relative_dir_name) STRING(REGEX REPLACE "/" "." example_output_relative_dir_name "${example_output_relative_dir}") - if (is_single) - set(example_name "${example_name}.single") - endif() + set(example_name "${example_name}${example_suffix}") + if (example_output_relative_dir_name STREQUAL "") set(example_output_name "${example_name}") else() set(example_output_name "${example_output_relative_dir_name}.${example_name}") endif() - add_executable(${example_name} ${example_source_file} assert.hpp) + add_executable(${example_name} ${example_source_file}) set_target_properties(${example_name} PROPERTIES - OUTPUT_NAME "${example_output_name}") - if (is_single) - target_link_libraries(${example_name} sol2_single) - else() - target_link_libraries(${example_name} sol2) - endif() - target_compile_features(${example_name} - PRIVATE ${CXX_FEATURES}) - target_link_libraries(${example_name} ${LUA_LIBRARIES}) - if(CMAKE_DL_LIBS) - target_link_libraries(${example_name} ${CMAKE_DL_LIBS}) - endif() - if (CI) - target_compile_definitions(${example_name} - PRIVATE SOL2_CI) - endif() + OUTPUT_NAME "${example_output_name}" + EXPORT_NAME sol2::${example_output_name}) + if (MSVC) - + target_compile_options(${example_name} + PRIVATE /std:c++latest /EHsc "$<$:/MDd>" + "$<$:/MD>" + "$<$:/MD>" + "$<$:/MD>") + target_compile_definitions(${example_name} + PRIVATE UNICODE _UNICODE + _CRT_SECURE_NO_WARNINGS _CRT_SECURE_NO_DEPRECATE ) else() target_compile_options(${example_name} - PRIVATE -Wno-noexcept-type) + PRIVATE -std=c++1z + -ftemplate-backtrace-limit=0 + -Wno-unknown-warning -Wno-unknown-warning-option + -Wall -Wpedantic -Werror -pedantic -pedantic-errors + -Wno-noexcept-type) + + if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") + # For another day, when C++ is not so crap + # and we have time to audit the entire lib + # for all uses of `detail::swallow`... + #target_compile_options(${example_name} + # PRIVATE -Wcomma) + endif() endif() - if (TESTS_EXAMPLES) - if ((NOT is_single) OR (is_single AND TESTS_SINGLE)) - add_test(NAME ${example_output_name} COMMAND ${example_name}) - endif() + target_link_libraries(${example_name} + PRIVATE ${target_sol} ${LUA_LIBRARIES} sol2::assert) + + if(CMAKE_DL_LIBS) + target_link_libraries(${example_name} PRIVATE ${CMAKE_DL_LIBS}) + endif() + + if (SOL2_TESTS_EXAMPLES) + add_test(NAME ${example_output_name} COMMAND ${example_name}) endif() install(TARGETS ${example_name} RUNTIME DESTINATION bin) endfunction(MAKE_EXAMPLE) -if (EXAMPLES) +if (SOL2_EXAMPLES) foreach(example_source_file ${EXAMPLES_SRC}) - MAKE_EXAMPLE(${example_source_file} FALSE) + MAKE_EXAMPLE(${example_source_file} "" sol2::sol2) + endforeach() +endif() + +if (SOL2_EXAMPLES_SINGLE) + foreach(example_source_file ${EXAMPLES_SRC}) + MAKE_EXAMPLE(${example_source_file} ".single" sol2::sol2_single) + endforeach() +endif() + +if (SOL2_EXAMPLES_SINGLE_GENERATED) + foreach(example_source_file ${EXAMPLES_SRC}) + MAKE_EXAMPLE(${example_source_file} ".single.generated" sol2::sol2_single_generated) endforeach() - - if (SOL2_SINGLE_FOUND AND EXAMPLES_SINGLE) - foreach(example_source_file ${EXAMPLES_SRC}) - MAKE_EXAMPLE(${example_source_file} TRUE) - endforeach() - endif() endif() diff --git a/examples/custom_global_transparent_argument.cpp b/examples/custom_global_transparent_argument.cpp deleted file mode 100644 index 62b4e007..00000000 --- a/examples/custom_global_transparent_argument.cpp +++ /dev/null @@ -1,74 +0,0 @@ -#define SOL_CHECK_ARGUMENTS 1 -#include - -// Something that can't be collided with -static const auto& script_key = "GlobalResource.MySpecialIdentifier123"; - -struct GlobalResource { - int value = 2; -}; - -// Customize sol2 to handle this type -namespace sol { - template <> - struct lua_type_of : std::integral_constant {}; - - namespace stack { - template <> - struct checker { - template - static bool check(lua_State* L, int /*index*/, Handler&& handler, record& tracking) { - tracking.use(0); - // get the field from global storage - stack::get_field(L, script_key); - // verify type - type t = static_cast(lua_type(L, -1)); - lua_pop(L, 1); - if (t != type::lightuserdata) { - handler(L, 0, type::lightuserdata, t, "global resource is not present"); - return false; - } - return true; - } - }; - - template <> - struct getter { - static GlobalResource* get(lua_State* L, int /*index*/, record& tracking) { - // retrieve the (light) userdata for this type - tracking.use(0); // not actually pulling anything off the stack - stack::get_field(L, script_key); - GlobalResource* ls = static_cast(lua_touserdata(L, -1)); - lua_pop(L, 1); // clean up stack value returned by `get_field` - return ls; - }; - }; - template <> - struct pusher { - static int push(lua_State* L, GlobalResource* ls) { - // push light userdata - return stack::push(L, make_light(ls));; - } - }; - } -} - -int main() { - - sol::state lua; - lua.open_libraries(sol::lib::base); - - GlobalResource instance; - - // get GlobalResource - lua.set_function("f", [](GlobalResource* l, int value) { - return l->value + value; - }); - lua.set(script_key, &instance); - - // note only 1 argument, - // despite being 2 - lua.script("assert(f(1) == 3)"); - - return 0; -} diff --git a/examples/customization.cpp b/examples/customization.cpp deleted file mode 100644 index 46e15e43..00000000 --- a/examples/customization.cpp +++ /dev/null @@ -1,98 +0,0 @@ -#define SOL_CHECK_ARGUMENTS 1 -#include - -#include -#include "assert.hpp" - -struct two_things { - int a; - bool b; -}; - -namespace sol { - - // First, the expected size - // Specialization of a struct - template <> - struct lua_size : std::integral_constant {}; - - // Then, specialize the type - // this makes sure Sol can return it properly - template <> - struct lua_type_of : std::integral_constant {}; - - // Now, specialize various stack structures - namespace stack { - - template <> - struct checker { - template - static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { - // indices can be negative to count backwards from the top of the stack, - // rather than the bottom up - // to deal with this, we adjust the index to - // its absolute position using the lua_absindex function - int absolute_index = lua_absindex(L, index); - // Check first and second second index for being the proper types - bool success = stack::check(L, absolute_index, handler) - && stack::check(L, absolute_index + 1, handler); - tracking.use(2); - return success; - } - }; - - template <> - struct getter { - static two_things get(lua_State* L, int index, record& tracking) { - int absolute_index = lua_absindex(L, index); - // Get the first element - int a = stack::get(L, absolute_index); - // Get the second element, - // in the +1 position from the first - bool b = stack::get(L, absolute_index + 1); - // we use 2 slots, each of the previous takes 1 - tracking.use(2); - return two_things{ a, b }; - } - }; - - template <> - struct pusher { - static int push(lua_State* L, const two_things& things) { - int amount = stack::push(L, things.a); - // amount will be 1: int pushes 1 item - amount += stack::push(L, things.b); - // amount 2 now, since bool pushes a single item - // Return 2 things - return amount; - } - }; - - } -} - -int main() { - std::cout << "=== customization ===" << std::endl; - std::cout << std::boolalpha; - - sol::state lua; - lua.open_libraries(sol::lib::base); - - // Create a pass-through style of function - lua.script("function f ( a, b ) print(a, b) return a, b end"); - - // get the function out of Lua - sol::function f = lua["f"]; - - two_things things = f(two_things{ 24, false }); - c_assert(things.a == 24); - c_assert(things.b == false); - // things.a == 24 - // things.b == true - - std::cout << "things.a: " << things.a << std::endl; - std::cout << "things.b: " << things.b << std::endl; - std::cout << std::endl; - - return 0; -} diff --git a/examples/customization_convert_on_get.cpp b/examples/customization_convert_on_get.cpp deleted file mode 100644 index 3efe7b0e..00000000 --- a/examples/customization_convert_on_get.cpp +++ /dev/null @@ -1,68 +0,0 @@ -#define SOL_CHECK_ARGUMENTS 1 -#include - -#include -#include -#include "assert.hpp" - -struct number_shim { - double num = 0; -}; - -namespace sol { - - template <> - struct lua_type_of : std::integral_constant {}; - - namespace stack { - template <> - struct checker { - template - static bool check(lua_State* L, int index, Handler&& handler, record& tracking) { - // check_usertype is a backdoor for directly checking sol2 usertypes - if (!check_usertype(L, index) && !stack::check(L, index)) { - handler(L, index, type_of(L, index), type::userdata, "expected a number_shim or a number"); - return false; - } - tracking.use(1); - return true; - } - }; - - template <> - struct getter { - static number_shim get(lua_State* L, int index, record& tracking) { - if (check_usertype(L, index)) { - number_shim& ns = get_usertype(L, index, tracking); - return ns; - } - number_shim ns{}; - ns.num = stack::get(L, index, tracking); - return ns; - } - }; - - } // namespace stack -} // namespace sol - -int main() { - sol::state lua; - - // Create a pass-through style of function - lua.safe_script("function f ( a ) return a end"); - lua.set_function("g", [](double a) { - number_shim ns; - ns.num = a; - return ns; - }); - - lua.script("vf = f(25) vg = g(35)"); - - number_shim thingsf = lua["vf"]; - number_shim thingsg = lua["vg"]; - - c_assert(thingsf.num == 25); - c_assert(thingsg.num == 35); - - return 0; -} diff --git a/examples/assert.hpp b/examples/include/assert.hpp similarity index 100% rename from examples/assert.hpp rename to examples/include/assert.hpp diff --git a/examples/interop/LuaBridge/CMakeLists.txt b/examples/interop/LuaBridge/CMakeLists.txt index d69ebb49..4e644cbe 100644 --- a/examples/interop/LuaBridge/CMakeLists.txt +++ b/examples/interop/LuaBridge/CMakeLists.txt @@ -1,7 +1,7 @@ # # # # sol2 # The MIT License (MIT) # -# Copyright (c) 2013-2018 Rapptz, ThePhD, and contributors +# Copyright (c) 2013-2019 Rapptz, ThePhD, and contributors # # 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 @@ -24,27 +24,46 @@ find_package(LuaBridgeBuild) -function (make_luabridge_interop_example target_library is_single) +function (make_luabridge_interop_example target_library example_suffix) set(example_name luabridge_interop_example) - if (is_single) - set(example_name "${example_name}.single") - endif(is_single) - add_executable(${example_name} LuaBridge.cpp) - target_link_libraries(${example_name} PUBLIC ${LUA_LIBRARIES} PRIVATE ${LUABRIDGE_LIBRARIES} ${target_library}) + set(example_name "${example_name}${example_suffix}") + + add_executable(${example_name} source/LuaBridge.cpp) + target_link_libraries(${example_name} + PRIVATE ${LUA_LIBRARIES} ${LUABRIDGE_LIBRARIES} + ${target_library} sol2::assert) + + if (MSVC) + target_compile_options(${example_name} + PRIVATE /std:c++latest /EHsc "$<$:/MDd>" + "$<$:/MD>" + "$<$:/MD>" + "$<$:/MD>") + target_compile_definitions(${example_name} + PRIVATE /W1 + UNICODE _UNICODE + _CRT_SECURE_NO_WARNINGS _CRT_SECURE_NO_DEPRECATE) + else() + target_compile_options(${example_name} + PRIVATE -std=c++1z -w + -Wno-unknown-warning -Wno-unknown-warning-option) + endif() + if (CMAKE_DL_LIBS) target_link_libraries(${example_name} PRIVATE ${CMAKE_DL_LIBS}) endif() - if (MSVC) - target_compile_options(${example_name} PRIVATE /W1) - else() - target_compile_options(${example_name} PRIVATE -w) - endif() - if (TESTS_EXAMPLES) + + if (SOL2_TESTS_INTEROP_EXAMPLES) add_test(NAME ${example_name} COMMAND ${example_name}) endif() endfunction() -make_luabridge_interop_example(sol2 FALSE) -if (SOL2_SINGLE_FOUND AND INTEROP_EXAMPLES_SINGLE) - make_luabridge_interop_example(sol2_single TRUE) +if (SOL2_INTEROP_EXAMPLES) + make_luabridge_interop_example(sol2::sol2 "") +endif() +if (SOL2_INTEROP_EXAMPLES_SINGLE) + make_luabridge_interop_example(sol2::sol2_single ".single") +endif() +if (SOL2_INTEROP_EXAMPLES_SINGLE_GENERATED) + make_luabridge_interop_example(sol2::sol2_single_generated ".single.generated") endif() diff --git a/examples/interop/LuaBridge/LuaBridge.cpp b/examples/interop/LuaBridge/source/LuaBridge.cpp similarity index 66% rename from examples/interop/LuaBridge/LuaBridge.cpp rename to examples/interop/LuaBridge/source/LuaBridge.cpp index 5e025382..72d9ccc7 100644 --- a/examples/interop/LuaBridge/LuaBridge.cpp +++ b/examples/interop/LuaBridge/source/LuaBridge.cpp @@ -1,11 +1,11 @@ #define SOL_CHECK_ARGUMENTS 1 #define SOL_ENABLE_INTEROP 1 // MUST be defined to use interop features -#include +#include #include #include -#include "../../assert.hpp" +#include // LuaBridge, // no longer maintained, by VinnieFalco: @@ -29,36 +29,27 @@ private: int v_ = 50; }; -namespace sol { -namespace stack { - template - struct userdata_checker> { - template - static bool check(lua_State* L, int relindex, type index_type, Handler&& handler, record& tracking) { - // just marking unused parameters for no compiler warnings - (void)index_type; - (void)handler; - tracking.use(1); - int index = lua_absindex(L, relindex); - T* corrected = luabridge::Userdata::get(L, index, true); - return corrected != nullptr; - } - }; - - template - struct userdata_getter> { - static std::pair get(lua_State* L, int relindex, void* unadjusted_pointer, record& tracking) { - (void)unadjusted_pointer; - int index = lua_absindex(L, relindex); - if (!userdata_checker>::check(L, index, type::userdata, no_panic, tracking)) { - return { false, nullptr }; - } - T* corrected = luabridge::Userdata::get(L, index, true); - return { true, corrected }; - } - }; +template +inline bool sol_lua_interop_check(sol::types, lua_State* L, int relindex, sol::type index_type, Handler&& handler, sol::stack::record& tracking) { + // just marking unused parameters for no compiler warnings + (void)index_type; + (void)handler; + tracking.use(1); + int index = lua_absindex(L, relindex); + T* corrected = luabridge::Userdata::get(L, index, true); + return corrected != nullptr; +} + +template +inline std::pair sol_lua_interop_get(sol::types t, lua_State* L, int relindex, void* unadjusted_pointer, sol::stack::record& tracking) { + (void)unadjusted_pointer; + int index = lua_absindex(L, relindex); + if (!sol_lua_interop_check(t, L, index, sol::type::userdata, sol::no_panic, tracking)) { + return { false, nullptr }; + } + T* corrected = luabridge::Userdata::get(L, index, true); + return { true, corrected }; } -} // namespace sol::stack void register_sol_stuff(lua_State* L) { // grab raw state and put into state_view diff --git a/examples/interop/kaguya/CMakeLists.txt b/examples/interop/kaguya/CMakeLists.txt index b1d07a2b..9e1bc8e9 100644 --- a/examples/interop/kaguya/CMakeLists.txt +++ b/examples/interop/kaguya/CMakeLists.txt @@ -1,7 +1,7 @@ # # # # sol2 # The MIT License (MIT) # -# Copyright (c) 2013-2018 Rapptz, ThePhD, and contributors +# Copyright (c) 2013-2019 Rapptz, ThePhD, and contributors # # 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 @@ -24,27 +24,45 @@ find_package(KaguyaBuild) -function (make_kaguya_interop_example target_library is_single) +function (make_kaguya_interop_example target_library example_suffix) set(example_name kaguya_interop_example) - if (is_single) - set(example_name "${example_name}.single") - endif(is_single) - add_executable(${example_name} kaguya.cpp) - target_link_libraries(${example_name} PUBLIC ${LUA_LIBRARIES} PRIVATE ${KAGUYA_LIBRARIES} ${target_library}) + set(example_name "${example_name}${example_suffix}") + + add_executable(${example_name} source/kaguya.cpp) + target_link_libraries(${example_name} + PRIVATE ${LUA_LIBRARIES} ${KAGUYA_LIBRARIES} + ${target_library} sol2::assert) + + if (MSVC) + target_compile_options(${example_name} + PRIVATE /W1 /std:c++latest /EHsc "$<$:/MDd>" + "$<$:/MD>" + "$<$:/MD>" + "$<$:/MD>") + target_compile_definitions(${example_name} + PRIVATE UNICODE _UNICODE + _CRT_SECURE_NO_WARNINGS _CRT_SECURE_NO_DEPRECATE) + else() + target_compile_options(${example_name} + PRIVATE -std=c++1z -w + -Wno-unknown-warning -Wno-unknown-warning-option) + endif() + if (CMAKE_DL_LIBS) target_link_libraries(${example_name} PRIVATE ${CMAKE_DL_LIBS}) endif() - if (MSVC) - target_compile_options(${example_name} PRIVATE /W1) - else() - target_compile_options(${example_name} PRIVATE -w) - endif() - if (TESTS_INTEROP_EXAMPLES) + + if (SOL2_TESTS_INTEROP_EXAMPLES) add_test(NAME ${example_name} COMMAND ${example_name}) endif() endfunction() -make_kaguya_interop_example(sol2 FALSE) -if (SOL2_SINGLE_FOUND AND INTEROP_EXAMPLES_SINGLE) - make_kaguya_interop_example(sol2_single TRUE) +if (SOL2_INTEROP_EXAMPLES) + make_kaguya_interop_example(sol2::sol2 "") +endif() +if (SOL2_INTEROP_EXAMPLES_SINGLE) + make_kaguya_interop_example(sol2::sol2_single ".single") +endif() +if (SOL2_INTEROP_EXAMPLES_SINGLE_GENERATED) + make_kaguya_interop_example(sol2::sol2_single_generated ".single.generated") endif() diff --git a/examples/interop/kaguya/kaguya.cpp b/examples/interop/kaguya/source/kaguya.cpp similarity index 61% rename from examples/interop/kaguya/kaguya.cpp rename to examples/interop/kaguya/source/kaguya.cpp index 627987e1..564f38ec 100644 --- a/examples/interop/kaguya/kaguya.cpp +++ b/examples/interop/kaguya/source/kaguya.cpp @@ -2,10 +2,10 @@ #define SOL_CHECK_ARGUMENTS 1 #define SOL_ENABLE_INTEROP 1 // MUST be defined to use interop features -#include +#include #include -#include "../../assert.hpp" +#include // kaguya code lifted from README.md, // written by satoren: @@ -39,46 +39,37 @@ private: int v_; }; -namespace sol { -namespace stack { - template - struct userdata_checker> { - template - static bool check(lua_State* L, int relindex, type index_type, Handler&& handler, record& tracking) { - // just marking unused parameters for no compiler warnings - (void)index_type; - (void)handler; - // using 1 element - tracking.use(1); - int index = lua_absindex(L, relindex); - // use kaguya's own detail wrapper check to see if it's correct - bool is_correct_type = kaguya::detail::object_wrapper_type_check(L, index); - return is_correct_type; - } - }; - - template - struct userdata_getter> { - static std::pair get(lua_State* L, int relindex, void* unadjusted_pointer, record& tracking) { - // you may not need to specialize this method every time: - // some libraries are compatible with sol2's layout - - // kaguya's storage of data is incompatible with sol's - // it stores the data directly in the pointer, not a pointer inside of the `void*` - // therefore, leave the raw userdata pointer as-is, - // if it's of the right type - int index = lua_absindex(L, relindex); - if (!kaguya::detail::object_wrapper_type_check(L, index)) { - return { false, nullptr }; - } - // using 1 element - tracking.use(1); - kaguya::ObjectWrapperBase* base = kaguya::object_wrapper(L, index); - return { true, static_cast(base->get()) }; - } - }; +template +inline bool sol_lua_interop_check(sol::types, lua_State* L, int relindex, sol::type index_type, Handler&& handler, sol::stack::record& tracking) { + // just marking unused parameters for no compiler warnings + (void)index_type; + (void)handler; + // using 1 element + tracking.use(1); + int index = lua_absindex(L, relindex); + // use kaguya's own detail wrapper check to see if it's correct + bool is_correct_type = kaguya::detail::object_wrapper_type_check(L, index); + return is_correct_type; +} + +template +inline std::pair sol_lua_interop_get(sol::types, lua_State* L, int relindex, void* unadjusted_pointer, sol::stack::record& tracking) { + // you may not need to specialize this method every time: + // some libraries are compatible with sol2's layout + + // kaguya's storage of data is incompatible with sol's + // it stores the data directly in the pointer, not a pointer inside of the `void*` + // therefore, leave the raw userdata pointer as-is, + // if it's of the right type + int index = lua_absindex(L, relindex); + if (!kaguya::detail::object_wrapper_type_check(L, index)) { + return { false, nullptr }; + } + // using 1 element + tracking.use(1); + kaguya::ObjectWrapperBase* base = kaguya::object_wrapper(L, index); + return { true, static_cast(base->get()) }; } -} // namespace sol::stack void register_sol_stuff(lua_State* L) { // grab raw state and put into state_view diff --git a/examples/interop/luwra/CMakeLists.txt b/examples/interop/luwra/CMakeLists.txt index dc9651d8..ef6c4501 100644 --- a/examples/interop/luwra/CMakeLists.txt +++ b/examples/interop/luwra/CMakeLists.txt @@ -1,7 +1,7 @@ # # # # sol2 # The MIT License (MIT) # -# Copyright (c) 2013-2018 Rapptz, ThePhD, and contributors +# Copyright (c) 2013-2019 Rapptz, ThePhD, and contributors # # 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 @@ -24,27 +24,45 @@ find_package(LuwraBuild) -function (make_luwra_interop_example target_library is_single) +function (make_luwra_interop_example target_library example_suffix) set(example_name luwra_interop_example) - if (is_single) - set(example_name "${example_name}.single") - endif(is_single) - add_executable(${example_name} luwra.cpp) - target_link_libraries(${example_name} PUBLIC ${LUA_LIBRARIES} PRIVATE ${LUWRA_LIBRARIES} ${target_library}) + set(example_name "${example_name}${example_suffix}") + + add_executable(${example_name} source/luwra.cpp) + target_link_libraries(${example_name} + PRIVATE ${LUA_LIBRARIES} ${LUWRA_LIBRARIES} + ${target_library} sol2::assert) + + if (MSVC) + target_compile_options(${example_name} + PRIVATE /std:c++latest /EHsc "$<$:/MDd>" + "$<$:/MD>" + "$<$:/MD>" + "$<$:/MD>") + target_compile_definitions(${example_name} + PRIVATE /W1 UNICODE _UNICODE + _CRT_SECURE_NO_WARNINGS _CRT_SECURE_NO_DEPRECATE) + else() + target_compile_options(${example_name} + PRIVATE -std=c++1z -w + -Wno-unknown-warning -Wno-unknown-warning-option) + endif() + if (CMAKE_DL_LIBS) target_link_libraries(${example_name} PRIVATE ${CMAKE_DL_LIBS}) endif() - if (MSVC) - target_compile_options(${example_name} PRIVATE /W1) - else() - target_compile_options(${example_name} PRIVATE -w) - endif() - if (TESTS_EXAMPLES) + + if (SOL2_TESTS_INTEROP_EXAMPLES) add_test(NAME ${example_name} COMMAND ${example_name}) endif() endfunction() -make_luwra_interop_example(sol2 FALSE) -if (SOL2_SINGLE_FOUND AND INTEROP_EXAMPLES_SINGLE) - make_luwra_interop_example(sol2_single TRUE) +if (SOL2_INTEROP_EXAMPLES) + make_luwra_interop_example(sol2::sol2 "") endif() +if (SOL2_INTEROP_EXAMPLES_SINGLE) + make_luwra_interop_example(sol2::sol2_single ".single") +endif() +if (SOL2_INTEROP_EXAMPLES_SINGLE_GENERATED) + make_luwra_interop_example(sol2::sol2_single_generated ".single.generated") +endif() \ No newline at end of file diff --git a/examples/interop/luwra/luwra.cpp b/examples/interop/luwra/source/luwra.cpp similarity index 59% rename from examples/interop/luwra/luwra.cpp rename to examples/interop/luwra/source/luwra.cpp index 6d390b24..6710194c 100644 --- a/examples/interop/luwra/luwra.cpp +++ b/examples/interop/luwra/source/luwra.cpp @@ -1,11 +1,11 @@ #define SOL_CHECK_ARGUMENTS 1 #define SOL_ENABLE_INTEROP 1 // MUST be defined to use interop features -#include +#include #include #include -#include "../../assert.hpp" +#include // luwra, // another C++ wrapper library: @@ -35,42 +35,33 @@ private: int v_; }; -namespace sol { -namespace stack { - template - struct userdata_checker> { - template - static bool check(lua_State* L, int relindex, type index_type, Handler&& handler, record& tracking) { - // just marking unused parameters for no compiler warnings - (void)index_type; - (void)handler; - // using 1 element - tracking.use(1); - int index = lua_absindex(L, relindex); - if (lua_getmetatable(L, index) == 1) { - luaL_getmetatable(L, luwra::internal::UserTypeReg::name.c_str()); - bool is_correct_type = lua_rawequal(L, -2, -1) == 1; - lua_pop(L, 2); - return is_correct_type; - } - return false; - } - }; - - template - struct userdata_getter> { - static std::pair get(lua_State* L, int relindex, void* unadjusted_pointer, record& tracking) { - // you may not need to specialize this method every time: - // some libraries are compatible with sol2's layout - int index = lua_absindex(L, relindex); - if (!userdata_checker>::check(L, index, type::userdata, no_panic, tracking)) { - return { false, nullptr }; - } - return { true, static_cast(unadjusted_pointer) }; - } - }; +template +inline bool sol_lua_interop_check(sol::types, lua_State* L, int relindex, sol::type index_type, Handler&& handler, sol::stack::record& tracking) { + // just marking unused parameters for no compiler warnings + (void)index_type; + (void)handler; + // using 1 element + tracking.use(1); + int index = lua_absindex(L, relindex); + if (lua_getmetatable(L, index) == 1) { + luaL_getmetatable(L, luwra::internal::UserTypeReg::name.c_str()); + bool is_correct_type = lua_rawequal(L, -2, -1) == 1; + lua_pop(L, 2); + return is_correct_type; + } + return false; +} + +template +inline std::pair sol_lua_interop_get(sol::types t, lua_State* L, int relindex, void* unadjusted_pointer, sol::stack::record& tracking) { + // you may not need to specialize this method every time: + // some libraries are compatible with sol2's layout + int index = lua_absindex(L, relindex); + if (!sol_lua_interop_check(t, L, index, sol::type::userdata, sol::no_panic, tracking)) { + return { false, nullptr }; + } + return { true, static_cast(unadjusted_pointer) }; } -} // namespace sol::stack void register_sol_stuff(lua_State* L) { // grab raw state and put into state_view diff --git a/examples/interop/tolua/CMakeLists.txt b/examples/interop/tolua/CMakeLists.txt index 65505ab4..fc3ce957 100644 --- a/examples/interop/tolua/CMakeLists.txt +++ b/examples/interop/tolua/CMakeLists.txt @@ -1,7 +1,7 @@ # # # # sol2 # The MIT License (MIT) # -# Copyright (c) 2013-2018 Rapptz, ThePhD, and contributors +# Copyright (c) 2013-2019 Rapptz, ThePhD, and contributors # # 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 @@ -24,27 +24,45 @@ find_package(ToLuappBuild REQUIRED) -function(make_tolua_interop_example target_library is_single) +function(make_tolua_interop_example target_library example_suffix) set(example_name tolua_interop_example) - if (is_single) - set(example_name "${example_name}.single") + set(example_name "${example_name}${example_suffix}") + + add_executable(${example_name} source/tolua.cpp) + target_link_libraries(${example_name} + PRIVATE sol2::assert + ${LUA_LIBRARIES} ${TOLUAPP_LIBRARIES} ${target_library}) + + if (MSVC) + target_compile_options(${example_name} + PRIVATE /std:c++latest /EHsc "$<$:/MDd>" + "$<$:/MD>" + "$<$:/MD>" + "$<$:/MD>") + target_compile_definitions(${example_name} + PRIVATE /W1 UNICODE _UNICODE + _CRT_SECURE_NO_WARNINGS _CRT_SECURE_NO_DEPRECATE) + else() + target_compile_options(${example_name} + PRIVATE -std=c++1z -w + -Wno-unknown-warning -Wno-unknown-warning-option) endif() - add_executable(${example_name} tolua.cpp) - target_link_libraries(${example_name} PUBLIC ${LUA_LIBRARIES} PRIVATE ${TOLUAPP_LIBRARIES} ${target_library}) + if (CMAKE_DL_LIBS) target_link_libraries(${example_name} PRIVATE ${CMAKE_DL_LIBS}) endif() - if (MSVC) - target_compile_options(${example_name} PRIVATE /W1) - else() - target_compile_options(${example_name} PRIVATE -w) - endif() - if (TESTS_EXAMPLES) + + if (SOL2_TESTS_INTEROP_EXAMPLES) add_test(NAME ${example_name} COMMAND ${example_name}) endif() endfunction() -make_tolua_interop_example(sol2 FALSE) -if (SOL2_SINGLE_FOUND AND INTEROP_EXAMPLES_SINGLE) - make_tolua_interop_example(sol2_single TRUE) -endif() \ No newline at end of file +if (SOL2_INTEROP_EXAMPLES) + make_tolua_interop_example(sol2::sol2 "") +endif() +if (SOL2_INTEROP_EXAMPLES_SINGLE) + make_tolua_interop_example(sol2::sol2_single ".single") +endif() +if (SOL2_INTEROP_EXAMPLES_SINGLE_GENERATED) + make_tolua_interop_example(sol2::sol2_single_generated ".single.generated") +endif() diff --git a/examples/interop/tolua/Player.h b/examples/interop/tolua/source/Player.h similarity index 100% rename from examples/interop/tolua/Player.h rename to examples/interop/tolua/source/Player.h diff --git a/examples/interop/tolua/Player.pkg b/examples/interop/tolua/source/Player.pkg similarity index 100% rename from examples/interop/tolua/Player.pkg rename to examples/interop/tolua/source/Player.pkg diff --git a/examples/interop/tolua/tolua.cpp b/examples/interop/tolua/source/tolua.cpp similarity index 68% rename from examples/interop/tolua/tolua.cpp rename to examples/interop/tolua/source/tolua.cpp index 6e5fbd99..0c31eee1 100644 --- a/examples/interop/tolua/tolua.cpp +++ b/examples/interop/tolua/source/tolua.cpp @@ -1,6 +1,6 @@ #define SOL_CHECK_ARGUMENTS 1 #define SOL_ENABLE_INTEROP 1 // MUST be defined to use interop features -#include +#include #include "Player.h" #include @@ -9,30 +9,37 @@ #include "tolua_Player.h" #include -#include "../../assert.hpp" +#include // tolua code lifted from some blog, if the link dies // I don't know where else you're gonna find the reference, // http://usefulgamedev.weebly.com/tolua-example.html -namespace sol { -namespace stack { - template - struct userdata_checker> { - template - static bool check(lua_State* L, int relindex, type index_type, Handler&& handler, record& tracking) { - tracking.use(1); - // just marking unused parameters for no compiler warnings - (void)index_type; - (void)handler; - int index = lua_absindex(L, relindex); - std::string name = sol::detail::short_demangle(); - tolua_Error tolua_err; - return tolua_isusertype(L, index, name.c_str(), 0, &tolua_err); - } - }; + +/* NOTE: there is no sol_lua_interop_get here, + because tolua types are -- thankfully -- memory-compatible + in most cases with sol. + Please check other examples like kaguya or LuaBribe for an example + of how to also write the getter for your type*/ +template +inline bool sol_lua_interop_check(sol::types, lua_State* L, int relindex, sol::type index_type, Handler&& handler, sol::stack::record& tracking) { + tracking.use(1); + // just marking unused parameters for no compiler warnings + (void)index_type; + (void)handler; + int index = lua_absindex(L, relindex); + std::string name = sol::detail::short_demangle(); + tolua_Error tolua_err{}; + int r = tolua_isusertype(L, index, name.c_str(), 0, &tolua_err); + if (r == 0) { + // tolua seems to leave garbage on the stack + // when the check fails + // thanks, tolua + lua_pop(L, 2); + return false; + } + return true; } -} // namespace sol::stack void register_sol_stuff(lua_State* L) { // grab raw state and put into state_view diff --git a/examples/interop/tolua/tolua_Player.h b/examples/interop/tolua/source/tolua_Player.h similarity index 100% rename from examples/interop/tolua/tolua_Player.h rename to examples/interop/tolua/source/tolua_Player.h diff --git a/examples/require_dll_example/CMakeLists.txt b/examples/require_dll_example/CMakeLists.txt index 3adb861e..39544d49 100644 --- a/examples/require_dll_example/CMakeLists.txt +++ b/examples/require_dll_example/CMakeLists.txt @@ -1,7 +1,7 @@ # # # # sol2 # The MIT License (MIT) # -# Copyright (c) 2013-2018 Rapptz, ThePhD, and contributors +# Copyright (c) 2013-2019 Rapptz, ThePhD, and contributors # # 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 @@ -24,84 +24,122 @@ # # Reusable function to call for single target # # Also hides variables from directory/global scope -function(make_require_from_dll_example target_lib is_single) +function(make_require_from_dll_example target_lib example_lib_name_suffix) # define sources - set(my_object_sources my_object.cpp my_object.hpp my_object_api.hpp) - set(require_from_dll_sources require_from_dll.cpp) + set(my_object_sources source/my_object.cpp) + set(require_from_dll_sources source/require_from_dll.cpp) # define names set(example_lib_name my_object) set(example_name require_from_dll) - if (is_single) - set(example_lib_name "${example_lib_name}.single") - set(example_name "${example_name}.single") - endif() - - # is the lua library a shared or static library? - list(GET LUA_LIBRARIES 0 lua_lib_target) - get_target_property(lua_lib_type ${lua_lib_target} TYPE) - + set(example_lib_name "${example_lib_name}${example_lib_name_suffix}") + set(example_name "${example_name}${example_lib_name_suffix}") + # add library target my_object for the require_from_dll program add_library(${example_lib_name} SHARED ${my_object_sources}) set_target_properties(${example_lib_name} PROPERTIES PREFIX "") - target_include_directories(${example_lib_name} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}") - target_compile_features(${example_lib_name} PRIVATE ${CXX_FEATURES}) - target_compile_definitions(${example_lib_name} PUBLIC MY_OBJECT_DLL PRIVATE MY_OBJECT_BUILD) + + target_include_directories(${example_lib_name} + PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include") + target_compile_definitions(${example_lib_name} + PUBLIC MY_OBJECT_DLL + PRIVATE MY_OBJECT_BUILD) + target_link_libraries(${example_lib_name} + PUBLIC ${target_lib} ${LUA_LIBRARIES} sol2::assert) + target_include_directories(${example_lib_name} + PUBLIC "${LUA_INCLUDE_DIRS}") + + if (MSVC) + target_compile_options(${example_lib_name} + PRIVATE /std:c++latest /EHsc "$<$:/MDd>" + "$<$:/MD>" + "$<$:/MD>" + "$<$:/MD>") + target_compile_definitions(${example_lib_name} + PRIVATE UNICODE _UNICODE + _CRT_SECURE_NO_WARNINGS _CRT_SECURE_NO_DEPRECATE) + else() + target_compile_options(${example_lib_name} + PRIVATE -std=c++1z + -Wno-unknown-warning -Wno-unknown-warning-option + -Wall -Wextra -Wpedantic -pedantic -pedantic-errors + -Wno-noexcept-type) + + if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") + # For another day, when C++ is not so crap + # and we have time to audit the entire lib + # for all uses of `detail::swallow`... + #target_compile_options(${test_target_name} + # PRIVATE -Wcomma) + endif() + + + if (IS_X86) + target_compile_options(${example_lib_name} BEFORE PRIVATE -m32) + endif() + endif() + if(CMAKE_DL_LIBS) target_link_libraries(${example_lib_name} PUBLIC ${CMAKE_DL_LIBS}) endif() - if (CI) - target_compile_definitions(${example_lib_name} PRIVATE SOL2_CI) - endif() - if (NOT MSVC) - target_compile_options(${example_lib_name} PRIVATE -Wno-noexcept-type) - if (lua_lib_type MATCHES "STATIC") - # ensure that the whole archive is input into the linker - # this ensure static builds are included properly - target_link_libraries(${example_lib_name} PRIVATE - -Wl,-whole-archive ${LUA_LIBRARIES} -Wl,-no-whole-archive) - else() - target_link_libraries(${example_lib_name} PRIVATE ${LUA_LIBRARIES}) - endif() - else() - target_link_libraries(${example_lib_name} PRIVATE ${LUA_LIBRARIES}) - endif() - if (IS_X86) - target_compile_options(${example_lib_name} BEFORE PRIVATE -m32) - endif() - target_link_libraries(${example_lib_name} PRIVATE ${target_lib}) - target_include_directories(${example_lib_name} PUBLIC "${LUA_INCLUDE_DIRS}") - + # add executable target that represents require_from_dll program add_executable(${example_name} ${require_from_dll_sources}) - target_compile_features(${example_name} PRIVATE ${CXX_FEATURES}) + target_link_libraries(${example_name} + PRIVATE my_object ${LUA_LIBRARIES} ${target_lib}) + target_include_directories(${example_name} + PRIVATE ${LUA_INCLUDE_DIRS}) + + if (MSVC) + target_compile_options(${example_name} + PRIVATE /std:c++latest /EHsc "$<$:/MDd>" + "$<$:/MD>" + "$<$:/MD>" + "$<$:/MD>") + target_compile_definitions(${example_name} + PRIVATE UNICODE _UNICODE + _CRT_SECURE_NO_WARNINGS _CRT_SECURE_NO_DEPRECATE) + else() + target_compile_options(${example_name} + PRIVATE -std=c++1z + -Wno-unknown-warning -Wno-unknown-warning-option + -Wall -Wextra -Wpedantic -pedantic -pedantic-errors + -Wno-noexcept-type) + + if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") + # For another day, when C++ is not so crap + # and we have time to audit the entire lib + # for all uses of `detail::swallow`... + #target_compile_options(${test_target_name} + # PRIVATE -Wcomma) + endif() + endif() + if(CMAKE_DL_LIBS) target_link_libraries(${example_name} PRIVATE ${CMAKE_DL_LIBS}) endif() - if (CI) - target_compile_definitions(${example_name} PRIVATE SOL2_CI) - endif() - if (NOT MSVC) - target_compile_options(${example_name} PRIVATE -Wno-noexcept-type) - endif() - target_link_libraries(${example_name} PRIVATE my_object ${target_lib}) - # avoid multiply defined references due to linking in the same static library - # twice over, and get "multiple definition" errors during linking - if (NOT lua_lib_type MATCHES "STATIC") - target_link_libraries(${example_name} PRIVATE ${LUA_LIBRARIES}) - endif() - target_include_directories(${example_name} PRIVATE ${LUA_INCLUDE_DIRS}) - - if (TESTS_DYNAMIC_LOADING_EXAMPLES) - if ((NOT is_single) OR (is_single AND DYNAMIC_LOADING_EXAMPLES_SINGLE)) - get_target_property(example_working_dir ${example_name} RUNTIME_OUTPUT_DIRECTORY) - add_test(NAME ${example_name} COMMAND ${example_name} WORKING_DIRECTORY "${example_working_dir}") - endif() + + if (SOL2_TESTS_DYNAMIC_LOADING_EXAMPLES) + get_target_property(example_working_dir ${example_name} RUNTIME_OUTPUT_DIRECTORY) + add_test(NAME ${example_name} COMMAND ${example_name} WORKING_DIRECTORY "${example_working_dir}") endif() endfunction() -make_require_from_dll_example(sol2 FALSE) -if (SOL2_SINGLE_FOUND AND DYNAMIC_LOADING_EXAMPLES_SINGLE) - make_require_from_dll_example(sol2_single TRUE) -endif() \ No newline at end of file +list(GET LUA_LIBRARIES 0 lua_lib_target) +get_target_property(lua_lib_type ${lua_lib_target} TYPE) +if (lua_lib_type MATCHES "STATIC") + # avoid multiply defined references due to linking in the same static library + # twice over, and get "multiple definition" errors during linking + return() +endif() + +if (SOL2_DYNAMIC_LOADING_EXAMPLES) + make_require_from_dll_example(sol2::sol2 "") +endif() +if (SOL2_DYNAMIC_LOADING_EXAMPLES_SINGLE) + make_require_from_dll_example(sol2::sol2_single ".single") +endif() +if (SOL2_DYNAMIC_LOADING_EXAMPLES_SINGLE_GENERATED) + make_require_from_dll_example(sol2::sol2_single_generated ".single.generated") +endif() diff --git a/examples/require_dll_example/my_object.hpp b/examples/require_dll_example/include/my_object/my_object.hpp similarity index 94% rename from examples/require_dll_example/my_object.hpp rename to examples/require_dll_example/include/my_object/my_object.hpp index ff86a502..f8ec5e5c 100644 --- a/examples/require_dll_example/my_object.hpp +++ b/examples/require_dll_example/include/my_object/my_object.hpp @@ -1,6 +1,6 @@ #pragma once -#include "my_object_api.hpp" +#include // forward declare as a C struct // so a pointer to lua_State can be part of a signature diff --git a/examples/require_dll_example/my_object_api.hpp b/examples/require_dll_example/include/my_object/my_object_api.hpp similarity index 100% rename from examples/require_dll_example/my_object_api.hpp rename to examples/require_dll_example/include/my_object/my_object_api.hpp diff --git a/examples/require_dll_example/my_object.cpp b/examples/require_dll_example/source/my_object.cpp similarity index 88% rename from examples/require_dll_example/my_object.cpp rename to examples/require_dll_example/source/my_object.cpp index 1b421568..a733afd4 100644 --- a/examples/require_dll_example/my_object.cpp +++ b/examples/require_dll_example/source/my_object.cpp @@ -1,7 +1,7 @@ -#include "my_object.hpp" +#include #define SOL_CHECK_ARGUMENTS 1 -#include +#include namespace my_object { @@ -10,8 +10,7 @@ namespace my_object { sol::table module = lua.create_table(); module.new_usertype("test", sol::constructors(), - "value", &test::value - ); + "value", &test::value); return module; } diff --git a/examples/require_dll_example/require_from_dll.cpp b/examples/require_dll_example/source/require_from_dll.cpp similarity index 89% rename from examples/require_dll_example/require_from_dll.cpp rename to examples/require_dll_example/source/require_from_dll.cpp index 76ee167a..33f71a8f 100644 --- a/examples/require_dll_example/require_from_dll.cpp +++ b/examples/require_dll_example/source/require_from_dll.cpp @@ -1,8 +1,8 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include -#include "my_object.hpp" -#include "../assert.hpp" +#include +#include #include diff --git a/examples/any_return.cpp b/examples/source/any_return.cpp similarity index 97% rename from examples/any_return.cpp rename to examples/source/any_return.cpp index 10a44abf..04e98f5c 100644 --- a/examples/any_return.cpp +++ b/examples/source/any_return.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include #include "assert.hpp" @@ -44,4 +44,6 @@ int main() { std::cout << "result2: " << result2 << std::endl; std::cout << "result3: " << result3 << std::endl; std::cout << std::endl; + + return 0; } \ No newline at end of file diff --git a/examples/args_from_container.cpp b/examples/source/args_from_container.cpp similarity index 90% rename from examples/args_from_container.cpp rename to examples/source/args_from_container.cpp index 5e9eec4e..43db2b9f 100644 --- a/examples/args_from_container.cpp +++ b/examples/source/args_from_container.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include #include diff --git a/examples/as_returns.cpp b/examples/source/as_returns.cpp similarity index 90% rename from examples/as_returns.cpp rename to examples/source/as_returns.cpp index bf56d59b..e24bbc9f 100644 --- a/examples/as_returns.cpp +++ b/examples/source/as_returns.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include "assert.hpp" diff --git a/examples/basic.cpp b/examples/source/basic.cpp similarity index 95% rename from examples/basic.cpp rename to examples/source/basic.cpp index d2a919be..577cfbc4 100644 --- a/examples/basic.cpp +++ b/examples/source/basic.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include #include "assert.hpp" diff --git a/examples/c_array.cpp b/examples/source/c_array.cpp similarity index 92% rename from examples/c_array.cpp rename to examples/source/c_array.cpp index b4ce328b..62c9af71 100644 --- a/examples/c_array.cpp +++ b/examples/source/c_array.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include "assert.hpp" diff --git a/examples/c_call.cpp b/examples/source/c_call.cpp similarity index 93% rename from examples/c_call.cpp rename to examples/source/c_call.cpp index ec9799a9..87f17bb5 100644 --- a/examples/c_call.cpp +++ b/examples/source/c_call.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include "assert.hpp" diff --git a/examples/calling_lua_functions.cpp b/examples/source/calling_lua_functions.cpp similarity index 98% rename from examples/calling_lua_functions.cpp rename to examples/source/calling_lua_functions.cpp index d24842a2..c848fb3a 100644 --- a/examples/calling_lua_functions.cpp +++ b/examples/source/calling_lua_functions.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include diff --git a/examples/config.cpp b/examples/source/config.cpp similarity index 93% rename from examples/config.cpp rename to examples/source/config.cpp index f4098a12..0c254a1f 100644 --- a/examples/config.cpp +++ b/examples/source/config.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include "assert.hpp" #include diff --git a/examples/config.lua b/examples/source/config.lua similarity index 100% rename from examples/config.lua rename to examples/source/config.lua diff --git a/examples/container_usertype_as_container.cpp b/examples/source/container_usertype_as_container.cpp similarity index 98% rename from examples/container_usertype_as_container.cpp rename to examples/source/container_usertype_as_container.cpp index 7156f2de..a9aa7dc5 100644 --- a/examples/container_usertype_as_container.cpp +++ b/examples/source/container_usertype_as_container.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include #include diff --git a/examples/container_with_pairs.cpp b/examples/source/container_with_pairs.cpp similarity index 93% rename from examples/container_with_pairs.cpp rename to examples/source/container_with_pairs.cpp index f6455076..0becee33 100644 --- a/examples/container_with_pairs.cpp +++ b/examples/source/container_with_pairs.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include "assert.hpp" diff --git a/examples/containers.cpp b/examples/source/containers.cpp similarity index 97% rename from examples/containers.cpp rename to examples/source/containers.cpp index 8153353f..800631d3 100644 --- a/examples/containers.cpp +++ b/examples/source/containers.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include #include diff --git a/examples/containers_as_table.cpp b/examples/source/containers_as_table.cpp similarity index 93% rename from examples/containers_as_table.cpp rename to examples/source/containers_as_table.cpp index d1e86dcd..c8d8fb5d 100644 --- a/examples/containers_as_table.cpp +++ b/examples/source/containers_as_table.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include #include @@ -14,7 +14,7 @@ // where someone wants to handle a nested table void demo(sol::nested>> src) { std::cout << "demo, sol::nested<...>" << std::endl; - const auto& listmap = src.source; + const auto& listmap = src.value(); c_assert(listmap.size() == 2); for (const auto& kvp : listmap) { const std::vector& strings = kvp.second; @@ -38,11 +38,11 @@ void demo(sol::nested>> src) { void demo_explicit (sol::as_table_t>>> src) { std::cout << "demo, explicit sol::as_table_t<...>" << std::endl; // Have to access the "source" member variable for as_table_t - const auto& listmap = src.source; + const auto& listmap = src.value(); c_assert(listmap.size() == 2); for (const auto& kvp : listmap) { // Have to access the internal "source" for the inner as_table_t, as well - const std::vector& strings = kvp.second.source; + const std::vector& strings = kvp.second.value(); c_assert(strings.size() == 3); std::cout << "\t" << kvp.first << " = "; for (const auto& s : strings) { diff --git a/examples/coroutine.cpp b/examples/source/coroutine.cpp similarity index 98% rename from examples/coroutine.cpp rename to examples/source/coroutine.cpp index e93c5d8c..7c87ed46 100644 --- a/examples/coroutine.cpp +++ b/examples/source/coroutine.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include #include diff --git a/examples/coroutine_state.cpp b/examples/source/coroutine_state.cpp similarity index 97% rename from examples/coroutine_state.cpp rename to examples/source/coroutine_state.cpp index 8ac2c92e..eb6f69f9 100644 --- a/examples/coroutine_state.cpp +++ b/examples/source/coroutine_state.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include "assert.hpp" #include diff --git a/examples/source/custom_reader.cpp b/examples/source/custom_reader.cpp new file mode 100644 index 00000000..252d3f7b --- /dev/null +++ b/examples/source/custom_reader.cpp @@ -0,0 +1,95 @@ +#define SOL_CHECK_ARGUMENTS 1 +#include + +#include "assert.hpp" + +#include +#include +#include + +struct custom_reader { + FILE* f; + // We use 2 here to demonstrate + // multiple calls to the function to + // parse whole file. + // Please don't use 2. + // PLEASE DO NOT USE A BUFFER + // OF SIZE 2! + char buffer[2]; + std::size_t current_size; + std::size_t read_count; + + custom_reader(FILE* f_) : f(f_), buffer(), current_size(0), read_count(0) { + + } + + bool read() { + std::cout << "custom read: read #" << ++read_count << std::endl; + current_size = fread( buffer, 1, 2, f ); + return current_size > 0 && ferror(f) == 0; + } + + ~custom_reader( ) { + std::fclose( f ); + } +}; + +// function must match signature found in type lua_Reader: +// const char* ( lua_State*, void*, size_t* ) +const char* custom_reader_function(lua_State*, void* pointer_to_my_object, size_t* data_size) { + custom_reader& cr = *(static_cast(pointer_to_my_object)); + if ( cr.read( ) ) { + *data_size = cr.current_size; + return cr.buffer; + } + else { + *data_size = 0; + return nullptr; + } +} + +int main( ) { + std::cout << "=== custom reader ===" << std::endl; + + // make a file to use for the custom reader + { + std::ofstream bjork("bjork.lua", std::ios::binary); + bjork << "print('hello!')\n"; + } + struct on_scope_exit { + ~on_scope_exit() { + // remove file when done + std::remove("bjork.lua"); + } + } remove_on_exit; + + + sol::state lua; + lua.open_libraries( sol::lib::base ); + + FILE* bjork_fp; +#ifdef _MSC_VER + if ( fopen_s( &bjork_fp, "bjork.lua", "r" ) != 0 ) { + std::cerr << "failed to open bjork.lua -- exiting" << std::endl; + return -1; + } +#else + bjork_fp = fopen( "bjork.lua", "r" ); +#endif + if (bjork_fp == nullptr) { + std::cerr << "failed to open bjork.lua -- exiting" << std::endl; + return -1; + } + custom_reader reader(bjork_fp); + + // load the code using our custom reader, then run it + auto result = lua.safe_script( custom_reader_function, &reader, sol::script_pass_on_error ); + // make sure we ran loaded and ran the code successfully + c_assert( result.valid( ) ); + + // note there are lua.load( ... ) variants that take a + // custom reader than JUST run the code, too! + + std::cout << std::endl; + return 0; +} diff --git a/examples/source/customization_convert_on_get.cpp b/examples/source/customization_convert_on_get.cpp new file mode 100644 index 00000000..77df4fc7 --- /dev/null +++ b/examples/source/customization_convert_on_get.cpp @@ -0,0 +1,54 @@ +#define SOL_CHECK_ARGUMENTS 1 +#include + +#include +#include +#include "assert.hpp" + +struct number_shim { + double num = 0; +}; + +template +bool sol_lua_check(sol::types, lua_State* L, int index, Handler&& handler, sol::stack::record& tracking) { + // check_usertype is a backdoor for directly checking sol2 usertypes + if (!sol::stack::check_usertype(L, index) + && !sol::stack::check(L, index)) { + handler(L, index, sol::type_of(L, index), sol::type::userdata, "expected a number_shim or a number"); + return false; + } + tracking.use(1); + return true; +} + +number_shim sol_lua_get(sol::types, lua_State* L, int index, sol::stack::record& tracking) { + if (sol::stack::check_usertype(L, index)) { + number_shim& ns = sol::stack::get_usertype(L, index, tracking); + return ns; + } + number_shim ns{}; + ns.num = sol::stack::get(L, index, tracking); + return ns; +} + +int main() { + sol::state lua; + + // Create a pass-through style of function + lua.safe_script("function f ( a ) return a end"); + lua.set_function("g", [](double a) { + number_shim ns; + ns.num = a; + return ns; + }); + + lua.script("vf = f(25) vg = g(35)"); + + number_shim thingsf = lua["vf"]; + number_shim thingsg = lua["vg"]; + + c_assert(thingsf.num == 25); + c_assert(thingsg.num == 35); + + return 0; +} diff --git a/examples/source/customization_global_transparent_argument.cpp b/examples/source/customization_global_transparent_argument.cpp new file mode 100644 index 00000000..2ab95073 --- /dev/null +++ b/examples/source/customization_global_transparent_argument.cpp @@ -0,0 +1,63 @@ +#define SOL_CHECK_ARGUMENTS 1 +#include + +// Something that can't be collided with +static const auto& script_key = "GlobalResource.MySpecialIdentifier123"; + +struct GlobalResource { + int value = 2; +}; + +template +bool sol_lua_check(sol::types, lua_State* L, int /*index*/, Handler&& handler, sol::stack::record& tracking) { + // not actually taking anything off the stack + tracking.use(0); + // get the field from global storage + sol::stack::get_field(L, script_key); + // verify type + sol::type t = static_cast(lua_type(L, -1)); + lua_pop(L, 1); + if (t != sol::type::lightuserdata) { + handler(L, 0, sol::type::lightuserdata, t, "global resource is not present as a light userdata"); + return false; + } + return true; +} + +GlobalResource* sol_lua_get(sol::types, lua_State* L, int /*index*/, sol::stack::record& tracking) { + // retrieve the (light) userdata for this type + + // not actually pulling anything off the stack + tracking.use(0); + sol::stack::get_field(L, script_key); + GlobalResource* ls = static_cast(lua_touserdata(L, -1)); + + // clean up stack value returned by `get_field` + lua_pop(L, 1); + return ls; +} + +int sol_lua_push(sol::types, lua_State* L, GlobalResource* ls) { + // push light userdata + return sol::stack::push(L, static_cast(ls)); +} + +int main() { + + sol::state lua; + lua.open_libraries(sol::lib::base); + + GlobalResource instance; + + // get GlobalResource + lua.set_function("f", [](GlobalResource* l, int value) { + return l->value + value; + }); + lua.set(script_key, &instance); + + // note only 1 argument, + // despite f having 2 arguments to recieve + lua.script("assert(f(1) == 3)"); + + return 0; +} diff --git a/examples/source/customization_multiple.cpp b/examples/source/customization_multiple.cpp new file mode 100644 index 00000000..796349e9 --- /dev/null +++ b/examples/source/customization_multiple.cpp @@ -0,0 +1,70 @@ +#define SOL_CHECK_ARGUMENTS 1 +#include + +#include +#include "assert.hpp" + +struct two_things { + int a; + bool b; +}; + +template +bool sol_lua_check(sol::types, lua_State* L, int index, Handler&& handler, sol::stack::record& tracking) { + // indices can be negative to count backwards from the top of the stack, + // rather than the bottom up + // to deal with this, we adjust the index to + // its absolute position using the lua_absindex function + int absolute_index = lua_absindex(L, index); + // Check first and second second index for being the proper types + bool success = sol::stack::check(L, absolute_index, handler) && sol::stack::check(L, absolute_index + 1, handler); + tracking.use(2); + return success; +} + +two_things sol_lua_get(sol::types, lua_State* L, int index, sol::stack::record& tracking) { + int absolute_index = lua_absindex(L, index); + // Get the first element + int a = sol::stack::get(L, absolute_index); + // Get the second element, + // in the +1 position from the first + bool b = sol::stack::get(L, absolute_index + 1); + // we use 2 slots, each of the previous takes 1 + tracking.use(2); + return two_things{ a, b }; +} + +int sol_lua_push(sol::types, lua_State* L, const two_things& things) { + int amount = sol::stack::push(L, things.a); + // amount will be 1: int pushes 1 item + amount += sol::stack::push(L, things.b); + // amount 2 now, since bool pushes a single item + // Return 2 things + return amount; +} + +int main() { + std::cout << "=== customization ===" << std::endl; + std::cout << std::boolalpha; + + sol::state lua; + lua.open_libraries(sol::lib::base); + + // Create a pass-through style of function + lua.script("function f ( a, b ) print(a, b) return a, b end"); + + // get the function out of Lua + sol::function f = lua["f"]; + + two_things things = f(two_things{ 24, false }); + c_assert(things.a == 24); + c_assert(things.b == false); + // things.a == 24 + // things.b == true + + std::cout << "things.a: " << things.a << std::endl; + std::cout << "things.b: " << things.b << std::endl; + std::cout << std::endl; + + return 0; +} diff --git a/examples/docs/as_function.cpp b/examples/source/docs/as_function.cpp similarity index 85% rename from examples/docs/as_function.cpp rename to examples/source/docs/as_function.cpp index 8d0a1f33..ea0ebe83 100644 --- a/examples/docs/as_function.cpp +++ b/examples/source/docs/as_function.cpp @@ -1,10 +1,10 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include int main () { struct callable { int operator()( int a, bool b ) { - return a + b ? 10 : 20; + return a + (b ? 10 : 20); } }; diff --git a/examples/docs/as_function_usertype_member_variable.cpp b/examples/source/docs/as_function_usertype_member_variable.cpp similarity index 88% rename from examples/docs/as_function_usertype_member_variable.cpp rename to examples/source/docs/as_function_usertype_member_variable.cpp index 15b8059d..456a3909 100644 --- a/examples/docs/as_function_usertype_member_variable.cpp +++ b/examples/source/docs/as_function_usertype_member_variable.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include int main () { class B { diff --git a/examples/docs/as_table_ipairs.cpp b/examples/source/docs/as_table_ipairs.cpp similarity index 88% rename from examples/docs/as_table_ipairs.cpp rename to examples/source/docs/as_table_ipairs.cpp index df33a8c6..8a393f8d 100644 --- a/examples/docs/as_table_ipairs.cpp +++ b/examples/source/docs/as_table_ipairs.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include diff --git a/examples/docs/inheritance.cpp b/examples/source/docs/inheritance.cpp similarity index 76% rename from examples/docs/inheritance.cpp rename to examples/source/docs/inheritance.cpp index 942f1846..e09a7ed2 100644 --- a/examples/docs/inheritance.cpp +++ b/examples/source/docs/inheritance.cpp @@ -1,9 +1,10 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include struct A { int a = 10; - virtual int call() { return 0; } + virtual int call() { return 0; } + virtual ~A(){} }; struct B : A { int b = 11; diff --git a/examples/docs/my_panic.cpp b/examples/source/docs/my_panic.cpp similarity index 97% rename from examples/docs/my_panic.cpp rename to examples/source/docs/my_panic.cpp index aa24af05..16471c7e 100644 --- a/examples/docs/my_panic.cpp +++ b/examples/source/docs/my_panic.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include inline void my_panic(sol::optional maybe_msg) { diff --git a/examples/docs/preparing_environments.cpp b/examples/source/docs/preparing_environments.cpp similarity index 93% rename from examples/docs/preparing_environments.cpp rename to examples/source/docs/preparing_environments.cpp index d220abed..1b377deb 100644 --- a/examples/docs/preparing_environments.cpp +++ b/examples/source/docs/preparing_environments.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include int main (int, char*[]) { sol::state lua; diff --git a/examples/docs/references_in_lambdas.cpp b/examples/source/docs/references_in_lambdas.cpp similarity index 91% rename from examples/docs/references_in_lambdas.cpp rename to examples/source/docs/references_in_lambdas.cpp index e5a8fffb..4bf0c6ea 100644 --- a/examples/docs/references_in_lambdas.cpp +++ b/examples/source/docs/references_in_lambdas.cpp @@ -1,7 +1,7 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include -#include "../assert.hpp" +#include int main(int, char*[]) { diff --git a/examples/docs/runtime_extension.cpp b/examples/source/docs/runtime_extension.cpp similarity index 91% rename from examples/docs/runtime_extension.cpp rename to examples/source/docs/runtime_extension.cpp index fc690238..e747f9b6 100644 --- a/examples/docs/runtime_extension.cpp +++ b/examples/source/docs/runtime_extension.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include diff --git a/examples/docs/simple_functions.cpp b/examples/source/docs/simple_functions.cpp similarity index 83% rename from examples/docs/simple_functions.cpp rename to examples/source/docs/simple_functions.cpp index 9ef02c94..17a1ed8b 100644 --- a/examples/docs/simple_functions.cpp +++ b/examples/source/docs/simple_functions.cpp @@ -1,7 +1,7 @@ #define SOL_CHECK_ARGUMENTS 1 -#include -#include "../assert.hpp" +#include +#include int main() { sol::state lua; diff --git a/examples/docs/simple_structs.cpp b/examples/source/docs/simple_structs.cpp similarity index 91% rename from examples/docs/simple_structs.cpp rename to examples/source/docs/simple_structs.cpp index 8016e78c..e98f2bb1 100644 --- a/examples/docs/simple_structs.cpp +++ b/examples/source/docs/simple_structs.cpp @@ -1,7 +1,7 @@ #define SOL_CHECK_ARGUMENTS 1 -#include -#include "../assert.hpp" +#include +#include struct vars { int boop = 0; diff --git a/examples/docs/state_script_safe.cpp b/examples/source/docs/state_script_safe.cpp similarity index 94% rename from examples/docs/state_script_safe.cpp rename to examples/source/docs/state_script_safe.cpp index 550933cc..372450ce 100644 --- a/examples/docs/state_script_safe.cpp +++ b/examples/source/docs/state_script_safe.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include diff --git a/examples/docs/state_transfer.cpp b/examples/source/docs/state_transfer.cpp similarity index 95% rename from examples/docs/state_transfer.cpp rename to examples/source/docs/state_transfer.cpp index 9fe7cc4d..332f17ca 100644 --- a/examples/docs/state_transfer.cpp +++ b/examples/source/docs/state_transfer.cpp @@ -1,8 +1,8 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include -#include "../assert.hpp" +#include #include int main (int, char*[]) { diff --git a/examples/dynamic_object.cpp b/examples/source/dynamic_object.cpp similarity index 98% rename from examples/dynamic_object.cpp rename to examples/source/dynamic_object.cpp index d5224f05..2c2cd684 100644 --- a/examples/dynamic_object.cpp +++ b/examples/source/dynamic_object.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include #include "assert.hpp" diff --git a/examples/environment_snooping.cpp b/examples/source/environment_snooping.cpp similarity index 99% rename from examples/environment_snooping.cpp rename to examples/source/environment_snooping.cpp index 1c5fb5ae..f6c272c7 100644 --- a/examples/environment_snooping.cpp +++ b/examples/source/environment_snooping.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include "assert.hpp" #include diff --git a/examples/environment_state.cpp b/examples/source/environment_state.cpp similarity index 97% rename from examples/environment_state.cpp rename to examples/source/environment_state.cpp index 0dcc8d8a..9f4bf3be 100644 --- a/examples/environment_state.cpp +++ b/examples/source/environment_state.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include diff --git a/examples/environment_state_2.cpp b/examples/source/environment_state_2.cpp similarity index 96% rename from examples/environment_state_2.cpp rename to examples/source/environment_state_2.cpp index 8ceb9a51..cbd8261d 100644 --- a/examples/environment_state_2.cpp +++ b/examples/source/environment_state_2.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include diff --git a/examples/environments.cpp b/examples/source/environments.cpp similarity index 98% rename from examples/environments.cpp rename to examples/source/environments.cpp index 254a41a8..f484a1fc 100644 --- a/examples/environments.cpp +++ b/examples/source/environments.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include diff --git a/examples/environments_on_functions.cpp b/examples/source/environments_on_functions.cpp similarity index 98% rename from examples/environments_on_functions.cpp rename to examples/source/environments_on_functions.cpp index a4f18ccf..4f1cd5b1 100644 --- a/examples/environments_on_functions.cpp +++ b/examples/source/environments_on_functions.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include "assert.hpp" #include diff --git a/examples/error_handler.cpp b/examples/source/error_handler.cpp similarity index 94% rename from examples/error_handler.cpp rename to examples/source/error_handler.cpp index 7e32815b..3fd333a6 100644 --- a/examples/error_handler.cpp +++ b/examples/source/error_handler.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include diff --git a/examples/exception_handler.cpp b/examples/source/exception_handler.cpp similarity index 98% rename from examples/exception_handler.cpp rename to examples/source/exception_handler.cpp index 31b48a05..0601fa22 100644 --- a/examples/exception_handler.cpp +++ b/examples/source/exception_handler.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include "assert.hpp" diff --git a/examples/functions.cpp b/examples/source/functions.cpp similarity index 94% rename from examples/functions.cpp rename to examples/source/functions.cpp index 4289402f..96c00369 100644 --- a/examples/functions.cpp +++ b/examples/source/functions.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include "assert.hpp" #include diff --git a/examples/functions_empty_arguments.cpp b/examples/source/functions_empty_arguments.cpp similarity index 95% rename from examples/functions_empty_arguments.cpp rename to examples/source/functions_empty_arguments.cpp index d2178e22..a2f393db 100644 --- a/examples/functions_empty_arguments.cpp +++ b/examples/source/functions_empty_arguments.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include "assert.hpp" #include diff --git a/examples/index_and_newindex_usertype.cpp b/examples/source/index_and_newindex_usertype.cpp similarity index 91% rename from examples/index_and_newindex_usertype.cpp rename to examples/source/index_and_newindex_usertype.cpp index dae54b2d..fece136b 100644 --- a/examples/index_and_newindex_usertype.cpp +++ b/examples/source/index_and_newindex_usertype.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include "assert.hpp" diff --git a/examples/indirect_function_calls.cpp b/examples/source/indirect_function_calls.cpp similarity index 98% rename from examples/indirect_function_calls.cpp rename to examples/source/indirect_function_calls.cpp index c9e5290c..8196aeab 100644 --- a/examples/indirect_function_calls.cpp +++ b/examples/source/indirect_function_calls.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include "assert.hpp" #include diff --git a/examples/metatable_customization.cpp b/examples/source/metatable_customization.cpp similarity index 92% rename from examples/metatable_customization.cpp rename to examples/source/metatable_customization.cpp index 0cbef2f0..01bd518e 100644 --- a/examples/metatable_customization.cpp +++ b/examples/source/metatable_customization.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include @@ -34,7 +34,7 @@ void register_thing_type(sol::state& lua) { if (!source.is()) { return luaL_error(L, "given an incorrect object for this call"); } - sol::optional maybe_svkey = key.as(); + sol::optional maybe_svkey = key.as>(); if (maybe_svkey) { { // functions are different from variables @@ -77,7 +77,7 @@ void register_thing_type(sol::state& lua) { return luaL_error(L, "given an incorrect object for this call"); } // write to member variables, etc. etc... - sol::optional maybe_svkey = key.as(); + sol::optional maybe_svkey = key.as>(); if (maybe_svkey) { { // variables are different than funtions diff --git a/examples/metatable_key_low_level.cpp b/examples/source/metatable_key_low_level.cpp similarity index 92% rename from examples/metatable_key_low_level.cpp rename to examples/source/metatable_key_low_level.cpp index 9c3790d4..341e016e 100644 --- a/examples/metatable_key_low_level.cpp +++ b/examples/source/metatable_key_low_level.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include "assert.hpp" diff --git a/examples/multi_results.cpp b/examples/source/multi_results.cpp similarity index 98% rename from examples/multi_results.cpp rename to examples/source/multi_results.cpp index 217af3e7..2aec0819 100644 --- a/examples/multi_results.cpp +++ b/examples/source/multi_results.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include #include "assert.hpp" diff --git a/examples/optional_with_iteration.cpp b/examples/source/optional_with_iteration.cpp similarity index 99% rename from examples/optional_with_iteration.cpp rename to examples/source/optional_with_iteration.cpp index 17b4f4da..1bbcb792 100644 --- a/examples/optional_with_iteration.cpp +++ b/examples/source/optional_with_iteration.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include #include diff --git a/examples/overloading.cpp b/examples/source/overloading.cpp similarity index 97% rename from examples/overloading.cpp rename to examples/source/overloading.cpp index 32afddeb..4c18a049 100644 --- a/examples/overloading.cpp +++ b/examples/source/overloading.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include "assert.hpp" #include diff --git a/examples/overloading_with_fallback.cpp b/examples/source/overloading_with_fallback.cpp similarity index 97% rename from examples/overloading_with_fallback.cpp rename to examples/source/overloading_with_fallback.cpp index 44b1ddef..e4f0092d 100644 --- a/examples/overloading_with_fallback.cpp +++ b/examples/source/overloading_with_fallback.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include diff --git a/examples/overloading_with_members.cpp b/examples/source/overloading_with_members.cpp similarity index 93% rename from examples/overloading_with_members.cpp rename to examples/source/overloading_with_members.cpp index 94e338eb..9fb3c2c1 100644 --- a/examples/overloading_with_members.cpp +++ b/examples/source/overloading_with_members.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include "assert.hpp" diff --git a/examples/overridable_function_members.cpp b/examples/source/overridable_function_members.cpp similarity index 92% rename from examples/overridable_function_members.cpp rename to examples/source/overridable_function_members.cpp index 84cbdd31..011b5530 100644 --- a/examples/overridable_function_members.cpp +++ b/examples/source/overridable_function_members.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include diff --git a/examples/pairs.cpp b/examples/source/pairs.cpp similarity index 99% rename from examples/pairs.cpp rename to examples/source/pairs.cpp index b4240bde..c406cedd 100644 --- a/examples/pairs.cpp +++ b/examples/source/pairs.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include #include diff --git a/examples/pairs_with_raw_functions.cpp b/examples/source/pairs_with_raw_functions.cpp similarity index 98% rename from examples/pairs_with_raw_functions.cpp rename to examples/source/pairs_with_raw_functions.cpp index 1a376777..56146b91 100644 --- a/examples/pairs_with_raw_functions.cpp +++ b/examples/source/pairs_with_raw_functions.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include #include diff --git a/examples/player_script.lua b/examples/source/player_script.lua similarity index 100% rename from examples/player_script.lua rename to examples/source/player_script.lua diff --git a/examples/property.cpp b/examples/source/property.cpp similarity index 92% rename from examples/property.cpp rename to examples/source/property.cpp index b4f721f2..1c795dc1 100644 --- a/examples/property.cpp +++ b/examples/source/property.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include "assert.hpp" diff --git a/examples/protect.cpp b/examples/source/protect.cpp similarity index 89% rename from examples/protect.cpp rename to examples/source/protect.cpp index 95de7232..6a3eeb7f 100644 --- a/examples/protect.cpp +++ b/examples/source/protect.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include "assert.hpp" diff --git a/examples/protected_functions.cpp b/examples/source/protected_functions.cpp similarity index 97% rename from examples/protected_functions.cpp rename to examples/source/protected_functions.cpp index ce8ba289..420ee9fb 100644 --- a/examples/protected_functions.cpp +++ b/examples/source/protected_functions.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include diff --git a/examples/read_only.cpp b/examples/source/read_only.cpp similarity index 94% rename from examples/read_only.cpp rename to examples/source/read_only.cpp index 1e01ff87..ecbe11b3 100644 --- a/examples/read_only.cpp +++ b/examples/source/read_only.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include diff --git a/examples/require.cpp b/examples/source/require.cpp similarity index 98% rename from examples/require.cpp rename to examples/source/require.cpp index 4398c396..640212ab 100644 --- a/examples/require.cpp +++ b/examples/source/require.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include "assert.hpp" #include diff --git a/examples/source/require_override_behavior.cpp b/examples/source/require_override_behavior.cpp new file mode 100644 index 00000000..9a839043 --- /dev/null +++ b/examples/source/require_override_behavior.cpp @@ -0,0 +1,76 @@ +// Thanks to OrfeasZ for their answer to +// an issue for this example! +#define SOL_CHECK_ARGUMENTS 1 +#include + +#include "assert.hpp" + +#include +#include + +// Use raw function of form "int(lua_State*)" +// -- this is called a "raw C function", +// and matches the type for lua_CFunction +int LoadFileRequire(lua_State* L) { + // use sol2 stack API to pull + // "first argument" + std::string path = sol::stack::get(L, 1); + + if (path == "a") { + std::string script = R"( + print("Hello from module land!") + test = 123 + return "bananas" + )"; + // load "module", but don't run it + luaL_loadbuffer(L, script.data(), script.size(), path.c_str()); + // returning 1 object left on Lua stack: + // a function that, when called, executes the script + // (this is what lua_loadX/luaL_loadX functions return + return 1; + } + + sol::stack::push(L, "This is not the module you're looking for!"); + return 1; +} + +int main() { + std::cout << "=== require override behavior ===" << std::endl; + + sol::state lua; + // need base for print, + // need package for package/searchers/require + lua.open_libraries(sol::lib::base, sol::lib::package); + + lua.clear_package_loaders( ); + lua.add_package_loader(LoadFileRequire); + + // this will call our function for + // the searcher and it will succeed + auto a_result = lua.safe_script(R"( + local a = require("a") + print(a) + print(test) + )", sol::script_pass_on_error); + c_assert(a_result.valid()); + try { + // this will always fail + auto b_result = lua.safe_script(R"( + local b = require("b") + print(b) + )", sol::script_throw_on_error); + // this will not be executed because of the throw, + // but it better be true regardless! + c_assert(!b_result.valid()); + } + catch (const std::exception& ex) { + // Whenever sol2 throws an exception from panic, + // catch + std::cout << "Something went wrong, as expected:\n" << ex.what() << std::endl; + // and CRASH / exit the application + return 0; + } + + // If we get here something went wrong...! + return -1; +} diff --git a/examples/runtime_additions.cpp b/examples/source/runtime_additions.cpp similarity index 96% rename from examples/runtime_additions.cpp rename to examples/source/runtime_additions.cpp index bad891ca..eccc1323 100644 --- a/examples/runtime_additions.cpp +++ b/examples/source/runtime_additions.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include "assert.hpp" #include diff --git a/examples/script_error_handling.cpp b/examples/source/script_error_handling.cpp similarity index 99% rename from examples/script_error_handling.cpp rename to examples/source/script_error_handling.cpp index bbf5aaa8..b7cb2cad 100644 --- a/examples/script_error_handling.cpp +++ b/examples/source/script_error_handling.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include "assert.hpp" #include diff --git a/examples/self_from_lua.cpp b/examples/source/self_from_lua.cpp similarity index 98% rename from examples/self_from_lua.cpp rename to examples/source/self_from_lua.cpp index b5e1f07e..294261a4 100644 --- a/examples/self_from_lua.cpp +++ b/examples/source/self_from_lua.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include "assert.hpp" diff --git a/examples/shared_ptr.cpp b/examples/source/shared_ptr.cpp similarity index 96% rename from examples/shared_ptr.cpp rename to examples/source/shared_ptr.cpp index 02bc05c7..7b8610cb 100644 --- a/examples/shared_ptr.cpp +++ b/examples/source/shared_ptr.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include "assert.hpp" diff --git a/examples/shared_ptr_modify_in_place.cpp b/examples/source/shared_ptr_modify_in_place.cpp similarity index 94% rename from examples/shared_ptr_modify_in_place.cpp rename to examples/source/shared_ptr_modify_in_place.cpp index d4eb82af..37673bfb 100644 --- a/examples/shared_ptr_modify_in_place.cpp +++ b/examples/source/shared_ptr_modify_in_place.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include diff --git a/examples/singleton.cpp b/examples/source/singleton.cpp similarity index 98% rename from examples/singleton.cpp rename to examples/source/singleton.cpp index fcd59cd5..eda5753d 100644 --- a/examples/singleton.cpp +++ b/examples/source/singleton.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include diff --git a/examples/stack_aligned_function.cpp b/examples/source/stack_aligned_function.cpp similarity index 97% rename from examples/stack_aligned_function.cpp rename to examples/source/stack_aligned_function.cpp index 3447e007..a0bde213 100644 --- a/examples/stack_aligned_function.cpp +++ b/examples/source/stack_aligned_function.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include "assert.hpp" diff --git a/examples/static_variables.cpp b/examples/source/static_variables.cpp similarity index 98% rename from examples/static_variables.cpp rename to examples/source/static_variables.cpp index 4284f1f0..46701cf9 100644 --- a/examples/static_variables.cpp +++ b/examples/source/static_variables.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include #include "assert.hpp" diff --git a/examples/table_proxy.cpp b/examples/source/table_proxy.cpp similarity index 92% rename from examples/table_proxy.cpp rename to examples/source/table_proxy.cpp index 61316801..9d4222f3 100644 --- a/examples/table_proxy.cpp +++ b/examples/source/table_proxy.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include "assert.hpp" diff --git a/examples/tables.cpp b/examples/source/tables.cpp similarity index 98% rename from examples/tables.cpp rename to examples/source/tables.cpp index bcfc57df..5627577a 100644 --- a/examples/tables.cpp +++ b/examples/source/tables.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include #include diff --git a/examples/this_state.cpp b/examples/source/this_state.cpp similarity index 93% rename from examples/this_state.cpp rename to examples/source/this_state.cpp index 78b6c3ad..ed3731f9 100644 --- a/examples/this_state.cpp +++ b/examples/source/this_state.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include "assert.hpp" diff --git a/examples/tie.cpp b/examples/source/tie.cpp similarity index 91% rename from examples/tie.cpp rename to examples/source/tie.cpp index aadbacb9..9069b5e2 100644 --- a/examples/tie.cpp +++ b/examples/source/tie.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include "assert.hpp" diff --git a/examples/tutorials/erase_demo.cpp b/examples/source/tutorials/erase_demo.cpp similarity index 100% rename from examples/tutorials/erase_demo.cpp rename to examples/source/tutorials/erase_demo.cpp diff --git a/examples/tutorials/lazy_demo.cpp b/examples/source/tutorials/lazy_demo.cpp similarity index 100% rename from examples/tutorials/lazy_demo.cpp rename to examples/source/tutorials/lazy_demo.cpp diff --git a/examples/tutorials/quick_n_dirty/functions_all.cpp b/examples/source/tutorials/quick_n_dirty/functions_all.cpp similarity index 97% rename from examples/tutorials/quick_n_dirty/functions_all.cpp rename to examples/source/tutorials/quick_n_dirty/functions_all.cpp index 4a6c5edd..5185c894 100644 --- a/examples/tutorials/quick_n_dirty/functions_all.cpp +++ b/examples/source/tutorials/quick_n_dirty/functions_all.cpp @@ -1,7 +1,7 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include -#include "../../assert.hpp" +#include #include void some_function() { diff --git a/examples/tutorials/quick_n_dirty/functions_easy.cpp b/examples/source/tutorials/quick_n_dirty/functions_easy.cpp similarity index 94% rename from examples/tutorials/quick_n_dirty/functions_easy.cpp rename to examples/source/tutorials/quick_n_dirty/functions_easy.cpp index 82fcfd63..366f25df 100644 --- a/examples/tutorials/quick_n_dirty/functions_easy.cpp +++ b/examples/source/tutorials/quick_n_dirty/functions_easy.cpp @@ -1,7 +1,7 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include -#include "../../assert.hpp" +#include int main(int, char*[]) { sol::state lua; diff --git a/examples/tutorials/quick_n_dirty/make_tables.cpp b/examples/source/tutorials/quick_n_dirty/make_tables.cpp similarity index 92% rename from examples/tutorials/quick_n_dirty/make_tables.cpp rename to examples/source/tutorials/quick_n_dirty/make_tables.cpp index 0e902996..14ee88cb 100644 --- a/examples/tutorials/quick_n_dirty/make_tables.cpp +++ b/examples/source/tutorials/quick_n_dirty/make_tables.cpp @@ -1,7 +1,7 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include -#include "../../assert.hpp" +#include int main(int, char* []) { sol::state lua; diff --git a/examples/tutorials/quick_n_dirty/multiple_returns_from_lua.cpp b/examples/source/tutorials/quick_n_dirty/multiple_returns_from_lua.cpp similarity index 89% rename from examples/tutorials/quick_n_dirty/multiple_returns_from_lua.cpp rename to examples/source/tutorials/quick_n_dirty/multiple_returns_from_lua.cpp index b5787f78..97648926 100644 --- a/examples/tutorials/quick_n_dirty/multiple_returns_from_lua.cpp +++ b/examples/source/tutorials/quick_n_dirty/multiple_returns_from_lua.cpp @@ -1,7 +1,7 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include -#include "../../assert.hpp" +#include int main(int, char* []) { sol::state lua; diff --git a/examples/tutorials/quick_n_dirty/multiple_returns_to_lua.cpp b/examples/source/tutorials/quick_n_dirty/multiple_returns_to_lua.cpp similarity index 95% rename from examples/tutorials/quick_n_dirty/multiple_returns_to_lua.cpp rename to examples/source/tutorials/quick_n_dirty/multiple_returns_to_lua.cpp index 3d8bee69..0861ee5f 100644 --- a/examples/tutorials/quick_n_dirty/multiple_returns_to_lua.cpp +++ b/examples/source/tutorials/quick_n_dirty/multiple_returns_to_lua.cpp @@ -1,7 +1,7 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include -#include "../../assert.hpp" +#include int main(int, char* []) { sol::state lua; diff --git a/examples/tutorials/quick_n_dirty/namespacing.cpp b/examples/source/tutorials/quick_n_dirty/namespacing.cpp similarity index 96% rename from examples/tutorials/quick_n_dirty/namespacing.cpp rename to examples/source/tutorials/quick_n_dirty/namespacing.cpp index e556e867..95229bd1 100644 --- a/examples/tutorials/quick_n_dirty/namespacing.cpp +++ b/examples/source/tutorials/quick_n_dirty/namespacing.cpp @@ -1,8 +1,8 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include -#include "../../assert.hpp" +#include int main() { std::cout << "=== namespacing ===" << std::endl; diff --git a/examples/tutorials/quick_n_dirty/opening_a_state.cpp b/examples/source/tutorials/quick_n_dirty/opening_a_state.cpp similarity index 87% rename from examples/tutorials/quick_n_dirty/opening_a_state.cpp rename to examples/source/tutorials/quick_n_dirty/opening_a_state.cpp index 7f1c20d0..071d8ab9 100644 --- a/examples/tutorials/quick_n_dirty/opening_a_state.cpp +++ b/examples/source/tutorials/quick_n_dirty/opening_a_state.cpp @@ -1,8 +1,8 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include -#include "../../assert.hpp" +#include int main(int, char*[]) { std::cout << "=== opening a state ===" << std::endl; diff --git a/examples/tutorials/quick_n_dirty/opening_state_on_raw_lua.cpp b/examples/source/tutorials/quick_n_dirty/opening_state_on_raw_lua.cpp similarity index 95% rename from examples/tutorials/quick_n_dirty/opening_state_on_raw_lua.cpp rename to examples/source/tutorials/quick_n_dirty/opening_state_on_raw_lua.cpp index bb974560..b97b5b1f 100644 --- a/examples/tutorials/quick_n_dirty/opening_state_on_raw_lua.cpp +++ b/examples/source/tutorials/quick_n_dirty/opening_state_on_raw_lua.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include diff --git a/examples/tutorials/quick_n_dirty/running_lua_code.cpp b/examples/source/tutorials/quick_n_dirty/running_lua_code.cpp similarity index 96% rename from examples/tutorials/quick_n_dirty/running_lua_code.cpp rename to examples/source/tutorials/quick_n_dirty/running_lua_code.cpp index 7e287474..068fca06 100644 --- a/examples/tutorials/quick_n_dirty/running_lua_code.cpp +++ b/examples/source/tutorials/quick_n_dirty/running_lua_code.cpp @@ -1,9 +1,9 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include #include -#include "../../assert.hpp" +#include int main(int, char*[]) { std::cout << "=== running lua code ===" << std::endl; diff --git a/examples/tutorials/quick_n_dirty/running_lua_code_low_level.cpp b/examples/source/tutorials/quick_n_dirty/running_lua_code_low_level.cpp similarity index 95% rename from examples/tutorials/quick_n_dirty/running_lua_code_low_level.cpp rename to examples/source/tutorials/quick_n_dirty/running_lua_code_low_level.cpp index 4aa1bb30..f341b29f 100644 --- a/examples/tutorials/quick_n_dirty/running_lua_code_low_level.cpp +++ b/examples/source/tutorials/quick_n_dirty/running_lua_code_low_level.cpp @@ -1,10 +1,10 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include #include #include -#include "../../assert.hpp" +#include int main(int, char*[]) { std::cout << "=== running lua code (low level) ===" << std::endl; diff --git a/examples/tutorials/quick_n_dirty/self_call.cpp b/examples/source/tutorials/quick_n_dirty/self_call.cpp similarity index 96% rename from examples/tutorials/quick_n_dirty/self_call.cpp rename to examples/source/tutorials/quick_n_dirty/self_call.cpp index 10015ba0..9fd3c349 100644 --- a/examples/tutorials/quick_n_dirty/self_call.cpp +++ b/examples/source/tutorials/quick_n_dirty/self_call.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include diff --git a/examples/tutorials/quick_n_dirty/set_and_get_variables.cpp b/examples/source/tutorials/quick_n_dirty/set_and_get_variables.cpp similarity index 97% rename from examples/tutorials/quick_n_dirty/set_and_get_variables.cpp rename to examples/source/tutorials/quick_n_dirty/set_and_get_variables.cpp index 87537e04..5ac7e3db 100644 --- a/examples/tutorials/quick_n_dirty/set_and_get_variables.cpp +++ b/examples/source/tutorials/quick_n_dirty/set_and_get_variables.cpp @@ -1,7 +1,7 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include -#include "../../assert.hpp" +#include int main(int, char*[]) { sol::state lua; diff --git a/examples/tutorials/quick_n_dirty/set_and_get_variables_exists.cpp b/examples/source/tutorials/quick_n_dirty/set_and_get_variables_exists.cpp similarity index 87% rename from examples/tutorials/quick_n_dirty/set_and_get_variables_exists.cpp rename to examples/source/tutorials/quick_n_dirty/set_and_get_variables_exists.cpp index 18fe1f5f..043c2640 100644 --- a/examples/tutorials/quick_n_dirty/set_and_get_variables_exists.cpp +++ b/examples/source/tutorials/quick_n_dirty/set_and_get_variables_exists.cpp @@ -1,7 +1,7 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include -#include "../../assert.hpp" +#include int main(int, char*[]) { sol::state lua; diff --git a/examples/tutorials/quick_n_dirty/tables_and_nesting.cpp b/examples/source/tutorials/quick_n_dirty/tables_and_nesting.cpp similarity index 95% rename from examples/tutorials/quick_n_dirty/tables_and_nesting.cpp rename to examples/source/tutorials/quick_n_dirty/tables_and_nesting.cpp index 51bd16ca..ea04f1d6 100644 --- a/examples/tutorials/quick_n_dirty/tables_and_nesting.cpp +++ b/examples/source/tutorials/quick_n_dirty/tables_and_nesting.cpp @@ -1,7 +1,7 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include -#include "../../assert.hpp" +#include int main(int, char*[]) { diff --git a/examples/tutorials/quick_n_dirty/userdata.cpp b/examples/source/tutorials/quick_n_dirty/userdata.cpp similarity index 98% rename from examples/tutorials/quick_n_dirty/userdata.cpp rename to examples/source/tutorials/quick_n_dirty/userdata.cpp index e587a5e9..9794b9cb 100644 --- a/examples/tutorials/quick_n_dirty/userdata.cpp +++ b/examples/source/tutorials/quick_n_dirty/userdata.cpp @@ -1,7 +1,7 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include -#include "../../assert.hpp" +#include #include struct Doge { diff --git a/examples/tutorials/quick_n_dirty/userdata_memory_reference.cpp b/examples/source/tutorials/quick_n_dirty/userdata_memory_reference.cpp similarity index 92% rename from examples/tutorials/quick_n_dirty/userdata_memory_reference.cpp rename to examples/source/tutorials/quick_n_dirty/userdata_memory_reference.cpp index e9833ef9..b2a33a83 100644 --- a/examples/tutorials/quick_n_dirty/userdata_memory_reference.cpp +++ b/examples/source/tutorials/quick_n_dirty/userdata_memory_reference.cpp @@ -1,7 +1,7 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include -#include "../../assert.hpp" +#include #include struct Doge { diff --git a/examples/tutorials/quick_n_dirty/usertypes.cpp b/examples/source/tutorials/quick_n_dirty/usertypes.cpp similarity index 97% rename from examples/tutorials/quick_n_dirty/usertypes.cpp rename to examples/source/tutorials/quick_n_dirty/usertypes.cpp index 90e77291..1b81ed4b 100644 --- a/examples/tutorials/quick_n_dirty/usertypes.cpp +++ b/examples/source/tutorials/quick_n_dirty/usertypes.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include diff --git a/examples/tutorials/variables_demo.cpp b/examples/source/tutorials/variables_demo.cpp similarity index 95% rename from examples/tutorials/variables_demo.cpp rename to examples/source/tutorials/variables_demo.cpp index 3327ab7c..9023fdd0 100644 --- a/examples/tutorials/variables_demo.cpp +++ b/examples/source/tutorials/variables_demo.cpp @@ -2,7 +2,7 @@ #include #include -#include "../assert.hpp" +#include #include // for std::pair int main() { diff --git a/examples/tutorials/write_variables_demo.cpp b/examples/source/tutorials/write_variables_demo.cpp similarity index 100% rename from examples/tutorials/write_variables_demo.cpp rename to examples/source/tutorials/write_variables_demo.cpp diff --git a/examples/unique_ptr.cpp b/examples/source/unique_ptr.cpp similarity index 95% rename from examples/unique_ptr.cpp rename to examples/source/unique_ptr.cpp index 78613f3c..aa87c2eb 100644 --- a/examples/unique_ptr.cpp +++ b/examples/source/unique_ptr.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include "assert.hpp" diff --git a/examples/usertype.cpp b/examples/source/usertype.cpp similarity index 81% rename from examples/usertype.cpp rename to examples/source/usertype.cpp index 32cfa9d5..56674b04 100644 --- a/examples/usertype.cpp +++ b/examples/source/usertype.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include #include "assert.hpp" @@ -55,7 +55,9 @@ int main() { // you must make sure that the name of the function // goes before the member function pointer - lua.new_usertype("foo", "print", &foo::print, "test", &foo::test); + lua.new_usertype("foo", sol::constructors(), + "print", &foo::print, + "test", &foo::test); // making the class from lua is simple // same with calling member functions @@ -72,27 +74,26 @@ int main() { { // Notice the brace: this means we're in a new scope + // first, define the different types of constructors // notice here that the return type // on the function-type doesn't exactly matter, // which allows you to use a shorter class name/void // if necessary sol::constructors ctor; - - // the only template parameter is the class type - // the first argument of construction is the name - // second is the constructor types - // then the rest are function name and member function pointer pairs - sol::usertype utype(ctor, "is_unit", &vector::is_unit); - // then you must register it - lua.set_usertype("vector", utype); - // You can throw away the usertype after you set it: you do NOT + sol::usertype utype = lua.new_usertype("vector", ctor); + + // add to it as much as you like + utype["is_unit"] = &vector::is_unit; + // You can throw away the usertype after + // you set it: you do NOT // have to keep it around - // cleanup happens automagically + // cleanup happens automagically! } - // calling it is the same as new_userdata + // calling it is the same as + // working with userdata is C++ lua.script("v = vector.new()\n" "v = vector.new(12)\n" "v = vector.new(10, 10)\n" @@ -112,4 +113,5 @@ int main() { std::cout << std::endl; + return 0; } diff --git a/examples/usertype_advanced.cpp b/examples/source/usertype_advanced.cpp similarity index 97% rename from examples/usertype_advanced.cpp rename to examples/source/usertype_advanced.cpp index 377b549a..314b63c7 100644 --- a/examples/usertype_advanced.cpp +++ b/examples/source/usertype_advanced.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include @@ -101,7 +101,7 @@ assert(not p2shoots) -- set variable property setter p1.hp = 545 --- get variable through property getter +-- get variable through property unqualified_getter print(p1.hp) assert(p1.hp == 545) diff --git a/examples/usertype_automatic_operators.cpp b/examples/source/usertype_automatic_operators.cpp similarity index 99% rename from examples/usertype_automatic_operators.cpp rename to examples/source/usertype_automatic_operators.cpp index 1867819b..ae330a86 100644 --- a/examples/usertype_automatic_operators.cpp +++ b/examples/source/usertype_automatic_operators.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include #include diff --git a/examples/usertype_basics.cpp b/examples/source/usertype_basics.cpp similarity index 76% rename from examples/usertype_basics.cpp rename to examples/source/usertype_basics.cpp index 54d25f8c..901ec793 100644 --- a/examples/usertype_basics.cpp +++ b/examples/source/usertype_basics.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include @@ -49,18 +49,12 @@ int main () { ); } else { - // an alternative way: - // use the constructor directly - sol::usertype shiptype( - "shoot", &ship::shoot, - "hurt", &ship::hurt, - "life", &ship::life, - "bullet_count", &ship::bullets - ); - // set usertype explicitly, with the given name - lua.set_usertype( "ship", shiptype ); - // the shiptype variable is now a useless skeleton type, just let it destruct naturally and don't use it again. + sol::usertype usertype_table = lua.new_usertype( "ship"); + usertype_table["shoot"] = &ship::shoot; + usertype_table["hurt"] = &ship::hurt; + usertype_table["life"] = &ship::life; + usertype_table["bullet_count"] = &ship::bullets; } const auto& code = R"( diff --git a/examples/usertype_bitfields.cpp b/examples/source/usertype_bitfields.cpp similarity index 99% rename from examples/usertype_bitfields.cpp rename to examples/source/usertype_bitfields.cpp index 59171c08..8eb3c969 100644 --- a/examples/usertype_bitfields.cpp +++ b/examples/source/usertype_bitfields.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include #include diff --git a/examples/usertype_call_from_c++.cpp b/examples/source/usertype_call_from_c++.cpp similarity index 97% rename from examples/usertype_call_from_c++.cpp rename to examples/source/usertype_call_from_c++.cpp index befe9b99..5639bd29 100644 --- a/examples/usertype_call_from_c++.cpp +++ b/examples/source/usertype_call_from_c++.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include diff --git a/examples/usertype_dynamic_getter_setter.cpp b/examples/source/usertype_dynamic_get_set.cpp similarity index 88% rename from examples/usertype_dynamic_getter_setter.cpp rename to examples/source/usertype_dynamic_get_set.cpp index 1ee41f69..c0d6d1ad 100644 --- a/examples/usertype_dynamic_getter_setter.cpp +++ b/examples/source/usertype_dynamic_get_set.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include #include "assert.hpp" @@ -21,7 +21,7 @@ struct vec { vec(double x) : vec(x, x) {} vec(double x, double y) : x(x), y(y) {} - sol::object getter(sol::stack_object key, sol::this_state L) { + sol::object get(sol::stack_object key, sol::this_state L) { // we use stack_object for the arguments because we know // the values from Lua will remain on Lua's stack, // so long we we don't mess with it @@ -61,7 +61,7 @@ struct vec { return sol::object(L, sol::in_place, sol::lua_nil); } - void setter(sol::stack_object key, sol::stack_object value, sol::this_state) { + void set(sol::stack_object key, sol::stack_object value, sol::this_state) { // we use stack_object for the arguments because we know // the values from Lua will remain on Lua's stack, // so long we we don't mess with it @@ -97,7 +97,7 @@ struct vec { }; int main() { - std::cout << "=== usertype dynamic getter/setter ===" << std::endl; + std::cout << "=== usertype dynamic get/set ===" << std::endl; sol::state lua; lua.open_libraries(); @@ -111,18 +111,18 @@ int main() { // we can use a member function to assume the "object" is of the `vec` // type, and then just have a function that takes // the key (get) or the key + the value (set) - sol::meta_function::index, &vec::getter, - sol::meta_function::new_index, &vec::setter + sol::meta_function::index, &vec::get, + sol::meta_function::new_index, &vec::set ); lua.script(R"( v1 = vec.new(1, 0) v2 = vec.new(0, 1) - -- observe usage of getter/setter + -- observe usage of get/set print("v1:", v1.x, v1.y) print("v2:", v2.x, v2.y) - -- gets 0, 1 by doing lookup into getter function + -- gets 0, 1 by doing lookup into get function print("changing v2...") v2.x = 3 v2[1] = 5 @@ -132,7 +132,7 @@ int main() { print("v2:", v2.x, v2.y) -- both obj.name and obj["name"] -- are equivalent lookup methods - -- and both will trigger the getter + -- and both will trigger the get -- if it can't find 'name' on the object assert(v1["x"] == v1.x) assert(v1[0] == v1.x) @@ -145,7 +145,7 @@ int main() { // Can also be manipulated from C++, - // and will call getter/setter methods: + // and will call get/set methods: sol::userdata v1 = lua["v1"]; double v1x = v1["x"]; double v1y = v1["y"]; diff --git a/examples/usertype_initializers.cpp b/examples/source/usertype_initializers.cpp similarity index 98% rename from examples/usertype_initializers.cpp rename to examples/source/usertype_initializers.cpp index 73157c4c..821e361c 100644 --- a/examples/usertype_initializers.cpp +++ b/examples/source/usertype_initializers.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include #include diff --git a/examples/usertype_special_functions.cpp b/examples/source/usertype_special_functions.cpp similarity index 98% rename from examples/usertype_special_functions.cpp rename to examples/source/usertype_special_functions.cpp index d957016d..9d9b0b54 100644 --- a/examples/usertype_special_functions.cpp +++ b/examples/source/usertype_special_functions.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include "assert.hpp" #include diff --git a/examples/usertype_var.cpp b/examples/source/usertype_var.cpp similarity index 97% rename from examples/usertype_var.cpp rename to examples/source/usertype_var.cpp index 981b358d..1cebad3f 100644 --- a/examples/usertype_var.cpp +++ b/examples/source/usertype_var.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include "assert.hpp" #include diff --git a/examples/variables.cpp b/examples/source/variables.cpp similarity index 93% rename from examples/variables.cpp rename to examples/source/variables.cpp index a8fea78f..ef173c75 100644 --- a/examples/variables.cpp +++ b/examples/source/variables.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include diff --git a/examples/variadic_args.cpp b/examples/source/variadic_args.cpp similarity index 98% rename from examples/variadic_args.cpp rename to examples/source/variadic_args.cpp index 15e19cc4..65ac16d5 100644 --- a/examples/variadic_args.cpp +++ b/examples/source/variadic_args.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include diff --git a/examples/variadic_args_shifted.cpp b/examples/source/variadic_args_shifted.cpp similarity index 91% rename from examples/variadic_args_shifted.cpp rename to examples/source/variadic_args_shifted.cpp index 7e47c3fd..67563e08 100644 --- a/examples/variadic_args_shifted.cpp +++ b/examples/source/variadic_args_shifted.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include diff --git a/examples/variadic_args_storage.cpp b/examples/source/variadic_args_storage.cpp similarity index 92% rename from examples/variadic_args_storage.cpp rename to examples/source/variadic_args_storage.cpp index dc6fa31d..be958c1e 100644 --- a/examples/variadic_args_storage.cpp +++ b/examples/source/variadic_args_storage.cpp @@ -1,5 +1,5 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include #include #include diff --git a/examples/wip/lua_inheritance.cpp b/examples/source/wip/lua_inheritance.cpp similarity index 89% rename from examples/wip/lua_inheritance.cpp rename to examples/source/wip/lua_inheritance.cpp index aaab52be..2ddffef6 100644 --- a/examples/wip/lua_inheritance.cpp +++ b/examples/source/wip/lua_inheritance.cpp @@ -1,7 +1,7 @@ #define SOL_CHECK_ARGUMENTS 1 -#include +#include -#include "../assert.hpp" +#include #include int main(int, char*[]) { diff --git a/examples/usertype_simple.cpp b/examples/usertype_simple.cpp deleted file mode 100644 index dd64a934..00000000 --- a/examples/usertype_simple.cpp +++ /dev/null @@ -1,98 +0,0 @@ -#define SOL_CHECK_ARGUMENTS 1 -#include - -#include -#include -#include "assert.hpp" - -class generator { -private: - int data = 2; - -public: - int get_data() const { return data; } - void set_data(int value) { data = value % 10; } - - std::vector generate_list() { - return { data, data * 2, data * 3, data * 4, data * 5 }; - } -}; - -struct my_data { - int first = 4; - int second = 8; - int third = 12; -}; - -int main() { - std::cout << "=== usertype_simple ===" << std::endl; - - sol::state lua; - lua.open_libraries(); - - // simple_usertype acts and behaves like - // a regular usertype - lua.new_simple_usertype("my_data", - "first", &my_data::first, - "second", &my_data::second, - "third", &my_data::third - ); - - { - // But, simple_usertype also has a `set` function - // where you can append things to the - // method listing after doing `create_simple_usertype`. - auto generator_registration = lua.create_simple_usertype(); - generator_registration.set("data", sol::property(&generator::get_data, &generator::set_data)); - // you MUST set the usertype after - // creating it - lua.set_usertype("generator", generator_registration); - } - - // Can update a usertype at runtime, after registration - lua["generator"]["generate_list"] = [](generator& self) { return self.generate_list(); }; - // can set 'static methods' (no self) as well - lua["generator"]["get_num"] = []() { return 100; }; - - // Mix it all together! - lua.script(R"( -mdata = my_data.new() -)"); - lua.script(R"( -local g = generator.new() -g.data = mdata.first -list1 = g:generate_list() -g.data = mdata.second -list2 = g:generate_list() -g.data = mdata.third -list3 = g:generate_list() -)"); - lua.script(R"( -print("From lua: ") -for i = 1, #list1 do - print("\tlist1[" .. i .. "] = " .. list1[i]) -end -for i = 1, #list2 do - print("\tlist2[" .. i .. "] = " .. list2[i]) -end -for i = 1, #list3 do - print("\tlist3[" .. i .. "] = " .. list3[i]) -end - -)"); - my_data& mdata = lua["mdata"]; - std::vector& list1 = lua["list1"]; - std::vector& list2 = lua["list2"]; - std::vector& list3 = lua["list3"]; - c_assert(list1.size() == 5); - c_assert(list2.size() == 5); - c_assert(list3.size() == 5); - for (int i = 1; i <= 5; ++i) { - c_assert(list1[i - 1] == (mdata.first % 10) * i); - c_assert(list2[i - 1] == (mdata.second % 10) * i); - c_assert(list3[i - 1] == (mdata.third % 10) * i); - } - - std::cout << std::endl; - return 0; -} \ No newline at end of file diff --git a/sol/as_args.hpp b/include/sol/as_args.hpp similarity index 85% rename from sol/as_args.hpp rename to include/sol/as_args.hpp index ad49be21..b2d25275 100644 --- a/sol/as_args.hpp +++ b/include/sol/as_args.hpp @@ -1,8 +1,8 @@ -// sol2 +// sol3 // The MIT License (MIT) -// Copyright (c) 2013-2018 Rapptz, ThePhD and contributors +// Copyright (c) 2013-2019 Rapptz, ThePhD and contributors // 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 @@ -11,7 +11,7 @@ // 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 +// The above copyright notice and this Spermission 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 @@ -34,12 +34,12 @@ namespace sol { template auto as_args(Source&& source) { - return as_args_t{ std::forward(source) }; + return as_args_t { std::forward(source) }; } namespace stack { template - struct pusher> { + struct unqualified_pusher> { int push(lua_State* L, const as_args_t& e) { int p = 0; for (const auto& i : e.src) { diff --git a/sol/as_returns.hpp b/include/sol/as_returns.hpp similarity index 93% rename from sol/as_returns.hpp rename to include/sol/as_returns.hpp index 5ff8b877..76448a8f 100644 --- a/sol/as_returns.hpp +++ b/include/sol/as_returns.hpp @@ -1,8 +1,8 @@ -// sol2 +// sol3 // The MIT License (MIT) -// Copyright (c) 2013-2018 Rapptz, ThePhD and contributors +// Copyright (c) 2013-2019 Rapptz, ThePhD and contributors // 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 @@ -40,7 +40,7 @@ namespace sol { namespace stack { template - struct pusher> { + struct unqualified_pusher> { int push(lua_State* L, const as_returns_t& e) { auto& src = detail::unwrap(e.src); int p = 0; diff --git a/include/sol/base_traits.hpp b/include/sol/base_traits.hpp new file mode 100644 index 00000000..712c8b9b --- /dev/null +++ b/include/sol/base_traits.hpp @@ -0,0 +1,110 @@ +// sol3 + +// The MIT License (MIT) + +// Copyright (c) 2013-2019 Rapptz, ThePhD and contributors + +// 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. + +#ifndef SOL_BASE_TRAITS_HPP +#define SOL_BASE_TRAITS_HPP + +#include + +namespace sol { + namespace detail { + struct unchecked_t {}; + const unchecked_t unchecked = unchecked_t{}; + } // namespace detail + + namespace meta { + using sfinae_yes_t = std::true_type; + using sfinae_no_t = std::false_type; + + template + using void_t = void; + + template + using unqualified = std::remove_cv>; + + template + using unqualified_t = typename unqualified::type; + + namespace meta_detail { + template + struct unqualified_non_alias : unqualified {}; + + template