| 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/heap.h" | 8 #include "vm/heap.h" |
| 9 #include "vm/native_entry.h" | 9 #include "vm/native_entry.h" |
| 10 #include "vm/object.h" | 10 #include "vm/object.h" |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 const Instance& instance = Instance::CheckedHandle(arguments->NativeArgAt(0)); | 106 const Instance& instance = Instance::CheckedHandle(arguments->NativeArgAt(0)); |
| 107 // Instantiator at position 1 is not used. It is passed along so that the call | 107 // Instantiator at position 1 is not used. It is passed along so that the call |
| 108 // can be easily converted to an optimized implementation. Instantiator is | 108 // can be easily converted to an optimized implementation. Instantiator is |
| 109 // used to populate the subtype cache. | 109 // used to populate the subtype cache. |
| 110 const AbstractTypeArguments& instantiator_type_arguments = | 110 const AbstractTypeArguments& instantiator_type_arguments = |
| 111 AbstractTypeArguments::CheckedHandle(arguments->NativeArgAt(2)); | 111 AbstractTypeArguments::CheckedHandle(arguments->NativeArgAt(2)); |
| 112 const AbstractType& type = | 112 const AbstractType& type = |
| 113 AbstractType::CheckedHandle(arguments->NativeArgAt(3)); | 113 AbstractType::CheckedHandle(arguments->NativeArgAt(3)); |
| 114 const Bool& negate = Bool::CheckedHandle(arguments->NativeArgAt(4)); | 114 const Bool& negate = Bool::CheckedHandle(arguments->NativeArgAt(4)); |
| 115 ASSERT(type.IsFinalized()); | 115 ASSERT(type.IsFinalized()); |
| 116 Error& malformed_error = Error::Handle(); | 116 ASSERT(!type.IsMalformed()); |
| 117 ASSERT(!type.IsMalbounded()); |
| 118 Error& bound_error = Error::Handle(); |
| 117 const bool is_instance_of = instance.IsInstanceOf(type, | 119 const bool is_instance_of = instance.IsInstanceOf(type, |
| 118 instantiator_type_arguments, | 120 instantiator_type_arguments, |
| 119 &malformed_error); | 121 &bound_error); |
| 120 if (FLAG_trace_type_checks) { | 122 if (FLAG_trace_type_checks) { |
| 121 const char* result_str = is_instance_of ? "true" : "false"; | 123 const char* result_str = is_instance_of ? "true" : "false"; |
| 122 OS::Print("Object.instanceOf: result %s\n", result_str); | 124 OS::Print("Object.instanceOf: result %s\n", result_str); |
| 123 const Type& instance_type = Type::Handle(instance.GetType()); | 125 const Type& instance_type = Type::Handle(instance.GetType()); |
| 124 OS::Print(" instance type: %s\n", | 126 OS::Print(" instance type: %s\n", |
| 125 String::Handle(instance_type.Name()).ToCString()); | 127 String::Handle(instance_type.Name()).ToCString()); |
| 126 OS::Print(" test type: %s\n", String::Handle(type.Name()).ToCString()); | 128 OS::Print(" test type: %s\n", String::Handle(type.Name()).ToCString()); |
| 127 if (!malformed_error.IsNull()) { | 129 if (!bound_error.IsNull()) { |
| 128 OS::Print(" malformed error: %s\n", malformed_error.ToErrorCString()); | 130 OS::Print(" bound error: %s\n", bound_error.ToErrorCString()); |
| 129 } | 131 } |
| 130 } | 132 } |
| 131 if (!is_instance_of && !malformed_error.IsNull()) { | 133 if (!is_instance_of && !bound_error.IsNull()) { |
| 132 // Throw a dynamic type error only if the instanceof test fails. | 134 // Throw a dynamic type error only if the instanceof test fails. |
| 133 DartFrameIterator iterator; | 135 DartFrameIterator iterator; |
| 134 StackFrame* caller_frame = iterator.NextFrame(); | 136 StackFrame* caller_frame = iterator.NextFrame(); |
| 135 ASSERT(caller_frame != NULL); | 137 ASSERT(caller_frame != NULL); |
| 136 const intptr_t location = caller_frame->GetTokenPos(); | 138 const intptr_t location = caller_frame->GetTokenPos(); |
| 137 String& malformed_error_message = String::Handle( | 139 String& bound_error_message = String::Handle( |
| 138 String::New(malformed_error.ToErrorCString())); | 140 String::New(bound_error.ToErrorCString())); |
| 139 Exceptions::CreateAndThrowTypeError( | 141 Exceptions::CreateAndThrowTypeError( |
| 140 location, Symbols::Empty(), Symbols::Empty(), | 142 location, Symbols::Empty(), Symbols::Empty(), |
| 141 Symbols::Empty(), malformed_error_message); | 143 Symbols::Empty(), bound_error_message); |
| 142 UNREACHABLE(); | 144 UNREACHABLE(); |
| 143 } | 145 } |
| 144 return Bool::Get(negate.value() ? !is_instance_of : is_instance_of).raw(); | 146 return Bool::Get(negate.value() ? !is_instance_of : is_instance_of).raw(); |
| 145 } | 147 } |
| 146 | 148 |
| 147 | 149 |
| 148 DEFINE_NATIVE_ENTRY(Object_as, 4) { | 150 DEFINE_NATIVE_ENTRY(Object_as, 4) { |
| 149 const Instance& instance = Instance::CheckedHandle(arguments->NativeArgAt(0)); | 151 const Instance& instance = Instance::CheckedHandle(arguments->NativeArgAt(0)); |
| 150 // Instantiator at position 1 is not used. It is passed along so that the call | 152 // Instantiator at position 1 is not used. It is passed along so that the call |
| 151 // can be easily converted to an optimized implementation. Instantiator is | 153 // can be easily converted to an optimized implementation. Instantiator is |
| 152 // used to populate the subtype cache. | 154 // used to populate the subtype cache. |
| 153 const AbstractTypeArguments& instantiator_type_arguments = | 155 const AbstractTypeArguments& instantiator_type_arguments = |
| 154 AbstractTypeArguments::CheckedHandle(arguments->NativeArgAt(2)); | 156 AbstractTypeArguments::CheckedHandle(arguments->NativeArgAt(2)); |
| 155 const AbstractType& type = | 157 const AbstractType& type = |
| 156 AbstractType::CheckedHandle(arguments->NativeArgAt(3)); | 158 AbstractType::CheckedHandle(arguments->NativeArgAt(3)); |
| 157 ASSERT(type.IsFinalized()); | 159 ASSERT(type.IsFinalized()); |
| 158 ASSERT(!type.IsMalformed()); | 160 ASSERT(!type.IsMalformed()); |
| 159 Error& malformed_error = Error::Handle(); | 161 ASSERT(!type.IsMalbounded()); |
| 162 Error& bound_error = Error::Handle(); |
| 160 if (instance.IsNull()) { | 163 if (instance.IsNull()) { |
| 161 return instance.raw(); | 164 return instance.raw(); |
| 162 } | 165 } |
| 163 const bool is_instance_of = instance.IsInstanceOf(type, | 166 const bool is_instance_of = instance.IsInstanceOf(type, |
| 164 instantiator_type_arguments, | 167 instantiator_type_arguments, |
| 165 &malformed_error); | 168 &bound_error); |
| 169 if (FLAG_trace_type_checks) { |
| 170 const char* result_str = is_instance_of ? "true" : "false"; |
| 171 OS::Print("Object.as: result %s\n", result_str); |
| 172 const Type& instance_type = Type::Handle(instance.GetType()); |
| 173 OS::Print(" instance type: %s\n", |
| 174 String::Handle(instance_type.Name()).ToCString()); |
| 175 OS::Print(" cast type: %s\n", String::Handle(type.Name()).ToCString()); |
| 176 if (!bound_error.IsNull()) { |
| 177 OS::Print(" bound error: %s\n", bound_error.ToErrorCString()); |
| 178 } |
| 179 } |
| 166 if (!is_instance_of) { | 180 if (!is_instance_of) { |
| 167 DartFrameIterator iterator; | 181 DartFrameIterator iterator; |
| 168 StackFrame* caller_frame = iterator.NextFrame(); | 182 StackFrame* caller_frame = iterator.NextFrame(); |
| 169 ASSERT(caller_frame != NULL); | 183 ASSERT(caller_frame != NULL); |
| 170 const intptr_t location = caller_frame->GetTokenPos(); | 184 const intptr_t location = caller_frame->GetTokenPos(); |
| 171 const AbstractType& instance_type = | 185 const AbstractType& instance_type = |
| 172 AbstractType::Handle(instance.GetType()); | 186 AbstractType::Handle(instance.GetType()); |
| 173 const String& instance_type_name = | 187 const String& instance_type_name = |
| 174 String::Handle(instance_type.UserVisibleName()); | 188 String::Handle(instance_type.UserVisibleName()); |
| 175 String& type_name = String::Handle(); | 189 String& type_name = String::Handle(); |
| 176 if (!type.IsInstantiated()) { | 190 if (!type.IsInstantiated()) { |
| 177 // Instantiate type before reporting the error. | 191 // Instantiate type before reporting the error. |
| 178 const AbstractType& instantiated_type = AbstractType::Handle( | 192 const AbstractType& instantiated_type = AbstractType::Handle( |
| 179 type.InstantiateFrom(instantiator_type_arguments, NULL)); | 193 type.InstantiateFrom(instantiator_type_arguments, NULL)); |
| 180 // Note that instantiated_type may be malformed. | 194 // Note that instantiated_type may be malformed. |
| 181 type_name = instantiated_type.UserVisibleName(); | 195 type_name = instantiated_type.UserVisibleName(); |
| 182 } else { | 196 } else { |
| 183 type_name = type.UserVisibleName(); | 197 type_name = type.UserVisibleName(); |
| 184 } | 198 } |
| 185 String& malformed_error_message = String::Handle(); | 199 String& bound_error_message = String::Handle(); |
| 186 if (malformed_error.IsNull()) { | 200 if (bound_error.IsNull()) { |
| 187 const String& dst_name = String::ZoneHandle( | 201 const String& dst_name = String::ZoneHandle( |
| 188 Symbols::New(Exceptions::kCastErrorDstName)); | 202 Symbols::New(Exceptions::kCastErrorDstName)); |
| 189 | 203 |
| 190 Exceptions::CreateAndThrowTypeError( | 204 Exceptions::CreateAndThrowTypeError( |
| 191 location, instance_type_name, type_name, | 205 location, instance_type_name, type_name, |
| 192 dst_name, Object::null_string()); | 206 dst_name, Object::null_string()); |
| 193 } else { | 207 } else { |
| 194 ASSERT(FLAG_enable_type_checks); | 208 ASSERT(FLAG_enable_type_checks); |
| 195 malformed_error_message = String::New(malformed_error.ToErrorCString()); | 209 bound_error_message = String::New(bound_error.ToErrorCString()); |
| 196 Exceptions::CreateAndThrowTypeError( | 210 Exceptions::CreateAndThrowTypeError( |
| 197 location, instance_type_name, Symbols::Empty(), | 211 location, instance_type_name, Symbols::Empty(), |
| 198 Symbols::Empty(), malformed_error_message); | 212 Symbols::Empty(), bound_error_message); |
| 199 } | 213 } |
| 200 UNREACHABLE(); | 214 UNREACHABLE(); |
| 201 } | 215 } |
| 202 return instance.raw(); | 216 return instance.raw(); |
| 203 } | 217 } |
| 204 | 218 |
| 205 | 219 |
| 206 DEFINE_NATIVE_ENTRY(AbstractType_toString, 1) { | 220 DEFINE_NATIVE_ENTRY(AbstractType_toString, 1) { |
| 207 const AbstractType& type = | 221 const AbstractType& type = |
| 208 AbstractType::CheckedHandle(arguments->NativeArgAt(0)); | 222 AbstractType::CheckedHandle(arguments->NativeArgAt(0)); |
| 209 return type.UserVisibleName(); | 223 return type.UserVisibleName(); |
| 210 } | 224 } |
| 211 | 225 |
| 212 } // namespace dart | 226 } // namespace dart |
| OLD | NEW |