Weee, script return value tests

This commit is contained in:
ThePhD 2016-11-02 19:00:16 -04:00
parent 9836aba51d
commit d863a56760
4 changed files with 104 additions and 16 deletions

View File

@ -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<typename... Ret, typename... Args>
decltype(auto) call(Args&&... args) {
return get<function>().template call<Ret...>(std::forward<Args>(args)...);
return get<protected_function>().template call<Ret...>(std::forward<Args>(args)...);
}
template<typename... Args>
@ -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) {

View File

@ -119,7 +119,7 @@ namespace sol {
template<typename... Ret, typename... Args>
decltype(auto) call(Args&&... args) {
return get<function>().template call<Ret...>(std::forward<Args>(args)...);
return get<protected_function>().template call<Ret...>(std::forward<Args>(args)...);
}
template<typename... Args>

View File

@ -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) {

View File

@ -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)");
}