| 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/stack_frame.h" | 10 #include "vm/stack_frame.h" |
| 11 #include "vm/symbols.h" | 11 #include "vm/symbols.h" |
| 12 | 12 |
| 13 namespace dart { | 13 namespace dart { |
| 14 | 14 |
| 15 DECLARE_FLAG(bool, enable_type_checks); |
| 16 |
| 17 |
| 15 DEFINE_NATIVE_ENTRY(Object_toString, 1) { | 18 DEFINE_NATIVE_ENTRY(Object_toString, 1) { |
| 16 const Instance& instance = Instance::CheckedHandle(arguments->NativeArgAt(0)); | 19 const Instance& instance = Instance::CheckedHandle(arguments->NativeArgAt(0)); |
| 17 const char* c_str = instance.ToCString(); | 20 const char* c_str = instance.ToCString(); |
| 18 return String::New(c_str); | 21 return String::New(c_str); |
| 19 } | 22 } |
| 20 | 23 |
| 21 | 24 |
| 22 DEFINE_NATIVE_ENTRY(Object_noSuchMethod, 6) { | 25 DEFINE_NATIVE_ENTRY(Object_noSuchMethod, 6) { |
| 23 const Instance& instance = Instance::CheckedHandle(arguments->NativeArgAt(0)); | 26 const Instance& instance = Instance::CheckedHandle(arguments->NativeArgAt(0)); |
| 24 GET_NON_NULL_NATIVE_ARGUMENT(Bool, is_method, arguments->NativeArgAt(1)); | 27 GET_NON_NULL_NATIVE_ARGUMENT(Bool, is_method, arguments->NativeArgAt(1)); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 Error& malformed_error = Error::Handle(); | 86 Error& malformed_error = Error::Handle(); |
| 84 const bool is_instance_of = instance.IsInstanceOf(type, | 87 const bool is_instance_of = instance.IsInstanceOf(type, |
| 85 instantiator_type_arguments, | 88 instantiator_type_arguments, |
| 86 &malformed_error); | 89 &malformed_error); |
| 87 if (!is_instance_of && !malformed_error.IsNull()) { | 90 if (!is_instance_of && !malformed_error.IsNull()) { |
| 88 // Throw a dynamic type error only if the instanceof test fails. | 91 // Throw a dynamic type error only if the instanceof test fails. |
| 89 DartFrameIterator iterator; | 92 DartFrameIterator iterator; |
| 90 StackFrame* caller_frame = iterator.NextFrame(); | 93 StackFrame* caller_frame = iterator.NextFrame(); |
| 91 ASSERT(caller_frame != NULL); | 94 ASSERT(caller_frame != NULL); |
| 92 const intptr_t location = caller_frame->GetTokenPos(); | 95 const intptr_t location = caller_frame->GetTokenPos(); |
| 93 String& malformed_error_message = String::Handle( | 96 String& malformed_error_message = String::Handle( |
| 94 String::New(malformed_error.ToErrorCString())); | 97 String::New(malformed_error.ToErrorCString())); |
| 95 Exceptions::CreateAndThrowTypeError( | 98 Exceptions::CreateAndThrowTypeError( |
| 96 location, Symbols::Empty(), Symbols::Empty(), | 99 location, Symbols::Empty(), Symbols::Empty(), |
| 97 Symbols::Empty(), malformed_error_message); | 100 Symbols::Empty(), malformed_error_message); |
| 98 UNREACHABLE(); | 101 UNREACHABLE(); |
| 99 } | 102 } |
| 100 return Bool::Get(negate.value() ? !is_instance_of : is_instance_of); | 103 return Bool::Get(negate.value() ? !is_instance_of : is_instance_of); |
| 101 } | 104 } |
| 102 | 105 |
| 103 | 106 |
| 107 DEFINE_NATIVE_ENTRY(Object_as, 4) { |
| 108 const Instance& instance = Instance::CheckedHandle(arguments->NativeArgAt(0)); |
| 109 // Instantiator at position 1 is not used. It is passed along so that the call |
| 110 // can be easily converted to an optimized implementation. Instantiator is |
| 111 // used to populate the subtype cache. |
| 112 const AbstractTypeArguments& instantiator_type_arguments = |
| 113 AbstractTypeArguments::CheckedHandle(arguments->NativeArgAt(2)); |
| 114 const AbstractType& type = |
| 115 AbstractType::CheckedHandle(arguments->NativeArgAt(3)); |
| 116 ASSERT(type.IsFinalized()); |
| 117 ASSERT(!type.IsMalformed()); |
| 118 Error& malformed_error = Error::Handle(); |
| 119 if (instance.IsNull()) { |
| 120 return instance.raw(); |
| 121 } |
| 122 const bool is_instance_of = instance.IsInstanceOf(type, |
| 123 instantiator_type_arguments, |
| 124 &malformed_error); |
| 125 if (!is_instance_of) { |
| 126 DartFrameIterator iterator; |
| 127 StackFrame* caller_frame = iterator.NextFrame(); |
| 128 ASSERT(caller_frame != NULL); |
| 129 const intptr_t location = caller_frame->GetTokenPos(); |
| 130 const AbstractType& instance_type = |
| 131 AbstractType::Handle(instance.GetType()); |
| 132 const String& instance_type_name = |
| 133 String::Handle(instance_type.UserVisibleName()); |
| 134 String& type_name = String::Handle(); |
| 135 if (!type.IsInstantiated()) { |
| 136 // Instantiate type before reporting the error. |
| 137 const AbstractType& instantiated_type = AbstractType::Handle( |
| 138 type.InstantiateFrom(instantiator_type_arguments, NULL)); |
| 139 // Note that instantiated_type may be malformed. |
| 140 type_name = instantiated_type.UserVisibleName(); |
| 141 } else { |
| 142 type_name = type.UserVisibleName(); |
| 143 } |
| 144 String& malformed_error_message = String::Handle(); |
| 145 if (malformed_error.IsNull()) { |
| 146 const String& dst_name = String::ZoneHandle( |
| 147 Symbols::New(Exceptions::kCastErrorDstName)); |
| 148 |
| 149 Exceptions::CreateAndThrowTypeError( |
| 150 location, instance_type_name, type_name, |
| 151 dst_name, String::Handle()); |
| 152 } else { |
| 153 ASSERT(FLAG_enable_type_checks); |
| 154 malformed_error_message = String::New(malformed_error.ToErrorCString()); |
| 155 Exceptions::CreateAndThrowTypeError( |
| 156 location, instance_type_name, Symbols::Empty(), |
| 157 Symbols::Empty(), malformed_error_message); |
| 158 } |
| 159 UNREACHABLE(); |
| 160 } |
| 161 return instance.raw(); |
| 162 } |
| 163 |
| 164 |
| 104 DEFINE_NATIVE_ENTRY(AbstractType_toString, 1) { | 165 DEFINE_NATIVE_ENTRY(AbstractType_toString, 1) { |
| 105 const AbstractType& type = | 166 const AbstractType& type = |
| 106 AbstractType::CheckedHandle(arguments->NativeArgAt(0)); | 167 AbstractType::CheckedHandle(arguments->NativeArgAt(0)); |
| 107 return type.UserVisibleName(); | 168 return type.UserVisibleName(); |
| 108 } | 169 } |
| 109 | 170 |
| 110 } // namespace dart | 171 } // namespace dart |
| OLD | NEW |