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

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

Issue 12544015: Implement 'dart:typeddata' directly in the VM instead of using 'dart:scalarlist' (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 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/typeddata.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) 2013, 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 "vm/bootstrap_natives.h"
6
7 #include "include/dart_api.h"
8
9 #include "vm/bigint_operations.h"
10 #include "vm/exceptions.h"
11 #include "vm/native_entry.h"
12 #include "vm/object.h"
13
14 namespace dart {
15
16 // TypedData.
17
18 // Checks to see if offsetInBytes is in the range.
19 static bool RangeCheck(intptr_t offsetInBytes, intptr_t lengthInBytes) {
20 return ((offsetInBytes >= 0) &&
21 (lengthInBytes > 0) &&
22 (offsetInBytes < lengthInBytes));
23 }
24
25
26 // Checks to see if a length will not result in an OOM error.
27 static void LengthCheck(intptr_t len, intptr_t max) {
28 ASSERT(len >= 0);
29 if (len > max) {
30 const String& error = String::Handle(String::NewFormatted(
31 "insufficient memory to allocate a TypedData object of length (%"Pd")",
32 len));
33 const Array& args = Array::Handle(Array::New(1));
34 args.SetAt(0, error);
35 Exceptions::ThrowByType(Exceptions::kOutOfMemory, args);
36 }
37 }
38
39
40 static void PeerFinalizer(Dart_Handle handle, void* peer) {
41 Dart_DeletePersistentHandle(handle);
42 OS::AlignedFree(peer);
43 }
44
45
46 DEFINE_NATIVE_ENTRY(TypedData_length, 1) {
47 GET_NON_NULL_NATIVE_ARGUMENT(Instance, instance, arguments->NativeArgAt(0));
48 if (instance.IsTypedData()) {
49 const TypedData& array = TypedData::Cast(instance);
50 return Smi::New(array.Length());
51 }
52 if (instance.IsExternalTypedData()) {
53 const ExternalTypedData& array = ExternalTypedData::Cast(instance);
54 return Smi::New(array.Length());
55 }
56 const String& error = String::Handle(String::NewFormatted(
57 "Expected a TypedData object but found %s", instance.ToCString()));
58 const Array& args = Array::Handle(Array::New(1));
59 args.SetAt(0, error);
60 Exceptions::ThrowByType(Exceptions::kArgument, args);
61 return Integer::null();
62 }
63
64
65 // We check the length parameter against a possible maximum length for the
66 // array based on available physical addressable memory on the system. The
67 // maximum possible length is a scaled value of kSmiMax which is set up based
68 // on whether the underlying architecture is 32-bit or 64-bit.
69 #define TYPED_DATA_NEW(name) \
70 DEFINE_NATIVE_ENTRY(TypedData_##name##_new, 1) { \
71 GET_NON_NULL_NATIVE_ARGUMENT(Smi, length, arguments->NativeArgAt(0)); \
72 intptr_t cid = kTypedData##name##Cid; \
73 intptr_t len = length.Value(); \
74 intptr_t max = (kSmiMax / TypedData::ElementSizeInBytes(cid)); \
75 LengthCheck(len, max); \
76 return TypedData::New(cid, len); \
77 } \
78
79
80 // We check the length parameter against a possible maximum length for the
81 // array based on available physical addressable memory on the system. The
82 // maximum possible length is a scaled value of kSmiMax which is set up based
83 // on whether the underlying architecture is 32-bit or 64-bit.
84 #define EXT_TYPED_DATA_NEW(name) \
85 DEFINE_NATIVE_ENTRY(ExternalTypedData_##name##_new, 1) { \
86 const int kAlignment = 16; \
87 GET_NON_NULL_NATIVE_ARGUMENT(Smi, length, arguments->NativeArgAt(0)); \
88 intptr_t cid = kExternalTypedData##name##Cid; \
89 intptr_t len = length.Value(); \
90 intptr_t max = (kSmiMax / ExternalTypedData::ElementSizeInBytes(cid)); \
91 LengthCheck(len, max); \
92 intptr_t len_bytes = len * ExternalTypedData::ElementSizeInBytes(cid); \
93 uint8_t* data = OS::AllocateAlignedArray<uint8_t>(len_bytes, kAlignment); \
94 const ExternalTypedData& obj = \
95 ExternalTypedData::Handle(ExternalTypedData::New(cid, data, len)); \
96 obj.AddFinalizer(data, PeerFinalizer); \
97 return obj.raw(); \
98 } \
99
100
101 #define TYPED_DATA_NEW_NATIVE(name) \
102 TYPED_DATA_NEW(name) \
103 EXT_TYPED_DATA_NEW(name) \
104
105
106 CLASS_LIST_TYPED_DATA(TYPED_DATA_NEW_NATIVE)
107
108
109 #define TYPED_DATA_GETTER(getter, object) \
110 DEFINE_NATIVE_ENTRY(TypedData_##getter, 2) { \
111 GET_NON_NULL_NATIVE_ARGUMENT(Instance, instance, arguments->NativeArgAt(0)); \
112 GET_NON_NULL_NATIVE_ARGUMENT(Smi, index, arguments->NativeArgAt(1)); \
113 if (instance.IsTypedData()) { \
114 const TypedData& array = TypedData::Cast(instance); \
115 intptr_t offsetInBytes = index.Value() * array.ElementSizeInBytes(); \
116 ASSERT(RangeCheck(offsetInBytes, array.LengthInBytes())); \
117 return object::New(array.getter(offsetInBytes)); \
118 } \
119 if (instance.IsExternalTypedData()) { \
120 const ExternalTypedData& array = ExternalTypedData::Cast(instance); \
121 intptr_t offsetInBytes = index.Value() * array.ElementSizeInBytes(); \
122 ASSERT(RangeCheck(offsetInBytes, array.LengthInBytes())); \
123 return object::New(array.getter(offsetInBytes)); \
124 } \
125 const String& error = String::Handle(String::NewFormatted( \
126 "Expected a TypedData object but found %s", instance.ToCString())); \
127 const Array& args = Array::Handle(Array::New(1)); \
128 args.SetAt(0, error); \
129 Exceptions::ThrowByType(Exceptions::kArgument, args); \
130 return object::null(); \
131 } \
132
133
134 #define TYPED_DATA_SETTER(setter, object, get_object_value) \
135 DEFINE_NATIVE_ENTRY(TypedData_##setter, 3) { \
136 GET_NON_NULL_NATIVE_ARGUMENT(Instance, instance, arguments->NativeArgAt(0)); \
137 GET_NON_NULL_NATIVE_ARGUMENT(Smi, index, arguments->NativeArgAt(1)); \
138 GET_NON_NULL_NATIVE_ARGUMENT(object, value, arguments->NativeArgAt(2)); \
139 if (instance.IsTypedData()) { \
140 const TypedData& array = TypedData::Cast(instance); \
141 intptr_t offsetInBytes = index.Value() * array.ElementSizeInBytes(); \
142 ASSERT(RangeCheck(offsetInBytes, array.LengthInBytes())); \
143 array.setter(offsetInBytes, value.get_object_value()); \
144 } else if (instance.IsExternalTypedData()) { \
145 const ExternalTypedData& array = ExternalTypedData::Cast(instance); \
146 intptr_t offsetInBytes = index.Value() * array.ElementSizeInBytes(); \
147 ASSERT(RangeCheck(offsetInBytes, array.LengthInBytes())); \
148 array.setter(offsetInBytes, value.get_object_value()); \
149 } else { \
150 const String& error = String::Handle(String::NewFormatted( \
151 "Expected a TypedData object but found %s", instance.ToCString())); \
152 const Array& args = Array::Handle(Array::New(1)); \
153 args.SetAt(0, error); \
154 Exceptions::ThrowByType(Exceptions::kArgument, args); \
155 } \
156 return Object::null(); \
157 }
158
159
160 #define TYPED_DATA_UINT64_GETTER(getter, object) \
161 DEFINE_NATIVE_ENTRY(TypedData_##getter, 2) { \
162 GET_NON_NULL_NATIVE_ARGUMENT(Instance, instance, arguments->NativeArgAt(0)); \
163 GET_NON_NULL_NATIVE_ARGUMENT(Smi, index, arguments->NativeArgAt(1)); \
164 uint64_t value = 0; \
165 if (instance.IsTypedData()) { \
166 const TypedData& array = TypedData::Cast(instance); \
167 intptr_t offsetInBytes = index.Value() * array.ElementSizeInBytes(); \
168 ASSERT(RangeCheck(offsetInBytes, array.LengthInBytes())); \
169 value = array.getter(offsetInBytes); \
170 } else if (instance.IsExternalTypedData()) { \
171 const ExternalTypedData& array = ExternalTypedData::Cast(instance); \
172 intptr_t offsetInBytes = index.Value() * array.ElementSizeInBytes(); \
173 ASSERT(RangeCheck(offsetInBytes, array.LengthInBytes())); \
174 value = array.getter(offsetInBytes); \
175 } else { \
176 const String& error = String::Handle(String::NewFormatted( \
177 "Expected a TypedData object but found %s", instance.ToCString())); \
178 const Array& args = Array::Handle(Array::New(1)); \
179 args.SetAt(0, error); \
180 Exceptions::ThrowByType(Exceptions::kArgument, args); \
181 } \
182 Integer& result = Integer::Handle(); \
183 if (value > static_cast<uint64_t>(Mint::kMaxValue)) { \
184 result = BigintOperations::NewFromUint64(value); \
185 } else if (value > static_cast<uint64_t>(Smi::kMaxValue)) { \
186 result = Mint::New(value); \
187 } else { \
188 result = Smi::New(value); \
189 } \
190 return result.raw(); \
191 } \
192
193
194 // TODO(asiva): Consider truncating the bigint value if it does not fit into
195 // a uint64_t value (see ASSERT(BigintOperations::FitsIntoUint64(bigint))).
196 #define TYPED_DATA_UINT64_SETTER(setter, object) \
197 DEFINE_NATIVE_ENTRY(TypedData_##setter, 3) { \
198 GET_NON_NULL_NATIVE_ARGUMENT(Instance, instance, arguments->NativeArgAt(0)); \
199 GET_NON_NULL_NATIVE_ARGUMENT(Smi, index, arguments->NativeArgAt(1)); \
200 GET_NON_NULL_NATIVE_ARGUMENT(object, value, arguments->NativeArgAt(2)); \
201 uint64_t object_value; \
202 if (value.IsBigint()) { \
203 const Bigint& bigint = Bigint::Cast(value); \
204 ASSERT(BigintOperations::FitsIntoUint64(bigint)); \
205 object_value = BigintOperations::AbsToUint64(bigint); \
206 } else { \
207 ASSERT(value.IsMint() || value.IsSmi()); \
208 object_value = value.AsInt64Value(); \
209 } \
210 if (instance.IsTypedData()) { \
211 const TypedData& array = TypedData::Cast(instance); \
212 intptr_t offsetInBytes = index.Value() * array.ElementSizeInBytes(); \
213 ASSERT(RangeCheck(offsetInBytes, array.LengthInBytes())); \
214 array.setter(offsetInBytes, object_value); \
215 } else if (instance.IsExternalTypedData()) { \
216 const ExternalTypedData& array = ExternalTypedData::Cast(instance); \
217 intptr_t offsetInBytes = index.Value() * array.ElementSizeInBytes(); \
218 ASSERT(RangeCheck(offsetInBytes, array.LengthInBytes())); \
219 array.setter(offsetInBytes, object_value); \
220 } else { \
221 const String& error = String::Handle(String::NewFormatted( \
222 "Expected a TypedData object but found %s", instance.ToCString())); \
223 const Array& args = Array::Handle(Array::New(1)); \
224 args.SetAt(0, error); \
225 Exceptions::ThrowByType(Exceptions::kArgument, args); \
226 } \
227 return Object::null(); \
228 }
229
230
231 #define TYPED_DATA_NATIVES(name, getter, setter, object, get_object_value) \
232 TYPED_DATA_GETTER(getter, object) \
233 TYPED_DATA_SETTER(setter, object, get_object_value) \
234
235
236 #define TYPED_DATA_UINT64_NATIVES(name, getter, setter, object) \
237 TYPED_DATA_UINT64_GETTER(getter, object) \
238 TYPED_DATA_UINT64_SETTER(setter, object) \
239
240
241 TYPED_DATA_NATIVES(Int8Array, GetInt8, SetInt8, Smi, Value)
242 TYPED_DATA_NATIVES(Uint8Array, GetUint8, SetUint8, Smi, Value)
243 TYPED_DATA_NATIVES(Int16Array, GetInt16, SetInt16, Smi, Value)
244 TYPED_DATA_NATIVES(Uint16Array, GetUint16, SetUint16, Smi, Value)
245 TYPED_DATA_NATIVES(Int32Array, GetInt32, SetInt32, Integer, AsInt64Value)
246 TYPED_DATA_NATIVES(Uint32Array, GetUint32, SetUint32, Integer, AsInt64Value)
247 TYPED_DATA_NATIVES(Int64Array, GetInt64, SetInt64, Integer, AsInt64Value)
248 TYPED_DATA_UINT64_NATIVES(Uint64Array, GetUint64, SetUint64, Integer)
249 TYPED_DATA_NATIVES(Float32Array, GetFloat32, SetFloat32, Double, value)
250 TYPED_DATA_NATIVES(Float64Array, GetFloat64, SetFloat64, Double, value)
251
252 } // namespace dart
OLDNEW
« no previous file with comments | « no previous file | runtime/lib/typeddata.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698