Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions benchmark/napi/function_args/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <assert.h>

using v8::Array;
using v8::ArrayBuffer;
using v8::ArrayBufferBase;
using v8::ArrayBufferView;
using v8::BackingStore;
using v8::Context;
Expand Down Expand Up @@ -104,7 +104,7 @@ void CallWithTypedarray(const FunctionCallbackInfo<Value>& args) {
const size_t byte_length = view->ByteLength();
assert(byte_length > 0);
assert(view->HasBuffer());
Local<ArrayBuffer> buffer = view->Buffer();
Local<ArrayBufferBase> buffer = view->GetBuffer();
std::shared_ptr<BackingStore> bs = buffer->GetBackingStore();
const uint32_t* data = reinterpret_cast<uint32_t*>(
static_cast<uint8_t*>(bs->Data()) + byte_offset);
Expand Down
8 changes: 6 additions & 2 deletions src/aliased_buffer-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ inline void AliasedBufferBase<NativeT, V8T>::Deserialize(
// be removed when we expand the snapshot support.
DCHECK_EQ(count_, arr->Length());
DCHECK_EQ(byte_offset_, arr->ByteOffset());
uint8_t* raw = static_cast<uint8_t*>(arr->Buffer()->Data());
uint8_t* raw = static_cast<uint8_t*>(arr->GetBuffer()->Data());
buffer_ = reinterpret_cast<NativeT*>(raw + byte_offset_);
js_array_.Reset(isolate_, arr);
index_ = nullptr;
Expand Down Expand Up @@ -146,7 +146,11 @@ template <typename NativeT, typename V8T>
requires std::is_scalar_v<NativeT>
v8::Local<v8::ArrayBuffer> AliasedBufferBase<NativeT, V8T>::GetArrayBuffer()
const {
return GetJSArray()->Buffer();
// The JS array is always created over an ArrayBuffer allocated by
// AliasedBufferBase itself, never over a SharedArrayBuffer.
v8::Local<v8::ArrayBufferBase> buffer = GetJSArray()->GetBuffer();
DCHECK(buffer->IsArrayBuffer());
return buffer.As<v8::ArrayBuffer>();
}

template <typename NativeT, typename V8T>
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/crypto_random.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace crypto {
namespace {
bool IsImmutableBufferSource(Local<Value> buffer) {
if (buffer->IsArrayBufferView()) {
return buffer.As<ArrayBufferView>()->Buffer()->IsImmutable();
return buffer.As<ArrayBufferView>()->GetBuffer()->IsImmutable();
}
return buffer->IsArrayBuffer() && buffer.As<ArrayBuffer>()->IsImmutable();
}
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/crypto_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,7 @@ MaybeLocal<Value> ToWebCryptoJobResult(Environment* env, Local<Value> value) {
}

if (Buffer::HasInstance(value)) {
return value.As<ArrayBufferView>()->Buffer();
return value.As<ArrayBufferView>()->GetBuffer();
}

CHECK(value->IsBoolean() || (value->IsObject() && !value->IsArray() &&
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/crypto_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ class ArrayBufferOrViewContents final {
auto view = buf.As<v8::ArrayBufferView>();
offset_ = view->ByteOffset();
length_ = view->ByteLength();
data_ = view->Buffer()->Data();
data_ = view->GetBuffer()->Data();
} else if (buf->IsArrayBuffer()) {
auto ab = buf.As<v8::ArrayBuffer>();
offset_ = 0;
Expand Down
12 changes: 9 additions & 3 deletions src/dataqueue/queue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

namespace node {

using v8::ArrayBuffer;
using v8::ArrayBufferBase;
using v8::ArrayBufferView;
using v8::BackingStore;
using v8::Local;
Expand Down Expand Up @@ -1163,13 +1165,17 @@ std::unique_ptr<DataQueue::Entry> DataQueue::CreateInMemoryEntryFromView(
// the underlying buffer, something else could modify the buffer while we're
// holding the reference, which means we cannot guarantee that reads will be
// idempotent.
if (!view->Buffer()->IsDetachable()) {
Local<ArrayBufferBase> buffer = view->GetBuffer();
if (!buffer->IsDetachable()) {
return nullptr;
}
auto store = view->Buffer()->GetBackingStore();
auto store = buffer->GetBackingStore();
auto offset = view->ByteOffset();
auto length = view->ByteLength();
USE(view->Buffer()->Detach(Local<Value>()));
// A SharedArrayBuffer is never detachable, assuming this is an ArrayBuffer
// is safe.
DCHECK(buffer->IsArrayBuffer());
USE(buffer.As<ArrayBuffer>()->Detach(Local<Value>()));
return CreateInMemoryEntryFromBackingStore(std::move(store), offset, length);
}

Expand Down
5 changes: 3 additions & 2 deletions src/encoding_binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace node {
namespace encoding_binding {

using v8::ArrayBuffer;
using v8::ArrayBufferBase;
using v8::BackingStore;
using v8::BackingStoreInitializationMode;
using v8::BackingStoreOnFailureMode;
Expand Down Expand Up @@ -197,7 +198,7 @@ void BindingData::EncodeInto(const FunctionCallbackInfo<Value>& args) {
Local<String> source = args[0].As<String>();

Local<Uint8Array> dest = args[1].As<Uint8Array>();
Local<ArrayBuffer> buf = dest->Buffer();
Local<ArrayBufferBase> buf = dest->GetBuffer();

// Handle detached buffers - return {read: 0, written: 0}
if (buf->Data() == nullptr) {
Expand Down Expand Up @@ -425,7 +426,7 @@ void BindingData::DecodeUTF8(const FunctionCallbackInfo<Value>& args) {

if (args[0]->IsArrayBufferView()) {
Local<v8::ArrayBufferView> view = args[0].As<v8::ArrayBufferView>();
isShared = view->Buffer()->IsSharedArrayBuffer();
isShared = view->GetBuffer()->IsSharedArrayBuffer();
}

ArrayBufferViewContents<char> buffer(args[0]);
Expand Down
4 changes: 2 additions & 2 deletions src/ffi/data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -793,15 +793,15 @@ void GetRawPointer(const FunctionCallbackInfo<Value>& args) {
store = args[0].As<SharedArrayBuffer>()->GetBackingStore();
} else if (args[0]->IsArrayBufferView()) {
Local<ArrayBufferView> view = args[0].As<ArrayBufferView>();
if (view->Buffer()->WasDetached()) {
if (view->GetBuffer()->WasDetached()) {
THROW_ERR_INVALID_ARG_VALUE(
env, "ArrayBufferView is backed by a detached ArrayBuffer");
return;
}
// Access the store here to ensure that it exists. Small typed arrays
// may not have a store until this point and can instead be stored
// entirely in-heap.
store = view->Buffer()->GetBackingStore();
store = view->GetBuffer()->GetBackingStore();
offset = view->ByteOffset();
} else {
THROW_ERR_INVALID_ARG_TYPE(
Expand Down
4 changes: 2 additions & 2 deletions src/ffi/fast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ uintptr_t PointerFromValue(v8::Local<v8::Value> value) {
// ArrayBuffer and SharedArrayBuffer point at the start of their store.
if (value->IsArrayBufferView()) {
v8::Local<v8::ArrayBufferView> view = value.As<v8::ArrayBufferView>();
void* data = view->Buffer()->Data();
void* data = view->GetBuffer()->Data();
return reinterpret_cast<uintptr_t>(static_cast<char*>(data) +
view->ByteOffset());
}
Expand Down Expand Up @@ -259,7 +259,7 @@ extern "C" uintptr_t node_ffi_fast_buffer_data(v8::Local<v8::Value> value,
// lifetimes match the generic path.
if (value->IsArrayBufferView()) {
v8::Local<v8::ArrayBufferView> view = value.As<v8::ArrayBufferView>();
if (view->Buffer()->WasDetached()) {
if (view->GetBuffer()->WasDetached()) {
THROW_ERR_INVALID_ARG_VALUE(
isolate,
"Argument %u is an ArrayBufferView backed by a detached "
Expand Down
5 changes: 3 additions & 2 deletions src/ffi/types.cc
Original file line number Diff line number Diff line change
Expand Up @@ -717,15 +717,16 @@ Maybe<FFIArgumentCategory> ToFFIArgument(Environment* env,
// invalidating that backing store during the active FFI call is
// unsupported and dangerous.
Local<ArrayBufferView> view = arg.As<ArrayBufferView>();
if (view->Buffer()->WasDetached()) {
if (view->GetBuffer()->WasDetached()) {
THROW_ERR_INVALID_ARG_VALUE(
env,
"Argument %u is an ArrayBufferView backed by a detached "
"ArrayBuffer",
index);
return {};
}
std::shared_ptr<BackingStore> store = view->Buffer()->GetBackingStore();
std::shared_ptr<BackingStore> store =
view->GetBuffer()->GetBackingStore();

if (!store) {
THROW_ERR_INVALID_ARG_VALUE(
Expand Down
2 changes: 1 addition & 1 deletion src/heap_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ HeapProfiler::HeapSnapshotOptions GetHeapSnapshotOptions(
CHECK(options_value->IsUint8Array());
Local<Uint8Array> arr = options_value.As<Uint8Array>();
uint8_t* options =
static_cast<uint8_t*>(arr->Buffer()->Data()) + arr->ByteOffset();
static_cast<uint8_t*>(arr->GetBuffer()->Data()) + arr->ByteOffset();
HeapProfiler::HeapSnapshotOptions result;
result.snapshot_mode = options[0]
? HeapProfiler::HeapSnapshotMode::kExposeInternals
Expand Down
6 changes: 3 additions & 3 deletions src/histogram.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3122,7 +3122,7 @@ void HistogramImpl::GetQrde(const FunctionCallbackInfo<Value>& args) {
Local<Float64Array> input = args[0].As<Float64Array>();
CHECK_GE(input->Length(), 2);
CHECK_LE(input->Length(), 1001);
auto backing = input->Buffer()->GetBackingStore();
auto backing = input->GetBuffer()->GetBackingStore();
const double* data = reinterpret_cast<const double*>(
static_cast<const char*>(backing->Data()) + input->ByteOffset());
std::vector<double> probabilities(data, data + input->Length());
Expand Down Expand Up @@ -3206,7 +3206,7 @@ void HistogramImpl::DoImport(const FunctionCallbackInfo<Value>& args) {
return;
}
Local<Uint8Array> input = args[0].As<Uint8Array>();
auto backing = input->Buffer()->GetBackingStore();
auto backing = input->GetBuffer()->GetBackingStore();
const uint8_t* data =
static_cast<const uint8_t*>(backing->Data()) + input->ByteOffset();
size_t len = input->ByteLength();
Expand Down Expand Up @@ -3279,7 +3279,7 @@ void HistogramImpl::GetPercentilesAt(const FunctionCallbackInfo<Value>& args) {
CHECK(args[1]->IsFloat64Array());
Local<Float64Array> input = args[1].As<Float64Array>();
size_t length = input->Length();
auto backing = input->Buffer()->GetBackingStore();
auto backing = input->GetBuffer()->GetBackingStore();
double* percentiles = reinterpret_cast<double*>(
static_cast<char*>(backing->Data()) + input->ByteOffset());

Expand Down
12 changes: 6 additions & 6 deletions src/js_native_api_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3394,11 +3394,11 @@ napi_status NAPI_CDECL napi_get_typedarray_info(napi_env env,
*length = array->Length();
}

v8::Local<v8::ArrayBuffer> buffer;
v8::Local<v8::ArrayBufferBase> buffer;
if (data != nullptr || arraybuffer != nullptr) {
// Calling Buffer() may have the side effect of allocating the buffer,
// Calling GetBuffer() may have the side effect of allocating the buffer,
// so only do this when it’s needed.
buffer = array->Buffer();
buffer = array->GetBuffer();
}

if (data != nullptr) {
Expand Down Expand Up @@ -3483,11 +3483,11 @@ napi_status NAPI_CDECL napi_get_dataview_info(napi_env env,
*byte_length = array->ByteLength();
}

v8::Local<v8::ArrayBuffer> buffer;
v8::Local<v8::ArrayBufferBase> buffer;
if (data != nullptr || arraybuffer != nullptr) {
// Calling Buffer() may have the side effect of allocating the buffer,
// Calling GetBuffer() may have the side effect of allocating the buffer,
// so only do this when it’s needed.
buffer = array->Buffer();
buffer = array->GetBuffer();
}

if (data != nullptr) {
Expand Down
2 changes: 1 addition & 1 deletion src/module_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ void ModuleWrap::New(const FunctionCallbackInfo<Value>& args) {
CHECK(!can_use_builtin_cache); // We don't use this option internally.
Local<ArrayBufferView> cached_data_buf = args[5].As<ArrayBufferView>();
uint8_t* data =
static_cast<uint8_t*>(cached_data_buf->Buffer()->Data());
static_cast<uint8_t*>(cached_data_buf->GetBuffer()->Data());
user_cached_data =
new ScriptCompiler::CachedData(data + cached_data_buf->ByteOffset(),
cached_data_buf->ByteLength());
Expand Down
12 changes: 8 additions & 4 deletions src/node_blob.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace node {

using v8::Array;
using v8::ArrayBuffer;
using v8::ArrayBufferBase;
using v8::ArrayBufferView;
using v8::BackingStore;
using v8::BackingStoreInitializationMode;
Expand Down Expand Up @@ -77,7 +78,7 @@ void Concat(const FunctionCallbackInfo<Value>& args) {
} else {
CHECK(val->IsArrayBufferView());
auto view = val.As<ArrayBufferView>();
views.push_back(View{view->Buffer()->GetBackingStore(),
views.push_back(View{view->GetBuffer()->GetBackingStore(),
view->ByteLength(),
view->ByteOffset()});
total += view->ByteLength();
Expand Down Expand Up @@ -206,13 +207,16 @@ void Blob::New(const FunctionCallbackInfo<Value>& args) {
Local<Value> entry = sources[i].Get(isolate);

auto entryFromArrayBuffer =
[isolate](Local<ArrayBuffer> buf,
[isolate](Local<ArrayBufferBase> buf,
size_t byte_length,
size_t byte_offset =
0) mutable -> std::unique_ptr<DataQueue::Entry> {
if (buf->IsDetachable()) {
std::shared_ptr<BackingStore> store = buf->GetBackingStore();
if (buf->Detach(Local<Value>()).IsNothing()) {
// A SharedArrayBuffer is never detachable, assuming this is an
// ArrayBuffer is safe.
DCHECK(buf->IsArrayBuffer());
if (buf.As<ArrayBuffer>()->Detach(Local<Value>()).IsNothing()) {
return nullptr;
}
return DataQueue::CreateInMemoryEntryFromBackingStore(
Expand Down Expand Up @@ -245,7 +249,7 @@ void Blob::New(const FunctionCallbackInfo<Value>& args) {
} else if (entry->IsArrayBufferView()) {
Local<ArrayBufferView> view = entry.As<ArrayBufferView>();
auto ret = entryFromArrayBuffer(
view->Buffer(), view->ByteLength(), view->ByteOffset());
view->GetBuffer(), view->ByteLength(), view->ByteOffset());
if (!ret) return;
entries[i] = std::move(ret);
} else if (Blob::HasInstance(env, entry)) {
Expand Down
14 changes: 7 additions & 7 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ namespace node {
namespace Buffer {

using v8::ArrayBuffer;
using v8::ArrayBufferBase;
using v8::ArrayBufferView;
using v8::BackingStore;
using v8::BackingStoreInitializationMode;
Expand Down Expand Up @@ -257,7 +258,7 @@ bool HasInstance(Local<Object> obj) {
char* Data(Local<Value> val) {
CHECK(val->IsArrayBufferView());
Local<ArrayBufferView> ui = val.As<ArrayBufferView>();
return static_cast<char*>(ui->Buffer()->Data()) + ui->ByteOffset();
return static_cast<char*>(ui->GetBuffer()->Data()) + ui->ByteOffset();
}


Expand All @@ -279,9 +280,8 @@ size_t Length(Local<Object> obj) {
return ui->ByteLength();
}


MaybeLocal<Uint8Array> New(Environment* env,
Local<ArrayBuffer> ab,
Local<ArrayBufferBase> ab,
size_t byte_offset,
size_t length) {
CHECK(!env->buffer_prototype_object().IsEmpty());
Expand Down Expand Up @@ -584,7 +584,7 @@ void StringSlice(const FunctionCallbackInfo<Value>& args) {
size_t length = end - start;

std::unique_ptr<char[]> data_copy;
if (view->Buffer()->IsSharedArrayBuffer()) {
if (view->GetBuffer()->IsSharedArrayBuffer()) {
data_copy = std::make_unique_for_overwrite<char[]>(length);
memcpy(data_copy.get(), data_ptr + start, length);
data_ptr = data_copy.get();
Expand All @@ -609,8 +609,8 @@ size_t CopyImpl(Local<Value> source_obj,
Local<ArrayBufferView> source = source_obj.As<ArrayBufferView>();
Local<ArrayBufferView> target = target_obj.As<ArrayBufferView>();

Local<ArrayBuffer> source_ab = source->Buffer();
Local<ArrayBuffer> target_ab = target->Buffer();
Local<ArrayBufferBase> source_ab = source->GetBuffer();
Local<ArrayBufferBase> target_ab = target->GetBuffer();

const size_t source_offset = source->ByteOffset() + source_start;
const size_t target_offset = target->ByteOffset() + target_start;
Expand Down Expand Up @@ -1502,7 +1502,7 @@ static bool MaskArgs(Local<Value> source_obj,
uint32_t mask) {
CHECK(destination_obj->IsArrayBufferView());
Local<ArrayBufferView> destination = destination_obj.As<ArrayBufferView>();
Local<ArrayBuffer> destination_ab = destination->Buffer();
Local<ArrayBufferBase> destination_ab = destination->GetBuffer();
if (destination_ab->IsImmutable()) return false;
if (length == 0) return true;
const size_t destination_length = destination->ByteLength();
Expand Down
4 changes: 2 additions & 2 deletions src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {

ScriptCompiler::CachedData* cached_data = nullptr;
if (!cached_data_buf.IsEmpty()) {
uint8_t* data = static_cast<uint8_t*>(cached_data_buf->Buffer()->Data());
uint8_t* data = static_cast<uint8_t*>(cached_data_buf->GetBuffer()->Data());
cached_data = new ScriptCompiler::CachedData(
data + cached_data_buf->ByteOffset(), cached_data_buf->ByteLength());
}
Expand Down Expand Up @@ -1436,7 +1436,7 @@ void ContextifyFunction::CompileFunction(
// Read cache from cached data buffer
ScriptCompiler::CachedData* cached_data = nullptr;
if (!cached_data_buf.IsEmpty()) {
uint8_t* data = static_cast<uint8_t*>(cached_data_buf->Buffer()->Data());
uint8_t* data = static_cast<uint8_t*>(cached_data_buf->GetBuffer()->Data());
cached_data = new ScriptCompiler::CachedData(
data + cached_data_buf->ByteOffset(), cached_data_buf->ByteLength());
}
Expand Down
2 changes: 1 addition & 1 deletion src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3960,7 +3960,7 @@ class WriteFileJob final : public AsyncWrap, public ThreadPoolWork {
// detached or collected meanwhile; a resizable buffer can still have its
// pages decommitted by a shrink, so its contents are copied instead.
length_ = view->ByteLength();
backing_store_ = view->Buffer()->GetBackingStore();
backing_store_ = view->GetBuffer()->GetBackingStore();
if (backing_store_->IsResizableByUserJavaScript()) {
copy_.reset(new char[length_]);
memcpy(copy_.get(),
Expand Down
4 changes: 2 additions & 2 deletions src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ v8::MaybeLocal<v8::Object> New(Environment* env,
v8::MaybeLocal<v8::Object> New(Environment* env,
char* data,
size_t length);
// Creates a Buffer instance over an existing ArrayBuffer.
// Creates a Buffer instance over an existing ArrayBuffer or SharedArrayBuffer.
v8::MaybeLocal<v8::Uint8Array> New(Environment* env,
v8::Local<v8::ArrayBuffer> ab,
v8::Local<v8::ArrayBufferBase> ab,
size_t byte_offset,
size_t length);
// Construct a Buffer from a MaybeStackBuffer (and also its subclasses like
Expand Down
Loading
Loading