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

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

Issue 9769013: Narrow when storing to a byte array and implement uint64 get and set. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: address review comments Created 8 years, 9 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 | « no previous file | runtime/lib/byte_array.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "vm/bootstrap_natives.h" 5 #include "vm/bootstrap_natives.h"
6 6
7 #include "vm/bigint_operations.h"
7 #include "vm/exceptions.h" 8 #include "vm/exceptions.h"
8 #include "vm/native_entry.h" 9 #include "vm/native_entry.h"
9 #include "vm/object.h" 10 #include "vm/object.h"
10 11
11 namespace dart { 12 namespace dart {
12 13
13 DEFINE_NATIVE_ENTRY(ByteArray_getLength, 1) { 14 DEFINE_NATIVE_ENTRY(ByteArray_getLength, 1) {
14 const ByteArray& byte_array = ByteArray::CheckedHandle(arguments->At(0)); 15 const ByteArray& byte_array = ByteArray::CheckedHandle(arguments->At(0));
15 const Smi& length = Smi::Handle(Smi::New(byte_array.Length())); 16 const Smi& length = Smi::Handle(Smi::New(byte_array.Length()));
16 arguments->SetReturn(length); 17 arguments->SetReturn(length);
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 intptr_t dst_start_value = dst_start.Value(); 54 intptr_t dst_start_value = dst_start.Value();
54 if (length_value < 0) { 55 if (length_value < 0) {
55 GrowableArray<const Object*> args; 56 GrowableArray<const Object*> args;
56 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args); 57 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args);
57 } 58 }
58 RangeCheck(src, src_start_value, length_value); 59 RangeCheck(src, src_start_value, length_value);
59 RangeCheck(dst, dst_start_value, length_value); 60 RangeCheck(dst, dst_start_value, length_value);
60 ByteArray::Copy(dst, dst_start_value, src, src_start_value, length_value); 61 ByteArray::Copy(dst, dst_start_value, src, src_start_value, length_value);
61 } 62 }
62 63
64 #define GETTER_ARGUMENTS(ArrayT, ValueT) \
65 GET_NATIVE_ARGUMENT(ArrayT, array, arguments->At(0)); \
66 GET_NATIVE_ARGUMENT(Smi, index, arguments->At(1)); \
67 RangeCheck(array, index.Value(), sizeof(ValueT));
63 68
64 #define GETTER(ArrayT, ObjectT, ValueT) \ 69
70 #define SETTER_ARGUMENTS(ArrayT, ObjectT, ValueT) \
65 GET_NATIVE_ARGUMENT(ArrayT, array, arguments->At(0)); \ 71 GET_NATIVE_ARGUMENT(ArrayT, array, arguments->At(0)); \
66 GET_NATIVE_ARGUMENT(Smi, index, arguments->At(1)); \ 72 GET_NATIVE_ARGUMENT(Smi, index, arguments->At(1)); \
67 RangeCheck(array, index.Value(), sizeof(ValueT)); \ 73 RangeCheck(array, index.Value(), sizeof(ValueT)); \
74 GET_NATIVE_ARGUMENT(ObjectT, integer_value, arguments->At(2));
75
76
77 #define GETTER(ArrayT, ObjectT, ValueT) \
78 GETTER_ARGUMENTS(ArrayT, ValueT); \
68 ValueT result = array.UnalignedAt<ValueT>(index.Value()); \ 79 ValueT result = array.UnalignedAt<ValueT>(index.Value()); \
69 arguments->SetReturn(ObjectT::Handle(ObjectT::New(result))); 80 arguments->SetReturn(ObjectT::Handle(ObjectT::New(result)));
70 81
71 82
72 #define SETTER(ArrayT, ObjectT, Getter, ValueT) \ 83 #define SETTER(ArrayT, ObjectT, Getter, ValueT) \
73 GET_NATIVE_ARGUMENT(ArrayT, array, arguments->At(0)); \ 84 SETTER_ARGUMENTS(ArrayT, ObjectT, ValueT); \
74 GET_NATIVE_ARGUMENT(Smi, index, arguments->At(1)); \ 85 array.SetUnalignedAt<ValueT>(index.Value(), integer_value.Getter());
75 RangeCheck(array, index.Value(), sizeof(ValueT)); \ 86
76 GET_NATIVE_ARGUMENT(ObjectT, value, arguments->At(2)); \ 87
77 array.SetUnalignedAt<ValueT>(index.Value(), value.Getter()); 88 #define GETTER_UINT64(ArrayT) \
89 GETTER_ARGUMENTS(ArrayT, uint64_t); \
90 uint64_t value = array.UnalignedAt<uint64_t>(index.Value()); \
91 Integer& result = Integer::Handle(); \
92 if (value > static_cast<uint64_t>(Mint::kMaxValue)) { \
93 result = BigintOperations::NewFromUint64(value); \
94 } else if (value > static_cast<uint64_t>(Smi::kMaxValue)) { \
95 result = Mint::New(value); \
96 } else { \
97 result = Smi::New(value); \
98 } \
99 arguments->SetReturn(result);
100
101
102 #define SETTER_UINT64(ArrayT) \
103 SETTER_ARGUMENTS(ArrayT, Integer, uint64_t); \
104 uint64_t value; \
105 if (integer_value.IsBigint()) { \
106 Bigint& bigint_value = Bigint::Handle(); \
107 bigint_value ^= integer_value.raw(); \
108 ASSERT(BigintOperations::FitsIntoUint64(bigint_value)); \
109 value = BigintOperations::AbsToUint64(bigint_value); \
110 } else { \
111 ASSERT(integer_value.IsMint() || integer_value.IsSmi()); \
112 value = integer_value.AsInt64Value(); \
113 } \
114 array.SetUnalignedAt<uint64_t>(index.Value(), value);
78 115
79 116
80 DEFINE_NATIVE_ENTRY(InternalByteArray_getInt8, 2) { 117 DEFINE_NATIVE_ENTRY(InternalByteArray_getInt8, 2) {
81 GETTER(InternalByteArray, Smi, int8_t); 118 GETTER(InternalByteArray, Smi, int8_t);
82 } 119 }
83 120
84 121
85 DEFINE_NATIVE_ENTRY(InternalByteArray_setInt8, 3) { 122 DEFINE_NATIVE_ENTRY(InternalByteArray_setInt8, 3) {
86 SETTER(InternalByteArray, Smi, Value, int8_t); 123 SETTER(InternalByteArray, Smi, Value, int8_t);
87 } 124 }
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 GETTER(InternalByteArray, Integer, int64_t); 178 GETTER(InternalByteArray, Integer, int64_t);
142 } 179 }
143 180
144 181
145 DEFINE_NATIVE_ENTRY(InternalByteArray_setInt64, 3) { 182 DEFINE_NATIVE_ENTRY(InternalByteArray_setInt64, 3) {
146 SETTER(InternalByteArray, Integer, AsInt64Value, int64_t); 183 SETTER(InternalByteArray, Integer, AsInt64Value, int64_t);
147 } 184 }
148 185
149 186
150 DEFINE_NATIVE_ENTRY(InternalByteArray_getUint64, 2) { 187 DEFINE_NATIVE_ENTRY(InternalByteArray_getUint64, 2) {
151 UNIMPLEMENTED(); // TODO(cshapiro): need getter implementation. 188 GETTER_UINT64(InternalByteArray);
152 } 189 }
153 190
154 191
155 DEFINE_NATIVE_ENTRY(InternalByteArray_setUint64, 3) { 192 DEFINE_NATIVE_ENTRY(InternalByteArray_setUint64, 3) {
156 UNIMPLEMENTED(); // TODO(cshapiro): need setter implementation. 193 SETTER_UINT64(InternalByteArray);
157 } 194 }
158 195
159 196
160 DEFINE_NATIVE_ENTRY(InternalByteArray_getFloat32, 2) { 197 DEFINE_NATIVE_ENTRY(InternalByteArray_getFloat32, 2) {
161 GETTER(InternalByteArray, Double, float); 198 GETTER(InternalByteArray, Double, float);
162 } 199 }
163 200
164 201
165 DEFINE_NATIVE_ENTRY(InternalByteArray_setFloat32, 3) { 202 DEFINE_NATIVE_ENTRY(InternalByteArray_setFloat32, 3) {
166 SETTER(InternalByteArray, Double, value, float); 203 SETTER(InternalByteArray, Double, value, float);
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 GETTER(ExternalByteArray, Integer, int64_t); 278 GETTER(ExternalByteArray, Integer, int64_t);
242 } 279 }
243 280
244 281
245 DEFINE_NATIVE_ENTRY(ExternalByteArray_setInt64, 3) { 282 DEFINE_NATIVE_ENTRY(ExternalByteArray_setInt64, 3) {
246 SETTER(ExternalByteArray, Integer, AsInt64Value, int64_t); 283 SETTER(ExternalByteArray, Integer, AsInt64Value, int64_t);
247 } 284 }
248 285
249 286
250 DEFINE_NATIVE_ENTRY(ExternalByteArray_getUint64, 2) { 287 DEFINE_NATIVE_ENTRY(ExternalByteArray_getUint64, 2) {
251 UNIMPLEMENTED(); // TODO(cshapiro): need getter implementation. 288 GETTER_UINT64(ExternalByteArray);
252 } 289 }
253 290
254 291
255 DEFINE_NATIVE_ENTRY(ExternalByteArray_setUint64, 3) { 292 DEFINE_NATIVE_ENTRY(ExternalByteArray_setUint64, 3) {
256 UNIMPLEMENTED(); // TODO(cshapiro): need setter implementation. 293 SETTER_UINT64(ExternalByteArray);
257 } 294 }
258 295
259 296
260 DEFINE_NATIVE_ENTRY(ExternalByteArray_getFloat32, 2) { 297 DEFINE_NATIVE_ENTRY(ExternalByteArray_getFloat32, 2) {
261 GETTER(ExternalByteArray, Double, float); 298 GETTER(ExternalByteArray, Double, float);
262 } 299 }
263 300
264 301
265 DEFINE_NATIVE_ENTRY(ExternalByteArray_setFloat32, 3) { 302 DEFINE_NATIVE_ENTRY(ExternalByteArray_setFloat32, 3) {
266 SETTER(ExternalByteArray, Double, value, float); 303 SETTER(ExternalByteArray, Double, value, float);
267 } 304 }
268 305
269 306
270 DEFINE_NATIVE_ENTRY(ExternalByteArray_getFloat64, 2) { 307 DEFINE_NATIVE_ENTRY(ExternalByteArray_getFloat64, 2) {
271 GETTER(ExternalByteArray, Double, double); 308 GETTER(ExternalByteArray, Double, double);
272 } 309 }
273 310
274 311
275 DEFINE_NATIVE_ENTRY(ExternalByteArray_setFloat64, 3) { 312 DEFINE_NATIVE_ENTRY(ExternalByteArray_setFloat64, 3) {
276 SETTER(ExternalByteArray, Double, value, double); 313 SETTER(ExternalByteArray, Double, value, double);
277 } 314 }
278 315
279 } // namespace dart 316 } // namespace dart
OLDNEW
« no previous file with comments | « no previous file | runtime/lib/byte_array.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698