| 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 <math.h> | 5 #include <math.h> |
| 6 #include <stdarg.h> | 6 #include <stdarg.h> |
| 7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 #include <stdlib.h> | 8 #include <stdlib.h> |
| 9 #include <string.h> | 9 #include <string.h> |
| 10 #include <sys/stat.h> | 10 #include <sys/stat.h> |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 extension_script = "gl.dart"; | 36 extension_script = "gl.dart"; |
| 37 } | 37 } |
| 38 size_t len = strlen(script_path) + strlen(main_script) + 2; | 38 size_t len = strlen(script_path) + strlen(main_script) + 2; |
| 39 main_script_ = new char[len]; | 39 main_script_ = new char[len]; |
| 40 snprintf(main_script_, len, "%s/%s", script_path, main_script); | 40 snprintf(main_script_, len, "%s/%s", script_path, main_script); |
| 41 len = strlen(script_path) + strlen(extension_script) + 2; | 41 len = strlen(script_path) + strlen(extension_script) + 2; |
| 42 extension_script_ = new char[len]; | 42 extension_script_ = new char[len]; |
| 43 snprintf(extension_script_, len, "%s/%s", script_path, extension_script); | 43 snprintf(extension_script_, len, "%s/%s", script_path, extension_script); |
| 44 } | 44 } |
| 45 | 45 |
| 46 int VMGlue::ErrorExit(const char* format, ...) { | |
| 47 va_list arguments; | |
| 48 va_start(arguments, format); | |
| 49 LOGE(format, arguments); | |
| 50 va_end(arguments); | |
| 51 Dart_ExitScope(); | |
| 52 Dart_ShutdownIsolate(); | |
| 53 LOGE("Shutdown isolate"); | |
| 54 return -1; | |
| 55 } | |
| 56 | |
| 57 Dart_Handle VMGlue::CheckError(Dart_Handle handle) { | 46 Dart_Handle VMGlue::CheckError(Dart_Handle handle) { |
| 58 if (Dart_IsError(handle)) { | 47 if (Dart_IsError(handle)) { |
| 59 LOGE("Unexpected Error Handle: %s", Dart_GetError(handle)); | 48 LOGE("Unexpected Error Handle: %s", Dart_GetError(handle)); |
| 60 Dart_PropagateError(handle); | 49 Dart_PropagateError(handle); |
| 61 } | 50 } |
| 62 return handle; | 51 return handle; |
| 63 } | 52 } |
| 64 | 53 |
| 65 #define CHECK_RESULT(result) \ | 54 #define CHECK_RESULT(result) \ |
| 66 if (Dart_IsError(result)) { \ | 55 if (Dart_IsError(result)) { \ |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 const char* main, | 111 const char* main, |
| 123 void* data, char** error) { | 112 void* data, char** error) { |
| 124 return CreateIsolateAndSetupHelper(script_uri, | 113 return CreateIsolateAndSetupHelper(script_uri, |
| 125 main, | 114 main, |
| 126 data, | 115 data, |
| 127 error); | 116 error); |
| 128 } | 117 } |
| 129 | 118 |
| 130 const char* VM_FLAGS[] = { | 119 const char* VM_FLAGS[] = { |
| 131 "--enable_type_checks", // TODO(gram): This should be an option! | 120 "--enable_type_checks", // TODO(gram): This should be an option! |
| 132 "--trace_isolates", | 121 // "--trace_isolates", |
| 133 "--trace_natives", | 122 // "--trace_natives", |
| 123 // "--trace_compiler", |
| 134 }; | 124 }; |
| 135 | 125 |
| 136 int VMGlue::InitializeVM() { | 126 int VMGlue::InitializeVM() { |
| 137 // We need the next call to get Dart_Initialize not to bail early. | 127 // We need the next call to get Dart_Initialize not to bail early. |
| 138 LOGI("Setting VM Options"); | 128 LOGI("Setting VM Options"); |
| 139 Dart_SetVMFlags(sizeof(VM_FLAGS) / sizeof(VM_FLAGS[0]), VM_FLAGS); | 129 Dart_SetVMFlags(sizeof(VM_FLAGS) / sizeof(VM_FLAGS[0]), VM_FLAGS); |
| 140 | 130 |
| 141 // Initialize the Dart VM, providing the callbacks to use for | 131 // Initialize the Dart VM, providing the callbacks to use for |
| 142 // creating and shutting down isolates. | 132 // creating and shutting down isolates. |
| 143 LOGI("Initializing Dart"); | 133 LOGI("Initializing Dart"); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 CheckError(Dart_LoadScript(url, source)); | 194 CheckError(Dart_LoadScript(url, source)); |
| 205 | 195 |
| 206 Dart_ExitScope(); | 196 Dart_ExitScope(); |
| 207 Dart_ExitIsolate(); | 197 Dart_ExitIsolate(); |
| 208 return 0; | 198 return 0; |
| 209 } | 199 } |
| 210 | 200 |
| 211 int VMGlue::CallSetup() { | 201 int VMGlue::CallSetup() { |
| 212 if (!initialized_script_) { | 202 if (!initialized_script_) { |
| 213 initialized_script_ = true; | 203 initialized_script_ = true; |
| 214 LOGI("Invoking setup(0,0,%d,%d)", surface_->width(), surface_->height()); | 204 LOGI("Invoking setup(NULL, %d,%d)", surface_->width(), surface_->height()); |
| 215 Dart_EnterIsolate(isolate_); | 205 Dart_EnterIsolate(isolate_); |
| 216 Dart_EnterScope(); | 206 Dart_EnterScope(); |
| 217 Dart_Handle args[2]; | 207 Dart_Handle args[3]; |
| 218 args[0] = CheckError(Dart_NewInteger(surface_->width())); | 208 args[0] = CheckError(Dart_Null()); |
| 219 args[1] = CheckError(Dart_NewInteger(surface_->height())); | 209 args[1] = CheckError(Dart_NewInteger(surface_->width())); |
| 220 int rtn = Invoke("setup", 2, args); | 210 args[2] = CheckError(Dart_NewInteger(surface_->height())); |
| 211 int rtn = Invoke("setup", 3, args); |
| 221 | 212 |
| 222 if (rtn == 0) { | 213 if (rtn == 0) { |
| 223 // Plug in the print handler. It would be nice if we could do this | 214 // Plug in the print handler. It would be nice if we could do this |
| 224 // before calling setup, but the call to GetField blows up if we | 215 // before calling setup, but the call to GetField blows up if we |
| 225 // haven't run anything yet. | 216 // haven't run anything yet. |
| 226 Dart_Handle library = CheckError(Dart_LookupLibrary( | 217 Dart_Handle library = CheckError(Dart_LookupLibrary( |
| 227 Dart_NewStringFromCString("gl.dart"))); | 218 Dart_NewStringFromCString("gl.dart"))); |
| 228 Dart_Handle print = CheckError( | 219 Dart_Handle print = CheckError( |
| 229 Dart_GetField(library, Dart_NewStringFromCString("_printClosure"))); | 220 Dart_GetField(library, Dart_NewStringFromCString("_printClosure"))); |
| 230 Dart_Handle corelib = CheckError(Dart_LookupLibrary( | 221 Dart_Handle corelib = CheckError(Dart_LookupLibrary( |
| 231 Dart_NewStringFromCString("dart:core"))); | 222 Dart_NewStringFromCString("dart:core"))); |
| 232 CheckError(Dart_SetField(corelib, | 223 CheckError(Dart_SetField(corelib, |
| 233 Dart_NewStringFromCString("_printClosure"), print)); | 224 Dart_NewStringFromCString("_printClosure"), print)); |
| 234 } | 225 } |
| 235 | |
| 236 Dart_ExitScope(); | 226 Dart_ExitScope(); |
| 237 Dart_ExitIsolate(); | 227 Dart_ExitIsolate(); |
| 238 LOGI("Done setup"); | 228 LOGI("Done setup"); |
| 239 return rtn; | 229 return rtn; |
| 240 } | 230 } |
| 241 return 0; | 231 return 0; |
| 242 } | 232 } |
| 243 | 233 |
| 244 int VMGlue::CallUpdate() { | 234 int VMGlue::CallUpdate() { |
| 245 if (initialized_script_) { | 235 if (initialized_script_) { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 int VMGlue::Invoke(const char* function, | 288 int VMGlue::Invoke(const char* function, |
| 299 int argc, | 289 int argc, |
| 300 Dart_Handle* args, | 290 Dart_Handle* args, |
| 301 bool failIfNotDefined) { | 291 bool failIfNotDefined) { |
| 302 LOGI("in invoke(%s)", function); | 292 LOGI("in invoke(%s)", function); |
| 303 | 293 |
| 304 // Lookup the library of the root script. | 294 // Lookup the library of the root script. |
| 305 LOGI("looking up the root library"); | 295 LOGI("looking up the root library"); |
| 306 Dart_Handle library = Dart_RootLibrary(); | 296 Dart_Handle library = Dart_RootLibrary(); |
| 307 if (Dart_IsNull(library)) { | 297 if (Dart_IsNull(library)) { |
| 308 return ErrorExit("Unable to find root library\n"); | 298 LOGE("Unable to find root library\n"); |
| 299 return -1; |
| 309 } | 300 } |
| 310 | 301 |
| 311 Dart_Handle nameHandle = Dart_NewStringFromCString(function); | 302 Dart_Handle nameHandle = Dart_NewStringFromCString(function); |
| 312 | 303 |
| 313 LOGI("invoking %s", function); | 304 LOGI("invoking %s", function); |
| 314 Dart_Handle result = Dart_Invoke(library, nameHandle, argc, args); | 305 Dart_Handle result = Dart_Invoke(library, nameHandle, argc, args); |
| 315 | 306 |
| 316 if (Dart_IsError(result)) { | 307 if (Dart_IsError(result)) { |
| 308 const char* error = Dart_GetError(result); |
| 309 LOGE("Invoke %s failed: %s", function, error); |
| 317 if (failIfNotDefined) { | 310 if (failIfNotDefined) { |
| 318 return ErrorExit("Invoke %s: %s\n", function, Dart_GetError(result)); | 311 return -1; |
| 319 } else { | |
| 320 LOGE("Invoke %s: %s", function, Dart_GetError(result)); | |
| 321 } | 312 } |
| 322 } | 313 } |
| 323 | 314 |
| 324 // TODO(vsm): I don't think we need this. | 315 // TODO(vsm): I don't think we need this. |
| 325 // Keep handling messages until the last active receive port is closed. | 316 // Keep handling messages until the last active receive port is closed. |
| 326 LOGI("Entering Dart message loop"); | 317 LOGI("Entering Dart message loop"); |
| 327 result = Dart_RunLoop(); | 318 result = Dart_RunLoop(); |
| 328 if (Dart_IsError(result)) { | 319 if (Dart_IsError(result)) { |
| 329 return ErrorExit("Dart_RunLoop: %s\n", Dart_GetError(result)); | 320 LOGE("Dart_RunLoop: %s\n", Dart_GetError(result)); |
| 321 return -1; |
| 330 } | 322 } |
| 331 | 323 |
| 332 LOGI("out invoke"); | 324 LOGI("out invoke"); |
| 333 return 0; | 325 return 0; |
| 334 } | 326 } |
| 335 | 327 |
| 336 void VMGlue::FinishMainIsolate() { | 328 void VMGlue::FinishMainIsolate() { |
| 337 LOGI("Finish main isolate"); | 329 LOGI("Finish main isolate"); |
| 338 Dart_EnterIsolate(isolate_); | 330 Dart_EnterIsolate(isolate_); |
| 339 // Shutdown the isolate. | 331 // Shutdown the isolate. |
| 340 Dart_ShutdownIsolate(); | 332 Dart_ShutdownIsolate(); |
| 341 isolate_ = NULL; | 333 isolate_ = NULL; |
| 342 initialized_script_ = false; | 334 initialized_script_ = false; |
| 343 } | 335 } |
| 344 | 336 |
| OLD | NEW |