Chromium Code Reviews| Index: bin/main.cc |
| =================================================================== |
| --- bin/main.cc (revision 1818) |
| +++ bin/main.cc (working copy) |
| @@ -19,6 +19,10 @@ |
| extern const uint8_t* snapshot_buffer; |
| +// Global state that stores a pointer to the application script file. |
| +static char* canonical_script_name = NULL; |
| + |
| + |
| // Global state that indicates whether pprof symbol information is |
| // to be generated or not. |
| static const char* generate_pprof_symbols_filename = NULL; |
| @@ -187,7 +191,7 @@ |
| } |
| result = DartUtils::LoadSource(NULL, library, url, tag, url_string); |
| if (!Dart_IsError(result) && (tag == kImportTag)) { |
| - Builtin_ImportLibrary(result); |
| + Builtin::ImportLibrary(result); |
| } |
| return result; |
| } |
| @@ -204,30 +208,39 @@ |
| } |
| -static void* MainIsolateInitCallback(void* data) { |
| - const char* script_name = reinterpret_cast<const char*>(data); |
| +static bool CreateIsolateAndSetup(void* data, Dart_ErrorBuffer error) { |
| + Dart_Isolate isolate = Dart_CreateIsolate(snapshot_buffer, data, error); |
| + if (isolate == NULL) { |
| + return false; |
| + } |
| + |
| Dart_Handle library; |
| Dart_EnterScope(); |
| - // Load the specified script. |
| - library = LoadScript(script_name); |
| + // Load the specified application script into the newly created isolate. |
| + library = LoadScript(canonical_script_name); |
| if (Dart_IsError(library)) { |
| - const char* err_msg = Dart_GetError(library); |
| - fprintf(stderr, "Errors encountered while loading script: %s\n", err_msg); |
| + snprintf(const_cast<char*>(error.buffer), error.length, |
| + "%s", Dart_GetError(library)); |
| Dart_ExitScope(); |
| - exit(255); |
| + Dart_ShutdownIsolate(); |
| + return false; |
| } |
| if (!Dart_IsLibrary(library)) { |
| - fprintf(stderr, |
| - "Expected a library when loading script: %s", |
| - script_name); |
| + snprintf(const_cast<char*>(error.buffer), error.length, |
| + "Expected a library when loading script: %s", |
| + canonical_script_name); |
| Dart_ExitScope(); |
| - exit(255); |
| + Dart_ShutdownIsolate(); |
| + return false; |
| } |
| - Builtin_ImportLibrary(library); // Import builtin library. |
| - |
| + Builtin::ImportLibrary(library); // Implicitly import builtin into app. |
| + if (snapshot_buffer != NULL) { |
| + // Setup the native resolver as the snapshot does not carry it. |
| + Builtin::SetNativeResolver(); |
| + } |
| Dart_ExitScope(); |
| - return data; |
| + return true; |
| } |
| @@ -271,31 +284,36 @@ |
| // dart API is fixed to take a const char** in Dart_Initialize). |
| Dart_Initialize(vm_options.count(), |
| vm_options.arguments(), |
| - MainIsolateInitCallback); |
| + CreateIsolateAndSetup); |
| - // Create an isolate. As a side effect, MainIsolateInitCallback |
| - // gets called, which loads the scripts and libraries. |
| - char* canonical_script_name = File::GetCanonicalPath(script_name); |
| + canonical_script_name = File::GetCanonicalPath(script_name); |
| if (canonical_script_name == NULL) { |
| fprintf(stderr, "Unable to find '%s'\n", script_name); |
| return 255; // Indicates we encountered an error. |
| } |
| - Dart_Isolate isolate = Dart_CreateIsolate(snapshot_buffer, |
| - canonical_script_name); |
| - if (isolate == NULL) { |
| + |
| + // Call CreateIsolateAndSetup which creates an isolate and loads up |
| + // the specified application script. |
| + Dart_ErrorBuffer error; |
| + static const int kErrorMsgLength = 256; |
| + error.buffer = reinterpret_cast<char*>(malloc(kErrorMsgLength)); |
| + error.length = kErrorMsgLength; |
| + if (!CreateIsolateAndSetup(NULL, error)) { |
| + fprintf(stderr, "%s\n", error.buffer); |
| free(canonical_script_name); |
| - return 255; |
| + free(error.buffer); |
| + return 255; // Indicates we encountered an error. |
| } |
| + free(error.buffer); |
| + Dart_Isolate isolate = Dart_CurrentIsolate(); |
| + ASSERT(isolate != NULL); |
| + Dart_Handle result; |
| + |
| Dart_EnterScope(); |
| - if (snapshot_buffer != NULL) { |
| - // Setup the native resolver as the snapshot does not carry it. |
| - Builtin_SetNativeResolver(); |
| - } |
| - |
| if (HasCompileAll(vm_options)) { |
| - Dart_Handle result = Dart_CompileAll(); |
| + result = Dart_CompileAll(); |
| if (Dart_IsError(result)) { |
|
turnidge
2011/11/28 18:39:58
If you'd like, you can just say
if (Dart_IsError(
siva
2011/11/29 00:54:25
result is used in Dart_GetError(result) below.
On
|
| fprintf(stderr, "%s\n", Dart_GetError(result)); |
| Dart_ExitScope(); |
| @@ -325,11 +343,11 @@ |
| free(canonical_script_name); |
| return 255; // Indicates we encountered an error. |
| } |
| - Dart_Handle result = Dart_InvokeStatic(library, |
| - Dart_NewString(""), |
| - Dart_NewString("main"), |
| - 0, |
| - NULL); |
| + result = Dart_InvokeStatic(library, |
| + Dart_NewString(""), |
| + Dart_NewString("main"), |
| + 0, |
| + NULL); |
| if (Dart_IsError(result)) { |
| fprintf(stderr, "%s\n", Dart_GetError(result)); |
| Dart_ExitScope(); |