mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
updated tests
fixed small error in state with new system all tests passing GCC 4.9 std=c++11
This commit is contained in:
parent
36e45d88b2
commit
b41f92adc9
|
@ -154,7 +154,7 @@ public:
|
||||||
template<typename Class, typename... CTor, typename... Args>
|
template<typename Class, typename... CTor, typename... Args>
|
||||||
state& new_userdata(const std::string& name, Args&&... args) {
|
state& new_userdata(const std::string& name, Args&&... args) {
|
||||||
constructors<types<CTor...>> ctor{};
|
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>
|
template<typename Class, typename... CArgs, typename... Args>
|
||||||
|
|
|
@ -40,7 +40,7 @@ template<typename T>
|
||||||
const std::string userdata_traits<T>::metatable = std::string("sol.").append(detail::demangle(typeid(T)));
|
const std::string userdata_traits<T>::metatable = std::string("sol.").append(detail::demangle(typeid(T)));
|
||||||
|
|
||||||
template<typename 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");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
34
tests.cpp
34
tests.cpp
|
@ -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"
|
REQUIRE_NOTHROW(lua.script("v = Vec.new(1, 2, 3)\n"
|
||||||
"print(v:normalized():length())" ));
|
"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())" );
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user