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

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

Issue 11421215: Support symbolic links and package imports in gen_snapshot. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: . Created 8 years 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') | no next file » | 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>
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 static void WriteSnapshotFile(const uint8_t* buffer, const intptr_t size) { 142 static void WriteSnapshotFile(const uint8_t* buffer, const intptr_t size) {
143 File* file = File::Open(snapshot_filename, File::kWriteTruncate); 143 File* file = File::Open(snapshot_filename, File::kWriteTruncate);
144 ASSERT(file != NULL); 144 ASSERT(file != NULL);
145 for (intptr_t i = 0; i < size; i++) { 145 for (intptr_t i = 0; i < size; i++) {
146 file->WriteByte(buffer[i]); 146 file->WriteByte(buffer[i]);
147 } 147 }
148 delete file; 148 delete file;
149 } 149 }
150 150
151 151
152 class UriResolverIsolateScope {
153 public:
154 UriResolverIsolateScope() {
155 ASSERT(isolate != NULL);
156 snapshotted_isolate_ = Dart_CurrentIsolate();
157 Dart_ExitIsolate();
158 Dart_EnterIsolate(isolate);
159 Dart_EnterScope();
160 }
161
162 ~UriResolverIsolateScope() {
163 ASSERT(snapshotted_isolate_ != NULL);
164 Dart_ExitScope();
165 Dart_ExitIsolate();
166 Dart_EnterIsolate(snapshotted_isolate_);
167 }
168
169 static Dart_Isolate isolate;
170
171 private:
172 Dart_Isolate snapshotted_isolate_;
173
174 DISALLOW_COPY_AND_ASSIGN(UriResolverIsolateScope);
175 };
176
177 Dart_Isolate UriResolverIsolateScope::isolate = NULL;
178
179
180 static Dart_Handle ResolveScriptUri(const char* script_uri) {
181 bool failed = false;
182 char* result_string = NULL;
183
184 {
185 UriResolverIsolateScope scope;
186
187 // Run DartUtils::ResolveScriptUri in context of uri resolver isolate.
188 Dart_Handle builtin_lib =
189 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary);
190 CHECK_RESULT(builtin_lib);
191
192 Dart_Handle result = DartUtils::ResolveScriptUri(
193 DartUtils::NewString(script_uri), builtin_lib);
194 if (Dart_IsError(result)) {
195 failed = true;
196 result_string = strdup(Dart_GetError(result));
197 } else {
198 result_string = strdup(DartUtils::GetStringValue(result));
199 }
200 }
201
202 Dart_Handle result = failed ? Dart_NewApiError(result_string) :
203 DartUtils::NewString(result_string);
204 free(result_string);
205 return result;
206 }
207
208
209 static Dart_Handle FilePathFromUri(const char* script_uri) {
210 bool failed = false;
211 char* result_string = NULL;
212
213 {
214 UriResolverIsolateScope scope;
215
216 // Run DartUtils::FilePathFromUri in context of uri resolver isolate.
217 Dart_Handle builtin_lib =
218 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary);
219 CHECK_RESULT(builtin_lib);
220
221 Dart_Handle result = DartUtils::FilePathFromUri(
222 DartUtils::NewString(script_uri), builtin_lib);
223 if (Dart_IsError(result)) {
224 failed = true;
225 result_string = strdup(Dart_GetError(result));
226 } else {
227 result_string = strdup(DartUtils::GetStringValue(result));
228 }
229 }
230
231 Dart_Handle result = failed ? Dart_NewApiError(result_string) :
232 DartUtils::NewString(result_string);
233 free(result_string);
234 return result;
235 }
236
237
238 static Dart_Handle ResolveUri(const char* library_uri, const char* uri) {
239 bool failed = false;
240 char* result_string = NULL;
241
242 {
243 UriResolverIsolateScope scope;
244
245 // Run DartUtils::ResolveUri in context of uri resolver isolate.
246 Dart_Handle builtin_lib =
247 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary);
248 CHECK_RESULT(builtin_lib);
249
250 Dart_Handle result = DartUtils::ResolveUri(
251 DartUtils::NewString(library_uri),
252 DartUtils::NewString(uri),
253 builtin_lib);
254 if (Dart_IsError(result)) {
255 failed = true;
256 result_string = strdup(Dart_GetError(result));
257 } else {
258 result_string = strdup(DartUtils::GetStringValue(result));
259 }
260 }
261
262 Dart_Handle result = failed ? Dart_NewApiError(result_string) :
263 DartUtils::NewString(result_string);
264 free(result_string);
265 return result;
266 }
267
268
152 static Dart_Handle CreateSnapshotLibraryTagHandler(Dart_LibraryTag tag, 269 static Dart_Handle CreateSnapshotLibraryTagHandler(Dart_LibraryTag tag,
153 Dart_Handle library, 270 Dart_Handle library,
154 Dart_Handle url) { 271 Dart_Handle url) {
155 if (!Dart_IsLibrary(library)) { 272 if (!Dart_IsLibrary(library)) {
156 return Dart_Error("not a library"); 273 return Dart_Error("not a library");
157 } 274 }
275 Dart_Handle library_url = Dart_LibraryUrl(library);
276 if (Dart_IsError(library_url)) {
277 return Dart_Error("accessing library url failed");
278 }
279 const char* library_url_string = DartUtils::GetStringValue(library_url);
280 const char* mapped_library_url_string = DartUtils::MapLibraryUrl(
281 url_mapping, library_url_string);
282 if (mapped_library_url_string != NULL) {
283 library_url = ResolveScriptUri(mapped_library_url_string);
284 library_url_string = DartUtils::GetStringValue(library_url);
285 }
286
158 if (!Dart_IsString(url)) { 287 if (!Dart_IsString(url)) {
159 return Dart_Error("url is not a string"); 288 return Dart_Error("url is not a string");
160 } 289 }
161 const char* url_string = NULL; 290 const char* url_string = DartUtils::GetStringValue(url);
162 Dart_Handle result = Dart_StringToCString(url, &url_string); 291 const char* mapped_url_string = DartUtils::MapLibraryUrl(url_mapping,
163 if (Dart_IsError(result)) { 292 url_string);
164 return result; 293
294 if (tag == kCanonicalizeUrl) {
295 // Keep original names for libraries that have a mapping (e.g. dart:io).
296 if (mapped_url_string) {
297 return url;
298 }
299 return ResolveUri(library_url_string, url_string);
165 } 300 }
166 301
167 // If the URL starts with "dart:" then it is handled specially. 302 Dart_Handle resolved_url = url;
168 bool is_dart_scheme_url = DartUtils::IsDartSchemeURL(url_string); 303 if (mapped_url_string != NULL) {
169 if (tag == kCanonicalizeUrl) { 304 // Mapped urls are relative to working directory.
170 if (is_dart_scheme_url) { 305 resolved_url = ResolveScriptUri(mapped_url_string);
171 return url; 306 if (Dart_IsError(resolved_url)) {
307 return resolved_url;
172 } 308 }
173 return DartUtils::CanonicalizeURL(url_mapping, library, url_string);
174 } 309 }
175 return DartUtils::LoadSource(url_mapping, 310
311 // Get the file path out of the url.
312 Dart_Handle file_path = FilePathFromUri(
313 DartUtils::GetStringValue(resolved_url));
314 if (Dart_IsError(file_path)) {
315 return file_path;
316 }
317
318 return DartUtils::LoadSource(NULL,
176 library, 319 library,
177 url, 320 url,
178 tag, 321 tag,
179 url_string); 322 DartUtils::GetStringValue(file_path));
180 } 323 }
181 324
182 325
183 static Dart_Handle LoadSnapshotCreationScript(const char* script_name) { 326 static Dart_Handle LoadSnapshotCreationScript(const char* script_name) {
184 Dart_Handle source = DartUtils::ReadStringFromFile(script_name); 327 Dart_Handle resolved_script_uri = ResolveScriptUri(script_name);
328 if (Dart_IsError(resolved_script_uri)) {
329 return resolved_script_uri;
330 }
331 Dart_Handle script_path = FilePathFromUri(
332 DartUtils::GetStringValue(resolved_script_uri));
333 if (Dart_IsError(script_path)) {
334 return script_path;
335 }
336 Dart_Handle source = DartUtils::ReadStringFromFile(
337 DartUtils::GetStringValue(script_path));
185 if (Dart_IsError(source)) { 338 if (Dart_IsError(source)) {
186 return source; // source contains the error string. 339 return source;
187 } 340 }
188 Dart_Handle url = DartUtils::NewString(script_name); 341 return Dart_LoadScript(resolved_script_uri, source);
189
190 return Dart_LoadScript(url, source);
191 } 342 }
192 343
193 344
194 static Dart_Handle LoadGenericSnapshotCreationScript( 345 static Dart_Handle LoadGenericSnapshotCreationScript(
195 Builtin::BuiltinLibraryId id) { 346 Builtin::BuiltinLibraryId id) {
196 Dart_Handle source = Builtin::Source(id); 347 Dart_Handle source = Builtin::Source(id);
197 if (Dart_IsError(source)) { 348 if (Dart_IsError(source)) {
198 return source; // source contains the error string. 349 return source; // source contains the error string.
199 } 350 }
200 Dart_Handle lib; 351 Dart_Handle lib;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 391
241 // Now write the snapshot out to specified file and exit. 392 // Now write the snapshot out to specified file and exit.
242 WriteSnapshotFile(buffer, size); 393 WriteSnapshotFile(buffer, size);
243 Dart_ExitScope(); 394 Dart_ExitScope();
244 395
245 // Shutdown the isolate. 396 // Shutdown the isolate.
246 Dart_ShutdownIsolate(); 397 Dart_ShutdownIsolate();
247 } 398 }
248 399
249 400
250 static void SetupForGenericSnapshotCreation() { 401 static void SetupForUriResolution() {
251 // Set up the library tag handler for this isolate. 402 // Set up the library tag handler for this isolate.
252 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); 403 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler);
253 if (Dart_IsError(result)) { 404 if (Dart_IsError(result)) {
254 Log::PrintErr("%s", Dart_GetError(result)); 405 Log::PrintErr("%s", Dart_GetError(result));
255 Dart_ExitScope(); 406 Dart_ExitScope();
256 Dart_ShutdownIsolate(); 407 Dart_ShutdownIsolate();
257 exit(255); 408 exit(255);
258 } 409 }
259 // This is a generic dart snapshot which needs builtin library setup. 410 // This is a generic dart snapshot which needs builtin library setup.
260 Dart_Handle library = 411 Dart_Handle library =
261 LoadGenericSnapshotCreationScript(Builtin::kBuiltinLibrary); 412 LoadGenericSnapshotCreationScript(Builtin::kBuiltinLibrary);
262 VerifyLoaded(library); 413 VerifyLoaded(library);
263 library = LoadGenericSnapshotCreationScript(Builtin::kJsonLibrary);
264 VerifyLoaded(library);
265 library = LoadGenericSnapshotCreationScript(Builtin::kUriLibrary); 414 library = LoadGenericSnapshotCreationScript(Builtin::kUriLibrary);
266 VerifyLoaded(library); 415 VerifyLoaded(library);
267 library = LoadGenericSnapshotCreationScript(Builtin::kCryptoLibrary);
268 VerifyLoaded(library);
269 library = LoadGenericSnapshotCreationScript(Builtin::kIOLibrary);
270 VerifyLoaded(library);
271 library = LoadGenericSnapshotCreationScript(Builtin::kUtfLibrary); 416 library = LoadGenericSnapshotCreationScript(Builtin::kUtfLibrary);
272 VerifyLoaded(library); 417 VerifyLoaded(library);
273 } 418 }
274 419
275 420
421 static void SetupForGenericSnapshotCreation() {
422 SetupForUriResolution();
423
424 Dart_Handle library =
425 LoadGenericSnapshotCreationScript(Builtin::kJsonLibrary);
426 VerifyLoaded(library);
427 library = LoadGenericSnapshotCreationScript(Builtin::kCryptoLibrary);
428 VerifyLoaded(library);
429 library = LoadGenericSnapshotCreationScript(Builtin::kIOLibrary);
430 VerifyLoaded(library);
431 }
432
433
276 int main(int argc, char** argv) { 434 int main(int argc, char** argv) {
277 CommandLineOptions vm_options(argc); 435 CommandLineOptions vm_options(argc);
278 436
279 // Initialize the URL mapping array. 437 // Initialize the URL mapping array.
280 CommandLineOptions url_mapping_array(argc); 438 CommandLineOptions url_mapping_array(argc);
281 url_mapping = &url_mapping_array; 439 url_mapping = &url_mapping_array;
282 440
283 // Parse command line arguments. 441 // Parse command line arguments.
284 if (ParseArguments(argc, 442 if (ParseArguments(argc,
285 argv, 443 argv,
(...skipping 30 matching lines...) Expand all
316 474
317 Dart_Handle result; 475 Dart_Handle result;
318 Dart_Handle library; 476 Dart_Handle library;
319 Dart_EnterScope(); 477 Dart_EnterScope();
320 478
321 ASSERT(snapshot_filename != NULL); 479 ASSERT(snapshot_filename != NULL);
322 // Load up the script before a snapshot is created. 480 // Load up the script before a snapshot is created.
323 if (app_script_name != NULL) { 481 if (app_script_name != NULL) {
324 if (!script_snapshot) { 482 if (!script_snapshot) {
325 // This is the case of a custom embedder (e.g: dartium) trying to 483 // This is the case of a custom embedder (e.g: dartium) trying to
326 // create a full snapshot. Set up the library tag handler for this case 484 // create a full snapshot. The current isolate is set up so that we can
327 // in such a manner that it will use the URL mapping specified on the 485 // invoke the dart uri resolution code like _resolveURI. App script is
328 // command line to load the libraries. 486 // loaded into a separate isolate.
487
488 SetupForUriResolution();
489
490 // Get handle to builtin library.
491 Dart_Handle builtin_lib =
492 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary);
493 CHECK_RESULT(builtin_lib);
494
495 // Prepare for script loading by setting up the 'print' and 'timer'
496 // closures and setting up 'package root' for URI resolution.
497 result = DartUtils::PrepareForScriptLoading(package_root, builtin_lib);
498 CHECK_RESULT(result);
499 Dart_ExitScope();
500
501 UriResolverIsolateScope::isolate = isolate;
502
503 // Now we create an isolate into which we load all the code that needs to
504 // be in the snapshot.
505 if (Dart_CreateIsolate(NULL, NULL, NULL, NULL, &error) == NULL) {
506 fprintf(stderr, "%s", error);
507 free(error);
508 exit(255);
509 }
510 Dart_EnterScope();
511
512 // Set up the library tag handler in such a manner that it will use the
513 // URL mapping specified on the command line to load the libraries.
329 result = Dart_SetLibraryTagHandler(CreateSnapshotLibraryTagHandler); 514 result = Dart_SetLibraryTagHandler(CreateSnapshotLibraryTagHandler);
330 CHECK_RESULT(result); 515 CHECK_RESULT(result);
331 // Load the specified script. 516 // Load the specified script.
332 library = LoadSnapshotCreationScript(app_script_name); 517 library = LoadSnapshotCreationScript(app_script_name);
333 VerifyLoaded(library); 518 VerifyLoaded(library);
334 CreateAndWriteSnapshot(false); 519 CreateAndWriteSnapshot(false);
520
521 Dart_EnterIsolate(UriResolverIsolateScope::isolate);
522 Dart_ShutdownIsolate();
335 } else { 523 } else {
336 // This is the case where we want to create a script snapshot of 524 // This is the case where we want to create a script snapshot of
337 // the specified script. There will be no URL mapping specified for 525 // the specified script. There will be no URL mapping specified for
338 // this case, use the generic library tag handler. 526 // this case, use the generic library tag handler.
339 527
340 // First setup and create a generic full snapshot. 528 // First setup and create a generic full snapshot.
341 SetupForGenericSnapshotCreation(); 529 SetupForGenericSnapshotCreation();
342 uint8_t* buffer = NULL; 530 uint8_t* buffer = NULL;
343 intptr_t size = 0; 531 intptr_t size = 0;
344 result = Dart_CreateSnapshot(&buffer, &size); 532 result = Dart_CreateSnapshot(&buffer, &size);
345 CHECK_RESULT(result); 533 CHECK_RESULT(result);
346 534
347 // Save the snapshot buffer as we are about to shutdown the isolate. 535 // Save the snapshot buffer as we are about to shutdown the isolate.
348 snapshot_buffer = reinterpret_cast<uint8_t*>(malloc(size)); 536 snapshot_buffer = reinterpret_cast<uint8_t*>(malloc(size));
349 ASSERT(snapshot_buffer != NULL); 537 ASSERT(snapshot_buffer != NULL);
350 memmove(snapshot_buffer, buffer, size); 538 memmove(snapshot_buffer, buffer, size);
351 539
352 // Shutdown the isolate. 540 // Shutdown the isolate.
353 Dart_ExitScope(); 541 Dart_ExitScope();
354 Dart_ShutdownIsolate(); 542 Dart_ShutdownIsolate();
355 543
356 // Now load the specified script and create a script snapshot. 544 // Now load the specified script and create a script snapshot.
357 Dart_Isolate isolate = Dart_CreateIsolate(NULL, 545 if (Dart_CreateIsolate(
358 NULL, 546 NULL, NULL, snapshot_buffer, NULL, &error) == NULL) {
359 snapshot_buffer,
360 NULL,
361 &error);
362 if (isolate == NULL) {
363 Log::PrintErr("%s", error); 547 Log::PrintErr("%s", error);
364 free(error); 548 free(error);
365 free(snapshot_buffer); 549 free(snapshot_buffer);
366 exit(255); 550 exit(255);
367 } 551 }
368 Dart_EnterScope(); 552 Dart_EnterScope();
369 553
370 // Setup generic library tag handler. 554 // Setup generic library tag handler.
371 result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); 555 result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler);
372 CHECK_RESULT(result); 556 CHECK_RESULT(result);
(...skipping 15 matching lines...) Expand all
388 CreateAndWriteSnapshot(true); 572 CreateAndWriteSnapshot(true);
389 573
390 free(snapshot_buffer); 574 free(snapshot_buffer);
391 } 575 }
392 } else { 576 } else {
393 SetupForGenericSnapshotCreation(); 577 SetupForGenericSnapshotCreation();
394 CreateAndWriteSnapshot(false); 578 CreateAndWriteSnapshot(false);
395 } 579 }
396 return 0; 580 return 0;
397 } 581 }
OLDNEW
« no previous file with comments | « runtime/bin/dartutils.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698