mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
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>
This commit is contained in:
1 parent
ced68bfa3d
commit
4875a511ea
9 files changed
+118
-21
No files matched your search
@@ -146,12 +146,12 @@ constexpr size_t kFsStatsBufferLength =
|
||||
// "node:" prefix to avoid name clashes with third-party code.
|
||||
#define PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V) \
|
||||
V(alpn_buffer_private_symbol, "node:alpnBuffer") \
|
||||
V(arraybuffer_untransferable_private_symbol, "node:untransferableBuffer") \
|
||||
V(arrow_message_private_symbol, "node:arrowMessage") \
|
||||
V(contextify_context_private_symbol, "node:contextify:context") \
|
||||
V(contextify_global_private_symbol, "node:contextify:global") \
|
||||
V(decorated_private_symbol, "node:decorated") \
|
||||
V(napi_wrapper, "node:napi:wrapper") \
|
||||
V(untransferable_object_private_symbol, "node:untransferableObject") \
|
||||
|
||||
// Symbols are per-isolate primitives but Environment proxies them
|
||||
// for the sake of convenience.
|
||||
|
||||
+1
-1
@@ -29,7 +29,7 @@ struct node_napi_env__ : public napi_env__ {
|
||||
v8::Local<v8::ArrayBuffer> ab) const override {
|
||||
return ab->SetPrivate(
|
||||
context(),
|
||||
node_env()->arraybuffer_untransferable_private_symbol(),
|
||||
node_env()->untransferable_object_private_symbol(),
|
||||
v8::True(isolate));
|
||||
}
|
||||
};
|
||||
|
||||
+2
-2
@@ -419,7 +419,7 @@ MaybeLocal<Object> New(Environment* env,
|
||||
Local<ArrayBuffer> ab =
|
||||
CallbackInfo::CreateTrackedArrayBuffer(env, data, length, callback, hint);
|
||||
if (ab->SetPrivate(env->context(),
|
||||
env->arraybuffer_untransferable_private_symbol(),
|
||||
env->untransferable_object_private_symbol(),
|
||||
True(env->isolate())).IsNothing()) {
|
||||
return Local<Object>();
|
||||
}
|
||||
@@ -1179,7 +1179,7 @@ void Initialize(Local<Object> target,
|
||||
ArrayBuffer::New(env->isolate(), std::move(backing));
|
||||
array_buffer->SetPrivate(
|
||||
env->context(),
|
||||
env->arraybuffer_untransferable_private_symbol(),
|
||||
env->untransferable_object_private_symbol(),
|
||||
True(env->isolate())).Check();
|
||||
CHECK(target
|
||||
->Set(env->context(),
|
||||
|
||||
+15
-11
@@ -399,7 +399,21 @@ Maybe<bool> Message::Serialize(Environment* env,
|
||||
std::vector<Local<ArrayBuffer>> array_buffers;
|
||||
for (uint32_t i = 0; i < transfer_list_v.length(); ++i) {
|
||||
Local<Value> entry = transfer_list_v[i];
|
||||
// Currently, we support ArrayBuffers and MessagePorts.
|
||||
if (entry->IsObject()) {
|
||||
// See https://github.com/nodejs/node/pull/30339#issuecomment-552225353
|
||||
// for details.
|
||||
bool untransferable;
|
||||
if (!entry.As<Object>()->HasPrivate(
|
||||
context,
|
||||
env->untransferable_object_private_symbol())
|
||||
.To(&untransferable)) {
|
||||
return Nothing<bool>();
|
||||
}
|
||||
if (untransferable) continue;
|
||||
}
|
||||
|
||||
// Currently, we support ArrayBuffers and BaseObjects for which
|
||||
// GetTransferMode() does not return kUntransferable.
|
||||
if (entry->IsArrayBuffer()) {
|
||||
Local<ArrayBuffer> ab = entry.As<ArrayBuffer>();
|
||||
// If we cannot render the ArrayBuffer unusable in this Isolate,
|
||||
@@ -411,16 +425,6 @@ Maybe<bool> Message::Serialize(Environment* env,
|
||||
// is always going to outlive any Workers it creates, and so will its
|
||||
// allocator along with it.
|
||||
if (!ab->IsDetachable()) continue;
|
||||
// See https://github.com/nodejs/node/pull/30339#issuecomment-552225353
|
||||
// for details.
|
||||
bool untransferrable;
|
||||
if (!ab->HasPrivate(
|
||||
context,
|
||||
env->arraybuffer_untransferable_private_symbol())
|
||||
.To(&untransferrable)) {
|
||||
return Nothing<bool>();
|
||||
}
|
||||
if (untransferrable) continue;
|
||||
if (std::find(array_buffers.begin(), array_buffers.end(), ab) !=
|
||||
array_buffers.end()) {
|
||||
ThrowDataCloneException(
|
||||
|
||||
Reference in new issue
Block a user