| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 #include <math.h> | |
| 6 #include <stdio.h> | |
| 7 #include <unistd.h> | |
| 8 #include <sys/stat.h> | |
| 9 | |
| 10 #include "include/dart_api.h" | |
| 11 #include "jni/android_extension.h" | |
| 12 #include "jni/log.h" | |
| 13 #include "jni/vm_glue.h" | |
| 14 | |
| 15 // snapshot_buffer points to a snapshot if we link in a snapshot otherwise | |
| 16 // it is initialized to NULL. | |
| 17 | |
| 18 VMGlue::VMGlue(Graphics* graphics) | |
| 19 : graphics_(graphics), | |
| 20 isolate_(NULL), | |
| 21 initialized_vm_(false), | |
| 22 initialized_script_(false) { | |
| 23 LOGI("Creating VMGlue"); | |
| 24 } | |
| 25 | |
| 26 int VMGlue::ErrorExit(const char* format, ...) { | |
| 27 va_list arguments; | |
| 28 va_start(arguments, format); | |
| 29 LOGE(format, arguments); | |
| 30 va_end(arguments); | |
| 31 Dart_ExitScope(); | |
| 32 Dart_ShutdownIsolate(); | |
| 33 LOGE("Shutdown isolate"); | |
| 34 return -1; | |
| 35 } | |
| 36 | |
| 37 Dart_Handle VMGlue::CheckError(Dart_Handle handle) { | |
| 38 if (Dart_IsError(handle)) { | |
| 39 LOGE("Unexpected Error Handle"); | |
| 40 Dart_PropagateError(handle); | |
| 41 } | |
| 42 return handle; | |
| 43 } | |
| 44 | |
| 45 #define CHECK_RESULT(result) \ | |
| 46 if (Dart_IsError(result)) { \ | |
| 47 *error = strdup(Dart_GetError(result)); \ | |
| 48 LOGE(*error); \ | |
| 49 Dart_ExitScope(); \ | |
| 50 Dart_ShutdownIsolate(); \ | |
| 51 return false; \ | |
| 52 } | |
| 53 | |
| 54 Dart_Handle VMGlue::LibraryTagHandler(Dart_LibraryTag tag, | |
| 55 Dart_Handle library, | |
| 56 Dart_Handle urlHandle) { | |
| 57 const char* url; | |
| 58 Dart_StringToCString(urlHandle, &url); | |
| 59 if (tag == kCanonicalizeUrl) { | |
| 60 return urlHandle; | |
| 61 } | |
| 62 // TODO(vsm): Split this up into separate libraries for 3D, 2D, | |
| 63 // Touch, Audio, etc. All builtin libraries should be handled here | |
| 64 // (or moved into a snapshot). | |
| 65 if (strcmp(url, "android_extension.dart") == 0) { | |
| 66 Dart_Handle source = VMGlue::LoadSourceFromFile("/data/data/com.google.dartn
dk/app_dart/android_extension.dart"); | |
| 67 Dart_Handle library = CheckError(Dart_LoadLibrary(urlHandle, source)); | |
| 68 CheckError(Dart_SetNativeResolver(library, ResolveName)); | |
| 69 return library; | |
| 70 } | |
| 71 LOGE("UNIMPLEMENTED: load library %s\n", url); | |
| 72 return NULL; | |
| 73 } | |
| 74 | |
| 75 // Returns true on success, false on failure. | |
| 76 bool VMGlue::CreateIsolateAndSetupHelper(const char* script_uri, | |
| 77 const char* main, | |
| 78 void* data, | |
| 79 char** error) { | |
| 80 LOGI("Creating isolate %s, %s", script_uri, main); | |
| 81 Dart_Isolate isolate = | |
| 82 Dart_CreateIsolate(script_uri, main, NULL, data, error); | |
| 83 if (isolate == NULL) { | |
| 84 LOGE("Couldn't create isolate: %s", error); | |
| 85 return false; | |
| 86 } | |
| 87 | |
| 88 LOGI("Entering scope"); | |
| 89 Dart_EnterScope(); | |
| 90 | |
| 91 // Set up the library tag handler for this isolate. | |
| 92 LOGI("Setting up library tag handler"); | |
| 93 Dart_Handle result = CheckError(Dart_SetLibraryTagHandler(LibraryTagHandler)); | |
| 94 CHECK_RESULT(result); | |
| 95 | |
| 96 Dart_ExitScope(); | |
| 97 return true; | |
| 98 } | |
| 99 | |
| 100 bool VMGlue::CreateIsolateAndSetup(const char* script_uri, | |
| 101 const char* main, | |
| 102 void* data, char** error) { | |
| 103 return CreateIsolateAndSetupHelper(script_uri, | |
| 104 main, | |
| 105 data, | |
| 106 error); | |
| 107 } | |
| 108 | |
| 109 #define VMHOSTNAME "android_dart_host" | |
| 110 #define MAINSCRIPT "/data/data/com.google.dartndk/app_dart/main.dart" | |
| 111 | |
| 112 const char* VM_FLAGS[] = { | |
| 113 "--enable_type_checks", | |
| 114 "--trace_isolates", | |
| 115 "--trace_natives", | |
| 116 }; | |
| 117 | |
| 118 int VMGlue::InitializeVM() { | |
| 119 // We need the next call to get Dart_Initialize not to bail early. | |
| 120 LOGI("Setting VM Options"); | |
| 121 Dart_SetVMFlags(sizeof(VM_FLAGS) / sizeof(VM_FLAGS[0]), VM_FLAGS); | |
| 122 | |
| 123 // Initialize the Dart VM, providing the callbacks to use for | |
| 124 // creating and shutting down isolates. | |
| 125 LOGI("Initializing Dart"); | |
| 126 if (!Dart_Initialize(CreateIsolateAndSetup, | |
| 127 NULL, | |
| 128 NULL, | |
| 129 NULL, | |
| 130 NULL, | |
| 131 NULL, | |
| 132 NULL)) { | |
| 133 LOGE("VM initialization failed\n"); | |
| 134 return -1; | |
| 135 } | |
| 136 initialized_vm_ = true; | |
| 137 | |
| 138 return 0; | |
| 139 } | |
| 140 | |
| 141 Dart_Handle VMGlue::LoadSourceFromFile(const char* url) { | |
| 142 FILE* file = fopen(url, "r"); | |
| 143 if (file == NULL) { | |
| 144 LOGE("Main script not found at: %s\n", url); | |
| 145 return NULL; | |
| 146 } | |
| 147 | |
| 148 struct stat sb; | |
| 149 int fd = fileno(file); | |
| 150 fstat(fd, &sb); | |
| 151 int length = sb.st_size; | |
| 152 LOGI("Entry file %s is %d bytes.\n", url, length); | |
| 153 | |
| 154 char* buffer = reinterpret_cast<char *>(malloc((length + 1) * sizeof(char))); | |
| 155 if (read(fd, buffer, length) < 0) { | |
| 156 LOGE("Could not read script %s.\n", url); | |
| 157 return NULL; | |
| 158 } | |
| 159 buffer[length] = 0; | |
| 160 fclose(file); | |
| 161 | |
| 162 Dart_Handle contents = CheckError(Dart_NewStringFromCString(buffer)); | |
| 163 free(buffer); | |
| 164 return contents; | |
| 165 } | |
| 166 | |
| 167 int VMGlue::StartMainIsolate() { | |
| 168 if (!initialized_vm_) { | |
| 169 int rtn = InitializeVM(); | |
| 170 if (rtn != 0) return rtn; | |
| 171 } | |
| 172 | |
| 173 // Create an isolate and loads up the application script. | |
| 174 char* error = NULL; | |
| 175 if (!CreateIsolateAndSetup(MAINSCRIPT, "main", NULL, &error)) { | |
| 176 LOGE("CreateIsolateAndSetup: %s\n", error); | |
| 177 free(error); | |
| 178 return -1; | |
| 179 } | |
| 180 LOGI("Created isolate"); | |
| 181 isolate_ = Dart_CurrentIsolate(); | |
| 182 Dart_EnterScope(); | |
| 183 | |
| 184 Dart_Handle url = CheckError(Dart_NewStringFromCString(MAINSCRIPT)); | |
| 185 Dart_Handle source = LoadSourceFromFile(MAINSCRIPT); | |
| 186 CheckError(Dart_LoadScript(url, source)); | |
| 187 | |
| 188 Dart_ExitScope(); | |
| 189 Dart_ExitIsolate(); | |
| 190 return 0; | |
| 191 } | |
| 192 | |
| 193 int VMGlue::CallSetup() { | |
| 194 if (!initialized_script_) { | |
| 195 initialized_script_ = true; | |
| 196 LOGI("Invoking setup"); | |
| 197 Dart_EnterIsolate(isolate_); | |
| 198 Dart_EnterScope(); | |
| 199 Dart_Handle args[2]; | |
| 200 args[0] = CheckError(Dart_NewInteger(graphics_->width())); | |
| 201 args[1] = CheckError(Dart_NewInteger(graphics_->height())); | |
| 202 int rtn = Invoke("setup", 2, args); | |
| 203 Dart_ExitScope(); | |
| 204 Dart_ExitIsolate(); | |
| 205 LOGI("Done setup"); | |
| 206 return rtn; | |
| 207 } | |
| 208 return 0; | |
| 209 } | |
| 210 | |
| 211 int VMGlue::CallUpdate() { | |
| 212 if (initialized_script_) { | |
| 213 LOGI("Invoking update"); | |
| 214 Dart_EnterIsolate(isolate_); | |
| 215 Dart_EnterScope(); | |
| 216 int rtn = Invoke("update", 0, 0); | |
| 217 Dart_ExitScope(); | |
| 218 Dart_ExitIsolate(); | |
| 219 LOGI("Done update"); | |
| 220 return rtn; | |
| 221 } | |
| 222 return -1; | |
| 223 } | |
| 224 | |
| 225 int VMGlue::OnMotionEvent(const char* pFunction, int64_t pWhen, | |
| 226 float pMoveX, float pMoveY) { | |
| 227 if (initialized_script_) { | |
| 228 LOGI("Invoking %s", pFunction); | |
| 229 Dart_EnterIsolate(isolate_); | |
| 230 Dart_EnterScope(); | |
| 231 Dart_Handle args[3]; | |
| 232 args[0] = CheckError(Dart_NewInteger(pWhen)); | |
| 233 args[1] = CheckError(Dart_NewDouble(pMoveX)); | |
| 234 args[2] = CheckError(Dart_NewDouble(pMoveY)); | |
| 235 int rtn = Invoke(pFunction, 3, args); | |
| 236 Dart_ExitScope(); | |
| 237 Dart_ExitIsolate(); | |
| 238 LOGI("Done %s", pFunction); | |
| 239 return rtn; | |
| 240 } | |
| 241 return -1; | |
| 242 } | |
| 243 | |
| 244 int VMGlue::OnKeyEvent(const char* function, int64_t when, int32_t flags, | |
| 245 int32_t key_code, int32_t meta_state, int32_t repeat) { | |
| 246 if (initialized_script_) { | |
| 247 LOGI("Invoking %s", function); | |
| 248 Dart_EnterIsolate(isolate_); | |
| 249 Dart_EnterScope(); | |
| 250 Dart_Handle args[5]; | |
| 251 args[0] = CheckError(Dart_NewInteger(when)); | |
| 252 args[1] = CheckError(Dart_NewInteger(flags)); | |
| 253 args[2] = CheckError(Dart_NewInteger(key_code)); | |
| 254 args[3] = CheckError(Dart_NewInteger(meta_state)); | |
| 255 args[4] = CheckError(Dart_NewInteger(repeat)); | |
| 256 int rtn = Invoke(function, 5, args); | |
| 257 Dart_ExitScope(); | |
| 258 Dart_ExitIsolate(); | |
| 259 LOGI("Done %s", function); | |
| 260 return rtn; | |
| 261 } | |
| 262 return -1; | |
| 263 } | |
| 264 | |
| 265 int VMGlue::Invoke(const char* function, int argc, Dart_Handle* args) { | |
| 266 Dart_Handle result; | |
| 267 | |
| 268 LOGI("in invoke(%s)", function); | |
| 269 | |
| 270 // Lookup the library of the root script. | |
| 271 LOGI("looking up the root library"); | |
| 272 Dart_Handle library = Dart_RootLibrary(); | |
| 273 if (Dart_IsNull(library)) { | |
| 274 return ErrorExit("Unable to find root library\n"); | |
| 275 } | |
| 276 | |
| 277 // Lookup and invoke the appropriate function. | |
| 278 LOGI("invoking %s", function); | |
| 279 result = Dart_Invoke(library, Dart_NewStringFromCString(function), argc, args)
; | |
| 280 | |
| 281 if (Dart_IsError(result)) { | |
| 282 return ErrorExit("%s\n", Dart_GetError(result)); | |
| 283 } | |
| 284 | |
| 285 // TODO(vsm): I don't think we need this. | |
| 286 // Keep handling messages until the last active receive port is closed. | |
| 287 LOGI("Entering Dart message loop"); | |
| 288 result = Dart_RunLoop(); | |
| 289 if (Dart_IsError(result)) { | |
| 290 return ErrorExit("%s\n", Dart_GetError(result)); | |
| 291 } | |
| 292 | |
| 293 LOGI("out invoke"); | |
| 294 return 0; | |
| 295 } | |
| 296 | |
| 297 void VMGlue::FinishMainIsolate() { | |
| 298 LOGI("Finish main isolate"); | |
| 299 Dart_EnterIsolate(isolate_); | |
| 300 // Shutdown the isolate. | |
| 301 Dart_ShutdownIsolate(); | |
| 302 isolate_ = NULL; | |
| 303 initialized_script_ = false; | |
| 304 } | |
| OLD | NEW |