Chromium Code Reviews| 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_createFromCodeUnits, 1) { |
|
cshapiro
2012/11/15 20:14:51
I might have missed something, but it seemed clear
erikcorry
2012/11/15 23:47:05
Done.
| |
| 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 array_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 intptr_t utf16_len = array_len; | |
| 23 uint32_t* utf32_array = zone->Alloc<uint32_t>(array_len); | 22 uint32_t* utf32_array = zone->Alloc<uint32_t>(array_len); |
| 24 Object& index_object = Object::Handle(isolate); | 23 Object& index_object = Object::Handle(isolate); |
| 25 for (intptr_t i = 0; i < array_len; i++) { | 24 for (intptr_t i = 0; i < array_len; i++) { |
| 26 index_object = a.At(i); | 25 index_object = a.At(i); |
| 27 if (!index_object.IsSmi()) { | 26 if (!index_object.IsSmi()) { |
| 28 GrowableArray<const Object*> args; | 27 GrowableArray<const Object*> args; |
| 29 args.Add(&index_object); | 28 args.Add(&index_object); |
| 30 Exceptions::ThrowByType(Exceptions::kArgument, args); | 29 Exceptions::ThrowByType(Exceptions::kArgument, args); |
| 31 } | 30 } |
| 32 intptr_t value = Smi::Cast(index_object).Value(); | 31 uintptr_t value = Smi::Cast(index_object).Value(); |
| 33 if (value < 0) { | 32 if (value > Utf16::kMaxCodeUnit) { |
| 34 GrowableArray<const Object*> args; | 33 GrowableArray<const Object*> args; |
| 35 Exceptions::ThrowByType(Exceptions::kArgument, args); | 34 Exceptions::ThrowByType(Exceptions::kArgument, args); |
| 36 } else { | 35 } else { |
| 37 if (value > 0x7F) { | 36 if (value > 0x7F) { |
| 38 is_one_byte_string = false; | 37 is_one_byte_string = false; |
| 39 } | 38 } |
| 40 if (value > 0xFFFF) { | |
| 41 utf16_len += 1; | |
| 42 } | |
| 43 } | 39 } |
| 44 utf32_array[i] = value; | 40 utf32_array[i] = value; |
| 45 } | 41 } |
| 46 if (is_one_byte_string) { | 42 if (is_one_byte_string) { |
| 47 return OneByteString::New(utf32_array, array_len, Heap::kNew); | 43 return OneByteString::New(utf32_array, array_len, Heap::kNew); |
| 48 } | 44 } |
| 49 return TwoByteString::New(utf16_len, utf32_array, array_len, Heap::kNew); | 45 return TwoByteString::New(array_len, utf32_array, array_len, Heap::kNew); |
| 50 } | 46 } |
| 51 | 47 |
| 52 | 48 |
| 53 DEFINE_NATIVE_ENTRY(StringBase_substringUnchecked, 3) { | 49 DEFINE_NATIVE_ENTRY(StringBase_substringUnchecked, 3) { |
| 54 GET_NATIVE_ARGUMENT(String, receiver, arguments->At(0)); | 50 GET_NATIVE_ARGUMENT(String, receiver, arguments->At(0)); |
| 55 GET_NATIVE_ARGUMENT(Smi, start_obj, arguments->At(1)); | 51 GET_NATIVE_ARGUMENT(Smi, start_obj, arguments->At(1)); |
| 56 GET_NATIVE_ARGUMENT(Smi, end_obj, arguments->At(2)); | 52 GET_NATIVE_ARGUMENT(Smi, end_obj, arguments->At(2)); |
| 57 | 53 |
| 58 intptr_t start = start_obj.Value(); | 54 intptr_t start = start_obj.Value(); |
| 59 intptr_t end = end_obj.Value(); | 55 intptr_t end = end_obj.Value(); |
| 60 return String::SubString(receiver, start, (end - start)); | 56 return String::SubString(receiver, start, (end - start)); |
| 61 } | 57 } |
| 62 | 58 |
| 63 | 59 |
| 64 DEFINE_NATIVE_ENTRY(String_getHashCode, 1) { | 60 DEFINE_NATIVE_ENTRY(String_getHashCode, 1) { |
| 65 const String& receiver = String::CheckedHandle(arguments->At(0)); | 61 const String& receiver = String::CheckedHandle(arguments->At(0)); |
| 66 intptr_t hash_val = receiver.Hash(); | 62 intptr_t hash_val = receiver.Hash(); |
| 67 ASSERT(hash_val > 0); | 63 ASSERT(hash_val > 0); |
| 68 ASSERT(Smi::IsValid(hash_val)); | 64 ASSERT(Smi::IsValid(hash_val)); |
| 69 return Smi::New(hash_val); | 65 return Smi::New(hash_val); |
| 70 } | 66 } |
| 71 | 67 |
| 72 | 68 |
| 73 DEFINE_NATIVE_ENTRY(String_getLength, 1) { | 69 DEFINE_NATIVE_ENTRY(String_getLength, 1) { |
| 74 const String& receiver = String::CheckedHandle(arguments->At(0)); | 70 const String& receiver = String::CheckedHandle(arguments->At(0)); |
| 75 return Smi::New(receiver.Length()); | 71 return Smi::New(receiver.Length()); |
| 76 } | 72 } |
| 77 | 73 |
| 78 | 74 |
| 79 static int32_t StringValueAt(const String& str, const Integer& index) { | 75 static int32_t StringCodeUnitAt(const String& str, const Integer& index) { |
| 80 if (index.IsSmi()) { | 76 if (index.IsSmi()) { |
| 81 Smi& smi = Smi::Handle(); | 77 Smi& smi = Smi::Handle(); |
| 82 smi ^= index.raw(); | 78 smi ^= index.raw(); |
| 83 int32_t index = smi.Value(); | 79 int32_t index = smi.Value(); |
| 84 if ((index < 0) || (index >= str.Length())) { | 80 if ((index < 0) || (index >= str.Length())) { |
| 85 GrowableArray<const Object*> arguments; | 81 GrowableArray<const Object*> arguments; |
| 86 arguments.Add(&smi); | 82 arguments.Add(&smi); |
| 87 Exceptions::ThrowByType(Exceptions::kRange, arguments); | 83 Exceptions::ThrowByType(Exceptions::kRange, arguments); |
| 88 } | 84 } |
| 89 return str.CharAt(index); | 85 return str.CodeUnitAt(index); |
| 90 } else { | 86 } else { |
| 91 // An index larger than Smi is always illegal. | 87 // An index larger than Smi is always illegal. |
| 92 GrowableArray<const Object*> arguments; | 88 GrowableArray<const Object*> arguments; |
| 93 arguments.Add(&index); | 89 arguments.Add(&index); |
| 94 Exceptions::ThrowByType(Exceptions::kRange, arguments); | 90 Exceptions::ThrowByType(Exceptions::kRange, arguments); |
| 95 return 0; | 91 return 0; |
| 96 } | 92 } |
| 97 } | 93 } |
| 98 | 94 |
| 99 | 95 |
| 100 DEFINE_NATIVE_ENTRY(String_charAt, 2) { | 96 DEFINE_NATIVE_ENTRY(String_charAt, 2) { |
| 101 const String& receiver = String::CheckedHandle(arguments->At(0)); | 97 const String& receiver = String::CheckedHandle(arguments->At(0)); |
| 102 GET_NATIVE_ARGUMENT(Integer, index, arguments->At(1)); | 98 GET_NATIVE_ARGUMENT(Integer, index, arguments->At(1)); |
| 103 uint32_t value = StringValueAt(receiver, index); | 99 uint32_t value = StringCodeUnitAt(receiver, index); |
| 104 ASSERT(value <= 0x10FFFF); | 100 // The VM does not GC interned strings, so we only create max 128 of them. |
|
cshapiro
2012/11/15 20:14:51
I am not sure why this ASSERT was removed. It is
erikcorry
2012/11/15 23:47:05
In the new version the value is always a UTF-16 co
| |
| 105 return Symbols::New(&value, 1); | 101 if (value <= 0x7f) { |
|
cshapiro
2012/11/15 20:14:51
Can we name this constant?
erikcorry
2012/11/15 23:47:05
kFrequentCharacterLimit.
| |
| 102 return Symbols::New(&value, 1); | |
| 103 } else { | |
| 104 return String::New(&value, 1); | |
| 105 } | |
| 106 } | 106 } |
| 107 | 107 |
| 108 DEFINE_NATIVE_ENTRY(String_charCodeAt, 2) { | 108 |
| 109 DEFINE_NATIVE_ENTRY(String_codeUnitAt, 2) { | |
| 109 const String& receiver = String::CheckedHandle(arguments->At(0)); | 110 const String& receiver = String::CheckedHandle(arguments->At(0)); |
| 110 GET_NATIVE_ARGUMENT(Integer, index, arguments->At(1)); | 111 GET_NATIVE_ARGUMENT(Integer, index, arguments->At(1)); |
| 111 int32_t value = StringValueAt(receiver, index); | 112 uint32_t value = StringCodeUnitAt(receiver, index); |
| 112 ASSERT(value >= 0); | 113 ASSERT(value <= Utf16::kMaxCodeUnit); |
| 113 ASSERT(value <= 0x10FFFF); | |
| 114 return Smi::New(value); | 114 return Smi::New(value); |
| 115 } | 115 } |
| 116 | 116 |
| 117 | 117 |
| 118 DEFINE_NATIVE_ENTRY(String_concat, 2) { | 118 DEFINE_NATIVE_ENTRY(String_concat, 2) { |
| 119 const String& receiver = String::CheckedHandle(arguments->At(0)); | 119 const String& receiver = String::CheckedHandle(arguments->At(0)); |
| 120 GET_NATIVE_ARGUMENT(String, b, arguments->At(1)); | 120 GET_NATIVE_ARGUMENT(String, b, arguments->At(1)); |
| 121 return String::Concat(receiver, b); | 121 return String::Concat(receiver, b); |
| 122 } | 122 } |
| 123 | 123 |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 150 if (!elem.IsString()) { | 150 if (!elem.IsString()) { |
| 151 GrowableArray<const Object*> args; | 151 GrowableArray<const Object*> args; |
| 152 args.Add(&elem); | 152 args.Add(&elem); |
| 153 Exceptions::ThrowByType(Exceptions::kArgument, args); | 153 Exceptions::ThrowByType(Exceptions::kArgument, args); |
| 154 } | 154 } |
| 155 } | 155 } |
| 156 return String::ConcatAll(strings); | 156 return String::ConcatAll(strings); |
| 157 } | 157 } |
| 158 | 158 |
| 159 } // namespace dart | 159 } // namespace dart |
| OLD | NEW |