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

Side by Side Diff: runtime/vm/isolate.cc

Issue 11558034: Second version of support for specifying an unhandled exception callback (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years 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 "vm/isolate.h" 5 #include "vm/isolate.h"
6 6
7 #include "include/dart_api.h" 7 #include "include/dart_api.h"
8 #include "platform/assert.h" 8 #include "platform/assert.h"
9 #include "lib/mirrors.h" 9 #include "lib/mirrors.h"
10 #include "vm/compiler_stats.h" 10 #include "vm/compiler_stats.h"
(...skipping 30 matching lines...) Expand all
41 const char* name() const; 41 const char* name() const;
42 void MessageNotify(Message::Priority priority); 42 void MessageNotify(Message::Priority priority);
43 bool HandleMessage(Message* message); 43 bool HandleMessage(Message* message);
44 44
45 #if defined(DEBUG) 45 #if defined(DEBUG)
46 // Check that it is safe to access this handler. 46 // Check that it is safe to access this handler.
47 void CheckAccess(); 47 void CheckAccess();
48 #endif 48 #endif
49 bool IsCurrentIsolate() const; 49 bool IsCurrentIsolate() const;
50 virtual Isolate* GetIsolate() const { return isolate_; } 50 virtual Isolate* GetIsolate() const { return isolate_; }
51 bool UnhandledExceptionCallbackHandler(const Object& message,
52 const UnhandledException& error);
51 53
52 private: 54 private:
53 bool ProcessUnhandledException(const Object& result); 55 bool ProcessUnhandledException(const Object& message, const Error& result);
54 56 RawFunction* ResolveCallbackFunction();
55 Isolate* isolate_; 57 Isolate* isolate_;
56 }; 58 };
57 59
58 60
59 IsolateMessageHandler::IsolateMessageHandler(Isolate* isolate) 61 IsolateMessageHandler::IsolateMessageHandler(Isolate* isolate)
60 : isolate_(isolate) { 62 : isolate_(isolate) {
61 } 63 }
62 64
63 65
64 IsolateMessageHandler::~IsolateMessageHandler() { 66 IsolateMessageHandler::~IsolateMessageHandler() {
(...skipping 21 matching lines...) Expand all
86 StartIsolateScope start_scope(isolate_); 88 StartIsolateScope start_scope(isolate_);
87 StackZone zone(isolate_); 89 StackZone zone(isolate_);
88 HandleScope handle_scope(isolate_); 90 HandleScope handle_scope(isolate_);
89 91
90 // Parse the message. 92 // Parse the message.
91 SnapshotReader reader(message->data(), message->len(), 93 SnapshotReader reader(message->data(), message->len(),
92 Snapshot::kMessage, Isolate::Current()); 94 Snapshot::kMessage, Isolate::Current());
93 const Object& msg_obj = Object::Handle(reader.ReadObject()); 95 const Object& msg_obj = Object::Handle(reader.ReadObject());
94 if (msg_obj.IsError()) { 96 if (msg_obj.IsError()) {
95 // An error occurred while reading the message. 97 // An error occurred while reading the message.
96 return ProcessUnhandledException(msg_obj); 98 return ProcessUnhandledException(Instance::Handle(), Error::Cast(msg_obj));
97 } 99 }
98 if (!msg_obj.IsNull() && !msg_obj.IsInstance()) { 100 if (!msg_obj.IsNull() && !msg_obj.IsInstance()) {
99 // TODO(turnidge): We need to decide what an isolate does with 101 // TODO(turnidge): We need to decide what an isolate does with
100 // malformed messages. If they (eventually) come from a remote 102 // malformed messages. If they (eventually) come from a remote
101 // machine, then it might make sense to drop the message entirely. 103 // machine, then it might make sense to drop the message entirely.
102 // In the case that the message originated locally, which is 104 // In the case that the message originated locally, which is
103 // always true for now, then this should never occur. 105 // always true for now, then this should never occur.
104 UNREACHABLE(); 106 UNREACHABLE();
105 } 107 }
106 108
107 Instance& msg = Instance::Handle(); 109 Instance& msg = Instance::Handle();
108 msg ^= msg_obj.raw(); // Can't use Instance::Cast because may be null. 110 msg ^= msg_obj.raw(); // Can't use Instance::Cast because may be null.
109 111
112 bool success = true;
110 if (message->IsOOB()) { 113 if (message->IsOOB()) {
111 // For now the only OOB messages are Mirrors messages. 114 // For now the only OOB messages are Mirrors messages.
112 HandleMirrorsMessage(isolate_, message->reply_port(), msg); 115 HandleMirrorsMessage(isolate_, message->reply_port(), msg);
113 delete message;
114 } else { 116 } else {
115 const Object& result = Object::Handle( 117 const Object& result = Object::Handle(
116 DartLibraryCalls::HandleMessage( 118 DartLibraryCalls::HandleMessage(
117 message->dest_port(), message->reply_port(), msg)); 119 message->dest_port(), message->reply_port(), msg));
118 delete message;
119 if (result.IsError()) { 120 if (result.IsError()) {
120 return ProcessUnhandledException(result); 121 success = ProcessUnhandledException(msg, Error::Cast(result));
122 } else {
123 ASSERT(result.IsNull());
121 } 124 }
122 ASSERT(result.IsNull());
123 } 125 }
124 return true; 126 delete message;
127 return success;
125 } 128 }
126 129
130 RawFunction* IsolateMessageHandler::ResolveCallbackFunction() {
131 ASSERT(isolate_->object_store()->unhandled_exception_handler() != NULL);
132 String& callback_name = String::Handle(isolate_);
133 if (isolate_->object_store()->unhandled_exception_handler() !=
134 String::null()) {
135 callback_name = isolate_->object_store()->unhandled_exception_handler();
136 } else {
137 callback_name = String::New("_unhandledExceptionCallback");
138 }
139 Library& lib = Library::Handle(isolate_);
siva 2012/12/14 06:28:36 can be written as const Library& lib = Library::Ha
Tom Ball 2012/12/14 21:22:44 Done.
140 Function& func = Function::Handle(isolate_);
141 lib ^= isolate_->object_store()->root_library();
142 func = lib.LookupLocalFunction(callback_name);
siva 2012/12/14 06:28:36 I thought for the spawnFunction case you wanted to
Tom Ball 2012/12/14 21:22:44 Now checks isolate_library first, and falls back t
143 return func.raw();
144 }
145
146
147 bool IsolateMessageHandler::UnhandledExceptionCallbackHandler(
148 const Object& message, const UnhandledException& error) {
149 const Instance& cause = Instance::Handle(isolate_, error.exception());
150 const Instance& stacktrace =
151 Instance::Handle(isolate_, error.stacktrace());
152
153 // Wrap these args into an IsolateUncaughtException object.
154 GrowableArray<const Object*> exception_args(3);
155 exception_args.Add(&message);
156 exception_args.Add(&cause);
157 exception_args.Add(&stacktrace);
158 Object& exception = Object::Handle();
siva 2012/12/14 06:28:36 Ditto comment const Object& exception = Object::Ha
Tom Ball 2012/12/14 21:22:44 Done.
159 exception = Exceptions::Create(Exceptions::kIsolateUnhandledException,
160 exception_args);
161 if (exception.IsError()) {
162 return false;
163 }
164 ASSERT(exception.IsInstance());
165
166 // Invoke script's callback function.
167 GrowableArray<const Object*> callback_args(0);
168 callback_args.Add(&exception);
169 const Array& kNoArgumentNames = Array::Handle(isolate_);
170 Object& function = Object::Handle(isolate_, ResolveCallbackFunction());
171 if (function.IsNull() || function.IsError()) {
172 return false;
173 }
174 const Object& result =
175 Object::Handle(DartEntry::InvokeStatic(Function::Cast(function),
176 callback_args,
177 kNoArgumentNames));
178 if (result.IsError()) {
179 const Error& err = Error::Cast(result);
180 OS::PrintErr("failed calling unhandled exception callback: %s\n",
181 err.ToErrorCString());
182 return false;
183 }
184
185 ASSERT(result.IsBool());
186 bool continue_from_exception = Bool::Cast(result).value();
187 if (continue_from_exception) {
188 isolate_->object_store()->clear_sticky_error();
189 }
190 return continue_from_exception;
191 }
127 192
128 #if defined(DEBUG) 193 #if defined(DEBUG)
129 void IsolateMessageHandler::CheckAccess() { 194 void IsolateMessageHandler::CheckAccess() {
130 ASSERT(IsCurrentIsolate()); 195 ASSERT(IsCurrentIsolate());
131 } 196 }
132 #endif 197 #endif
133 198
134 199
135 bool IsolateMessageHandler::IsCurrentIsolate() const { 200 bool IsolateMessageHandler::IsCurrentIsolate() const {
136 return (isolate_ == Isolate::Current()); 201 return (isolate_ == Isolate::Current());
137 } 202 }
138 203
139 204
140 bool IsolateMessageHandler::ProcessUnhandledException(const Object& result) { 205 bool IsolateMessageHandler::ProcessUnhandledException(
141 isolate_->object_store()->set_sticky_error(Error::Cast(result)); 206 const Object& message, const Error& result) {
142 // Invoke the dart unhandled exception callback if there is one. 207 if (result.IsUnhandledException()) {
208 // Invoke the isolate's uncaught exception handler, if it exists.
209 const UnhandledException& error = UnhandledException::Cast(result);
210 RawInstance* exception = error.exception();
211 if ((exception != isolate_->object_store()->out_of_memory()) &&
212 (exception != isolate_->object_store()->stack_overflow())) {
213 if (UnhandledExceptionCallbackHandler(message, error)) {
214 return true;
215 }
216 }
217 }
218
219 // Invoke the isolate's unhandled exception callback if there is one.
143 if (Isolate::UnhandledExceptionCallback() != NULL) { 220 if (Isolate::UnhandledExceptionCallback() != NULL) {
144 Dart_EnterScope(); 221 Dart_EnterScope();
145 Dart_Handle error = Api::NewHandle(isolate_, result.raw()); 222 Dart_Handle error = Api::NewHandle(isolate_, result.raw());
146 (Isolate::UnhandledExceptionCallback())(error); 223 (Isolate::UnhandledExceptionCallback())(error);
147 Dart_ExitScope(); 224 Dart_ExitScope();
148 } 225 }
226
227 isolate_->object_store()->set_sticky_error(result);
149 return false; 228 return false;
150 } 229 }
151 230
152 231
153 #if defined(DEBUG) 232 #if defined(DEBUG)
154 // static 233 // static
155 void BaseIsolate::AssertCurrent(BaseIsolate* isolate) { 234 void BaseIsolate::AssertCurrent(BaseIsolate* isolate) {
156 ASSERT(isolate == Isolate::Current()); 235 ASSERT(isolate == Isolate::Current());
157 } 236 }
158 #endif 237 #endif
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after
519 598
520 599
521 void Isolate::VisitWeakPersistentHandles(HandleVisitor* visitor, 600 void Isolate::VisitWeakPersistentHandles(HandleVisitor* visitor,
522 bool visit_prologue_weak_handles) { 601 bool visit_prologue_weak_handles) {
523 if (api_state() != NULL) { 602 if (api_state() != NULL) {
524 api_state()->VisitWeakHandles(visitor, visit_prologue_weak_handles); 603 api_state()->VisitWeakHandles(visitor, visit_prologue_weak_handles);
525 } 604 }
526 } 605 }
527 606
528 } // namespace dart 607 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698