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
3 // found in the LICENSE file.
7 #include "base/logging.h"
8 #include "gin/array_buffer.h"
9 #include "gin/per_isolate_data.h"
15 gin::WrapperInfo g_array_buffer_wrapper_info
= {gin::kEmbedderNativeGin
};
19 static_assert(V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT
== 2,
20 "array buffers must have two internal fields");
22 // ArrayBufferAllocator -------------------------------------------------------
24 void* ArrayBufferAllocator::Allocate(size_t length
) {
25 return calloc(1, length
);
28 void* ArrayBufferAllocator::AllocateUninitialized(size_t length
) {
29 return malloc(length
);
32 void ArrayBufferAllocator::Free(void* data
, size_t length
) {
36 ArrayBufferAllocator
* ArrayBufferAllocator::SharedInstance() {
37 static ArrayBufferAllocator
* instance
= new ArrayBufferAllocator();
41 // ArrayBuffer::Private -------------------------------------------------------
43 // This class exists to solve a tricky lifetime problem. The V8 API doesn't
44 // want to expose a direct view into the memory behind an array buffer because
45 // V8 might deallocate that memory during garbage collection. Instead, the V8
46 // API forces us to externalize the buffer and take ownership of the memory.
47 // In order to know when to free the memory, we need to figure out both when
48 // we're done with it and when V8 is done with it.
50 // To determine whether we're done with the memory, every view we have into
51 // the array buffer takes a reference to the ArrayBuffer::Private object that
52 // actually owns the memory. To determine when V8 is done with the memory, we
53 // open a weak handle to the ArrayBuffer object. When we receive the weak
54 // callback, we know the object is about to be garbage collected and we can
55 // drop V8's implied reference to the memory.
57 // The final subtlety is that we need every ArrayBuffer into the same array
58 // buffer to AddRef the same ArrayBuffer::Private. To make that work, we store
59 // a pointer to the ArrayBuffer::Private object in an internal field of the
60 // ArrayBuffer object.
62 class ArrayBuffer::Private
: public base::RefCounted
<ArrayBuffer::Private
> {
64 static scoped_refptr
<Private
> From(v8::Isolate
* isolate
,
65 v8::Handle
<v8::ArrayBuffer
> array
);
67 void* buffer() const { return buffer_
; }
68 size_t length() const { return length_
; }
71 friend class base::RefCounted
<Private
>;
73 Private(v8::Isolate
* isolate
, v8::Handle
<v8::ArrayBuffer
> array
);
76 static void FirstWeakCallback(const v8::WeakCallbackInfo
<Private
>& data
);
77 static void SecondWeakCallback(const v8::WeakCallbackInfo
<Private
>& data
);
79 v8::Global
<v8::ArrayBuffer
> array_buffer_
;
80 scoped_refptr
<Private
> self_reference_
;
81 v8::Isolate
* isolate_
;
86 scoped_refptr
<ArrayBuffer::Private
> ArrayBuffer::Private::From(
87 v8::Isolate
* isolate
, v8::Handle
<v8::ArrayBuffer
> array
) {
88 if (array
->IsExternal()) {
89 CHECK_EQ(WrapperInfo::From(v8::Handle
<v8::Object
>::Cast(array
)),
90 &g_array_buffer_wrapper_info
)
91 << "Cannot mix blink and gin ArrayBuffers";
92 return make_scoped_refptr(static_cast<Private
*>(
93 array
->GetAlignedPointerFromInternalField(kEncodedValueIndex
)));
95 return make_scoped_refptr(new Private(isolate
, array
));
98 ArrayBuffer::Private::Private(v8::Isolate
* isolate
,
99 v8::Handle
<v8::ArrayBuffer
> array
)
100 : array_buffer_(isolate
, array
), isolate_(isolate
) {
101 // Take ownership of the array buffer.
102 CHECK(!array
->IsExternal());
103 v8::ArrayBuffer::Contents contents
= array
->Externalize();
104 buffer_
= contents
.Data();
105 length_
= contents
.ByteLength();
107 array
->SetAlignedPointerInInternalField(kWrapperInfoIndex
,
108 &g_array_buffer_wrapper_info
);
109 array
->SetAlignedPointerInInternalField(kEncodedValueIndex
, this);
111 self_reference_
= this; // Cleared in SecondWeakCallback.
112 array_buffer_
.SetWeak(this, FirstWeakCallback
,
113 v8::WeakCallbackType::kParameter
);
116 ArrayBuffer::Private::~Private() {
117 PerIsolateData::From(isolate_
)->allocator()->Free(buffer_
, length_
);
120 void ArrayBuffer::Private::FirstWeakCallback(
121 const v8::WeakCallbackInfo
<Private
>& data
) {
122 Private
* parameter
= data
.GetParameter();
123 parameter
->array_buffer_
.Reset();
124 data
.SetSecondPassCallback(SecondWeakCallback
);
127 void ArrayBuffer::Private::SecondWeakCallback(
128 const v8::WeakCallbackInfo
<Private
>& data
) {
129 Private
* parameter
= data
.GetParameter();
130 parameter
->self_reference_
= NULL
;
133 // ArrayBuffer ----------------------------------------------------------------
135 ArrayBuffer::ArrayBuffer()
140 ArrayBuffer::ArrayBuffer(v8::Isolate
* isolate
,
141 v8::Handle
<v8::ArrayBuffer
> array
) {
142 private_
= ArrayBuffer::Private::From(isolate
, array
);
143 bytes_
= private_
->buffer();
144 num_bytes_
= private_
->length();
147 ArrayBuffer::~ArrayBuffer() {
150 ArrayBuffer
& ArrayBuffer::operator=(const ArrayBuffer
& other
) {
151 private_
= other
.private_
;
152 bytes_
= other
.bytes_
;
153 num_bytes_
= other
.num_bytes_
;
157 // Converter<ArrayBuffer> -----------------------------------------------------
159 bool Converter
<ArrayBuffer
>::FromV8(v8::Isolate
* isolate
,
160 v8::Handle
<v8::Value
> val
,
162 if (!val
->IsArrayBuffer())
164 *out
= ArrayBuffer(isolate
, v8::Handle
<v8::ArrayBuffer
>::Cast(val
));
168 // ArrayBufferView ------------------------------------------------------------
170 ArrayBufferView::ArrayBufferView()
175 ArrayBufferView::ArrayBufferView(v8::Isolate
* isolate
,
176 v8::Handle
<v8::ArrayBufferView
> view
)
177 : array_buffer_(isolate
, view
->Buffer()),
178 offset_(view
->ByteOffset()),
179 num_bytes_(view
->ByteLength()) {
182 ArrayBufferView::~ArrayBufferView() {
185 ArrayBufferView
& ArrayBufferView::operator=(const ArrayBufferView
& other
) {
186 array_buffer_
= other
.array_buffer_
;
187 offset_
= other
.offset_
;
188 num_bytes_
= other
.num_bytes_
;
193 // Converter<ArrayBufferView> -------------------------------------------------
195 bool Converter
<ArrayBufferView
>::FromV8(v8::Isolate
* isolate
,
196 v8::Handle
<v8::Value
> val
,
197 ArrayBufferView
* out
) {
198 if (!val
->IsArrayBufferView())
200 *out
= ArrayBufferView(isolate
, v8::Handle
<v8::ArrayBufferView
>::Cast(val
));