Chromium Code Reviews| 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 // We pass in x, y, w, h. x and y are always zero but we include them |
| 201 args[0] = CheckError(Dart_NewInteger(graphics_->width())); | 201 // because for the same app running in web page with HTML canvas this |
| 202 args[1] = CheckError(Dart_NewInteger(graphics_->height())); | 202 // will not be true. |
|
vsm
2013/01/09 21:17:21
Don't follow. Why would x and y be non-zero for H
gram
2013/01/09 22:33:28
You're right. My browser based bubble app used the
| |
| 203 int rtn = Invoke("setup", 2, args); | 203 Dart_Handle args[4]; |
| 204 args[0] = CheckError(Dart_NewInteger(0)); | |
| 205 args[1] = CheckError(Dart_NewInteger(0)); | |
| 206 args[2] = CheckError(Dart_NewInteger(graphics_->width())); | |
| 207 args[3] = CheckError(Dart_NewInteger(graphics_->height())); | |
| 208 int rtn = Invoke("setup", 4, args); | |
| 204 Dart_ExitScope(); | 209 Dart_ExitScope(); |
| 205 Dart_ExitIsolate(); | 210 Dart_ExitIsolate(); |
| 206 LOGI("Done setup"); | 211 LOGI("Done setup"); |
| 207 return rtn; | 212 return rtn; |
| 208 } | 213 } |
| 209 return 0; | 214 return 0; |
| 210 } | 215 } |
| 211 | 216 |
| 212 int VMGlue::CallUpdate() { | 217 int VMGlue::CallUpdate() { |
| 213 if (initialized_script_) { | 218 if (initialized_script_) { |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 226 int VMGlue::OnMotionEvent(const char* pFunction, int64_t pWhen, | 231 int VMGlue::OnMotionEvent(const char* pFunction, int64_t pWhen, |
| 227 float pMoveX, float pMoveY) { | 232 float pMoveX, float pMoveY) { |
| 228 if (initialized_script_) { | 233 if (initialized_script_) { |
| 229 LOGI("Invoking %s", pFunction); | 234 LOGI("Invoking %s", pFunction); |
| 230 Dart_EnterIsolate(isolate_); | 235 Dart_EnterIsolate(isolate_); |
| 231 Dart_EnterScope(); | 236 Dart_EnterScope(); |
| 232 Dart_Handle args[3]; | 237 Dart_Handle args[3]; |
| 233 args[0] = CheckError(Dart_NewInteger(pWhen)); | 238 args[0] = CheckError(Dart_NewInteger(pWhen)); |
| 234 args[1] = CheckError(Dart_NewDouble(pMoveX)); | 239 args[1] = CheckError(Dart_NewDouble(pMoveX)); |
| 235 args[2] = CheckError(Dart_NewDouble(pMoveY)); | 240 args[2] = CheckError(Dart_NewDouble(pMoveY)); |
| 236 int rtn = Invoke(pFunction, 3, args); | 241 int rtn = Invoke(pFunction, 3, args, false); |
| 237 Dart_ExitScope(); | 242 Dart_ExitScope(); |
| 238 Dart_ExitIsolate(); | 243 Dart_ExitIsolate(); |
| 239 LOGI("Done %s", pFunction); | 244 LOGI("Done %s", pFunction); |
| 240 return rtn; | 245 return rtn; |
| 241 } | 246 } |
| 242 return -1; | 247 return -1; |
| 243 } | 248 } |
| 244 | 249 |
| 245 int VMGlue::OnKeyEvent(const char* function, int64_t when, int32_t flags, | 250 int VMGlue::OnKeyEvent(const char* function, int64_t when, int32_t flags, |
| 246 int32_t key_code, int32_t meta_state, int32_t repeat) { | 251 int32_t key_code, int32_t meta_state, int32_t repeat) { |
| 247 if (initialized_script_) { | 252 if (initialized_script_) { |
| 248 LOGI("Invoking %s", function); | 253 LOGI("Invoking %s", function); |
| 249 Dart_EnterIsolate(isolate_); | 254 Dart_EnterIsolate(isolate_); |
| 250 Dart_EnterScope(); | 255 Dart_EnterScope(); |
| 251 Dart_Handle args[5]; | 256 Dart_Handle args[5]; |
| 252 args[0] = CheckError(Dart_NewInteger(when)); | 257 args[0] = CheckError(Dart_NewInteger(when)); |
| 253 args[1] = CheckError(Dart_NewInteger(flags)); | 258 args[1] = CheckError(Dart_NewInteger(flags)); |
| 254 args[2] = CheckError(Dart_NewInteger(key_code)); | 259 args[2] = CheckError(Dart_NewInteger(key_code)); |
| 255 args[3] = CheckError(Dart_NewInteger(meta_state)); | 260 args[3] = CheckError(Dart_NewInteger(meta_state)); |
| 256 args[4] = CheckError(Dart_NewInteger(repeat)); | 261 args[4] = CheckError(Dart_NewInteger(repeat)); |
| 257 int rtn = Invoke(function, 5, args); | 262 int rtn = Invoke(function, 5, args, false); |
| 258 Dart_ExitScope(); | 263 Dart_ExitScope(); |
| 259 Dart_ExitIsolate(); | 264 Dart_ExitIsolate(); |
| 260 LOGI("Done %s", function); | 265 LOGI("Done %s", function); |
| 261 return rtn; | 266 return rtn; |
| 262 } | 267 } |
| 263 return -1; | 268 return -1; |
| 264 } | 269 } |
| 265 | 270 |
| 266 int VMGlue::Invoke(const char* function, int argc, Dart_Handle* args) { | 271 int VMGlue::Invoke(const char* function, |
| 267 Dart_Handle result; | 272 int argc, |
| 268 | 273 Dart_Handle* args, |
| 274 bool failIfNotDefined) { | |
| 269 LOGI("in invoke(%s)", function); | 275 LOGI("in invoke(%s)", function); |
| 270 | 276 |
| 271 // Lookup the library of the root script. | 277 // Lookup the library of the root script. |
| 272 LOGI("looking up the root library"); | 278 LOGI("looking up the root library"); |
| 273 Dart_Handle library = Dart_RootLibrary(); | 279 Dart_Handle library = Dart_RootLibrary(); |
| 274 if (Dart_IsNull(library)) { | 280 if (Dart_IsNull(library)) { |
| 275 return ErrorExit("Unable to find root library\n"); | 281 return ErrorExit("Unable to find root library\n"); |
| 276 } | 282 } |
| 277 | 283 |
| 284 Dart_Handle nameHandle = Dart_NewStringFromCString(function); | |
| 285 | |
| 286 if (!failIfNotDefined) { | |
| 287 // TODO(gram) - this doesn't work - we need a way of seeing if | |
| 288 // the function is defined before we call it. | |
| 289 // Dart_Handle h = Dart_LookupVariable(library, nameHandle); | |
| 290 // if (Dart_IsNull(h)) { | |
|
vsm
2013/01/09 21:17:21
I think you want Dart_LookupFunction instead here.
gram
2013/01/09 22:33:28
I tried a few different things here, so I may have
| |
| 291 // LOGE("%s is not defined", function); | |
| 292 // LOGI("out invoke"); | |
| 293 // return 0; | |
| 294 // } | |
| 295 } | |
| 278 // Lookup and invoke the appropriate function. | 296 // Lookup and invoke the appropriate function. |
| 279 LOGI("invoking %s", function); | 297 LOGI("invoking %s", function); |
| 280 result = | 298 Dart_Handle result = Dart_Invoke(library, nameHandle, argc, args); |
| 281 Dart_Invoke(library, Dart_NewStringFromCString(function), argc, args); | |
| 282 | 299 |
| 283 if (Dart_IsError(result)) { | 300 if (Dart_IsError(result)) { |
| 284 return ErrorExit("%s\n", Dart_GetError(result)); | 301 if (failIfNotDefined) { // Temp hack; see above. |
| 302 return ErrorExit("Invoke %s: %s\n", function, Dart_GetError(result)); | |
| 303 } else { | |
| 304 LOGE("Invoke %s: %s", function, Dart_GetError(result)); | |
| 305 } | |
| 285 } | 306 } |
| 286 | 307 |
| 287 // TODO(vsm): I don't think we need this. | 308 // TODO(vsm): I don't think we need this. |
| 288 // Keep handling messages until the last active receive port is closed. | 309 // Keep handling messages until the last active receive port is closed. |
| 289 LOGI("Entering Dart message loop"); | 310 LOGI("Entering Dart message loop"); |
| 290 result = Dart_RunLoop(); | 311 result = Dart_RunLoop(); |
| 291 if (Dart_IsError(result)) { | 312 if (Dart_IsError(result)) { |
| 292 return ErrorExit("%s\n", Dart_GetError(result)); | 313 return ErrorExit("Dart_RunLoop: %s\n", Dart_GetError(result)); |
| 293 } | 314 } |
| 294 | 315 |
| 295 LOGI("out invoke"); | 316 LOGI("out invoke"); |
| 296 return 0; | 317 return 0; |
| 297 } | 318 } |
| 298 | 319 |
| 299 void VMGlue::FinishMainIsolate() { | 320 void VMGlue::FinishMainIsolate() { |
| 300 LOGI("Finish main isolate"); | 321 LOGI("Finish main isolate"); |
| 301 Dart_EnterIsolate(isolate_); | 322 Dart_EnterIsolate(isolate_); |
| 302 // Shutdown the isolate. | 323 // Shutdown the isolate. |
| 303 Dart_ShutdownIsolate(); | 324 Dart_ShutdownIsolate(); |
| 304 isolate_ = NULL; | 325 isolate_ = NULL; |
| 305 initialized_script_ = false; | 326 initialized_script_ = false; |
| 306 } | 327 } |
| OLD | NEW |