Chromium Code Reviews| Index: runtime/vm/dart_api_impl.cc |
| diff --git a/runtime/vm/dart_api_impl.cc b/runtime/vm/dart_api_impl.cc |
| index 8a9e31ecf3ecb99fbf2fba69486d8b3804c142d9..7f1c6f7d3c157576493cf907c4ddba7126b1451e 100644 |
| --- a/runtime/vm/dart_api_impl.cc |
| +++ b/runtime/vm/dart_api_impl.cc |
| @@ -357,6 +357,24 @@ static RawObject* Send1Arg(const Instance& receiver, |
| } |
| +static const char* GetErrorString(Thread* thread, const Object& obj) { |
| + if (obj.IsError()) { |
| + const Error& error = Error::Cast(obj); |
| + const char* str = error.ToErrorCString(); |
| + intptr_t len = strlen(str) + 1; |
| + char* str_copy = Api::TopScope(thread)->zone()->Alloc<char>(len); |
| + strncpy(str_copy, str, len); |
| + // Strip a possible trailing '\n'. |
| + if ((len > 1) && (str_copy[len - 2] == '\n')) { |
| + str_copy[len - 2] = '\0'; |
| + } |
| + return str_copy; |
| + } else { |
| + return ""; |
| + } |
| +} |
| + |
| + |
| Dart_Handle Api::InitNewHandle(Thread* thread, RawObject* raw) { |
| LocalHandles* local_handles = Api::TopScope(thread)->local_handles(); |
| ASSERT(local_handles != NULL); |
| @@ -451,7 +469,9 @@ Dart_Isolate Api::CastIsolate(Isolate* isolate) { |
| Dart_Handle Api::NewError(const char* format, ...) { |
| - DARTSCOPE(Thread::Current()); |
| + Thread* T = Thread::Current(); |
| + CHECK_API_SCOPE(T); |
| + HANDLESCOPE(T); |
| CHECK_CALLBACK_STATE(T); |
| va_list args; |
| @@ -748,20 +768,7 @@ DART_EXPORT const char* Dart_GetError(Dart_Handle handle) { |
| API_TIMELINE_DURATION; |
| DARTSCOPE(Thread::Current()); |
| const Object& obj = Object::Handle(Z, Api::UnwrapHandle(handle)); |
| - if (obj.IsError()) { |
| - const Error& error = Error::Cast(obj); |
| - const char* str = error.ToErrorCString(); |
| - intptr_t len = strlen(str) + 1; |
| - char* str_copy = Api::TopScope(T)->zone()->Alloc<char>(len); |
| - strncpy(str_copy, str, len); |
| - // Strip a possible trailing '\n'. |
| - if ((len > 1) && (str_copy[len - 2] == '\n')) { |
| - str_copy[len - 2] = '\0'; |
| - } |
| - return str_copy; |
| - } else { |
| - return ""; |
| - } |
| + return GetErrorString(T, obj); |
| } |
| @@ -818,7 +825,8 @@ DART_EXPORT Dart_Handle Dart_NewUnhandledExceptionError(Dart_Handle exception) { |
| Instance& obj = Instance::Handle(Z); |
| intptr_t class_id = Api::ClassId(exception); |
| if ((class_id == kApiErrorCid) || (class_id == kLanguageErrorCid)) { |
| - obj = String::New(::Dart_GetError(exception)); |
| + const Object& excp = Object::Handle(Z, Api::UnwrapHandle(exception)); |
| + obj = String::New(GetErrorString(T, excp)); |
| } else { |
| obj = Api::UnwrapInstanceHandle(Z, exception).raw(); |
| if (obj.IsNull()) { |
| @@ -1033,6 +1041,7 @@ DART_EXPORT Dart_WeakPersistentHandle Dart_NewWeakPersistentHandle( |
| if (callback == NULL) { |
| return NULL; |
| } |
| + TransitionNativeToVM transition(thread); |
| return AllocateFinalizableHandle(thread, |
| object, |
| peer, |
| @@ -1251,6 +1260,8 @@ DART_EXPORT Dart_Isolate Dart_CreateIsolate(const char* script_uri, |
| #endif // defined(DART_NO_SNAPSHOT). |
| // We exit the API scope entered above. |
| Dart_ExitScope(); |
| + T->set_execution_state(Thread::kThreadInNative); |
| + T->EnterSafepoint(); |
| return Api::CastIsolate(I); |
| } |
| *error = strdup(error_obj.ToErrorCString()); |
| @@ -1271,6 +1282,8 @@ DART_EXPORT void Dart_ShutdownIsolate() { |
| HandleScope handle_scope(T); |
| Dart::RunShutdownCallback(); |
| } |
| + T->ExitSafepoint(); |
| + T->set_execution_state(Thread::kThreadInVM); |
| Dart::ShutdownIsolate(); |
| } |
| @@ -1314,6 +1327,9 @@ DART_EXPORT void Dart_EnterIsolate(Dart_Isolate isolate) { |
| if (!Thread::EnterIsolate(iso)) { |
| FATAL("Unable to Enter Isolate as Dart VM is shutting down"); |
| } |
| + Thread* T = Thread::Current(); |
| + T->set_execution_state(Thread::kThreadInNative); |
| + T->EnterSafepoint(); |
| } |
| @@ -1532,6 +1548,9 @@ DART_EXPORT Dart_Handle Dart_RunLoop() { |
| if (!Thread::EnterIsolate(I)) { |
| FATAL("Inconsistent state, VM shutting down while in run loop."); |
| } |
| + Thread* thread = Thread::Current(); |
|
zra
2016/01/08 23:32:07
It looks like TransitionNativeToVM transition(thre
siva
2016/01/12 21:26:22
DARTSCOPE has a TransitionNativeToVM in it which t
|
| + thread->set_execution_state(Thread::kThreadInNative); |
| + thread->EnterSafepoint(); |
| } |
| if (I->object_store()->sticky_error() != Object::null()) { |
| Dart_Handle error = Api::NewHandle(T, I->object_store()->sticky_error()); |
| @@ -1552,6 +1571,7 @@ DART_EXPORT Dart_Handle Dart_HandleMessage() { |
| CHECK_API_SCOPE(T); |
| CHECK_CALLBACK_STATE(T); |
| API_TIMELINE_BEGIN_END; |
| + TransitionNativeToVM trainsition(T); |
| if (I->message_handler()->HandleNextMessage() != MessageHandler::kOK) { |
| Dart_Handle error = Api::NewHandle(T, I->object_store()->sticky_error()); |
| I->object_store()->clear_sticky_error(); |
| @@ -1567,6 +1587,7 @@ DART_EXPORT bool Dart_HandleServiceMessages() { |
| CHECK_API_SCOPE(T); |
| CHECK_CALLBACK_STATE(T); |
| API_TIMELINE_DURATION; |
| + TransitionNativeToVM trainsition(T); |
| ASSERT(I->GetAndClearResumeRequest() == false); |
| MessageHandler::MessageStatus status = |
| I->message_handler()->HandleOOBMessages(); |
| @@ -4847,10 +4868,11 @@ DART_EXPORT void Dart_SetWeakHandleReturnValue(Dart_NativeArguments args, |
| // --- Environment --- |
| RawString* Api::CallEnvironmentCallback(Thread* thread, const String& name) { |
| Isolate* isolate = thread->isolate(); |
| - Scope api_scope(thread); |
| Dart_EnvironmentCallback callback = isolate->environment_callback(); |
| String& result = String::Handle(thread->zone()); |
| if (callback != NULL) { |
| + TransitionVMToNative transition(thread); |
| + Scope api_scope(thread); |
| Dart_Handle response = callback(Api::NewHandle(thread, name.raw())); |
| if (::Dart_IsString(response)) { |
| result ^= Api::UnwrapHandle(response); |