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

Side by Side Diff: bin/gen_snapshot.cc

Issue 8439061: Support command line parameters to specify a url to file name mapping. This is used during snapsh... (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/runtime/
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 | « no previous file | bin/process_script.h » ('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 // Generate a snapshot file after loading all the scripts specified on the 5 // Generate a snapshot file after loading all the scripts specified on the
6 // command line. 6 // command line.
7 7
8 #include <stdlib.h> 8 #include <stdlib.h>
9 #include <string.h> 9 #include <string.h>
10 #include <stdio.h> 10 #include <stdio.h>
11 11
12 #include "include/dart_api.h" 12 #include "include/dart_api.h"
13 13
14 #include "bin/builtin.h" 14 #include "bin/builtin.h"
15 #include "bin/file.h" 15 #include "bin/file.h"
16 #include "bin/globals.h" 16 #include "bin/globals.h"
17 #include "bin/process_script.h" 17 #include "bin/process_script.h"
18 18
19 // Global state that indicates whether a snapshot is to be created and 19 // Global state that indicates whether a snapshot is to be created and
20 // if so which file to write the snapshot into. 20 // if so which file to write the snapshot into.
21 static const char* snapshot_filename = NULL; 21 static const char* snapshot_filename = NULL;
22 22
23 // Global state that captures the URL mappings specified on the command line.
24 static CommandLineOptions* url_mapping = NULL;
25
23 static bool IsValidFlag(const char* name, 26 static bool IsValidFlag(const char* name,
24 const char* prefix, 27 const char* prefix,
25 intptr_t prefix_length) { 28 intptr_t prefix_length) {
26 intptr_t name_length = strlen(name); 29 intptr_t name_length = strlen(name);
27 return ((name_length > prefix_length) && 30 return ((name_length > prefix_length) &&
28 (strncmp(name, prefix, prefix_length) == 0)); 31 (strncmp(name, prefix, prefix_length) == 0));
29 } 32 }
30 33
31 34
32 static const char* ProcessOption(const char* option, const char* name) { 35 static const char* ProcessOption(const char* option, const char* name) {
33 const intptr_t length = strlen(name); 36 const intptr_t length = strlen(name);
34 if (strncmp(option, name, length) == 0) { 37 if (strncmp(option, name, length) == 0) {
35 return (option + length); 38 return (option + length);
36 } 39 }
37 return NULL; 40 return NULL;
38 } 41 }
39 42
40 43
41 static bool ProcessSnapshotOption(const char* option) { 44 static bool ProcessSnapshotOption(const char* option) {
42 const char* kSnapshotOption = "--snapshot="; 45 const char* kSnapshotOption = "--snapshot=";
43 snapshot_filename = ProcessOption(option, kSnapshotOption); 46 const char* name = ProcessOption(option, kSnapshotOption);
44 return snapshot_filename != NULL; 47 if (name != NULL) {
48 snapshot_filename = name;
49 return true;
50 }
51 return false;
52 }
53
54
55 static bool ProcessURLmappingOption(const char* option) {
56 const char* kURLmappingOption = "--url_mapping=";
57 const char* mapping = ProcessOption(option, kURLmappingOption);
58 if (mapping != NULL) {
59 url_mapping->AddArgument(mapping);
60 return true;
61 }
62 return false;
45 } 63 }
46 64
47 65
48 // Parse out the command line arguments. Returns -1 if the arguments 66 // Parse out the command line arguments. Returns -1 if the arguments
49 // are incorrect, 0 otherwise. 67 // are incorrect, 0 otherwise.
50 static int ParseArguments(int argc, 68 static int ParseArguments(int argc,
51 char** argv, 69 char** argv,
52 CommandLineOptions* vm_options, 70 CommandLineOptions* vm_options,
53 char** script_name) { 71 char** script_name) {
54 const char* kPrefix = "--"; 72 const char* kPrefix = "--";
55 const intptr_t kPrefixLen = strlen(kPrefix); 73 const intptr_t kPrefixLen = strlen(kPrefix);
56 74
57 // Skip the binary name. 75 // Skip the binary name.
58 int i = 1; 76 int i = 1;
59 77
60 // Parse out the vm options. 78 // Parse out the vm options.
61 while ((i < argc) && IsValidFlag(argv[i], kPrefix, kPrefixLen)) { 79 while ((i < argc) && IsValidFlag(argv[i], kPrefix, kPrefixLen)) {
62 if (ProcessSnapshotOption(argv[i])) { 80 if (ProcessSnapshotOption(argv[i]) || ProcessURLmappingOption(argv[i])) {
63 i += 1; 81 i += 1;
64 continue; 82 continue;
65 } 83 }
66 vm_options->AddArgument(argv[i]); 84 vm_options->AddArgument(argv[i]);
67 i += 1; 85 i += 1;
68 } 86 }
69 87
70 // Get the script name. 88 // Get the script name.
71 if (i < argc) { 89 if (i < argc) {
72 *script_name = argv[i]; 90 *script_name = argv[i];
(...skipping 10 matching lines...) Expand all
83 const bool kWritable = true; 101 const bool kWritable = true;
84 File* file = File::Open(snapshot_filename, kWritable); 102 File* file = File::Open(snapshot_filename, kWritable);
85 ASSERT(file != NULL); 103 ASSERT(file != NULL);
86 for (intptr_t i = 0; i < size; i++) { 104 for (intptr_t i = 0; i < size; i++) {
87 file->WriteByte(buffer[i]); 105 file->WriteByte(buffer[i]);
88 } 106 }
89 delete file; 107 delete file;
90 } 108 }
91 109
92 110
111 static Dart_Handle CreateSnapshotLibraryTagHandler(Dart_LibraryTag tag,
112 Dart_Handle library,
113 Dart_Handle url) {
114 const bool kDontImportBuiltinLib = false; // Do not import builtin lib.
115 Dart_Handle mapped_url = url;
116 if ((tag == kCanonicalizeUrl) && (url_mapping != NULL)) {
117 // We need to check if the passed in url is found in the url_mapping array,
118 // in that case return the mapped entry as the cannonical form.
119 if (!Dart_IsString8(url)) {
120 return Dart_Error("url is not a string");
121 }
122 const char* url_chars = NULL;
123 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
124 if (!Dart_IsValid(result)) {
125 return Dart_Error("accessing url characters failed");
126 }
127 for (int idx = 0; idx < url_mapping->count(); idx++) {
128 const char* url_name = url_mapping->GetArgument(idx);
129 int len = strlen(url_chars);
130 if (!strncmp(url_chars, url_name, len) && (url_name[len] == ',')) {
131 const char* url_mapped_name = url_name + len + 1;
132 if (strlen(url_mapped_name) != 0) {
133 mapped_url = Dart_NewString(url_mapped_name);
134 }
135 break;
136 }
137 }
138 }
139 return LibraryTagHandler(tag, library, mapped_url, kDontImportBuiltinLib);
140 }
141
142
143 static Dart_Handle LoadSnapshotCreationScript(const char* script_name) {
144 Dart_Handle source = ReadStringFromFile(script_name);
145 if (!Dart_IsValid(source)) {
146 return source;
147 }
148 Dart_Handle url = Dart_NewString(script_name);
149
150 return Dart_LoadScript(url, source, CreateSnapshotLibraryTagHandler);
151 }
152
153
93 static void* SnapshotCreateCallback(void* data) { 154 static void* SnapshotCreateCallback(void* data) {
94 const char* script_name = reinterpret_cast<const char*>(data); 155 const char* script_name = reinterpret_cast<const char*>(data);
95 Dart_Handle result; 156 Dart_Handle result;
96 Dart_EnterScope(); 157 Dart_EnterScope();
97 158
98 ASSERT(snapshot_filename != NULL); 159 ASSERT(snapshot_filename != NULL);
99 160
100 // If a file is specified on the command line, load it up before a snapshot 161 // If a file is specified on the command line, load it up before a snapshot
101 // is created. 162 // is created.
102 if (script_name != NULL) { 163 if (script_name != NULL) {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 fprintf(stderr, 205 fprintf(stderr,
145 "dart [<vm-flags>] " 206 "dart [<vm-flags>] "
146 "[<dart-script-file>]\n"); 207 "[<dart-script-file>]\n");
147 } 208 }
148 209
149 210
150 int main(int argc, char** argv) { 211 int main(int argc, char** argv) {
151 CommandLineOptions vm_options(argc); 212 CommandLineOptions vm_options(argc);
152 char* script_name; 213 char* script_name;
153 214
215 // Initialize the URL mapping array.
216 CommandLineOptions url_mapping_array(argc);
217 url_mapping = &url_mapping_array;
218
154 // Parse command line arguments. 219 // Parse command line arguments.
155 if (ParseArguments(argc, 220 if (ParseArguments(argc,
156 argv, 221 argv,
157 &vm_options, 222 &vm_options,
158 &script_name) < 0) { 223 &script_name) < 0) {
159 PrintUsage(); 224 PrintUsage();
160 return 255; 225 return 255;
161 } 226 }
162 227
163 if (snapshot_filename == NULL) { 228 if (snapshot_filename == NULL) {
(...skipping 11 matching lines...) Expand all
175 // and writes out a snapshot. 240 // and writes out a snapshot.
176 Dart_Isolate isolate = Dart_CreateIsolate(NULL, script_name); 241 Dart_Isolate isolate = Dart_CreateIsolate(NULL, script_name);
177 if (isolate == NULL) { 242 if (isolate == NULL) {
178 return 255; 243 return 255;
179 } 244 }
180 245
181 // Shutdown the isolate. 246 // Shutdown the isolate.
182 Dart_ShutdownIsolate(); 247 Dart_ShutdownIsolate();
183 return 0; 248 return 0;
184 } 249 }
OLDNEW
« no previous file with comments | « no previous file | bin/process_script.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698