11/* Copyright (c) 2017-2019 Rolf Timmermans */
22#include " incoming_msg.h"
33
4+ #include " util/electron_helper.h"
45#include " util/error.h"
56
67namespace zmq {
@@ -14,39 +15,38 @@ IncomingMsg::~IncomingMsg() {
1415}
1516
1617Napi::Value IncomingMsg::IntoBuffer (const Napi::Env& env) {
17- #if !(NODE_RUNTIME_ELECTRON && NODE_MODULE_VERSION >= 109) // 109 is Electron v21 and up
18- if (moved) {
19- /* If ownership has been transferred, do not attempt to read the buffer
20- again in any case. This should not happen of course. */
21- ErrnoException (env, EINVAL).ThrowAsJavaScriptException ();
22- return env.Undefined ();
18+ static auto const noElectronMemoryCage = !hasElectronMemoryCage (env);
19+ if (noElectronMemoryCage) {
20+ if (moved) {
21+ /* If ownership has been transferred, do not attempt to read the buffer
22+ again in any case. This should not happen of course. */
23+ ErrnoException (env, EINVAL).ThrowAsJavaScriptException ();
24+ return env.Undefined ();
25+ }
2326 }
24-
25- static auto constexpr zero_copy_threshold = 1 << 7 ;
26- #endif
27-
2827 auto data = reinterpret_cast <uint8_t *>(zmq_msg_data (*ref));
2928 auto length = zmq_msg_size (*ref);
3029
31- #if !(NODE_RUNTIME_ELECTRON && NODE_MODULE_VERSION >= 109) // 109 is Electron v21 and up
32- if (length > zero_copy_threshold) {
33- /* Reuse existing buffer for external storage. This avoids copying but
34- does include an overhead in having to call a finalizer when the
35- buffer is GC'ed. For very small messages it is faster to copy. */
36- moved = true ;
30+ if (noElectronMemoryCage) {
31+ static auto constexpr zero_copy_threshold = 1 << 7 ;
32+ if (length > zero_copy_threshold) {
33+ /* Reuse existing buffer for external storage. This avoids copying but
34+ does include an overhead in having to call a finalizer when the
35+ buffer is GC'ed. For very small messages it is faster to copy. */
36+ moved = true ;
3737
38- /* Put appropriate GC pressure according to the size of the buffer. */
39- Napi::MemoryManagement::AdjustExternalMemory (env, length);
38+ /* Put appropriate GC pressure according to the size of the buffer. */
39+ Napi::MemoryManagement::AdjustExternalMemory (env, length);
4040
41- auto release = [](const Napi::Env& env, uint8_t *, Reference* ref) {
42- ptrdiff_t length = zmq_msg_size (*ref);
43- Napi::MemoryManagement::AdjustExternalMemory (env, -length);
44- delete ref;
45- };
41+ auto release = [](const Napi::Env& env, uint8_t *, Reference* ref) {
42+ ptrdiff_t length = zmq_msg_size (*ref);
43+ Napi::MemoryManagement::AdjustExternalMemory (env, -length);
44+ delete ref;
45+ };
4646
47- return Napi::Buffer<uint8_t >::New (env, data, length, release, ref);
47+ return Napi::Buffer<uint8_t >::New (env, data, length, release, ref);
48+ }
4849 }
49- #endif
5050
5151 if (length > 0 ) {
5252 return Napi::Buffer<uint8_t >::Copy (env, data, length);
0 commit comments