From af908f97dc55738052f261499467aa1decf6f90d Mon Sep 17 00:00:00 2001 From: "K.J. Valencik" Date: Tue, 31 Aug 2021 19:32:37 -0400 Subject: [PATCH] fix(neon-sys): Add a hack for recent electron versions on windows to use Buffer to get the contents of an ArrayBuffer Fixes https://github.com/neon-bindings/neon/issues/782 --- crates/neon-sys/native/src/neon.cc | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/crates/neon-sys/native/src/neon.cc b/crates/neon-sys/native/src/neon.cc index 0286eb3e3..7b2981bdd 100644 --- a/crates/neon-sys/native/src/neon.cc +++ b/crates/neon-sys/native/src/neon.cc @@ -221,15 +221,27 @@ extern "C" bool Neon_ArrayBuffer_New(v8::Local *out, v8::Isolat extern "C" size_t Neon_ArrayBuffer_Data(v8::Isolate *isolate, void **base_out, v8::Local buffer) { -#if (V8_MAJOR_VERSION >= 8) - auto contents = buffer->GetBackingStore(); - *base_out = contents->Data(); - return contents->ByteLength(); -#else - v8::ArrayBuffer::Contents contents = buffer->GetContents(); - *base_out = contents.Data(); - return contents.ByteLength(); -#endif + // HACK: `v8::ArrayBuffer::Contents` leaks `std::shared_ptr` in the function signature + // Since recent versions of Electron are built with `clang`, `neon-sys` will fail to compile with MSVC + // As a workaround, when building with MSVC on recent versions of Node, create a temporary `Buffer` to extra + // the contents. + // + // https://github.com/electron/electron/issues/29893 + #if _MSC_VER && NODE_MODULE_VERSION >= 89 + v8::Local local; + node::Buffer::New(isolate, buffer, 0, buffer->ByteLength()).ToLocal(&local); + return Neon_Buffer_Data(isolate, base_out, local); + #else + #if (V8_MAJOR_VERSION >= 8) + auto contents = buffer->GetBackingStore(); + *base_out = contents->Data(); + return contents->ByteLength(); + #else + v8::ArrayBuffer::Contents contents = buffer->GetContents(); + *base_out = contents.Data(); + return contents.ByteLength(); + #endif + #endif } extern "C" bool Neon_Tag_IsArrayBuffer(v8::Isolate *isolate, v8::Local value) {