| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 "include/dart_api.h" | 5 #include "include/dart_api.h" |
| 6 | 6 |
| 7 #include "vm/bigint_operations.h" | 7 #include "vm/bigint_operations.h" |
| 8 #include "vm/class_finalizer.h" | 8 #include "vm/class_finalizer.h" |
| 9 #include "vm/compiler.h" | 9 #include "vm/compiler.h" |
| 10 #include "vm/dart.h" | 10 #include "vm/dart.h" |
| 11 #include "vm/dart_api_impl.h" | 11 #include "vm/dart_api_impl.h" |
| 12 #include "vm/dart_api_state.h" | 12 #include "vm/dart_api_state.h" |
| 13 #include "vm/dart_entry.h" | 13 #include "vm/dart_entry.h" |
| 14 #include "vm/debuginfo.h" | 14 #include "vm/debuginfo.h" |
| 15 #include "vm/exceptions.h" | 15 #include "vm/exceptions.h" |
| 16 #include "vm/growable_array.h" | 16 #include "vm/growable_array.h" |
| 17 #include "vm/longjump.h" | 17 #include "vm/longjump.h" |
| 18 #include "vm/native_entry.h" | 18 #include "vm/native_entry.h" |
| 19 #include "vm/object.h" | 19 #include "vm/object.h" |
| 20 #include "vm/object_store.h" | 20 #include "vm/object_store.h" |
| 21 #include "vm/port.h" | 21 #include "vm/port.h" |
| 22 #include "vm/resolver.h" | 22 #include "vm/resolver.h" |
| 23 #include "vm/snapshot.h" | 23 #include "vm/snapshot.h" |
| 24 #include "vm/stack_frame.h" | 24 #include "vm/stack_frame.h" |
| 25 #include "vm/timer.h" | 25 #include "vm/timer.h" |
| 26 #include "vm/verifier.h" | 26 #include "vm/verifier.h" |
| 27 | 27 |
| 28 namespace dart { | 28 namespace dart { |
| 29 | 29 |
| 30 DART_EXPORT bool Dart_IsValidResult(const Dart_Result& result) { |
| 31 ASSERT(Isolate::Current() != NULL); |
| 32 if (result.type_ == kRetObject) { |
| 33 Zone zone; // Setup a VM zone as we are creating some handles. |
| 34 HandleScope scope; // Setup a VM handle scope. |
| 35 |
| 36 // Make sure that the object isn't an ApiFailure. |
| 37 const Object& obj = |
| 38 Object::Handle(Api::UnwrapHandle(result.retval_.obj_value)); |
| 39 return !obj.IsApiFailure(); |
| 40 } |
| 41 return true; |
| 42 } |
| 43 |
| 44 DART_EXPORT const char* Dart_GetErrorCString(const Dart_Result& result) { |
| 45 ASSERT(Isolate::Current() != NULL); |
| 46 ASSERT(result.type_ == kRetObject); |
| 47 Zone zone; // Setup a VM zone as we are creating some handles. |
| 48 HandleScope scope; // Setup a VM handle scope. |
| 49 const Object& obj = Object::Handle( |
| 50 Api::UnwrapHandle(result.retval_.obj_value)); |
| 51 if (!obj.IsApiFailure()) { |
| 52 return ""; |
| 53 } |
| 54 ApiFailure& failure = ApiFailure::Handle(); |
| 55 failure ^= obj.raw(); |
| 56 const String& message = String::Handle(failure.message()); |
| 57 const char* msg = message.ToCString(); |
| 58 intptr_t len = strlen(msg) + 1; |
| 59 char* msg_copy = reinterpret_cast<char*>(Api::Allocate(len)); |
| 60 OS::SNPrint(msg_copy, len, "%s", msg); |
| 61 return msg_copy; |
| 62 } |
| 63 |
| 64 |
| 65 DART_EXPORT Dart_Result Dart_ErrorResult(const char* value) { |
| 66 ASSERT(Isolate::Current() != NULL); |
| 67 |
| 68 Zone zone; // Setup a VM zone as we are creating some handles. |
| 69 HandleScope scope; // Setup a VM handle scope. |
| 70 const String& message = String::Handle(String::New(value)); |
| 71 const Object& obj = Object::Handle(ApiFailure::New(message)); |
| 72 |
| 73 Dart_Result result; |
| 74 result.type_ = kRetObject; |
| 75 result.retval_.obj_value = Api::NewLocalHandle(obj); |
| 76 return result; |
| 77 } |
| 78 |
| 79 |
| 30 // TODO(iposva): This is a placeholder for the eventual external Dart API. | 80 // TODO(iposva): This is a placeholder for the eventual external Dart API. |
| 31 DART_EXPORT bool Dart_Initialize(int argc, | 81 DART_EXPORT bool Dart_Initialize(int argc, |
| 32 char** argv, | 82 char** argv, |
| 33 Dart_IsolateInitCallback callback) { | 83 Dart_IsolateInitCallback callback) { |
| 34 return Dart::InitOnce(argc, argv, callback); | 84 return Dart::InitOnce(argc, argv, callback); |
| 35 } | 85 } |
| 36 | 86 |
| 37 | 87 |
| 38 DART_EXPORT Dart_Isolate Dart_CreateIsolate(const Dart_Snapshot* snapshot, | 88 DART_EXPORT Dart_Isolate Dart_CreateIsolate(const Dart_Snapshot* snapshot, |
| 39 void* data) { | 89 void* data) { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 67 ASSERT(Isolate::Current() != NULL); | 117 ASSERT(Isolate::Current() != NULL); |
| 68 Isolate::SetCurrent(NULL); | 118 Isolate::SetCurrent(NULL); |
| 69 } | 119 } |
| 70 | 120 |
| 71 | 121 |
| 72 static void SetupErrorResult(Dart_Result* result) { | 122 static void SetupErrorResult(Dart_Result* result) { |
| 73 // Make a copy of the error message as the original message string | 123 // Make a copy of the error message as the original message string |
| 74 // may get deallocated when we return back from the Dart API call. | 124 // may get deallocated when we return back from the Dart API call. |
| 75 const String& error = String::Handle( | 125 const String& error = String::Handle( |
| 76 Isolate::Current()->object_store()->sticky_error()); | 126 Isolate::Current()->object_store()->sticky_error()); |
| 77 const char* errmsg = error.ToCString(); | 127 const Object& obj = Object::Handle(ApiFailure::New(error)); |
| 78 intptr_t errlen = strlen(errmsg) + 1; | 128 result->type_ = kRetObject; |
| 79 char* msg = reinterpret_cast<char*>(Api::Allocate(errlen)); | 129 result->retval_.obj_value = Api::NewLocalHandle(obj); |
| 80 OS::SNPrint(msg, errlen, "%s", errmsg); | |
| 81 result->type_ = kRetError; | |
| 82 result->retval_.errmsg = msg; | |
| 83 } | 130 } |
| 84 | 131 |
| 85 | 132 |
| 86 DART_EXPORT void Dart_SetMessageCallbacks( | 133 DART_EXPORT void Dart_SetMessageCallbacks( |
| 87 Dart_PostMessageCallback post_message_callback, | 134 Dart_PostMessageCallback post_message_callback, |
| 88 Dart_ClosePortCallback close_port_callback) { | 135 Dart_ClosePortCallback close_port_callback) { |
| 89 Isolate* isolate = Isolate::Current(); | 136 Isolate* isolate = Isolate::Current(); |
| 90 ASSERT(isolate != NULL); | 137 ASSERT(isolate != NULL); |
| 91 ASSERT(post_message_callback != NULL); | 138 ASSERT(post_message_callback != NULL); |
| 92 ASSERT(close_port_callback != NULL); | 139 ASSERT(close_port_callback != NULL); |
| (...skipping 1055 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1148 const Object& stacktrace = Object::Handle(unhandled.stacktrace()); | 1195 const Object& stacktrace = Object::Handle(unhandled.stacktrace()); |
| 1149 RETURN_OBJECT(stacktrace); | 1196 RETURN_OBJECT(stacktrace); |
| 1150 } | 1197 } |
| 1151 RETURN_FAILURE("Object is not an unhandled exception object"); | 1198 RETURN_FAILURE("Object is not an unhandled exception object"); |
| 1152 } | 1199 } |
| 1153 | 1200 |
| 1154 | 1201 |
| 1155 DART_EXPORT Dart_Result Dart_ThrowException(Dart_Handle exception) { | 1202 DART_EXPORT Dart_Result Dart_ThrowException(Dart_Handle exception) { |
| 1156 Isolate* isolate = Isolate::Current(); | 1203 Isolate* isolate = Isolate::Current(); |
| 1157 ASSERT(isolate != NULL); | 1204 ASSERT(isolate != NULL); |
| 1205 Zone zone; // Setup a VM zone as we are creating some handles. |
| 1206 HandleScope scope; // Setup a VM handle scope. |
| 1158 if (isolate->top_exit_frame_info() == 0) { | 1207 if (isolate->top_exit_frame_info() == 0) { |
| 1159 // There are no dart frames on the stack so it would be illegal to | 1208 // There are no dart frames on the stack so it would be illegal to |
| 1160 // throw an exception here. | 1209 // throw an exception here. |
| 1161 RETURN_FAILURE("No Dart frames on stack, cannot throw exception"); | 1210 RETURN_FAILURE("No Dart frames on stack, cannot throw exception"); |
| 1162 } | 1211 } |
| 1163 Zone zone; // Setup a VM zone as we are creating some handles. | |
| 1164 HandleScope scope; // Setup a VM handle scope. | |
| 1165 const Instance& excp = Instance::CheckedHandle(Api::UnwrapHandle(exception)); | 1212 const Instance& excp = Instance::CheckedHandle(Api::UnwrapHandle(exception)); |
| 1166 // Unwind all the API scopes till the exit frame before throwing an | 1213 // Unwind all the API scopes till the exit frame before throwing an |
| 1167 // exception. | 1214 // exception. |
| 1168 ApiState* state = isolate->api_state(); | 1215 ApiState* state = isolate->api_state(); |
| 1169 ASSERT(state != NULL); | 1216 ASSERT(state != NULL); |
| 1170 state->UnwindScopes(isolate->top_exit_frame_info()); | 1217 state->UnwindScopes(isolate->top_exit_frame_info()); |
| 1171 Exceptions::Throw(excp); | 1218 Exceptions::Throw(excp); |
| 1172 RETURN_FAILURE("Exception was not thrown, internal error"); | 1219 RETURN_FAILURE("Exception was not thrown, internal error"); |
| 1173 } | 1220 } |
| 1174 | 1221 |
| (...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1671 ASSERT(isolate != NULL); | 1718 ASSERT(isolate != NULL); |
| 1672 ApiState* state = isolate->api_state(); | 1719 ApiState* state = isolate->api_state(); |
| 1673 ASSERT(state != NULL); | 1720 ASSERT(state != NULL); |
| 1674 ApiLocalScope* scope = state->top_scope(); | 1721 ApiLocalScope* scope = state->top_scope(); |
| 1675 ASSERT(scope != NULL); | 1722 ASSERT(scope != NULL); |
| 1676 return scope->zone().Reallocate(ptr, old_size, new_size); | 1723 return scope->zone().Reallocate(ptr, old_size, new_size); |
| 1677 } | 1724 } |
| 1678 | 1725 |
| 1679 | 1726 |
| 1680 } // namespace dart | 1727 } // namespace dart |
| OLD | NEW |