Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(27)

Unified Diff: src/api.cc

Issue 1136553006: Implement SharedArrayBuffer (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: fix dumb typo Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/api.cc
diff --git a/src/api.cc b/src/api.cc
index 125a31be06f94b802e9f61a465335ee641b8c189..8adf2693f127a26a5897dfc1cdaffc3b0bba8551 100644
--- a/src/api.cc
+++ b/src/api.cc
@@ -341,6 +341,7 @@ StartupData V8::CreateSnapshotDataBlob(const char* custom_source) {
i::Isolate* internal_isolate = new i::Isolate(true);
ArrayBufferAllocator allocator;
internal_isolate->set_array_buffer_allocator(&allocator);
+ internal_isolate->set_shared_array_buffer_allocator(&allocator);
Isolate* isolate = reinterpret_cast<Isolate*>(internal_isolate);
StartupData result = {NULL, 0};
{
@@ -2677,7 +2678,8 @@ bool Value::IsArray() const {
bool Value::IsArrayBuffer() const {
- return Utils::OpenHandle(this)->IsJSArrayBuffer();
+ i::Handle<i::Object> obj = Utils::OpenHandle(this);
+ return obj->IsJSArrayBuffer() && !i::JSArrayBuffer::cast(*obj)->is_shared();
}
@@ -2698,6 +2700,7 @@ bool Value::IsTypedArray() const {
i::JSTypedArray::cast(*obj)->type() == i::kExternal##Type##Array; \
}
+
TYPED_ARRAYS(VALUE_IS_TYPED_ARRAY)
#undef VALUE_IS_TYPED_ARRAY
@@ -2708,6 +2711,12 @@ bool Value::IsDataView() const {
}
+bool Value::IsSharedArrayBuffer() const {
+ i::Handle<i::Object> obj = Utils::OpenHandle(this);
+ return obj->IsJSArrayBuffer() && i::JSArrayBuffer::cast(*obj)->is_shared();
+}
+
+
bool Value::IsObject() const {
return Utils::OpenHandle(this)->IsJSObject();
}
@@ -3084,9 +3093,9 @@ void v8::Promise::Resolver::CheckCast(Value* that) {
void v8::ArrayBuffer::CheckCast(Value* that) {
i::Handle<i::Object> obj = Utils::OpenHandle(that);
- Utils::ApiCheck(obj->IsJSArrayBuffer(),
- "v8::ArrayBuffer::Cast()",
- "Could not convert to ArrayBuffer");
+ Utils::ApiCheck(
+ obj->IsJSArrayBuffer() && !i::JSArrayBuffer::cast(*obj)->is_shared(),
+ "v8::ArrayBuffer::Cast()", "Could not convert to ArrayBuffer");
}
@@ -3129,6 +3138,15 @@ void v8::DataView::CheckCast(Value* that) {
}
+void v8::SharedArrayBuffer::CheckCast(Value* that) {
+ i::Handle<i::Object> obj = Utils::OpenHandle(that);
+ Utils::ApiCheck(
+ obj->IsJSArrayBuffer() && i::JSArrayBuffer::cast(*obj)->is_shared(),
+ "v8::SharedArrayBuffer::Cast()",
+ "Could not convert to SharedArrayBuffer");
+}
+
+
void v8::Date::CheckCast(v8::Value* that) {
i::Handle<i::Object> obj = Utils::OpenHandle(that);
i::Isolate* isolate = NULL;
@@ -6303,7 +6321,7 @@ Local<ArrayBuffer> v8::ArrayBuffer::New(Isolate* isolate, size_t byte_length) {
LOG_API(i_isolate, "v8::ArrayBuffer::New(size_t)");
ENTER_V8(i_isolate);
i::Handle<i::JSArrayBuffer> obj =
- i_isolate->factory()->NewJSArrayBuffer();
+ i_isolate->factory()->NewJSArrayBuffer(i::NOT_SHARED);
i::Runtime::SetupArrayBufferAllocatingData(i_isolate, obj, byte_length);
return Utils::ToLocal(obj);
}
@@ -6316,7 +6334,7 @@ Local<ArrayBuffer> v8::ArrayBuffer::New(Isolate* isolate, void* data,
LOG_API(i_isolate, "v8::ArrayBuffer::New(void*, size_t)");
ENTER_V8(i_isolate);
i::Handle<i::JSArrayBuffer> obj =
- i_isolate->factory()->NewJSArrayBuffer();
+ i_isolate->factory()->NewJSArrayBuffer(i::NOT_SHARED);
i::Runtime::SetupArrayBuffer(i_isolate, obj,
mode == ArrayBufferCreationMode::kExternalized,
data, byte_length);
@@ -6422,6 +6440,66 @@ Local<DataView> DataView::New(Handle<ArrayBuffer> array_buffer,
}
+bool v8::SharedArrayBuffer::IsExternal() const {
+ return Utils::OpenHandle(this)->is_external();
+}
+
+
+v8::SharedArrayBuffer::Contents v8::SharedArrayBuffer::Externalize() {
+ i::Handle<i::JSArrayBuffer> self = Utils::OpenHandle(this);
+ i::Isolate* isolate = self->GetIsolate();
+ Utils::ApiCheck(!self->is_external(), "v8::SharedArrayBuffer::Externalize",
+ "SharedArrayBuffer already externalized");
+ self->set_is_external(true);
+ isolate->heap()->UnregisterArrayBuffer(self->backing_store());
+ return GetContents();
+}
+
+
+v8::SharedArrayBuffer::Contents v8::SharedArrayBuffer::GetContents() {
+ i::Handle<i::JSArrayBuffer> self = Utils::OpenHandle(this);
+ size_t byte_length = static_cast<size_t>(self->byte_length()->Number());
+ Contents contents;
+ contents.data_ = self->backing_store();
+ contents.byte_length_ = byte_length;
+ return contents;
+}
+
+
+size_t v8::SharedArrayBuffer::ByteLength() const {
+ i::Handle<i::JSArrayBuffer> obj = Utils::OpenHandle(this);
+ return static_cast<size_t>(obj->byte_length()->Number());
+}
+
+
+Local<SharedArrayBuffer> v8::SharedArrayBuffer::New(Isolate* isolate,
+ size_t byte_length) {
+ i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
+ LOG_API(i_isolate, "v8::SharedArrayBuffer::New(size_t)");
+ ENTER_V8(i_isolate);
+ i::Handle<i::JSArrayBuffer> obj =
+ i_isolate->factory()->NewJSArrayBuffer(i::SHARED);
+ i::Runtime::SetupArrayBufferAllocatingData(i_isolate, obj, byte_length, true,
+ i::SHARED);
+ return Utils::ToLocalShared(obj);
+}
+
+
+Local<SharedArrayBuffer> v8::SharedArrayBuffer::New(
+ Isolate* isolate, void* data, size_t byte_length,
+ ArrayBufferCreationMode mode) {
+ i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
+ LOG_API(i_isolate, "v8::SharedArrayBuffer::New(void*, size_t)");
+ ENTER_V8(i_isolate);
+ i::Handle<i::JSArrayBuffer> obj =
+ i_isolate->factory()->NewJSArrayBuffer(i::SHARED);
+ i::Runtime::SetupArrayBuffer(i_isolate, obj,
+ mode == ArrayBufferCreationMode::kExternalized,
+ data, byte_length, i::SHARED);
+ return Utils::ToLocalShared(obj);
+}
+
+
Local<Symbol> v8::Symbol::New(Isolate* isolate, Local<String> name) {
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
LOG_API(i_isolate, "Symbol::New()");
@@ -6768,6 +6846,14 @@ Isolate* Isolate::New(const Isolate::CreateParams& params) {
} else {
isolate->set_array_buffer_allocator(i::V8::ArrayBufferAllocator());
}
+ if (params.shared_array_buffer_allocator) {
+ isolate->set_shared_array_buffer_allocator(
+ params.shared_array_buffer_allocator);
+ } else {
+ // Default to using the same allocator as the array_buffer_allocator.
+ isolate->set_shared_array_buffer_allocator(
+ isolate->array_buffer_allocator());
+ }
if (params.snapshot_blob != NULL) {
isolate->set_snapshot_blob(params.snapshot_blob);
} else {

Powered by Google App Engine
This is Rietveld 408576698