| OLD | NEW |
| 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 #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 | 10 |
| 11 #include "bin/builtin.h" | 11 #include "bin/builtin.h" |
| 12 #include "bin/dartutils.h" | 12 #include "bin/dartutils.h" |
| 13 #include "bin/file.h" | 13 #include "bin/file.h" |
| 14 #include "bin/globals.h" | 14 #include "bin/globals.h" |
| 15 #include "bin/platform.h" | 15 #include "bin/platform.h" |
| 16 | 16 |
| 17 // snapshot_buffer points to a snapshot if we link in a snapshot otherwise | 17 // snapshot_buffer points to a snapshot if we link in a snapshot otherwise |
| 18 // it is initialized to NULL. | 18 // it is initialized to NULL. |
| 19 extern const uint8_t* snapshot_buffer; | 19 extern const uint8_t* snapshot_buffer; |
| 20 | 20 |
| 21 | 21 |
| 22 // Global state that stores a pointer to the application script file. |
| 23 static char* canonical_script_name = NULL; |
| 24 |
| 25 |
| 22 // Global state that indicates whether pprof symbol information is | 26 // Global state that indicates whether pprof symbol information is |
| 23 // to be generated or not. | 27 // to be generated or not. |
| 24 static const char* generate_pprof_symbols_filename = NULL; | 28 static const char* generate_pprof_symbols_filename = NULL; |
| 25 | 29 |
| 26 | 30 |
| 27 static bool IsValidFlag(const char* name, | 31 static bool IsValidFlag(const char* name, |
| 28 const char* prefix, | 32 const char* prefix, |
| 29 intptr_t prefix_length) { | 33 intptr_t prefix_length) { |
| 30 intptr_t name_length = strlen(name); | 34 intptr_t name_length = strlen(name); |
| 31 return ((name_length > prefix_length) && | 35 return ((name_length > prefix_length) && |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 return url; | 184 return url; |
| 181 } | 185 } |
| 182 // Create a canonical path based on the including library and current url. | 186 // Create a canonical path based on the including library and current url. |
| 183 return DartUtils::CanonicalizeURL(NULL, library, url_string); | 187 return DartUtils::CanonicalizeURL(NULL, library, url_string); |
| 184 } | 188 } |
| 185 if (is_dart_scheme_url) { | 189 if (is_dart_scheme_url) { |
| 186 return Dart_Error("Do not know how to load '%s'", url_string); | 190 return Dart_Error("Do not know how to load '%s'", url_string); |
| 187 } | 191 } |
| 188 result = DartUtils::LoadSource(NULL, library, url, tag, url_string); | 192 result = DartUtils::LoadSource(NULL, library, url, tag, url_string); |
| 189 if (!Dart_IsError(result) && (tag == kImportTag)) { | 193 if (!Dart_IsError(result) && (tag == kImportTag)) { |
| 190 Builtin_ImportLibrary(result); | 194 Builtin::ImportLibrary(result); |
| 191 } | 195 } |
| 192 return result; | 196 return result; |
| 193 } | 197 } |
| 194 | 198 |
| 195 | 199 |
| 196 static Dart_Handle LoadScript(const char* script_name) { | 200 static Dart_Handle LoadScript(const char* script_name) { |
| 197 Dart_Handle source = DartUtils::ReadStringFromFile(script_name); | 201 Dart_Handle source = DartUtils::ReadStringFromFile(script_name); |
| 198 if (Dart_IsError(source)) { | 202 if (Dart_IsError(source)) { |
| 199 return source; | 203 return source; |
| 200 } | 204 } |
| 201 Dart_Handle url = Dart_NewString(script_name); | 205 Dart_Handle url = Dart_NewString(script_name); |
| 202 | 206 |
| 203 return Dart_LoadScript(url, source, LibraryTagHandler); | 207 return Dart_LoadScript(url, source, LibraryTagHandler); |
| 204 } | 208 } |
| 205 | 209 |
| 206 | 210 |
| 207 static void* MainIsolateInitCallback(void* data) { | 211 static bool CreateIsolateAndSetup(void* data, char** error) { |
| 208 const char* script_name = reinterpret_cast<const char*>(data); | 212 Dart_Isolate isolate = Dart_CreateIsolate(snapshot_buffer, data, error); |
| 213 if (isolate == NULL) { |
| 214 return false; |
| 215 } |
| 216 |
| 209 Dart_Handle library; | 217 Dart_Handle library; |
| 210 Dart_EnterScope(); | 218 Dart_EnterScope(); |
| 211 | 219 |
| 212 // Load the specified script. | 220 // Load the specified application script into the newly created isolate. |
| 213 library = LoadScript(script_name); | 221 library = LoadScript(canonical_script_name); |
| 214 if (Dart_IsError(library)) { | 222 if (Dart_IsError(library)) { |
| 215 const char* err_msg = Dart_GetError(library); | 223 *error = strdup(Dart_GetError(library)); |
| 216 fprintf(stderr, "Errors encountered while loading script: %s\n", err_msg); | |
| 217 Dart_ExitScope(); | 224 Dart_ExitScope(); |
| 218 exit(255); | 225 Dart_ShutdownIsolate(); |
| 226 return false; |
| 219 } | 227 } |
| 220 if (!Dart_IsLibrary(library)) { | 228 if (!Dart_IsLibrary(library)) { |
| 221 fprintf(stderr, | 229 char errbuf[256]; |
| 222 "Expected a library when loading script: %s", | 230 snprintf(errbuf, sizeof(errbuf), |
| 223 script_name); | 231 "Expected a library when loading script: %s", |
| 232 canonical_script_name); |
| 233 *error = strdup(errbuf); |
| 224 Dart_ExitScope(); | 234 Dart_ExitScope(); |
| 225 exit(255); | 235 Dart_ShutdownIsolate(); |
| 236 return false; |
| 226 } | 237 } |
| 227 Builtin_ImportLibrary(library); // Import builtin library. | 238 Builtin::ImportLibrary(library); // Implicitly import builtin into app. |
| 228 | 239 if (snapshot_buffer != NULL) { |
| 240 // Setup the native resolver as the snapshot does not carry it. |
| 241 Builtin::SetNativeResolver(); |
| 242 } |
| 229 Dart_ExitScope(); | 243 Dart_ExitScope(); |
| 230 return data; | 244 return true; |
| 231 } | 245 } |
| 232 | 246 |
| 233 | 247 |
| 234 static void PrintUsage() { | 248 static void PrintUsage() { |
| 235 fprintf(stderr, | 249 fprintf(stderr, |
| 236 "dart [<vm-flags>] <dart-script-file> [<dart-options>]\n"); | 250 "dart [<vm-flags>] <dart-script-file> [<dart-options>]\n"); |
| 237 } | 251 } |
| 238 | 252 |
| 239 | 253 |
| 240 static bool HasCompileAll(const CommandLineOptions& options) { | 254 static bool HasCompileAll(const CommandLineOptions& options) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 260 // Parse command line arguments. | 274 // Parse command line arguments. |
| 261 if (ParseArguments(argc, | 275 if (ParseArguments(argc, |
| 262 argv, | 276 argv, |
| 263 &vm_options, | 277 &vm_options, |
| 264 &script_name, | 278 &script_name, |
| 265 &dart_options) < 0) { | 279 &dart_options) < 0) { |
| 266 PrintUsage(); | 280 PrintUsage(); |
| 267 return 255; | 281 return 255; |
| 268 } | 282 } |
| 269 | 283 |
| 270 // Initialize the Dart VM (TODO(asiva) - remove const_cast once | 284 // Initialize the Dart VM. |
| 271 // dart API is fixed to take a const char** in Dart_Initialize). | |
| 272 Dart_Initialize(vm_options.count(), | 285 Dart_Initialize(vm_options.count(), |
| 273 vm_options.arguments(), | 286 vm_options.arguments(), |
| 274 MainIsolateInitCallback); | 287 CreateIsolateAndSetup); |
| 275 | 288 |
| 276 // Create an isolate. As a side effect, MainIsolateInitCallback | 289 canonical_script_name = File::GetCanonicalPath(script_name); |
| 277 // gets called, which loads the scripts and libraries. | |
| 278 char* canonical_script_name = File::GetCanonicalPath(script_name); | |
| 279 if (canonical_script_name == NULL) { | 290 if (canonical_script_name == NULL) { |
| 280 fprintf(stderr, "Unable to find '%s'\n", script_name); | 291 fprintf(stderr, "Unable to find '%s'\n", script_name); |
| 281 return 255; // Indicates we encountered an error. | 292 return 255; // Indicates we encountered an error. |
| 282 } | 293 } |
| 283 Dart_Isolate isolate = Dart_CreateIsolate(snapshot_buffer, | 294 |
| 284 canonical_script_name); | 295 // Call CreateIsolateAndSetup which creates an isolate and loads up |
| 285 if (isolate == NULL) { | 296 // the specified application script. |
| 297 char* error = NULL; |
| 298 if (!CreateIsolateAndSetup(NULL, &error)) { |
| 299 fprintf(stderr, "%s\n", error); |
| 286 free(canonical_script_name); | 300 free(canonical_script_name); |
| 287 return 255; | 301 free(error); |
| 302 return 255; // Indicates we encountered an error. |
| 288 } | 303 } |
| 289 | 304 |
| 305 Dart_Isolate isolate = Dart_CurrentIsolate(); |
| 306 ASSERT(isolate != NULL); |
| 307 Dart_Handle result; |
| 308 |
| 290 Dart_EnterScope(); | 309 Dart_EnterScope(); |
| 291 | 310 |
| 292 if (snapshot_buffer != NULL) { | |
| 293 // Setup the native resolver as the snapshot does not carry it. | |
| 294 Builtin_SetNativeResolver(); | |
| 295 } | |
| 296 | |
| 297 if (HasCompileAll(vm_options)) { | 311 if (HasCompileAll(vm_options)) { |
| 298 Dart_Handle result = Dart_CompileAll(); | 312 result = Dart_CompileAll(); |
| 299 if (Dart_IsError(result)) { | 313 if (Dart_IsError(result)) { |
| 300 fprintf(stderr, "%s\n", Dart_GetError(result)); | 314 fprintf(stderr, "%s\n", Dart_GetError(result)); |
| 301 Dart_ExitScope(); | 315 Dart_ExitScope(); |
| 302 Dart_ShutdownIsolate(); | 316 Dart_ShutdownIsolate(); |
| 303 free(canonical_script_name); | 317 free(canonical_script_name); |
| 304 return 255; // Indicates we encountered an error. | 318 return 255; // Indicates we encountered an error. |
| 305 } | 319 } |
| 306 } | 320 } |
| 307 | 321 |
| 308 // Create a dart options object that can be accessed from dart code. | 322 // Create a dart options object that can be accessed from dart code. |
| 309 Dart_Handle options_result = SetupRuntimeOptions(&dart_options); | 323 Dart_Handle options_result = SetupRuntimeOptions(&dart_options); |
| 310 if (Dart_IsError(options_result)) { | 324 if (Dart_IsError(options_result)) { |
| 311 fprintf(stderr, "%s\n", Dart_GetError(options_result)); | 325 fprintf(stderr, "%s\n", Dart_GetError(options_result)); |
| 312 Dart_ExitScope(); | 326 Dart_ExitScope(); |
| 313 Dart_ShutdownIsolate(); | 327 Dart_ShutdownIsolate(); |
| 314 free(canonical_script_name); | 328 free(canonical_script_name); |
| 315 return 255; // Indicates we encountered an error. | 329 return 255; // Indicates we encountered an error. |
| 316 } | 330 } |
| 317 | 331 |
| 318 // Lookup and invoke the top level main function. | 332 // Lookup and invoke the top level main function. |
| 319 Dart_Handle script_url = Dart_NewString(canonical_script_name); | 333 Dart_Handle script_url = Dart_NewString(canonical_script_name); |
| 320 Dart_Handle library = Dart_LookupLibrary(script_url); | 334 Dart_Handle library = Dart_LookupLibrary(script_url); |
| 321 if (Dart_IsError(library)) { | 335 if (Dart_IsError(library)) { |
| 322 fprintf(stderr, "%s\n", Dart_GetError(library)); | 336 fprintf(stderr, "%s\n", Dart_GetError(library)); |
| 323 Dart_ExitScope(); | 337 Dart_ExitScope(); |
| 324 Dart_ShutdownIsolate(); | 338 Dart_ShutdownIsolate(); |
| 325 free(canonical_script_name); | 339 free(canonical_script_name); |
| 326 return 255; // Indicates we encountered an error. | 340 return 255; // Indicates we encountered an error. |
| 327 } | 341 } |
| 328 Dart_Handle result = Dart_InvokeStatic(library, | 342 result = Dart_InvokeStatic(library, |
| 329 Dart_NewString(""), | 343 Dart_NewString(""), |
| 330 Dart_NewString("main"), | 344 Dart_NewString("main"), |
| 331 0, | 345 0, |
| 332 NULL); | 346 NULL); |
| 333 if (Dart_IsError(result)) { | 347 if (Dart_IsError(result)) { |
| 334 fprintf(stderr, "%s\n", Dart_GetError(result)); | 348 fprintf(stderr, "%s\n", Dart_GetError(result)); |
| 335 Dart_ExitScope(); | 349 Dart_ExitScope(); |
| 336 Dart_ShutdownIsolate(); | 350 Dart_ShutdownIsolate(); |
| 337 free(canonical_script_name); | 351 free(canonical_script_name); |
| 338 return 255; // Indicates we encountered an error. | 352 return 255; // Indicates we encountered an error. |
| 339 } | 353 } |
| 340 // Keep handling messages until the last active receive port is closed. | 354 // Keep handling messages until the last active receive port is closed. |
| 341 result = Dart_RunLoop(); | 355 result = Dart_RunLoop(); |
| 342 if (Dart_IsError(result)) { | 356 if (Dart_IsError(result)) { |
| 343 fprintf(stderr, "%s\n", Dart_GetError(result)); | 357 fprintf(stderr, "%s\n", Dart_GetError(result)); |
| 344 Dart_ExitScope(); | 358 Dart_ExitScope(); |
| 345 Dart_ShutdownIsolate(); | 359 Dart_ShutdownIsolate(); |
| 346 free(canonical_script_name); | 360 free(canonical_script_name); |
| 347 return 255; // Indicates we encountered an error. | 361 return 255; // Indicates we encountered an error. |
| 348 } | 362 } |
| 349 free(canonical_script_name); | 363 free(canonical_script_name); |
| 350 Dart_ExitScope(); | 364 Dart_ExitScope(); |
| 351 // Dump symbol information for the profiler. | 365 // Dump symbol information for the profiler. |
| 352 DumpPprofSymbolInfo(); | 366 DumpPprofSymbolInfo(); |
| 353 // Shutdown the isolate. | 367 // Shutdown the isolate. |
| 354 Dart_ShutdownIsolate(); | 368 Dart_ShutdownIsolate(); |
| 355 return 0; | 369 return 0; |
| 356 } | 370 } |
| OLD | NEW |