| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "vm/bootstrap_natives.h" | 5 #include "vm/bootstrap_natives.h" |
| 6 | 6 |
| 7 #include "include/dart_api.h" | 7 #include "include/dart_api.h" |
| 8 | 8 |
| 9 #include "vm/bigint_operations.h" | 9 #include "vm/bigint_operations.h" |
| 10 #include "vm/exceptions.h" | 10 #include "vm/exceptions.h" |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 TYPED_DATA_NEW(name) \ | 102 TYPED_DATA_NEW(name) \ |
| 103 EXT_TYPED_DATA_NEW(name) \ | 103 EXT_TYPED_DATA_NEW(name) \ |
| 104 | 104 |
| 105 | 105 |
| 106 CLASS_LIST_TYPED_DATA(TYPED_DATA_NEW_NATIVE) | 106 CLASS_LIST_TYPED_DATA(TYPED_DATA_NEW_NATIVE) |
| 107 | 107 |
| 108 | 108 |
| 109 #define TYPED_DATA_GETTER(getter, object) \ | 109 #define TYPED_DATA_GETTER(getter, object) \ |
| 110 DEFINE_NATIVE_ENTRY(TypedData_##getter, 2) { \ | 110 DEFINE_NATIVE_ENTRY(TypedData_##getter, 2) { \ |
| 111 GET_NON_NULL_NATIVE_ARGUMENT(Instance, instance, arguments->NativeArgAt(0)); \ | 111 GET_NON_NULL_NATIVE_ARGUMENT(Instance, instance, arguments->NativeArgAt(0)); \ |
| 112 GET_NON_NULL_NATIVE_ARGUMENT(Smi, index, arguments->NativeArgAt(1)); \ | 112 GET_NON_NULL_NATIVE_ARGUMENT(Smi, offsetInBytes, arguments->NativeArgAt(1)); \ |
| 113 if (instance.IsTypedData()) { \ | 113 if (instance.IsTypedData()) { \ |
| 114 const TypedData& array = TypedData::Cast(instance); \ | 114 const TypedData& array = TypedData::Cast(instance); \ |
| 115 intptr_t offsetInBytes = index.Value() * array.ElementSizeInBytes(); \ | 115 ASSERT(RangeCheck(offsetInBytes.Value(), array.LengthInBytes())); \ |
| 116 ASSERT(RangeCheck(offsetInBytes, array.LengthInBytes())); \ | 116 return object::New(array.getter(offsetInBytes.Value())); \ |
| 117 return object::New(array.getter(offsetInBytes)); \ | |
| 118 } \ | 117 } \ |
| 119 if (instance.IsExternalTypedData()) { \ | 118 if (instance.IsExternalTypedData()) { \ |
| 120 const ExternalTypedData& array = ExternalTypedData::Cast(instance); \ | 119 const ExternalTypedData& array = ExternalTypedData::Cast(instance); \ |
| 121 intptr_t offsetInBytes = index.Value() * array.ElementSizeInBytes(); \ | 120 ASSERT(RangeCheck(offsetInBytes.Value(), array.LengthInBytes())); \ |
| 122 ASSERT(RangeCheck(offsetInBytes, array.LengthInBytes())); \ | 121 return object::New(array.getter(offsetInBytes.Value())); \ |
| 123 return object::New(array.getter(offsetInBytes)); \ | |
| 124 } \ | 122 } \ |
| 125 const String& error = String::Handle(String::NewFormatted( \ | 123 const String& error = String::Handle(String::NewFormatted( \ |
| 126 "Expected a TypedData object but found %s", instance.ToCString())); \ | 124 "Expected a TypedData object but found %s", instance.ToCString())); \ |
| 127 const Array& args = Array::Handle(Array::New(1)); \ | 125 const Array& args = Array::Handle(Array::New(1)); \ |
| 128 args.SetAt(0, error); \ | 126 args.SetAt(0, error); \ |
| 129 Exceptions::ThrowByType(Exceptions::kArgument, args); \ | 127 Exceptions::ThrowByType(Exceptions::kArgument, args); \ |
| 130 return object::null(); \ | 128 return object::null(); \ |
| 131 } \ | 129 } \ |
| 132 | 130 |
| 133 | 131 |
| 134 #define TYPED_DATA_SETTER(setter, object, get_object_value) \ | 132 #define TYPED_DATA_SETTER(setter, object, get_object_value) \ |
| 135 DEFINE_NATIVE_ENTRY(TypedData_##setter, 3) { \ | 133 DEFINE_NATIVE_ENTRY(TypedData_##setter, 3) { \ |
| 136 GET_NON_NULL_NATIVE_ARGUMENT(Instance, instance, arguments->NativeArgAt(0)); \ | 134 GET_NON_NULL_NATIVE_ARGUMENT(Instance, instance, arguments->NativeArgAt(0)); \ |
| 137 GET_NON_NULL_NATIVE_ARGUMENT(Smi, index, arguments->NativeArgAt(1)); \ | 135 GET_NON_NULL_NATIVE_ARGUMENT(Smi, offsetInBytes, arguments->NativeArgAt(1)); \ |
| 138 GET_NON_NULL_NATIVE_ARGUMENT(object, value, arguments->NativeArgAt(2)); \ | 136 GET_NON_NULL_NATIVE_ARGUMENT(object, value, arguments->NativeArgAt(2)); \ |
| 139 if (instance.IsTypedData()) { \ | 137 if (instance.IsTypedData()) { \ |
| 140 const TypedData& array = TypedData::Cast(instance); \ | 138 const TypedData& array = TypedData::Cast(instance); \ |
| 141 intptr_t offsetInBytes = index.Value() * array.ElementSizeInBytes(); \ | 139 ASSERT(RangeCheck(offsetInBytes.Value(), array.LengthInBytes())); \ |
| 142 ASSERT(RangeCheck(offsetInBytes, array.LengthInBytes())); \ | 140 array.setter(offsetInBytes.Value(), value.get_object_value()); \ |
| 143 array.setter(offsetInBytes, value.get_object_value()); \ | |
| 144 } else if (instance.IsExternalTypedData()) { \ | 141 } else if (instance.IsExternalTypedData()) { \ |
| 145 const ExternalTypedData& array = ExternalTypedData::Cast(instance); \ | 142 const ExternalTypedData& array = ExternalTypedData::Cast(instance); \ |
| 146 intptr_t offsetInBytes = index.Value() * array.ElementSizeInBytes(); \ | 143 ASSERT(RangeCheck(offsetInBytes.Value(), array.LengthInBytes())); \ |
| 147 ASSERT(RangeCheck(offsetInBytes, array.LengthInBytes())); \ | 144 array.setter(offsetInBytes.Value(), value.get_object_value()); \ |
| 148 array.setter(offsetInBytes, value.get_object_value()); \ | |
| 149 } else { \ | 145 } else { \ |
| 150 const String& error = String::Handle(String::NewFormatted( \ | 146 const String& error = String::Handle(String::NewFormatted( \ |
| 151 "Expected a TypedData object but found %s", instance.ToCString())); \ | 147 "Expected a TypedData object but found %s", instance.ToCString())); \ |
| 152 const Array& args = Array::Handle(Array::New(1)); \ | 148 const Array& args = Array::Handle(Array::New(1)); \ |
| 153 args.SetAt(0, error); \ | 149 args.SetAt(0, error); \ |
| 154 Exceptions::ThrowByType(Exceptions::kArgument, args); \ | 150 Exceptions::ThrowByType(Exceptions::kArgument, args); \ |
| 155 } \ | 151 } \ |
| 156 return Object::null(); \ | 152 return Object::null(); \ |
| 157 } | 153 } |
| 158 | 154 |
| 159 | 155 |
| 160 #define TYPED_DATA_UINT64_GETTER(getter, object) \ | 156 #define TYPED_DATA_UINT64_GETTER(getter, object) \ |
| 161 DEFINE_NATIVE_ENTRY(TypedData_##getter, 2) { \ | 157 DEFINE_NATIVE_ENTRY(TypedData_##getter, 2) { \ |
| 162 GET_NON_NULL_NATIVE_ARGUMENT(Instance, instance, arguments->NativeArgAt(0)); \ | 158 GET_NON_NULL_NATIVE_ARGUMENT(Instance, instance, arguments->NativeArgAt(0)); \ |
| 163 GET_NON_NULL_NATIVE_ARGUMENT(Smi, index, arguments->NativeArgAt(1)); \ | 159 GET_NON_NULL_NATIVE_ARGUMENT(Smi, offsetInBytes, arguments->NativeArgAt(1)); \ |
| 164 uint64_t value = 0; \ | 160 uint64_t value = 0; \ |
| 165 if (instance.IsTypedData()) { \ | 161 if (instance.IsTypedData()) { \ |
| 166 const TypedData& array = TypedData::Cast(instance); \ | 162 const TypedData& array = TypedData::Cast(instance); \ |
| 167 intptr_t offsetInBytes = index.Value() * array.ElementSizeInBytes(); \ | 163 ASSERT(RangeCheck(offsetInBytes.Value(), array.LengthInBytes())); \ |
| 168 ASSERT(RangeCheck(offsetInBytes, array.LengthInBytes())); \ | 164 value = array.getter(offsetInBytes.Value()); \ |
| 169 value = array.getter(offsetInBytes); \ | |
| 170 } else if (instance.IsExternalTypedData()) { \ | 165 } else if (instance.IsExternalTypedData()) { \ |
| 171 const ExternalTypedData& array = ExternalTypedData::Cast(instance); \ | 166 const ExternalTypedData& array = ExternalTypedData::Cast(instance); \ |
| 172 intptr_t offsetInBytes = index.Value() * array.ElementSizeInBytes(); \ | 167 ASSERT(RangeCheck(offsetInBytes.Value(), array.LengthInBytes())); \ |
| 173 ASSERT(RangeCheck(offsetInBytes, array.LengthInBytes())); \ | 168 value = array.getter(offsetInBytes.Value()); \ |
| 174 value = array.getter(offsetInBytes); \ | |
| 175 } else { \ | 169 } else { \ |
| 176 const String& error = String::Handle(String::NewFormatted( \ | 170 const String& error = String::Handle(String::NewFormatted( \ |
| 177 "Expected a TypedData object but found %s", instance.ToCString())); \ | 171 "Expected a TypedData object but found %s", instance.ToCString())); \ |
| 178 const Array& args = Array::Handle(Array::New(1)); \ | 172 const Array& args = Array::Handle(Array::New(1)); \ |
| 179 args.SetAt(0, error); \ | 173 args.SetAt(0, error); \ |
| 180 Exceptions::ThrowByType(Exceptions::kArgument, args); \ | 174 Exceptions::ThrowByType(Exceptions::kArgument, args); \ |
| 181 } \ | 175 } \ |
| 182 Integer& result = Integer::Handle(); \ | 176 Integer& result = Integer::Handle(); \ |
| 183 if (value > static_cast<uint64_t>(Mint::kMaxValue)) { \ | 177 if (value > static_cast<uint64_t>(Mint::kMaxValue)) { \ |
| 184 result = BigintOperations::NewFromUint64(value); \ | 178 result = BigintOperations::NewFromUint64(value); \ |
| 185 } else if (value > static_cast<uint64_t>(Smi::kMaxValue)) { \ | 179 } else if (value > static_cast<uint64_t>(Smi::kMaxValue)) { \ |
| 186 result = Mint::New(value); \ | 180 result = Mint::New(value); \ |
| 187 } else { \ | 181 } else { \ |
| 188 result = Smi::New(value); \ | 182 result = Smi::New(value); \ |
| 189 } \ | 183 } \ |
| 190 return result.raw(); \ | 184 return result.raw(); \ |
| 191 } \ | 185 } \ |
| 192 | 186 |
| 193 | 187 |
| 194 // TODO(asiva): Consider truncating the bigint value if it does not fit into | 188 // TODO(asiva): Consider truncating the bigint value if it does not fit into |
| 195 // a uint64_t value (see ASSERT(BigintOperations::FitsIntoUint64(bigint))). | 189 // a uint64_t value (see ASSERT(BigintOperations::FitsIntoUint64(bigint))). |
| 196 #define TYPED_DATA_UINT64_SETTER(setter, object) \ | 190 #define TYPED_DATA_UINT64_SETTER(setter, object) \ |
| 197 DEFINE_NATIVE_ENTRY(TypedData_##setter, 3) { \ | 191 DEFINE_NATIVE_ENTRY(TypedData_##setter, 3) { \ |
| 198 GET_NON_NULL_NATIVE_ARGUMENT(Instance, instance, arguments->NativeArgAt(0)); \ | 192 GET_NON_NULL_NATIVE_ARGUMENT(Instance, instance, arguments->NativeArgAt(0)); \ |
| 199 GET_NON_NULL_NATIVE_ARGUMENT(Smi, index, arguments->NativeArgAt(1)); \ | 193 GET_NON_NULL_NATIVE_ARGUMENT(Smi, offsetInBytes, arguments->NativeArgAt(1)); \ |
| 200 GET_NON_NULL_NATIVE_ARGUMENT(object, value, arguments->NativeArgAt(2)); \ | 194 GET_NON_NULL_NATIVE_ARGUMENT(object, value, arguments->NativeArgAt(2)); \ |
| 201 uint64_t object_value; \ | 195 uint64_t object_value; \ |
| 202 if (value.IsBigint()) { \ | 196 if (value.IsBigint()) { \ |
| 203 const Bigint& bigint = Bigint::Cast(value); \ | 197 const Bigint& bigint = Bigint::Cast(value); \ |
| 204 ASSERT(BigintOperations::FitsIntoUint64(bigint)); \ | 198 ASSERT(BigintOperations::FitsIntoUint64(bigint)); \ |
| 205 object_value = BigintOperations::AbsToUint64(bigint); \ | 199 object_value = BigintOperations::AbsToUint64(bigint); \ |
| 206 } else { \ | 200 } else { \ |
| 207 ASSERT(value.IsMint() || value.IsSmi()); \ | 201 ASSERT(value.IsMint() || value.IsSmi()); \ |
| 208 object_value = value.AsInt64Value(); \ | 202 object_value = value.AsInt64Value(); \ |
| 209 } \ | 203 } \ |
| 210 if (instance.IsTypedData()) { \ | 204 if (instance.IsTypedData()) { \ |
| 211 const TypedData& array = TypedData::Cast(instance); \ | 205 const TypedData& array = TypedData::Cast(instance); \ |
| 212 intptr_t offsetInBytes = index.Value() * array.ElementSizeInBytes(); \ | 206 ASSERT(RangeCheck(offsetInBytes.Value(), array.LengthInBytes())); \ |
| 213 ASSERT(RangeCheck(offsetInBytes, array.LengthInBytes())); \ | 207 array.setter(offsetInBytes.Value(), object_value); \ |
| 214 array.setter(offsetInBytes, object_value); \ | |
| 215 } else if (instance.IsExternalTypedData()) { \ | 208 } else if (instance.IsExternalTypedData()) { \ |
| 216 const ExternalTypedData& array = ExternalTypedData::Cast(instance); \ | 209 const ExternalTypedData& array = ExternalTypedData::Cast(instance); \ |
| 217 intptr_t offsetInBytes = index.Value() * array.ElementSizeInBytes(); \ | 210 ASSERT(RangeCheck(offsetInBytes.Value(), array.LengthInBytes())); \ |
| 218 ASSERT(RangeCheck(offsetInBytes, array.LengthInBytes())); \ | 211 array.setter(offsetInBytes.Value(), object_value); \ |
| 219 array.setter(offsetInBytes, object_value); \ | |
| 220 } else { \ | 212 } else { \ |
| 221 const String& error = String::Handle(String::NewFormatted( \ | 213 const String& error = String::Handle(String::NewFormatted( \ |
| 222 "Expected a TypedData object but found %s", instance.ToCString())); \ | 214 "Expected a TypedData object but found %s", instance.ToCString())); \ |
| 223 const Array& args = Array::Handle(Array::New(1)); \ | 215 const Array& args = Array::Handle(Array::New(1)); \ |
| 224 args.SetAt(0, error); \ | 216 args.SetAt(0, error); \ |
| 225 Exceptions::ThrowByType(Exceptions::kArgument, args); \ | 217 Exceptions::ThrowByType(Exceptions::kArgument, args); \ |
| 226 } \ | 218 } \ |
| 227 return Object::null(); \ | 219 return Object::null(); \ |
| 228 } | 220 } |
| 229 | 221 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 243 TYPED_DATA_NATIVES(Int16Array, GetInt16, SetInt16, Smi, Value) | 235 TYPED_DATA_NATIVES(Int16Array, GetInt16, SetInt16, Smi, Value) |
| 244 TYPED_DATA_NATIVES(Uint16Array, GetUint16, SetUint16, Smi, Value) | 236 TYPED_DATA_NATIVES(Uint16Array, GetUint16, SetUint16, Smi, Value) |
| 245 TYPED_DATA_NATIVES(Int32Array, GetInt32, SetInt32, Integer, AsInt64Value) | 237 TYPED_DATA_NATIVES(Int32Array, GetInt32, SetInt32, Integer, AsInt64Value) |
| 246 TYPED_DATA_NATIVES(Uint32Array, GetUint32, SetUint32, Integer, AsInt64Value) | 238 TYPED_DATA_NATIVES(Uint32Array, GetUint32, SetUint32, Integer, AsInt64Value) |
| 247 TYPED_DATA_NATIVES(Int64Array, GetInt64, SetInt64, Integer, AsInt64Value) | 239 TYPED_DATA_NATIVES(Int64Array, GetInt64, SetInt64, Integer, AsInt64Value) |
| 248 TYPED_DATA_UINT64_NATIVES(Uint64Array, GetUint64, SetUint64, Integer) | 240 TYPED_DATA_UINT64_NATIVES(Uint64Array, GetUint64, SetUint64, Integer) |
| 249 TYPED_DATA_NATIVES(Float32Array, GetFloat32, SetFloat32, Double, value) | 241 TYPED_DATA_NATIVES(Float32Array, GetFloat32, SetFloat32, Double, value) |
| 250 TYPED_DATA_NATIVES(Float64Array, GetFloat64, SetFloat64, Double, value) | 242 TYPED_DATA_NATIVES(Float64Array, GetFloat64, SetFloat64, Double, value) |
| 251 | 243 |
| 252 } // namespace dart | 244 } // namespace dart |
| OLD | NEW |