OLD | NEW |
(Empty) | |
| 1 #include "jni/vm_glue.h" |
| 2 |
| 3 #include <math.h> |
| 4 #include <unistd.h> |
| 5 |
| 6 #include "bin/eventhandler.h" |
| 7 #include "bin/isolate_data.h" |
| 8 #include "bin/log.h" |
| 9 #include "bin/platform.h" |
| 10 #include "bin/process.h" |
| 11 #include "vm/flags.h" |
| 12 |
| 13 // snapshot_buffer points to a snapshot if we link in a snapshot otherwise |
| 14 // it is initialized to NULL. |
| 15 extern const uint8_t* snapshot_buffer; |
| 16 |
| 17 VMGlue::VMGlue(Graphics* graphics) |
| 18 : graphics_(graphics), |
| 19 isolate_(NULL), |
| 20 initialized_vm_(false), |
| 21 initialized_script_(false) { |
| 22 Log::Print("Creating VMGlue"); |
| 23 } |
| 24 |
| 25 int VMGlue::ErrorExit(const char* format, ...) { |
| 26 va_list arguments; |
| 27 va_start(arguments, format); |
| 28 Log::VPrintErr(format, arguments); |
| 29 va_end(arguments); |
| 30 Dart_ExitScope(); |
| 31 Dart_ShutdownIsolate(); |
| 32 Log::PrintErr("Shutdown isolate"); |
| 33 return -1; |
| 34 } |
| 35 |
| 36 Dart_Handle VMGlue::SetupRuntimeOptions(CommandLineOptions* options, |
| 37 const char* executable_name, |
| 38 const char* script_name) { |
| 39 int options_count = 0; |
| 40 Dart_Handle dart_executable = DartUtils::NewString(executable_name); |
| 41 if (Dart_IsError(dart_executable)) { |
| 42 return dart_executable; |
| 43 } |
| 44 Dart_Handle dart_script = DartUtils::NewString(script_name); |
| 45 if (Dart_IsError(dart_script)) { |
| 46 return dart_script; |
| 47 } |
| 48 Dart_Handle dart_arguments = Dart_NewList(0); |
| 49 if (Dart_IsError(dart_arguments)) { |
| 50 return dart_arguments; |
| 51 } |
| 52 Dart_Handle core_lib_url = DartUtils::NewString("dart:core"); |
| 53 if (Dart_IsError(core_lib_url)) { |
| 54 return core_lib_url; |
| 55 } |
| 56 Dart_Handle core_lib = Dart_LookupLibrary(core_lib_url); |
| 57 if (Dart_IsError(core_lib)) { |
| 58 return core_lib; |
| 59 } |
| 60 Dart_Handle runtime_options_class_name = |
| 61 DartUtils::NewString("_OptionsImpl"); |
| 62 if (Dart_IsError(runtime_options_class_name)) { |
| 63 return runtime_options_class_name; |
| 64 } |
| 65 Dart_Handle runtime_options_class = Dart_GetClass( |
| 66 core_lib, runtime_options_class_name); |
| 67 if (Dart_IsError(runtime_options_class)) { |
| 68 return runtime_options_class; |
| 69 } |
| 70 Dart_Handle executable_name_name = DartUtils::NewString("_nativeExecutable"); |
| 71 if (Dart_IsError(executable_name_name)) { |
| 72 return executable_name_name; |
| 73 } |
| 74 Dart_Handle set_executable_name = |
| 75 Dart_SetField(runtime_options_class, |
| 76 executable_name_name, |
| 77 dart_executable); |
| 78 if (Dart_IsError(set_executable_name)) { |
| 79 return set_executable_name; |
| 80 } |
| 81 Dart_Handle script_name_name = DartUtils::NewString("_nativeScript"); |
| 82 if (Dart_IsError(script_name_name)) { |
| 83 return script_name_name; |
| 84 } |
| 85 Dart_Handle set_script_name = |
| 86 Dart_SetField(runtime_options_class, script_name_name, dart_script); |
| 87 if (Dart_IsError(set_script_name)) { |
| 88 return set_script_name; |
| 89 } |
| 90 Dart_Handle native_name = DartUtils::NewString("_nativeArguments"); |
| 91 if (Dart_IsError(native_name)) { |
| 92 return native_name; |
| 93 } |
| 94 return Dart_SetField(runtime_options_class, native_name, dart_arguments); |
| 95 } |
| 96 |
| 97 #define CHECK_RESULT(result) \ |
| 98 if (Dart_IsError(result)) { \ |
| 99 *error = strdup(Dart_GetError(result)); \ |
| 100 Log::PrintErr(*error); \ |
| 101 Dart_ExitScope(); \ |
| 102 Dart_ShutdownIsolate(); \ |
| 103 return false; \ |
| 104 } |
| 105 |
| 106 // Returns true on success, false on failure. |
| 107 bool VMGlue::CreateIsolateAndSetupHelper(const char* script_uri, |
| 108 const char* main, |
| 109 void* data, |
| 110 char** error) { |
| 111 Log::Print("Creating isolate %s, %s", script_uri, main); |
| 112 Dart_Isolate isolate = |
| 113 Dart_CreateIsolate(script_uri, main, snapshot_buffer, data, error); |
| 114 if (isolate == NULL) { |
| 115 Log::PrintErr("Couldn't create isolate: %s", error); |
| 116 return false; |
| 117 } |
| 118 |
| 119 Log::Print("Entering scope"); |
| 120 Dart_EnterScope(); |
| 121 |
| 122 if (snapshot_buffer != NULL) { |
| 123 // Setup the native resolver as the snapshot does not carry it. |
| 124 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); |
| 125 Builtin::SetNativeResolver(Builtin::kIOLibrary); |
| 126 } |
| 127 |
| 128 // Set up the library tag handler for this isolate. |
| 129 Log::Print("Setting up library tag handler"); |
| 130 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); |
| 131 CHECK_RESULT(result); |
| 132 |
| 133 // Load the specified application script into the newly created isolate. |
| 134 Dart_Handle library; |
| 135 |
| 136 // Prepare builtin and its dependent libraries for use to resolve URIs. |
| 137 Log::Print("Preparing uriLibrary"); |
| 138 Dart_Handle uri_lib = Builtin::LoadAndCheckLibrary(Builtin::kUriLibrary); |
| 139 CHECK_RESULT(uri_lib); |
| 140 Log::Print("Preparing builtinLibrary"); |
| 141 Dart_Handle builtin_lib = |
| 142 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); |
| 143 CHECK_RESULT(builtin_lib); |
| 144 |
| 145 // Prepare for script loading by setting up the 'print' and 'timer' |
| 146 // closures and setting up 'package root' for URI resolution. |
| 147 char* package_root = NULL; |
| 148 Log::Print("Preparing for script loading"); |
| 149 result = DartUtils::PrepareForScriptLoading(package_root, builtin_lib); |
| 150 CHECK_RESULT(result); |
| 151 |
| 152 Log::Print("Loading script %s", script_uri); |
| 153 library = DartUtils::LoadScript(script_uri, builtin_lib); |
| 154 |
| 155 CHECK_RESULT(library); |
| 156 if (!Dart_IsLibrary(library)) { |
| 157 Log::PrintErr("Expected a library when loading script: %s", |
| 158 script_uri); |
| 159 Dart_ExitScope(); |
| 160 Dart_ShutdownIsolate(); |
| 161 return false; |
| 162 } |
| 163 Dart_ExitScope(); |
| 164 return true; |
| 165 } |
| 166 |
| 167 bool VMGlue::CreateIsolateAndSetup(const char* script_uri, |
| 168 const char* main, |
| 169 void* data, char** error) { |
| 170 return CreateIsolateAndSetupHelper(script_uri, |
| 171 main, |
| 172 new IsolateData(), |
| 173 error); |
| 174 } |
| 175 |
| 176 #define VMHOSTNAME "android_dart_host" |
| 177 #define MAINSCRIPT "/data/data/com.google.dartndk/app_dart/main.dart" |
| 178 |
| 179 void VMGlue::ShutdownIsolate(void* callback_data) { |
| 180 IsolateData* isolate_data = reinterpret_cast<IsolateData*>(callback_data); |
| 181 EventHandler* handler = isolate_data->event_handler; |
| 182 if (handler != NULL) handler->Shutdown(); |
| 183 delete isolate_data; |
| 184 } |
| 185 |
| 186 int VMGlue::InitializeVM() { |
| 187 // Perform platform specific initialization. |
| 188 Log::Print("Initializing platform"); |
| 189 if (!Platform::Initialize()) { |
| 190 Log::PrintErr("Initialization failed\n"); |
| 191 return -1; |
| 192 } |
| 193 |
| 194 // We need the next call to get Datrt_Initialize not to bail early. |
| 195 Log::Print("Processing command line flags"); |
| 196 dart::Flags::ProcessCommandLineFlags(0, NULL); |
| 197 |
| 198 // Initialize the Dart VM, providing the callbacks to use for |
| 199 // creating and shutting down isolates. |
| 200 Log::Print("Initializing Dart"); |
| 201 if (!Dart_Initialize(CreateIsolateAndSetup, |
| 202 NULL, |
| 203 NULL, |
| 204 ShutdownIsolate)) { |
| 205 Log::PrintErr("VM initialization failed\n"); |
| 206 return -1; |
| 207 } |
| 208 DartUtils::SetOriginalWorkingDirectory(); |
| 209 initialized_vm_ = true; |
| 210 return 0; |
| 211 } |
| 212 |
| 213 int VMGlue::StartMainIsolate() { |
| 214 if (!initialized_vm_) { |
| 215 int rtn = InitializeVM(); |
| 216 if (rtn != 0) return rtn; |
| 217 } |
| 218 |
| 219 // Create an isolate and loads up the application script. |
| 220 char* error = NULL; |
| 221 if (!CreateIsolateAndSetup(MAINSCRIPT, "main", NULL, &error)) { |
| 222 Log::PrintErr("CreateIsolateAndSetup: %s\n", error); |
| 223 free(error); |
| 224 return -1; |
| 225 } |
| 226 |
| 227 Log::Print("Created isolate"); |
| 228 |
| 229 isolate_ = Dart_CurrentIsolate(); |
| 230 Dart_ExitIsolate(); |
| 231 return 0; |
| 232 } |
| 233 |
| 234 int VMGlue::CallSetup() { |
| 235 if (!initialized_script_) { |
| 236 initialized_script_ = true; |
| 237 Log::Print("Invoking setup"); |
| 238 Dart_EnterIsolate(isolate_); |
| 239 Dart_EnterScope(); |
| 240 Dart_Handle args[2]; |
| 241 args[0] = Dart_NewInteger(graphics_->width()); |
| 242 args[1] = Dart_NewInteger(graphics_->height()); |
| 243 int rtn = Invoke("setup", 2, args); |
| 244 Dart_ExitScope(); |
| 245 Dart_ExitIsolate(); |
| 246 Log::Print("Done setup"); |
| 247 return rtn; |
| 248 } |
| 249 return 0; |
| 250 } |
| 251 |
| 252 int VMGlue::CallUpdate() { |
| 253 if (initialized_script_) { |
| 254 Log::Print("Invoking update"); |
| 255 Dart_EnterIsolate(isolate_); |
| 256 Dart_EnterScope(); |
| 257 int rtn = Invoke("update", 0, 0); |
| 258 Dart_ExitScope(); |
| 259 Dart_ExitIsolate(); |
| 260 Log::Print("Done update"); |
| 261 return rtn; |
| 262 } |
| 263 return -1; |
| 264 } |
| 265 |
| 266 int VMGlue::OnMotionEvent(const char* pFunction, int64_t pWhen, |
| 267 float pMoveX, float pMoveY) { |
| 268 if (initialized_script_) { |
| 269 Log::Print("Invoking %s", pFunction); |
| 270 Dart_EnterIsolate(isolate_); |
| 271 Dart_EnterScope(); |
| 272 Dart_Handle args[3]; |
| 273 args[0] = Dart_NewInteger(pWhen); |
| 274 args[1] = Dart_NewDouble(pMoveX); |
| 275 args[2] = Dart_NewDouble(pMoveY); |
| 276 int rtn = Invoke(pFunction, 3, args); |
| 277 Dart_ExitScope(); |
| 278 Dart_ExitIsolate(); |
| 279 Log::Print("Done %s", pFunction); |
| 280 return rtn; |
| 281 } |
| 282 return -1; |
| 283 } |
| 284 |
| 285 int VMGlue::OnKeyEvent(const char* function, int64_t when, int32_t flags, |
| 286 int32_t key_code, int32_t meta_state, int32_t repeat) { |
| 287 if (initialized_script_) { |
| 288 Log::Print("Invoking %s", function); |
| 289 Dart_EnterIsolate(isolate_); |
| 290 Dart_EnterScope(); |
| 291 Dart_Handle args[5]; |
| 292 args[0] = Dart_NewInteger(when); |
| 293 args[1] = Dart_NewInteger(flags); |
| 294 args[2] = Dart_NewInteger(key_code); |
| 295 args[3] = Dart_NewInteger(meta_state); |
| 296 args[4] = Dart_NewInteger(repeat); |
| 297 int rtn = Invoke(function, 5, args); |
| 298 Dart_ExitScope(); |
| 299 Dart_ExitIsolate(); |
| 300 Log::Print("Done %s", function); |
| 301 return rtn; |
| 302 } |
| 303 return -1; |
| 304 } |
| 305 |
| 306 int VMGlue::Invoke(const char* function, int argc, Dart_Handle* args) { |
| 307 Dart_Handle result; |
| 308 |
| 309 Log::Print("in invoke(%s)", function); |
| 310 |
| 311 // Create a dart options object that can be accessed from dart code. |
| 312 Log::Print("setting up runtime options"); |
| 313 CommandLineOptions dart_options(0); |
| 314 Dart_Handle options_result = |
| 315 SetupRuntimeOptions(&dart_options, VMHOSTNAME, MAINSCRIPT); |
| 316 if (Dart_IsError(options_result)) { |
| 317 return ErrorExit("%s\n", Dart_GetError(options_result)); |
| 318 } |
| 319 |
| 320 // Lookup the library of the root script. |
| 321 Log::Print("looking up the root library"); |
| 322 Dart_Handle library = Dart_RootLibrary(); |
| 323 if (Dart_IsNull(library)) { |
| 324 return ErrorExit("Unable to find root library\n"); |
| 325 } |
| 326 |
| 327 // Lookup and invoke the appropriate function. |
| 328 result = Dart_Invoke(library, DartUtils::NewString(function), argc, args); |
| 329 |
| 330 if (Dart_IsError(result)) { |
| 331 return ErrorExit("%s\n", Dart_GetError(result)); |
| 332 } |
| 333 |
| 334 // Keep handling messages until the last active receive port is closed. |
| 335 Log::Print("Entering Dart message loop"); |
| 336 result = Dart_RunLoop(); |
| 337 if (Dart_IsError(result)) { |
| 338 return ErrorExit("%s\n", Dart_GetError(result)); |
| 339 } |
| 340 |
| 341 Log::Print("out invoke"); |
| 342 return 0; |
| 343 } |
| 344 |
| 345 void VMGlue::FinishMainIsolate() { |
| 346 Log::Print("Finish main isolate"); |
| 347 Dart_EnterIsolate(isolate_); |
| 348 // Shutdown the isolate. |
| 349 Dart_ShutdownIsolate(); |
| 350 // Terminate process exit-code handler. |
| 351 Process::TerminateExitCodeHandler(); |
| 352 isolate_ = NULL; |
| 353 initialized_script_ = false; |
| 354 } |
| 355 |
OLD | NEW |