From d863a567604f49434d0179ae81362a09438937e7 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Wed, 2 Nov 2016 19:00:16 -0400 Subject: [PATCH] Weee, script return value tests --- single/sol/sol.hpp | 24 ++++++++------ sol/load_result.hpp | 2 +- sol/state_view.hpp | 18 +++++++---- tests.cpp | 76 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+), 16 deletions(-) diff --git a/single/sol/sol.hpp b/single/sol/sol.hpp index bdac1e79..dde2f071 100644 --- a/single/sol/sol.hpp +++ b/single/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 2016-11-01 23:23:32.715464 UTC -// This header was generated with sol v2.14.12 (revision 0a165dc) +// Generated 2016-11-02 22:59:47.937773 UTC +// This header was generated with sol v2.14.12 (revision 9836aba) // https://github.com/ThePhD/sol2 #ifndef SOL_SINGLE_INCLUDE_HPP @@ -11962,7 +11962,7 @@ namespace sol { template decltype(auto) call(Args&&... args) { - return get().template call(std::forward(args)...); + return get().template call(std::forward(args)...); } template @@ -12069,6 +12069,10 @@ namespace sol { } + state_view(this_state L) : state_view(L.L){ + + } + lua_State* lua_state() const { return L; } @@ -12187,17 +12191,19 @@ namespace sol { } function_result script(const std::string& code) { - int index = (::std::max)(lua_gettop(L), 1); + int index = lua_gettop(L); stack::script(L, code); - int returns = lua_gettop(L) - (index - 1); - return function_result(L, index, returns); + int postindex = lua_gettop(L); + int returns = postindex - index; + return function_result(L, (std::max)(postindex - (returns - 1), 1), returns); } function_result script_file(const std::string& filename) { - int index = (::std::max)(lua_gettop(L), 1); + int index = lua_gettop(L); stack::script_file(L, filename); - int returns = lua_gettop(L) - (index - 1); - return function_result(L, index, returns); + int postindex = lua_gettop(L); + int returns = postindex - index; + return function_result(L, (std::max)(postindex - (returns - 1), 1), returns); } load_result load(const std::string& code) { diff --git a/sol/load_result.hpp b/sol/load_result.hpp index 1a91cc6f..0f465a2b 100644 --- a/sol/load_result.hpp +++ b/sol/load_result.hpp @@ -119,7 +119,7 @@ namespace sol { template decltype(auto) call(Args&&... args) { - return get().template call(std::forward(args)...); + return get().template call(std::forward(args)...); } template diff --git a/sol/state_view.hpp b/sol/state_view.hpp index a9dcd5d9..d3c12d3b 100644 --- a/sol/state_view.hpp +++ b/sol/state_view.hpp @@ -115,6 +115,10 @@ namespace sol { } + state_view(this_state L) : state_view(L.L){ + + } + lua_State* lua_state() const { return L; } @@ -233,17 +237,19 @@ namespace sol { } function_result script(const std::string& code) { - int index = (::std::max)(lua_gettop(L), 1); + int index = lua_gettop(L); stack::script(L, code); - int returns = lua_gettop(L) - (index - 1); - return function_result(L, index, returns); + int postindex = lua_gettop(L); + int returns = postindex - index; + return function_result(L, (std::max)(postindex - (returns - 1), 1), returns); } function_result script_file(const std::string& filename) { - int index = (::std::max)(lua_gettop(L), 1); + int index = lua_gettop(L); stack::script_file(L, filename); - int returns = lua_gettop(L) - (index - 1); - return function_result(L, index, returns); + int postindex = lua_gettop(L); + int returns = postindex - index; + return function_result(L, (std::max)(postindex - (returns - 1), 1), returns); } load_result load(const std::string& code) { diff --git a/tests.cpp b/tests.cpp index e9faf8a3..e1a92b54 100644 --- a/tests.cpp +++ b/tests.cpp @@ -716,3 +716,79 @@ TEST_CASE("numbers/integers", "make sure integers are detectable on most platfor REQUIRE_FALSE(b_is_int); REQUIRE(b_is_double); } + +TEST_CASE("state/script-returns", "make sure script returns are done properly") { + std::string script = + R"( +local example = +{ + str = "this is a string", + num = 1234, + + func = function(self) + print(self.str) + return "fstr" + end +} + +return example; +)"; + + auto bar = [&script](sol::this_state l) { + sol::state_view lua = l; + sol::table data = lua.script(script); + + std::string str = data["str"]; + int num = data["num"]; + std::string fstr = data["func"](data); + REQUIRE(str == "this is a string"); + REQUIRE(fstr == "fstr"); + REQUIRE(num == 1234); + }; + + auto foo = [&script](int, sol::this_state l) { + sol::state_view lua = l; + sol::table data = lua.script(script); + + std::string str = data["str"]; + int num = data["num"]; + std::string fstr = data["func"](data); + REQUIRE(str == "this is a string"); + REQUIRE(fstr == "fstr"); + REQUIRE(num == 1234); + }; + + auto bar2 = [&script](sol::this_state l) { + sol::state_view lua = l; + sol::table data = lua.do_string(script); + + std::string str = data["str"]; + int num = data["num"]; + std::string fstr = data["func"](data); + REQUIRE(str == "this is a string"); + REQUIRE(fstr == "fstr"); + REQUIRE(num == 1234); + }; + + auto foo2 = [&script](int, sol::this_state l) { + sol::state_view lua = l; + sol::table data = lua.do_string(script); + + std::string str = data["str"]; + int num = data["num"]; + std::string fstr = data["func"](data); + REQUIRE(str == "this is a string"); + REQUIRE(fstr == "fstr"); + REQUIRE(num == 1234); + }; + + sol::state lua; + lua.open_libraries(); + + lua.set_function("foo", foo); + lua.set_function("foo2", foo2); + lua.set_function("bar", bar); + lua.set_function("bar2", bar2); + + lua.script("bar() bar2() foo(1) foo2(1)"); +}