1
1
/* Copyright (c) 2017-2019 Rolf Timmermans */
2
2
#include " incoming_msg.h"
3
3
4
+ #include " util/electron_helper.h"
4
5
#include " util/error.h"
5
6
6
7
namespace zmq {
@@ -14,39 +15,38 @@ IncomingMsg::~IncomingMsg() {
14
15
}
15
16
16
17
Napi::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
+ }
23
26
}
24
-
25
- static auto constexpr zero_copy_threshold = 1 << 7 ;
26
- #endif
27
-
28
27
auto data = reinterpret_cast <uint8_t *>(zmq_msg_data (*ref));
29
28
auto length = zmq_msg_size (*ref);
30
29
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 ;
37
37
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);
40
40
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
+ };
46
46
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
+ }
48
49
}
49
- #endif
50
50
51
51
if (length > 0 ) {
52
52
return Napi::Buffer<uint8_t >::Copy (env, data, length);
0 commit comments