Chromium Code Reviews| Index: bin/main.cc |
| =================================================================== |
| --- bin/main.cc (revision 4985) |
| +++ bin/main.cc (working copy) |
| @@ -24,7 +24,9 @@ |
| // Global state that stores a pointer to the application script file. |
| -static char* canonical_script_name = NULL; |
| +static const char* original_script_name = NULL; |
| +static const char* original_working_directory = NULL; |
| +static const char* original_script_url = NULL; |
| // Global state that stores the import URL map specified on the |
| @@ -32,11 +34,6 @@ |
| static CommandLineOptions* import_map_options = NULL; |
| -// Global state that stores a pointer to the application script snapshot. |
| -static bool use_script_snapshot = false; |
| -static uint8_t* script_snapshot_buffer = NULL; |
| - |
| - |
| // Global state that indicates whether pprof symbol information is |
| // to be generated or not. |
| static const char* generate_pprof_symbols_filename = NULL; |
| @@ -80,12 +77,6 @@ |
| } |
| -static void ProcessSnapshotOption(const char* snapshot) { |
| - ASSERT(snapshot != NULL); |
| - use_script_snapshot = true; |
| -} |
| - |
| - |
| static void ProcessImportMapOption(const char* map) { |
| ASSERT(map != NULL); |
| import_map_options->AddArgument(map); |
| @@ -99,7 +90,6 @@ |
| { "--break_at=", ProcessBreakpointOption }, |
| { "--compile_all", ProcessCompileAllOption }, |
| { "--generate_pprof_symbols=", ProcessPprofOption }, |
| - { "--use_script_snapshot", ProcessSnapshotOption }, |
| { "--import_map=", ProcessImportMapOption }, |
| { NULL, NULL } |
| }; |
| @@ -166,7 +156,7 @@ |
| static Dart_Handle SetupRuntimeOptions(CommandLineOptions* options, |
| - char* script_name) { |
| + const char* script_name) { |
| int options_count = options->count(); |
| Dart_Handle dart_script = Dart_NewString(script_name); |
| if (Dart_IsError(dart_script)) { |
| @@ -265,8 +255,18 @@ |
| if (is_dart_scheme_url || is_dart_extension_url) { |
| return url; |
| } |
| - // Create a canonical path based on the including library and current url. |
| - return DartUtils::CanonicalizeURL(NULL, library, url_string); |
| + // Resolve the url within the context of the library's URL. |
| + Dart_Handle builtin_lib = Builtin::LoadLibrary(Builtin::kBuiltinLibrary); |
|
siva
2012/03/07 18:32:17
Why not store builtin_lib in a persistent handle i
Ivan Posva
2012/03/08 00:49:33
Because you would need a current isolate to a grou
|
| + Dart_Handle library_url = Dart_LibraryUrl(library); |
| + if (Dart_IsError(library_url)) { |
| + return Dart_Error("accessing library url failed"); |
| + } |
| + Dart_Handle dart_args[2]; |
| + dart_args[0] = library_url; |
| + dart_args[1] = url; |
| + return Dart_InvokeStatic(builtin_lib, |
| + Dart_NewString(""), Dart_NewString("resolveUri"), |
|
siva
2012/03/07 18:32:17
Why not store
Dart_NewString("");
Dart_NewString(
Ivan Posva
2012/03/08 00:49:33
ditto
|
| + 2, dart_args); |
| } |
| if (is_dart_scheme_url) { |
| ASSERT(tag == kImportTag); |
| @@ -287,6 +287,19 @@ |
| return Dart_Error("Dart extensions must use import: '%s'", url_string); |
| } |
| return Extensions::LoadExtension(url_string, library); |
| + } else { |
| + // Get the file path out of the url. |
| + Dart_Handle builtin_lib = Builtin::LoadLibrary(Builtin::kBuiltinLibrary); |
| + Dart_Handle dart_args[1]; |
| + dart_args[0] = url; |
| + Dart_Handle file_path = Dart_InvokeStatic(builtin_lib, |
| + Dart_NewString(""), |
| + Dart_NewString("filePathFromUri"), |
| + 1, dart_args); |
| + if (Dart_IsError(file_path)) { |
| + return file_path; |
| + } |
| + Dart_StringToCString(file_path, &url_string); |
| } |
| result = DartUtils::LoadSource(NULL, |
| library, |
| @@ -301,13 +314,38 @@ |
| } |
| -static Dart_Handle LoadScript(const char* script_name, |
| +static Dart_Handle LoadScript(Dart_Handle builtin_lib, |
| CommandLineOptions* map) { |
| - Dart_Handle source = DartUtils::ReadStringFromFile(script_name); |
| + Dart_Handle dart_args[2]; |
| + dart_args[0] = Dart_NewString(original_working_directory); |
| + dart_args[1] = Dart_NewString(original_script_name); |
| + Dart_Handle script_url = Dart_InvokeStatic(builtin_lib, |
| + Dart_NewString(""), |
| + Dart_NewString("resolveScriptUri"), |
| + 2, dart_args); |
| + if (Dart_IsError(script_url)) { |
| + fprintf(stderr, "%s", Dart_GetError(script_url)); |
| + return false; |
| + } |
| + if (original_script_url == NULL) { |
| + const char* script_url_cstr; |
| + Dart_StringToCString(script_url, &script_url_cstr); |
| + original_script_url = strdup(script_url_cstr); |
| + } |
| + dart_args[0] = script_url; |
| + Dart_Handle script_path = Dart_InvokeStatic(builtin_lib, |
| + Dart_NewString(""), |
| + Dart_NewString("filePathFromUri"), |
| + 1, dart_args); |
| + if (Dart_IsError(script_path)) { |
| + return script_path; |
| + } |
| + const char* script_path_cstr; |
| + Dart_StringToCString(script_path, &script_path_cstr); |
| + Dart_Handle source = DartUtils::ReadStringFromFile(script_path_cstr); |
| if (Dart_IsError(source)) { |
| return source; |
| } |
| - Dart_Handle url = Dart_NewString(script_name); |
| intptr_t length = (map == NULL) ? 0 : map->count(); |
| Dart_Handle import_map = Dart_NewList(length * 2); |
| for (intptr_t i = 0; i < length; i++) { |
| @@ -327,7 +365,7 @@ |
| } |
| free(name); |
| } |
| - return Dart_LoadScript(url, source, LibraryTagHandler, import_map); |
| + return Dart_LoadScript(script_url, source, LibraryTagHandler, import_map); |
| } |
| @@ -339,15 +377,21 @@ |
| return false; |
| } |
| - Dart_Handle library; |
| Dart_EnterScope(); |
| - // Load the specified application script into the newly created isolate. |
| - if (script_snapshot_buffer != NULL) { |
| - library = Dart_LoadScriptFromSnapshot(script_snapshot_buffer); |
| - } else { |
| - library = LoadScript(canonical_script_name, import_map_options); |
| + if (snapshot_buffer != NULL) { |
| + // Setup the native resolver as the snapshot does not carry it. |
| + Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); |
| + Builtin::SetNativeResolver(Builtin::kIOLibrary); |
| } |
| + |
| + // Prepare builtin for use to resolve URIs. |
| + Dart_Handle uri_lib = Builtin::LoadLibrary(Builtin::kUriLibrary); |
| + Dart_Handle builtin_lib = Builtin::LoadLibrary(Builtin::kBuiltinLibrary); |
| + Builtin::ImportLibrary(builtin_lib, Builtin::kUriLibrary); |
| + |
| + // Load the specified application script into the newly created isolate. |
| + Dart_Handle library = LoadScript(builtin_lib, import_map_options); |
| if (Dart_IsError(library)) { |
| *error = strdup(Dart_GetError(library)); |
| Dart_ExitScope(); |
| @@ -358,76 +402,19 @@ |
| char errbuf[256]; |
| snprintf(errbuf, sizeof(errbuf), |
| "Expected a library when loading script: %s", |
| - canonical_script_name); |
| + original_script_name); |
| *error = strdup(errbuf); |
| Dart_ExitScope(); |
| Dart_ShutdownIsolate(); |
| return false; |
| } |
| - if (script_snapshot_buffer == NULL) { |
| - // Implicitly import builtin into app. |
| - Builtin::ImportLibrary(library, Builtin::kBuiltinLibrary); |
| - } |
| - if (snapshot_buffer != NULL) { |
| - // Setup the native resolver as the snapshot does not carry it. |
| - Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); |
| - Builtin::SetNativeResolver(Builtin::kIOLibrary); |
| - } |
| + // Implicitly import builtin into app. |
| + Builtin::ImportLibrary(library, Builtin::kBuiltinLibrary); |
| Dart_ExitScope(); |
| return true; |
| } |
| -static bool CaptureScriptSnapshot() { |
| - char* error = NULL; |
| - Dart_Handle result; |
| - |
| - // First create an isolate and load up the specified script in it. |
| - if (!CreateIsolateAndSetup(NULL, NULL, &error)) { |
| - fprintf(stderr, "%s\n", error); |
| - free(canonical_script_name); |
| - free(error); |
| - return false; // Indicates we encountered an error. |
| - } |
| - |
| - Dart_EnterScope(); |
| - |
| -#if 0 |
| - // Lookup the library of the main script. |
| - Dart_Handle script_url = Dart_NewString(canonical_script_name); |
| - Dart_Handle library = Dart_LookupLibrary(script_url); |
| - if (Dart_IsError(library)) { |
| - fprintf(stderr, "%s\n", Dart_GetError(library)); |
| - Dart_ExitScope(); |
| - Dart_ShutdownIsolate(); |
| - free(canonical_script_name); |
| - return false; // Indicates we encountered an error. |
| - } |
| -#endif |
| - |
| - // Now create the script snapshot and save into a buffer. |
| - uint8_t* buffer; |
| - intptr_t size; |
| - result = Dart_CreateScriptSnapshot(&buffer, &size); |
| - if (Dart_IsError(result)) { |
| - fprintf(stderr, "%s\n", Dart_GetError(result)); |
| - Dart_ExitScope(); |
| - Dart_ShutdownIsolate(); |
| - return false; // Indicates we encountered an error. |
| - } |
| - |
| - // Save the script snapshot as we are about to shutdown the isolate. |
| - script_snapshot_buffer = reinterpret_cast<uint8_t*>(malloc(size)); |
| - ASSERT(script_snapshot_buffer != NULL); |
| - memmove(script_snapshot_buffer, buffer, size); |
| - |
| - // Shutdown this isolate. |
| - Dart_ExitScope(); |
| - Dart_ShutdownIsolate(); |
| - return true; |
| -} |
| - |
| - |
| static void PrintUsage() { |
| fprintf(stderr, |
| "dart [<vm-flags>] <dart-script-file> [<dart-options>]\n"); |
| @@ -516,34 +503,20 @@ |
| // Initialize the Dart VM. |
| Dart_Initialize(CreateIsolateAndSetup, NULL); |
| - 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. |
| - } |
| + original_script_name = strdup(script_name); |
| + original_working_directory = File::GetWorkingDirectory(); |
| - // If application snapshot option is specified, first create the |
| - // application snapshot and then load the script using the snapshot |
| - // created. |
| - if (use_script_snapshot) { |
| - if (!CaptureScriptSnapshot()) { |
| - return 255; // Error capturing script snapshot, error already reported. |
| - } |
| - } |
| - |
| // Call CreateIsolateAndSetup which creates an isolate and loads up |
| // the specified application script. |
| char* error = NULL; |
| - char* isolate_name = BuildIsolateName(canonical_script_name, "main"); |
| + char* isolate_name = BuildIsolateName(original_script_name, "main"); |
| if (!CreateIsolateAndSetup(isolate_name, NULL, &error)) { |
| fprintf(stderr, "%s\n", error); |
| - free(canonical_script_name); |
| + free(const_cast<char*>(original_script_name)); |
| free(error); |
| - free(script_snapshot_buffer); |
| delete [] isolate_name; |
| return 255; // Indicates we encountered an error. |
| } |
| - free(script_snapshot_buffer); // Don't need it anymore. |
| delete [] isolate_name; |
| Dart_Isolate isolate = Dart_CurrentIsolate(); |
| @@ -558,29 +531,29 @@ |
| fprintf(stderr, "%s\n", Dart_GetError(result)); |
| Dart_ExitScope(); |
| Dart_ShutdownIsolate(); |
| - free(canonical_script_name); |
| + free(const_cast<char*>(original_script_name)); |
| return 255; // Indicates we encountered an error. |
| } |
| } |
| // Create a dart options object that can be accessed from dart code. |
| Dart_Handle options_result = |
| - SetupRuntimeOptions(&dart_options, canonical_script_name); |
| + SetupRuntimeOptions(&dart_options, original_script_name); |
| if (Dart_IsError(options_result)) { |
| fprintf(stderr, "%s\n", Dart_GetError(options_result)); |
| Dart_ExitScope(); |
| Dart_ShutdownIsolate(); |
| - free(canonical_script_name); |
| + free(const_cast<char*>(original_script_name)); |
| return 255; // Indicates we encountered an error. |
| } |
| // Lookup the library of the main script. |
| - Dart_Handle script_url = Dart_NewString(canonical_script_name); |
| + Dart_Handle script_url = Dart_NewString(original_script_url); |
| Dart_Handle library = Dart_LookupLibrary(script_url); |
| if (Dart_IsError(library)) { |
| fprintf(stderr, "%s\n", Dart_GetError(library)); |
| Dart_ExitScope(); |
| Dart_ShutdownIsolate(); |
| - free(canonical_script_name); |
| + free(const_cast<char*>(original_script_name)); |
| return 255; // Indicates we encountered an error. |
| } |
| // Set debug breakpoint if specified on the command line. |
| @@ -592,7 +565,7 @@ |
| Dart_GetError(result)); |
| Dart_ExitScope(); |
| Dart_ShutdownIsolate(); |
| - free(canonical_script_name); |
| + free(const_cast<char*>(original_script_name)); |
| return 255; // Indicates we encountered an error. |
| } |
| } |
| @@ -606,7 +579,7 @@ |
| fprintf(stderr, "%s\n", Dart_GetError(result)); |
| Dart_ExitScope(); |
| Dart_ShutdownIsolate(); |
| - free(canonical_script_name); |
| + free(const_cast<char*>(original_script_name)); |
| return 255; // Indicates we encountered an error. |
| } |
| // Keep handling messages until the last active receive port is closed. |
| @@ -615,10 +588,10 @@ |
| fprintf(stderr, "%s\n", Dart_GetError(result)); |
| Dart_ExitScope(); |
| Dart_ShutdownIsolate(); |
| - free(canonical_script_name); |
| + free(const_cast<char*>(original_script_name)); |
| return 255; // Indicates we encountered an error. |
| } |
| - free(canonical_script_name); |
| + free(const_cast<char*>(original_script_name)); |
| Dart_ExitScope(); |
| // Dump symbol information for the profiler. |
| DumpPprofSymbolInfo(); |