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)) { | |
|
Ivan Posva
2011/11/10 18:06:41
Shouldn't length.IsNull already be caught by the I
cshapiro
2011/11/11 00:37:53
I suppose so. Fixed.
| |
| 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 T, typename C> | |
| 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())) { | |
| 66 GrowableArray<const Object*> arguments; | |
| 67 arguments.Add(&index); | |
| 68 Exceptions::ThrowByType(Exceptions::kIndexOutOfRange, arguments); | |
| 69 } | |
| 70 arguments->SetReturn(C::Handle(C::New(buffer.At<T>(index.Value())))); | |
| 71 } | |
| 72 | |
| 73 | |
| 74 class IntegerValue { | |
| 75 public: | |
| 76 typedef Integer ValueType; | |
| 77 static bool Is(const Instance& obj) { return obj.IsInteger(); } | |
| 78 static int64_t ValueOf(const Integer& val) { return val.AsInt64Value(); } | |
| 79 private: | |
| 80 DISALLOW_IMPLICIT_CONSTRUCTORS(IntegerValue); | |
| 81 }; | |
| 82 | |
| 83 | |
| 84 class DoubleValue { | |
| 85 public: | |
| 86 typedef Double ValueType; | |
| 87 static bool Is(const Instance& obj) { return obj.IsDouble(); } | |
| 88 static double ValueOf(const Double& val) { return val.value(); } | |
| 89 private: | |
| 90 DISALLOW_IMPLICIT_CONSTRUCTORS(DoubleValue); | |
| 91 }; | |
| 92 | |
| 93 | |
| 94 template<typename T, typename C> | |
| 95 static void SetIndexed(const NativeArguments* arguments) { | |
| 96 const ByteBuffer& buffer = ByteBuffer::CheckedHandle(arguments->At(0)); | |
| 97 const Instance& index_instance = Instance::CheckedHandle(arguments->At(1)); | |
| 98 if (!index_instance.IsSmi()) { | |
| 99 GrowableArray<const Object*> args; | |
| 100 args.Add(&index_instance); | |
| 101 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args); | |
| 102 } | |
| 103 Smi& index = Smi::Handle(); | |
| 104 index ^= index_instance.raw(); | |
| 105 const Instance& value_instance = Instance::CheckedHandle(arguments->At(2)); | |
| 106 if (buffer.IsNull() || index.IsNull()) { | |
| 107 // TODO(asiva): Need to handle error cases. | |
| 108 UNIMPLEMENTED(); | |
| 109 return; | |
| 110 } | |
| 111 if ((index.Value() < 0) || (index.Value() >= buffer.Length())) { | |
| 112 GrowableArray<const Object*> arguments; | |
| 113 arguments.Add(&index); | |
| 114 Exceptions::ThrowByType(Exceptions::kIndexOutOfRange, arguments); | |
| 115 } | |
| 116 if (!C::Is(value_instance)) { | |
| 117 GrowableArray<const Object*> args; | |
| 118 args.Add(&value_instance); | |
| 119 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args); | |
| 120 } | |
| 121 typename C::ValueType& value = C::ValueType::Handle(); | |
| 122 value ^= value_instance.raw(); | |
| 123 buffer.SetAt<T>(index.Value(), C::ValueOf(value)); | |
| 124 } | |
| 125 | |
| 126 | |
| 127 DEFINE_NATIVE_ENTRY(ByteBuffer_getInt8, 2) { | |
| 128 GetIndexed<int8_t, Smi>(arguments); | |
| 129 } | |
| 130 | |
| 131 | |
| 132 DEFINE_NATIVE_ENTRY(ByteBuffer_setInt8, 3) { | |
| 133 SetIndexed<int8_t, IntegerValue>(arguments); | |
| 134 } | |
| 135 | |
| 136 | |
| 137 DEFINE_NATIVE_ENTRY(ByteBuffer_getUint8, 2) { | |
| 138 GetIndexed<uint8_t, Smi>(arguments); | |
| 139 } | |
| 140 | |
| 141 | |
| 142 DEFINE_NATIVE_ENTRY(ByteBuffer_setUint8, 3) { | |
| 143 SetIndexed<uint8_t, IntegerValue>(arguments); | |
| 144 } | |
| 145 | |
| 146 | |
| 147 DEFINE_NATIVE_ENTRY(ByteBuffer_getInt16, 2) { | |
| 148 GetIndexed<int16_t, Smi>(arguments); | |
| 149 } | |
| 150 | |
| 151 | |
| 152 DEFINE_NATIVE_ENTRY(ByteBuffer_setInt16, 3) { | |
| 153 SetIndexed<int16_t, IntegerValue>(arguments); | |
| 154 } | |
| 155 | |
| 156 | |
| 157 DEFINE_NATIVE_ENTRY(ByteBuffer_getUint16, 2) { | |
| 158 GetIndexed<uint16_t, Smi>(arguments); | |
| 159 } | |
| 160 | |
| 161 | |
| 162 DEFINE_NATIVE_ENTRY(ByteBuffer_setUint16, 3) { | |
| 163 SetIndexed<uint16_t, IntegerValue>(arguments); | |
| 164 } | |
| 165 | |
| 166 | |
| 167 DEFINE_NATIVE_ENTRY(ByteBuffer_getInt32, 2) { | |
| 168 GetIndexed<int32_t, Integer>(arguments); | |
| 169 } | |
| 170 | |
| 171 | |
| 172 DEFINE_NATIVE_ENTRY(ByteBuffer_setInt32, 3) { | |
| 173 SetIndexed<int32_t, IntegerValue>(arguments); | |
| 174 } | |
| 175 | |
| 176 | |
| 177 DEFINE_NATIVE_ENTRY(ByteBuffer_getUint32, 2) { | |
| 178 GetIndexed<uint32_t, Integer>(arguments); | |
| 179 } | |
| 180 | |
| 181 | |
| 182 DEFINE_NATIVE_ENTRY(ByteBuffer_setUint32, 3) { | |
| 183 SetIndexed<uint32_t, IntegerValue>(arguments); | |
| 184 } | |
| 185 | |
| 186 | |
| 187 DEFINE_NATIVE_ENTRY(ByteBuffer_getInt64, 2) { | |
| 188 GetIndexed<int64_t, Integer>(arguments); | |
| 189 } | |
| 190 | |
| 191 | |
| 192 DEFINE_NATIVE_ENTRY(ByteBuffer_setInt64, 3) { | |
| 193 SetIndexed<int64_t, IntegerValue>(arguments); | |
| 194 } | |
| 195 | |
| 196 | |
| 197 DEFINE_NATIVE_ENTRY(ByteBuffer_getUint64, 2) { | |
| 198 UNIMPLEMENTED(); // TODO(cshapiro): need getter implementation. | |
| 199 } | |
| 200 | |
| 201 | |
| 202 DEFINE_NATIVE_ENTRY(ByteBuffer_setUint64, 3) { | |
| 203 UNIMPLEMENTED(); // TODO(cshapiro): need setter implementation. | |
| 204 } | |
| 205 | |
| 206 | |
| 207 DEFINE_NATIVE_ENTRY(ByteBuffer_getFloat32, 2) { | |
| 208 GetIndexed<float, Double>(arguments); | |
| 209 } | |
| 210 | |
| 211 | |
| 212 DEFINE_NATIVE_ENTRY(ByteBuffer_setFloat32, 3) { | |
| 213 SetIndexed<float, DoubleValue>(arguments); | |
| 214 } | |
| 215 | |
| 216 | |
| 217 DEFINE_NATIVE_ENTRY(ByteBuffer_getFloat64, 2) { | |
| 218 GetIndexed<double, Double>(arguments); | |
| 219 } | |
| 220 | |
| 221 | |
| 222 DEFINE_NATIVE_ENTRY(ByteBuffer_setFloat64, 3) { | |
| 223 SetIndexed<double, DoubleValue>(arguments); | |
| 224 } | |
| 225 | |
| 226 } // namespace dart | |
| OLD | NEW |