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