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 "lib/invocation_mirror.h" | 7 #include "lib/invocation_mirror.h" |
8 #include "vm/code_patcher.h" | 8 #include "vm/code_patcher.h" |
9 #include "vm/exceptions.h" | 9 #include "vm/exceptions.h" |
10 #include "vm/heap.h" | 10 #include "vm/heap.h" |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 ASSERT(!type.IsMalformed()); | 131 ASSERT(!type.IsMalformed()); |
132 ASSERT(!type.IsMalbounded()); | 132 ASSERT(!type.IsMalbounded()); |
133 Error& bound_error = Error::Handle(zone, Error::null()); | 133 Error& bound_error = Error::Handle(zone, Error::null()); |
134 const bool is_instance_of = instance.IsInstanceOf(type, | 134 const bool is_instance_of = instance.IsInstanceOf(type, |
135 instantiator_type_arguments, | 135 instantiator_type_arguments, |
136 &bound_error); | 136 &bound_error); |
137 if (FLAG_trace_type_checks) { | 137 if (FLAG_trace_type_checks) { |
138 const char* result_str = is_instance_of ? "true" : "false"; | 138 const char* result_str = is_instance_of ? "true" : "false"; |
139 OS::Print("Native Object.instanceOf: result %s\n", result_str); | 139 OS::Print("Native Object.instanceOf: result %s\n", result_str); |
140 const AbstractType& instance_type = | 140 const AbstractType& instance_type = |
141 AbstractType::Handle(instance.GetType()); | 141 AbstractType::Handle(zone, instance.GetType()); |
142 OS::Print(" instance type: %s\n", | 142 OS::Print(" instance type: %s\n", |
143 String::Handle(instance_type.Name()).ToCString()); | 143 String::Handle(zone, instance_type.Name()).ToCString()); |
144 OS::Print(" test type: %s\n", String::Handle(type.Name()).ToCString()); | 144 OS::Print(" test type: %s\n", |
| 145 String::Handle(zone, type.Name()).ToCString()); |
145 if (!bound_error.IsNull()) { | 146 if (!bound_error.IsNull()) { |
146 OS::Print(" bound error: %s\n", bound_error.ToErrorCString()); | 147 OS::Print(" bound error: %s\n", bound_error.ToErrorCString()); |
147 } | 148 } |
148 } | 149 } |
149 if (!is_instance_of && !bound_error.IsNull()) { | 150 if (!is_instance_of && !bound_error.IsNull()) { |
150 // Throw a dynamic type error only if the instanceof test fails. | 151 // Throw a dynamic type error only if the instanceof test fails. |
151 DartFrameIterator iterator; | 152 DartFrameIterator iterator; |
152 StackFrame* caller_frame = iterator.NextFrame(); | 153 StackFrame* caller_frame = iterator.NextFrame(); |
153 ASSERT(caller_frame != NULL); | 154 ASSERT(caller_frame != NULL); |
154 const TokenPosition location = caller_frame->GetTokenPos(); | 155 const TokenPosition location = caller_frame->GetTokenPos(); |
155 String& bound_error_message = String::Handle( | 156 String& bound_error_message = String::Handle( |
156 zone, String::New(bound_error.ToErrorCString())); | 157 zone, String::New(bound_error.ToErrorCString())); |
157 Exceptions::CreateAndThrowTypeError( | 158 Exceptions::CreateAndThrowTypeError( |
158 location, Symbols::Empty(), Symbols::Empty(), | 159 location, AbstractType::Handle(zone), AbstractType::Handle(zone), |
159 Symbols::Empty(), bound_error_message); | 160 Symbols::Empty(), bound_error_message); |
160 UNREACHABLE(); | 161 UNREACHABLE(); |
161 } | 162 } |
162 return Bool::Get(negate.value() ? !is_instance_of : is_instance_of).raw(); | 163 return Bool::Get(negate.value() ? !is_instance_of : is_instance_of).raw(); |
163 } | 164 } |
164 | 165 |
165 | 166 |
166 DEFINE_NATIVE_ENTRY(Object_instanceOfNum, 2) { | 167 DEFINE_NATIVE_ENTRY(Object_instanceOfNum, 2) { |
167 const Instance& instance = | 168 const Instance& instance = |
168 Instance::CheckedHandle(zone, arguments->NativeArgAt(0)); | 169 Instance::CheckedHandle(zone, arguments->NativeArgAt(0)); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 const Bool& negate = Bool::CheckedHandle(zone, arguments->NativeArgAt(1)); | 218 const Bool& negate = Bool::CheckedHandle(zone, arguments->NativeArgAt(1)); |
218 bool is_instance_of = instance.IsString(); | 219 bool is_instance_of = instance.IsString(); |
219 if (negate.value()) { | 220 if (negate.value()) { |
220 is_instance_of = !is_instance_of; | 221 is_instance_of = !is_instance_of; |
221 } | 222 } |
222 return Bool::Get(is_instance_of).raw(); | 223 return Bool::Get(is_instance_of).raw(); |
223 } | 224 } |
224 | 225 |
225 | 226 |
226 DEFINE_NATIVE_ENTRY(Object_as, 3) { | 227 DEFINE_NATIVE_ENTRY(Object_as, 3) { |
227 const Instance& instance = Instance::CheckedHandle(arguments->NativeArgAt(0)); | 228 const Instance& instance = |
| 229 Instance::CheckedHandle(zone, arguments->NativeArgAt(0)); |
228 const TypeArguments& instantiator_type_arguments = | 230 const TypeArguments& instantiator_type_arguments = |
229 TypeArguments::CheckedHandle(arguments->NativeArgAt(1)); | 231 TypeArguments::CheckedHandle(zone, arguments->NativeArgAt(1)); |
230 const AbstractType& type = | 232 AbstractType& type = |
231 AbstractType::CheckedHandle(arguments->NativeArgAt(2)); | 233 AbstractType::CheckedHandle(zone, arguments->NativeArgAt(2)); |
232 ASSERT(type.IsFinalized()); | 234 ASSERT(type.IsFinalized()); |
233 ASSERT(!type.IsMalformed()); | 235 ASSERT(!type.IsMalformed()); |
234 ASSERT(!type.IsMalbounded()); | 236 ASSERT(!type.IsMalbounded()); |
235 Error& bound_error = Error::Handle(); | 237 Error& bound_error = Error::Handle(zone); |
236 if (instance.IsNull()) { | 238 if (instance.IsNull()) { |
237 return instance.raw(); | 239 return instance.raw(); |
238 } | 240 } |
239 const bool is_instance_of = instance.IsInstanceOf(type, | 241 const bool is_instance_of = instance.IsInstanceOf(type, |
240 instantiator_type_arguments, | 242 instantiator_type_arguments, |
241 &bound_error); | 243 &bound_error); |
242 if (FLAG_trace_type_checks) { | 244 if (FLAG_trace_type_checks) { |
243 const char* result_str = is_instance_of ? "true" : "false"; | 245 const char* result_str = is_instance_of ? "true" : "false"; |
244 OS::Print("Object.as: result %s\n", result_str); | 246 OS::Print("Object.as: result %s\n", result_str); |
245 const AbstractType& instance_type = | 247 const AbstractType& instance_type = |
246 AbstractType::Handle(instance.GetType()); | 248 AbstractType::Handle(zone, instance.GetType()); |
247 OS::Print(" instance type: %s\n", | 249 OS::Print(" instance type: %s\n", |
248 String::Handle(instance_type.Name()).ToCString()); | 250 String::Handle(zone, instance_type.Name()).ToCString()); |
249 OS::Print(" cast type: %s\n", String::Handle(type.Name()).ToCString()); | 251 OS::Print(" cast type: %s\n", |
| 252 String::Handle(zone, type.Name()).ToCString()); |
250 if (!bound_error.IsNull()) { | 253 if (!bound_error.IsNull()) { |
251 OS::Print(" bound error: %s\n", bound_error.ToErrorCString()); | 254 OS::Print(" bound error: %s\n", bound_error.ToErrorCString()); |
252 } | 255 } |
253 } | 256 } |
254 if (!is_instance_of) { | 257 if (!is_instance_of) { |
255 DartFrameIterator iterator; | 258 DartFrameIterator iterator; |
256 StackFrame* caller_frame = iterator.NextFrame(); | 259 StackFrame* caller_frame = iterator.NextFrame(); |
257 ASSERT(caller_frame != NULL); | 260 ASSERT(caller_frame != NULL); |
258 const TokenPosition location = caller_frame->GetTokenPos(); | 261 const TokenPosition location = caller_frame->GetTokenPos(); |
259 const AbstractType& instance_type = | 262 const AbstractType& instance_type = |
260 AbstractType::Handle(instance.GetType()); | 263 AbstractType::Handle(zone, instance.GetType()); |
261 const String& instance_type_name = | |
262 String::Handle(instance_type.UserVisibleName()); | |
263 String& type_name = String::Handle(); | |
264 if (!type.IsInstantiated()) { | 264 if (!type.IsInstantiated()) { |
265 // Instantiate type before reporting the error. | 265 // Instantiate type before reporting the error. |
266 const AbstractType& instantiated_type = AbstractType::Handle( | 266 type = type.InstantiateFrom(instantiator_type_arguments, NULL, |
267 type.InstantiateFrom(instantiator_type_arguments, NULL, | 267 NULL, NULL, Heap::kNew); |
268 NULL, NULL, Heap::kNew)); | 268 // Note that the instantiated type may be malformed. |
269 // Note that instantiated_type may be malformed. | |
270 type_name = instantiated_type.UserVisibleName(); | |
271 } else { | |
272 type_name = type.UserVisibleName(); | |
273 } | 269 } |
274 String& bound_error_message = String::Handle(); | |
275 if (bound_error.IsNull()) { | 270 if (bound_error.IsNull()) { |
276 const String& dst_name = String::ZoneHandle( | |
277 Symbols::New(Exceptions::kCastErrorDstName)); | |
278 | |
279 Exceptions::CreateAndThrowTypeError( | 271 Exceptions::CreateAndThrowTypeError( |
280 location, instance_type_name, type_name, | 272 location, instance_type, type, |
281 dst_name, Object::null_string()); | 273 Symbols::InTypeCast(), Object::null_string()); |
282 } else { | 274 } else { |
283 ASSERT(isolate->type_checks()); | 275 ASSERT(isolate->type_checks()); |
284 bound_error_message = String::New(bound_error.ToErrorCString()); | 276 const String& bound_error_message = |
| 277 String::Handle(zone, String::New(bound_error.ToErrorCString())); |
285 Exceptions::CreateAndThrowTypeError( | 278 Exceptions::CreateAndThrowTypeError( |
286 location, instance_type_name, Symbols::Empty(), | 279 location, instance_type, AbstractType::Handle(zone), |
287 Symbols::Empty(), bound_error_message); | 280 Symbols::Empty(), bound_error_message); |
288 } | 281 } |
289 UNREACHABLE(); | 282 UNREACHABLE(); |
290 } | 283 } |
291 return instance.raw(); | 284 return instance.raw(); |
292 } | 285 } |
293 | 286 |
294 | 287 |
295 DEFINE_NATIVE_ENTRY(AbstractType_toString, 1) { | 288 DEFINE_NATIVE_ENTRY(AbstractType_toString, 1) { |
296 const AbstractType& type = | 289 const AbstractType& type = |
297 AbstractType::CheckedHandle(arguments->NativeArgAt(0)); | 290 AbstractType::CheckedHandle(zone, arguments->NativeArgAt(0)); |
298 return type.UserVisibleName(); | 291 return type.UserVisibleName(); |
299 } | 292 } |
300 | 293 |
301 | 294 |
302 DEFINE_NATIVE_ENTRY(LibraryPrefix_invalidateDependentCode, 1) { | 295 DEFINE_NATIVE_ENTRY(LibraryPrefix_invalidateDependentCode, 1) { |
303 const LibraryPrefix& prefix = | 296 const LibraryPrefix& prefix = |
304 LibraryPrefix::CheckedHandle(arguments->NativeArgAt(0)); | 297 LibraryPrefix::CheckedHandle(zone, arguments->NativeArgAt(0)); |
305 prefix.InvalidateDependentCode(); | 298 prefix.InvalidateDependentCode(); |
306 return Bool::Get(true).raw(); | 299 return Bool::Get(true).raw(); |
307 } | 300 } |
308 | 301 |
309 | 302 |
310 DEFINE_NATIVE_ENTRY(LibraryPrefix_load, 1) { | 303 DEFINE_NATIVE_ENTRY(LibraryPrefix_load, 1) { |
311 const LibraryPrefix& prefix = | 304 const LibraryPrefix& prefix = |
312 LibraryPrefix::CheckedHandle(arguments->NativeArgAt(0)); | 305 LibraryPrefix::CheckedHandle(zone, arguments->NativeArgAt(0)); |
313 bool hasCompleted = prefix.LoadLibrary(); | 306 bool hasCompleted = prefix.LoadLibrary(); |
314 return Bool::Get(hasCompleted).raw(); | 307 return Bool::Get(hasCompleted).raw(); |
315 } | 308 } |
316 | 309 |
317 | 310 |
318 DEFINE_NATIVE_ENTRY(LibraryPrefix_loadError, 1) { | 311 DEFINE_NATIVE_ENTRY(LibraryPrefix_loadError, 1) { |
319 const LibraryPrefix& prefix = | 312 const LibraryPrefix& prefix = |
320 LibraryPrefix::CheckedHandle(arguments->NativeArgAt(0)); | 313 LibraryPrefix::CheckedHandle(zone, arguments->NativeArgAt(0)); |
321 // Currently all errors are Dart instances, e.g. I/O errors | 314 // Currently all errors are Dart instances, e.g. I/O errors |
322 // created by deferred loading code. LanguageErrors from | 315 // created by deferred loading code. LanguageErrors from |
323 // failed loading or finalization attempts are propagated and result | 316 // failed loading or finalization attempts are propagated and result |
324 // in the isolate's death. | 317 // in the isolate's death. |
325 const Instance& error = Instance::Handle(prefix.LoadError()); | 318 const Instance& error = Instance::Handle(zone, prefix.LoadError()); |
326 return error.raw(); | 319 return error.raw(); |
327 } | 320 } |
328 | 321 |
329 | 322 |
330 DEFINE_NATIVE_ENTRY(LibraryPrefix_isLoaded, 1) { | 323 DEFINE_NATIVE_ENTRY(LibraryPrefix_isLoaded, 1) { |
331 const LibraryPrefix& prefix = | 324 const LibraryPrefix& prefix = |
332 LibraryPrefix::CheckedHandle(arguments->NativeArgAt(0)); | 325 LibraryPrefix::CheckedHandle(zone, arguments->NativeArgAt(0)); |
333 return Bool::Get(prefix.is_loaded()).raw(); | 326 return Bool::Get(prefix.is_loaded()).raw(); |
334 } | 327 } |
335 | 328 |
336 | 329 |
337 DEFINE_NATIVE_ENTRY(Internal_inquireIs64Bit, 0) { | 330 DEFINE_NATIVE_ENTRY(Internal_inquireIs64Bit, 0) { |
338 #if defined(ARCH_IS_64_BIT) | 331 #if defined(ARCH_IS_64_BIT) |
339 return Bool::True().raw(); | 332 return Bool::True().raw(); |
340 #else | 333 #else |
341 return Bool::False().raw(); | 334 return Bool::False().raw(); |
342 #endif // defined(ARCH_IS_64_BIT) | 335 #endif // defined(ARCH_IS_64_BIT) |
343 } | 336 } |
344 | 337 |
345 } // namespace dart | 338 } // namespace dart |
OLD | NEW |