Skip to content

Commit a2022e5

Browse files
kvakilruyadorno
authored andcommitted
src: remove unowned usages of GetBackingStore
This removes all usages of GetBackingStore without any entries in the `CODEOWNERS` file. For the most part this is a pretty straightforward review; except `SPREAD_BUFFER_ARG` and the changes to `CopyArrayBuffer`. See the linked issue for an explanation. Refs: #32226 Refs: #43921 PR-URL: #44080 Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 8e1b7e2 commit a2022e5

8 files changed

+53
-50
lines changed

src/aliased_buffer.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class AliasedBufferBase {
5050
// allocate v8 ArrayBuffer
5151
v8::Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New(
5252
isolate_, size_in_bytes);
53-
buffer_ = static_cast<NativeT*>(ab->GetBackingStore()->Data());
53+
buffer_ = static_cast<NativeT*>(ab->Data());
5454

5555
// allocate v8 TypedArray
5656
v8::Local<V8T> js_array = V8T::New(ab, byte_offset_, count);
@@ -119,8 +119,7 @@ class AliasedBufferBase {
119119
// be removed when we expand the snapshot support.
120120
DCHECK_EQ(count_, arr->Length());
121121
DCHECK_EQ(byte_offset_, arr->ByteOffset());
122-
uint8_t* raw =
123-
static_cast<uint8_t*>(arr->Buffer()->GetBackingStore()->Data());
122+
uint8_t* raw = static_cast<uint8_t*>(arr->Buffer()->Data());
124123
buffer_ = reinterpret_cast<NativeT*>(raw + byte_offset_);
125124
js_array_.Reset(isolate_, arr);
126125
index_ = nullptr;
@@ -278,7 +277,7 @@ class AliasedBufferBase {
278277
isolate_, new_size_in_bytes);
279278

280279
// allocate new native buffer
281-
NativeT* new_buffer = static_cast<NativeT*>(ab->GetBackingStore()->Data());
280+
NativeT* new_buffer = static_cast<NativeT*>(ab->Data());
282281
// copy old content
283282
memcpy(new_buffer, buffer_, old_size_in_bytes);
284283

src/node_buffer.cc

+35-24
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,7 @@ bool HasInstance(Local<Object> obj) {
244244
char* 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+
12311250
void 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

src/node_os.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ static void GetLoadAvg(const FunctionCallbackInfo<Value>& args) {
161161
Local<Float64Array> array = args[0].As<Float64Array>();
162162
CHECK_EQ(array->Length(), 3);
163163
Local<ArrayBuffer> ab = array->Buffer();
164-
double* loadavg = static_cast<double*>(ab->GetBackingStore()->Data());
164+
double* loadavg = static_cast<double*>(ab->Data());
165165
uv_loadavg(loadavg);
166166
}
167167

src/node_process_methods.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ static void CPUUsage(const FunctionCallbackInfo<Value>& args) {
116116

117117
// Get the double array pointer from the Float64Array argument.
118118
Local<ArrayBuffer> ab = get_fields_array_buffer(args, 0, 2);
119-
double* fields = static_cast<double*>(ab->GetBackingStore()->Data());
119+
double* fields = static_cast<double*>(ab->Data());
120120

121121
// Set the Float64Array elements to be user / system values in microseconds.
122122
fields[0] = MICROS_PER_SEC * rusage.ru_utime.tv_sec + rusage.ru_utime.tv_usec;
@@ -189,7 +189,7 @@ static void MemoryUsage(const FunctionCallbackInfo<Value>& args) {
189189

190190
// Get the double array pointer from the Float64Array argument.
191191
Local<ArrayBuffer> ab = get_fields_array_buffer(args, 0, 5);
192-
double* fields = static_cast<double*>(ab->GetBackingStore()->Data());
192+
double* fields = static_cast<double*>(ab->Data());
193193

194194
size_t rss;
195195
int err = uv_resident_set_memory(&rss);
@@ -311,7 +311,7 @@ static void ResourceUsage(const FunctionCallbackInfo<Value>& args) {
311311
return env->ThrowUVException(err, "uv_getrusage");
312312

313313
Local<ArrayBuffer> ab = get_fields_array_buffer(args, 0, 16);
314-
double* fields = static_cast<double*>(ab->GetBackingStore()->Data());
314+
double* fields = static_cast<double*>(ab->Data());
315315

316316
fields[0] = MICROS_PER_SEC * rusage.ru_utime.tv_sec + rusage.ru_utime.tv_usec;
317317
fields[1] = MICROS_PER_SEC * rusage.ru_stime.tv_sec + rusage.ru_stime.tv_usec;

src/node_worker.cc

+1-3
Original file line numberDiff line numberDiff line change
@@ -710,9 +710,7 @@ void Worker::GetResourceLimits(const FunctionCallbackInfo<Value>& args) {
710710
Local<Float64Array> Worker::GetResourceLimits(Isolate* isolate) const {
711711
Local<ArrayBuffer> ab = ArrayBuffer::New(isolate, sizeof(resource_limits_));
712712

713-
memcpy(ab->GetBackingStore()->Data(),
714-
resource_limits_,
715-
sizeof(resource_limits_));
713+
memcpy(ab->Data(), resource_limits_, sizeof(resource_limits_));
716714
return Float64Array::New(ab, 0, kTotalResourceLimitCount);
717715
}
718716

src/node_zlib.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -606,8 +606,7 @@ class ZlibStream final : public CompressionStream<ZlibContext> {
606606
CHECK(args[4]->IsUint32Array());
607607
Local<Uint32Array> array = args[4].As<Uint32Array>();
608608
Local<ArrayBuffer> ab = array->Buffer();
609-
uint32_t* write_result = static_cast<uint32_t*>(
610-
ab->GetBackingStore()->Data());
609+
uint32_t* write_result = static_cast<uint32_t*>(ab->Data());
611610

612611
CHECK(args[5]->IsFunction());
613612
Local<Function> write_js_callback = args[5].As<Function>();

src/util-inl.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -537,8 +537,7 @@ void ArrayBufferViewContents<T, S>::Read(v8::Local<v8::ArrayBufferView> abv) {
537537
static_assert(sizeof(T) == 1, "Only supports one-byte data at the moment");
538538
length_ = abv->ByteLength();
539539
if (length_ > sizeof(stack_storage_) || abv->HasBuffer()) {
540-
data_ = static_cast<T*>(abv->Buffer()->GetBackingStore()->Data()) +
541-
abv->ByteOffset();
540+
data_ = static_cast<T*>(abv->Buffer()->Data()) + abv->ByteOffset();
542541
} else {
543542
abv->CopyContents(stack_storage_, sizeof(stack_storage_));
544543
data_ = stack_storage_;

src/util.h

+8-11
Original file line numberDiff line numberDiff line change
@@ -535,17 +535,14 @@ class BufferValue : public MaybeStackBuffer<char> {
535535
inline std::string ToString() const { return std::string(out(), length()); }
536536
};
537537

538-
#define SPREAD_BUFFER_ARG(val, name) \
539-
CHECK((val)->IsArrayBufferView()); \
540-
v8::Local<v8::ArrayBufferView> name = (val).As<v8::ArrayBufferView>(); \
541-
std::shared_ptr<v8::BackingStore> name##_bs = \
542-
name->Buffer()->GetBackingStore(); \
543-
const size_t name##_offset = name->ByteOffset(); \
544-
const size_t name##_length = name->ByteLength(); \
545-
char* const name##_data = \
546-
static_cast<char*>(name##_bs->Data()) + name##_offset; \
547-
if (name##_length > 0) \
548-
CHECK_NE(name##_data, nullptr);
538+
#define SPREAD_BUFFER_ARG(val, name) \
539+
CHECK((val)->IsArrayBufferView()); \
540+
v8::Local<v8::ArrayBufferView> name = (val).As<v8::ArrayBufferView>(); \
541+
const size_t name##_offset = name->ByteOffset(); \
542+
const size_t name##_length = name->ByteLength(); \
543+
char* const name##_data = \
544+
static_cast<char*>(name->Buffer()->Data()) + name##_offset; \
545+
if (name##_length > 0) CHECK_NE(name##_data, nullptr);
549546

550547
// Use this when a variable or parameter is unused in order to explicitly
551548
// silence a compiler warning about that.

0 commit comments

Comments
 (0)