Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(59)

Side by Side Diff: runtime/lib/error.cc

Issue 18531003: Cleanup VM error handling. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Rebase Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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));
siva 2013/07/08 20:57:55 I find this hard coding of 4 based on the knowledg
floitsch 2013/07/09 10:56:29 This has nothing to do with SetLocationFields. It'
30 const Instance& assertion_error = Instance::Handle( 30 intptr_t args_index = 0;
31 Exceptions::NewInstance("AssertionErrorImplementation"));
32 31
33 // Initialize 'url', 'line', and 'column' fields.
34 DartFrameIterator iterator; 32 DartFrameIterator iterator;
35 iterator.NextFrame(); // Skip native call. 33 iterator.NextFrame(); // Skip native call.
36 const Script& script = Script::Handle(Exceptions::GetCallerScript(&iterator)); 34 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 35
40 // Initialize field 'failed_assertion' with source snippet. 36 // Initialize argument 'failed_assertion' with source snippet.
41 intptr_t from_line, from_column; 37 intptr_t from_line, from_column;
42 script.GetTokenLocation(assertion_start, &from_line, &from_column); 38 script.GetTokenLocation(assertion_start, &from_line, &from_column);
43 intptr_t to_line, to_column; 39 intptr_t to_line, to_column;
44 script.GetTokenLocation(assertion_end, &to_line, &to_column); 40 script.GetTokenLocation(assertion_end, &to_line, &to_column);
45 Exceptions::SetField(assertion_error, cls, "failedAssertion", String::Handle( 41 args.SetAt(args_index++, String::Handle(
46 script.GetSnippet(from_line, from_column, to_line, to_column))); 42 script.GetSnippet(from_line, from_column, to_line, to_column)));
siva 2013/07/12 16:49:36 args.SetAt(0, ....);
floitsch 2013/07/12 17:12:36 Done.
47 43
48 // Throw AssertionError instance. 44 // Initialize location arguments starting at position 1.
49 Exceptions::Throw(assertion_error); 45 args_index +=
46 Exceptions::SetLocationFields(args, args_index, script, assertion_start);
47 ASSERT(args_index == args.Length());
siva 2013/07/12 16:49:36 I think this code is unreadable and hard to unders
floitsch 2013/07/12 17:12:36 Done.
48
49 Exceptions::ThrowByType(Exceptions::kAssertion, args);
50 UNREACHABLE(); 50 UNREACHABLE();
51 return Object::null(); 51 return Object::null();
52 } 52 }
53 53
54 54
55 // Allocate and throw a new TypeError. 55 // Allocate and throw a new TypeError.
56 // Arg0: index of the token of the failed type check. 56 // Arg0: index of the token of the failed type check.
57 // Arg1: src value. 57 // Arg1: src value.
58 // Arg2: dst type name. 58 // Arg2: dst type name.
59 // Arg3: dst name. 59 // Arg3: dst name.
(...skipping 18 matching lines...) Expand all
78 } 78 }
79 79
80 80
81 // Allocate and throw a new FallThroughError. 81 // Allocate and throw a new FallThroughError.
82 // Arg0: index of the case clause token into which we fall through. 82 // Arg0: index of the case clause token into which we fall through.
83 // Return value: none, throws an exception. 83 // Return value: none, throws an exception.
84 DEFINE_NATIVE_ENTRY(FallThroughError_throwNew, 1) { 84 DEFINE_NATIVE_ENTRY(FallThroughError_throwNew, 1) {
85 GET_NON_NULL_NATIVE_ARGUMENT(Smi, smi_pos, arguments->NativeArgAt(0)); 85 GET_NON_NULL_NATIVE_ARGUMENT(Smi, smi_pos, arguments->NativeArgAt(0));
86 intptr_t fallthrough_pos = smi_pos.Value(); 86 intptr_t fallthrough_pos = smi_pos.Value();
87 87
88 // Allocate a new instance of type FallThroughError. 88 const Array& args = Array::Handle(Array::New(2));
89 const Instance& fallthrough_error = Instance::Handle(Exceptions::NewInstance( 89 intptr_t args_index = 0;
90 "FallThroughErrorImplementation"));
91 ASSERT(!fallthrough_error.IsNull());
92 90
93 // Initialize 'url' and 'line' fields. 91 // Initialize 'url' and 'line' arguments.
94 DartFrameIterator iterator; 92 DartFrameIterator iterator;
95 iterator.NextFrame(); // Skip native call. 93 iterator.NextFrame(); // Skip native call.
96 const Script& script = Script::Handle(Exceptions::GetCallerScript(&iterator)); 94 const Script& script = Script::Handle(Exceptions::GetCallerScript(&iterator));
97 const Class& cls = Class::Handle(fallthrough_error.clazz()); 95 args.SetAt(args_index++, String::Handle(script.url()));
98 Exceptions::SetField(fallthrough_error, cls, "url",
99 String::Handle(script.url()));
100 intptr_t line, column; 96 intptr_t line, column;
101 script.GetTokenLocation(fallthrough_pos, &line, &column); 97 script.GetTokenLocation(fallthrough_pos, &line, &column);
102 Exceptions::SetField(fallthrough_error, cls, "line", 98 args.SetAt(args_index++, Smi::Handle(Smi::New(line)));
103 Smi::Handle(Smi::New(line))); 99 ASSERT(args_index == args.Length());
siva 2013/07/12 16:49:36 See comment above regarding using 0 and 1 etc. ins
floitsch 2013/07/12 17:12:36 Done.
104 100
105 // Throw FallThroughError instance. 101 Exceptions::ThrowByType(Exceptions::kFallThrough, args);
106 Exceptions::Throw(fallthrough_error);
107 UNREACHABLE(); 102 UNREACHABLE();
108 return Object::null(); 103 return Object::null();
109 } 104 }
110 105
111 106
112 // Allocate and throw a new AbstractClassInstantiationError. 107 // Allocate and throw a new AbstractClassInstantiationError.
113 // Arg0: Token position of allocation statement. 108 // Arg0: Token position of allocation statement.
114 // Arg1: class name of the abstract class that cannot be instantiated. 109 // Arg1: class name of the abstract class that cannot be instantiated.
115 // Return value: none, throws an exception. 110 // Return value: none, throws an exception.
116 DEFINE_NATIVE_ENTRY(AbstractClassInstantiationError_throwNew, 2) { 111 DEFINE_NATIVE_ENTRY(AbstractClassInstantiationError_throwNew, 2) {
117 GET_NON_NULL_NATIVE_ARGUMENT(Smi, smi_pos, arguments->NativeArgAt(0)); 112 GET_NON_NULL_NATIVE_ARGUMENT(Smi, smi_pos, arguments->NativeArgAt(0));
118 GET_NON_NULL_NATIVE_ARGUMENT(String, class_name, arguments->NativeArgAt(1)); 113 GET_NON_NULL_NATIVE_ARGUMENT(String, class_name, arguments->NativeArgAt(1));
119 intptr_t error_pos = smi_pos.Value(); 114 intptr_t error_pos = smi_pos.Value();
120 115
121 // Allocate a new instance of type AbstractClassInstantiationError. 116 const Array& args = Array::Handle(Array::New(3));
122 const Instance& error = Instance::Handle(Exceptions::NewInstance( 117 intptr_t args_index = 0;
123 "AbstractClassInstantiationErrorImplementation"));
124 ASSERT(!error.IsNull());
125 118
126 // Initialize 'url', 'line' and 'className' fields. 119 // Initialize 'className', 'url' and 'line' arguments.
127 DartFrameIterator iterator; 120 DartFrameIterator iterator;
128 iterator.NextFrame(); // Skip native call. 121 iterator.NextFrame(); // Skip native call.
129 const Script& script = Script::Handle(Exceptions::GetCallerScript(&iterator)); 122 const Script& script = Script::Handle(Exceptions::GetCallerScript(&iterator));
130 const Class& cls = Class::Handle(error.clazz()); 123 args.SetAt(args_index++, class_name);
131 Exceptions::SetField(error, cls, "url", String::Handle(script.url())); 124 args.SetAt(args_index++, String::Handle(script.url()));
132 intptr_t line, column; 125 intptr_t line, column;
133 script.GetTokenLocation(error_pos, &line, &column); 126 script.GetTokenLocation(error_pos, &line, &column);
134 Exceptions::SetField(error, cls, "line", Smi::Handle(Smi::New(line))); 127 args.SetAt(args_index++, Smi::Handle(Smi::New(line)));
135 Exceptions::SetField(error, cls, "className", class_name); 128 ASSERT(args_index == args.Length());
siva 2013/07/12 16:49:36 Ditto comment about args_index.
floitsch 2013/07/12 17:12:36 Done.
136 129
137 // Throw AbstractClassInstantiationError instance. 130 Exceptions::ThrowByType(Exceptions::kAbstractClassInstantiation, args);
138 Exceptions::Throw(error);
139 UNREACHABLE(); 131 UNREACHABLE();
140 return Object::null(); 132 return Object::null();
141 } 133 }
142 134
143 } // namespace dart 135 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/bin/dartutils.cc ('k') | runtime/lib/error.dart » ('j') | runtime/lib/error.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698