diff --git a/include/sol/state_view.hpp b/include/sol/state_view.hpp index ce353415..3bd18ef4 100644 --- a/include/sol/state_view.hpp +++ b/include/sol/state_view.hpp @@ -86,7 +86,16 @@ namespace sol { optional loaded = is_loaded_package(key); if (loaded && loaded->valid()) return std::move(*loaded); + int before = lua_gettop(L); action(); + int after = lua_gettop(L); + if (before == after) { + // I mean, you were supposed to return + // something, ANYTHING, from your requires script. I guess I'll just + // but some trash in here, it's on you after that? + ensure_package(key, static_cast(L)); + return object(L, lua_nil); + } stack_reference sr(L, -1); if (create_global) set(key, sr); diff --git a/include/sol/table_proxy.hpp b/include/sol/table_proxy.hpp index 97823d5e..eda5556e 100644 --- a/include/sol/table_proxy.hpp +++ b/include/sol/table_proxy.hpp @@ -85,7 +85,7 @@ namespace sol { template table_proxy&& set(T&& item) && { - tuple_set(std::make_index_sequence>>(), std::forward(item)); + std::move(*this).tuple_set(std::make_index_sequence>>(), std::forward(item)); return std::move(*this); } diff --git a/single/include/sol/config.hpp b/single/include/sol/config.hpp index 0ecba8f1..c53a8873 100644 --- a/single/include/sol/config.hpp +++ b/single/include/sol/config.hpp @@ -20,8 +20,8 @@ // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // This file was generated with a script. -// Generated 2021-01-24 17:38:14.787587 UTC -// This header was generated with sol v3.2.3 (revision d363ccd7) +// Generated 2021-01-25 02:52:07.886776 UTC +// This header was generated with sol v3.2.3 (revision e1950b9a) // https://github.com/ThePhD/sol2 #ifndef SOL_SINGLE_CONFIG_HPP diff --git a/single/include/sol/forward.hpp b/single/include/sol/forward.hpp index 0e0cb469..6e9af917 100644 --- a/single/include/sol/forward.hpp +++ b/single/include/sol/forward.hpp @@ -20,8 +20,8 @@ // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // This file was generated with a script. -// Generated 2021-01-24 17:38:14.725587 UTC -// This header was generated with sol v3.2.3 (revision d363ccd7) +// Generated 2021-01-25 02:52:07.863772 UTC +// This header was generated with sol v3.2.3 (revision e1950b9a) // https://github.com/ThePhD/sol2 #ifndef SOL_SINGLE_INCLUDE_FORWARD_HPP diff --git a/single/include/sol/sol.hpp b/single/include/sol/sol.hpp index c4a4c99a..6d2ad776 100644 --- a/single/include/sol/sol.hpp +++ b/single/include/sol/sol.hpp @@ -20,8 +20,8 @@ // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // This file was generated with a script. -// Generated 2021-01-24 17:38:13.604128 UTC -// This header was generated with sol v3.2.3 (revision d363ccd7) +// Generated 2021-01-25 02:52:07.000664 UTC +// This header was generated with sol v3.2.3 (revision e1950b9a) // https://github.com/ThePhD/sol2 #ifndef SOL_SINGLE_INCLUDE_HPP @@ -24145,7 +24145,7 @@ namespace sol { template table_proxy&& set(T&& item) && { - tuple_set(std::make_index_sequence>>(), std::forward(item)); + std::move(*this).tuple_set(std::make_index_sequence>>(), std::forward(item)); return std::move(*this); } @@ -26553,7 +26553,16 @@ namespace sol { optional loaded = is_loaded_package(key); if (loaded && loaded->valid()) return std::move(*loaded); + int before = lua_gettop(L); action(); + int after = lua_gettop(L); + if (before == after) { + // I mean, you were supposed to return + // something, ANYTHING, from your requires script. I guess I'll just + // but some trash in here, it's on you after that? + ensure_package(key, static_cast(L)); + return object(L, lua_nil); + } stack_reference sr(L, -1); if (create_global) set(key, sr); diff --git a/tests/runtime_tests/source/state.cpp b/tests/runtime_tests/source/state.cpp index d42ba446..6003a05b 100644 --- a/tests/runtime_tests/source/state.cpp +++ b/tests/runtime_tests/source/state.cpp @@ -145,7 +145,7 @@ TEST_CASE("state/require_file", "opening files as 'requires'") { } TEST_CASE("state/require_script", "opening strings as 'requires' clauses") { - std::string code = "return { modfunc = function () return 221 end }"; + const std::string code = "return { modfunc = function () return 221 end }"; sol::state lua; sol::stack_guard luasg(lua); @@ -160,6 +160,29 @@ TEST_CASE("state/require_script", "opening strings as 'requires' clauses") { REQUIRE((thingy1 == thingy2)); } +TEST_CASE("state/require_script empty", "opening strings as 'requires' clauses that return nothing insert a bogus value") { + sol::state lua; + lua.open_libraries(sol::lib::package, sol::lib::base); + + lua.set_function("print_to_console", [](std::string const& message) { std::cout << message << std::endl; }); + + const std::string code = "print_to_console('hello from required file') i = 20"; + + { + int begintop = 0, endtop = 0; + test_stack_guard guard(lua, begintop, endtop); + lua.require_script("require_test", code, false); + } + + sol::optional maybe_error = lua.safe_script(R"( +require ("require_test") +print_to_console("hello from script") +assert(i == 20) +)", + sol::script_pass_on_error); + REQUIRE_FALSE(maybe_error.has_value()); +} + TEST_CASE("state/require", "require using a function") { struct open { static int open_func(lua_State* L) {