@@ -244,8 +244,7 @@ bool HasInstance(Local<Object> obj) {
244244char * Data (Local<Value> val) {
245245 CHECK (val->IsArrayBufferView ());
246246 Local<ArrayBufferView> ui = val.As <ArrayBufferView>();
247- return static_cast <char *>(ui->Buffer ()->GetBackingStore ()->Data ()) +
248- ui->ByteOffset ();
247+ return static_cast <char *>(ui->Buffer ()->Data ()) + ui->ByteOffset ();
249248}
250249
251250
@@ -1157,14 +1156,13 @@ static void EncodeInto(const FunctionCallbackInfo<Value>& args) {
11571156
11581157 Local<Uint8Array> dest = args[1 ].As <Uint8Array>();
11591158 Local<ArrayBuffer> buf = dest->Buffer ();
1160- char * write_result =
1161- static_cast <char *>(buf->GetBackingStore ()->Data ()) + dest->ByteOffset ();
1159+ char * write_result = static_cast <char *>(buf->Data ()) + dest->ByteOffset ();
11621160 size_t dest_length = dest->ByteLength ();
11631161
11641162 // results = [ read, written ]
11651163 Local<Uint32Array> result_arr = args[2 ].As <Uint32Array>();
11661164 uint32_t * results = reinterpret_cast <uint32_t *>(
1167- static_cast <char *>(result_arr->Buffer ()->GetBackingStore ()-> Data ()) +
1165+ static_cast <char *>(result_arr->Buffer ()->Data ()) +
11681166 result_arr->ByteOffset ());
11691167
11701168 int nchars;
@@ -1228,6 +1226,27 @@ void DetachArrayBuffer(const FunctionCallbackInfo<Value>& args) {
12281226 }
12291227}
12301228
1229+ namespace {
1230+
1231+ std::pair<void *, size_t > DecomposeBufferToParts (Local<Value> buffer) {
1232+ void * pointer;
1233+ size_t byte_length;
1234+ if (buffer->IsArrayBuffer ()) {
1235+ Local<ArrayBuffer> ab = buffer.As <ArrayBuffer>();
1236+ pointer = ab->Data ();
1237+ byte_length = ab->ByteLength ();
1238+ } else if (buffer->IsSharedArrayBuffer ()) {
1239+ Local<SharedArrayBuffer> ab = buffer.As <SharedArrayBuffer>();
1240+ pointer = ab->Data ();
1241+ byte_length = ab->ByteLength ();
1242+ } else {
1243+ UNREACHABLE (); // Caller must validate.
1244+ }
1245+ return {pointer, byte_length};
1246+ }
1247+
1248+ } // namespace
1249+
12311250void CopyArrayBuffer (const FunctionCallbackInfo<Value>& args) {
12321251 // args[0] == Destination ArrayBuffer
12331252 // args[1] == Destination ArrayBuffer Offset
@@ -1241,32 +1260,24 @@ void CopyArrayBuffer(const FunctionCallbackInfo<Value>& args) {
12411260 CHECK (args[3 ]->IsUint32 ());
12421261 CHECK (args[4 ]->IsUint32 ());
12431262
1244- std::shared_ptr<BackingStore> destination;
1245- std::shared_ptr<BackingStore> source;
1263+ void * destination;
1264+ size_t destination_byte_length;
1265+ std::tie (destination, destination_byte_length) =
1266+ DecomposeBufferToParts (args[0 ]);
12461267
1247- if (args[0 ]->IsArrayBuffer ()) {
1248- destination = args[0 ].As <ArrayBuffer>()->GetBackingStore ();
1249- } else if (args[0 ]->IsSharedArrayBuffer ()) {
1250- destination = args[0 ].As <SharedArrayBuffer>()->GetBackingStore ();
1251- }
1252-
1253- if (args[2 ]->IsArrayBuffer ()) {
1254- source = args[2 ].As <ArrayBuffer>()->GetBackingStore ();
1255- } else if (args[0 ]->IsSharedArrayBuffer ()) {
1256- source = args[2 ].As <SharedArrayBuffer>()->GetBackingStore ();
1257- }
1268+ void * source;
1269+ size_t source_byte_length;
1270+ std::tie (source, source_byte_length) = DecomposeBufferToParts (args[2 ]);
12581271
12591272 uint32_t destination_offset = args[1 ].As <Uint32>()->Value ();
12601273 uint32_t source_offset = args[3 ].As <Uint32>()->Value ();
12611274 size_t bytes_to_copy = args[4 ].As <Uint32>()->Value ();
12621275
1263- CHECK_GE (destination-> ByteLength () - destination_offset, bytes_to_copy);
1264- CHECK_GE (source-> ByteLength () - source_offset, bytes_to_copy);
1276+ CHECK_GE (destination_byte_length - destination_offset, bytes_to_copy);
1277+ CHECK_GE (source_byte_length - source_offset, bytes_to_copy);
12651278
1266- uint8_t * dest =
1267- static_cast <uint8_t *>(destination->Data ()) + destination_offset;
1268- uint8_t * src =
1269- static_cast <uint8_t *>(source->Data ()) + source_offset;
1279+ uint8_t * dest = static_cast <uint8_t *>(destination) + destination_offset;
1280+ uint8_t * src = static_cast <uint8_t *>(source) + source_offset;
12701281 memcpy (dest, src, bytes_to_copy);
12711282}
12721283
0 commit comments