updated tests

fixed small error in state with new system
all tests passing GCC 4.9 std=c++11
This commit is contained in:
ThePhD 2014-06-27 21:34:18 -07:00
parent 36e45d88b2
commit b41f92adc9
3 changed files with 36 additions and 2 deletions

View File

@ -154,7 +154,7 @@ public:
template<typename Class, typename... CTor, typename... Args>
state& new_userdata(const std::string& name, Args&&... args) {
constructors<types<CTor...>> ctor{};
return new_userdata(name, ctor, std::forward<Args>(args)...);
return new_userdata<Class>(name, ctor, std::forward<Args>(args)...);
}
template<typename Class, typename... CArgs, typename... Args>

View File

@ -40,7 +40,7 @@ template<typename T>
const std::string userdata_traits<T>::metatable = std::string("sol.").append(detail::demangle(typeid(T)));
template<typename T>
const std::string userdata_traits<T>::metatable = std::string("sol.").append(detail::demangle(typeid(T))).append(".\xE2\x99\xBB");
const std::string userdata_traits<T>::gctable = std::string("sol.").append(detail::demangle(typeid(T))).append(".\xE2\x99\xBB");
}

View File

@ -703,3 +703,37 @@ TEST_CASE("userdata/issue-number-thirty-five", "using value types created from l
REQUIRE_NOTHROW(lua.script("v = Vec.new(1, 2, 3)\n"
"print(v:normalized():length())" ));
}
TEST_CASE("userdata/lua-stored-userdata", "ensure userdata values can be stored without keeping userdata object alive") {
struct Vec {
float x, y, z;
Vec(float x, float y, float z) : x{x}, y{y}, z{z} {}
float length() {
return sqrtf(x*x + y*y + z*z);
}
Vec normalized() {
float invS = 1 / length();
return {x * invS, y * invS, z * invS};
}
};
sol::state lua;
lua.open_libraries(sol::lib::base);
{
sol::constructors<sol::types<float, float, float>> ctor;
sol::userdata<Vec> udata("Vec", ctor,
"normalized", &Vec::normalized,
"length", &Vec::length);
lua.set_userdata(udata);
// userdata dies, but still usable in lua!
}
lua.script("collectgarbage()\n"
"v = Vec.new(1, 2, 3)\n"
"print(v:length())");
lua.script("v = Vec.new(1, 2, 3)\n"
"print(v:normalized():length())" );
}