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->NativeArgAt(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; | 22 intptr_t utf16_len = array_len; |
23 uint32_t* utf32_array = zone->Alloc<uint32_t>(array_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 < array_len; i++) { | 25 for (intptr_t i = 0; i < array_len; i++) { |
(...skipping 18 matching lines...) Expand all Loading... |
44 utf32_array[i] = value; | 44 utf32_array[i] = value; |
45 } | 45 } |
46 if (is_one_byte_string) { | 46 if (is_one_byte_string) { |
47 return OneByteString::New(utf32_array, array_len, Heap::kNew); | 47 return OneByteString::New(utf32_array, array_len, Heap::kNew); |
48 } | 48 } |
49 return TwoByteString::New(utf16_len, utf32_array, array_len, Heap::kNew); | 49 return TwoByteString::New(utf16_len, utf32_array, array_len, Heap::kNew); |
50 } | 50 } |
51 | 51 |
52 | 52 |
53 DEFINE_NATIVE_ENTRY(StringBase_substringUnchecked, 3) { | 53 DEFINE_NATIVE_ENTRY(StringBase_substringUnchecked, 3) { |
54 GET_NATIVE_ARGUMENT(String, receiver, arguments->At(0)); | 54 GET_NATIVE_ARGUMENT(String, receiver, arguments->NativeArgAt(0)); |
55 GET_NATIVE_ARGUMENT(Smi, start_obj, arguments->At(1)); | 55 GET_NATIVE_ARGUMENT(Smi, start_obj, arguments->NativeArgAt(1)); |
56 GET_NATIVE_ARGUMENT(Smi, end_obj, arguments->At(2)); | 56 GET_NATIVE_ARGUMENT(Smi, end_obj, arguments->NativeArgAt(2)); |
57 | 57 |
58 intptr_t start = start_obj.Value(); | 58 intptr_t start = start_obj.Value(); |
59 intptr_t end = end_obj.Value(); | 59 intptr_t end = end_obj.Value(); |
60 return String::SubString(receiver, start, (end - start)); | 60 return String::SubString(receiver, start, (end - start)); |
61 } | 61 } |
62 | 62 |
63 | 63 |
64 DEFINE_NATIVE_ENTRY(String_getHashCode, 1) { | 64 DEFINE_NATIVE_ENTRY(String_getHashCode, 1) { |
65 const String& receiver = String::CheckedHandle(arguments->At(0)); | 65 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0)); |
66 intptr_t hash_val = receiver.Hash(); | 66 intptr_t hash_val = receiver.Hash(); |
67 ASSERT(hash_val > 0); | 67 ASSERT(hash_val > 0); |
68 ASSERT(Smi::IsValid(hash_val)); | 68 ASSERT(Smi::IsValid(hash_val)); |
69 return Smi::New(hash_val); | 69 return Smi::New(hash_val); |
70 } | 70 } |
71 | 71 |
72 | 72 |
73 DEFINE_NATIVE_ENTRY(String_getLength, 1) { | 73 DEFINE_NATIVE_ENTRY(String_getLength, 1) { |
74 const String& receiver = String::CheckedHandle(arguments->At(0)); | 74 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0)); |
75 return Smi::New(receiver.Length()); | 75 return Smi::New(receiver.Length()); |
76 } | 76 } |
77 | 77 |
78 | 78 |
79 static int32_t StringValueAt(const String& str, const Integer& index) { | 79 static int32_t StringValueAt(const String& str, const Integer& index) { |
80 if (index.IsSmi()) { | 80 if (index.IsSmi()) { |
81 Smi& smi = Smi::Handle(); | 81 Smi& smi = Smi::Handle(); |
82 smi ^= index.raw(); | 82 smi ^= index.raw(); |
83 int32_t index = smi.Value(); | 83 int32_t index = smi.Value(); |
84 if ((index < 0) || (index >= str.Length())) { | 84 if ((index < 0) || (index >= str.Length())) { |
85 GrowableArray<const Object*> arguments; | 85 GrowableArray<const Object*> arguments; |
86 arguments.Add(&smi); | 86 arguments.Add(&smi); |
87 Exceptions::ThrowByType(Exceptions::kRange, arguments); | 87 Exceptions::ThrowByType(Exceptions::kRange, arguments); |
88 } | 88 } |
89 return str.CharAt(index); | 89 return str.CharAt(index); |
90 } else { | 90 } else { |
91 // An index larger than Smi is always illegal. | 91 // An index larger than Smi is always illegal. |
92 GrowableArray<const Object*> arguments; | 92 GrowableArray<const Object*> arguments; |
93 arguments.Add(&index); | 93 arguments.Add(&index); |
94 Exceptions::ThrowByType(Exceptions::kRange, arguments); | 94 Exceptions::ThrowByType(Exceptions::kRange, arguments); |
95 return 0; | 95 return 0; |
96 } | 96 } |
97 } | 97 } |
98 | 98 |
99 | 99 |
100 DEFINE_NATIVE_ENTRY(String_charAt, 2) { | 100 DEFINE_NATIVE_ENTRY(String_charAt, 2) { |
101 const String& receiver = String::CheckedHandle(arguments->At(0)); | 101 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0)); |
102 GET_NATIVE_ARGUMENT(Integer, index, arguments->At(1)); | 102 GET_NATIVE_ARGUMENT(Integer, index, arguments->NativeArgAt(1)); |
103 uint32_t value = StringValueAt(receiver, index); | 103 uint32_t value = StringValueAt(receiver, index); |
104 ASSERT(value <= 0x10FFFF); | 104 ASSERT(value <= 0x10FFFF); |
105 return Symbols::New(&value, 1); | 105 return Symbols::New(&value, 1); |
106 } | 106 } |
107 | 107 |
108 DEFINE_NATIVE_ENTRY(String_charCodeAt, 2) { | 108 DEFINE_NATIVE_ENTRY(String_charCodeAt, 2) { |
109 const String& receiver = String::CheckedHandle(arguments->At(0)); | 109 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0)); |
110 GET_NATIVE_ARGUMENT(Integer, index, arguments->At(1)); | 110 GET_NATIVE_ARGUMENT(Integer, index, arguments->NativeArgAt(1)); |
| 111 |
111 int32_t value = StringValueAt(receiver, index); | 112 int32_t value = StringValueAt(receiver, index); |
112 ASSERT(value >= 0); | 113 ASSERT(value >= 0); |
113 ASSERT(value <= 0x10FFFF); | 114 ASSERT(value <= 0x10FFFF); |
114 return Smi::New(value); | 115 return Smi::New(value); |
115 } | 116 } |
116 | 117 |
117 | 118 |
118 DEFINE_NATIVE_ENTRY(String_concat, 2) { | 119 DEFINE_NATIVE_ENTRY(String_concat, 2) { |
119 const String& receiver = String::CheckedHandle(arguments->At(0)); | 120 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0)); |
120 GET_NATIVE_ARGUMENT(String, b, arguments->At(1)); | 121 GET_NATIVE_ARGUMENT(String, b, arguments->NativeArgAt(1)); |
121 return String::Concat(receiver, b); | 122 return String::Concat(receiver, b); |
122 } | 123 } |
123 | 124 |
124 | 125 |
125 DEFINE_NATIVE_ENTRY(String_toLowerCase, 1) { | 126 DEFINE_NATIVE_ENTRY(String_toLowerCase, 1) { |
126 const String& receiver = String::CheckedHandle(arguments->At(0)); | 127 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0)); |
127 ASSERT(!receiver.IsNull()); | 128 ASSERT(!receiver.IsNull()); |
128 return String::ToLowerCase(receiver); | 129 return String::ToLowerCase(receiver); |
129 } | 130 } |
130 | 131 |
131 | 132 |
132 DEFINE_NATIVE_ENTRY(String_toUpperCase, 1) { | 133 DEFINE_NATIVE_ENTRY(String_toUpperCase, 1) { |
133 const String& receiver = String::CheckedHandle(arguments->At(0)); | 134 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0)); |
134 ASSERT(!receiver.IsNull()); | 135 ASSERT(!receiver.IsNull()); |
135 return String::ToUpperCase(receiver); | 136 return String::ToUpperCase(receiver); |
136 } | 137 } |
137 | 138 |
138 | 139 |
139 DEFINE_NATIVE_ENTRY(Strings_concatAll, 1) { | 140 DEFINE_NATIVE_ENTRY(Strings_concatAll, 1) { |
140 GET_NATIVE_ARGUMENT(Array, strings, arguments->At(0)); | 141 GET_NATIVE_ARGUMENT(Array, strings, arguments->NativeArgAt(0)); |
141 ASSERT(!strings.IsNull()); | 142 ASSERT(!strings.IsNull()); |
142 // Check that the array contains strings. | 143 // Check that the array contains strings. |
143 Instance& elem = Instance::Handle(); | 144 Instance& elem = Instance::Handle(); |
144 for (intptr_t i = 0; i < strings.Length(); i++) { | 145 for (intptr_t i = 0; i < strings.Length(); i++) { |
145 elem ^= strings.At(i); | 146 elem ^= strings.At(i); |
146 if (elem.IsNull()) { | 147 if (elem.IsNull()) { |
147 GrowableArray<const Object*> args; | 148 GrowableArray<const Object*> args; |
148 Exceptions::ThrowByType(Exceptions::kNullPointer, args); | 149 Exceptions::ThrowByType(Exceptions::kNullPointer, args); |
149 } | 150 } |
150 if (!elem.IsString()) { | 151 if (!elem.IsString()) { |
151 GrowableArray<const Object*> args; | 152 GrowableArray<const Object*> args; |
152 args.Add(&elem); | 153 args.Add(&elem); |
153 Exceptions::ThrowByType(Exceptions::kArgument, args); | 154 Exceptions::ThrowByType(Exceptions::kArgument, args); |
154 } | 155 } |
155 } | 156 } |
156 return String::ConcatAll(strings); | 157 return String::ConcatAll(strings); |
157 } | 158 } |
158 | 159 |
159 } // namespace dart | 160 } // namespace dart |
OLD | NEW |