diff options
author | Anna Henningsen <anna@addaleax.net> | 2020-06-19 21:39:23 +0300 |
---|---|---|
committer | James M Snell <jasnell@gmail.com> | 2020-06-25 18:33:29 +0300 |
commit | 88fb5a5c7933022de750745e51e5dc0996a1e2c4 (patch) | |
tree | af15b54935bd784813ccf832b6dd4c7d0b0dc596 /lib/internal/buffer.js | |
parent | a80000ec08a6a11f7e9e0291287fbc18357b2739 (diff) |
worker: add public method for marking objects as untransferable
We currently mark a number of `ArrayBuffer`s as not transferable,
including the `Buffer` pool and ones with finalizers provided
by C++ addons.
There is no good reason to assume that userland code might not
encounter similar problems, for example when doing `ArrayBuffer`
pooling similar to ours. Therefore, provide an API that lets
userland code also mark objects as not transferable.
PR-URL: https://github.com/nodejs/node/pull/33979
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
Diffstat (limited to 'lib/internal/buffer.js')
-rw-r--r-- | lib/internal/buffer.js | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/lib/internal/buffer.js b/lib/internal/buffer.js index c344a994181..be509088ada 100644 --- a/lib/internal/buffer.js +++ b/lib/internal/buffer.js @@ -27,6 +27,10 @@ const { ucs2Write, utf8Write } = internalBinding('buffer'); +const { + untransferable_object_private_symbol, + setHiddenValue, +} = internalBinding('util'); // Temporary buffers to convert numbers. const float32Array = new Float32Array(1); @@ -1007,7 +1011,16 @@ function addBufferPrototypeMethods(proto) { proto.utf8Write = utf8Write; } +// This would better be placed in internal/worker/io.js, but that doesn't work +// because Buffer needs this and that would introduce a cyclic dependency. +function markAsUntransferable(obj) { + if ((typeof obj !== 'object' && typeof obj !== 'function') || obj === null) + return; // This object is a primitive and therefore already untransferable. + setHiddenValue(obj, untransferable_object_private_symbol, true); +} + module.exports = { FastBuffer, - addBufferPrototypeMethods + addBufferPrototypeMethods, + markAsUntransferable, }; |