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,37 @@ 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
+ if (!hasElectronMemoryCage (env)) {
19
+ if (moved) {
20
+ /* If ownership has been transferred, do not attempt to read the buffer
21
+ again in any case. This should not happen of course. */
22
+ ErrnoException (env, EINVAL).ThrowAsJavaScriptException ();
23
+ return env.Undefined ();
24
+ }
23
25
}
24
-
25
- static auto constexpr zero_copy_threshold = 1 << 7 ;
26
- #endif
27
-
28
26
auto data = reinterpret_cast <uint8_t *>(zmq_msg_data (*ref));
29
27
auto length = zmq_msg_size (*ref);
30
28
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 ;
29
+ if (!hasElectronMemoryCage (env)) {
30
+ static auto constexpr zero_copy_threshold = 1 << 7 ;
31
+ if (length > zero_copy_threshold) {
32
+ /* Reuse existing buffer for external storage. This avoids copying but
33
+ does include an overhead in having to call a finalizer when the
34
+ buffer is GC'ed. For very small messages it is faster to copy. */
35
+ moved = true ;
37
36
38
- /* Put appropriate GC pressure according to the size of the buffer. */
39
- Napi::MemoryManagement::AdjustExternalMemory (env, length);
37
+ /* Put appropriate GC pressure according to the size of the buffer. */
38
+ Napi::MemoryManagement::AdjustExternalMemory (env, length);
40
39
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
- };
40
+ auto release = [](const Napi::Env& env, uint8_t *, Reference* ref) {
41
+ ptrdiff_t length = zmq_msg_size (*ref);
42
+ Napi::MemoryManagement::AdjustExternalMemory (env, -length);
43
+ delete ref;
44
+ };
46
45
47
- return Napi::Buffer<uint8_t >::New (env, data, length, release, ref);
46
+ return Napi::Buffer<uint8_t >::New (env, data, length, release, ref);
47
+ }
48
48
}
49
- #endif
50
49
51
50
if (length > 0 ) {
52
51
return Napi::Buffer<uint8_t >::Copy (env, data, length);
0 commit comments