| 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 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 i += 1; | 86 i += 1; |
| 87 } | 87 } |
| 88 | 88 |
| 89 return 0; | 89 return 0; |
| 90 } | 90 } |
| 91 | 91 |
| 92 | 92 |
| 93 static Dart_Handle SetupRuntimeOptions(CommandLineOptions* options) { | 93 static Dart_Handle SetupRuntimeOptions(CommandLineOptions* options) { |
| 94 int options_count = options->count(); | 94 int options_count = options->count(); |
| 95 Dart_Handle dart_arguments = Dart_NewList(options_count); | 95 Dart_Handle dart_arguments = Dart_NewList(options_count); |
| 96 if (!Dart_IsValid(dart_arguments)) { | 96 if (Dart_IsError(dart_arguments)) { |
| 97 return dart_arguments; | 97 return dart_arguments; |
| 98 } | 98 } |
| 99 for (int i = 0; i < options_count; i++) { | 99 for (int i = 0; i < options_count; i++) { |
| 100 Dart_Handle argument_value = Dart_NewString(options->GetArgument(i)); | 100 Dart_Handle argument_value = Dart_NewString(options->GetArgument(i)); |
| 101 if (!Dart_IsValid(argument_value)) { | 101 if (Dart_IsError(argument_value)) { |
| 102 return argument_value; | 102 return argument_value; |
| 103 } | 103 } |
| 104 Dart_Handle result = Dart_ListSetAt(dart_arguments, i, argument_value); | 104 Dart_Handle result = Dart_ListSetAt(dart_arguments, i, argument_value); |
| 105 if (!Dart_IsValid(result)) { | 105 if (Dart_IsError(result)) { |
| 106 return result; | 106 return result; |
| 107 } | 107 } |
| 108 } | 108 } |
| 109 Dart_Handle core_lib_url = Dart_NewString("dart:coreimpl"); | 109 Dart_Handle core_lib_url = Dart_NewString("dart:coreimpl"); |
| 110 if (!Dart_IsValid(core_lib_url)) { | 110 if (Dart_IsError(core_lib_url)) { |
| 111 return core_lib_url; | 111 return core_lib_url; |
| 112 } | 112 } |
| 113 Dart_Handle core_lib = Dart_LookupLibrary(core_lib_url); | 113 Dart_Handle core_lib = Dart_LookupLibrary(core_lib_url); |
| 114 if (!Dart_IsValid(core_lib)) { | 114 if (Dart_IsError(core_lib)) { |
| 115 return core_lib; | 115 return core_lib; |
| 116 } | 116 } |
| 117 Dart_Handle runtime_options_class_name = Dart_NewString("RuntimeOptions"); | 117 Dart_Handle runtime_options_class_name = Dart_NewString("RuntimeOptions"); |
| 118 if (!Dart_IsValid(runtime_options_class_name)) { | 118 if (Dart_IsError(runtime_options_class_name)) { |
| 119 return runtime_options_class_name; | 119 return runtime_options_class_name; |
| 120 } | 120 } |
| 121 Dart_Handle runtime_options_class = Dart_GetClass( | 121 Dart_Handle runtime_options_class = Dart_GetClass( |
| 122 core_lib, runtime_options_class_name); | 122 core_lib, runtime_options_class_name); |
| 123 if (!Dart_IsValid(runtime_options_class)) { | 123 if (Dart_IsError(runtime_options_class)) { |
| 124 return runtime_options_class; | 124 return runtime_options_class; |
| 125 } | 125 } |
| 126 Dart_Handle native_name = Dart_NewString("_nativeArguments"); | 126 Dart_Handle native_name = Dart_NewString("_nativeArguments"); |
| 127 if (!Dart_IsValid(native_name)) { | 127 if (Dart_IsError(native_name)) { |
| 128 return native_name; | 128 return native_name; |
| 129 } | 129 } |
| 130 | 130 |
| 131 return Dart_SetStaticField(runtime_options_class, | 131 return Dart_SetStaticField(runtime_options_class, |
| 132 native_name, | 132 native_name, |
| 133 dart_arguments); | 133 dart_arguments); |
| 134 } | 134 } |
| 135 | 135 |
| 136 | 136 |
| 137 static void DumpPprofSymbolInfo() { | 137 static void DumpPprofSymbolInfo() { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 152 } | 152 } |
| 153 | 153 |
| 154 | 154 |
| 155 static void* MainIsolateInitCallback(void* data) { | 155 static void* MainIsolateInitCallback(void* data) { |
| 156 const char* script_name = reinterpret_cast<const char*>(data); | 156 const char* script_name = reinterpret_cast<const char*>(data); |
| 157 Dart_Handle library; | 157 Dart_Handle library; |
| 158 Dart_EnterScope(); | 158 Dart_EnterScope(); |
| 159 | 159 |
| 160 // Load the specified script. | 160 // Load the specified script. |
| 161 library = LoadScript(script_name); | 161 library = LoadScript(script_name); |
| 162 if (!Dart_IsValid(library)) { | 162 if (Dart_IsError(library)) { |
| 163 const char* err_msg = Dart_GetError(library); | 163 const char* err_msg = Dart_GetError(library); |
| 164 fprintf(stderr, "Errors encountered while loading script: %s\n", err_msg); | 164 fprintf(stderr, "Errors encountered while loading script: %s\n", err_msg); |
| 165 Dart_ExitScope(); | 165 Dart_ExitScope(); |
| 166 exit(255); | 166 exit(255); |
| 167 } | 167 } |
| 168 | 168 |
| 169 if (!Dart_IsLibrary(library)) { | 169 if (!Dart_IsLibrary(library)) { |
| 170 fprintf(stderr, | 170 fprintf(stderr, |
| 171 "Expected a library when loading script: %s", | 171 "Expected a library when loading script: %s", |
| 172 script_name); | 172 script_name); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 191 static bool HasCompileAll(const CommandLineOptions& options) { | 191 static bool HasCompileAll(const CommandLineOptions& options) { |
| 192 for (int i = 0; i < options.count(); i++) { | 192 for (int i = 0; i < options.count(); i++) { |
| 193 if (strcmp(options.GetArgument(i), "--compile_all") == 0) { | 193 if (strcmp(options.GetArgument(i), "--compile_all") == 0) { |
| 194 return true; | 194 return true; |
| 195 } | 195 } |
| 196 } | 196 } |
| 197 return false; | 197 return false; |
| 198 } | 198 } |
| 199 | 199 |
| 200 | 200 |
| 201 static void PrintObject(FILE* out, Dart_Handle object) { | |
| 202 Dart_Handle result = Dart_ToString(object); | |
| 203 if (Dart_IsValid(result)) { | |
| 204 PrintString(out, result); | |
| 205 } else { | |
| 206 fprintf(out, "%s\n", Dart_GetError(result)); | |
| 207 } | |
| 208 } | |
| 209 | |
| 210 | |
| 211 int main(int argc, char** argv) { | 201 int main(int argc, char** argv) { |
| 212 char* script_name; | 202 char* script_name; |
| 213 CommandLineOptions vm_options(argc); | 203 CommandLineOptions vm_options(argc); |
| 214 CommandLineOptions dart_options(argc); | 204 CommandLineOptions dart_options(argc); |
| 215 | 205 |
| 216 // Parse command line arguments. | 206 // Parse command line arguments. |
| 217 if (ParseArguments(argc, | 207 if (ParseArguments(argc, |
| 218 argv, | 208 argv, |
| 219 &vm_options, | 209 &vm_options, |
| 220 &script_name, | 210 &script_name, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 238 Dart_Isolate isolate = Dart_CreateIsolate(snapshot_buffer, | 228 Dart_Isolate isolate = Dart_CreateIsolate(snapshot_buffer, |
| 239 canonical_script_name); | 229 canonical_script_name); |
| 240 if (isolate == NULL) { | 230 if (isolate == NULL) { |
| 241 free(canonical_script_name); | 231 free(canonical_script_name); |
| 242 return 255; | 232 return 255; |
| 243 } | 233 } |
| 244 | 234 |
| 245 Dart_EnterScope(); | 235 Dart_EnterScope(); |
| 246 if (HasCompileAll(vm_options)) { | 236 if (HasCompileAll(vm_options)) { |
| 247 Dart_Handle result = Dart_CompileAll(); | 237 Dart_Handle result = Dart_CompileAll(); |
| 248 if (!Dart_IsValid(result)) { | 238 if (Dart_IsError(result)) { |
| 249 fprintf(stderr, "%s\n", Dart_GetError(result)); | 239 fprintf(stderr, "%s\n", Dart_GetError(result)); |
| 250 Dart_ExitScope(); | 240 Dart_ExitScope(); |
| 251 Dart_ShutdownIsolate(); | 241 Dart_ShutdownIsolate(); |
| 252 free(canonical_script_name); | 242 free(canonical_script_name); |
| 253 return 255; // Indicates we encountered an error. | 243 return 255; // Indicates we encountered an error. |
| 254 } | 244 } |
| 255 } | 245 } |
| 256 | 246 |
| 257 // Create a dart options object that can be accessed from dart code. | 247 // Create a dart options object that can be accessed from dart code. |
| 258 Dart_Handle options_result = SetupRuntimeOptions(&dart_options); | 248 Dart_Handle options_result = SetupRuntimeOptions(&dart_options); |
| 259 if (!Dart_IsValid(options_result)) { | 249 if (Dart_IsError(options_result)) { |
| 260 fprintf(stderr, "%s\n", Dart_GetError(options_result)); | 250 fprintf(stderr, "%s\n", Dart_GetError(options_result)); |
| 261 Dart_ExitScope(); | 251 Dart_ExitScope(); |
| 262 Dart_ShutdownIsolate(); | 252 Dart_ShutdownIsolate(); |
| 263 free(canonical_script_name); | 253 free(canonical_script_name); |
| 264 return 255; // Indicates we encountered an error. | 254 return 255; // Indicates we encountered an error. |
| 265 } | 255 } |
| 266 | 256 |
| 267 // Lookup and invoke the top level main function. | 257 // Lookup and invoke the top level main function. |
| 268 Dart_Handle script_url = Dart_NewString(canonical_script_name); | 258 Dart_Handle script_url = Dart_NewString(canonical_script_name); |
| 269 Dart_Handle library = Dart_LookupLibrary(script_url); | 259 Dart_Handle library = Dart_LookupLibrary(script_url); |
| 270 if (!Dart_IsValid(library)) { | 260 if (Dart_IsError(library)) { |
| 271 fprintf(stderr, "%s\n", Dart_GetError(library)); | 261 fprintf(stderr, "%s\n", Dart_GetError(library)); |
| 272 Dart_ExitScope(); | 262 Dart_ExitScope(); |
| 273 Dart_ShutdownIsolate(); | 263 Dart_ShutdownIsolate(); |
| 274 free(canonical_script_name); | 264 free(canonical_script_name); |
| 275 return 255; // Indicates we encountered an error. | 265 return 255; // Indicates we encountered an error. |
| 276 } | 266 } |
| 277 Dart_Handle result = Dart_InvokeStatic(library, | 267 Dart_Handle result = Dart_InvokeStatic(library, |
| 278 Dart_NewString(""), | 268 Dart_NewString(""), |
| 279 Dart_NewString("main"), | 269 Dart_NewString("main"), |
| 280 0, | 270 0, |
| 281 NULL); | 271 NULL); |
| 282 if (Dart_IsValid(result)) { | 272 if (Dart_IsError(result)) { |
| 283 if (Dart_ExceptionOccurred(result)) { | |
| 284 // Print the exception object. | |
| 285 fprintf(stderr, "An unhandled exception has been thrown\n"); | |
| 286 Dart_Handle exception_result = Dart_GetException(result); | |
| 287 ASSERT(Dart_IsValid(exception_result)); | |
| 288 PrintObject(stderr, exception_result); | |
| 289 // Print the stack trace. | |
| 290 Dart_Handle stacktrace = Dart_GetStacktrace(result); | |
| 291 ASSERT(Dart_IsValid(stacktrace)); | |
| 292 PrintObject(stderr, stacktrace); | |
| 293 fprintf(stderr, "\n"); | |
| 294 Dart_ExitScope(); | |
| 295 Dart_ShutdownIsolate(); | |
| 296 free(canonical_script_name); | |
| 297 return 255; // We had an unhandled exception, hence indicate an error. | |
| 298 } | |
| 299 } else { | |
| 300 fprintf(stderr, "%s\n", Dart_GetError(result)); | 273 fprintf(stderr, "%s\n", Dart_GetError(result)); |
| 301 Dart_ExitScope(); | 274 Dart_ExitScope(); |
| 302 Dart_ShutdownIsolate(); | 275 Dart_ShutdownIsolate(); |
| 303 free(canonical_script_name); | 276 free(canonical_script_name); |
| 304 return 255; // Indicates we encountered an error. | 277 return 255; // Indicates we encountered an error. |
| 305 } | 278 } |
| 306 // Keep handling messages until the last active receive port is closed. | 279 // Keep handling messages until the last active receive port is closed. |
| 307 result = Dart_RunLoop(); | 280 result = Dart_RunLoop(); |
| 308 if (!Dart_IsValid(result)) { | 281 if (Dart_IsError(result)) { |
| 309 fprintf(stderr, "%s\n", Dart_GetError(result)); | 282 fprintf(stderr, "%s\n", Dart_GetError(result)); |
| 310 Dart_ExitScope(); | 283 Dart_ExitScope(); |
| 311 Dart_ShutdownIsolate(); | 284 Dart_ShutdownIsolate(); |
| 312 free(canonical_script_name); | 285 free(canonical_script_name); |
| 313 return 255; // Indicates we encountered an error. | 286 return 255; // Indicates we encountered an error. |
| 314 } | 287 } |
| 315 free(canonical_script_name); | 288 free(canonical_script_name); |
| 316 Dart_ExitScope(); | 289 Dart_ExitScope(); |
| 317 // Dump symbol information for the profiler. | 290 // Dump symbol information for the profiler. |
| 318 DumpPprofSymbolInfo(); | 291 DumpPprofSymbolInfo(); |
| 319 // Shutdown the isolate. | 292 // Shutdown the isolate. |
| 320 Dart_ShutdownIsolate(); | 293 Dart_ShutdownIsolate(); |
| 321 return 0; | 294 return 0; |
| 322 } | 295 } |
| OLD | NEW |