Chromium Code Reviews| 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/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 Loading... | |
| 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 Loading... | |
| 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 Library& lib = Library::Handle(isolate_); | |
| 133 String& callback_name = String::Handle(isolate_); | |
| 134 if (isolate_->object_store()->unhandled_exception_handler() != | |
| 135 String::null()) { | |
| 136 callback_name = isolate_->object_store()->unhandled_exception_handler(); | |
| 137 } else { | |
| 138 callback_name = String::New("_unhandledExceptionCallback"); | |
| 139 } | |
| 140 const GrowableObjectArray& libs = | |
| 141 GrowableObjectArray::Handle(isolate_->object_store()->libraries()); | |
| 142 Function& func = Function::Handle(isolate_); | |
| 143 for (int i = 0; i < libs.Length(); i++) { | |
| 144 lib ^= libs.At(i); | |
| 145 func = lib.LookupLocalFunction(callback_name); | |
| 146 if (!func.IsNull()) { | |
| 147 return func.raw(); | |
| 148 } | |
| 149 } | |
|
siva
2012/12/13 18:30:38
How does this work if we have multiple libraries w
Tom Ball
2012/12/13 19:33:22
As we discussed, the function is now just looked u
| |
| 150 return Function::null(); | |
| 151 } | |
| 152 | |
| 153 | |
| 154 bool IsolateMessageHandler::UnhandledExceptionCallbackHandler( | |
| 155 const Object& message, const UnhandledException& error) { | |
| 156 const Instance& cause = Instance::Handle(isolate_, error.exception()); | |
| 157 const Instance& stacktrace = | |
| 158 Instance::Handle(isolate_, error.stacktrace()); | |
| 159 | |
| 160 // Wrap these args into an IsolateUncaughtException object. | |
| 161 GrowableArray<const Object*> exception_args(3); | |
| 162 exception_args.Add(&message); | |
| 163 exception_args.Add(&cause); | |
| 164 exception_args.Add(&stacktrace); | |
| 165 Object& exception = Object::Handle(); | |
| 166 exception = Exceptions::Create(Exceptions::kIsolateUnhandledException, | |
| 167 exception_args); | |
| 168 if (exception.IsError()) { | |
| 169 return false; | |
| 170 } | |
| 171 ASSERT(exception.IsInstance()); | |
| 172 | |
| 173 // Invoke script's callback function. | |
| 174 GrowableArray<const Object*> callback_args(0); | |
| 175 callback_args.Add(&exception); | |
| 176 const Array& kNoArgumentNames = Array::Handle(isolate_); | |
| 177 Object& function = Object::Handle(isolate_, ResolveCallbackFunction()); | |
| 178 if (function.IsNull()) { | |
| 179 return false; | |
| 180 } | |
| 181 RawObject* response = DartEntry::InvokeStatic(Function::Cast(function), | |
| 182 callback_args, | |
| 183 kNoArgumentNames); | |
| 184 const Object& result = Object::Handle(response); | |
| 185 if (result.IsError()) { | |
| 186 const Error& err = Error::Cast(result); | |
| 187 OS::PrintErr("failed calling unhandled exception callback: %s\n", | |
| 188 err.ToErrorCString()); | |
| 189 return false; | |
| 190 } | |
| 191 | |
| 192 ASSERT(result.IsBool()); | |
| 193 bool continue_from_exception = (response == Bool::True()); | |
| 194 if (continue_from_exception) { | |
| 195 isolate_->object_store()->clear_sticky_error(); | |
| 196 } | |
| 197 return continue_from_exception; | |
| 198 } | |
| 127 | 199 |
| 128 #if defined(DEBUG) | 200 #if defined(DEBUG) |
| 129 void IsolateMessageHandler::CheckAccess() { | 201 void IsolateMessageHandler::CheckAccess() { |
| 130 ASSERT(IsCurrentIsolate()); | 202 ASSERT(IsCurrentIsolate()); |
| 131 } | 203 } |
| 132 #endif | 204 #endif |
| 133 | 205 |
| 134 | 206 |
| 135 bool IsolateMessageHandler::IsCurrentIsolate() const { | 207 bool IsolateMessageHandler::IsCurrentIsolate() const { |
| 136 return (isolate_ == Isolate::Current()); | 208 return (isolate_ == Isolate::Current()); |
| 137 } | 209 } |
| 138 | 210 |
| 139 | 211 |
| 140 bool IsolateMessageHandler::ProcessUnhandledException(const Object& result) { | 212 bool IsolateMessageHandler::ProcessUnhandledException( |
| 141 isolate_->object_store()->set_sticky_error(Error::Cast(result)); | 213 const Object& message, const Error& result) { |
| 142 // Invoke the dart unhandled exception callback if there is one. | 214 if (result.IsUnhandledException()) { |
| 215 // Invoke the isolate's uncaught exception handler, if it exists. | |
| 216 const UnhandledException& error = UnhandledException::Cast(result); | |
| 217 RawInstance* exception = error.exception(); | |
| 218 if ((exception != isolate_->object_store()->out_of_memory()) && | |
| 219 (exception != isolate_->object_store()->stack_overflow())) { | |
| 220 if (UnhandledExceptionCallbackHandler(message, error)) { | |
| 221 return true; | |
| 222 } | |
| 223 } | |
| 224 } | |
| 225 | |
| 226 // Invoke the isolate's unhandled exception callback if there is one. | |
| 143 if (Isolate::UnhandledExceptionCallback() != NULL) { | 227 if (Isolate::UnhandledExceptionCallback() != NULL) { |
| 144 Dart_EnterScope(); | 228 Dart_EnterScope(); |
| 145 Dart_Handle error = Api::NewHandle(isolate_, result.raw()); | 229 Dart_Handle error = Api::NewHandle(isolate_, result.raw()); |
| 146 (Isolate::UnhandledExceptionCallback())(error); | 230 (Isolate::UnhandledExceptionCallback())(error); |
| 147 Dart_ExitScope(); | 231 Dart_ExitScope(); |
| 148 } | 232 } |
| 233 | |
| 234 isolate_->object_store()->set_sticky_error(result); | |
| 149 return false; | 235 return false; |
| 150 } | 236 } |
| 151 | 237 |
| 152 | 238 |
| 153 #if defined(DEBUG) | 239 #if defined(DEBUG) |
| 154 // static | 240 // static |
| 155 void BaseIsolate::AssertCurrent(BaseIsolate* isolate) { | 241 void BaseIsolate::AssertCurrent(BaseIsolate* isolate) { |
| 156 ASSERT(isolate == Isolate::Current()); | 242 ASSERT(isolate == Isolate::Current()); |
| 157 } | 243 } |
| 158 #endif | 244 #endif |
| (...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 519 | 605 |
| 520 | 606 |
| 521 void Isolate::VisitWeakPersistentHandles(HandleVisitor* visitor, | 607 void Isolate::VisitWeakPersistentHandles(HandleVisitor* visitor, |
| 522 bool visit_prologue_weak_handles) { | 608 bool visit_prologue_weak_handles) { |
| 523 if (api_state() != NULL) { | 609 if (api_state() != NULL) { |
| 524 api_state()->VisitWeakHandles(visitor, visit_prologue_weak_handles); | 610 api_state()->VisitWeakHandles(visitor, visit_prologue_weak_handles); |
| 525 } | 611 } |
| 526 } | 612 } |
| 527 | 613 |
| 528 } // namespace dart | 614 } // namespace dart |
| OLD | NEW |