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 "vm/bootstrap_natives.h" | 5 #include "vm/bootstrap_natives.h" |
6 #include "vm/exceptions.h" | 6 #include "vm/exceptions.h" |
7 #include "vm/object_store.h" | 7 #include "vm/object_store.h" |
8 #include "vm/runtime_entry.h" | 8 #include "vm/runtime_entry.h" |
9 #include "vm/stack_frame.h" | 9 #include "vm/stack_frame.h" |
| 10 #include "vm/symbols.h" |
10 | 11 |
11 namespace dart { | 12 namespace dart { |
12 | 13 |
| 14 // Scan the stack until we hit the first function in the _AssertionError |
| 15 // class. We then return the next frame's script taking inlining into account. |
| 16 static RawScript* FindScript(DartFrameIterator* iterator) { |
| 17 StackFrame* stack_frame = iterator->NextFrame(); |
| 18 Code& code = Code::Handle(); |
| 19 Function& func = Function::Handle(); |
| 20 const Class& assert_error_class = Class::Handle( |
| 21 Library::LookupCoreClass(Symbols::AssertionError())); |
| 22 ASSERT(!assert_error_class.IsNull()); |
| 23 bool hit_assertion_error = false; |
| 24 while (stack_frame != NULL) { |
| 25 code ^= stack_frame->LookupDartCode(); |
| 26 if (code.is_optimized()) { |
| 27 InlinedFunctionsIterator inlined_iterator(code, stack_frame->pc()); |
| 28 while (!inlined_iterator.Done()) { |
| 29 func ^= inlined_iterator.function(); |
| 30 if (hit_assertion_error) { |
| 31 return func.script(); |
| 32 } |
| 33 ASSERT(!hit_assertion_error); |
| 34 hit_assertion_error = (func.Owner() == assert_error_class.raw()); |
| 35 inlined_iterator.Advance(); |
| 36 } |
| 37 } else { |
| 38 func ^= code.function(); |
| 39 ASSERT(!func.IsNull()); |
| 40 if (hit_assertion_error) { |
| 41 return func.script(); |
| 42 } |
| 43 ASSERT(!hit_assertion_error); |
| 44 hit_assertion_error = (func.Owner() == assert_error_class.raw()); |
| 45 } |
| 46 stack_frame = iterator->NextFrame(); |
| 47 } |
| 48 UNREACHABLE(); |
| 49 return Script::null(); |
| 50 } |
| 51 |
| 52 |
13 // Allocate and throw a new AssertionError. | 53 // Allocate and throw a new AssertionError. |
14 // Arg0: index of the first token of the failed assertion. | 54 // Arg0: index of the first token of the failed assertion. |
15 // Arg1: index of the first token after the failed assertion. | 55 // Arg1: index of the first token after the failed assertion. |
16 // Return value: none, throws an exception. | 56 // Return value: none, throws an exception. |
17 DEFINE_NATIVE_ENTRY(AssertionError_throwNew, 2) { | 57 DEFINE_NATIVE_ENTRY(AssertionError_throwNew, 2) { |
18 // No need to type check the arguments. This function can only be called | 58 // No need to type check the arguments. This function can only be called |
19 // internally from the VM. | 59 // internally from the VM. |
20 const TokenPosition assertion_start = | 60 const TokenPosition assertion_start = |
21 TokenPosition(Smi::CheckedHandle(arguments->NativeArgAt(0)).Value()); | 61 TokenPosition(Smi::CheckedHandle(arguments->NativeArgAt(0)).Value()); |
22 const TokenPosition assertion_end = | 62 const TokenPosition assertion_end = |
23 TokenPosition(Smi::CheckedHandle(arguments->NativeArgAt(1)).Value()); | 63 TokenPosition(Smi::CheckedHandle(arguments->NativeArgAt(1)).Value()); |
24 | 64 |
25 const Array& args = Array::Handle(Array::New(4)); | 65 const Array& args = Array::Handle(Array::New(4)); |
26 | 66 |
27 DartFrameIterator iterator; | 67 DartFrameIterator iterator; |
28 iterator.NextFrame(); // Skip native call. | 68 iterator.NextFrame(); // Skip native call. |
29 iterator.NextFrame(); // Skip _AssertionError._checkAssertion frame | 69 const Script& script = Script::Handle(FindScript(&iterator)); |
30 const Script& script = Script::Handle(Exceptions::GetCallerScript(&iterator)); | |
31 | 70 |
32 // Initialize argument 'failed_assertion' with source snippet. | 71 // Initialize argument 'failed_assertion' with source snippet. |
33 intptr_t from_line, from_column; | 72 intptr_t from_line, from_column; |
34 script.GetTokenLocation(assertion_start, &from_line, &from_column); | 73 script.GetTokenLocation(assertion_start, &from_line, &from_column); |
35 intptr_t to_line, to_column; | 74 intptr_t to_line, to_column; |
36 script.GetTokenLocation(assertion_end, &to_line, &to_column); | 75 script.GetTokenLocation(assertion_end, &to_line, &to_column); |
37 // The snippet will extract the correct assertion code even if the source | 76 // The snippet will extract the correct assertion code even if the source |
38 // is generated. | 77 // is generated. |
39 args.SetAt(0, String::Handle( | 78 args.SetAt(0, String::Handle( |
40 script.GetSnippet(from_line, from_column, to_line, to_column))); | 79 script.GetSnippet(from_line, from_column, to_line, to_column))); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 | 168 |
130 // Rethrow an error with a stacktrace. | 169 // Rethrow an error with a stacktrace. |
131 DEFINE_NATIVE_ENTRY(Async_rethrow, 2) { | 170 DEFINE_NATIVE_ENTRY(Async_rethrow, 2) { |
132 GET_NON_NULL_NATIVE_ARGUMENT(Instance, error, arguments->NativeArgAt(0)); | 171 GET_NON_NULL_NATIVE_ARGUMENT(Instance, error, arguments->NativeArgAt(0)); |
133 GET_NON_NULL_NATIVE_ARGUMENT(Instance, stacktrace, arguments->NativeArgAt(1)); | 172 GET_NON_NULL_NATIVE_ARGUMENT(Instance, stacktrace, arguments->NativeArgAt(1)); |
134 Exceptions::ReThrow(thread, error, stacktrace); | 173 Exceptions::ReThrow(thread, error, stacktrace); |
135 return Object::null(); | 174 return Object::null(); |
136 } | 175 } |
137 | 176 |
138 } // namespace dart | 177 } // namespace dart |
OLD | NEW |