Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(180)

Side by Side Diff: samples/android_sample/jni/vm_glue.cc

Issue 11416343: Refactored Android samples / embedder. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix type error on playBackground Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « samples/android_sample/jni/vm_glue.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #include "jni/vm_glue.h" 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.
2 4
3 #include <math.h> 5 #include <math.h>
6 #include <stdio.h>
4 #include <unistd.h> 7 #include <unistd.h>
8 #include <sys/stat.h>
5 9
6 #include "bin/eventhandler.h" 10 #include "include/dart_api.h"
7 #include "bin/isolate_data.h" 11 #include "jni/android_extension.h"
8 #include "bin/log.h" 12 #include "jni/log.h"
9 #include "bin/platform.h" 13 #include "jni/vm_glue.h"
10 #include "bin/process.h"
11 #include "vm/flags.h"
12 14
13 // snapshot_buffer points to a snapshot if we link in a snapshot otherwise 15 // snapshot_buffer points to a snapshot if we link in a snapshot otherwise
14 // it is initialized to NULL. 16 // it is initialized to NULL.
15 extern const uint8_t* snapshot_buffer;
16 17
17 VMGlue::VMGlue(Graphics* graphics) 18 VMGlue::VMGlue(Graphics* graphics)
18 : graphics_(graphics), 19 : graphics_(graphics),
19 isolate_(NULL), 20 isolate_(NULL),
20 initialized_vm_(false), 21 initialized_vm_(false),
21 initialized_script_(false) { 22 initialized_script_(false) {
22 Log::Print("Creating VMGlue"); 23 LOGI("Creating VMGlue");
23 } 24 }
24 25
25 int VMGlue::ErrorExit(const char* format, ...) { 26 int VMGlue::ErrorExit(const char* format, ...) {
26 va_list arguments; 27 va_list arguments;
27 va_start(arguments, format); 28 va_start(arguments, format);
28 Log::VPrintErr(format, arguments); 29 LOGE(format, arguments);
29 va_end(arguments); 30 va_end(arguments);
30 Dart_ExitScope(); 31 Dart_ExitScope();
31 Dart_ShutdownIsolate(); 32 Dart_ShutdownIsolate();
32 Log::PrintErr("Shutdown isolate"); 33 LOGE("Shutdown isolate");
33 return -1; 34 return -1;
34 } 35 }
35 36
36 Dart_Handle VMGlue::SetupRuntimeOptions(CommandLineOptions* options, 37 Dart_Handle VMGlue::CheckError(Dart_Handle handle) {
37 const char* executable_name, 38 if (Dart_IsError(handle)) {
38 const char* script_name) { 39 LOGE("Unexpected Error Handle");
39 int options_count = 0; 40 Dart_PropagateError(handle);
40 Dart_Handle dart_executable = DartUtils::NewString(executable_name);
41 if (Dart_IsError(dart_executable)) {
42 return dart_executable;
43 } 41 }
44 Dart_Handle dart_script = DartUtils::NewString(script_name); 42 return handle;
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 } 43 }
96 44
97 #define CHECK_RESULT(result) \ 45 #define CHECK_RESULT(result) \
98 if (Dart_IsError(result)) { \ 46 if (Dart_IsError(result)) { \
99 *error = strdup(Dart_GetError(result)); \ 47 *error = strdup(Dart_GetError(result)); \
100 Log::PrintErr(*error); \ 48 LOGE(*error); \
101 Dart_ExitScope(); \ 49 Dart_ExitScope(); \
102 Dart_ShutdownIsolate(); \ 50 Dart_ShutdownIsolate(); \
103 return false; \ 51 return false; \
104 } 52 }
105 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
106 // Returns true on success, false on failure. 75 // Returns true on success, false on failure.
107 bool VMGlue::CreateIsolateAndSetupHelper(const char* script_uri, 76 bool VMGlue::CreateIsolateAndSetupHelper(const char* script_uri,
108 const char* main, 77 const char* main,
109 void* data, 78 void* data,
110 char** error) { 79 char** error) {
111 Log::Print("Creating isolate %s, %s", script_uri, main); 80 LOGI("Creating isolate %s, %s", script_uri, main);
112 Dart_Isolate isolate = 81 Dart_Isolate isolate =
113 Dart_CreateIsolate(script_uri, main, snapshot_buffer, data, error); 82 Dart_CreateIsolate(script_uri, main, NULL, data, error);
114 if (isolate == NULL) { 83 if (isolate == NULL) {
115 Log::PrintErr("Couldn't create isolate: %s", error); 84 LOGE("Couldn't create isolate: %s", error);
116 return false; 85 return false;
117 } 86 }
118 87
119 Log::Print("Entering scope"); 88 LOGI("Entering scope");
120 Dart_EnterScope(); 89 Dart_EnterScope();
121 90
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. 91 // Set up the library tag handler for this isolate.
129 Log::Print("Setting up library tag handler"); 92 LOGI("Setting up library tag handler");
130 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); 93 Dart_Handle result = CheckError(Dart_SetLibraryTagHandler(LibraryTagHandler));
131 CHECK_RESULT(result); 94 CHECK_RESULT(result);
132 95
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(); 96 Dart_ExitScope();
164 return true; 97 return true;
165 } 98 }
166 99
167 bool VMGlue::CreateIsolateAndSetup(const char* script_uri, 100 bool VMGlue::CreateIsolateAndSetup(const char* script_uri,
168 const char* main, 101 const char* main,
169 void* data, char** error) { 102 void* data, char** error) {
170 return CreateIsolateAndSetupHelper(script_uri, 103 return CreateIsolateAndSetupHelper(script_uri,
171 main, 104 main,
172 new IsolateData(), 105 data,
173 error); 106 error);
174 } 107 }
175 108
176 #define VMHOSTNAME "android_dart_host" 109 #define VMHOSTNAME "android_dart_host"
177 #define MAINSCRIPT "/data/data/com.google.dartndk/app_dart/main.dart" 110 #define MAINSCRIPT "/data/data/com.google.dartndk/app_dart/main.dart"
178 111
179 void VMGlue::ShutdownIsolate(void* callback_data) { 112 const char* VM_FLAGS[] = {
180 IsolateData* isolate_data = reinterpret_cast<IsolateData*>(callback_data); 113 "--enable_type_checks",
181 EventHandler* handler = isolate_data->event_handler; 114 "--trace_isolates",
182 if (handler != NULL) handler->Shutdown(); 115 "--trace_natives",
183 delete isolate_data; 116 };
184 }
185 117
186 int VMGlue::InitializeVM() { 118 int VMGlue::InitializeVM() {
187 // Perform platform specific initialization. 119 // We need the next call to get Dart_Initialize not to bail early.
188 Log::Print("Initializing platform"); 120 LOGI("Setting VM Options");
189 if (!Platform::Initialize()) { 121 Dart_SetVMFlags(sizeof(VM_FLAGS) / sizeof(VM_FLAGS[0]), VM_FLAGS);
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 122
198 // Initialize the Dart VM, providing the callbacks to use for 123 // Initialize the Dart VM, providing the callbacks to use for
199 // creating and shutting down isolates. 124 // creating and shutting down isolates.
200 Log::Print("Initializing Dart"); 125 LOGI("Initializing Dart");
201 if (!Dart_Initialize(CreateIsolateAndSetup, 126 if (!Dart_Initialize(CreateIsolateAndSetup,
202 NULL, 127 NULL,
203 NULL, 128 NULL,
204 ShutdownIsolate)) { 129 NULL)) {
205 Log::PrintErr("VM initialization failed\n"); 130 LOGE("VM initialization failed\n");
206 return -1; 131 return -1;
207 } 132 }
208 DartUtils::SetOriginalWorkingDirectory();
209 initialized_vm_ = true; 133 initialized_vm_ = true;
134
210 return 0; 135 return 0;
211 } 136 }
212 137
138 Dart_Handle VMGlue::LoadSourceFromFile(const char* url) {
139 FILE* file = fopen(url, "r");
140 if (file == NULL) {
141 LOGE("Main script not found at: %s\n", url);
142 return NULL;
143 }
144
145 struct stat sb;
146 int fd = fileno(file);
147 fstat(fd, &sb);
148 int length = sb.st_size;
149 LOGI("Entry file %s is %d bytes.\n", url, length);
150
151 char* buffer = reinterpret_cast<char *>(malloc((length + 1) * sizeof(char)));
152 if (read(fd, buffer, length) < 0) {
153 LOGE("Could not read script %s.\n", url);
154 return NULL;
155 }
156 buffer[length] = 0;
157 fclose(file);
158
159 Dart_Handle contents = CheckError(Dart_NewStringFromCString(buffer));
160 free(buffer);
161 return contents;
162 }
163
213 int VMGlue::StartMainIsolate() { 164 int VMGlue::StartMainIsolate() {
214 if (!initialized_vm_) { 165 if (!initialized_vm_) {
215 int rtn = InitializeVM(); 166 int rtn = InitializeVM();
216 if (rtn != 0) return rtn; 167 if (rtn != 0) return rtn;
217 } 168 }
218 169
219 // Create an isolate and loads up the application script. 170 // Create an isolate and loads up the application script.
220 char* error = NULL; 171 char* error = NULL;
221 if (!CreateIsolateAndSetup(MAINSCRIPT, "main", NULL, &error)) { 172 if (!CreateIsolateAndSetup(MAINSCRIPT, "main", NULL, &error)) {
222 Log::PrintErr("CreateIsolateAndSetup: %s\n", error); 173 LOGE("CreateIsolateAndSetup: %s\n", error);
223 free(error); 174 free(error);
224 return -1; 175 return -1;
225 } 176 }
177 LOGI("Created isolate");
178 isolate_ = Dart_CurrentIsolate();
179 Dart_EnterScope();
226 180
227 Log::Print("Created isolate"); 181 Dart_Handle url = CheckError(Dart_NewStringFromCString(MAINSCRIPT));
182 Dart_Handle source = LoadSourceFromFile(MAINSCRIPT);
183 CheckError(Dart_LoadScript(url, source));
228 184
229 isolate_ = Dart_CurrentIsolate(); 185 Dart_ExitScope();
230 Dart_ExitIsolate(); 186 Dart_ExitIsolate();
231 return 0; 187 return 0;
232 } 188 }
233 189
234 int VMGlue::CallSetup() { 190 int VMGlue::CallSetup() {
235 if (!initialized_script_) { 191 if (!initialized_script_) {
236 initialized_script_ = true; 192 initialized_script_ = true;
237 Log::Print("Invoking setup"); 193 LOGI("Invoking setup");
238 Dart_EnterIsolate(isolate_); 194 Dart_EnterIsolate(isolate_);
239 Dart_EnterScope(); 195 Dart_EnterScope();
240 Dart_Handle args[2]; 196 Dart_Handle args[2];
241 args[0] = Dart_NewInteger(graphics_->width()); 197 args[0] = CheckError(Dart_NewInteger(graphics_->width()));
242 args[1] = Dart_NewInteger(graphics_->height()); 198 args[1] = CheckError(Dart_NewInteger(graphics_->height()));
243 int rtn = Invoke("setup", 2, args); 199 int rtn = Invoke("setup", 2, args);
244 Dart_ExitScope(); 200 Dart_ExitScope();
245 Dart_ExitIsolate(); 201 Dart_ExitIsolate();
246 Log::Print("Done setup"); 202 LOGI("Done setup");
247 return rtn; 203 return rtn;
248 } 204 }
249 return 0; 205 return 0;
250 } 206 }
251 207
252 int VMGlue::CallUpdate() { 208 int VMGlue::CallUpdate() {
253 if (initialized_script_) { 209 if (initialized_script_) {
254 Log::Print("Invoking update"); 210 LOGI("Invoking update");
255 Dart_EnterIsolate(isolate_); 211 Dart_EnterIsolate(isolate_);
256 Dart_EnterScope(); 212 Dart_EnterScope();
257 int rtn = Invoke("update", 0, 0); 213 int rtn = Invoke("update", 0, 0);
258 Dart_ExitScope(); 214 Dart_ExitScope();
259 Dart_ExitIsolate(); 215 Dart_ExitIsolate();
260 Log::Print("Done update"); 216 LOGI("Done update");
261 return rtn; 217 return rtn;
262 } 218 }
263 return -1; 219 return -1;
264 } 220 }
265 221
266 int VMGlue::OnMotionEvent(const char* pFunction, int64_t pWhen, 222 int VMGlue::OnMotionEvent(const char* pFunction, int64_t pWhen,
267 float pMoveX, float pMoveY) { 223 float pMoveX, float pMoveY) {
268 if (initialized_script_) { 224 if (initialized_script_) {
269 Log::Print("Invoking %s", pFunction); 225 LOGI("Invoking %s", pFunction);
270 Dart_EnterIsolate(isolate_); 226 Dart_EnterIsolate(isolate_);
271 Dart_EnterScope(); 227 Dart_EnterScope();
272 Dart_Handle args[3]; 228 Dart_Handle args[3];
273 args[0] = Dart_NewInteger(pWhen); 229 args[0] = CheckError(Dart_NewInteger(pWhen));
274 args[1] = Dart_NewDouble(pMoveX); 230 args[1] = CheckError(Dart_NewDouble(pMoveX));
275 args[2] = Dart_NewDouble(pMoveY); 231 args[2] = CheckError(Dart_NewDouble(pMoveY));
276 int rtn = Invoke(pFunction, 3, args); 232 int rtn = Invoke(pFunction, 3, args);
277 Dart_ExitScope(); 233 Dart_ExitScope();
278 Dart_ExitIsolate(); 234 Dart_ExitIsolate();
279 Log::Print("Done %s", pFunction); 235 LOGI("Done %s", pFunction);
280 return rtn; 236 return rtn;
281 } 237 }
282 return -1; 238 return -1;
283 } 239 }
284 240
285 int VMGlue::OnKeyEvent(const char* function, int64_t when, int32_t flags, 241 int VMGlue::OnKeyEvent(const char* function, int64_t when, int32_t flags,
286 int32_t key_code, int32_t meta_state, int32_t repeat) { 242 int32_t key_code, int32_t meta_state, int32_t repeat) {
287 if (initialized_script_) { 243 if (initialized_script_) {
288 Log::Print("Invoking %s", function); 244 LOGI("Invoking %s", function);
289 Dart_EnterIsolate(isolate_); 245 Dart_EnterIsolate(isolate_);
290 Dart_EnterScope(); 246 Dart_EnterScope();
291 Dart_Handle args[5]; 247 Dart_Handle args[5];
292 args[0] = Dart_NewInteger(when); 248 args[0] = CheckError(Dart_NewInteger(when));
293 args[1] = Dart_NewInteger(flags); 249 args[1] = CheckError(Dart_NewInteger(flags));
294 args[2] = Dart_NewInteger(key_code); 250 args[2] = CheckError(Dart_NewInteger(key_code));
295 args[3] = Dart_NewInteger(meta_state); 251 args[3] = CheckError(Dart_NewInteger(meta_state));
296 args[4] = Dart_NewInteger(repeat); 252 args[4] = CheckError(Dart_NewInteger(repeat));
297 int rtn = Invoke(function, 5, args); 253 int rtn = Invoke(function, 5, args);
298 Dart_ExitScope(); 254 Dart_ExitScope();
299 Dart_ExitIsolate(); 255 Dart_ExitIsolate();
300 Log::Print("Done %s", function); 256 LOGI("Done %s", function);
301 return rtn; 257 return rtn;
302 } 258 }
303 return -1; 259 return -1;
304 } 260 }
305 261
306 int VMGlue::Invoke(const char* function, int argc, Dart_Handle* args) { 262 int VMGlue::Invoke(const char* function, int argc, Dart_Handle* args) {
307 Dart_Handle result; 263 Dart_Handle result;
308 264
309 Log::Print("in invoke(%s)", function); 265 LOGI("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 266
320 // Lookup the library of the root script. 267 // Lookup the library of the root script.
321 Log::Print("looking up the root library"); 268 LOGI("looking up the root library");
322 Dart_Handle library = Dart_RootLibrary(); 269 Dart_Handle library = Dart_RootLibrary();
323 if (Dart_IsNull(library)) { 270 if (Dart_IsNull(library)) {
324 return ErrorExit("Unable to find root library\n"); 271 return ErrorExit("Unable to find root library\n");
325 } 272 }
326 273
327 // Lookup and invoke the appropriate function. 274 // Lookup and invoke the appropriate function.
328 result = Dart_Invoke(library, DartUtils::NewString(function), argc, args); 275 LOGI("invoking %s", function);
276 result = Dart_Invoke(library, Dart_NewStringFromCString(function), argc, args) ;
329 277
330 if (Dart_IsError(result)) { 278 if (Dart_IsError(result)) {
331 return ErrorExit("%s\n", Dart_GetError(result)); 279 return ErrorExit("%s\n", Dart_GetError(result));
332 } 280 }
333 281
282 // TODO(vsm): I don't think we need this.
334 // Keep handling messages until the last active receive port is closed. 283 // Keep handling messages until the last active receive port is closed.
335 Log::Print("Entering Dart message loop"); 284 LOGI("Entering Dart message loop");
336 result = Dart_RunLoop(); 285 result = Dart_RunLoop();
337 if (Dart_IsError(result)) { 286 if (Dart_IsError(result)) {
338 return ErrorExit("%s\n", Dart_GetError(result)); 287 return ErrorExit("%s\n", Dart_GetError(result));
339 } 288 }
340 289
341 Log::Print("out invoke"); 290 LOGI("out invoke");
342 return 0; 291 return 0;
343 } 292 }
344 293
345 void VMGlue::FinishMainIsolate() { 294 void VMGlue::FinishMainIsolate() {
346 Log::Print("Finish main isolate"); 295 LOGI("Finish main isolate");
347 Dart_EnterIsolate(isolate_); 296 Dart_EnterIsolate(isolate_);
348 // Shutdown the isolate. 297 // Shutdown the isolate.
349 Dart_ShutdownIsolate(); 298 Dart_ShutdownIsolate();
350 // Terminate process exit-code handler.
351 Process::TerminateExitCodeHandler();
352 isolate_ = NULL; 299 isolate_ = NULL;
353 initialized_script_ = false; 300 initialized_script_ = false;
354 } 301 }
355
OLDNEW
« no previous file with comments | « samples/android_sample/jni/vm_glue.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698