Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 "vm/bootstrap_natives.h" | |
| 6 | |
| 7 #include "vm/exceptions.h" | |
| 8 #include "vm/native_entry.h" | |
| 9 #include "vm/object.h" | |
| 10 | |
| 11 namespace dart { | |
| 12 | |
| 13 DEFINE_NATIVE_ENTRY(ByteArray_getLength, 1) { | |
| 14 const ByteArray& byte_array = ByteArray::CheckedHandle(arguments->At(0)); | |
| 15 const Smi& length = Smi::Handle(Smi::New(byte_array.Length())); | |
| 16 arguments->SetReturn(length); | |
| 17 } | |
| 18 | |
| 19 | |
| 20 DEFINE_NATIVE_ENTRY(InternalByteArray_allocate, 1) { | |
| 21 GET_NATIVE_ARGUMENT(Smi, length, arguments->At(0)); | |
| 22 if (length.Value() < 0) { | |
| 23 GrowableArray<const Object*> args; | |
| 24 args.Add(&length); | |
| 25 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args); | |
| 26 } | |
| 27 const InternalByteArray& new_array = | |
| 28 InternalByteArray::Handle(InternalByteArray::New(length.Value())); | |
| 29 arguments->SetReturn(new_array); | |
| 30 } | |
| 31 | |
| 32 | |
| 33 static RawSmi* GetIndexArgument(NativeArguments* arguments) { | |
| 34 const Instance& index_instance = Instance::CheckedHandle(arguments->At(1)); | |
|
Ivan Posva
2012/01/25 23:49:57
GET_NATIVE_ARGUMENT(Smi, index, arguments->At(1));
cshapiro
2012/01/26 02:25:42
Removed entirely.
| |
| 35 if (!index_instance.IsSmi()) { | |
| 36 GrowableArray<const Object*> args; | |
| 37 args.Add(&index_instance); | |
| 38 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args); | |
| 39 } | |
| 40 Smi& index = Smi::Handle(); | |
| 41 index ^= index_instance.raw(); | |
| 42 return index.raw(); | |
| 43 } | |
| 44 | |
| 45 | |
| 46 static void RangeCheck(const ByteArray& array, const Smi& index, | |
| 47 intptr_t num_bytes) { | |
| 48 if ((index.Value() < 0) || ((index.Value() + num_bytes) > array.Length())) { | |
| 49 GrowableArray<const Object*> arguments; | |
| 50 arguments.Add(&index); | |
| 51 Exceptions::ThrowByType(Exceptions::kIndexOutOfRange, arguments); | |
| 52 } | |
| 53 } | |
| 54 | |
| 55 | |
| 56 #define GETTER(ArrayT, ObjectT, ValueT) \ | |
| 57 const Instance& array_instance = \ | |
|
Ivan Posva
2012/01/25 23:49:57
A whole chunk here can be replaced with:
GET_NATIV
cshapiro
2012/01/26 02:25:42
Yes, that looks much better. Done.
| |
| 58 Instance::CheckedHandle(arguments->At(0)); \ | |
| 59 if (!array_instance.Is##ArrayT()) { \ | |
| 60 GrowableArray<const Object*> args; \ | |
| 61 args.Add(&array_instance); \ | |
| 62 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args); \ | |
| 63 } \ | |
| 64 ArrayT& array = ArrayT::Handle(); \ | |
| 65 array ^= array_instance.raw(); \ | |
| 66 const Smi& index = Smi::Handle(GetIndexArgument(arguments)); \ | |
| 67 RangeCheck(array, index, sizeof(ValueT)); \ | |
| 68 const Instance& value_instance = \ | |
| 69 Instance::CheckedHandle(arguments->At(1)); \ | |
| 70 if (!value_instance.Is##ObjectT()) { \ | |
| 71 GrowableArray<const Object*> args; \ | |
| 72 args.Add(&value_instance); \ | |
| 73 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args); \ | |
| 74 } \ | |
| 75 ObjectT& value = ObjectT::Handle(); \ | |
| 76 value ^= value_instance.raw(); \ | |
|
siva
2012/01/25 17:22:47
Why do you need the value_instance stuff in the GE
cshapiro
2012/01/25 23:48:49
How embarrassing. You are correct.
| |
| 77 ValueT result = array.UnalignedAt<ValueT>(index.Value()); \ | |
| 78 arguments->SetReturn(ObjectT::Handle(ObjectT::New(result))); | |
| 79 | |
| 80 | |
| 81 #define SETTER(ArrayT, ObjectT, Getter, ValueT) \ | |
| 82 const Instance& array_instance = \ | |
|
Ivan Posva
2012/01/25 23:49:57
ditto + GET_NATIVE_ARGUMENT(ObjectT, value, argume
cshapiro
2012/01/26 02:25:42
Yup. Done.
| |
| 83 Instance::CheckedHandle(arguments->At(0)); \ | |
| 84 if (!array_instance.Is##ArrayT()) { \ | |
| 85 GrowableArray<const Object*> args; \ | |
| 86 args.Add(&array_instance); \ | |
| 87 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args); \ | |
| 88 } \ | |
| 89 ArrayT& array = ArrayT::Handle(); \ | |
| 90 array ^= array_instance.raw(); \ | |
| 91 const Smi& index = Smi::Handle(GetIndexArgument(arguments)); \ | |
| 92 RangeCheck(array, index, sizeof(ValueT)); \ | |
| 93 const Instance& value_instance = \ | |
| 94 Instance::CheckedHandle(arguments->At(2)); \ | |
| 95 if (!value_instance.Is##ObjectT()) { \ | |
| 96 GrowableArray<const Object*> args; \ | |
| 97 args.Add(&value_instance); \ | |
| 98 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args); \ | |
| 99 } \ | |
| 100 ObjectT& value = ObjectT::Handle(); \ | |
| 101 value ^= value_instance.raw(); \ | |
| 102 array.SetUnalignedAt<ValueT>(index.Value(), value.Getter()); | |
| 103 | |
| 104 | |
| 105 DEFINE_NATIVE_ENTRY(InternalByteArray_getInt8, 2) { | |
| 106 GETTER(InternalByteArray, Smi, int8_t); | |
| 107 } | |
| 108 | |
| 109 | |
| 110 DEFINE_NATIVE_ENTRY(InternalByteArray_setInt8, 3) { | |
| 111 SETTER(InternalByteArray, Smi, Value, int8_t); | |
| 112 } | |
| 113 | |
| 114 | |
| 115 DEFINE_NATIVE_ENTRY(InternalByteArray_getUint8, 2) { | |
| 116 GETTER(InternalByteArray, Smi, uint8_t); | |
| 117 } | |
| 118 | |
| 119 | |
| 120 DEFINE_NATIVE_ENTRY(InternalByteArray_setUint8, 3) { | |
| 121 SETTER(InternalByteArray, Smi, Value, uint8_t); | |
| 122 } | |
| 123 | |
| 124 | |
| 125 DEFINE_NATIVE_ENTRY(InternalByteArray_getInt16, 2) { | |
| 126 GETTER(InternalByteArray, Smi, int16_t); | |
| 127 } | |
| 128 | |
| 129 | |
| 130 DEFINE_NATIVE_ENTRY(InternalByteArray_setInt16, 3) { | |
| 131 SETTER(InternalByteArray, Smi, Value, int16_t); | |
| 132 } | |
| 133 | |
| 134 | |
| 135 DEFINE_NATIVE_ENTRY(InternalByteArray_getUint16, 2) { | |
| 136 GETTER(InternalByteArray, Smi, uint16_t); | |
| 137 } | |
| 138 | |
| 139 | |
| 140 DEFINE_NATIVE_ENTRY(InternalByteArray_setUint16, 3) { | |
| 141 SETTER(InternalByteArray, Smi, Value, uint16_t); | |
| 142 } | |
| 143 | |
| 144 | |
| 145 DEFINE_NATIVE_ENTRY(InternalByteArray_getInt32, 2) { | |
| 146 GETTER(InternalByteArray, Integer, int32_t); | |
| 147 } | |
| 148 | |
| 149 | |
| 150 DEFINE_NATIVE_ENTRY(InternalByteArray_setInt32, 3) { | |
| 151 SETTER(InternalByteArray, Integer, AsInt64Value, int32_t); | |
| 152 } | |
| 153 | |
| 154 | |
| 155 DEFINE_NATIVE_ENTRY(InternalByteArray_getUint32, 2) { | |
| 156 GETTER(InternalByteArray, Integer, uint32_t); | |
| 157 } | |
| 158 | |
| 159 | |
| 160 DEFINE_NATIVE_ENTRY(InternalByteArray_setUint32, 3) { | |
| 161 SETTER(InternalByteArray, Integer, AsInt64Value, uint32_t); | |
| 162 } | |
| 163 | |
| 164 | |
| 165 DEFINE_NATIVE_ENTRY(InternalByteArray_getInt64, 2) { | |
| 166 GETTER(InternalByteArray, Integer, int64_t); | |
| 167 } | |
| 168 | |
| 169 | |
| 170 DEFINE_NATIVE_ENTRY(InternalByteArray_setInt64, 3) { | |
| 171 SETTER(InternalByteArray, Integer, AsInt64Value, int64_t); | |
| 172 } | |
| 173 | |
| 174 | |
| 175 DEFINE_NATIVE_ENTRY(InternalByteArray_getUint64, 2) { | |
| 176 UNIMPLEMENTED(); // TODO(cshapiro): need getter implementation. | |
| 177 } | |
| 178 | |
| 179 | |
| 180 DEFINE_NATIVE_ENTRY(InternalByteArray_setUint64, 3) { | |
| 181 UNIMPLEMENTED(); // TODO(cshapiro): need setter implementation. | |
| 182 } | |
| 183 | |
| 184 | |
| 185 DEFINE_NATIVE_ENTRY(InternalByteArray_getFloat32, 2) { | |
| 186 GETTER(InternalByteArray, Double, float); | |
| 187 } | |
| 188 | |
| 189 | |
| 190 DEFINE_NATIVE_ENTRY(InternalByteArray_setFloat32, 3) { | |
| 191 SETTER(InternalByteArray, Double, value, float); | |
| 192 } | |
| 193 | |
| 194 | |
| 195 DEFINE_NATIVE_ENTRY(InternalByteArray_getFloat64, 2) { | |
| 196 GETTER(InternalByteArray, Double, double); | |
| 197 } | |
| 198 | |
| 199 | |
| 200 DEFINE_NATIVE_ENTRY(InternalByteArray_setFloat64, 3) { | |
| 201 SETTER(InternalByteArray, Double, value, double); | |
| 202 } | |
| 203 | |
| 204 | |
| 205 DEFINE_NATIVE_ENTRY(ExternalByteArray_getInt8, 2) { | |
| 206 GETTER(ExternalByteArray, Smi, int8_t); | |
| 207 } | |
| 208 | |
| 209 | |
| 210 DEFINE_NATIVE_ENTRY(ExternalByteArray_setInt8, 3) { | |
| 211 SETTER(ExternalByteArray, Smi, Value, int8_t); | |
| 212 } | |
| 213 | |
| 214 | |
| 215 DEFINE_NATIVE_ENTRY(ExternalByteArray_getUint8, 2) { | |
| 216 GETTER(ExternalByteArray, Smi, uint8_t); | |
| 217 } | |
| 218 | |
| 219 | |
| 220 DEFINE_NATIVE_ENTRY(ExternalByteArray_setUint8, 3) { | |
| 221 SETTER(ExternalByteArray, Smi, Value, uint8_t); | |
| 222 } | |
| 223 | |
| 224 | |
| 225 DEFINE_NATIVE_ENTRY(ExternalByteArray_getInt16, 2) { | |
| 226 GETTER(ExternalByteArray, Smi, int16_t); | |
| 227 } | |
| 228 | |
| 229 | |
| 230 DEFINE_NATIVE_ENTRY(ExternalByteArray_setInt16, 3) { | |
| 231 SETTER(ExternalByteArray, Smi, Value, int16_t); | |
| 232 } | |
| 233 | |
| 234 | |
| 235 DEFINE_NATIVE_ENTRY(ExternalByteArray_getUint16, 2) { | |
| 236 GETTER(ExternalByteArray, Smi, uint16_t); | |
| 237 } | |
| 238 | |
| 239 | |
| 240 DEFINE_NATIVE_ENTRY(ExternalByteArray_setUint16, 3) { | |
| 241 SETTER(ExternalByteArray, Smi, Value, uint16_t); | |
| 242 } | |
| 243 | |
| 244 | |
| 245 DEFINE_NATIVE_ENTRY(ExternalByteArray_getInt32, 2) { | |
| 246 GETTER(ExternalByteArray, Integer, int32_t); | |
| 247 } | |
| 248 | |
| 249 | |
| 250 DEFINE_NATIVE_ENTRY(ExternalByteArray_setInt32, 3) { | |
| 251 SETTER(ExternalByteArray, Integer, AsInt64Value, int32_t); | |
| 252 } | |
| 253 | |
| 254 | |
| 255 DEFINE_NATIVE_ENTRY(ExternalByteArray_getUint32, 2) { | |
| 256 GETTER(ExternalByteArray, Integer, uint32_t); | |
| 257 } | |
| 258 | |
| 259 | |
| 260 DEFINE_NATIVE_ENTRY(ExternalByteArray_setUint32, 3) { | |
| 261 SETTER(ExternalByteArray, Integer, AsInt64Value, uint32_t); | |
| 262 } | |
| 263 | |
| 264 | |
| 265 DEFINE_NATIVE_ENTRY(ExternalByteArray_getInt64, 2) { | |
| 266 GETTER(ExternalByteArray, Integer, int64_t); | |
| 267 } | |
| 268 | |
| 269 | |
| 270 DEFINE_NATIVE_ENTRY(ExternalByteArray_setInt64, 3) { | |
| 271 SETTER(ExternalByteArray, Integer, AsInt64Value, int64_t); | |
| 272 } | |
| 273 | |
| 274 | |
| 275 DEFINE_NATIVE_ENTRY(ExternalByteArray_getUint64, 2) { | |
| 276 UNIMPLEMENTED(); // TODO(cshapiro): need getter implementation. | |
| 277 } | |
| 278 | |
| 279 | |
| 280 DEFINE_NATIVE_ENTRY(ExternalByteArray_setUint64, 3) { | |
| 281 UNIMPLEMENTED(); // TODO(cshapiro): need setter implementation. | |
| 282 } | |
| 283 | |
| 284 | |
| 285 DEFINE_NATIVE_ENTRY(ExternalByteArray_getFloat32, 2) { | |
| 286 GETTER(ExternalByteArray, Double, float); | |
| 287 } | |
| 288 | |
| 289 | |
| 290 DEFINE_NATIVE_ENTRY(ExternalByteArray_setFloat32, 3) { | |
| 291 SETTER(ExternalByteArray, Double, value, float); | |
| 292 } | |
| 293 | |
| 294 | |
| 295 DEFINE_NATIVE_ENTRY(ExternalByteArray_getFloat64, 2) { | |
| 296 GETTER(ExternalByteArray, Double, double); | |
| 297 } | |
| 298 | |
| 299 | |
| 300 DEFINE_NATIVE_ENTRY(ExternalByteArray_setFloat64, 3) { | |
| 301 SETTER(ExternalByteArray, Double, value, double); | |
| 302 } | |
| 303 | |
| 304 } // namespace dart | |
| OLD | NEW |