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

Side by Side Diff: gin/array_buffer.cc

Issue 59153005: Begin implementing V8 bindings for Mojo (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix skipped comment Created 7 years, 1 month 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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" 5 #include "gin/array_buffer.h"
6 6
7 #include <stdlib.h> 7 #include <stdlib.h>
8 8
9 namespace gin { 9 namespace gin {
10 10
(...skipping 14 matching lines...) Expand all
25 25
26 void ArrayBufferAllocator::Free(void* data, size_t length) { 26 void ArrayBufferAllocator::Free(void* data, size_t length) {
27 free(data); 27 free(data);
28 } 28 }
29 29
30 ArrayBufferAllocator* ArrayBufferAllocator::SharedInstance() { 30 ArrayBufferAllocator* ArrayBufferAllocator::SharedInstance() {
31 static ArrayBufferAllocator* instance = new ArrayBufferAllocator(); 31 static ArrayBufferAllocator* instance = new ArrayBufferAllocator();
32 return instance; 32 return instance;
33 } 33 }
34 34
35 // BufferView::Private -------------------------------------------------------- 35 // ArrayBuffer::Private -------------------------------------------------------
36 36
37 // This class exists to solve a tricky lifetime problem. The V8 API doesn't 37 // This class exists to solve a tricky lifetime problem. The V8 API doesn't
38 // want to expose a direct view into the memory behind an array buffer because 38 // want to expose a direct view into the memory behind an array buffer because
39 // V8 might deallocate that memory during garbage collection. Instead, the V8 39 // V8 might deallocate that memory during garbage collection. Instead, the V8
40 // API forces us to externalize the buffer and take ownership of the memory. 40 // API forces us to externalize the buffer and take ownership of the memory.
41 // In order to know when to free the memory, we need to figure out both when 41 // In order to know when to free the memory, we need to figure out both when
42 // we're done with it and when V8 is done with it. 42 // we're done with it and when V8 is done with it.
43 // 43 //
44 // To determine whether we're done with the memory, every view we have into 44 // To determine whether we're done with the memory, every view we have into
45 // the array buffer takes a reference to the BufferView::Private object that 45 // the array buffer takes a reference to the ArrayBuffer::Private object that
46 // actually owns the memory. To determine when V8 is done with the memory, we 46 // actually owns the memory. To determine when V8 is done with the memory, we
47 // open a weak handle to the ArrayBuffer object. When we receive the weak 47 // open a weak handle to the ArrayBuffer object. When we receive the weak
48 // callback, we know the object is about to be garbage collected and we can 48 // callback, we know the object is about to be garbage collected and we can
49 // drop V8's implied reference to the memory. 49 // drop V8's implied reference to the memory.
50 // 50 //
51 // The final subtlety is that we need every BufferView into the same array 51 // The final subtlety is that we need every ArrayBuffer into the same array
52 // buffer to AddRef the same BufferView::Private. To make that work, we store a 52 // buffer to AddRef the same ArrayBuffer::Private. To make that work, we store
53 // pointer to the BufferView::Private object in an internal field of the 53 // a pointer to the ArrayBuffer::Private object in an internal field of the
54 // ArrayBuffer object. 54 // ArrayBuffer object.
55 // 55 //
56 class BufferView::Private { 56 class ArrayBuffer::Private {
57 public: 57 public:
58 static scoped_refptr<Private> From(v8::Isolate* isolate, 58 static scoped_refptr<Private> From(v8::Isolate* isolate,
59 v8::Handle<v8::ArrayBuffer> array); 59 v8::Handle<v8::ArrayBuffer> array);
60 60
61 void AddRef(); 61 void AddRef();
62 void Release(); 62 void Release();
63 63
64 void* buffer() const { return buffer_; } 64 void* buffer() const { return buffer_; }
65 size_t length() const { return length_; } 65 size_t length() const { return length_; }
66 66
67 private: 67 private:
68 Private(v8::Isolate* isolate, v8::Handle<v8::ArrayBuffer> array); 68 Private(v8::Isolate* isolate, v8::Handle<v8::ArrayBuffer> array);
69 ~Private(); 69 ~Private();
70 70
71 static void WeakCallback( 71 static void WeakCallback(
72 const v8::WeakCallbackData<v8::ArrayBuffer, Private>& data); 72 const v8::WeakCallbackData<v8::ArrayBuffer, Private>& data);
73 73
74 size_t ref_count_; 74 size_t ref_count_;
75 v8::Persistent<v8::ArrayBuffer> array_buffer_; 75 v8::Persistent<v8::ArrayBuffer> array_buffer_;
76 void* buffer_; 76 void* buffer_;
77 size_t length_; 77 size_t length_;
78 }; 78 };
79 79
80 scoped_refptr<BufferView::Private> BufferView::Private::From( 80 scoped_refptr<ArrayBuffer::Private> ArrayBuffer::Private::From(
81 v8::Isolate* isolate, v8::Handle<v8::ArrayBuffer> array) { 81 v8::Isolate* isolate, v8::Handle<v8::ArrayBuffer> array) {
82 if (array->IsExternal()) { 82 if (array->IsExternal()) {
83 return make_scoped_refptr(static_cast<Private*>( 83 return make_scoped_refptr(static_cast<Private*>(
84 array->GetAlignedPointerFromInternalField(kBufferViewPrivateIndex))); 84 array->GetAlignedPointerFromInternalField(kBufferViewPrivateIndex)));
85 } 85 }
86 return make_scoped_refptr(new Private(isolate, array)); 86 return make_scoped_refptr(new Private(isolate, array));
87 } 87 }
88 88
89 void BufferView::Private::AddRef() { 89 void ArrayBuffer::Private::AddRef() {
90 ++ref_count_; 90 ++ref_count_;
91 } 91 }
92 92
93 void BufferView::Private::Release() { 93 void ArrayBuffer::Private::Release() {
94 if (--ref_count_) 94 if (--ref_count_)
95 return; 95 return;
96 delete this; 96 delete this;
97 } 97 }
98 98
99 BufferView::Private::Private(v8::Isolate* isolate, 99 ArrayBuffer::Private::Private(v8::Isolate* isolate,
100 v8::Handle<v8::ArrayBuffer> array) 100 v8::Handle<v8::ArrayBuffer> array)
101 : ref_count_(0), 101 : ref_count_(0),
102 array_buffer_(isolate, array) { 102 array_buffer_(isolate, array) {
103 // Take ownership of the array buffer. 103 // Take ownership of the array buffer.
104 v8::ArrayBuffer::Contents contents = array->Externalize(); 104 v8::ArrayBuffer::Contents contents = array->Externalize();
105 buffer_ = contents.Data(); 105 buffer_ = contents.Data();
106 length_ = contents.ByteLength(); 106 length_ = contents.ByteLength();
107 107
108 array->SetAlignedPointerInInternalField(kBufferViewPrivateIndex, this); 108 array->SetAlignedPointerInInternalField(kBufferViewPrivateIndex, this);
109 109
110 AddRef(); // Balanced in WeakCallback. 110 AddRef(); // Balanced in WeakCallback.
111 array_buffer_.SetWeak(this, WeakCallback); 111 array_buffer_.SetWeak(this, WeakCallback);
112 } 112 }
113 113
114 BufferView::Private::~Private() { 114 ArrayBuffer::Private::~Private() {
115 ArrayBufferAllocator::SharedInstance()->Free(buffer_, length_); 115 ArrayBufferAllocator::SharedInstance()->Free(buffer_, length_);
116 } 116 }
117 117
118 void BufferView::Private::WeakCallback( 118 void ArrayBuffer::Private::WeakCallback(
119 const v8::WeakCallbackData<v8::ArrayBuffer, Private>& data) { 119 const v8::WeakCallbackData<v8::ArrayBuffer, Private>& data) {
120 Private* parameter = data.GetParameter(); 120 Private* parameter = data.GetParameter();
121 parameter->array_buffer_.Reset(); 121 parameter->array_buffer_.Reset();
122 parameter->Release(); // Balanced in BufferView::Private::Private. 122 parameter->Release(); // Balanced in ArrayBuffer::Private::Private.
123 } 123 }
124 124
125 // BufferView ----------------------------------------------------------------- 125 // ArrayBuffer ----------------------------------------------------------------
126 126
127 BufferView::BufferView(v8::Isolate* isolate, 127 ArrayBuffer::ArrayBuffer(v8::Isolate* isolate)
128 v8::Handle<v8::ArrayBufferView> view) { 128 : isolate_(isolate),
129 Initialize(isolate, view->Buffer()); 129 bytes_(0),
130 uint8_t* ptr = static_cast<uint8_t*>(bytes_); 130 num_bytes_(0) {
131 bytes_ = static_cast<void*>(ptr + view->ByteOffset());
132 num_bytes_ = view->ByteLength();
133 } 131 }
134 132
135 BufferView::BufferView(v8::Isolate* isolate, 133 ArrayBuffer::ArrayBuffer(v8::Isolate* isolate,
136 v8::Handle<v8::ArrayBuffer> array) { 134 v8::Handle<v8::ArrayBuffer> array)
137 Initialize(isolate, array); 135 : isolate_(isolate) {
138 } 136 private_ = ArrayBuffer::Private::From(isolate_, array);
139
140 BufferView::~BufferView() {
141 }
142
143 void BufferView::Initialize(v8::Isolate* isolate,
144 v8::Handle<v8::ArrayBuffer> array) {
145 private_ = BufferView::Private::From(isolate, array);
146 bytes_ = private_->buffer(); 137 bytes_ = private_->buffer();
147 num_bytes_ = private_->length(); 138 num_bytes_ = private_->length();
148 } 139 }
149 140
141 ArrayBuffer::~ArrayBuffer() {
142 }
143
144 // Converter<ArrayBuffer> -----------------------------------------------------
145
146 bool Converter<ArrayBuffer>::FromV8(v8::Handle<v8::Value> val,
147 ArrayBuffer* out) {
148 if (!val->IsArrayBuffer())
149 return false;
150 *out = ArrayBuffer(out->isolate(), v8::Handle<v8::ArrayBuffer>::Cast(val));
151 return true;
152 }
153
154 // ArrayBufferView ------------------------------------------------------------
155
156 ArrayBufferView::ArrayBufferView(v8::Isolate* isolate)
157 : array_buffer_(isolate),
158 offset_(0),
159 num_bytes_(0) {
160 }
161
162 ArrayBufferView::ArrayBufferView(v8::Isolate* isolate,
163 v8::Handle<v8::ArrayBufferView> view)
164 : array_buffer_(isolate, view->Buffer()),
165 offset_(view->ByteOffset()),
166 num_bytes_(view->ByteLength()) {
167 }
168
169 ArrayBufferView::~ArrayBufferView() {
170 }
171
172 // Converter<ArrayBufferView> -------------------------------------------------
173
174 bool Converter<ArrayBufferView>::FromV8(v8::Handle<v8::Value> val,
175 ArrayBufferView* out) {
176 if (!val->IsArrayBufferView())
177 return false;
178 *out = ArrayBufferView(out->isolate(),
179 v8::Handle<v8::ArrayBufferView>::Cast(val));
180 return true;
181 }
182
150 } // namespace gin 183 } // namespace gin
OLDNEW
« gin/arguments.cc ('K') | « gin/array_buffer.h ('k') | gin/converter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698