Chromium Code Reviews| Index: bin/gen_snapshot.cc |
| =================================================================== |
| --- bin/gen_snapshot.cc (revision 1106) |
| +++ bin/gen_snapshot.cc (working copy) |
| @@ -20,6 +20,9 @@ |
| // if so which file to write the snapshot into. |
| static const char* snapshot_filename = NULL; |
| +// Global state that captures the URL mappings specified on the command line. |
| +static CommandLineOptions* url_mapping = NULL; |
| + |
| static bool IsValidFlag(const char* name, |
| const char* prefix, |
| intptr_t prefix_length) { |
| @@ -40,11 +43,26 @@ |
| static bool ProcessSnapshotOption(const char* option) { |
| const char* kSnapshotOption = "--snapshot="; |
| - snapshot_filename = ProcessOption(option, kSnapshotOption); |
| - return snapshot_filename != NULL; |
| + const char* name = ProcessOption(option, kSnapshotOption); |
| + if (name != NULL) { |
| + snapshot_filename = name; |
| + return true; |
| + } |
| + return false; |
| } |
| +static bool ProcessURLmappingOption(const char* option) { |
| + const char* kURLmappingOption = "--url_mapping="; |
| + const char* mapping = ProcessOption(option, kURLmappingOption); |
| + if (mapping != NULL) { |
| + url_mapping->AddArgument(mapping); |
| + return true; |
| + } |
| + return false; |
| +} |
| + |
| + |
| // Parse out the command line arguments. Returns -1 if the arguments |
| // are incorrect, 0 otherwise. |
| static int ParseArguments(int argc, |
| @@ -59,7 +77,7 @@ |
| // Parse out the vm options. |
| while ((i < argc) && IsValidFlag(argv[i], kPrefix, kPrefixLen)) { |
| - if (ProcessSnapshotOption(argv[i])) { |
| + if (ProcessSnapshotOption(argv[i]) || ProcessURLmappingOption(argv[i])) { |
| i += 1; |
| continue; |
| } |
| @@ -90,6 +108,49 @@ |
| } |
| +static Dart_Handle CreateSnapshotLibraryTagHandler(Dart_LibraryTag tag, |
| + Dart_Handle library, |
| + Dart_Handle url) { |
| + const bool kDontImportBuiltinLib = false; // Do not import builtin lib. |
| + Dart_Handle mapped_url = url; |
| + if ((tag == kCanonicalizeUrl) && (url_mapping != NULL)) { |
| + // We need to check if the passed in url is found in the url_mapping array, |
| + // in that case return the mapped entry as the cannonical form. |
| + if (!Dart_IsString8(url)) { |
| + return Dart_Error("url is not a string"); |
| + } |
| + const char* url_chars = NULL; |
| + Dart_Handle result = Dart_StringToCString(url, &url_chars); |
|
Anton Muhin
2011/11/14 14:02:13
nit: maybe TODO to switch to API like Dart_StringG
|
| + if (!Dart_IsValid(result)) { |
| + return Dart_Error("accessing url characters failed"); |
| + } |
| + for (int idx = 0; idx < url_mapping->count(); idx++) { |
| + const char* url_name = url_mapping->GetArgument(idx); |
| + int len = strlen(url_chars); |
| + if (!strncmp(url_chars, url_name, len) && (url_name[len] == ',')) { |
| + const char* url_mapped_name = url_name + len + 1; |
| + if (strlen(url_mapped_name) != 0) { |
| + mapped_url = Dart_NewString(url_mapped_name); |
| + } |
| + break; |
| + } |
| + } |
| + } |
| + return LibraryTagHandler(tag, library, mapped_url, kDontImportBuiltinLib); |
| +} |
| + |
| + |
| +static Dart_Handle LoadSnapshotCreationScript(const char* script_name) { |
| + Dart_Handle source = ReadStringFromFile(script_name); |
| + if (!Dart_IsValid(source)) { |
| + return source; |
| + } |
| + Dart_Handle url = Dart_NewString(script_name); |
| + |
| + return Dart_LoadScript(url, source, CreateSnapshotLibraryTagHandler); |
| +} |
| + |
| + |
| static void* SnapshotCreateCallback(void* data) { |
| const char* script_name = reinterpret_cast<const char*>(data); |
| Dart_Handle result; |
| @@ -151,6 +212,10 @@ |
| CommandLineOptions vm_options(argc); |
| char* script_name; |
| + // Initialize the URL mapping array. |
| + CommandLineOptions url_mapping_array(argc); |
| + url_mapping = &url_mapping_array; |
| + |
| // Parse command line arguments. |
| if (ParseArguments(argc, |
| argv, |