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