diff --git a/sol/function.hpp b/sol/function.hpp index c3ba6378..631dddd6 100644 --- a/sol/function.hpp +++ b/sol/function.hpp @@ -108,7 +108,10 @@ public: class function : public reference { public: - static reference default_handler; + static reference& default_handler() { + static sol::reference h; + return h; + } private: struct handler { @@ -196,7 +199,7 @@ public: sol::reference error_handler; function() = default; - function(lua_State* L, int index = -1): reference(L, index), error_handler(default_handler) { + function(lua_State* L, int index = -1): reference(L, index), error_handler(default_handler()) { type_assert(L, index, type::function); } function(const function&) = default; @@ -226,8 +229,6 @@ public: } }; -sol::reference function::default_handler; - namespace stack { template struct pusher> { diff --git a/tests.cpp b/tests.cpp index 3f62aa9a..cf791df8 100644 --- a/tests.cpp +++ b/tests.cpp @@ -472,20 +472,24 @@ TEST_CASE("functions/overloaded", "Check if overloaded function resolution templ TEST_CASE("functions/return_order_and_multi_get", "Check if return order is in the same reading order specified in Lua") { const static std::tuple triple = std::make_tuple(10, 11, 12); + const static std::tuple paired = std::make_tuple(10, 10.f); sol::state lua; lua.set_function("f", [] { return std::make_tuple(10, 11, 12); - }); + } ); + int a = 0; + lua.set_function( "h", []() { + return std::make_tuple( 10, 10.0f ); + } ); lua.script("function g() return 10, 11, 12 end\nx,y,z = g()"); auto tcpp = lua.get("f").call(); - auto tlua = lua.get("g").call(); - auto tluaget = lua.get("x", "y", "z"); - std::cout << "cpp: " << std::get<0>(tcpp) << ',' << std::get<1>(tcpp) << ',' << std::get<2>(tcpp) << std::endl; - std::cout << "lua: " << std::get<0>(tlua) << ',' << std::get<1>(tlua) << ',' << std::get<2>(tlua) << std::endl; - std::cout << "lua xyz: " << lua.get("x") << ',' << lua.get("y") << ',' << lua.get("z") << std::endl; + auto tlua = lua.get( "g" ).call(); + auto tcpp2 = lua.get( "h" ).call(); + auto tluaget = lua.get( "x", "y", "z" ); REQUIRE(tcpp == triple); REQUIRE(tlua == triple); REQUIRE(tluaget == triple); + REQUIRE(tcpp2 == paired); } TEST_CASE("functions/deducing_return_order_and_multi_get", "Check if return order is in the same reading order specified in Lua, with regular deducing calls") {