Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
|
siva
2012/01/20 22:51:04
2012 here and other files.
| |
| 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 "vm/assembler.h" | |
|
siva
2012/01/20 22:51:04
Why does assembler.h have to included here?
cshapiro
2012/01/24 02:14:23
True. In fact, both assembler.h and bigint_operat
| |
| 8 #include "vm/bigint_operations.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(ByteArray_getLength, 1) { | |
| 16 const ByteArray& byte_array = ByteArray::CheckedHandle(arguments->At(0)); | |
| 17 const Smi& length = Smi::Handle(Smi::New(byte_array.Length())); | |
| 18 arguments->SetReturn(length); | |
| 19 } | |
| 20 | |
| 21 | |
| 22 DEFINE_NATIVE_ENTRY(InternalByteArray_allocate, 1) { | |
| 23 GET_NATIVE_ARGUMENT(Smi, length, arguments->At(0)); | |
| 24 if (length.Value() < 0) { | |
| 25 GrowableArray<const Object*> args; | |
| 26 args.Add(&length); | |
| 27 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args); | |
| 28 } | |
| 29 const InternalByteArray& new_array = | |
| 30 InternalByteArray::Handle(InternalByteArray::New(length.Value())); | |
| 31 arguments->SetReturn(new_array); | |
| 32 } | |
| 33 | |
| 34 | |
| 35 template<typename T> | |
| 36 static const T& GetArray(NativeArguments* arguments) { | |
| 37 const T& byte_array = T::CheckedHandle(arguments->At(0)); | |
| 38 if (!byte_array.IsByteArray()) { | |
|
siva
2012/01/20 22:51:04
Checked handle will assert out if it is not a byte
cshapiro
2012/01/24 02:14:23
Fixed, the hard way.
| |
| 39 GrowableArray<const Object*> args; | |
| 40 args.Add(&byte_array); | |
| 41 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args); | |
| 42 } | |
| 43 return byte_array; | |
| 44 } | |
| 45 | |
| 46 | |
| 47 static Smi& GetIndex(NativeArguments* arguments) { | |
|
siva
2012/01/20 22:51:04
Our normal style has been to return raw objects fr
| |
| 48 const Instance& index_instance = Instance::CheckedHandle(arguments->At(1)); | |
| 49 if (!index_instance.IsSmi()) { | |
| 50 GrowableArray<const Object*> args; | |
| 51 args.Add(&index_instance); | |
| 52 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args); | |
| 53 } | |
| 54 Smi& index = Smi::Handle(); | |
| 55 index ^= index_instance.raw(); | |
| 56 return index; | |
|
siva
2012/01/20 22:51:04
just return reinterpret_cast<RawSmi*>(index_instan
| |
| 57 } | |
| 58 | |
| 59 | |
| 60 template<typename T> | |
| 61 static void RangeCheck(const ByteArray& array, const Smi& index) { | |
| 62 intptr_t num_bytes = sizeof(T); | |
| 63 if ((index.Value() < 0) || ((index.Value() + num_bytes) > array.Length())) { | |
| 64 GrowableArray<const Object*> arguments; | |
| 65 arguments.Add(&index); | |
| 66 Exceptions::ThrowByType(Exceptions::kIndexOutOfRange, arguments); | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 | |
| 71 template<typename T> | |
| 72 static T GetSmiValue(NativeArguments* arguments) { | |
| 73 const Instance& value_instance = Instance::CheckedHandle(arguments->At(2)); | |
| 74 if (value_instance.IsSmi()) { | |
|
siva
2012/01/20 22:51:04
shouldn't this be
if (!value_instance.IsSmi()) {
| |
| 75 GrowableArray<const Object*> args; | |
| 76 args.Add(&value_instance); | |
| 77 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args); | |
| 78 } | |
| 79 Smi& value = Smi::Handle(); | |
| 80 value ^= value_instance.raw(); | |
| 81 return value.Value(); | |
| 82 } | |
| 83 | |
| 84 | |
| 85 template<typename T> | |
| 86 static T GetIntegerValue(NativeArguments* arguments) { | |
| 87 const Instance& value_instance = Instance::CheckedHandle(arguments->At(2)); | |
| 88 if (value_instance.IsInteger()) { | |
|
siva
2012/01/20 22:51:04
if (!value_instance.IsInteger()) {
...
...
}
| |
| 89 GrowableArray<const Object*> args; | |
| 90 args.Add(&value_instance); | |
| 91 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args); | |
| 92 } | |
| 93 Integer& value = Integer::Handle(); | |
| 94 value ^= value_instance.raw(); | |
| 95 return value.AsInt64Value(); | |
| 96 } | |
| 97 | |
| 98 | |
| 99 template<typename T> | |
| 100 static T GetDoubleValue(NativeArguments* arguments) { | |
| 101 const Instance& value_instance = Instance::CheckedHandle(arguments->At(2)); | |
| 102 if (value_instance.IsDouble()) { | |
|
siva
2012/01/20 22:51:04
if (!value_instance.IsDouble()) {
...
...
}
| |
| 103 GrowableArray<const Object*> args; | |
| 104 args.Add(&value_instance); | |
| 105 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args); | |
| 106 } | |
| 107 Double& value = Double::Handle(); | |
| 108 value ^= value_instance.raw(); | |
| 109 return value.value(); | |
| 110 } | |
| 111 | |
| 112 | |
| 113 template<typename ArrayType, typename PrimitiveType, typename ObjectType> | |
| 114 static void GetIndexed(NativeArguments* arguments) { | |
| 115 const InternalByteArray& array = GetArray<InternalByteArray>(arguments); | |
|
siva
2012/01/20 22:51:04
const ArrayType& array = GetArray<ArrayType>(argum
| |
| 116 Smi& index = GetIndex(arguments); | |
| 117 RangeCheck<PrimitiveType>(array, index); | |
| 118 PrimitiveType result = array.UnalignedAt<PrimitiveType>(index.Value()); | |
| 119 arguments->SetReturn(ObjectType::Handle(ObjectType::New(result))); | |
| 120 } | |
| 121 | |
| 122 | |
| 123 template<typename ArrayType, typename PrimitiveType, typename Getter> | |
| 124 static void SetIndexed(NativeArguments* arguments, Getter getter) { | |
| 125 const InternalByteArray& array = GetArray<InternalByteArray>(arguments); | |
|
siva
2012/01/20 22:51:04
Ditto here?
| |
| 126 Smi& index = GetIndex(arguments); | |
| 127 RangeCheck<PrimitiveType>(array, index); | |
| 128 PrimitiveType value = getter(arguments); | |
| 129 array.SetUnalignedAt<PrimitiveType>(index.Value(), value); | |
| 130 } | |
| 131 | |
| 132 | |
| 133 DEFINE_NATIVE_ENTRY(InternalByteArray_getInt8, 2) { | |
| 134 const InternalByteArray& array = GetArray<InternalByteArray>(arguments); | |
| 135 Smi& index = GetIndex(arguments); | |
| 136 RangeCheck<int8_t>(array, index); | |
| 137 int8_t result = array.UnalignedAt<int8_t>(index.Value()); | |
| 138 arguments->SetReturn(Smi::Handle(Smi::New(result))); | |
| 139 } | |
| 140 | |
| 141 | |
| 142 DEFINE_NATIVE_ENTRY(InternalByteArray_setInt8, 3) { | |
| 143 const InternalByteArray& array = GetArray<InternalByteArray>(arguments); | |
| 144 const Smi& index = GetIndex(arguments); | |
| 145 RangeCheck<int8_t>(array, index); | |
| 146 int8_t value = GetSmiValue<int8_t>(arguments); | |
| 147 array.SetUnalignedAt<int8_t>(index.Value(), value); | |
| 148 } | |
| 149 | |
| 150 | |
| 151 DEFINE_NATIVE_ENTRY(InternalByteArray_getUint8, 2) { | |
| 152 GetIndexed<InternalByteArray, uint8_t, Smi>(arguments); | |
| 153 } | |
| 154 | |
| 155 | |
| 156 DEFINE_NATIVE_ENTRY(InternalByteArray_setUint8, 3) { | |
| 157 SetIndexed<InternalByteArray, uint8_t>(arguments, GetSmiValue<uint8_t>); | |
|
siva
2012/01/20 22:51:04
The template instantiation here takes only two typ
| |
| 158 } | |
| 159 | |
| 160 | |
| 161 DEFINE_NATIVE_ENTRY(InternalByteArray_getInt16, 2) { | |
| 162 GetIndexed<InternalByteArray, int16_t, Smi>(arguments); | |
| 163 } | |
| 164 | |
| 165 | |
| 166 DEFINE_NATIVE_ENTRY(InternalByteArray_setInt16, 3) { | |
| 167 SetIndexed<InternalByteArray, int16_t>(arguments, GetSmiValue<int16_t>); | |
| 168 } | |
| 169 | |
| 170 | |
| 171 DEFINE_NATIVE_ENTRY(InternalByteArray_getUint16, 2) { | |
| 172 GetIndexed<InternalByteArray, uint16_t, Smi>(arguments); | |
| 173 } | |
| 174 | |
| 175 | |
| 176 DEFINE_NATIVE_ENTRY(InternalByteArray_setUint16, 3) { | |
| 177 SetIndexed<InternalByteArray, uint16_t>(arguments, GetSmiValue<uint16_t>); | |
| 178 } | |
| 179 | |
| 180 | |
| 181 DEFINE_NATIVE_ENTRY(InternalByteArray_getInt32, 2) { | |
| 182 GetIndexed<InternalByteArray, int32_t, Integer>(arguments); | |
| 183 } | |
| 184 | |
| 185 | |
| 186 DEFINE_NATIVE_ENTRY(InternalByteArray_setInt32, 3) { | |
| 187 SetIndexed<InternalByteArray, int32_t>(arguments, GetSmiValue<int32_t>); | |
| 188 } | |
| 189 | |
| 190 | |
| 191 DEFINE_NATIVE_ENTRY(InternalByteArray_getUint32, 2) { | |
| 192 GetIndexed<InternalByteArray, uint32_t, Integer>(arguments); | |
| 193 } | |
| 194 | |
| 195 | |
| 196 DEFINE_NATIVE_ENTRY(InternalByteArray_setUint32, 3) { | |
| 197 SetIndexed<InternalByteArray, uint32_t>(arguments, GetIntegerValue<uint32_t>); | |
| 198 } | |
| 199 | |
| 200 | |
| 201 DEFINE_NATIVE_ENTRY(InternalByteArray_getInt64, 2) { | |
| 202 GetIndexed<InternalByteArray, int64_t, Integer>(arguments); | |
| 203 } | |
| 204 | |
| 205 | |
| 206 DEFINE_NATIVE_ENTRY(InternalByteArray_setInt64, 3) { | |
| 207 SetIndexed<InternalByteArray, int64_t>(arguments, GetIntegerValue<int64_t>); | |
| 208 } | |
| 209 | |
| 210 | |
| 211 DEFINE_NATIVE_ENTRY(InternalByteArray_getUint64, 2) { | |
| 212 UNIMPLEMENTED(); // TODO(cshapiro): need getter implementation. | |
| 213 } | |
| 214 | |
| 215 | |
| 216 DEFINE_NATIVE_ENTRY(InternalByteArray_setUint64, 3) { | |
| 217 UNIMPLEMENTED(); // TODO(cshapiro): need setter implementation. | |
| 218 } | |
| 219 | |
| 220 | |
| 221 DEFINE_NATIVE_ENTRY(InternalByteArray_getFloat32, 2) { | |
| 222 GetIndexed<InternalByteArray, float, Double>(arguments); | |
| 223 } | |
| 224 | |
| 225 | |
| 226 DEFINE_NATIVE_ENTRY(InternalByteArray_setFloat32, 3) { | |
| 227 SetIndexed<InternalByteArray, float>(arguments, GetDoubleValue<float>); | |
| 228 } | |
| 229 | |
| 230 | |
| 231 DEFINE_NATIVE_ENTRY(InternalByteArray_getFloat64, 2) { | |
| 232 GetIndexed<InternalByteArray, double, Double>(arguments); | |
| 233 } | |
| 234 | |
| 235 | |
| 236 DEFINE_NATIVE_ENTRY(InternalByteArray_setFloat64, 3) { | |
| 237 SetIndexed<InternalByteArray, double>(arguments, GetDoubleValue<double>); | |
| 238 } | |
| 239 | |
| 240 | |
| 241 DEFINE_NATIVE_ENTRY(ExternalByteArray_getInt8, 2) { | |
| 242 GetIndexed<ExternalByteArray, int8_t, Smi>(arguments); | |
| 243 } | |
| 244 | |
| 245 | |
| 246 DEFINE_NATIVE_ENTRY(ExternalByteArray_setInt8, 3) { | |
| 247 SetIndexed<ExternalByteArray, int8_t>(arguments, GetSmiValue<int8_t>); | |
| 248 } | |
| 249 | |
| 250 | |
| 251 DEFINE_NATIVE_ENTRY(ExternalByteArray_getUint8, 2) { | |
| 252 GetIndexed<ExternalByteArray, uint8_t, Smi>(arguments); | |
|
siva
2012/01/20 22:51:04
I don't understand how these worked as GetIndexed
| |
| 253 } | |
| 254 | |
| 255 | |
| 256 DEFINE_NATIVE_ENTRY(ExternalByteArray_setUint8, 3) { | |
| 257 SetIndexed<ExternalByteArray, uint8_t>(arguments, GetSmiValue<uint8_t>); | |
| 258 } | |
| 259 | |
| 260 | |
| 261 DEFINE_NATIVE_ENTRY(ExternalByteArray_getInt16, 2) { | |
| 262 GetIndexed<ExternalByteArray, int16_t, Smi>(arguments); | |
| 263 } | |
| 264 | |
| 265 | |
| 266 DEFINE_NATIVE_ENTRY(ExternalByteArray_setInt16, 3) { | |
| 267 SetIndexed<ExternalByteArray, int16_t>(arguments, GetSmiValue<int16_t>); | |
| 268 } | |
| 269 | |
| 270 | |
| 271 DEFINE_NATIVE_ENTRY(ExternalByteArray_getUint16, 2) { | |
| 272 GetIndexed<ExternalByteArray, uint16_t, Smi>(arguments); | |
| 273 } | |
| 274 | |
| 275 | |
| 276 DEFINE_NATIVE_ENTRY(ExternalByteArray_setUint16, 3) { | |
| 277 SetIndexed<ExternalByteArray, uint16_t>(arguments, GetSmiValue<uint16_t>); | |
| 278 } | |
| 279 | |
| 280 | |
| 281 DEFINE_NATIVE_ENTRY(ExternalByteArray_getInt32, 2) { | |
| 282 GetIndexed<ExternalByteArray, int32_t, Integer>(arguments); | |
| 283 } | |
| 284 | |
| 285 | |
| 286 DEFINE_NATIVE_ENTRY(ExternalByteArray_setInt32, 3) { | |
| 287 SetIndexed<ExternalByteArray, int32_t>(arguments, GetIntegerValue<int32_t>); | |
| 288 } | |
| 289 | |
| 290 | |
| 291 DEFINE_NATIVE_ENTRY(ExternalByteArray_getUint32, 2) { | |
| 292 GetIndexed<ExternalByteArray, uint32_t, Integer>(arguments); | |
| 293 } | |
| 294 | |
| 295 | |
| 296 DEFINE_NATIVE_ENTRY(ExternalByteArray_setUint32, 3) { | |
| 297 SetIndexed<ExternalByteArray, uint32_t>(arguments, GetIntegerValue<uint32_t>); | |
| 298 } | |
| 299 | |
| 300 | |
| 301 DEFINE_NATIVE_ENTRY(ExternalByteArray_getInt64, 2) { | |
| 302 GetIndexed<ExternalByteArray, int64_t, Integer>(arguments); | |
| 303 } | |
| 304 | |
| 305 | |
| 306 DEFINE_NATIVE_ENTRY(ExternalByteArray_setInt64, 3) { | |
| 307 SetIndexed<ExternalByteArray, int64_t>(arguments, GetIntegerValue<int64_t>); | |
| 308 } | |
| 309 | |
| 310 | |
| 311 DEFINE_NATIVE_ENTRY(ExternalByteArray_getUint64, 2) { | |
| 312 UNIMPLEMENTED(); // TODO(cshapiro): need getter implementation. | |
| 313 } | |
| 314 | |
| 315 | |
| 316 DEFINE_NATIVE_ENTRY(ExternalByteArray_setUint64, 3) { | |
| 317 UNIMPLEMENTED(); // TODO(cshapiro): need setter implementation. | |
| 318 } | |
| 319 | |
| 320 | |
| 321 DEFINE_NATIVE_ENTRY(ExternalByteArray_getFloat32, 2) { | |
| 322 GetIndexed<ExternalByteArray, float, Double>(arguments); | |
| 323 } | |
| 324 | |
| 325 | |
| 326 DEFINE_NATIVE_ENTRY(ExternalByteArray_setFloat32, 3) { | |
| 327 SetIndexed<ExternalByteArray, float>(arguments, GetDoubleValue<float>); | |
| 328 } | |
| 329 | |
| 330 | |
| 331 DEFINE_NATIVE_ENTRY(ExternalByteArray_getFloat64, 2) { | |
| 332 GetIndexed<ExternalByteArray, double, Double>(arguments); | |
| 333 } | |
| 334 | |
| 335 | |
| 336 DEFINE_NATIVE_ENTRY(ExternalByteArray_setFloat64, 3) { | |
| 337 SetIndexed<ExternalByteArray, double>(arguments, GetDoubleValue<double>); | |
| 338 } | |
| 339 | |
| 340 } // namespace dart | |
| OLD | NEW |