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

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

Issue 21832003: Fix VM implementation of CastError not to extend TypeError (issue 5280). (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 4 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
« no previous file with comments | « runtime/lib/error.h ('k') | runtime/lib/error.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 #include "lib/error.h"
6
7 #include "vm/bootstrap_natives.h"
8 #include "vm/exceptions.h"
9 #include "vm/object_store.h"
10 #include "vm/runtime_entry.h"
11 #include "vm/stack_frame.h"
12
13 namespace dart {
14
15 DEFINE_FLAG(bool, trace_type_checks, false, "Trace runtime type checks.");
16
17 // Allocate and throw a new AssertionError.
18 // Arg0: index of the first token of the failed assertion.
19 // Arg1: index of the first token after the failed assertion.
20 // Return value: none, throws an exception.
21 DEFINE_NATIVE_ENTRY(AssertionError_throwNew, 2) {
22 // No need to type check the arguments. This function can only be called
23 // internally from the VM.
24 intptr_t assertion_start =
25 Smi::CheckedHandle(arguments->NativeArgAt(0)).Value();
26 intptr_t assertion_end =
27 Smi::CheckedHandle(arguments->NativeArgAt(1)).Value();
28
29 const Array& args = Array::Handle(Array::New(4));
30
31 DartFrameIterator iterator;
32 iterator.NextFrame(); // Skip native call.
33 const Script& script = Script::Handle(Exceptions::GetCallerScript(&iterator));
34
35 // Initialize argument 'failed_assertion' with source snippet.
36 intptr_t from_line, from_column;
37 script.GetTokenLocation(assertion_start, &from_line, &from_column);
38 intptr_t to_line, to_column;
39 script.GetTokenLocation(assertion_end, &to_line, &to_column);
40 args.SetAt(0, String::Handle(
41 script.GetSnippet(from_line, from_column, to_line, to_column)));
42
43 // Initialize location arguments starting at position 1.
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);
49 UNREACHABLE();
50 return Object::null();
51 }
52
53
54 // Allocate and throw a new TypeError.
55 // Arg0: index of the token of the failed type check.
56 // Arg1: src value.
57 // Arg2: dst type name.
58 // Arg3: dst name.
59 // Arg4: type error message.
60 // Return value: none, throws an exception.
61 DEFINE_NATIVE_ENTRY(TypeError_throwNew, 5) {
62 // No need to type check the arguments. This function can only be called
63 // internally from the VM.
64 intptr_t location = Smi::CheckedHandle(arguments->NativeArgAt(0)).Value();
65 const Instance& src_value =
66 Instance::CheckedHandle(arguments->NativeArgAt(1));
67 const String& dst_type_name =
68 String::CheckedHandle(arguments->NativeArgAt(2));
69 const String& dst_name = String::CheckedHandle(arguments->NativeArgAt(3));
70 const String& type_error = String::CheckedHandle(arguments->NativeArgAt(4));
71 const String& src_type_name =
72 String::Handle(Type::Handle(src_value.GetType()).UserVisibleName());
73 Exceptions::CreateAndThrowTypeError(location, src_type_name,
74 dst_type_name, dst_name, type_error);
75 UNREACHABLE();
76 return Object::null();
77 }
78
79
80 // Allocate and throw a new FallThroughError.
81 // Arg0: index of the case clause token into which we fall through.
82 // Return value: none, throws an exception.
83 DEFINE_NATIVE_ENTRY(FallThroughError_throwNew, 1) {
84 GET_NON_NULL_NATIVE_ARGUMENT(Smi, smi_pos, arguments->NativeArgAt(0));
85 intptr_t fallthrough_pos = smi_pos.Value();
86
87 const Array& args = Array::Handle(Array::New(2));
88
89 // Initialize 'url' and 'line' arguments.
90 DartFrameIterator iterator;
91 iterator.NextFrame(); // Skip native call.
92 const Script& script = Script::Handle(Exceptions::GetCallerScript(&iterator));
93 args.SetAt(0, String::Handle(script.url()));
94 intptr_t line, column;
95 script.GetTokenLocation(fallthrough_pos, &line, &column);
96 args.SetAt(1, Smi::Handle(Smi::New(line)));
97
98 Exceptions::ThrowByType(Exceptions::kFallThrough, args);
99 UNREACHABLE();
100 return Object::null();
101 }
102
103
104 // Allocate and throw a new AbstractClassInstantiationError.
105 // Arg0: Token position of allocation statement.
106 // Arg1: class name of the abstract class that cannot be instantiated.
107 // Return value: none, throws an exception.
108 DEFINE_NATIVE_ENTRY(AbstractClassInstantiationError_throwNew, 2) {
109 GET_NON_NULL_NATIVE_ARGUMENT(Smi, smi_pos, arguments->NativeArgAt(0));
110 GET_NON_NULL_NATIVE_ARGUMENT(String, class_name, arguments->NativeArgAt(1));
111 intptr_t error_pos = smi_pos.Value();
112
113 const Array& args = Array::Handle(Array::New(3));
114
115 // Initialize 'className', 'url' and 'line' arguments.
116 DartFrameIterator iterator;
117 iterator.NextFrame(); // Skip native call.
118 const Script& script = Script::Handle(Exceptions::GetCallerScript(&iterator));
119 args.SetAt(0, class_name);
120 args.SetAt(1, String::Handle(script.url()));
121 intptr_t line, column;
122 script.GetTokenLocation(error_pos, &line, &column);
123 args.SetAt(2, Smi::Handle(Smi::New(line)));
124
125 Exceptions::ThrowByType(Exceptions::kAbstractClassInstantiation, args);
126 UNREACHABLE();
127 return Object::null();
128 }
129
130 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/lib/error.h ('k') | runtime/lib/error.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698