OLD | NEW |
(Empty) | |
| 1 #include <unistd.h> |
| 2 #include <math.h> |
| 3 |
| 4 #include "vm_glue.h" |
| 5 #include "vm/flags.h" |
| 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 |
| 12 // snapshot_buffer points to a snapshot if we link in a snapshot otherwise |
| 13 // it is initialized to NULL. |
| 14 extern const uint8_t* snapshot_buffer; |
| 15 |
| 16 VMGlue::VMGlue(Graphics* pGraphics) : |
| 17 mGraphics(pGraphics), |
| 18 mIsolate(NULL), |
| 19 mInitializedVM(false), |
| 20 mInitializedScript(false) { |
| 21 Log::Print("Creating VMGlue"); |
| 22 } |
| 23 |
| 24 int VMGlue::ErrorExit(const char* format, ...) { |
| 25 va_list arguments; |
| 26 va_start(arguments, format); |
| 27 Log::VPrintErr(format, arguments); |
| 28 va_end(arguments); |
| 29 Dart_ExitScope(); |
| 30 Dart_ShutdownIsolate(); |
| 31 Log::PrintErr("Shutdown isolate"); |
| 32 return -1; |
| 33 } |
| 34 |
| 35 Dart_Handle VMGlue::SetupRuntimeOptions(CommandLineOptions* options, |
| 36 const char* executable_name, |
| 37 const char* script_name) { |
| 38 int options_count = 0; |
| 39 Dart_Handle dart_executable = DartUtils::NewString(executable_name); |
| 40 if (Dart_IsError(dart_executable)) { |
| 41 return dart_executable; |
| 42 } |
| 43 Dart_Handle dart_script = DartUtils::NewString(script_name); |
| 44 if (Dart_IsError(dart_script)) { |
| 45 return dart_script; |
| 46 } |
| 47 Dart_Handle dart_arguments = Dart_NewList(0); |
| 48 if (Dart_IsError(dart_arguments)) { |
| 49 return dart_arguments; |
| 50 } |
| 51 Dart_Handle core_lib_url = DartUtils::NewString("dart:core"); |
| 52 if (Dart_IsError(core_lib_url)) { |
| 53 return core_lib_url; |
| 54 } |
| 55 Dart_Handle core_lib = Dart_LookupLibrary(core_lib_url); |
| 56 if (Dart_IsError(core_lib)) { |
| 57 return core_lib; |
| 58 } |
| 59 Dart_Handle runtime_options_class_name = |
| 60 DartUtils::NewString("_OptionsImpl"); |
| 61 if (Dart_IsError(runtime_options_class_name)) { |
| 62 return runtime_options_class_name; |
| 63 } |
| 64 Dart_Handle runtime_options_class = Dart_GetClass( |
| 65 core_lib, runtime_options_class_name); |
| 66 if (Dart_IsError(runtime_options_class)) { |
| 67 return runtime_options_class; |
| 68 } |
| 69 Dart_Handle executable_name_name = DartUtils::NewString("_nativeExecutable"); |
| 70 if (Dart_IsError(executable_name_name)) { |
| 71 return executable_name_name; |
| 72 } |
| 73 Dart_Handle set_executable_name = |
| 74 Dart_SetField(runtime_options_class, |
| 75 executable_name_name, |
| 76 dart_executable); |
| 77 if (Dart_IsError(set_executable_name)) { |
| 78 return set_executable_name; |
| 79 } |
| 80 Dart_Handle script_name_name = DartUtils::NewString("_nativeScript"); |
| 81 if (Dart_IsError(script_name_name)) { |
| 82 return script_name_name; |
| 83 } |
| 84 Dart_Handle set_script_name = |
| 85 Dart_SetField(runtime_options_class, script_name_name, dart_script); |
| 86 if (Dart_IsError(set_script_name)) { |
| 87 return set_script_name; |
| 88 } |
| 89 Dart_Handle native_name = DartUtils::NewString("_nativeArguments"); |
| 90 if (Dart_IsError(native_name)) { |
| 91 return native_name; |
| 92 } |
| 93 return Dart_SetField(runtime_options_class, native_name, dart_arguments); |
| 94 } |
| 95 |
| 96 #define CHECK_RESULT(result) \ |
| 97 if (Dart_IsError(result)) { \ |
| 98 *error = strdup(Dart_GetError(result)); \ |
| 99 Log::PrintErr(*error);
\ |
| 100 Dart_ExitScope(); \ |
| 101 Dart_ShutdownIsolate(); \ |
| 102 return false; \ |
| 103 } |
| 104 |
| 105 // Returns true on success, false on failure. |
| 106 bool VMGlue::CreateIsolateAndSetupHelper(const char* script_uri, |
| 107 const char* main, |
| 108 void* data, |
| 109 char** error) { |
| 110 Log::Print("Creating isolate %s, %s", script_uri, main); |
| 111 Dart_Isolate isolate = |
| 112 Dart_CreateIsolate(script_uri, main, snapshot_buffer, data, error); |
| 113 if (isolate == NULL) { |
| 114 Log::PrintErr("Couldn't create isolate: %s", error); |
| 115 return false; |
| 116 } |
| 117 |
| 118 Log::Print("Entering scope"); |
| 119 Dart_EnterScope(); |
| 120 |
| 121 if (snapshot_buffer != NULL) { |
| 122 // Setup the native resolver as the snapshot does not carry it. |
| 123 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); |
| 124 Builtin::SetNativeResolver(Builtin::kIOLibrary); |
| 125 } |
| 126 |
| 127 // Set up the library tag handler for this isolate. |
| 128 Log::Print("Setting up library tag handler"); |
| 129 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); |
| 130 CHECK_RESULT(result); |
| 131 |
| 132 // Load the specified application script into the newly created isolate. |
| 133 Dart_Handle library; |
| 134 |
| 135 // Prepare builtin and its dependent libraries for use to resolve URIs. |
| 136 Log::Print("Preparing uriLibrary"); |
| 137 Dart_Handle uri_lib = Builtin::LoadAndCheckLibrary(Builtin::kUriLibrary); |
| 138 CHECK_RESULT(uri_lib); |
| 139 Log::Print("Preparing builtinLibrary"); |
| 140 Dart_Handle builtin_lib = |
| 141 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); |
| 142 CHECK_RESULT(builtin_lib); |
| 143 |
| 144 // Prepare for script loading by setting up the 'print' and 'timer' |
| 145 // closures and setting up 'package root' for URI resolution. |
| 146 char *package_root = NULL; |
| 147 Log::Print("Preparing for script loading"); |
| 148 result = DartUtils::PrepareForScriptLoading(package_root, builtin_lib); |
| 149 CHECK_RESULT(result); |
| 150 |
| 151 Log::Print("Loading script %s", script_uri); |
| 152 library = DartUtils::LoadScript(script_uri, builtin_lib); |
| 153 |
| 154 CHECK_RESULT(library); |
| 155 if (!Dart_IsLibrary(library)) { |
| 156 Log::PrintErr("Expected a library when loading script: %s", |
| 157 script_uri); |
| 158 Dart_ExitScope(); |
| 159 Dart_ShutdownIsolate(); |
| 160 return false; |
| 161 } |
| 162 Dart_ExitScope(); |
| 163 return true; |
| 164 } |
| 165 |
| 166 bool VMGlue::CreateIsolateAndSetup(const char* script_uri, |
| 167 const char* main, |
| 168 void* data, char** error) { |
| 169 return CreateIsolateAndSetupHelper(script_uri, |
| 170 main, |
| 171 new IsolateData(), |
| 172 error); |
| 173 } |
| 174 |
| 175 #define VMHOSTNAME "android_dart_host" |
| 176 #define MAINSCRIPT "/data/data/com.google.dartndk/app_dart/main.dart" |
| 177 |
| 178 void VMGlue::ShutdownIsolate(void* callback_data) { |
| 179 IsolateData* isolate_data = reinterpret_cast<IsolateData*>(callback_data); |
| 180 EventHandler* handler = isolate_data->event_handler; |
| 181 if (handler != NULL) handler->Shutdown(); |
| 182 delete isolate_data; |
| 183 } |
| 184 |
| 185 int VMGlue::initializeVM() { |
| 186 |
| 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 mInitializedVM = true; |
| 210 return 0; |
| 211 } |
| 212 |
| 213 int VMGlue::startMainIsolate() { |
| 214 if (!mInitializedVM) { |
| 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 mIsolate = Dart_CurrentIsolate(); |
| 230 Dart_ExitIsolate(); |
| 231 return 0; |
| 232 } |
| 233 |
| 234 int VMGlue::callSetup() { |
| 235 if (!mInitializedScript) { |
| 236 mInitializedScript = true; |
| 237 Log::Print("Invoking setup"); |
| 238 Dart_EnterIsolate(mIsolate); |
| 239 Dart_EnterScope(); |
| 240 Dart_Handle lArgs[2]; |
| 241 lArgs[0] = Dart_NewInteger(mGraphics->getWidth()); |
| 242 lArgs[1] = Dart_NewInteger(mGraphics->getHeight()); |
| 243 int rtn = invoke("setup", 2, lArgs); |
| 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 (mInitializedScript) { |
| 254 Log::Print("Invoking update"); |
| 255 Dart_EnterIsolate(mIsolate); |
| 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 (mInitializedScript) { |
| 269 Log::Print("Invoking %s", pFunction); |
| 270 Dart_EnterIsolate(mIsolate); |
| 271 Dart_EnterScope(); |
| 272 Dart_Handle lArgs[3]; |
| 273 lArgs[0] = Dart_NewInteger(pWhen); |
| 274 lArgs[1] = Dart_NewDouble(pMoveX); |
| 275 lArgs[2] = Dart_NewDouble(pMoveY); |
| 276 int rtn = invoke(pFunction, 3, lArgs); |
| 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* pFunction, int64_t pWhen, int32_t pFlags, |
| 286 int32_t pKeyCode, int32_t pMetaState, int32_t pRepeat) { |
| 287 if (mInitializedScript) { |
| 288 Log::Print("Invoking %s", pFunction); |
| 289 Dart_EnterIsolate(mIsolate); |
| 290 Dart_EnterScope(); |
| 291 Dart_Handle lArgs[5]; |
| 292 lArgs[0] = Dart_NewInteger(pWhen); |
| 293 lArgs[1] = Dart_NewInteger(pFlags); |
| 294 lArgs[2] = Dart_NewInteger(pKeyCode); |
| 295 lArgs[3] = Dart_NewInteger(pMetaState); |
| 296 lArgs[4] = Dart_NewInteger(pRepeat); |
| 297 int rtn = invoke(pFunction, 5, lArgs); |
| 298 Dart_ExitScope(); |
| 299 Dart_ExitIsolate(); |
| 300 Log::Print("Done %s", pFunction); |
| 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(mIsolate); |
| 348 // Shutdown the isolate. |
| 349 Dart_ShutdownIsolate(); |
| 350 // Terminate process exit-code handler. |
| 351 Process::TerminateExitCodeHandler(); |
| 352 mIsolate = NULL; |
| 353 mInitializedScript = false; |
| 354 } |
| 355 |
OLD | NEW |