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

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
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;
22 32
23 33
24 // Global state which contains a pointer to the script name for which 34 // 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 35 // a snapshot needs to be created (NULL would result in the creation
26 // of a generic snapshot that contains only the corelibs). 36 // of a generic snapshot that contains only the corelibs).
27 static char* app_script_name = NULL; 37 static char* app_script_name = NULL;
28 38
29 39
30 // Global state that captures the URL mappings specified on the command line. 40 // Global state that captures the URL mappings specified on the command line.
31 static CommandLineOptions* url_mapping = NULL; 41 static CommandLineOptions* url_mapping = NULL;
(...skipping 10 matching lines...) Expand all
42 static const char* ProcessOption(const char* option, const char* name) { 52 static const char* ProcessOption(const char* option, const char* name) {
43 const intptr_t length = strlen(name); 53 const intptr_t length = strlen(name);
44 if (strncmp(option, name, length) == 0) { 54 if (strncmp(option, name, length) == 0) {
45 return (option + length); 55 return (option + length);
46 } 56 }
47 return NULL; 57 return NULL;
48 } 58 }
49 59
50 60
51 static bool ProcessSnapshotOption(const char* option) { 61 static bool ProcessSnapshotOption(const char* option) {
52 const char* kSnapshotOption = "--snapshot="; 62 const char* name = ProcessOption(option, "--snapshot=");
53 const char* name = ProcessOption(option, kSnapshotOption);
54 if (name != NULL) { 63 if (name != NULL) {
64 script_snapshot = false;
65 snapshot_filename = name;
66 return true;
67 }
68 name = ProcessOption(option, "--script_snapshot=");
69 if (name != NULL) {
70 script_snapshot = true;
55 snapshot_filename = name; 71 snapshot_filename = name;
56 return true; 72 return true;
57 } 73 }
58 return false; 74 return false;
59 } 75 }
60 76
61 77
62 static bool ProcessURLmappingOption(const char* option) { 78 static bool ProcessURLmappingOption(const char* option) {
63 const char* kURLmappingOption = "--url_mapping="; 79 const char* mapping = ProcessOption(option, "--url_mapping=");
64 const char* mapping = ProcessOption(option, kURLmappingOption); 80 if (mapping == NULL) {
81 mapping = ProcessOption(option, "--url-mapping=");
82 }
65 if (mapping != NULL) { 83 if (mapping != NULL) {
66 url_mapping->AddArgument(mapping); 84 url_mapping->AddArgument(mapping);
67 return true; 85 return true;
68 } 86 }
69 return false; 87 return false;
70 } 88 }
71 89
72 90
73 // Parse out the command line arguments. Returns -1 if the arguments 91 // Parse out the command line arguments. Returns -1 if the arguments
74 // are incorrect, 0 otherwise. 92 // are incorrect, 0 otherwise.
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 Dart_Handle source = DartUtils::ReadStringFromFile(script_name); 167 Dart_Handle source = DartUtils::ReadStringFromFile(script_name);
150 if (Dart_IsError(source)) { 168 if (Dart_IsError(source)) {
151 return source; // source contains the error string. 169 return source; // source contains the error string.
152 } 170 }
153 Dart_Handle url = Dart_NewString(script_name); 171 Dart_Handle url = Dart_NewString(script_name);
154 172
155 return Dart_LoadScript(url, source); 173 return Dart_LoadScript(url, source);
156 } 174 }
157 175
158 176
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( 177 static Dart_Handle LoadGenericSnapshotCreationScript(
201 Builtin::BuiltinLibraryId id) { 178 Builtin::BuiltinLibraryId id) {
202 Dart_Handle source = Builtin::Source(id); 179 Dart_Handle source = Builtin::Source(id);
203 if (Dart_IsError(source)) { 180 if (Dart_IsError(source)) {
204 return source; // source contains the error string. 181 return source; // source contains the error string.
205 } 182 }
206 Dart_Handle lib; 183 Dart_Handle lib;
207 // Load the builtin library to make it available in the snapshot 184 // Load the builtin library to make it available in the snapshot
208 // for importing. 185 // for importing.
209 lib = Builtin::LoadLibrary(id); 186 lib = Builtin::LoadLibrary(id);
(...skipping 16 matching lines...) Expand all
226 const char* err_msg = Dart_GetError(library); 203 const char* err_msg = Dart_GetError(library);
227 fprintf(stderr, "Errors encountered while loading: %s\n", err_msg); 204 fprintf(stderr, "Errors encountered while loading: %s\n", err_msg);
228 Dart_ExitScope(); 205 Dart_ExitScope();
229 Dart_ShutdownIsolate(); 206 Dart_ShutdownIsolate();
230 exit(255); 207 exit(255);
231 } 208 }
232 ASSERT(Dart_IsLibrary(library)); 209 ASSERT(Dart_IsLibrary(library));
233 } 210 }
234 211
235 212
213 static void CreateAndWriteSnapshot(bool script_snapshot) {
214 Dart_Handle result;
215 uint8_t* buffer = NULL;
216 intptr_t size = 0;
217
218 // First create a snapshot.
219 if (script_snapshot) {
220 // Script snapshot specified so create a script snapshot.
221 result = Dart_CreateScriptSnapshot(&buffer, &size);
222 } else {
223 // Create a full snapshot.
224 result = Dart_CreateSnapshot(&buffer, &size);
225 }
226 CHECK_RESULT(result);
227
228 // Now write the snapshot out to specified file and exit.
229 WriteSnapshotFile(buffer, size);
230 Dart_ExitScope();
231
232 // Shutdown the isolate.
233 Dart_ShutdownIsolate();
234 }
235
236
237 static void SetupForGenericSnapshotCreation() {
238 // Set up the library tag handler for this isolate.
239 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler);
240 if (Dart_IsError(result)) {
241 fprintf(stderr, "%s", Dart_GetError(result));
242 Dart_ExitScope();
243 Dart_ShutdownIsolate();
244 exit(255);
245 }
246 // This is a generic dart snapshot which needs builtin library setup.
247 Dart_Handle library =
248 LoadGenericSnapshotCreationScript(Builtin::kBuiltinLibrary);
249 VerifyLoaded(library);
250 library = LoadGenericSnapshotCreationScript(Builtin::kCryptoLibrary);
251 VerifyLoaded(library);
252 library = LoadGenericSnapshotCreationScript(Builtin::kIOLibrary);
253 VerifyLoaded(library);
254 library = LoadGenericSnapshotCreationScript(Builtin::kJsonLibrary);
255 VerifyLoaded(library);
256 library = LoadGenericSnapshotCreationScript(Builtin::kUriLibrary);
257 VerifyLoaded(library);
258 library = LoadGenericSnapshotCreationScript(Builtin::kUtfLibrary);
259 VerifyLoaded(library);
260 }
261
262
236 int main(int argc, char** argv) { 263 int main(int argc, char** argv) {
237 CommandLineOptions vm_options(argc); 264 CommandLineOptions vm_options(argc);
238 265
239 // Initialize the URL mapping array. 266 // Initialize the URL mapping array.
240 CommandLineOptions url_mapping_array(argc); 267 CommandLineOptions url_mapping_array(argc);
241 url_mapping = &url_mapping_array; 268 url_mapping = &url_mapping_array;
242 269
243 // Parse command line arguments. 270 // Parse command line arguments.
244 if (ParseArguments(argc, 271 if (ParseArguments(argc,
245 argv, 272 argv,
246 &vm_options, 273 &vm_options,
247 &app_script_name) < 0) { 274 &app_script_name) < 0) {
248 PrintUsage(); 275 PrintUsage();
249 return 255; 276 return 255;
250 } 277 }
251 278
252 if (snapshot_filename == NULL) { 279 if (snapshot_filename == NULL) {
253 fprintf(stderr, "No snapshot output file specified\n"); 280 fprintf(stderr, "No snapshot output file specified\n");
254 return 255; 281 return 255;
255 } 282 }
256 283
284 DartUtils::SetOriginalWorkingDirectory();
285
257 Dart_SetVMFlags(vm_options.count(), vm_options.arguments()); 286 Dart_SetVMFlags(vm_options.count(), vm_options.arguments());
258 287
259 // Initialize the Dart VM. 288 // Initialize the Dart VM.
260 // Note: We don't expect isolates to be created from dart code during 289 // Note: We don't expect isolates to be created from dart code during
261 // snapshot generation. 290 // snapshot generation.
262 Dart_Initialize(NULL, NULL, NULL); 291 Dart_Initialize(NULL, NULL, NULL);
263 292
264 char* error; 293 char* error;
265 Dart_Isolate isolate = Dart_CreateIsolate(NULL, NULL, NULL, NULL, &error); 294 Dart_Isolate isolate = Dart_CreateIsolate(NULL, NULL, NULL, NULL, &error);
266 if (isolate == NULL) { 295 if (isolate == NULL) {
267 fprintf(stderr, "%s", error); 296 fprintf(stderr, "Error: %s", error);
268 free(error); 297 free(error);
269 exit(255); 298 exit(255);
270 } 299 }
271 300
272 Dart_Handle result; 301 Dart_Handle result;
273 Dart_Handle library; 302 Dart_Handle library;
274 Dart_EnterScope(); 303 Dart_EnterScope();
275 304
276 ASSERT(snapshot_filename != NULL); 305 ASSERT(snapshot_filename != NULL);
277 // Load up the script before a snapshot is created. 306 // Load up the script before a snapshot is created.
278 if (app_script_name != NULL) { 307 if (app_script_name != NULL) {
279 // Set up the library tag handler for this isolate. 308 if (!script_snapshot) {
280 result = Dart_SetLibraryTagHandler(CreateSnapshotLibraryTagHandler); 309 // This is the case of a custom embedder (e.g: dartium) trying to
281 if (Dart_IsError(result)) { 310 // create a full snapshot. Set up the library tag handler for this case
282 fprintf(stderr, "%s", Dart_GetError(result)); 311 // in such a manner that it will use the URL mapping specified on the
312 // command line to load the libraries.
313 result = Dart_SetLibraryTagHandler(CreateSnapshotLibraryTagHandler);
314 CHECK_RESULT(result);
315 // Load the specified script.
316 library = LoadSnapshotCreationScript(app_script_name);
317 VerifyLoaded(library);
318 CreateAndWriteSnapshot(false);
319 } else {
320 // This is the case where we want to create a script snapshot of
321 // the specified script. There will be no URL mapping specified for
322 // this case, use the generic library tag handler.
323
324 // First setup and create a generic full snapshot.
325 SetupForGenericSnapshotCreation();
326 uint8_t* buffer = NULL;
327 intptr_t size = 0;
328 result = Dart_CreateSnapshot(&buffer, &size);
329 CHECK_RESULT(result);
330
331 // Save the snapshot buffer as we are about to shutdown the isolate.
332 uint8_t* snapshot_buffer = reinterpret_cast<uint8_t*>(malloc(size));
333 ASSERT(snapshot_buffer != NULL);
334 memmove(snapshot_buffer, buffer, size);
335
336 // Shutdown the isolate.
283 Dart_ExitScope(); 337 Dart_ExitScope();
284 Dart_ShutdownIsolate(); 338 Dart_ShutdownIsolate();
285 exit(255); 339
340 // Now load the specified script and create a script snapshot.
341 Dart_Isolate isolate = Dart_CreateIsolate(NULL,
342 NULL,
343 snapshot_buffer,
344 NULL,
345 &error);
346 free(snapshot_buffer);
347 if (isolate == NULL) {
348 fprintf(stderr, "%s", error);
349 free(error);
350 exit(255);
351 }
352 Dart_EnterScope();
353
354 // Setup generic library tag handler.
355 result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler);
356 CHECK_RESULT(result);
357
358 // Get handle to builtin library.
359 Dart_Handle builtin_lib = Builtin::LoadLibrary(Builtin::kBuiltinLibrary);
360 CHECK_RESULT(builtin_lib);
361
362 // Load specified script.
363 library = DartUtils::LoadScript(app_script_name, true, builtin_lib);
364
365 // Implicitly import builtin into app.
366 Builtin::ImportLibrary(library, Builtin::kBuiltinLibrary);
367
368 // Now create and write snapshot of script.
369 CreateAndWriteSnapshot(true);
286 } 370 }
287 // Load the specified script.
288 library = LoadSnapshotCreationScript(app_script_name);
289 VerifyLoaded(library);
290 } else { 371 } else {
291 // Set up the library tag handler for this isolate. 372 SetupForGenericSnapshotCreation();
292 result = Dart_SetLibraryTagHandler(BuiltinLibraryTagHandler); 373 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 } 374 }
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; 375 return 0;
333 } 376 }
OLDNEW
« no previous file with comments | « runtime/bin/dartutils.cc ('k') | runtime/bin/main.cc » ('j') | runtime/bin/main.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698