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

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: Added copyright 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
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 if (strcmp(url, "android_extension.dart") == 0) {
gram 2012/12/04 22:03:42 Can you add a TODO here that this should not be ha
vsm 2012/12/04 23:03:08 TODO added. We'll need a table somewhere to assoc
63 Dart_Handle source = VMGlue::LoadSourceFromFile("/data/data/com.google.dartn dk/app_dart/android_extension.dart");
64 Dart_Handle library = CheckError(Dart_LoadLibrary(urlHandle, source));
65 CheckError(Dart_SetNativeResolver(library, ResolveName));
66 return library;
67 }
68 LOGE("UNIMPLEMENTED: load library %s\n", url);
69 return NULL;
70 }
71
106 // Returns true on success, false on failure. 72 // Returns true on success, false on failure.
107 bool VMGlue::CreateIsolateAndSetupHelper(const char* script_uri, 73 bool VMGlue::CreateIsolateAndSetupHelper(const char* script_uri,
108 const char* main, 74 const char* main,
109 void* data, 75 void* data,
110 char** error) { 76 char** error) {
111 Log::Print("Creating isolate %s, %s", script_uri, main); 77 LOGI("Creating isolate %s, %s", script_uri, main);
112 Dart_Isolate isolate = 78 Dart_Isolate isolate =
113 Dart_CreateIsolate(script_uri, main, snapshot_buffer, data, error); 79 Dart_CreateIsolate(script_uri, main, NULL, data, error);
114 if (isolate == NULL) { 80 if (isolate == NULL) {
115 Log::PrintErr("Couldn't create isolate: %s", error); 81 LOGE("Couldn't create isolate: %s", error);
116 return false; 82 return false;
117 } 83 }
118 84
119 Log::Print("Entering scope"); 85 LOGI("Entering scope");
120 Dart_EnterScope(); 86 Dart_EnterScope();
121 87
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. 88 // Set up the library tag handler for this isolate.
129 Log::Print("Setting up library tag handler"); 89 LOGI("Setting up library tag handler");
130 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); 90 Dart_Handle result = CheckError(Dart_SetLibraryTagHandler(LibraryTagHandler));
131 CHECK_RESULT(result); 91 CHECK_RESULT(result);
132 92
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(); 93 Dart_ExitScope();
164 return true; 94 return true;
165 } 95 }
166 96
167 bool VMGlue::CreateIsolateAndSetup(const char* script_uri, 97 bool VMGlue::CreateIsolateAndSetup(const char* script_uri,
168 const char* main, 98 const char* main,
169 void* data, char** error) { 99 void* data, char** error) {
170 return CreateIsolateAndSetupHelper(script_uri, 100 return CreateIsolateAndSetupHelper(script_uri,
171 main, 101 main,
172 new IsolateData(), 102 data,
173 error); 103 error);
174 } 104 }
175 105
176 #define VMHOSTNAME "android_dart_host" 106 #define VMHOSTNAME "android_dart_host"
177 #define MAINSCRIPT "/data/data/com.google.dartndk/app_dart/main.dart" 107 #define MAINSCRIPT "/data/data/com.google.dartndk/app_dart/main.dart"
178 108
179 void VMGlue::ShutdownIsolate(void* callback_data) { 109 const char* VM_FLAGS[] = {
180 IsolateData* isolate_data = reinterpret_cast<IsolateData*>(callback_data); 110 "--enable_type_checks",
181 EventHandler* handler = isolate_data->event_handler; 111 "--trace_isolates",
182 if (handler != NULL) handler->Shutdown(); 112 "--trace_natives",
183 delete isolate_data; 113 };
184 }
185 114
186 int VMGlue::InitializeVM() { 115 int VMGlue::InitializeVM() {
187 // Perform platform specific initialization. 116 // We need the next call to get Dart_Initialize not to bail early.
188 Log::Print("Initializing platform"); 117 LOGI("Setting VM Options");
189 if (!Platform::Initialize()) { 118 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 119
198 // Initialize the Dart VM, providing the callbacks to use for 120 // Initialize the Dart VM, providing the callbacks to use for
199 // creating and shutting down isolates. 121 // creating and shutting down isolates.
200 Log::Print("Initializing Dart"); 122 LOGI("Initializing Dart");
201 if (!Dart_Initialize(CreateIsolateAndSetup, 123 if (!Dart_Initialize(CreateIsolateAndSetup,
202 NULL, 124 NULL,
203 NULL, 125 NULL,
204 ShutdownIsolate)) { 126 NULL)) {
205 Log::PrintErr("VM initialization failed\n"); 127 LOGE("VM initialization failed\n");
206 return -1; 128 return -1;
207 } 129 }
208 DartUtils::SetOriginalWorkingDirectory();
209 initialized_vm_ = true; 130 initialized_vm_ = true;
131
210 return 0; 132 return 0;
211 } 133 }
212 134
135 Dart_Handle VMGlue::LoadSourceFromFile(const char* url) {
136 FILE* file = fopen(url, "r");
137 if (file == NULL) {
138 LOGE("Main script not found at: %s\n", url);
139 return NULL;
140 }
141
142 struct stat sb;
143 int fd = fileno(file);
144 fstat(fd, &sb);
145 int length = sb.st_size;
146 LOGI("Entry file %s is %d bytes.\n", url, length);
147
148 char* buffer = reinterpret_cast<char *>(malloc((length + 1) * sizeof(char)));
149 if (read(fd, buffer, length) < 0) {
150 LOGE("Could not read script %s.\n", url);
151 return NULL;
152 }
153 buffer[length] = 0;
154 fclose(file);
155
156 Dart_Handle contents = CheckError(Dart_NewStringFromCString(buffer));
157 free(buffer);
158 return contents;
159 }
160
213 int VMGlue::StartMainIsolate() { 161 int VMGlue::StartMainIsolate() {
214 if (!initialized_vm_) { 162 if (!initialized_vm_) {
215 int rtn = InitializeVM(); 163 int rtn = InitializeVM();
216 if (rtn != 0) return rtn; 164 if (rtn != 0) return rtn;
217 } 165 }
218 166
219 // Create an isolate and loads up the application script. 167 // Create an isolate and loads up the application script.
220 char* error = NULL; 168 char* error = NULL;
221 if (!CreateIsolateAndSetup(MAINSCRIPT, "main", NULL, &error)) { 169 if (!CreateIsolateAndSetup(MAINSCRIPT, "main", NULL, &error)) {
222 Log::PrintErr("CreateIsolateAndSetup: %s\n", error); 170 LOGE("CreateIsolateAndSetup: %s\n", error);
223 free(error); 171 free(error);
224 return -1; 172 return -1;
225 } 173 }
174 LOGI("Created isolate");
175 isolate_ = Dart_CurrentIsolate();
176 Dart_EnterScope();
226 177
227 Log::Print("Created isolate"); 178 Dart_Handle url = CheckError(Dart_NewStringFromCString(MAINSCRIPT));
179 Dart_Handle source = LoadSourceFromFile(MAINSCRIPT);
180 CheckError(Dart_LoadScript(url, source));
228 181
229 isolate_ = Dart_CurrentIsolate(); 182 Dart_ExitScope();
230 Dart_ExitIsolate(); 183 Dart_ExitIsolate();
231 return 0; 184 return 0;
232 } 185 }
233 186
234 int VMGlue::CallSetup() { 187 int VMGlue::CallSetup() {
235 if (!initialized_script_) { 188 if (!initialized_script_) {
236 initialized_script_ = true; 189 initialized_script_ = true;
237 Log::Print("Invoking setup"); 190 LOGI("Invoking setup");
238 Dart_EnterIsolate(isolate_); 191 Dart_EnterIsolate(isolate_);
239 Dart_EnterScope(); 192 Dart_EnterScope();
240 Dart_Handle args[2]; 193 Dart_Handle args[2];
241 args[0] = Dart_NewInteger(graphics_->width()); 194 args[0] = CheckError(Dart_NewInteger(graphics_->width()));
242 args[1] = Dart_NewInteger(graphics_->height()); 195 args[1] = CheckError(Dart_NewInteger(graphics_->height()));
243 int rtn = Invoke("setup", 2, args); 196 int rtn = Invoke("setup", 2, args);
244 Dart_ExitScope(); 197 Dart_ExitScope();
245 Dart_ExitIsolate(); 198 Dart_ExitIsolate();
246 Log::Print("Done setup"); 199 LOGI("Done setup");
247 return rtn; 200 return rtn;
248 } 201 }
249 return 0; 202 return 0;
250 } 203 }
251 204
252 int VMGlue::CallUpdate() { 205 int VMGlue::CallUpdate() {
253 if (initialized_script_) { 206 if (initialized_script_) {
254 Log::Print("Invoking update"); 207 LOGI("Invoking update");
255 Dart_EnterIsolate(isolate_); 208 Dart_EnterIsolate(isolate_);
256 Dart_EnterScope(); 209 Dart_EnterScope();
257 int rtn = Invoke("update", 0, 0); 210 int rtn = Invoke("update", 0, 0);
258 Dart_ExitScope(); 211 Dart_ExitScope();
259 Dart_ExitIsolate(); 212 Dart_ExitIsolate();
260 Log::Print("Done update"); 213 LOGI("Done update");
261 return rtn; 214 return rtn;
262 } 215 }
263 return -1; 216 return -1;
264 } 217 }
265 218
266 int VMGlue::OnMotionEvent(const char* pFunction, int64_t pWhen, 219 int VMGlue::OnMotionEvent(const char* pFunction, int64_t pWhen,
267 float pMoveX, float pMoveY) { 220 float pMoveX, float pMoveY) {
268 if (initialized_script_) { 221 if (initialized_script_) {
269 Log::Print("Invoking %s", pFunction); 222 LOGI("Invoking %s", pFunction);
270 Dart_EnterIsolate(isolate_); 223 Dart_EnterIsolate(isolate_);
271 Dart_EnterScope(); 224 Dart_EnterScope();
272 Dart_Handle args[3]; 225 Dart_Handle args[3];
273 args[0] = Dart_NewInteger(pWhen); 226 args[0] = CheckError(Dart_NewInteger(pWhen));
274 args[1] = Dart_NewDouble(pMoveX); 227 args[1] = CheckError(Dart_NewDouble(pMoveX));
275 args[2] = Dart_NewDouble(pMoveY); 228 args[2] = CheckError(Dart_NewDouble(pMoveY));
276 int rtn = Invoke(pFunction, 3, args); 229 int rtn = Invoke(pFunction, 3, args);
277 Dart_ExitScope(); 230 Dart_ExitScope();
278 Dart_ExitIsolate(); 231 Dart_ExitIsolate();
279 Log::Print("Done %s", pFunction); 232 LOGI("Done %s", pFunction);
280 return rtn; 233 return rtn;
281 } 234 }
282 return -1; 235 return -1;
283 } 236 }
284 237
285 int VMGlue::OnKeyEvent(const char* function, int64_t when, int32_t flags, 238 int VMGlue::OnKeyEvent(const char* function, int64_t when, int32_t flags,
286 int32_t key_code, int32_t meta_state, int32_t repeat) { 239 int32_t key_code, int32_t meta_state, int32_t repeat) {
287 if (initialized_script_) { 240 if (initialized_script_) {
288 Log::Print("Invoking %s", function); 241 LOGI("Invoking %s", function);
289 Dart_EnterIsolate(isolate_); 242 Dart_EnterIsolate(isolate_);
290 Dart_EnterScope(); 243 Dart_EnterScope();
291 Dart_Handle args[5]; 244 Dart_Handle args[5];
292 args[0] = Dart_NewInteger(when); 245 args[0] = CheckError(Dart_NewInteger(when));
293 args[1] = Dart_NewInteger(flags); 246 args[1] = CheckError(Dart_NewInteger(flags));
294 args[2] = Dart_NewInteger(key_code); 247 args[2] = CheckError(Dart_NewInteger(key_code));
295 args[3] = Dart_NewInteger(meta_state); 248 args[3] = CheckError(Dart_NewInteger(meta_state));
296 args[4] = Dart_NewInteger(repeat); 249 args[4] = CheckError(Dart_NewInteger(repeat));
297 int rtn = Invoke(function, 5, args); 250 int rtn = Invoke(function, 5, args);
298 Dart_ExitScope(); 251 Dart_ExitScope();
299 Dart_ExitIsolate(); 252 Dart_ExitIsolate();
300 Log::Print("Done %s", function); 253 LOGI("Done %s", function);
301 return rtn; 254 return rtn;
302 } 255 }
303 return -1; 256 return -1;
304 } 257 }
305 258
306 int VMGlue::Invoke(const char* function, int argc, Dart_Handle* args) { 259 int VMGlue::Invoke(const char* function, int argc, Dart_Handle* args) {
307 Dart_Handle result; 260 Dart_Handle result;
308 261
309 Log::Print("in invoke(%s)", function); 262 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 263
320 // Lookup the library of the root script. 264 // Lookup the library of the root script.
321 Log::Print("looking up the root library"); 265 LOGI("looking up the root library");
322 Dart_Handle library = Dart_RootLibrary(); 266 Dart_Handle library = Dart_RootLibrary();
323 if (Dart_IsNull(library)) { 267 if (Dart_IsNull(library)) {
324 return ErrorExit("Unable to find root library\n"); 268 return ErrorExit("Unable to find root library\n");
325 } 269 }
326 270
327 // Lookup and invoke the appropriate function. 271 // Lookup and invoke the appropriate function.
328 result = Dart_Invoke(library, DartUtils::NewString(function), argc, args); 272 LOGI("invoking %s", function);
273 result = Dart_Invoke(library, Dart_NewStringFromCString(function), argc, args) ;
329 274
330 if (Dart_IsError(result)) { 275 if (Dart_IsError(result)) {
331 return ErrorExit("%s\n", Dart_GetError(result)); 276 return ErrorExit("%s\n", Dart_GetError(result));
332 } 277 }
333 278
279 // TODO(vsm): I don't think we need this.
334 // Keep handling messages until the last active receive port is closed. 280 // Keep handling messages until the last active receive port is closed.
335 Log::Print("Entering Dart message loop"); 281 LOGI("Entering Dart message loop");
336 result = Dart_RunLoop(); 282 result = Dart_RunLoop();
337 if (Dart_IsError(result)) { 283 if (Dart_IsError(result)) {
338 return ErrorExit("%s\n", Dart_GetError(result)); 284 return ErrorExit("%s\n", Dart_GetError(result));
339 } 285 }
340 286
341 Log::Print("out invoke"); 287 LOGI("out invoke");
342 return 0; 288 return 0;
343 } 289 }
344 290
345 void VMGlue::FinishMainIsolate() { 291 void VMGlue::FinishMainIsolate() {
346 Log::Print("Finish main isolate"); 292 LOGI("Finish main isolate");
347 Dart_EnterIsolate(isolate_); 293 Dart_EnterIsolate(isolate_);
348 // Shutdown the isolate. 294 // Shutdown the isolate.
349 Dart_ShutdownIsolate(); 295 Dart_ShutdownIsolate();
350 // Terminate process exit-code handler.
351 Process::TerminateExitCodeHandler();
352 isolate_ = NULL; 296 isolate_ = NULL;
353 initialized_script_ = false; 297 initialized_script_ = false;
354 } 298 }
355
OLDNEW
« samples/android_sample/jni/timer.h ('K') | « 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