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

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

Issue 8334009: Use the distinguished class, ApiFailure, to signal when a Dart_Result is (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 9 years, 2 months 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) 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);
Ivan Posva 2011/10/18 20:44:28 ASSERT
turnidge 2011/10/18 22:03:12 Done.
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);
Ivan Posva 2011/10/18 20:44:28 ditto
turnidge 2011/10/18 22:03:12 Done.
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 ApiFailure& failure =
50 ApiFailure::CheckedHandle(Api::UnwrapHandle(result.retval_.obj_value));
Ivan Posva 2011/10/18 20:44:28 If the result does not contain an ApiFailure objec
turnidge 2011/10/18 22:03:12 Well, it's potentially inconsistent as it currentl
51 const String& message = String::Handle(failure.message());
52 const char* msg = message.ToCString();
53 intptr_t len = strlen(msg) + 1;
54 char* msg_copy = reinterpret_cast<char*>(Api::Allocate(len));
55 OS::SNPrint(msg_copy, len, "%s", msg);
56 return msg_copy;
57 }
58
59
60 DART_EXPORT Dart_Result Dart_ErrorResult(const char* value) {
61 assert(Isolate::Current() != NULL);
Ivan Posva 2011/10/18 20:44:28 ditto
turnidge 2011/10/18 22:03:12 Done.
62
63 Zone zone; // Setup a VM zone as we are creating some handles.
64 HandleScope scope; // Setup a VM handle scope.
65 const String& message = String::Handle(String::New(value));
66 const Object& obj = Object::Handle(ApiFailure::New(message));
67
68 Dart_Result result;
69 result.type_ = kRetObject;
70 result.retval_.obj_value = Api::NewLocalHandle(obj);
71 return result;
72 }
73
74
30 // TODO(iposva): This is a placeholder for the eventual external Dart API. 75 // TODO(iposva): This is a placeholder for the eventual external Dart API.
31 DART_EXPORT bool Dart_Initialize(int argc, 76 DART_EXPORT bool Dart_Initialize(int argc,
32 char** argv, 77 char** argv,
33 Dart_IsolateInitCallback callback) { 78 Dart_IsolateInitCallback callback) {
34 return Dart::InitOnce(argc, argv, callback); 79 return Dart::InitOnce(argc, argv, callback);
35 } 80 }
36 81
37 82
38 DART_EXPORT Dart_Isolate Dart_CreateIsolate(const Dart_Snapshot* snapshot, 83 DART_EXPORT Dart_Isolate Dart_CreateIsolate(const Dart_Snapshot* snapshot,
39 void* data) { 84 void* data) {
(...skipping 27 matching lines...) Expand all
67 ASSERT(Isolate::Current() != NULL); 112 ASSERT(Isolate::Current() != NULL);
68 Isolate::SetCurrent(NULL); 113 Isolate::SetCurrent(NULL);
69 } 114 }
70 115
71 116
72 static void SetupErrorResult(Dart_Result* result) { 117 static void SetupErrorResult(Dart_Result* result) {
73 // Make a copy of the error message as the original message string 118 // 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. 119 // may get deallocated when we return back from the Dart API call.
75 const String& error = String::Handle( 120 const String& error = String::Handle(
76 Isolate::Current()->object_store()->sticky_error()); 121 Isolate::Current()->object_store()->sticky_error());
77 const char* errmsg = error.ToCString(); 122 const Object& obj = Object::Handle(ApiFailure::New(error));
78 intptr_t errlen = strlen(errmsg) + 1; 123 result->type_ = kRetObject;
79 char* msg = reinterpret_cast<char*>(Api::Allocate(errlen)); 124 result->retval_.obj_value = Api::NewLocalHandle(obj);
80 OS::SNPrint(msg, errlen, "%s", errmsg);
81 result->type_ = kRetError;
82 result->retval_.errmsg = msg;
83 } 125 }
84 126
85 127
86 DART_EXPORT Dart_Result Dart_RunLoop() { 128 DART_EXPORT Dart_Result Dart_RunLoop() {
87 Isolate* isolate = Isolate::Current(); 129 Isolate* isolate = Isolate::Current();
88 LongJump* base = isolate->long_jump_base(); 130 LongJump* base = isolate->long_jump_base();
89 LongJump jump; 131 LongJump jump;
90 Dart_Result result; 132 Dart_Result result;
91 isolate->set_long_jump_base(&jump); 133 isolate->set_long_jump_base(&jump);
92 if (setjmp(*jump.Set()) == 0) { 134 if (setjmp(*jump.Set()) == 0) {
(...skipping 989 matching lines...) Expand 10 before | Expand all | Expand 10 after
1082 const Object& stacktrace = Object::Handle(unhandled.stacktrace()); 1124 const Object& stacktrace = Object::Handle(unhandled.stacktrace());
1083 RETURN_OBJECT(stacktrace); 1125 RETURN_OBJECT(stacktrace);
1084 } 1126 }
1085 RETURN_FAILURE("Object is not an unhandled exception object"); 1127 RETURN_FAILURE("Object is not an unhandled exception object");
1086 } 1128 }
1087 1129
1088 1130
1089 DART_EXPORT Dart_Result Dart_ThrowException(Dart_Handle exception) { 1131 DART_EXPORT Dart_Result Dart_ThrowException(Dart_Handle exception) {
1090 Isolate* isolate = Isolate::Current(); 1132 Isolate* isolate = Isolate::Current();
1091 ASSERT(isolate != NULL); 1133 ASSERT(isolate != NULL);
1134 Zone zone; // Setup a VM zone as we are creating some handles.
1135 HandleScope scope; // Setup a VM handle scope.
1092 if (isolate->top_exit_frame_info() == 0) { 1136 if (isolate->top_exit_frame_info() == 0) {
1093 // There are no dart frames on the stack so it would be illegal to 1137 // There are no dart frames on the stack so it would be illegal to
1094 // throw an exception here. 1138 // throw an exception here.
1095 RETURN_FAILURE("No Dart frames on stack, cannot throw exception"); 1139 RETURN_FAILURE("No Dart frames on stack, cannot throw exception");
1096 } 1140 }
1097 Zone zone; // Setup a VM zone as we are creating some handles.
1098 HandleScope scope; // Setup a VM handle scope.
1099 const Instance& excp = Instance::CheckedHandle(Api::UnwrapHandle(exception)); 1141 const Instance& excp = Instance::CheckedHandle(Api::UnwrapHandle(exception));
1100 // Unwind all the API scopes till the exit frame before throwing an 1142 // Unwind all the API scopes till the exit frame before throwing an
1101 // exception. 1143 // exception.
1102 ApiState* state = isolate->api_state(); 1144 ApiState* state = isolate->api_state();
1103 ASSERT(state != NULL); 1145 ASSERT(state != NULL);
1104 state->UnwindScopes(isolate->top_exit_frame_info()); 1146 state->UnwindScopes(isolate->top_exit_frame_info());
1105 Exceptions::Throw(excp); 1147 Exceptions::Throw(excp);
1106 RETURN_FAILURE("Exception was not thrown, internal error"); 1148 RETURN_FAILURE("Exception was not thrown, internal error");
1107 } 1149 }
1108 1150
(...skipping 499 matching lines...) Expand 10 before | Expand all | Expand 10 after
1608 ASSERT(isolate != NULL); 1650 ASSERT(isolate != NULL);
1609 ApiState* state = isolate->api_state(); 1651 ApiState* state = isolate->api_state();
1610 ASSERT(state != NULL); 1652 ASSERT(state != NULL);
1611 ApiLocalScope* scope = state->top_scope(); 1653 ApiLocalScope* scope = state->top_scope();
1612 ASSERT(scope != NULL); 1654 ASSERT(scope != NULL);
1613 return scope->zone().Reallocate(ptr, old_size, new_size); 1655 return scope->zone().Reallocate(ptr, old_size, new_size);
1614 } 1656 }
1615 1657
1616 1658
1617 } // namespace dart 1659 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/include/dart_api.h ('k') | runtime/vm/object.h » ('j') | runtime/vm/object.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698