| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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 "vm/exceptions.h" | 7 #include "vm/exceptions.h" |
| 8 #include "vm/native_entry.h" | 8 #include "vm/native_entry.h" |
| 9 #include "vm/object.h" | 9 #include "vm/object.h" |
| 10 #include "vm/symbols.h" | 10 #include "vm/symbols.h" |
| 11 #include "vm/unicode.h" | 11 #include "vm/unicode.h" |
| 12 | 12 |
| 13 namespace dart { | 13 namespace dart { |
| 14 | 14 |
| 15 DEFINE_NATIVE_ENTRY(StringBase_createFromCodePoints, 1) { | 15 DEFINE_NATIVE_ENTRY(StringBase_createFromCodePoints, 1) { |
| 16 GET_NON_NULL_NATIVE_ARGUMENT(Array, a, arguments->NativeArgAt(0)); | 16 GET_NON_NULL_NATIVE_ARGUMENT(Array, a, arguments->NativeArgAt(0)); |
| 17 // TODO(srdjan): Check that parameterized type is an int. | 17 // TODO(srdjan): Check that parameterized type is an int. |
| 18 Zone* zone = isolate->current_zone(); | 18 Zone* zone = isolate->current_zone(); |
| 19 intptr_t array_len = a.Length(); | 19 intptr_t array_len = a.Length(); |
| 20 | 20 |
| 21 // Unbox the array and determine the maximum element width. | 21 // Unbox the array and determine the maximum element width. |
| 22 bool is_one_byte_string = true; | 22 bool is_one_byte_string = true; |
| 23 intptr_t utf16_len = array_len; | 23 intptr_t utf16_len = array_len; |
| 24 int32_t* utf32_array = zone->Alloc<int32_t>(array_len); | 24 int32_t* utf32_array = zone->Alloc<int32_t>(array_len); |
| 25 Object& index_object = Object::Handle(isolate); | 25 Object& index_object = Object::Handle(isolate); |
| 26 for (intptr_t i = 0; i < array_len; i++) { | 26 for (intptr_t i = 0; i < array_len; i++) { |
| 27 index_object = a.At(i); | 27 index_object = a.At(i); |
| 28 if (!index_object.IsSmi()) { | 28 if (!index_object.IsSmi()) { |
| 29 GrowableArray<const Object*> args; | 29 const Array& args = Array::Handle(Array::New(1)); |
| 30 args.Add(&index_object); | 30 args.SetAt(0, index_object); |
| 31 Exceptions::ThrowByType(Exceptions::kArgument, args); | 31 Exceptions::ThrowByType(Exceptions::kArgument, args); |
| 32 } | 32 } |
| 33 intptr_t value = Smi::Cast(index_object).Value(); | 33 intptr_t value = Smi::Cast(index_object).Value(); |
| 34 if (Utf::IsOutOfRange(value)) { | 34 if (Utf::IsOutOfRange(value)) { |
| 35 GrowableArray<const Object*> args; | 35 const Array& args = Array::Handle(Object::empty_array()); |
| 36 Exceptions::ThrowByType(Exceptions::kArgument, args); | 36 Exceptions::ThrowByType(Exceptions::kArgument, args); |
| 37 } else { | 37 } else { |
| 38 if (!Utf::IsLatin1(value)) { | 38 if (!Utf::IsLatin1(value)) { |
| 39 is_one_byte_string = false; | 39 is_one_byte_string = false; |
| 40 if (Utf::IsSupplementary(value)) { | 40 if (Utf::IsSupplementary(value)) { |
| 41 utf16_len += 1; | 41 utf16_len += 1; |
| 42 } | 42 } |
| 43 } | 43 } |
| 44 } | 44 } |
| 45 utf32_array[i] = value; | 45 utf32_array[i] = value; |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 return Smi::New(receiver.Length()); | 121 return Smi::New(receiver.Length()); |
| 122 } | 122 } |
| 123 | 123 |
| 124 | 124 |
| 125 static int32_t StringValueAt(const String& str, const Integer& index) { | 125 static int32_t StringValueAt(const String& str, const Integer& index) { |
| 126 if (index.IsSmi()) { | 126 if (index.IsSmi()) { |
| 127 Smi& smi = Smi::Handle(); | 127 Smi& smi = Smi::Handle(); |
| 128 smi ^= index.raw(); | 128 smi ^= index.raw(); |
| 129 int32_t index = smi.Value(); | 129 int32_t index = smi.Value(); |
| 130 if ((index < 0) || (index >= str.Length())) { | 130 if ((index < 0) || (index >= str.Length())) { |
| 131 GrowableArray<const Object*> arguments; | 131 const Array& args = Array::Handle(Array::New(1)); |
| 132 arguments.Add(&smi); | 132 args.SetAt(0, smi); |
| 133 Exceptions::ThrowByType(Exceptions::kRange, arguments); | 133 Exceptions::ThrowByType(Exceptions::kRange, args); |
| 134 } | 134 } |
| 135 return str.CharAt(index); | 135 return str.CharAt(index); |
| 136 } else { | 136 } else { |
| 137 // An index larger than Smi is always illegal. | 137 // An index larger than Smi is always illegal. |
| 138 GrowableArray<const Object*> arguments; | 138 const Array& args = Array::Handle(Array::New(1)); |
| 139 arguments.Add(&index); | 139 args.SetAt(0, index); |
| 140 Exceptions::ThrowByType(Exceptions::kRange, arguments); | 140 Exceptions::ThrowByType(Exceptions::kRange, args); |
| 141 return 0; | 141 return 0; |
| 142 } | 142 } |
| 143 } | 143 } |
| 144 | 144 |
| 145 | 145 |
| 146 DEFINE_NATIVE_ENTRY(String_charAt, 2) { | 146 DEFINE_NATIVE_ENTRY(String_charAt, 2) { |
| 147 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0)); | 147 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0)); |
| 148 GET_NON_NULL_NATIVE_ARGUMENT(Integer, index, arguments->NativeArgAt(1)); | 148 GET_NON_NULL_NATIVE_ARGUMENT(Integer, index, arguments->NativeArgAt(1)); |
| 149 uint32_t value = StringValueAt(receiver, index); | 149 uint32_t value = StringValueAt(receiver, index); |
| 150 ASSERT(value <= 0x10FFFF); | 150 ASSERT(value <= 0x10FFFF); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 | 184 |
| 185 | 185 |
| 186 DEFINE_NATIVE_ENTRY(Strings_concatAll, 1) { | 186 DEFINE_NATIVE_ENTRY(Strings_concatAll, 1) { |
| 187 GET_NON_NULL_NATIVE_ARGUMENT(Array, strings, arguments->NativeArgAt(0)); | 187 GET_NON_NULL_NATIVE_ARGUMENT(Array, strings, arguments->NativeArgAt(0)); |
| 188 ASSERT(!strings.IsNull()); | 188 ASSERT(!strings.IsNull()); |
| 189 // Check that the array contains strings. | 189 // Check that the array contains strings. |
| 190 Instance& elem = Instance::Handle(); | 190 Instance& elem = Instance::Handle(); |
| 191 for (intptr_t i = 0; i < strings.Length(); i++) { | 191 for (intptr_t i = 0; i < strings.Length(); i++) { |
| 192 elem ^= strings.At(i); | 192 elem ^= strings.At(i); |
| 193 if (!elem.IsString()) { | 193 if (!elem.IsString()) { |
| 194 GrowableArray<const Object*> args; | 194 const Array& args = Array::Handle(Array::New(1)); |
| 195 args.Add(&elem); | 195 args.SetAt(0, elem); |
| 196 Exceptions::ThrowByType(Exceptions::kArgument, args); | 196 Exceptions::ThrowByType(Exceptions::kArgument, args); |
| 197 } | 197 } |
| 198 } | 198 } |
| 199 return String::ConcatAll(strings); | 199 return String::ConcatAll(strings); |
| 200 } | 200 } |
| 201 | 201 |
| 202 } // namespace dart | 202 } // namespace dart |
| OLD | NEW |