@@ -244,8 +244,7 @@ bool HasInstance(Local<Object> obj) {
244
244
char * Data (Local<Value> val) {
245
245
CHECK (val->IsArrayBufferView ());
246
246
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 ();
249
248
}
250
249
251
250
@@ -1156,14 +1155,13 @@ static void EncodeInto(const FunctionCallbackInfo<Value>& args) {
1156
1155
1157
1156
Local<Uint8Array> dest = args[1 ].As <Uint8Array>();
1158
1157
Local<ArrayBuffer> buf = dest->Buffer ();
1159
- char * write_result =
1160
- static_cast <char *>(buf->GetBackingStore ()->Data ()) + dest->ByteOffset ();
1158
+ char * write_result = static_cast <char *>(buf->Data ()) + dest->ByteOffset ();
1161
1159
size_t dest_length = dest->ByteLength ();
1162
1160
1163
1161
// results = [ read, written ]
1164
1162
Local<Uint32Array> result_arr = args[2 ].As <Uint32Array>();
1165
1163
uint32_t * results = reinterpret_cast <uint32_t *>(
1166
- static_cast <char *>(result_arr->Buffer ()->GetBackingStore ()-> Data ()) +
1164
+ static_cast <char *>(result_arr->Buffer ()->Data ()) +
1167
1165
result_arr->ByteOffset ());
1168
1166
1169
1167
int nchars;
@@ -1227,6 +1225,27 @@ void DetachArrayBuffer(const FunctionCallbackInfo<Value>& args) {
1227
1225
}
1228
1226
}
1229
1227
1228
+ namespace {
1229
+
1230
+ std::pair<void *, size_t > DecomposeBufferToParts (Local<Value> buffer) {
1231
+ void * pointer;
1232
+ size_t byte_length;
1233
+ if (buffer->IsArrayBuffer ()) {
1234
+ Local<ArrayBuffer> ab = buffer.As <ArrayBuffer>();
1235
+ pointer = ab->Data ();
1236
+ byte_length = ab->ByteLength ();
1237
+ } else if (buffer->IsSharedArrayBuffer ()) {
1238
+ Local<ArrayBuffer> ab = buffer.As <ArrayBuffer>();
1239
+ pointer = ab->Data ();
1240
+ byte_length = ab->ByteLength ();
1241
+ } else {
1242
+ CHECK (false ); // Caller must validate.
1243
+ }
1244
+ return {pointer, byte_length};
1245
+ }
1246
+
1247
+ }
1248
+
1230
1249
void CopyArrayBuffer (const FunctionCallbackInfo<Value>& args) {
1231
1250
// args[0] == Destination ArrayBuffer
1232
1251
// args[1] == Destination ArrayBuffer Offset
@@ -1240,32 +1259,23 @@ void CopyArrayBuffer(const FunctionCallbackInfo<Value>& args) {
1240
1259
CHECK (args[3 ]->IsUint32 ());
1241
1260
CHECK (args[4 ]->IsUint32 ());
1242
1261
1243
- std::shared_ptr<BackingStore> destination;
1244
- std::shared_ptr<BackingStore> source;
1245
-
1246
- if (args[0 ]->IsArrayBuffer ()) {
1247
- destination = args[0 ].As <ArrayBuffer>()->GetBackingStore ();
1248
- } else if (args[0 ]->IsSharedArrayBuffer ()) {
1249
- destination = args[0 ].As <SharedArrayBuffer>()->GetBackingStore ();
1250
- }
1262
+ void * destination;
1263
+ size_t destination_byte_length;
1264
+ std::tie (destination, destination_byte_length) = DecomposeBufferToParts (args[0 ]);
1251
1265
1252
- if (args[2 ]->IsArrayBuffer ()) {
1253
- source = args[2 ].As <ArrayBuffer>()->GetBackingStore ();
1254
- } else if (args[0 ]->IsSharedArrayBuffer ()) {
1255
- source = args[2 ].As <SharedArrayBuffer>()->GetBackingStore ();
1256
- }
1266
+ void * source;
1267
+ size_t source_byte_length;
1268
+ std::tie (source, source_byte_length) = DecomposeBufferToParts (args[2 ]);
1257
1269
1258
1270
uint32_t destination_offset = args[1 ].As <Uint32>()->Value ();
1259
1271
uint32_t source_offset = args[3 ].As <Uint32>()->Value ();
1260
1272
size_t bytes_to_copy = args[4 ].As <Uint32>()->Value ();
1261
1273
1262
- CHECK_GE (destination-> ByteLength () - destination_offset, bytes_to_copy);
1263
- CHECK_GE (source-> ByteLength () - source_offset, bytes_to_copy);
1274
+ CHECK_GE (destination_byte_length - destination_offset, bytes_to_copy);
1275
+ CHECK_GE (source_byte_length - source_offset, bytes_to_copy);
1264
1276
1265
- uint8_t * dest =
1266
- static_cast <uint8_t *>(destination->Data ()) + destination_offset;
1267
- uint8_t * src =
1268
- static_cast <uint8_t *>(source->Data ()) + source_offset;
1277
+ uint8_t * dest = static_cast <uint8_t *>(destination) + destination_offset;
1278
+ uint8_t * src = static_cast <uint8_t *>(source) + source_offset;
1269
1279
memcpy (dest, src, bytes_to_copy);
1270
1280
}
1271
1281
0 commit comments