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

Side by Side Diff: runtime/bin/main.cc

Issue 8501034: Deal with unhandled exceptions the same way in all Dart api functions. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: '' Created 9 years, 1 month 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
« no previous file with comments | « runtime/bin/gen_snapshot.cc ('k') | runtime/bin/process.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <stdlib.h> 5 #include <stdlib.h>
6 #include <string.h> 6 #include <string.h>
7 #include <stdio.h> 7 #include <stdio.h>
8 8
9 #include "include/dart_api.h" 9 #include "include/dart_api.h"
10 10
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 i += 1; 91 i += 1;
92 } 92 }
93 93
94 return 0; 94 return 0;
95 } 95 }
96 96
97 97
98 static Dart_Handle SetupRuntimeOptions(CommandLineOptions* options) { 98 static Dart_Handle SetupRuntimeOptions(CommandLineOptions* options) {
99 int options_count = options->count(); 99 int options_count = options->count();
100 Dart_Handle dart_arguments = Dart_NewList(options_count); 100 Dart_Handle dart_arguments = Dart_NewList(options_count);
101 if (!Dart_IsValid(dart_arguments)) { 101 if (Dart_IsError(dart_arguments)) {
102 return dart_arguments; 102 return dart_arguments;
103 } 103 }
104 for (int i = 0; i < options_count; i++) { 104 for (int i = 0; i < options_count; i++) {
105 Dart_Handle argument_value = Dart_NewString(options->GetArgument(i)); 105 Dart_Handle argument_value = Dart_NewString(options->GetArgument(i));
106 if (!Dart_IsValid(argument_value)) { 106 if (Dart_IsError(argument_value)) {
107 return argument_value; 107 return argument_value;
108 } 108 }
109 Dart_Handle result = Dart_ListSetAt(dart_arguments, i, argument_value); 109 Dart_Handle result = Dart_ListSetAt(dart_arguments, i, argument_value);
110 if (!Dart_IsValid(result)) { 110 if (Dart_IsError(result)) {
111 return result; 111 return result;
112 } 112 }
113 } 113 }
114 Dart_Handle core_lib_url = Dart_NewString("dart:coreimpl"); 114 Dart_Handle core_lib_url = Dart_NewString("dart:coreimpl");
115 if (!Dart_IsValid(core_lib_url)) { 115 if (Dart_IsError(core_lib_url)) {
116 return core_lib_url; 116 return core_lib_url;
117 } 117 }
118 Dart_Handle core_lib = Dart_LookupLibrary(core_lib_url); 118 Dart_Handle core_lib = Dart_LookupLibrary(core_lib_url);
119 if (!Dart_IsValid(core_lib)) { 119 if (Dart_IsError(core_lib)) {
120 return core_lib; 120 return core_lib;
121 } 121 }
122 Dart_Handle runtime_options_class_name = Dart_NewString("RuntimeOptions"); 122 Dart_Handle runtime_options_class_name = Dart_NewString("RuntimeOptions");
123 if (!Dart_IsValid(runtime_options_class_name)) { 123 if (Dart_IsError(runtime_options_class_name)) {
124 return runtime_options_class_name; 124 return runtime_options_class_name;
125 } 125 }
126 Dart_Handle runtime_options_class = Dart_GetClass( 126 Dart_Handle runtime_options_class = Dart_GetClass(
127 core_lib, runtime_options_class_name); 127 core_lib, runtime_options_class_name);
128 if (!Dart_IsValid(runtime_options_class)) { 128 if (Dart_IsError(runtime_options_class)) {
129 return runtime_options_class; 129 return runtime_options_class;
130 } 130 }
131 Dart_Handle native_name = Dart_NewString("_nativeArguments"); 131 Dart_Handle native_name = Dart_NewString("_nativeArguments");
132 if (!Dart_IsValid(native_name)) { 132 if (Dart_IsError(native_name)) {
133 return native_name; 133 return native_name;
134 } 134 }
135 135
136 return Dart_SetStaticField(runtime_options_class, 136 return Dart_SetStaticField(runtime_options_class,
137 native_name, 137 native_name,
138 dart_arguments); 138 dart_arguments);
139 } 139 }
140 140
141 141
142 static void DumpPprofSymbolInfo() { 142 static void DumpPprofSymbolInfo() {
(...skipping 14 matching lines...) Expand all
157 } 157 }
158 158
159 159
160 static void* MainIsolateInitCallback(void* data) { 160 static void* MainIsolateInitCallback(void* data) {
161 const char* script_name = reinterpret_cast<const char*>(data); 161 const char* script_name = reinterpret_cast<const char*>(data);
162 Dart_Handle library; 162 Dart_Handle library;
163 Dart_EnterScope(); 163 Dart_EnterScope();
164 164
165 // Load the specified script. 165 // Load the specified script.
166 library = LoadScript(script_name); 166 library = LoadScript(script_name);
167 if (!Dart_IsValid(library)) { 167 if (Dart_IsError(library)) {
168 const char* err_msg = Dart_GetError(library); 168 const char* err_msg = Dart_GetError(library);
169 fprintf(stderr, "Errors encountered while loading script: %s\n", err_msg); 169 fprintf(stderr, "Errors encountered while loading script: %s\n", err_msg);
170 Dart_ExitScope(); 170 Dart_ExitScope();
171 exit(255); 171 exit(255);
172 } 172 }
173 173
174 if (!Dart_IsLibrary(library)) { 174 if (!Dart_IsLibrary(library)) {
175 fprintf(stderr, 175 fprintf(stderr,
176 "Expected a library when loading script: %s", 176 "Expected a library when loading script: %s",
177 script_name); 177 script_name);
(...skipping 18 matching lines...) Expand all
196 static bool HasCompileAll(const CommandLineOptions& options) { 196 static bool HasCompileAll(const CommandLineOptions& options) {
197 for (int i = 0; i < options.count(); i++) { 197 for (int i = 0; i < options.count(); i++) {
198 if (strcmp(options.GetArgument(i), "--compile_all") == 0) { 198 if (strcmp(options.GetArgument(i), "--compile_all") == 0) {
199 return true; 199 return true;
200 } 200 }
201 } 201 }
202 return false; 202 return false;
203 } 203 }
204 204
205 205
206 static void PrintObject(FILE* out, Dart_Handle object) {
207 Dart_Handle result = Dart_ToString(object);
208 if (Dart_IsValid(result)) {
209 PrintString(out, result);
210 } else {
211 fprintf(out, "%s\n", Dart_GetError(result));
212 }
213 }
214
215
216 int main(int argc, char** argv) { 206 int main(int argc, char** argv) {
217 char* script_name; 207 char* script_name;
218 CommandLineOptions vm_options(argc); 208 CommandLineOptions vm_options(argc);
219 CommandLineOptions dart_options(argc); 209 CommandLineOptions dart_options(argc);
220 210
221 // Parse command line arguments. 211 // Parse command line arguments.
222 if (ParseArguments(argc, 212 if (ParseArguments(argc,
223 argv, 213 argv,
224 &vm_options, 214 &vm_options,
225 &script_name, 215 &script_name,
(...skipping 17 matching lines...) Expand all
243 Dart_Isolate isolate = Dart_CreateIsolate(snapshot_buffer, 233 Dart_Isolate isolate = Dart_CreateIsolate(snapshot_buffer,
244 canonical_script_name); 234 canonical_script_name);
245 if (isolate == NULL) { 235 if (isolate == NULL) {
246 free(canonical_script_name); 236 free(canonical_script_name);
247 return 255; 237 return 255;
248 } 238 }
249 239
250 Dart_EnterScope(); 240 Dart_EnterScope();
251 if (HasCompileAll(vm_options)) { 241 if (HasCompileAll(vm_options)) {
252 Dart_Handle result = Dart_CompileAll(); 242 Dart_Handle result = Dart_CompileAll();
253 if (!Dart_IsValid(result)) { 243 if (Dart_IsError(result)) {
254 fprintf(stderr, "%s\n", Dart_GetError(result)); 244 fprintf(stderr, "%s\n", Dart_GetError(result));
255 Dart_ExitScope(); 245 Dart_ExitScope();
256 Dart_ShutdownIsolate(); 246 Dart_ShutdownIsolate();
257 free(canonical_script_name); 247 free(canonical_script_name);
258 return 255; // Indicates we encountered an error. 248 return 255; // Indicates we encountered an error.
259 } 249 }
260 } 250 }
261 251
262 // Create a dart options object that can be accessed from dart code. 252 // Create a dart options object that can be accessed from dart code.
263 Dart_Handle options_result = SetupRuntimeOptions(&dart_options); 253 Dart_Handle options_result = SetupRuntimeOptions(&dart_options);
264 if (!Dart_IsValid(options_result)) { 254 if (Dart_IsError(options_result)) {
265 fprintf(stderr, "%s\n", Dart_GetError(options_result)); 255 fprintf(stderr, "%s\n", Dart_GetError(options_result));
266 Dart_ExitScope(); 256 Dart_ExitScope();
267 Dart_ShutdownIsolate(); 257 Dart_ShutdownIsolate();
268 free(canonical_script_name); 258 free(canonical_script_name);
269 return 255; // Indicates we encountered an error. 259 return 255; // Indicates we encountered an error.
270 } 260 }
271 261
272 // Lookup and invoke the top level main function. 262 // Lookup and invoke the top level main function.
273 Dart_Handle script_url = Dart_NewString(canonical_script_name); 263 Dart_Handle script_url = Dart_NewString(canonical_script_name);
274 Dart_Handle library = Dart_LookupLibrary(script_url); 264 Dart_Handle library = Dart_LookupLibrary(script_url);
275 if (!Dart_IsValid(library)) { 265 if (Dart_IsError(library)) {
276 fprintf(stderr, "%s\n", Dart_GetError(library)); 266 fprintf(stderr, "%s\n", Dart_GetError(library));
277 Dart_ExitScope(); 267 Dart_ExitScope();
278 Dart_ShutdownIsolate(); 268 Dart_ShutdownIsolate();
279 free(canonical_script_name); 269 free(canonical_script_name);
280 return 255; // Indicates we encountered an error. 270 return 255; // Indicates we encountered an error.
281 } 271 }
282 Dart_Handle result = Dart_InvokeStatic(library, 272 Dart_Handle result = Dart_InvokeStatic(library,
283 Dart_NewString(""), 273 Dart_NewString(""),
284 Dart_NewString("main"), 274 Dart_NewString("main"),
285 0, 275 0,
286 NULL); 276 NULL);
287 if (Dart_IsValid(result)) { 277 if (Dart_IsError(result)) {
288 if (Dart_ExceptionOccurred(result)) {
289 // Print the exception object.
290 fprintf(stderr, "An unhandled exception has been thrown\n");
291 Dart_Handle exception_result = Dart_GetException(result);
292 ASSERT(Dart_IsValid(exception_result));
293 PrintObject(stderr, exception_result);
294 // Print the stack trace.
295 Dart_Handle stacktrace = Dart_GetStacktrace(result);
296 ASSERT(Dart_IsValid(stacktrace));
297 PrintObject(stderr, stacktrace);
298 fprintf(stderr, "\n");
299 Dart_ExitScope();
300 Dart_ShutdownIsolate();
301 free(canonical_script_name);
302 return 255; // We had an unhandled exception, hence indicate an error.
303 }
304 } else {
305 fprintf(stderr, "%s\n", Dart_GetError(result)); 278 fprintf(stderr, "%s\n", Dart_GetError(result));
306 Dart_ExitScope(); 279 Dart_ExitScope();
307 Dart_ShutdownIsolate(); 280 Dart_ShutdownIsolate();
308 free(canonical_script_name); 281 free(canonical_script_name);
309 return 255; // Indicates we encountered an error. 282 return 255; // Indicates we encountered an error.
310 } 283 }
311 // Keep handling messages until the last active receive port is closed. 284 // Keep handling messages until the last active receive port is closed.
312 result = Dart_RunLoop(); 285 result = Dart_RunLoop();
313 if (!Dart_IsValid(result)) { 286 if (Dart_IsError(result)) {
314 fprintf(stderr, "%s\n", Dart_GetError(result)); 287 fprintf(stderr, "%s\n", Dart_GetError(result));
315 Dart_ExitScope(); 288 Dart_ExitScope();
316 Dart_ShutdownIsolate(); 289 Dart_ShutdownIsolate();
317 free(canonical_script_name); 290 free(canonical_script_name);
318 return 255; // Indicates we encountered an error. 291 return 255; // Indicates we encountered an error.
319 } 292 }
320 free(canonical_script_name); 293 free(canonical_script_name);
321 Dart_ExitScope(); 294 Dart_ExitScope();
322 // Dump symbol information for the profiler. 295 // Dump symbol information for the profiler.
323 DumpPprofSymbolInfo(); 296 DumpPprofSymbolInfo();
324 // Shutdown the isolate. 297 // Shutdown the isolate.
325 Dart_ShutdownIsolate(); 298 Dart_ShutdownIsolate();
326 return 0; 299 return 0;
327 } 300 }
OLDNEW
« no previous file with comments | « runtime/bin/gen_snapshot.cc ('k') | runtime/bin/process.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698