diff options
author | 2021-07-26 16:39:40 -0700 | |
---|---|---|
committer | 2021-07-26 16:39:40 -0700 | |
commit | a7214ab61c42f1454a8e17c633085f12ed6bef5a (patch) | |
tree | 7da3047f2d6cbf2b1e19fd670d0d40edce5e7192 /src/javascript/jsc/bindings/helpers.h | |
parent | 796a9854b4a698b309a6e7a4c040047558858da6 (diff) | |
download | bun-a7214ab61c42f1454a8e17c633085f12ed6bef5a.tar.gz bun-a7214ab61c42f1454a8e17c633085f12ed6bef5a.tar.zst bun-a7214ab61c42f1454a8e17c633085f12ed6bef5a.zip |
cool
Diffstat (limited to 'src/javascript/jsc/bindings/helpers.h')
-rw-r--r-- | src/javascript/jsc/bindings/helpers.h | 25 |
1 files changed, 19 insertions, 6 deletions
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 <JavaScriptCore/VM.h> + template<class CppType, typename ZigType> class Wrap { public: @@ -17,16 +18,20 @@ public: cpp = static_cast<CppType*>(static_cast<void*>(&zig)); }; + Wrap(ZigType* zig){ + cpp = static_cast<CppType*>(static_cast<void*>(&zig)); + }; + + Wrap(CppType _cpp){ - char* buffer = alignedBuffer(); - memcpy(buffer, std::move(reinterpret_cast<char*>(reinterpret_cast<void*>(&_cpp))), sizeof(CppType)); - cpp = reinterpret_cast<CppType*>(buffer); + auto buffer = alignedBuffer(); + cpp = new (buffer) CppType(_cpp); }; ~Wrap(){}; - char* alignedBuffer() { + unsigned char* alignedBuffer() { return result.bytes + alignof(CppType) - reinterpret_cast<intptr_t>(result.bytes) % alignof(CppType); } @@ -37,9 +42,17 @@ public: return *static_cast<ZigType*>(static_cast<void*>(&obj)); } - static ZigType wrap(CppType* obj) { - return *static_cast<ZigType*>(static_cast<void*>(obj)); + static CppType unwrap(ZigType obj) { + return *static_cast<CppType*>(static_cast<void*>(&obj)); } + + static CppType* unwrap(ZigType* obj) { + return static_cast<CppType*>(static_cast<void*>(obj)); + } + + + + }; |