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