| 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 | 11 |
| 12 namespace dart { | 12 namespace dart { |
| 13 | 13 |
| 14 DEFINE_NATIVE_ENTRY(StringBase_createFromCodePoints, 1) { | 14 DEFINE_NATIVE_ENTRY(StringBase_createFromCodePoints, 1) { |
| 15 GET_NATIVE_ARGUMENT(Array, a, arguments->At(0)); | 15 GET_NATIVE_ARGUMENT(Array, a, arguments->At(0)); |
| 16 // TODO(srdjan): Check that parameterized type is an int. | 16 // TODO(srdjan): Check that parameterized type is an int. |
| 17 Zone* zone = isolate->current_zone(); | 17 Zone* zone = isolate->current_zone(); |
| 18 intptr_t len = a.Length(); | 18 intptr_t array_len = a.Length(); |
| 19 | 19 |
| 20 // Unbox the array and determine the maximum element width. | 20 // Unbox the array and determine the maximum element width. |
| 21 bool is_one_byte_string = true; | 21 bool is_one_byte_string = true; |
| 22 bool is_two_byte_string = true; | 22 intptr_t utf16_len = array_len; |
| 23 uint32_t* temp = zone->Alloc<uint32_t>(len); | 23 uint32_t* utf32_array = zone->Alloc<uint32_t>(array_len); |
| 24 Object& index_object = Object::Handle(isolate); | 24 Object& index_object = Object::Handle(isolate); |
| 25 for (intptr_t i = 0; i < len; i++) { | 25 for (intptr_t i = 0; i < array_len; i++) { |
| 26 index_object = a.At(i); | 26 index_object = a.At(i); |
| 27 if (!index_object.IsSmi()) { | 27 if (!index_object.IsSmi()) { |
| 28 GrowableArray<const Object*> args; | 28 GrowableArray<const Object*> args; |
| 29 args.Add(&index_object); | 29 args.Add(&index_object); |
| 30 Exceptions::ThrowByType(Exceptions::kArgument, args); | 30 Exceptions::ThrowByType(Exceptions::kArgument, args); |
| 31 } | 31 } |
| 32 intptr_t value = Smi::Cast(index_object).Value(); | 32 intptr_t value = Smi::Cast(index_object).Value(); |
| 33 if (value < 0) { | 33 if (value < 0) { |
| 34 GrowableArray<const Object*> args; | 34 GrowableArray<const Object*> args; |
| 35 Exceptions::ThrowByType(Exceptions::kArgument, args); | 35 Exceptions::ThrowByType(Exceptions::kArgument, args); |
| 36 } else if (value > 0xFFFF) { | 36 } else { |
| 37 is_one_byte_string = false; | 37 if (value > 0xFF) { |
| 38 is_two_byte_string = false; | 38 is_one_byte_string = false; |
| 39 } else if (value > 0xFF) { | 39 } |
| 40 is_one_byte_string = false; | 40 if (value > 0xFFFF) { |
| 41 utf16_len += 1; |
| 42 } |
| 41 } | 43 } |
| 42 temp[i] = value; | 44 utf32_array[i] = value; |
| 43 } | 45 } |
| 44 if (is_one_byte_string) { | 46 if (is_one_byte_string) { |
| 45 return OneByteString::New(temp, len, Heap::kNew); | 47 return OneByteString::New(utf32_array, array_len, Heap::kNew); |
| 46 } else if (is_two_byte_string) { | |
| 47 return TwoByteString::New(temp, len, Heap::kNew); | |
| 48 } else { | |
| 49 return FourByteString::New(temp, len, Heap::kNew); | |
| 50 } | 48 } |
| 49 return TwoByteString::New(utf16_len, utf32_array, array_len, Heap::kNew); |
| 51 } | 50 } |
| 52 | 51 |
| 53 | 52 |
| 54 DEFINE_NATIVE_ENTRY(StringBase_substringUnchecked, 3) { | 53 DEFINE_NATIVE_ENTRY(StringBase_substringUnchecked, 3) { |
| 55 GET_NATIVE_ARGUMENT(String, receiver, arguments->At(0)); | 54 GET_NATIVE_ARGUMENT(String, receiver, arguments->At(0)); |
| 56 GET_NATIVE_ARGUMENT(Smi, start_obj, arguments->At(1)); | 55 GET_NATIVE_ARGUMENT(Smi, start_obj, arguments->At(1)); |
| 57 GET_NATIVE_ARGUMENT(Smi, end_obj, arguments->At(2)); | 56 GET_NATIVE_ARGUMENT(Smi, end_obj, arguments->At(2)); |
| 58 | 57 |
| 59 intptr_t start = start_obj.Value(); | 58 intptr_t start = start_obj.Value(); |
| 60 intptr_t end = end_obj.Value(); | 59 intptr_t end = end_obj.Value(); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 if (!elem.IsString()) { | 150 if (!elem.IsString()) { |
| 152 GrowableArray<const Object*> args; | 151 GrowableArray<const Object*> args; |
| 153 args.Add(&elem); | 152 args.Add(&elem); |
| 154 Exceptions::ThrowByType(Exceptions::kArgument, args); | 153 Exceptions::ThrowByType(Exceptions::kArgument, args); |
| 155 } | 154 } |
| 156 } | 155 } |
| 157 return String::ConcatAll(strings); | 156 return String::ConcatAll(strings); |
| 158 } | 157 } |
| 159 | 158 |
| 160 } // namespace dart | 159 } // namespace dart |
| OLD | NEW |