diff --git a/docs/source/functions.rst b/docs/source/functions.rst index faa7903b..b7709ee0 100644 --- a/docs/source/functions.rst +++ b/docs/source/functions.rst @@ -63,6 +63,8 @@ functions and argument passing All arguments are forwarded. Unlike :doc:`get/set/operator[] on sol::state` or :doc:`sol::table`, value semantics are not used here. It is forwarding reference semantics, which do not copy/move unless it is specifically done by the receiving functions / specifically done by the user. +You can change this behavior by defining ``SOL_FUNCTION_CALL_VALUE_SEMANTICS``, as defined in the :doc:`safety configuration page`. + .. note:: This also means that you should pass and receive arguments in certain ways to maximize efficiency. For example, ``sol::table``, ``sol::object``, ``sol::userdata`` and friends are cheap to copy, and should simply by taken as values. This includes primitive types like ``int`` and ``double``. However, C++ types -- if you do not want copies -- should be taken as ``const type&`` or ``type&``, to save on copies if it's important. Note that taking references from Lua also means you can modify the data inside of Lua directly, so be careful. Lua by default deals with things mostly by reference (save for primitive types). diff --git a/docs/source/safety.rst b/docs/source/safety.rst index d0745f7a..82e30243 100644 --- a/docs/source/safety.rst +++ b/docs/source/safety.rst @@ -62,6 +62,11 @@ Safety Config * If ``SOL_SAFE_NUMERICS`` is defined, turns off number precision and integer precision fitting when pushing numbers into sol2 * **Not** turned on by default under any settings: *this MUST be turned on manually* +``SOL_FUNCTION_CALL_VALUE_SEMANTICS`` triggers the following changes: + * Function arguments and returns into Lua (``sol:function`` and similar) do not pass their arguments by reference: they get copied + * Individual types can be blessed by template specializing ``sol::is_value_semantic_for_function`` + * **Not** turned on by default under any settings: *this MUST be turned on manually* + ``SOL_STRINGS_ARE_NUMBERS`` triggers the following changes: * Allows automatic to-string conversions for numbers - ``lua_tolstring`` conversions are not permitted on numbers through sol3 by default: only actual strings are allowed @@ -124,6 +129,10 @@ Feature Config * If this is defined to a numeric value, it uses that numeric value for the number of bytes of input to be put into the error message blurb in standard tracebacks and ``chunkname`` descriptions for ``.script``/``.script_file`` usage. * Defaults to the ``LUA_ID_SIZE`` macro if defined, or some basic internal value like 2048. +``SOL_LUAJIT`` triggers the following change: + * Has sol2 expect LuaJIT, and all of its quirks. + * Turns on by default if the macro ``LUAJIT_VERSION`` is detected from including Lua headers without any work on your part. Can also be manually defined. + .. _config-memory: Memory Config diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 6a11680d..219f0ca2 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -63,7 +63,6 @@ function (MAKE_EXAMPLE example_source_file example_suffix target_sol) 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}") set(example_name "${example_name}${example_suffix}") - if (example_output_relative_dir_name STREQUAL "") set(example_output_name "${example_name}") else() @@ -88,7 +87,7 @@ function (MAKE_EXAMPLE example_source_file example_suffix target_sol) -ftemplate-backtrace-limit=0 -Wno-unknown-warning -Wno-unknown-warning-option -Wall -Wpedantic -Werror -pedantic -pedantic-errors - -Wno-noexcept-type) + -Wno-noexcept-type -Wshadow -Wconversion) if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") # For another day, when C++ is not so crap diff --git a/examples/source/exception_handler.cpp b/examples/source/exception_handler.cpp index 5fdb4dd4..852afe62 100644 --- a/examples/source/exception_handler.cpp +++ b/examples/source/exception_handler.cpp @@ -17,11 +17,11 @@ int my_exception_handler(lua_State* L, sol::optional mayb } else { std::cout << "(from the description parameter): "; - std::cout.write(description.data(), description.size()); + std::cout.write(description.data(), static_cast(description.size())); std::cout << std::endl; } - // you must push 1 element onto the stack to be + // you must push 1 element onto the stack to be // transported through as the error object in Lua // note that Lua -- and 99.5% of all Lua users and libraries -- expects a string // so we push a single string (in our case, the description of the error) @@ -44,7 +44,7 @@ int main() { sol::protected_function_result pfr = lua.safe_script("will_throw()", &sol::script_pass_on_error); c_assert(!pfr.valid()); - + sol::error err = pfr; std::cout << err.what() << std::endl; diff --git a/examples/source/usertype.constructors.cpp b/examples/source/usertype.constructors.cpp deleted file mode 100644 index ed97a8df..00000000 --- a/examples/source/usertype.constructors.cpp +++ /dev/null @@ -1,50 +0,0 @@ -#define SOL_ALL_SAFETIES_ON 1 -#include - -#include -#include - -class MyClass { -public: - MyClass(double d) : data(d) {} - double data; -}; - -int main() { - std::cout << "=== coroutine ===" << std::endl; - - - sol::state lua; - lua.open_libraries(sol::lib::base); - - lua.new_usertype("MyClass", - sol::meta_function::construct, sol::factories( - // MyClass.new(...) -- dot syntax, no "self" value passed in - [](const double& d) {return std::make_shared(d);}, - // MyClass:new(...) -- colon syntax, passes in the "self" value - // as first argument implicitly - [](sol::object, const double& d) {return std::make_shared(d);} - ), - // MyClass(...) syntax, only - sol::call_constructor, sol::factories([](const double& d) {return std::make_shared(d);}), - "data", &MyClass::data - ); - - sol::optional maybe_error = lua.safe_script(R"( - d1 = MyClass(2.1) - d2 = MyClass:new(3.1) - d3 = MyClass(4.1) - assert(d1.data == 2.1) - assert(d2.data == 2.1) - assert(d3.data == 2.1) - )", sol::script_pass_on_error); - - if (maybe_error) { - // something went wrong! - std::cerr << "Something has gone horribly unexpected and wrong:\n" << maybe_error->what() << std::endl; - return 1; - } - - // everything is okay! - return 0; -} diff --git a/examples/source/usertype.cpp b/examples/source/usertype.cpp index 75c6e89b..861a64e9 100644 --- a/examples/source/usertype.cpp +++ b/examples/source/usertype.cpp @@ -16,7 +16,7 @@ public: } int test(int x) { - return static_cast(name.length() + x); + return static_cast(name.length()) + x; } }; diff --git a/examples/source/usertype_constructors.cpp b/examples/source/usertype_constructors.cpp new file mode 100644 index 00000000..80ebda03 --- /dev/null +++ b/examples/source/usertype_constructors.cpp @@ -0,0 +1,55 @@ +#define SOL_ALL_SAFETIES_ON 1 +#include + +#include +#include + +class MyClass { +public: + MyClass(double d) : data(d) { + } + double data; +}; + +int main() { + std::cout << "=== usertype constructors ===" << std::endl; + + + sol::state lua; + lua.open_libraries(sol::lib::base); + + lua.new_usertype("MyClass", + sol::meta_function::construct, + sol::factories( + // MyClass.new(...) -- dot syntax, no "self" value passed in + [](const double& d) { return std::make_shared(d); }, + // MyClass:new(...) -- colon syntax, passes in the "self" value + // as first argument implicitly + [](sol::object, const double& d) { return std::make_shared(d); }), + // MyClass(...) syntax, only + sol::call_constructor, + sol::factories([](const double& d) { return std::make_shared(d); }), + "data", + &MyClass::data); + + sol::optional maybe_error = lua.safe_script(R"( + d1 = MyClass(2.1) + d2 = MyClass:new(3.1) + d3 = MyClass(4.1) + assert(d1.data == 2.1) + assert(d2.data == 3.1) + assert(d3.data == 4.1) + )", + sol::script_pass_on_error); + + if (maybe_error) { + // something went wrong! + std::cerr << "Something has gone horribly unexpected and wrong:\n" << maybe_error->what() << std::endl; + return 1; + } + + // everything is okay! + std::cout << "Everything went okay and all the asserts passed!" << std::endl; + + return 0; +} diff --git a/include/sol/as_returns.hpp b/include/sol/as_returns.hpp index 8bf39f1e..ee6844e2 100644 --- a/include/sol/as_returns.hpp +++ b/include/sol/as_returns.hpp @@ -1,4 +1,4 @@ -// sol3 +// sol3 // The MIT License (MIT) @@ -35,7 +35,7 @@ namespace sol { template auto as_returns(Source&& source) { - return as_returns_t>{ std::forward(source) }; + return as_returns_t> { std::forward(source) }; } namespace stack { diff --git a/include/sol/base_traits.hpp b/include/sol/base_traits.hpp index 6d8da9b5..53f084ff 100644 --- a/include/sol/base_traits.hpp +++ b/include/sol/base_traits.hpp @@ -28,8 +28,8 @@ namespace sol { namespace detail { - struct unchecked_t {}; - const unchecked_t unchecked = unchecked_t{}; + struct unchecked_t { }; + const unchecked_t unchecked = unchecked_t {}; } // namespace detail namespace meta { @@ -47,13 +47,13 @@ namespace sol { namespace meta_detail { template - struct unqualified_non_alias : unqualified {}; + struct unqualified_non_alias : unqualified { }; template