Skip to content

Commit 554fdf2

Browse files
committed
src: remove many uses of GetBackingStore
This replaces many uses of `GetBackingStore()->Data()` with the newly backported `Data()`. I only replaced the "obvious" usages here, where I could easily convince myself that the lifetime was safe. The less obvious changes will come in a separate PR. Refs: nodejs#32226 Refs: nodejs#43921
1 parent 0359430 commit 554fdf2

14 files changed

+61
-61
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/js_native_api_v8.cc

+7-10
Original file line numberDiff line numberDiff line change
@@ -2780,7 +2780,7 @@ napi_status NAPI_CDECL napi_create_arraybuffer(napi_env env,
27802780
// Optionally return a pointer to the buffer's data, to avoid another call to
27812781
// retrieve it.
27822782
if (data != nullptr) {
2783-
*data = buffer->GetBackingStore()->Data();
2783+
*data = buffer->Data();
27842784
}
27852785

27862786
*result = v8impl::JsValueFromV8LocalValue(buffer);
@@ -2814,15 +2814,14 @@ napi_status NAPI_CDECL napi_get_arraybuffer_info(napi_env env,
28142814
v8::Local<v8::Value> value = v8impl::V8LocalValueFromJsValue(arraybuffer);
28152815
RETURN_STATUS_IF_FALSE(env, value->IsArrayBuffer(), napi_invalid_arg);
28162816

2817-
std::shared_ptr<v8::BackingStore> backing_store =
2818-
value.As<v8::ArrayBuffer>()->GetBackingStore();
2817+
v8::Local<v8::ArrayBuffer> ab = value.As<v8::ArrayBuffer>();
28192818

28202819
if (data != nullptr) {
2821-
*data = backing_store->Data();
2820+
*data = ab->Data();
28222821
}
28232822

28242823
if (byte_length != nullptr) {
2825-
*byte_length = backing_store->ByteLength();
2824+
*byte_length = ab->ByteLength();
28262825
}
28272826

28282827
return napi_clear_last_error(env);
@@ -2963,8 +2962,7 @@ napi_status NAPI_CDECL napi_get_typedarray_info(napi_env env,
29632962
}
29642963

29652964
if (data != nullptr) {
2966-
*data = static_cast<uint8_t*>(buffer->GetBackingStore()->Data()) +
2967-
array->ByteOffset();
2965+
*data = static_cast<uint8_t*>(buffer->Data()) + array->ByteOffset();
29682966
}
29692967

29702968
if (arraybuffer != nullptr) {
@@ -3044,8 +3042,7 @@ napi_status NAPI_CDECL napi_get_dataview_info(napi_env env,
30443042
}
30453043

30463044
if (data != nullptr) {
3047-
*data = static_cast<uint8_t*>(buffer->GetBackingStore()->Data()) +
3048-
array->ByteOffset();
3045+
*data = static_cast<uint8_t*>(buffer->Data()) + array->ByteOffset();
30493046
}
30503047

30513048
if (arraybuffer != nullptr) {
@@ -3256,7 +3253,7 @@ napi_status NAPI_CDECL napi_is_detached_arraybuffer(napi_env env,
32563253
v8::Local<v8::Value> value = v8impl::V8LocalValueFromJsValue(arraybuffer);
32573254

32583255
*result = value->IsArrayBuffer() &&
3259-
value.As<v8::ArrayBuffer>()->GetBackingStore()->Data() == nullptr;
3256+
value.As<v8::ArrayBuffer>()->Data() == nullptr;
32603257

32613258
return napi_clear_last_error(env);
32623259
}

src/module_wrap.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,7 @@ void ModuleWrap::New(const FunctionCallbackInfo<Value>& args) {
178178
if (!args[5]->IsUndefined()) {
179179
CHECK(args[5]->IsArrayBufferView());
180180
Local<ArrayBufferView> cached_data_buf = args[5].As<ArrayBufferView>();
181-
uint8_t* data = static_cast<uint8_t*>(
182-
cached_data_buf->Buffer()->GetBackingStore()->Data());
181+
uint8_t* data = static_cast<uint8_t*>(cached_data_buf->Buffer()->Data());
183182
cached_data =
184183
new ScriptCompiler::CachedData(data + cached_data_buf->ByteOffset(),
185184
cached_data_buf->ByteLength());

src/node.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ static void AtomicsWaitCallback(Isolate::AtomicsWaitEvent event,
269269
", %.f) %s\n",
270270
static_cast<int>(uv_os_getpid()),
271271
env->thread_id(),
272-
array_buffer->GetBackingStore()->Data(),
272+
array_buffer->Data(),
273273
offset_in_bytes,
274274
value,
275275
timeout_in_ms,

src/node_buffer.cc

+34-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

@@ -1156,14 +1155,13 @@ static void EncodeInto(const FunctionCallbackInfo<Value>& args) {
11561155

11571156
Local<Uint8Array> dest = args[1].As<Uint8Array>();
11581157
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();
11611159
size_t dest_length = dest->ByteLength();
11621160

11631161
// results = [ read, written ]
11641162
Local<Uint32Array> result_arr = args[2].As<Uint32Array>();
11651163
uint32_t* results = reinterpret_cast<uint32_t*>(
1166-
static_cast<char*>(result_arr->Buffer()->GetBackingStore()->Data()) +
1164+
static_cast<char*>(result_arr->Buffer()->Data()) +
11671165
result_arr->ByteOffset());
11681166

11691167
int nchars;
@@ -1227,6 +1225,27 @@ void DetachArrayBuffer(const FunctionCallbackInfo<Value>& args) {
12271225
}
12281226
}
12291227

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+
12301249
void CopyArrayBuffer(const FunctionCallbackInfo<Value>& args) {
12311250
// args[0] == Destination ArrayBuffer
12321251
// args[1] == Destination ArrayBuffer Offset
@@ -1240,32 +1259,23 @@ void CopyArrayBuffer(const FunctionCallbackInfo<Value>& args) {
12401259
CHECK(args[3]->IsUint32());
12411260
CHECK(args[4]->IsUint32());
12421261

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]);
12511265

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]);
12571269

12581270
uint32_t destination_offset = args[1].As<Uint32>()->Value();
12591271
uint32_t source_offset = args[3].As<Uint32>()->Value();
12601272
size_t bytes_to_copy = args[4].As<Uint32>()->Value();
12611273

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);
12641276

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;
12691279
memcpy(dest, src, bytes_to_copy);
12701280
}
12711281

src/node_contextify.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
743743
ScriptCompiler::CachedData* cached_data = nullptr;
744744
if (!cached_data_buf.IsEmpty()) {
745745
uint8_t* data = static_cast<uint8_t*>(
746-
cached_data_buf->Buffer()->GetBackingStore()->Data());
746+
cached_data_buf->Buffer()->Data());
747747
cached_data = new ScriptCompiler::CachedData(
748748
data + cached_data_buf->ByteOffset(), cached_data_buf->ByteLength());
749749
}
@@ -1065,7 +1065,7 @@ void ContextifyContext::CompileFunction(
10651065
ScriptCompiler::CachedData* cached_data = nullptr;
10661066
if (!cached_data_buf.IsEmpty()) {
10671067
uint8_t* data = static_cast<uint8_t*>(
1068-
cached_data_buf->Buffer()->GetBackingStore()->Data());
1068+
cached_data_buf->Buffer()->Data());
10691069
cached_data = new ScriptCompiler::CachedData(
10701070
data + cached_data_buf->ByteOffset(), cached_data_buf->ByteLength());
10711071
}

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_wasi.cc

+3-4
Original file line numberDiff line numberDiff line change
@@ -1654,10 +1654,9 @@ void WASI::_SetMemory(const FunctionCallbackInfo<Value>& args) {
16541654

16551655
uvwasi_errno_t WASI::backingStore(char** store, size_t* byte_length) {
16561656
Local<WasmMemoryObject> memory = PersistentToLocal::Strong(this->memory_);
1657-
std::shared_ptr<BackingStore> backing_store =
1658-
memory->Buffer()->GetBackingStore();
1659-
*byte_length = backing_store->ByteLength();
1660-
*store = static_cast<char*>(backing_store->Data());
1657+
Local<v8::ArrayBuffer> ab = memory->Buffer();
1658+
*byte_length = ab->ByteLength();
1659+
*store = static_cast<char*>(ab->Data());
16611660
CHECK_NOT_NULL(*store);
16621661
return UVWASI_ESUCCESS;
16631662
}

src/node_wasm_web_api.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,12 @@ void WasmStreamingObject::Push(const FunctionCallbackInfo<Value>& args) {
105105

106106
if (LIKELY(chunk->IsArrayBufferView())) {
107107
Local<ArrayBufferView> view = chunk.As<ArrayBufferView>();
108-
bytes = view->Buffer()->GetBackingStore()->Data();
108+
bytes = view->Buffer()->Data();
109109
offset = view->ByteOffset();
110110
size = view->ByteLength();
111111
} else if (LIKELY(chunk->IsArrayBuffer())) {
112112
Local<ArrayBuffer> buffer = chunk.As<ArrayBuffer>();
113-
bytes = buffer->GetBackingStore()->Data();
113+
bytes = buffer->Data();
114114
offset = 0;
115115
size = buffer->ByteLength();
116116
} else {

src/node_worker.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -710,7 +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(),
713+
memcpy(ab->Data(),
714714
resource_limits_,
715715
sizeof(resource_limits_));
716716
return Float64Array::New(ab, 0, kTotalResourceLimitCount);

src/node_zlib.cc

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

611610
CHECK(args[5]->IsFunction());
612611
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

+1-3
Original file line numberDiff line numberDiff line change
@@ -538,12 +538,10 @@ class BufferValue : public MaybeStackBuffer<char> {
538538
#define SPREAD_BUFFER_ARG(val, name) \
539539
CHECK((val)->IsArrayBufferView()); \
540540
v8::Local<v8::ArrayBufferView> name = (val).As<v8::ArrayBufferView>(); \
541-
std::shared_ptr<v8::BackingStore> name##_bs = \
542-
name->Buffer()->GetBackingStore(); \
543541
const size_t name##_offset = name->ByteOffset(); \
544542
const size_t name##_length = name->ByteLength(); \
545543
char* const name##_data = \
546-
static_cast<char*>(name##_bs->Data()) + name##_offset; \
544+
static_cast<char*>(name->Buffer()->Data()) + name##_offset; \
547545
if (name##_length > 0) \
548546
CHECK_NE(name##_data, nullptr);
549547

0 commit comments

Comments
 (0)