| OLD | NEW |
| 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 #include <stdlib.h> | 5 #include <stdlib.h> |
| 6 #include <string.h> | 6 #include <string.h> |
| 7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 | 8 |
| 9 #include "include/dart_api.h" | 9 #include "include/dart_api.h" |
| 10 #include "include/dart_debugger_api.h" | 10 #include "include/dart_debugger_api.h" |
| 11 | 11 |
| 12 #include "bin/builtin.h" | 12 #include "bin/builtin.h" |
| 13 #include "bin/dartutils.h" | 13 #include "bin/dartutils.h" |
| 14 #include "bin/eventhandler.h" | 14 #include "bin/eventhandler.h" |
| 15 #include "bin/file.h" | 15 #include "bin/file.h" |
| 16 #include "bin/platform.h" | 16 #include "bin/platform.h" |
| 17 #include "bin/process.h" | 17 #include "bin/process.h" |
| 18 #include "platform/globals.h" | 18 #include "platform/globals.h" |
| 19 | 19 |
| 20 // snapshot_buffer points to a snapshot if we link in a snapshot otherwise | 20 // snapshot_buffer points to a snapshot if we link in a snapshot otherwise |
| 21 // it is initialized to NULL. | 21 // it is initialized to NULL. |
| 22 extern const uint8_t* snapshot_buffer; | 22 extern const uint8_t* snapshot_buffer; |
| 23 | 23 |
| 24 | 24 |
| 25 // Global state that stores a pointer to the application script file. | 25 // Global state that stores a pointer to the application script file. |
| 26 static char* canonical_script_name = NULL; | 26 static char* canonical_script_name = NULL; |
| 27 | 27 |
| 28 | 28 |
| 29 // Global state that stores the import URL map specified on the |
| 30 // command line. |
| 31 static CommandLineOptions* import_map_options = NULL; |
| 32 |
| 33 |
| 29 // Global state that stores a pointer to the application script snapshot. | 34 // Global state that stores a pointer to the application script snapshot. |
| 30 static bool use_script_snapshot = false; | 35 static bool use_script_snapshot = false; |
| 31 static uint8_t* script_snapshot_buffer = NULL; | 36 static uint8_t* script_snapshot_buffer = NULL; |
| 32 | 37 |
| 33 | 38 |
| 34 // Global state that indicates whether pprof symbol information is | 39 // Global state that indicates whether pprof symbol information is |
| 35 // to be generated or not. | 40 // to be generated or not. |
| 36 static const char* generate_pprof_symbols_filename = NULL; | 41 static const char* generate_pprof_symbols_filename = NULL; |
| 37 | 42 |
| 38 | 43 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 generate_pprof_symbols_filename = filename; | 78 generate_pprof_symbols_filename = filename; |
| 74 } | 79 } |
| 75 | 80 |
| 76 | 81 |
| 77 static void ProcessSnapshotOption(const char* snapshot) { | 82 static void ProcessSnapshotOption(const char* snapshot) { |
| 78 ASSERT(snapshot != NULL); | 83 ASSERT(snapshot != NULL); |
| 79 use_script_snapshot = true; | 84 use_script_snapshot = true; |
| 80 } | 85 } |
| 81 | 86 |
| 82 | 87 |
| 88 static void ProcessImportMapOption(const char* map) { |
| 89 ASSERT(map != NULL); |
| 90 import_map_options->AddArgument(map); |
| 91 } |
| 92 |
| 93 |
| 83 static struct { | 94 static struct { |
| 84 const char* option_name; | 95 const char* option_name; |
| 85 void (*process)(const char* option); | 96 void (*process)(const char* option); |
| 86 } main_options[] = { | 97 } main_options[] = { |
| 87 { "--break_at=", ProcessBreakpointOption }, | 98 { "--break_at=", ProcessBreakpointOption }, |
| 88 { "--compile_all", ProcessCompileAllOption }, | 99 { "--compile_all", ProcessCompileAllOption }, |
| 89 { "--generate_pprof_symbols=", ProcessPprofOption }, | 100 { "--generate_pprof_symbols=", ProcessPprofOption }, |
| 90 { "--use_script_snapshot", ProcessSnapshotOption }, | 101 { "--use_script_snapshot", ProcessSnapshotOption }, |
| 102 { "--import_map=", ProcessImportMapOption }, |
| 91 { NULL, NULL } | 103 { NULL, NULL } |
| 92 }; | 104 }; |
| 93 | 105 |
| 94 | 106 |
| 95 static bool ProcessMainOptions(const char* option) { | 107 static bool ProcessMainOptions(const char* option) { |
| 96 int i = 0; | 108 int i = 0; |
| 97 const char* name = main_options[0].option_name; | 109 const char* name = main_options[0].option_name; |
| 98 while (name != NULL) { | 110 while (name != NULL) { |
| 99 int length = strlen(name); | 111 int length = strlen(name); |
| 100 if (strncmp(option, name, length) == 0) { | 112 if (strncmp(option, name, length) == 0) { |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 pprof_file->WriteFully(buffer, buffer_size); | 236 pprof_file->WriteFully(buffer, buffer_size); |
| 225 } | 237 } |
| 226 delete pprof_file; // Closes the file. | 238 delete pprof_file; // Closes the file. |
| 227 Dart_ExitScope(); | 239 Dart_ExitScope(); |
| 228 } | 240 } |
| 229 } | 241 } |
| 230 | 242 |
| 231 | 243 |
| 232 static Dart_Handle LibraryTagHandler(Dart_LibraryTag tag, | 244 static Dart_Handle LibraryTagHandler(Dart_LibraryTag tag, |
| 233 Dart_Handle library, | 245 Dart_Handle library, |
| 234 Dart_Handle url) { | 246 Dart_Handle url, |
| 247 Dart_Handle import_map) { |
| 235 if (!Dart_IsLibrary(library)) { | 248 if (!Dart_IsLibrary(library)) { |
| 236 return Dart_Error("not a library"); | 249 return Dart_Error("not a library"); |
| 237 } | 250 } |
| 238 if (!Dart_IsString8(url)) { | 251 if (!Dart_IsString8(url)) { |
| 239 return Dart_Error("url is not a string"); | 252 return Dart_Error("url is not a string"); |
| 240 } | 253 } |
| 241 const char* url_string = NULL; | 254 const char* url_string = NULL; |
| 242 Dart_Handle result = Dart_StringToCString(url, &url_string); | 255 Dart_Handle result = Dart_StringToCString(url, &url_string); |
| 243 if (Dart_IsError(result)) { | 256 if (Dart_IsError(result)) { |
| 244 return Dart_Error("accessing url characters failed"); | 257 return Dart_Error("accessing url characters failed"); |
| 245 } | 258 } |
| 246 bool is_dart_scheme_url = DartUtils::IsDartSchemeURL(url_string); | 259 bool is_dart_scheme_url = DartUtils::IsDartSchemeURL(url_string); |
| 247 if (tag == kCanonicalizeUrl) { | 260 if (tag == kCanonicalizeUrl) { |
| 248 // If this is a Dart Scheme URL then it is not modified as it will be | 261 // If this is a Dart Scheme URL then it is not modified as it will be |
| 249 // handled by the VM internally. | 262 // handled by the VM internally. |
| 250 if (is_dart_scheme_url) { | 263 if (is_dart_scheme_url) { |
| 251 return url; | 264 return url; |
| 252 } | 265 } |
| 253 // Create a canonical path based on the including library and current url. | 266 // Create a canonical path based on the including library and current url. |
| 254 return DartUtils::CanonicalizeURL(NULL, library, url_string); | 267 return DartUtils::CanonicalizeURL(NULL, library, url_string); |
| 255 } | 268 } |
| 256 if (is_dart_scheme_url) { | 269 if (is_dart_scheme_url) { |
| 257 // Handle imports of dart:io. | 270 // Handle imports of dart:io. |
| 258 if (DartUtils::IsDartIOLibURL(url_string) && (tag == kImportTag)) { | 271 if (DartUtils::IsDartIOLibURL(url_string) && (tag == kImportTag)) { |
| 259 return Builtin::LoadLibrary(Builtin::kIOLibrary); | 272 return Builtin::LoadLibrary(Builtin::kIOLibrary); |
| 260 } else { | 273 } else { |
| 261 return Dart_Error("Do not know how to load '%s'", url_string); | 274 return Dart_Error("Do not know how to load '%s'", url_string); |
| 262 } | 275 } |
| 263 } | 276 } |
| 264 result = DartUtils::LoadSource(NULL, library, url, tag, url_string); | 277 result = DartUtils::LoadSource(NULL, |
| 278 library, |
| 279 url, |
| 280 tag, |
| 281 url_string, |
| 282 import_map); |
| 265 if (!Dart_IsError(result) && (tag == kImportTag)) { | 283 if (!Dart_IsError(result) && (tag == kImportTag)) { |
| 266 Builtin::ImportLibrary(result, Builtin::kBuiltinLibrary); | 284 Builtin::ImportLibrary(result, Builtin::kBuiltinLibrary); |
| 267 } | 285 } |
| 268 return result; | 286 return result; |
| 269 } | 287 } |
| 270 | 288 |
| 271 | 289 |
| 272 static Dart_Handle LoadScript(const char* script_name) { | 290 static Dart_Handle LoadScript(const char* script_name, |
| 291 CommandLineOptions* map) { |
| 273 Dart_Handle source = DartUtils::ReadStringFromFile(script_name); | 292 Dart_Handle source = DartUtils::ReadStringFromFile(script_name); |
| 274 if (Dart_IsError(source)) { | 293 if (Dart_IsError(source)) { |
| 275 return source; | 294 return source; |
| 276 } | 295 } |
| 277 Dart_Handle url = Dart_NewString(script_name); | 296 Dart_Handle url = Dart_NewString(script_name); |
| 278 | 297 intptr_t length = (map == NULL) ? 0 : map->count(); |
| 279 return Dart_LoadScript(url, source, LibraryTagHandler); | 298 Dart_Handle import_map = Dart_NewList(length * 2); |
| 299 for (intptr_t i = 0; i < length; i++) { |
| 300 ASSERT(map->GetArgument(i) != NULL); |
| 301 char* name = strdup(map->GetArgument(i)); |
| 302 ASSERT(name != NULL); |
| 303 char* map_name = strchr(name, ','); |
| 304 intptr_t index = i * 2; |
| 305 if (map_name != NULL) { |
| 306 *map_name = '\0'; |
| 307 map_name += 1; |
| 308 Dart_ListSetAt(import_map, index, Dart_NewString(name)); |
| 309 Dart_ListSetAt(import_map, (index + 1), Dart_NewString(map_name)); |
| 310 } else { |
| 311 Dart_ListSetAt(import_map, index, Dart_NewString(name)); |
| 312 Dart_ListSetAt(import_map, (index + 1), Dart_NewString("")); |
| 313 } |
| 314 free(name); |
| 315 } |
| 316 return Dart_LoadScript(url, source, LibraryTagHandler, import_map); |
| 280 } | 317 } |
| 281 | 318 |
| 282 | 319 |
| 283 static bool CreateIsolateAndSetup(const char* name_prefix, | 320 static bool CreateIsolateAndSetup(const char* name_prefix, |
| 284 void* data, char** error) { | 321 void* data, char** error) { |
| 285 Dart_Isolate isolate = | 322 Dart_Isolate isolate = |
| 286 Dart_CreateIsolate(name_prefix, snapshot_buffer, data, error); | 323 Dart_CreateIsolate(name_prefix, snapshot_buffer, data, error); |
| 287 if (isolate == NULL) { | 324 if (isolate == NULL) { |
| 288 return false; | 325 return false; |
| 289 } | 326 } |
| 290 | 327 |
| 291 Dart_Handle library; | 328 Dart_Handle library; |
| 292 Dart_EnterScope(); | 329 Dart_EnterScope(); |
| 293 | 330 |
| 294 // Load the specified application script into the newly created isolate. | 331 // Load the specified application script into the newly created isolate. |
| 295 if (script_snapshot_buffer != NULL) { | 332 if (script_snapshot_buffer != NULL) { |
| 296 library = Dart_LoadScriptFromSnapshot(script_snapshot_buffer); | 333 library = Dart_LoadScriptFromSnapshot(script_snapshot_buffer); |
| 297 } else { | 334 } else { |
| 298 library = LoadScript(canonical_script_name); | 335 library = LoadScript(canonical_script_name, import_map_options); |
| 299 } | 336 } |
| 300 if (Dart_IsError(library)) { | 337 if (Dart_IsError(library)) { |
| 301 *error = strdup(Dart_GetError(library)); | 338 *error = strdup(Dart_GetError(library)); |
| 302 Dart_ExitScope(); | 339 Dart_ExitScope(); |
| 303 Dart_ShutdownIsolate(); | 340 Dart_ShutdownIsolate(); |
| 304 return false; | 341 return false; |
| 305 } | 342 } |
| 306 if (!Dart_IsLibrary(library)) { | 343 if (!Dart_IsLibrary(library)) { |
| 307 char errbuf[256]; | 344 char errbuf[256]; |
| 308 snprintf(errbuf, sizeof(errbuf), | 345 snprintf(errbuf, sizeof(errbuf), |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 432 ASSERT(buffer != NULL); | 469 ASSERT(buffer != NULL); |
| 433 snprintf(buffer, len, kFormat, script_name, func_name); | 470 snprintf(buffer, len, kFormat, script_name, func_name); |
| 434 return buffer; | 471 return buffer; |
| 435 } | 472 } |
| 436 | 473 |
| 437 | 474 |
| 438 int main(int argc, char** argv) { | 475 int main(int argc, char** argv) { |
| 439 char* script_name; | 476 char* script_name; |
| 440 CommandLineOptions vm_options(argc); | 477 CommandLineOptions vm_options(argc); |
| 441 CommandLineOptions dart_options(argc); | 478 CommandLineOptions dart_options(argc); |
| 479 CommandLineOptions import_map(argc); |
| 480 import_map_options = &import_map; |
| 442 | 481 |
| 443 // Perform platform specific initialization. | 482 // Perform platform specific initialization. |
| 444 if (!Platform::Initialize()) { | 483 if (!Platform::Initialize()) { |
| 445 fprintf(stderr, "Initialization failed\n"); | 484 fprintf(stderr, "Initialization failed\n"); |
| 446 } | 485 } |
| 447 | 486 |
| 448 // Parse command line arguments. | 487 // Parse command line arguments. |
| 449 if (ParseArguments(argc, | 488 if (ParseArguments(argc, |
| 450 argv, | 489 argv, |
| 451 &vm_options, | 490 &vm_options, |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 571 DumpPprofSymbolInfo(); | 610 DumpPprofSymbolInfo(); |
| 572 // Shutdown the isolate. | 611 // Shutdown the isolate. |
| 573 Dart_ShutdownIsolate(); | 612 Dart_ShutdownIsolate(); |
| 574 // Terminate event handler. | 613 // Terminate event handler. |
| 575 EventHandler::Terminate(); | 614 EventHandler::Terminate(); |
| 576 // Terminate process exit-code handler. | 615 // Terminate process exit-code handler. |
| 577 Process::TerminateExitCodeHandler(); | 616 Process::TerminateExitCodeHandler(); |
| 578 | 617 |
| 579 return 0; | 618 return 0; |
| 580 } | 619 } |
| OLD | NEW |