From a7214ab61c42f1454a8e17c633085f12ed6bef5a Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Mon, 26 Jul 2021 16:39:40 -0700 Subject: cool --- src/javascript/jsc/bindings/helpers.h | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'src/javascript/jsc/bindings/helpers.h') diff --git a/src/javascript/jsc/bindings/helpers.h b/src/javascript/jsc/bindings/helpers.h index c21f3fb3c..b1839d0b4 100644 --- a/src/javascript/jsc/bindings/helpers.h +++ b/src/javascript/jsc/bindings/helpers.h @@ -6,6 +6,7 @@ #include + template class Wrap { public: @@ -17,16 +18,20 @@ public: cpp = static_cast(static_cast(&zig)); }; + Wrap(ZigType* zig){ + cpp = static_cast(static_cast(&zig)); + }; + + Wrap(CppType _cpp){ - char* buffer = alignedBuffer(); - memcpy(buffer, std::move(reinterpret_cast(reinterpret_cast(&_cpp))), sizeof(CppType)); - cpp = reinterpret_cast(buffer); + auto buffer = alignedBuffer(); + cpp = new (buffer) CppType(_cpp); }; ~Wrap(){}; - char* alignedBuffer() { + unsigned char* alignedBuffer() { return result.bytes + alignof(CppType) - reinterpret_cast(result.bytes) % alignof(CppType); } @@ -37,9 +42,17 @@ public: return *static_cast(static_cast(&obj)); } - static ZigType wrap(CppType* obj) { - return *static_cast(static_cast(obj)); + static CppType unwrap(ZigType obj) { + return *static_cast(static_cast(&obj)); } + + static CppType* unwrap(ZigType* obj) { + return static_cast(static_cast(obj)); + } + + + + }; -- cgit v1.2.3