Skip to content

Commit

Permalink
Fix properties of properties returning a copy
Browse files Browse the repository at this point in the history
  • Loading branch information
matusnovak committed Oct 16, 2023
1 parent 4ae1512 commit 3255318
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ temp/
install/
.vscode/
.vs/
.idea/
cmake-build-*/
*.kdev4
*~
CMakeSettings.json
Expand Down
2 changes: 1 addition & 1 deletion include/wrenbind17/caller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ namespace wrenbind17 {

static void getter(WrenVM* vm) {
auto self = PopHelper<T*>::f(vm, 0);
PushHelper<V>::f(vm, 0, std::forward<decltype(self->*Ptr)>(self->*Ptr));
PushHelper<V*>::f(vm, 0, &(self->*Ptr));
}
};
} // namespace detail
Expand Down
6 changes: 6 additions & 0 deletions include/wrenbind17/push.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ namespace wrenbind17 {
}
};

template <> struct PushHelper<std::string*> {
static inline void f(WrenVM* vm, int idx, const std::string* value) {
wrenSetSlotString(vm, idx, value->c_str());
}
};

template <size_t N> struct PushHelper<const char (&)[N]> {
static inline void f(WrenVM* vm, int idx, const char (&value)[N]) {
wrenSetSlotString(vm, idx, value);
Expand Down
3 changes: 2 additions & 1 deletion tests/operators.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <limits>
#include <catch2/catch.hpp>
#include <filesystem>
#include <limits>
#include <wrenbind17/wrenbind17.hpp>

namespace wren = wrenbind17;
Expand Down
85 changes: 85 additions & 0 deletions tests/props.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,88 @@ TEST_CASE("Properties from base class") {
}
}

typedef struct Vector2 {
float x;
float y;
} Vector2;

typedef struct Camera2D {
Vector2 offset;
float rotation;

void debug() const {
std::cout << "Camera2D" << std::endl;
std::cout << "offset: " << offset.x << ", " << offset.y << std::endl;
std::cout << "rotation: " << rotation << std::endl;
}
} Camera2D;

TEST_CASE("Properties of properties") {
wren::VM vm;
auto& m = vm.module("test");

{
auto& cls = m.klass<Vector2>("Vector2");
cls.ctor<>();
cls.var<&Vector2::x>("x");
cls.var<&Vector2::y>("y");
}

{
auto& cls = m.klass<Camera2D>("Camera2D");
cls.ctor<>();
cls.var<&Camera2D::offset>("offset");
cls.var<&Camera2D::rotation>("rotation");
cls.func<&Camera2D::debug>("debug");
}

const std::string code = R"(
import "test" for Camera2D, Vector2
class Main {
static main1() {
var cam = Camera2D.new()
cam.rotation = 180
cam.offset.x = 1280
cam.offset.y = 768
System.print("Cam Offset: %(cam.offset.x), %(cam.offset.y) ")
cam.debug()
return cam.offset.x
}
static main2() {
var cam = Camera2D.new()
cam.rotation = 180
cam.offset.x = 1280
cam.offset.y = 768
System.print("Cam Offset: %(cam.offset.x), %(cam.offset.y) ")
cam.debug()
return cam.offset
}
}
)";

vm.runFromSource("main", code);

{ // Main 1, return a component of a property
auto main = vm.find("main", "Main").func("main1()");
auto r = main();
REQUIRE(r.is<float>());
REQUIRE(r.as<float>() == Approx(1280.0f));
}

{ // Main 2, return a property itself
auto main = vm.find("main", "Main").func("main2()");
auto r = main();
REQUIRE(r.is<Vector2>());
auto vec = r.as<Vector2>();
REQUIRE(vec.x == Approx(1280.0f));
REQUIRE(vec.y == Approx(768.0f));
}
}

0 comments on commit 3255318

Please sign in to comment.