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

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

Issue 10871092: Added option --script-snapshot in gen_snapshot to generate the snapshot of a scipt into the file. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 3 months 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/dartutils.cc ('k') | runtime/bin/main.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) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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/dartutils.h" 15 #include "bin/dartutils.h"
16 #include "bin/file.h" 16 #include "bin/file.h"
17 #include "platform/globals.h" 17 #include "platform/globals.h"
18 18
19 #define CHECK_RESULT(result) \
20 if (Dart_IsError(result)) { \
21 fprintf(stderr, "Error: %s", Dart_GetError(result)); \
22 Dart_ExitScope(); \
23 Dart_ShutdownIsolate(); \
24 exit(255); \
25 } \
26
27
19 // Global state that indicates whether a snapshot is to be created and 28 // Global state that indicates whether a snapshot is to be created and
20 // if so which file to write the snapshot into. 29 // if so which file to write the snapshot into.
21 static const char* snapshot_filename = NULL; 30 static const char* snapshot_filename = NULL;
31 static bool script_snapshot = false;
32 static const char* package_root = NULL;
22 33
23 34
24 // Global state which contains a pointer to the script name for which 35 // Global state which contains a pointer to the script name for which
25 // a snapshot needs to be created (NULL would result in the creation 36 // a snapshot needs to be created (NULL would result in the creation
26 // of a generic snapshot that contains only the corelibs). 37 // of a generic snapshot that contains only the corelibs).
27 static char* app_script_name = NULL; 38 static char* app_script_name = NULL;
28 39
29 40
30 // Global state that captures the URL mappings specified on the command line. 41 // Global state that captures the URL mappings specified on the command line.
31 static CommandLineOptions* url_mapping = NULL; 42 static CommandLineOptions* url_mapping = NULL;
(...skipping 10 matching lines...) Expand all
42 static const char* ProcessOption(const char* option, const char* name) { 53 static const char* ProcessOption(const char* option, const char* name) {
43 const intptr_t length = strlen(name); 54 const intptr_t length = strlen(name);
44 if (strncmp(option, name, length) == 0) { 55 if (strncmp(option, name, length) == 0) {
45 return (option + length); 56 return (option + length);
46 } 57 }
47 return NULL; 58 return NULL;
48 } 59 }
49 60
50 61
51 static bool ProcessSnapshotOption(const char* option) { 62 static bool ProcessSnapshotOption(const char* option) {
52 const char* kSnapshotOption = "--snapshot="; 63 const char* name = ProcessOption(option, "--snapshot=");
53 const char* name = ProcessOption(option, kSnapshotOption);
54 if (name != NULL) { 64 if (name != NULL) {
65 script_snapshot = false;
66 snapshot_filename = name;
67 return true;
68 }
69 name = ProcessOption(option, "--script_snapshot=");
70 if (name != NULL) {
71 script_snapshot = true;
55 snapshot_filename = name; 72 snapshot_filename = name;
56 return true; 73 return true;
57 } 74 }
58 return false; 75 return false;
59 } 76 }
60 77
61 78
79 static bool ProcessPackageRootOption(const char* option) {
80 const char* name = ProcessOption(option, "--package_root=");
81 if (name != NULL) {
82 package_root = name;
83 return true;
84 }
85 return false;
86 }
87
88
62 static bool ProcessURLmappingOption(const char* option) { 89 static bool ProcessURLmappingOption(const char* option) {
63 const char* kURLmappingOption = "--url_mapping="; 90 const char* mapping = ProcessOption(option, "--url_mapping=");
64 const char* mapping = ProcessOption(option, kURLmappingOption); 91 if (mapping == NULL) {
92 mapping = ProcessOption(option, "--url-mapping=");
93 }
65 if (mapping != NULL) { 94 if (mapping != NULL) {
66 url_mapping->AddArgument(mapping); 95 url_mapping->AddArgument(mapping);
67 return true; 96 return true;
68 } 97 }
69 return false; 98 return false;
70 } 99 }
71 100
72 101
73 // Parse out the command line arguments. Returns -1 if the arguments 102 // Parse out the command line arguments. Returns -1 if the arguments
74 // are incorrect, 0 otherwise. 103 // are incorrect, 0 otherwise.
75 static int ParseArguments(int argc, 104 static int ParseArguments(int argc,
76 char** argv, 105 char** argv,
77 CommandLineOptions* vm_options, 106 CommandLineOptions* vm_options,
78 char** script_name) { 107 char** script_name) {
79 const char* kPrefix = "--"; 108 const char* kPrefix = "--";
80 const intptr_t kPrefixLen = strlen(kPrefix); 109 const intptr_t kPrefixLen = strlen(kPrefix);
81 110
82 // Skip the binary name. 111 // Skip the binary name.
83 int i = 1; 112 int i = 1;
84 113
85 // Parse out the vm options. 114 // Parse out the vm options.
86 while ((i < argc) && IsValidFlag(argv[i], kPrefix, kPrefixLen)) { 115 while ((i < argc) && IsValidFlag(argv[i], kPrefix, kPrefixLen)) {
87 if (ProcessSnapshotOption(argv[i]) || ProcessURLmappingOption(argv[i])) { 116 if (ProcessSnapshotOption(argv[i]) ||
117 ProcessURLmappingOption(argv[i]) ||
118 ProcessPackageRootOption(argv[i])) {
88 i += 1; 119 i += 1;
89 continue; 120 continue;
90 } 121 }
91 vm_options->AddArgument(argv[i]); 122 vm_options->AddArgument(argv[i]);
92 i += 1; 123 i += 1;
93 } 124 }
94 125
95 // Get the script name. 126 // Get the script name.
96 if (i < argc) { 127 if (i < argc) {
97 *script_name = argv[i]; 128 *script_name = argv[i];
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 Dart_Handle source = DartUtils::ReadStringFromFile(script_name); 180 Dart_Handle source = DartUtils::ReadStringFromFile(script_name);
150 if (Dart_IsError(source)) { 181 if (Dart_IsError(source)) {
151 return source; // source contains the error string. 182 return source; // source contains the error string.
152 } 183 }
153 Dart_Handle url = Dart_NewString(script_name); 184 Dart_Handle url = Dart_NewString(script_name);
154 185
155 return Dart_LoadScript(url, source); 186 return Dart_LoadScript(url, source);
156 } 187 }
157 188
158 189
159 static Dart_Handle BuiltinLibraryTagHandler(Dart_LibraryTag tag,
160 Dart_Handle library,
161 Dart_Handle url) {
162 if (!Dart_IsLibrary(library)) {
163 return Dart_Error("not a library");
164 }
165 if (!Dart_IsString8(url)) {
166 return Dart_Error("url is not a string");
167 }
168 const char* url_string = NULL;
169 Dart_Handle result = Dart_StringToCString(url, &url_string);
170 if (Dart_IsError(result)) {
171 return result;
172 }
173 // We only support canonicalization of "dart:".
174 if (DartUtils::IsDartSchemeURL(url_string)) {
175 if (tag == kCanonicalizeUrl) {
176 return url;
177 }
178 ASSERT(tag == kImportTag);
179 // Handle imports of other built-in libraries present in the SDK.
180 Builtin::BuiltinLibraryId id;
181 if (DartUtils::IsDartCryptoLibURL(url_string)) {
182 id = Builtin::kCryptoLibrary;
183 } else if (DartUtils::IsDartIOLibURL(url_string)) {
184 id = Builtin::kIOLibrary;
185 } else if (DartUtils::IsDartJsonLibURL(url_string)) {
186 id = Builtin::kJsonLibrary;
187 } else if (DartUtils::IsDartUriLibURL(url_string)) {
188 id = Builtin::kUriLibrary;
189 } else if (DartUtils::IsDartUtfLibURL(url_string)) {
190 id = Builtin::kUtfLibrary;
191 } else {
192 return Dart_Error("Do not know how to load '%s'", url_string);
193 }
194 return Builtin::LoadLibrary(id);
195 }
196 return Dart_Error("unexpected tag encountered %d", tag);
197 }
198
199
200 static Dart_Handle LoadGenericSnapshotCreationScript( 190 static Dart_Handle LoadGenericSnapshotCreationScript(
201 Builtin::BuiltinLibraryId id) { 191 Builtin::BuiltinLibraryId id) {
202 Dart_Handle source = Builtin::Source(id); 192 Dart_Handle source = Builtin::Source(id);
203 if (Dart_IsError(source)) { 193 if (Dart_IsError(source)) {
204 return source; // source contains the error string. 194 return source; // source contains the error string.
205 } 195 }
206 Dart_Handle lib; 196 Dart_Handle lib;
207 // Load the builtin library to make it available in the snapshot 197 // Load the builtin library to make it available in the snapshot
208 // for importing. 198 // for importing.
209 lib = Builtin::LoadLibrary(id); 199 lib = Builtin::LoadLibrary(id);
(...skipping 16 matching lines...) Expand all
226 const char* err_msg = Dart_GetError(library); 216 const char* err_msg = Dart_GetError(library);
227 fprintf(stderr, "Errors encountered while loading: %s\n", err_msg); 217 fprintf(stderr, "Errors encountered while loading: %s\n", err_msg);
228 Dart_ExitScope(); 218 Dart_ExitScope();
229 Dart_ShutdownIsolate(); 219 Dart_ShutdownIsolate();
230 exit(255); 220 exit(255);
231 } 221 }
232 ASSERT(Dart_IsLibrary(library)); 222 ASSERT(Dart_IsLibrary(library));
233 } 223 }
234 224
235 225
226 static void CreateAndWriteSnapshot(bool script_snapshot) {
227 Dart_Handle result;
228 uint8_t* buffer = NULL;
229 intptr_t size = 0;
230
231 // First create a snapshot.
232 if (script_snapshot) {
233 // Script snapshot specified so create a script snapshot.
234 result = Dart_CreateScriptSnapshot(&buffer, &size);
235 } else {
236 // Create a full snapshot.
237 result = Dart_CreateSnapshot(&buffer, &size);
238 }
239 CHECK_RESULT(result);
240
241 // Now write the snapshot out to specified file and exit.
242 WriteSnapshotFile(buffer, size);
243 Dart_ExitScope();
244
245 // Shutdown the isolate.
246 Dart_ShutdownIsolate();
247 }
248
249
250 static void SetupForGenericSnapshotCreation() {
251 // Set up the library tag handler for this isolate.
252 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler);
253 if (Dart_IsError(result)) {
254 fprintf(stderr, "%s", Dart_GetError(result));
255 Dart_ExitScope();
256 Dart_ShutdownIsolate();
257 exit(255);
258 }
259 // This is a generic dart snapshot which needs builtin library setup.
260 Dart_Handle library =
261 LoadGenericSnapshotCreationScript(Builtin::kBuiltinLibrary);
262 VerifyLoaded(library);
263 library = LoadGenericSnapshotCreationScript(Builtin::kCryptoLibrary);
264 VerifyLoaded(library);
265 library = LoadGenericSnapshotCreationScript(Builtin::kIOLibrary);
266 VerifyLoaded(library);
267 library = LoadGenericSnapshotCreationScript(Builtin::kJsonLibrary);
268 VerifyLoaded(library);
269 library = LoadGenericSnapshotCreationScript(Builtin::kUriLibrary);
270 VerifyLoaded(library);
271 library = LoadGenericSnapshotCreationScript(Builtin::kUtfLibrary);
272 VerifyLoaded(library);
273 }
274
275
236 int main(int argc, char** argv) { 276 int main(int argc, char** argv) {
237 CommandLineOptions vm_options(argc); 277 CommandLineOptions vm_options(argc);
238 278
239 // Initialize the URL mapping array. 279 // Initialize the URL mapping array.
240 CommandLineOptions url_mapping_array(argc); 280 CommandLineOptions url_mapping_array(argc);
241 url_mapping = &url_mapping_array; 281 url_mapping = &url_mapping_array;
242 282
243 // Parse command line arguments. 283 // Parse command line arguments.
244 if (ParseArguments(argc, 284 if (ParseArguments(argc,
245 argv, 285 argv,
246 &vm_options, 286 &vm_options,
247 &app_script_name) < 0) { 287 &app_script_name) < 0) {
248 PrintUsage(); 288 PrintUsage();
249 return 255; 289 return 255;
250 } 290 }
251 291
252 if (snapshot_filename == NULL) { 292 if (snapshot_filename == NULL) {
253 fprintf(stderr, "No snapshot output file specified\n"); 293 fprintf(stderr, "No snapshot output file specified\n");
254 return 255; 294 return 255;
255 } 295 }
256 296
297 DartUtils::SetOriginalWorkingDirectory();
298
257 Dart_SetVMFlags(vm_options.count(), vm_options.arguments()); 299 Dart_SetVMFlags(vm_options.count(), vm_options.arguments());
258 300
259 // Initialize the Dart VM. 301 // Initialize the Dart VM.
260 // Note: We don't expect isolates to be created from dart code during 302 // Note: We don't expect isolates to be created from dart code during
261 // snapshot generation. 303 // snapshot generation.
262 Dart_Initialize(NULL, NULL, NULL); 304 Dart_Initialize(NULL, NULL, NULL);
263 305
264 char* error; 306 char* error;
265 Dart_Isolate isolate = Dart_CreateIsolate(NULL, NULL, NULL, NULL, &error); 307 Dart_Isolate isolate = Dart_CreateIsolate(NULL, NULL, NULL, NULL, &error);
266 if (isolate == NULL) { 308 if (isolate == NULL) {
267 fprintf(stderr, "%s", error); 309 fprintf(stderr, "Error: %s", error);
268 free(error); 310 free(error);
269 exit(255); 311 exit(255);
270 } 312 }
271 313
272 Dart_Handle result; 314 Dart_Handle result;
273 Dart_Handle library; 315 Dart_Handle library;
274 Dart_EnterScope(); 316 Dart_EnterScope();
275 317
276 ASSERT(snapshot_filename != NULL); 318 ASSERT(snapshot_filename != NULL);
277 // Load up the script before a snapshot is created. 319 // Load up the script before a snapshot is created.
278 if (app_script_name != NULL) { 320 if (app_script_name != NULL) {
279 // Set up the library tag handler for this isolate. 321 if (!script_snapshot) {
280 result = Dart_SetLibraryTagHandler(CreateSnapshotLibraryTagHandler); 322 // This is the case of a custom embedder (e.g: dartium) trying to
281 if (Dart_IsError(result)) { 323 // create a full snapshot. Set up the library tag handler for this case
282 fprintf(stderr, "%s", Dart_GetError(result)); 324 // in such a manner that it will use the URL mapping specified on the
325 // command line to load the libraries.
326 result = Dart_SetLibraryTagHandler(CreateSnapshotLibraryTagHandler);
327 CHECK_RESULT(result);
328 // Load the specified script.
329 library = LoadSnapshotCreationScript(app_script_name);
330 VerifyLoaded(library);
331 CreateAndWriteSnapshot(false);
332 } else {
333 // This is the case where we want to create a script snapshot of
334 // the specified script. There will be no URL mapping specified for
335 // this case, use the generic library tag handler.
336
337 // First setup and create a generic full snapshot.
338 SetupForGenericSnapshotCreation();
339 uint8_t* buffer = NULL;
340 intptr_t size = 0;
341 result = Dart_CreateSnapshot(&buffer, &size);
342 CHECK_RESULT(result);
343
344 // Save the snapshot buffer as we are about to shutdown the isolate.
345 uint8_t* snapshot_buffer = reinterpret_cast<uint8_t*>(malloc(size));
346 ASSERT(snapshot_buffer != NULL);
347 memmove(snapshot_buffer, buffer, size);
348
349 // Shutdown the isolate.
283 Dart_ExitScope(); 350 Dart_ExitScope();
284 Dart_ShutdownIsolate(); 351 Dart_ShutdownIsolate();
285 exit(255); 352
353 // Now load the specified script and create a script snapshot.
354 Dart_Isolate isolate = Dart_CreateIsolate(NULL,
355 NULL,
356 snapshot_buffer,
357 NULL,
358 &error);
359 free(snapshot_buffer);
360 if (isolate == NULL) {
361 fprintf(stderr, "%s", error);
362 free(error);
363 exit(255);
364 }
365 Dart_EnterScope();
366
367 // Setup generic library tag handler.
368 result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler);
369 CHECK_RESULT(result);
370
371 // Get handle to builtin library.
372 Dart_Handle builtin_lib = Builtin::LoadLibrary(Builtin::kBuiltinLibrary);
373 CHECK_RESULT(builtin_lib);
374
375 // Prepare for script loading by setting up the 'print' and 'timer'
376 // closures and setting up 'package root' for URI resolution.
377 result = DartUtils::PrepareForScriptLoading(package_root, builtin_lib);
378 CHECK_RESULT(result);
379
380 // Load specified script.
381 library = DartUtils::LoadScript(app_script_name, true, builtin_lib);
382
383 // Implicitly import builtin into app.
384 Builtin::ImportLibrary(library, Builtin::kBuiltinLibrary);
385
386 // Now create and write snapshot of script.
387 CreateAndWriteSnapshot(true);
286 } 388 }
287 // Load the specified script.
288 library = LoadSnapshotCreationScript(app_script_name);
289 VerifyLoaded(library);
290 } else { 389 } else {
291 // Set up the library tag handler for this isolate. 390 SetupForGenericSnapshotCreation();
292 result = Dart_SetLibraryTagHandler(BuiltinLibraryTagHandler); 391 CreateAndWriteSnapshot(false);
293 if (Dart_IsError(result)) {
294 fprintf(stderr, "%s", Dart_GetError(result));
295 Dart_ExitScope();
296 Dart_ShutdownIsolate();
297 exit(255);
298 }
299 // This is a generic dart snapshot which needs builtin library setup.
300 Dart_Handle library =
301 LoadGenericSnapshotCreationScript(Builtin::kBuiltinLibrary);
302 VerifyLoaded(library);
303 library = LoadGenericSnapshotCreationScript(Builtin::kCryptoLibrary);
304 VerifyLoaded(library);
305 library = LoadGenericSnapshotCreationScript(Builtin::kIOLibrary);
306 VerifyLoaded(library);
307 library = LoadGenericSnapshotCreationScript(Builtin::kJsonLibrary);
308 VerifyLoaded(library);
309 library = LoadGenericSnapshotCreationScript(Builtin::kUriLibrary);
310 VerifyLoaded(library);
311 library = LoadGenericSnapshotCreationScript(Builtin::kUtfLibrary);
312 VerifyLoaded(library);
313 } 392 }
314
315 uint8_t* buffer = NULL;
316 intptr_t size = 0;
317 // First create the snapshot.
318 result = Dart_CreateSnapshot(&buffer, &size);
319 if (Dart_IsError(result)) {
320 const char* err_msg = Dart_GetError(result);
321 fprintf(stderr, "Error while creating snapshot: %s\n", err_msg);
322 Dart_ExitScope();
323 Dart_ShutdownIsolate();
324 exit(255);
325 }
326 // Now write the snapshot out to specified file and exit.
327 WriteSnapshotFile(buffer, size);
328 Dart_ExitScope();
329
330 // Shutdown the isolate.
331 Dart_ShutdownIsolate();
332 return 0; 393 return 0;
333 } 394 }
OLDNEW
« no previous file with comments | « runtime/bin/dartutils.cc ('k') | runtime/bin/main.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698