| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011, 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/assembler.h" |
| 8 #include "vm/assert.h" |
| 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 DEFINE_NATIVE_ENTRY(ByteBuffer_allocate, 1) { |
| 17 const Instance& length_instance = Instance::CheckedHandle(arguments->At(0)); |
| 18 if (!length_instance.IsSmi()) { |
| 19 GrowableArray<const Object*> args; |
| 20 args.Add(&length_instance); |
| 21 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args); |
| 22 } |
| 23 Smi& length = Smi::Handle(); |
| 24 length ^= length_instance.raw(); |
| 25 if (length.Value() < 0) { |
| 26 GrowableArray<const Object*> args; |
| 27 args.Add(&length); |
| 28 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args); |
| 29 } |
| 30 uint8_t* data = new uint8_t[length.Value()]; |
| 31 memset(data, 0, length.Value()); |
| 32 const ByteBuffer& new_array = |
| 33 ByteBuffer::Handle(ByteBuffer::New(data, length.Value())); |
| 34 arguments->SetReturn(new_array); |
| 35 } |
| 36 |
| 37 |
| 38 DEFINE_NATIVE_ENTRY(ByteBuffer_getLength, 1) { |
| 39 const ByteBuffer& array = ByteBuffer::CheckedHandle(arguments->At(0)); |
| 40 if (array.IsNull()) { |
| 41 // TODO(asiva): Need to handle error cases. |
| 42 UNIMPLEMENTED(); |
| 43 return; |
| 44 } |
| 45 const Smi& length = Smi::Handle(Smi::New(array.Length())); |
| 46 arguments->SetReturn(length); |
| 47 } |
| 48 |
| 49 |
| 50 template<typename ValueType, typename ObjectType> |
| 51 static void GetIndexed(NativeArguments* arguments) { |
| 52 const ByteBuffer& buffer = ByteBuffer::CheckedHandle(arguments->At(0)); |
| 53 const Instance& index_instance = Instance::CheckedHandle(arguments->At(1)); |
| 54 if (!index_instance.IsSmi()) { |
| 55 GrowableArray<const Object*> args; |
| 56 args.Add(&index_instance); |
| 57 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args); |
| 58 } |
| 59 Smi& index = Smi::Handle(); |
| 60 index ^= index_instance.raw(); |
| 61 if (buffer.IsNull() || index.IsNull()) { |
| 62 // TODO(asiva): Need to handle error cases. |
| 63 UNIMPLEMENTED(); |
| 64 return; |
| 65 } |
| 66 intptr_t num_bytes = sizeof(ValueType); |
| 67 if ((index.Value() < 0) || ((index.Value() + num_bytes) > buffer.Length())) { |
| 68 GrowableArray<const Object*> arguments; |
| 69 arguments.Add(&index); |
| 70 Exceptions::ThrowByType(Exceptions::kIndexOutOfRange, arguments); |
| 71 } |
| 72 arguments->SetReturn( |
| 73 ObjectType::Handle( |
| 74 ObjectType::New(buffer.UnalignedAt<ValueType>(index.Value())))); |
| 75 } |
| 76 |
| 77 |
| 78 template<typename T> static bool HasType(const Object& obj); |
| 79 |
| 80 |
| 81 template<> |
| 82 bool HasType<Smi>(const Object& obj) { |
| 83 return obj.IsSmi(); |
| 84 } |
| 85 |
| 86 |
| 87 template<> |
| 88 bool HasType<Integer>(const Object& obj) { |
| 89 return obj.IsInteger(); |
| 90 } |
| 91 |
| 92 |
| 93 template<> |
| 94 bool HasType<Double>(const Object& obj) { |
| 95 return obj.IsDouble(); |
| 96 } |
| 97 |
| 98 |
| 99 static intptr_t Value(const Smi& obj) { |
| 100 return obj.Value(); |
| 101 } |
| 102 |
| 103 |
| 104 static int64_t Value(const Integer& obj) { |
| 105 return obj.AsInt64Value(); |
| 106 } |
| 107 |
| 108 |
| 109 static double Value(const Double& obj) { |
| 110 return obj.value(); |
| 111 } |
| 112 |
| 113 |
| 114 template<typename ValueType, typename ObjectType> |
| 115 static void SetIndexed(const NativeArguments* arguments) { |
| 116 const ByteBuffer& buffer = ByteBuffer::CheckedHandle(arguments->At(0)); |
| 117 const Instance& index_instance = Instance::CheckedHandle(arguments->At(1)); |
| 118 if (!index_instance.IsSmi()) { |
| 119 GrowableArray<const Object*> args; |
| 120 args.Add(&index_instance); |
| 121 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args); |
| 122 } |
| 123 Smi& index = Smi::Handle(); |
| 124 index ^= index_instance.raw(); |
| 125 const Instance& value_instance = Instance::CheckedHandle(arguments->At(2)); |
| 126 if (buffer.IsNull() || index.IsNull()) { |
| 127 // TODO(asiva): Need to handle error cases. |
| 128 UNIMPLEMENTED(); |
| 129 return; |
| 130 } |
| 131 if (index.Value() >= buffer.Length()) { |
| 132 GrowableArray<const Object*> arguments; |
| 133 arguments.Add(&index); |
| 134 Exceptions::ThrowByType(Exceptions::kIndexOutOfRange, arguments); |
| 135 } |
| 136 if (!HasType<ObjectType>(value_instance)) { |
| 137 GrowableArray<const Object*> args; |
| 138 args.Add(&value_instance); |
| 139 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args); |
| 140 } |
| 141 ObjectType& value = ObjectType::Handle(); |
| 142 value ^= value_instance.raw(); |
| 143 buffer.SetUnalignedAt<ValueType>(index.Value(), Value(value)); |
| 144 } |
| 145 |
| 146 |
| 147 DEFINE_NATIVE_ENTRY(ByteBuffer_getInt8, 2) { |
| 148 GetIndexed<int8_t, Smi>(arguments); |
| 149 } |
| 150 |
| 151 |
| 152 DEFINE_NATIVE_ENTRY(ByteBuffer_setInt8, 3) { |
| 153 SetIndexed<int8_t, Smi>(arguments); |
| 154 } |
| 155 |
| 156 |
| 157 DEFINE_NATIVE_ENTRY(ByteBuffer_getUint8, 2) { |
| 158 GetIndexed<uint8_t, Smi>(arguments); |
| 159 } |
| 160 |
| 161 |
| 162 DEFINE_NATIVE_ENTRY(ByteBuffer_setUint8, 3) { |
| 163 SetIndexed<uint8_t, Smi>(arguments); |
| 164 } |
| 165 |
| 166 |
| 167 DEFINE_NATIVE_ENTRY(ByteBuffer_getInt16, 2) { |
| 168 GetIndexed<int16_t, Smi>(arguments); |
| 169 } |
| 170 |
| 171 |
| 172 DEFINE_NATIVE_ENTRY(ByteBuffer_setInt16, 3) { |
| 173 SetIndexed<int16_t, Smi>(arguments); |
| 174 } |
| 175 |
| 176 |
| 177 DEFINE_NATIVE_ENTRY(ByteBuffer_getUint16, 2) { |
| 178 GetIndexed<uint16_t, Smi>(arguments); |
| 179 } |
| 180 |
| 181 |
| 182 DEFINE_NATIVE_ENTRY(ByteBuffer_setUint16, 3) { |
| 183 SetIndexed<uint16_t, Smi>(arguments); |
| 184 } |
| 185 |
| 186 |
| 187 DEFINE_NATIVE_ENTRY(ByteBuffer_getInt32, 2) { |
| 188 GetIndexed<int32_t, Integer>(arguments); |
| 189 } |
| 190 |
| 191 |
| 192 DEFINE_NATIVE_ENTRY(ByteBuffer_setInt32, 3) { |
| 193 SetIndexed<int32_t, Integer>(arguments); |
| 194 } |
| 195 |
| 196 |
| 197 DEFINE_NATIVE_ENTRY(ByteBuffer_getUint32, 2) { |
| 198 GetIndexed<uint32_t, Integer>(arguments); |
| 199 } |
| 200 |
| 201 |
| 202 DEFINE_NATIVE_ENTRY(ByteBuffer_setUint32, 3) { |
| 203 SetIndexed<uint32_t, Integer>(arguments); |
| 204 } |
| 205 |
| 206 |
| 207 DEFINE_NATIVE_ENTRY(ByteBuffer_getInt64, 2) { |
| 208 GetIndexed<int64_t, Integer>(arguments); |
| 209 } |
| 210 |
| 211 |
| 212 DEFINE_NATIVE_ENTRY(ByteBuffer_setInt64, 3) { |
| 213 SetIndexed<int64_t, Integer>(arguments); |
| 214 } |
| 215 |
| 216 |
| 217 DEFINE_NATIVE_ENTRY(ByteBuffer_getUint64, 2) { |
| 218 UNIMPLEMENTED(); // TODO(cshapiro): need getter implementation. |
| 219 } |
| 220 |
| 221 |
| 222 DEFINE_NATIVE_ENTRY(ByteBuffer_setUint64, 3) { |
| 223 UNIMPLEMENTED(); // TODO(cshapiro): need setter implementation. |
| 224 } |
| 225 |
| 226 |
| 227 DEFINE_NATIVE_ENTRY(ByteBuffer_getFloat32, 2) { |
| 228 GetIndexed<float, Double>(arguments); |
| 229 } |
| 230 |
| 231 |
| 232 DEFINE_NATIVE_ENTRY(ByteBuffer_setFloat32, 3) { |
| 233 SetIndexed<float, Double>(arguments); |
| 234 } |
| 235 |
| 236 |
| 237 DEFINE_NATIVE_ENTRY(ByteBuffer_getFloat64, 2) { |
| 238 GetIndexed<double, Double>(arguments); |
| 239 } |
| 240 |
| 241 |
| 242 DEFINE_NATIVE_ENTRY(ByteBuffer_setFloat64, 3) { |
| 243 SetIndexed<double, Double>(arguments); |
| 244 } |
| 245 |
| 246 } // namespace dart |
| OLD | NEW |