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

Side by Side Diff: runtime/lib/byte_buffer.cc

Issue 9195031: Add ByteArray interface and provide internal and external implementations. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: address remaining review comments Created 8 years, 11 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/lib/byte_array.dart ('k') | runtime/lib/byte_buffer.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 #include "platform/assert.h"
6 #include "vm/assembler.h"
7 #include "vm/bigint_operations.h"
8 #include "vm/bootstrap_natives.h"
9 #include "vm/exceptions.h"
10 #include "vm/native_entry.h"
11 #include "vm/object.h"
12
13 namespace dart {
14
15 DEFINE_NATIVE_ENTRY(ByteBuffer_allocate, 1) {
16 GET_NATIVE_ARGUMENT(Smi, length, arguments->At(0));
17 if (length.Value() < 0) {
18 GrowableArray<const Object*> args;
19 args.Add(&length);
20 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args);
21 }
22 uint8_t* data = new uint8_t[length.Value()];
23 memset(data, 0, length.Value());
24 const ByteBuffer& new_array =
25 ByteBuffer::Handle(ByteBuffer::New(data, length.Value()));
26 arguments->SetReturn(new_array);
27 }
28
29
30 DEFINE_NATIVE_ENTRY(ByteBuffer_getLength, 1) {
31 const ByteBuffer& array = ByteBuffer::CheckedHandle(arguments->At(0));
32 const Smi& length = Smi::Handle(Smi::New(array.Length()));
33 arguments->SetReturn(length);
34 }
35
36
37 template<typename ValueType, typename ObjectType>
38 static void GetIndexed(NativeArguments* arguments) {
39 const ByteBuffer& buffer = ByteBuffer::CheckedHandle(arguments->At(0));
40 const Instance& index_instance = Instance::CheckedHandle(arguments->At(1));
41 if (!index_instance.IsSmi()) {
42 GrowableArray<const Object*> args;
43 args.Add(&index_instance);
44 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args);
45 }
46 Smi& index = Smi::Handle();
47 index ^= index_instance.raw();
48 if (buffer.IsNull() || index.IsNull()) {
49 // TODO(asiva): Need to handle error cases.
50 UNIMPLEMENTED();
51 return;
52 }
53 intptr_t num_bytes = sizeof(ValueType);
54 if ((index.Value() < 0) || ((index.Value() + num_bytes) > buffer.Length())) {
55 GrowableArray<const Object*> arguments;
56 arguments.Add(&index);
57 Exceptions::ThrowByType(Exceptions::kIndexOutOfRange, arguments);
58 }
59 arguments->SetReturn(
60 ObjectType::Handle(
61 ObjectType::New(buffer.UnalignedAt<ValueType>(index.Value()))));
62 }
63
64
65 template<typename T> static bool HasType(const Object& obj);
66
67
68 template<>
69 bool HasType<Smi>(const Object& obj) {
70 return obj.IsSmi();
71 }
72
73
74 template<>
75 bool HasType<Integer>(const Object& obj) {
76 return obj.IsInteger();
77 }
78
79
80 template<>
81 bool HasType<Double>(const Object& obj) {
82 return obj.IsDouble();
83 }
84
85
86 static intptr_t Value(const Smi& obj) {
87 return obj.Value();
88 }
89
90
91 static int64_t Value(const Integer& obj) {
92 return obj.AsInt64Value();
93 }
94
95
96 static double Value(const Double& obj) {
97 return obj.value();
98 }
99
100
101 template<typename ValueType, typename ObjectType>
102 static void SetIndexed(const NativeArguments* arguments) {
103 const ByteBuffer& buffer = ByteBuffer::CheckedHandle(arguments->At(0));
104 const Instance& index_instance = Instance::CheckedHandle(arguments->At(1));
105 if (!index_instance.IsSmi()) {
106 GrowableArray<const Object*> args;
107 args.Add(&index_instance);
108 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args);
109 }
110 Smi& index = Smi::Handle();
111 index ^= index_instance.raw();
112 const Instance& value_instance = Instance::CheckedHandle(arguments->At(2));
113 if (buffer.IsNull() || index.IsNull()) {
114 // TODO(asiva): Need to handle error cases.
115 UNIMPLEMENTED();
116 return;
117 }
118 if (index.Value() >= buffer.Length()) {
119 GrowableArray<const Object*> arguments;
120 arguments.Add(&index);
121 Exceptions::ThrowByType(Exceptions::kIndexOutOfRange, arguments);
122 }
123 if (!HasType<ObjectType>(value_instance)) {
124 GrowableArray<const Object*> args;
125 args.Add(&value_instance);
126 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args);
127 }
128 ObjectType& value = ObjectType::Handle();
129 value ^= value_instance.raw();
130 buffer.SetUnalignedAt<ValueType>(index.Value(), Value(value));
131 }
132
133
134 DEFINE_NATIVE_ENTRY(ByteBuffer_getInt8, 2) {
135 GetIndexed<int8_t, Smi>(arguments);
136 }
137
138
139 DEFINE_NATIVE_ENTRY(ByteBuffer_setInt8, 3) {
140 SetIndexed<int8_t, Smi>(arguments);
141 }
142
143
144 DEFINE_NATIVE_ENTRY(ByteBuffer_getUint8, 2) {
145 GetIndexed<uint8_t, Smi>(arguments);
146 }
147
148
149 DEFINE_NATIVE_ENTRY(ByteBuffer_setUint8, 3) {
150 SetIndexed<uint8_t, Smi>(arguments);
151 }
152
153
154 DEFINE_NATIVE_ENTRY(ByteBuffer_getInt16, 2) {
155 GetIndexed<int16_t, Smi>(arguments);
156 }
157
158
159 DEFINE_NATIVE_ENTRY(ByteBuffer_setInt16, 3) {
160 SetIndexed<int16_t, Smi>(arguments);
161 }
162
163
164 DEFINE_NATIVE_ENTRY(ByteBuffer_getUint16, 2) {
165 GetIndexed<uint16_t, Smi>(arguments);
166 }
167
168
169 DEFINE_NATIVE_ENTRY(ByteBuffer_setUint16, 3) {
170 SetIndexed<uint16_t, Smi>(arguments);
171 }
172
173
174 DEFINE_NATIVE_ENTRY(ByteBuffer_getInt32, 2) {
175 GetIndexed<int32_t, Integer>(arguments);
176 }
177
178
179 DEFINE_NATIVE_ENTRY(ByteBuffer_setInt32, 3) {
180 SetIndexed<int32_t, Integer>(arguments);
181 }
182
183
184 DEFINE_NATIVE_ENTRY(ByteBuffer_getUint32, 2) {
185 GetIndexed<uint32_t, Integer>(arguments);
186 }
187
188
189 DEFINE_NATIVE_ENTRY(ByteBuffer_setUint32, 3) {
190 SetIndexed<uint32_t, Integer>(arguments);
191 }
192
193
194 DEFINE_NATIVE_ENTRY(ByteBuffer_getInt64, 2) {
195 GetIndexed<int64_t, Integer>(arguments);
196 }
197
198
199 DEFINE_NATIVE_ENTRY(ByteBuffer_setInt64, 3) {
200 SetIndexed<int64_t, Integer>(arguments);
201 }
202
203
204 DEFINE_NATIVE_ENTRY(ByteBuffer_getUint64, 2) {
205 UNIMPLEMENTED(); // TODO(cshapiro): need getter implementation.
206 }
207
208
209 DEFINE_NATIVE_ENTRY(ByteBuffer_setUint64, 3) {
210 UNIMPLEMENTED(); // TODO(cshapiro): need setter implementation.
211 }
212
213
214 DEFINE_NATIVE_ENTRY(ByteBuffer_getFloat32, 2) {
215 GetIndexed<float, Double>(arguments);
216 }
217
218
219 DEFINE_NATIVE_ENTRY(ByteBuffer_setFloat32, 3) {
220 SetIndexed<float, Double>(arguments);
221 }
222
223
224 DEFINE_NATIVE_ENTRY(ByteBuffer_getFloat64, 2) {
225 GetIndexed<double, Double>(arguments);
226 }
227
228
229 DEFINE_NATIVE_ENTRY(ByteBuffer_setFloat64, 3) {
230 SetIndexed<double, Double>(arguments);
231 }
232
233 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/lib/byte_array.dart ('k') | runtime/lib/byte_buffer.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698