| 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 <stdio.h> | 6 #include <stdio.h> |
| 7 #include <unistd.h> | 7 #include <unistd.h> |
| 8 #include <sys/stat.h> | 8 #include <sys/stat.h> |
| 9 | 9 |
| 10 #include "include/dart_api.h" | 10 #include "include/dart_api.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 LOGE(format, arguments); | 29 LOGE(format, arguments); |
| 30 va_end(arguments); | 30 va_end(arguments); |
| 31 Dart_ExitScope(); | 31 Dart_ExitScope(); |
| 32 Dart_ShutdownIsolate(); | 32 Dart_ShutdownIsolate(); |
| 33 LOGE("Shutdown isolate"); | 33 LOGE("Shutdown isolate"); |
| 34 return -1; | 34 return -1; |
| 35 } | 35 } |
| 36 | 36 |
| 37 Dart_Handle VMGlue::CheckError(Dart_Handle handle) { | 37 Dart_Handle VMGlue::CheckError(Dart_Handle handle) { |
| 38 if (Dart_IsError(handle)) { | 38 if (Dart_IsError(handle)) { |
| 39 LOGE("Unexpected Error Handle"); | 39 LOGE("Unexpected Error Handle: %s", Dart_GetError(handle)); |
| 40 Dart_PropagateError(handle); | 40 Dart_PropagateError(handle); |
| 41 } | 41 } |
| 42 return handle; | 42 return handle; |
| 43 } | 43 } |
| 44 | 44 |
| 45 #define CHECK_RESULT(result) \ | 45 #define CHECK_RESULT(result) \ |
| 46 if (Dart_IsError(result)) { \ | 46 if (Dart_IsError(result)) { \ |
| 47 *error = strdup(Dart_GetError(result)); \ | 47 *error = strdup(Dart_GetError(result)); \ |
| 48 LOGE(*error); \ | 48 LOGE(*error); \ |
| 49 Dart_ExitScope(); \ | 49 Dart_ExitScope(); \ |
| 50 Dart_ShutdownIsolate(); \ | 50 Dart_ShutdownIsolate(); \ |
| 51 return false; \ | 51 return false; \ |
| 52 } | 52 } |
| 53 | 53 |
| 54 Dart_Handle VMGlue::LibraryTagHandler(Dart_LibraryTag tag, | 54 Dart_Handle VMGlue::LibraryTagHandler(Dart_LibraryTag tag, |
| 55 Dart_Handle library, | 55 Dart_Handle library, |
| 56 Dart_Handle urlHandle) { | 56 Dart_Handle urlHandle) { |
| 57 const char* url; | 57 const char* url; |
| 58 Dart_StringToCString(urlHandle, &url); | 58 Dart_StringToCString(urlHandle, &url); |
| 59 if (tag == kCanonicalizeUrl) { | 59 if (tag == kCanonicalizeUrl) { |
| 60 return urlHandle; | 60 return urlHandle; |
| 61 } | 61 } |
| 62 // TODO(vsm): Split this up into separate libraries for 3D, 2D, | 62 // TODO(vsm): Split this up into separate libraries for 3D, 2D, |
| 63 // Touch, Audio, etc. All builtin libraries should be handled here | 63 // Touch, Audio, etc. All builtin libraries should be handled here |
| 64 // (or moved into a snapshot). | 64 // (or moved into a snapshot). |
| 65 if (strcmp(url, "android_extension.dart") == 0) { | 65 if (strcmp(url, "android_extension.dart") == 0) { |
| 66 Dart_Handle source = | 66 Dart_Handle source = VMGlue::LoadSourceFromFile( |
| 67 VMGlue::LoadSourceFromFile("/data/data/com.google.dartndk/app_dart/andro
id_extension.dart"); | 67 "/data/data/com.google.dartndk/app_dart/android_extension.dart"); |
| 68 Dart_Handle library = CheckError(Dart_LoadLibrary(urlHandle, source)); | 68 Dart_Handle library = CheckError(Dart_LoadLibrary(urlHandle, source)); |
| 69 CheckError(Dart_SetNativeResolver(library, ResolveName)); | 69 CheckError(Dart_SetNativeResolver(library, ResolveName)); |
| 70 return library; | 70 return library; |
| 71 } | 71 } |
| 72 LOGE("UNIMPLEMENTED: load library %s\n", url); | 72 LOGE("UNIMPLEMENTED: load library %s\n", url); |
| 73 return NULL; | 73 return NULL; |
| 74 } | 74 } |
| 75 | 75 |
| 76 // Returns true on success, false on failure. | 76 // Returns true on success, false on failure. |
| 77 bool VMGlue::CreateIsolateAndSetupHelper(const char* script_uri, | 77 bool VMGlue::CreateIsolateAndSetupHelper(const char* script_uri, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 104 return CreateIsolateAndSetupHelper(script_uri, | 104 return CreateIsolateAndSetupHelper(script_uri, |
| 105 main, | 105 main, |
| 106 data, | 106 data, |
| 107 error); | 107 error); |
| 108 } | 108 } |
| 109 | 109 |
| 110 #define VMHOSTNAME "android_dart_host" | 110 #define VMHOSTNAME "android_dart_host" |
| 111 #define MAINSCRIPT "/data/data/com.google.dartndk/app_dart/main.dart" | 111 #define MAINSCRIPT "/data/data/com.google.dartndk/app_dart/main.dart" |
| 112 | 112 |
| 113 const char* VM_FLAGS[] = { | 113 const char* VM_FLAGS[] = { |
| 114 "--enable_type_checks", | 114 "--enable_type_checks", // TODO(gram): This should be an option! |
| 115 "--trace_isolates", | 115 "--trace_isolates", |
| 116 "--trace_natives", | 116 "--trace_natives", |
| 117 }; | 117 }; |
| 118 | 118 |
| 119 int VMGlue::InitializeVM() { | 119 int VMGlue::InitializeVM() { |
| 120 // We need the next call to get Dart_Initialize not to bail early. | 120 // We need the next call to get Dart_Initialize not to bail early. |
| 121 LOGI("Setting VM Options"); | 121 LOGI("Setting VM Options"); |
| 122 Dart_SetVMFlags(sizeof(VM_FLAGS) / sizeof(VM_FLAGS[0]), VM_FLAGS); | 122 Dart_SetVMFlags(sizeof(VM_FLAGS) / sizeof(VM_FLAGS[0]), VM_FLAGS); |
| 123 | 123 |
| 124 // Initialize the Dart VM, providing the callbacks to use for | 124 // Initialize the Dart VM, providing the callbacks to use for |
| (...skipping 20 matching lines...) Expand all Loading... |
| 145 LOGE("Main script not found at: %s\n", url); | 145 LOGE("Main script not found at: %s\n", url); |
| 146 return NULL; | 146 return NULL; |
| 147 } | 147 } |
| 148 | 148 |
| 149 struct stat sb; | 149 struct stat sb; |
| 150 int fd = fileno(file); | 150 int fd = fileno(file); |
| 151 fstat(fd, &sb); | 151 fstat(fd, &sb); |
| 152 int length = sb.st_size; | 152 int length = sb.st_size; |
| 153 LOGI("Entry file %s is %d bytes.\n", url, length); | 153 LOGI("Entry file %s is %d bytes.\n", url, length); |
| 154 | 154 |
| 155 char* buffer = reinterpret_cast<char *>(malloc((length + 1) * sizeof(char))); | 155 char* buffer = new char[length+1]; |
| 156 if (read(fd, buffer, length) < 0) { | 156 if (read(fd, buffer, length) < 0) { |
| 157 LOGE("Could not read script %s.\n", url); | 157 LOGE("Could not read script %s.\n", url); |
| 158 return NULL; | 158 return NULL; |
| 159 } | 159 } |
| 160 buffer[length] = 0; | 160 buffer[length] = 0; |
| 161 fclose(file); | 161 fclose(file); |
| 162 | 162 |
| 163 Dart_Handle contents = CheckError(Dart_NewStringFromCString(buffer)); | 163 Dart_Handle contents = CheckError(Dart_NewStringFromCString(buffer)); |
| 164 free(buffer); | 164 delete[] buffer; |
| 165 return contents; | 165 return contents; |
| 166 } | 166 } |
| 167 | 167 |
| 168 int VMGlue::StartMainIsolate() { | 168 int VMGlue::StartMainIsolate() { |
| 169 if (!initialized_vm_) { | 169 if (!initialized_vm_) { |
| 170 int rtn = InitializeVM(); | 170 int rtn = InitializeVM(); |
| 171 if (rtn != 0) return rtn; | 171 if (rtn != 0) return rtn; |
| 172 } | 172 } |
| 173 | 173 |
| 174 // Create an isolate and loads up the application script. | 174 // Create an isolate and loads up the application script. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 187 CheckError(Dart_LoadScript(url, source)); | 187 CheckError(Dart_LoadScript(url, source)); |
| 188 | 188 |
| 189 Dart_ExitScope(); | 189 Dart_ExitScope(); |
| 190 Dart_ExitIsolate(); | 190 Dart_ExitIsolate(); |
| 191 return 0; | 191 return 0; |
| 192 } | 192 } |
| 193 | 193 |
| 194 int VMGlue::CallSetup() { | 194 int VMGlue::CallSetup() { |
| 195 if (!initialized_script_) { | 195 if (!initialized_script_) { |
| 196 initialized_script_ = true; | 196 initialized_script_ = true; |
| 197 LOGI("Invoking setup"); | 197 LOGI("Invoking setup(0,0,%d,%d)", graphics_->width(), graphics_->height()); |
| 198 Dart_EnterIsolate(isolate_); | 198 Dart_EnterIsolate(isolate_); |
| 199 Dart_EnterScope(); | 199 Dart_EnterScope(); |
| 200 Dart_Handle args[2]; | 200 Dart_Handle args[2]; |
| 201 args[0] = CheckError(Dart_NewInteger(graphics_->width())); | 201 args[0] = CheckError(Dart_NewInteger(graphics_->width())); |
| 202 args[1] = CheckError(Dart_NewInteger(graphics_->height())); | 202 args[1] = CheckError(Dart_NewInteger(graphics_->height())); |
| 203 int rtn = Invoke("setup", 2, args); | 203 int rtn = Invoke("setup", 2, args); |
| 204 Dart_ExitScope(); | 204 Dart_ExitScope(); |
| 205 Dart_ExitIsolate(); | 205 Dart_ExitIsolate(); |
| 206 LOGI("Done setup"); | 206 LOGI("Done setup"); |
| 207 return rtn; | 207 return rtn; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 226 int VMGlue::OnMotionEvent(const char* pFunction, int64_t pWhen, | 226 int VMGlue::OnMotionEvent(const char* pFunction, int64_t pWhen, |
| 227 float pMoveX, float pMoveY) { | 227 float pMoveX, float pMoveY) { |
| 228 if (initialized_script_) { | 228 if (initialized_script_) { |
| 229 LOGI("Invoking %s", pFunction); | 229 LOGI("Invoking %s", pFunction); |
| 230 Dart_EnterIsolate(isolate_); | 230 Dart_EnterIsolate(isolate_); |
| 231 Dart_EnterScope(); | 231 Dart_EnterScope(); |
| 232 Dart_Handle args[3]; | 232 Dart_Handle args[3]; |
| 233 args[0] = CheckError(Dart_NewInteger(pWhen)); | 233 args[0] = CheckError(Dart_NewInteger(pWhen)); |
| 234 args[1] = CheckError(Dart_NewDouble(pMoveX)); | 234 args[1] = CheckError(Dart_NewDouble(pMoveX)); |
| 235 args[2] = CheckError(Dart_NewDouble(pMoveY)); | 235 args[2] = CheckError(Dart_NewDouble(pMoveY)); |
| 236 int rtn = Invoke(pFunction, 3, args); | 236 int rtn = Invoke(pFunction, 3, args, false); |
| 237 Dart_ExitScope(); | 237 Dart_ExitScope(); |
| 238 Dart_ExitIsolate(); | 238 Dart_ExitIsolate(); |
| 239 LOGI("Done %s", pFunction); | 239 LOGI("Done %s", pFunction); |
| 240 return rtn; | 240 return rtn; |
| 241 } | 241 } |
| 242 return -1; | 242 return -1; |
| 243 } | 243 } |
| 244 | 244 |
| 245 int VMGlue::OnKeyEvent(const char* function, int64_t when, int32_t flags, | 245 int VMGlue::OnKeyEvent(const char* function, int64_t when, int32_t flags, |
| 246 int32_t key_code, int32_t meta_state, int32_t repeat) { | 246 int32_t key_code, int32_t meta_state, int32_t repeat) { |
| 247 if (initialized_script_) { | 247 if (initialized_script_) { |
| 248 LOGI("Invoking %s", function); | 248 LOGI("Invoking %s", function); |
| 249 Dart_EnterIsolate(isolate_); | 249 Dart_EnterIsolate(isolate_); |
| 250 Dart_EnterScope(); | 250 Dart_EnterScope(); |
| 251 Dart_Handle args[5]; | 251 Dart_Handle args[5]; |
| 252 args[0] = CheckError(Dart_NewInteger(when)); | 252 args[0] = CheckError(Dart_NewInteger(when)); |
| 253 args[1] = CheckError(Dart_NewInteger(flags)); | 253 args[1] = CheckError(Dart_NewInteger(flags)); |
| 254 args[2] = CheckError(Dart_NewInteger(key_code)); | 254 args[2] = CheckError(Dart_NewInteger(key_code)); |
| 255 args[3] = CheckError(Dart_NewInteger(meta_state)); | 255 args[3] = CheckError(Dart_NewInteger(meta_state)); |
| 256 args[4] = CheckError(Dart_NewInteger(repeat)); | 256 args[4] = CheckError(Dart_NewInteger(repeat)); |
| 257 int rtn = Invoke(function, 5, args); | 257 int rtn = Invoke(function, 5, args, false); |
| 258 Dart_ExitScope(); | 258 Dart_ExitScope(); |
| 259 Dart_ExitIsolate(); | 259 Dart_ExitIsolate(); |
| 260 LOGI("Done %s", function); | 260 LOGI("Done %s", function); |
| 261 return rtn; | 261 return rtn; |
| 262 } | 262 } |
| 263 return -1; | 263 return -1; |
| 264 } | 264 } |
| 265 | 265 |
| 266 int VMGlue::Invoke(const char* function, int argc, Dart_Handle* args) { | 266 int VMGlue::Invoke(const char* function, |
| 267 Dart_Handle result; | 267 int argc, |
| 268 | 268 Dart_Handle* args, |
| 269 bool failIfNotDefined) { |
| 269 LOGI("in invoke(%s)", function); | 270 LOGI("in invoke(%s)", function); |
| 270 | 271 |
| 271 // Lookup the library of the root script. | 272 // Lookup the library of the root script. |
| 272 LOGI("looking up the root library"); | 273 LOGI("looking up the root library"); |
| 273 Dart_Handle library = Dart_RootLibrary(); | 274 Dart_Handle library = Dart_RootLibrary(); |
| 274 if (Dart_IsNull(library)) { | 275 if (Dart_IsNull(library)) { |
| 275 return ErrorExit("Unable to find root library\n"); | 276 return ErrorExit("Unable to find root library\n"); |
| 276 } | 277 } |
| 277 | 278 |
| 278 // Lookup and invoke the appropriate function. | 279 Dart_Handle nameHandle = Dart_NewStringFromCString(function); |
| 280 |
| 279 LOGI("invoking %s", function); | 281 LOGI("invoking %s", function); |
| 280 result = | 282 Dart_Handle result = Dart_Invoke(library, nameHandle, argc, args); |
| 281 Dart_Invoke(library, Dart_NewStringFromCString(function), argc, args); | |
| 282 | 283 |
| 283 if (Dart_IsError(result)) { | 284 if (Dart_IsError(result)) { |
| 284 return ErrorExit("%s\n", Dart_GetError(result)); | 285 if (failIfNotDefined) { |
| 286 return ErrorExit("Invoke %s: %s\n", function, Dart_GetError(result)); |
| 287 } else { |
| 288 LOGE("Invoke %s: %s", function, Dart_GetError(result)); |
| 289 } |
| 285 } | 290 } |
| 286 | 291 |
| 287 // TODO(vsm): I don't think we need this. | 292 // TODO(vsm): I don't think we need this. |
| 288 // Keep handling messages until the last active receive port is closed. | 293 // Keep handling messages until the last active receive port is closed. |
| 289 LOGI("Entering Dart message loop"); | 294 LOGI("Entering Dart message loop"); |
| 290 result = Dart_RunLoop(); | 295 result = Dart_RunLoop(); |
| 291 if (Dart_IsError(result)) { | 296 if (Dart_IsError(result)) { |
| 292 return ErrorExit("%s\n", Dart_GetError(result)); | 297 return ErrorExit("Dart_RunLoop: %s\n", Dart_GetError(result)); |
| 293 } | 298 } |
| 294 | 299 |
| 295 LOGI("out invoke"); | 300 LOGI("out invoke"); |
| 296 return 0; | 301 return 0; |
| 297 } | 302 } |
| 298 | 303 |
| 299 void VMGlue::FinishMainIsolate() { | 304 void VMGlue::FinishMainIsolate() { |
| 300 LOGI("Finish main isolate"); | 305 LOGI("Finish main isolate"); |
| 301 Dart_EnterIsolate(isolate_); | 306 Dart_EnterIsolate(isolate_); |
| 302 // Shutdown the isolate. | 307 // Shutdown the isolate. |
| 303 Dart_ShutdownIsolate(); | 308 Dart_ShutdownIsolate(); |
| 304 isolate_ = NULL; | 309 isolate_ = NULL; |
| 305 initialized_script_ = false; | 310 initialized_script_ = false; |
| 306 } | 311 } |
| OLD | NEW |