| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 "lib/error.h" | 5 #include "lib/error.h" |
| 6 | 6 |
| 7 #include "vm/bootstrap_natives.h" | 7 #include "vm/bootstrap_natives.h" |
| 8 #include "vm/exceptions.h" | 8 #include "vm/exceptions.h" |
| 9 #include "vm/object_store.h" | 9 #include "vm/object_store.h" |
| 10 #include "vm/runtime_entry.h" | 10 #include "vm/runtime_entry.h" |
| 11 #include "vm/stack_frame.h" | 11 #include "vm/stack_frame.h" |
| 12 | 12 |
| 13 namespace dart { | 13 namespace dart { |
| 14 | 14 |
| 15 DEFINE_FLAG(bool, trace_type_checks, false, "Trace runtime type checks."); | 15 DEFINE_FLAG(bool, trace_type_checks, false, "Trace runtime type checks."); |
| 16 | 16 |
| 17 // Allocate and throw a new AssertionError. | 17 // Allocate and throw a new AssertionError. |
| 18 // Arg0: index of the first token of the failed assertion. | 18 // Arg0: index of the first token of the failed assertion. |
| 19 // Arg1: index of the first token after the failed assertion. | 19 // Arg1: index of the first token after the failed assertion. |
| 20 // Return value: none, throws an exception. | 20 // Return value: none, throws an exception. |
| 21 DEFINE_NATIVE_ENTRY(AssertionError_throwNew, 2) { | 21 DEFINE_NATIVE_ENTRY(AssertionError_throwNew, 2) { |
| 22 // No need to type check the arguments. This function can only be called | 22 // No need to type check the arguments. This function can only be called |
| 23 // internally from the VM. | 23 // internally from the VM. |
| 24 intptr_t assertion_start = | 24 intptr_t assertion_start = |
| 25 Smi::CheckedHandle(arguments->NativeArgAt(0)).Value(); | 25 Smi::CheckedHandle(arguments->NativeArgAt(0)).Value(); |
| 26 intptr_t assertion_end = | 26 intptr_t assertion_end = |
| 27 Smi::CheckedHandle(arguments->NativeArgAt(1)).Value(); | 27 Smi::CheckedHandle(arguments->NativeArgAt(1)).Value(); |
| 28 | 28 |
| 29 // Allocate a new instance of type AssertionError. | 29 const Array& args = Array::Handle(Array::New(4)); |
| 30 const Instance& assertion_error = Instance::Handle( | |
| 31 Exceptions::NewInstance("AssertionErrorImplementation")); | |
| 32 | 30 |
| 33 // Initialize 'url', 'line', and 'column' fields. | |
| 34 DartFrameIterator iterator; | 31 DartFrameIterator iterator; |
| 35 iterator.NextFrame(); // Skip native call. | 32 iterator.NextFrame(); // Skip native call. |
| 36 const Script& script = Script::Handle(Exceptions::GetCallerScript(&iterator)); | 33 const Script& script = Script::Handle(Exceptions::GetCallerScript(&iterator)); |
| 37 const Class& cls = Class::Handle(assertion_error.clazz()); | |
| 38 Exceptions::SetLocationFields(assertion_error, cls, script, assertion_start); | |
| 39 | 34 |
| 40 // Initialize field 'failed_assertion' with source snippet. | 35 // Initialize argument 'failed_assertion' with source snippet. |
| 41 intptr_t from_line, from_column; | 36 intptr_t from_line, from_column; |
| 42 script.GetTokenLocation(assertion_start, &from_line, &from_column); | 37 script.GetTokenLocation(assertion_start, &from_line, &from_column); |
| 43 intptr_t to_line, to_column; | 38 intptr_t to_line, to_column; |
| 44 script.GetTokenLocation(assertion_end, &to_line, &to_column); | 39 script.GetTokenLocation(assertion_end, &to_line, &to_column); |
| 45 Exceptions::SetField(assertion_error, cls, "failedAssertion", String::Handle( | 40 args.SetAt(0, String::Handle( |
| 46 script.GetSnippet(from_line, from_column, to_line, to_column))); | 41 script.GetSnippet(from_line, from_column, to_line, to_column))); |
| 47 | 42 |
| 48 // Throw AssertionError instance. | 43 // Initialize location arguments starting at position 1. |
| 49 Exceptions::Throw(assertion_error); | 44 args.SetAt(1, String::Handle(script.url())); |
| 45 args.SetAt(2, Smi::Handle(Smi::New(from_line))); |
| 46 args.SetAt(3, Smi::Handle(Smi::New(from_column))); |
| 47 |
| 48 Exceptions::ThrowByType(Exceptions::kAssertion, args); |
| 50 UNREACHABLE(); | 49 UNREACHABLE(); |
| 51 return Object::null(); | 50 return Object::null(); |
| 52 } | 51 } |
| 53 | 52 |
| 54 | 53 |
| 55 // Allocate and throw a new TypeError. | 54 // Allocate and throw a new TypeError. |
| 56 // Arg0: index of the token of the failed type check. | 55 // Arg0: index of the token of the failed type check. |
| 57 // Arg1: src value. | 56 // Arg1: src value. |
| 58 // Arg2: dst type name. | 57 // Arg2: dst type name. |
| 59 // Arg3: dst name. | 58 // Arg3: dst name. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 78 } | 77 } |
| 79 | 78 |
| 80 | 79 |
| 81 // Allocate and throw a new FallThroughError. | 80 // Allocate and throw a new FallThroughError. |
| 82 // Arg0: index of the case clause token into which we fall through. | 81 // Arg0: index of the case clause token into which we fall through. |
| 83 // Return value: none, throws an exception. | 82 // Return value: none, throws an exception. |
| 84 DEFINE_NATIVE_ENTRY(FallThroughError_throwNew, 1) { | 83 DEFINE_NATIVE_ENTRY(FallThroughError_throwNew, 1) { |
| 85 GET_NON_NULL_NATIVE_ARGUMENT(Smi, smi_pos, arguments->NativeArgAt(0)); | 84 GET_NON_NULL_NATIVE_ARGUMENT(Smi, smi_pos, arguments->NativeArgAt(0)); |
| 86 intptr_t fallthrough_pos = smi_pos.Value(); | 85 intptr_t fallthrough_pos = smi_pos.Value(); |
| 87 | 86 |
| 88 // Allocate a new instance of type FallThroughError. | 87 const Array& args = Array::Handle(Array::New(2)); |
| 89 const Instance& fallthrough_error = Instance::Handle(Exceptions::NewInstance( | |
| 90 "FallThroughErrorImplementation")); | |
| 91 ASSERT(!fallthrough_error.IsNull()); | |
| 92 | 88 |
| 93 // Initialize 'url' and 'line' fields. | 89 // Initialize 'url' and 'line' arguments. |
| 94 DartFrameIterator iterator; | 90 DartFrameIterator iterator; |
| 95 iterator.NextFrame(); // Skip native call. | 91 iterator.NextFrame(); // Skip native call. |
| 96 const Script& script = Script::Handle(Exceptions::GetCallerScript(&iterator)); | 92 const Script& script = Script::Handle(Exceptions::GetCallerScript(&iterator)); |
| 97 const Class& cls = Class::Handle(fallthrough_error.clazz()); | 93 args.SetAt(0, String::Handle(script.url())); |
| 98 Exceptions::SetField(fallthrough_error, cls, "url", | |
| 99 String::Handle(script.url())); | |
| 100 intptr_t line, column; | 94 intptr_t line, column; |
| 101 script.GetTokenLocation(fallthrough_pos, &line, &column); | 95 script.GetTokenLocation(fallthrough_pos, &line, &column); |
| 102 Exceptions::SetField(fallthrough_error, cls, "line", | 96 args.SetAt(1, Smi::Handle(Smi::New(line))); |
| 103 Smi::Handle(Smi::New(line))); | |
| 104 | 97 |
| 105 // Throw FallThroughError instance. | 98 Exceptions::ThrowByType(Exceptions::kFallThrough, args); |
| 106 Exceptions::Throw(fallthrough_error); | |
| 107 UNREACHABLE(); | 99 UNREACHABLE(); |
| 108 return Object::null(); | 100 return Object::null(); |
| 109 } | 101 } |
| 110 | 102 |
| 111 | 103 |
| 112 // Allocate and throw a new AbstractClassInstantiationError. | 104 // Allocate and throw a new AbstractClassInstantiationError. |
| 113 // Arg0: Token position of allocation statement. | 105 // Arg0: Token position of allocation statement. |
| 114 // Arg1: class name of the abstract class that cannot be instantiated. | 106 // Arg1: class name of the abstract class that cannot be instantiated. |
| 115 // Return value: none, throws an exception. | 107 // Return value: none, throws an exception. |
| 116 DEFINE_NATIVE_ENTRY(AbstractClassInstantiationError_throwNew, 2) { | 108 DEFINE_NATIVE_ENTRY(AbstractClassInstantiationError_throwNew, 2) { |
| 117 GET_NON_NULL_NATIVE_ARGUMENT(Smi, smi_pos, arguments->NativeArgAt(0)); | 109 GET_NON_NULL_NATIVE_ARGUMENT(Smi, smi_pos, arguments->NativeArgAt(0)); |
| 118 GET_NON_NULL_NATIVE_ARGUMENT(String, class_name, arguments->NativeArgAt(1)); | 110 GET_NON_NULL_NATIVE_ARGUMENT(String, class_name, arguments->NativeArgAt(1)); |
| 119 intptr_t error_pos = smi_pos.Value(); | 111 intptr_t error_pos = smi_pos.Value(); |
| 120 | 112 |
| 121 // Allocate a new instance of type AbstractClassInstantiationError. | 113 const Array& args = Array::Handle(Array::New(3)); |
| 122 const Instance& error = Instance::Handle(Exceptions::NewInstance( | |
| 123 "AbstractClassInstantiationErrorImplementation")); | |
| 124 ASSERT(!error.IsNull()); | |
| 125 | 114 |
| 126 // Initialize 'url', 'line' and 'className' fields. | 115 // Initialize 'className', 'url' and 'line' arguments. |
| 127 DartFrameIterator iterator; | 116 DartFrameIterator iterator; |
| 128 iterator.NextFrame(); // Skip native call. | 117 iterator.NextFrame(); // Skip native call. |
| 129 const Script& script = Script::Handle(Exceptions::GetCallerScript(&iterator)); | 118 const Script& script = Script::Handle(Exceptions::GetCallerScript(&iterator)); |
| 130 const Class& cls = Class::Handle(error.clazz()); | 119 args.SetAt(0, class_name); |
| 131 Exceptions::SetField(error, cls, "url", String::Handle(script.url())); | 120 args.SetAt(1, String::Handle(script.url())); |
| 132 intptr_t line, column; | 121 intptr_t line, column; |
| 133 script.GetTokenLocation(error_pos, &line, &column); | 122 script.GetTokenLocation(error_pos, &line, &column); |
| 134 Exceptions::SetField(error, cls, "line", Smi::Handle(Smi::New(line))); | 123 args.SetAt(2, Smi::Handle(Smi::New(line))); |
| 135 Exceptions::SetField(error, cls, "className", class_name); | |
| 136 | 124 |
| 137 // Throw AbstractClassInstantiationError instance. | 125 Exceptions::ThrowByType(Exceptions::kAbstractClassInstantiation, args); |
| 138 Exceptions::Throw(error); | |
| 139 UNREACHABLE(); | 126 UNREACHABLE(); |
| 140 return Object::null(); | 127 return Object::null(); |
| 141 } | 128 } |
| 142 | 129 |
| 143 } // namespace dart | 130 } // namespace dart |
| OLD | NEW |