OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "gin/array_buffer.h" | |
6 | |
7 #include <stdlib.h> | 5 #include <stdlib.h> |
8 | 6 |
7 #include "base/logging.h" | |
8 #include "gin/array_buffer.h" | |
9 #include "gin/per_isolate_data.h" | |
10 | |
9 namespace gin { | 11 namespace gin { |
10 | 12 |
13 namespace { | |
14 | |
15 gin::WrapperInfo g_array_buffer_wrapper_info = {gin::kEmbedderNativeGin}; | |
16 | |
17 } // namespace | |
18 | |
11 COMPILE_ASSERT(V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT == 2, | 19 COMPILE_ASSERT(V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT == 2, |
12 array_buffers_must_have_two_internal_fields); | 20 array_buffers_must_have_two_internal_fields); |
13 | 21 |
14 static const int kBufferViewPrivateIndex = 0; | |
15 | |
16 // ArrayBufferAllocator ------------------------------------------------------- | 22 // ArrayBufferAllocator ------------------------------------------------------- |
17 | 23 |
18 void* ArrayBufferAllocator::Allocate(size_t length) { | 24 void* ArrayBufferAllocator::Allocate(size_t length) { |
19 return calloc(1, length); | 25 return calloc(1, length); |
20 } | 26 } |
21 | 27 |
22 void* ArrayBufferAllocator::AllocateUninitialized(size_t length) { | 28 void* ArrayBufferAllocator::AllocateUninitialized(size_t length) { |
23 return malloc(length); | 29 return malloc(length); |
24 } | 30 } |
25 | 31 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
65 friend class base::RefCounted<Private>; | 71 friend class base::RefCounted<Private>; |
66 | 72 |
67 Private(v8::Isolate* isolate, v8::Handle<v8::ArrayBuffer> array); | 73 Private(v8::Isolate* isolate, v8::Handle<v8::ArrayBuffer> array); |
68 ~Private(); | 74 ~Private(); |
69 | 75 |
70 static void WeakCallback( | 76 static void WeakCallback( |
71 const v8::WeakCallbackData<v8::ArrayBuffer, Private>& data); | 77 const v8::WeakCallbackData<v8::ArrayBuffer, Private>& data); |
72 | 78 |
73 v8::Persistent<v8::ArrayBuffer> array_buffer_; | 79 v8::Persistent<v8::ArrayBuffer> array_buffer_; |
74 scoped_refptr<Private> self_reference_; | 80 scoped_refptr<Private> self_reference_; |
81 v8::Isolate* isolate_; | |
75 void* buffer_; | 82 void* buffer_; |
76 size_t length_; | 83 size_t length_; |
77 }; | 84 }; |
78 | 85 |
79 scoped_refptr<ArrayBuffer::Private> ArrayBuffer::Private::From( | 86 scoped_refptr<ArrayBuffer::Private> ArrayBuffer::Private::From( |
80 v8::Isolate* isolate, v8::Handle<v8::ArrayBuffer> array) { | 87 v8::Isolate* isolate, v8::Handle<v8::ArrayBuffer> array) { |
81 if (array->IsExternal()) { | 88 if (array->IsExternal()) { |
89 CHECK(WrapperInfo::From(v8::Handle<v8::Object>::Cast(array)) == | |
90 &g_array_buffer_wrapper_info) | |
91 << "Cannot mix blink and gin ArrayBuffers"; | |
abarth-chromium
2014/02/19 18:59:57
CHECK_EQ ?
This kind of sucks, but it seems ok in
| |
82 return make_scoped_refptr(static_cast<Private*>( | 92 return make_scoped_refptr(static_cast<Private*>( |
83 array->GetAlignedPointerFromInternalField(kBufferViewPrivateIndex))); | 93 array->GetAlignedPointerFromInternalField(kEncodedValueIndex))); |
84 } | 94 } |
85 return make_scoped_refptr(new Private(isolate, array)); | 95 return make_scoped_refptr(new Private(isolate, array)); |
86 } | 96 } |
87 | 97 |
88 ArrayBuffer::Private::Private(v8::Isolate* isolate, | 98 ArrayBuffer::Private::Private(v8::Isolate* isolate, |
89 v8::Handle<v8::ArrayBuffer> array) | 99 v8::Handle<v8::ArrayBuffer> array) |
90 : array_buffer_(isolate, array) { | 100 : array_buffer_(isolate, array), isolate_(isolate) { |
91 // Take ownership of the array buffer. | 101 // Take ownership of the array buffer. |
102 CHECK(!array->IsExternal()); | |
92 v8::ArrayBuffer::Contents contents = array->Externalize(); | 103 v8::ArrayBuffer::Contents contents = array->Externalize(); |
93 buffer_ = contents.Data(); | 104 buffer_ = contents.Data(); |
94 length_ = contents.ByteLength(); | 105 length_ = contents.ByteLength(); |
95 | 106 |
96 array->SetAlignedPointerInInternalField(kBufferViewPrivateIndex, this); | 107 array->SetAlignedPointerInInternalField(kWrapperInfoIndex, |
108 &g_array_buffer_wrapper_info); | |
109 array->SetAlignedPointerInInternalField(kEncodedValueIndex, this); | |
97 | 110 |
98 self_reference_ = this; // Cleared in WeakCallback. | 111 self_reference_ = this; // Cleared in WeakCallback. |
99 array_buffer_.SetWeak(this, WeakCallback); | 112 array_buffer_.SetWeak(this, WeakCallback); |
100 } | 113 } |
101 | 114 |
102 ArrayBuffer::Private::~Private() { | 115 ArrayBuffer::Private::~Private() { |
103 ArrayBufferAllocator::SharedInstance()->Free(buffer_, length_); | 116 PerIsolateData::From(isolate_)->allocator()->Free(buffer_, length_); |
104 } | 117 } |
105 | 118 |
106 void ArrayBuffer::Private::WeakCallback( | 119 void ArrayBuffer::Private::WeakCallback( |
107 const v8::WeakCallbackData<v8::ArrayBuffer, Private>& data) { | 120 const v8::WeakCallbackData<v8::ArrayBuffer, Private>& data) { |
108 Private* parameter = data.GetParameter(); | 121 Private* parameter = data.GetParameter(); |
109 parameter->array_buffer_.Reset(); | 122 parameter->array_buffer_.Reset(); |
110 parameter->self_reference_ = NULL; | 123 parameter->self_reference_ = NULL; |
111 } | 124 } |
112 | 125 |
113 // ArrayBuffer ---------------------------------------------------------------- | 126 // ArrayBuffer ---------------------------------------------------------------- |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
167 bool Converter<ArrayBufferView>::FromV8(v8::Isolate* isolate, | 180 bool Converter<ArrayBufferView>::FromV8(v8::Isolate* isolate, |
168 v8::Handle<v8::Value> val, | 181 v8::Handle<v8::Value> val, |
169 ArrayBufferView* out) { | 182 ArrayBufferView* out) { |
170 if (!val->IsArrayBufferView()) | 183 if (!val->IsArrayBufferView()) |
171 return false; | 184 return false; |
172 *out = ArrayBufferView(isolate, v8::Handle<v8::ArrayBufferView>::Cast(val)); | 185 *out = ArrayBufferView(isolate, v8::Handle<v8::ArrayBufferView>::Cast(val)); |
173 return true; | 186 return true; |
174 } | 187 } |
175 | 188 |
176 } // namespace gin | 189 } // namespace gin |
OLD | NEW |