2019-11-30 02:08:41 +08:00
|
|
|
// sol3
|
2017-12-20 17:58:32 +08:00
|
|
|
|
|
|
|
// The MIT License (MIT)
|
|
|
|
|
2020-08-13 22:42:37 +08:00
|
|
|
// Copyright (c) 2013-2020 Rapptz, ThePhD and contributors
|
2017-12-20 17:58:32 +08:00
|
|
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
// this software and associated documentation files (the "Software"), to deal in
|
|
|
|
// the Software without restriction, including without limitation the rights to
|
|
|
|
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
// the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
// subject to the following conditions:
|
|
|
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2018-12-20 12:17:15 +08:00
|
|
|
#include "sol_test.hpp"
|
2017-08-22 03:25:43 +08:00
|
|
|
|
|
|
|
#include <catch.hpp>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
|
2019-05-21 15:57:10 +08:00
|
|
|
TEST_CASE("policies/self", "ensure we return a direct reference to the lua userdata rather than creating a new one") {
|
2017-08-22 03:25:43 +08:00
|
|
|
struct vec2 {
|
|
|
|
float x = 20.f;
|
|
|
|
float y = 20.f;
|
|
|
|
|
|
|
|
vec2& normalize() {
|
|
|
|
float len2 = x * x + y * y;
|
|
|
|
if (len2 != 0) {
|
|
|
|
float len = sqrtf(len2);
|
|
|
|
x /= len;
|
|
|
|
y /= len;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
~vec2() {
|
|
|
|
x = std::numeric_limits<float>::lowest();
|
|
|
|
y = std::numeric_limits<float>::lowest();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
sol::state lua;
|
|
|
|
lua.open_libraries(sol::lib::base);
|
|
|
|
|
2019-11-30 02:08:41 +08:00
|
|
|
lua.new_usertype<vec2>("vec2", "x", &vec2::x, "y", &vec2::y, "normalize", sol::policies(&vec2::normalize, sol::returns_self()));
|
2018-01-29 11:21:13 +08:00
|
|
|
|
|
|
|
auto result1 = lua.safe_script(R"(
|
2017-08-22 03:25:43 +08:00
|
|
|
v1 = vec2.new()
|
|
|
|
print('v1:', v1.x, v1.y)
|
|
|
|
v2 = v1:normalize()
|
|
|
|
print('v1:', v1.x, v1.y)
|
|
|
|
print('v2:', v2.x, v2.y)
|
|
|
|
print(v1, v2)
|
|
|
|
assert(rawequal(v1, v2))
|
|
|
|
v1 = nil
|
|
|
|
collectgarbage()
|
|
|
|
print(v2) -- v2 points to same, is not destroyed
|
2019-11-30 02:08:41 +08:00
|
|
|
)",
|
|
|
|
sol::script_pass_on_error);
|
2018-01-29 11:21:13 +08:00
|
|
|
REQUIRE(result1.valid());
|
2017-08-22 03:25:43 +08:00
|
|
|
}
|
|
|
|
|
2019-05-21 15:57:10 +08:00
|
|
|
TEST_CASE("policies/self_dependency", "ensure we can keep a userdata instance alive by attaching it to the lifetime of another userdata") {
|
2017-08-22 03:25:43 +08:00
|
|
|
struct dep;
|
|
|
|
struct gc_test;
|
|
|
|
static std::vector<dep*> deps_destroyed;
|
|
|
|
static std::vector<gc_test*> gc_tests_destroyed;
|
|
|
|
|
|
|
|
struct dep {
|
|
|
|
int value = 20;
|
|
|
|
~dep() {
|
2017-09-17 02:18:45 +08:00
|
|
|
std::cout << "\t"
|
2019-11-30 02:08:41 +08:00
|
|
|
<< "[C++] ~dep" << std::endl;
|
2017-08-22 03:25:43 +08:00
|
|
|
value = std::numeric_limits<int>::max();
|
|
|
|
deps_destroyed.push_back(this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct gc_test {
|
|
|
|
|
|
|
|
dep d;
|
|
|
|
|
|
|
|
~gc_test() {
|
2017-09-17 02:18:45 +08:00
|
|
|
std::cout << "\t"
|
2019-11-30 02:08:41 +08:00
|
|
|
<< "[C++] ~gc_test" << std::endl;
|
2017-08-22 03:25:43 +08:00
|
|
|
gc_tests_destroyed.push_back(this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
sol::state lua;
|
|
|
|
lua.open_libraries(sol::lib::base);
|
|
|
|
|
2019-11-30 02:08:41 +08:00
|
|
|
lua.new_usertype<dep>("dep", "value", &dep::value, sol::meta_function::to_string, [](dep& d) { return "{ " + std::to_string(d.value) + " }"; });
|
|
|
|
lua.new_usertype<gc_test>("gc_test", "d", sol::policies(&gc_test::d, sol::self_dependency()), sol::meta_function::to_string, [](gc_test& g) {
|
|
|
|
return "{ d: { " + std::to_string(g.d.value) + " } }";
|
|
|
|
});
|
2017-08-22 03:25:43 +08:00
|
|
|
|
2018-01-29 11:21:13 +08:00
|
|
|
auto result1 = lua.safe_script(R"(
|
2017-08-22 03:25:43 +08:00
|
|
|
g = gc_test.new()
|
|
|
|
d = g.d
|
|
|
|
print("new gc_test, d = g.d")
|
|
|
|
print("", g)
|
2019-11-30 02:08:41 +08:00
|
|
|
)",
|
|
|
|
sol::script_pass_on_error);
|
2018-01-29 11:21:13 +08:00
|
|
|
REQUIRE(result1.valid());
|
2017-08-22 03:25:43 +08:00
|
|
|
REQUIRE(deps_destroyed.empty());
|
|
|
|
REQUIRE(gc_tests_destroyed.empty());
|
|
|
|
|
|
|
|
gc_test* g = lua["g"];
|
|
|
|
dep* d = lua["d"];
|
|
|
|
|
2018-01-29 11:21:13 +08:00
|
|
|
auto result2 = lua.safe_script(R"(
|
2017-08-22 03:25:43 +08:00
|
|
|
print("g = nil, collectgarbage")
|
|
|
|
g = nil
|
|
|
|
collectgarbage()
|
|
|
|
print("", d)
|
2019-11-30 02:08:41 +08:00
|
|
|
)",
|
|
|
|
sol::script_pass_on_error);
|
2018-01-29 11:21:13 +08:00
|
|
|
REQUIRE(result2.valid());
|
|
|
|
|
2017-08-22 03:25:43 +08:00
|
|
|
REQUIRE(deps_destroyed.empty());
|
|
|
|
REQUIRE(gc_tests_destroyed.empty());
|
|
|
|
|
2018-01-29 11:21:13 +08:00
|
|
|
auto result3 = lua.safe_script(R"(
|
2017-08-22 03:25:43 +08:00
|
|
|
print("d = nil, collectgarbage")
|
|
|
|
d = nil
|
|
|
|
collectgarbage()
|
2019-11-30 02:08:41 +08:00
|
|
|
)",
|
|
|
|
sol::script_pass_on_error);
|
2018-01-29 11:21:13 +08:00
|
|
|
REQUIRE(result3.valid());
|
2017-08-22 03:25:43 +08:00
|
|
|
|
|
|
|
REQUIRE(deps_destroyed.size() == 1);
|
|
|
|
REQUIRE(gc_tests_destroyed.size() == 1);
|
|
|
|
REQUIRE(deps_destroyed[0] == d);
|
|
|
|
REQUIRE(gc_tests_destroyed[0] == g);
|
|
|
|
}
|
|
|
|
|
2019-05-21 15:57:10 +08:00
|
|
|
TEST_CASE("policies/stack_dependencies", "ensure we can take dependencies even to arguments pushed on the stack") {
|
2017-08-22 03:25:43 +08:00
|
|
|
struct holder;
|
|
|
|
struct depends_on_reference;
|
|
|
|
struct composition_related;
|
|
|
|
static std::vector<composition_related*> composition_relateds_destroyed;
|
|
|
|
static std::vector<holder*> holders_destroyed;
|
|
|
|
static std::vector<depends_on_reference*> depends_on_references_destroyed;
|
|
|
|
|
|
|
|
struct composition_related {
|
|
|
|
std::string text = "bark";
|
|
|
|
|
|
|
|
~composition_related() {
|
|
|
|
std::cout << "[C++] ~composition_related" << std::endl;
|
|
|
|
text = "";
|
|
|
|
composition_relateds_destroyed.push_back(this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct holder {
|
|
|
|
int value = 20;
|
|
|
|
~holder() {
|
|
|
|
std::cout << "[C++] ~holder" << std::endl;
|
|
|
|
value = std::numeric_limits<int>::max();
|
|
|
|
holders_destroyed.push_back(this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct depends_on_reference {
|
|
|
|
|
|
|
|
std::reference_wrapper<holder> href;
|
|
|
|
composition_related comp;
|
|
|
|
|
2019-11-30 02:08:41 +08:00
|
|
|
depends_on_reference(holder& h) : href(h) {
|
2017-09-17 02:18:45 +08:00
|
|
|
}
|
2017-08-22 03:25:43 +08:00
|
|
|
|
|
|
|
~depends_on_reference() {
|
|
|
|
std::cout << "[C++] ~depends_on_reference" << std::endl;
|
|
|
|
depends_on_references_destroyed.push_back(this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
sol::state lua;
|
|
|
|
lua.open_libraries(sol::lib::base);
|
|
|
|
|
2019-11-30 02:08:41 +08:00
|
|
|
lua.new_usertype<holder>("holder", "value", &holder::value);
|
2017-08-22 03:25:43 +08:00
|
|
|
lua.new_usertype<depends_on_reference>("depends_on_reference",
|
2019-11-30 02:08:41 +08:00
|
|
|
"new",
|
|
|
|
sol::policies(sol::constructors<depends_on_reference(holder&)>(), sol::stack_dependencies(-1, 2)),
|
|
|
|
"comp",
|
|
|
|
&depends_on_reference::comp);
|
2017-08-22 03:25:43 +08:00
|
|
|
|
2018-01-29 11:21:13 +08:00
|
|
|
auto result1 = lua.safe_script(R"(
|
2017-08-22 03:25:43 +08:00
|
|
|
h = holder.new()
|
|
|
|
dor = depends_on_reference.new(h)
|
|
|
|
c = dor.comp
|
2019-11-30 02:08:41 +08:00
|
|
|
)",
|
|
|
|
sol::script_pass_on_error);
|
2018-01-29 11:21:13 +08:00
|
|
|
REQUIRE(result1.valid());
|
2017-08-22 03:25:43 +08:00
|
|
|
REQUIRE(composition_relateds_destroyed.empty());
|
|
|
|
REQUIRE(holders_destroyed.empty());
|
|
|
|
REQUIRE(depends_on_references_destroyed.empty());
|
|
|
|
|
|
|
|
holder* h = lua["h"];
|
|
|
|
composition_related* c = lua["c"];
|
|
|
|
depends_on_reference* dor = lua["dor"];
|
|
|
|
|
|
|
|
REQUIRE(h == &dor->href.get());
|
|
|
|
REQUIRE(c == &dor->comp);
|
|
|
|
|
2018-01-29 11:21:13 +08:00
|
|
|
auto result2 = lua.safe_script(R"(
|
2017-08-22 03:25:43 +08:00
|
|
|
h = nil
|
|
|
|
collectgarbage()
|
|
|
|
)");
|
2018-01-29 11:21:13 +08:00
|
|
|
REQUIRE(result2.valid());
|
2017-08-22 03:25:43 +08:00
|
|
|
REQUIRE(composition_relateds_destroyed.empty());
|
|
|
|
REQUIRE(holders_destroyed.empty());
|
|
|
|
REQUIRE(depends_on_references_destroyed.empty());
|
|
|
|
|
2018-01-29 11:21:13 +08:00
|
|
|
auto result3 = lua.safe_script(R"(
|
2017-08-22 03:25:43 +08:00
|
|
|
c = nil
|
|
|
|
collectgarbage()
|
2019-11-30 02:08:41 +08:00
|
|
|
)",
|
|
|
|
sol::script_pass_on_error);
|
2018-01-29 11:21:13 +08:00
|
|
|
REQUIRE(result3.valid());
|
2017-08-22 03:25:43 +08:00
|
|
|
|
|
|
|
REQUIRE(composition_relateds_destroyed.empty());
|
|
|
|
REQUIRE(holders_destroyed.empty());
|
|
|
|
REQUIRE(depends_on_references_destroyed.empty());
|
|
|
|
|
2018-01-29 11:21:13 +08:00
|
|
|
auto result4 = lua.safe_script(R"(
|
2017-08-22 03:25:43 +08:00
|
|
|
dor = nil
|
|
|
|
collectgarbage()
|
2019-11-30 02:08:41 +08:00
|
|
|
)",
|
|
|
|
sol::script_pass_on_error);
|
2018-01-29 11:21:13 +08:00
|
|
|
REQUIRE(result4.valid());
|
2017-08-22 03:25:43 +08:00
|
|
|
|
|
|
|
REQUIRE(composition_relateds_destroyed.size() == 1);
|
|
|
|
REQUIRE(holders_destroyed.size() == 1);
|
|
|
|
REQUIRE(depends_on_references_destroyed.size() == 1);
|
|
|
|
REQUIRE(composition_relateds_destroyed[0] == c);
|
|
|
|
REQUIRE(holders_destroyed[0] == h);
|
|
|
|
REQUIRE(depends_on_references_destroyed[0] == dor);
|
|
|
|
}
|
|
|
|
|
|
|
|
int always_return_24(lua_State* L, int) {
|
|
|
|
return sol::stack::push(L, 24);
|
|
|
|
}
|
|
|
|
|
2019-05-21 15:57:10 +08:00
|
|
|
TEST_CASE("policies/custom", "ensure we can return dependencies on multiple things in the stack") {
|
2017-08-22 03:25:43 +08:00
|
|
|
|
|
|
|
sol::state lua;
|
2019-05-21 15:57:10 +08:00
|
|
|
lua.set_function("f", sol::policies([]() { return std::string("hi there"); }, always_return_24));
|
2017-08-22 03:25:43 +08:00
|
|
|
|
|
|
|
int value = lua["f"]();
|
|
|
|
REQUIRE(value == 24);
|
|
|
|
}
|