@@ -1117,6 +1117,34 @@ void SetBufferPrototype(const FunctionCallbackInfo<Value>& args) {
11171117 env->set_buffer_prototype_object (proto);
11181118}
11191119
1120+ void GetZeroFillToggle (const FunctionCallbackInfo<Value>& args) {
1121+ Environment* env = Environment::GetCurrent (args);
1122+ NodeArrayBufferAllocator* allocator = env->isolate_data ()->node_allocator ();
1123+ Local<ArrayBuffer> ab;
1124+ // It can be a nullptr when running inside an isolate where we
1125+ // do not own the ArrayBuffer allocator.
1126+ if (allocator == nullptr ) {
1127+ // Create a dummy Uint32Array - the JS land can only toggle the C++ land
1128+ // setting when the allocator uses our toggle. With this the toggle in JS
1129+ // land results in no-ops.
1130+ ab = ArrayBuffer::New (env->isolate (), sizeof (uint32_t ));
1131+ } else {
1132+ uint32_t * zero_fill_field = allocator->zero_fill_field ();
1133+ std::unique_ptr<BackingStore> backing =
1134+ ArrayBuffer::NewBackingStore (zero_fill_field,
1135+ sizeof (*zero_fill_field),
1136+ [](void *, size_t , void *) {},
1137+ nullptr );
1138+ ab = ArrayBuffer::New (env->isolate (), std::move (backing));
1139+ }
1140+
1141+ ab->SetPrivate (
1142+ env->context (),
1143+ env->untransferable_object_private_symbol (),
1144+ True (env->isolate ())).Check ();
1145+
1146+ args.GetReturnValue ().Set (Uint32Array::New (ab, 0 , 1 ));
1147+ }
11201148
11211149void Initialize (Local<Object> target,
11221150 Local<Value> unused,
@@ -1165,28 +1193,7 @@ void Initialize(Local<Object> target,
11651193 env->SetMethod (target, " ucs2Write" , StringWrite<UCS2>);
11661194 env->SetMethod (target, " utf8Write" , StringWrite<UTF8>);
11671195
1168- // It can be a nullptr when running inside an isolate where we
1169- // do not own the ArrayBuffer allocator.
1170- if (NodeArrayBufferAllocator* allocator =
1171- env->isolate_data ()->node_allocator ()) {
1172- uint32_t * zero_fill_field = allocator->zero_fill_field ();
1173- std::unique_ptr<BackingStore> backing =
1174- ArrayBuffer::NewBackingStore (zero_fill_field,
1175- sizeof (*zero_fill_field),
1176- [](void *, size_t , void *){},
1177- nullptr );
1178- Local<ArrayBuffer> array_buffer =
1179- ArrayBuffer::New (env->isolate (), std::move (backing));
1180- array_buffer->SetPrivate (
1181- env->context (),
1182- env->untransferable_object_private_symbol (),
1183- True (env->isolate ())).Check ();
1184- CHECK (target
1185- ->Set (env->context (),
1186- FIXED_ONE_BYTE_STRING (env->isolate (), " zeroFill" ),
1187- Uint32Array::New (array_buffer, 0 , 1 ))
1188- .FromJust ());
1189- }
1196+ env->SetMethod (target, " getZeroFillToggle" , GetZeroFillToggle);
11901197}
11911198
11921199} // anonymous namespace
0 commit comments