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

Side by Side Diff: bin/main.cc

Issue 8673002: - Refactor the isolate callback mechanism to also include creation of the (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/runtime/
Patch Set: '' Created 9 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 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 <stdlib.h> 5 #include <stdlib.h>
6 #include <string.h> 6 #include <string.h>
7 #include <stdio.h> 7 #include <stdio.h>
8 8
9 #include "include/dart_api.h" 9 #include "include/dart_api.h"
10 10
11 #include "bin/builtin.h" 11 #include "bin/builtin.h"
12 #include "bin/dartutils.h" 12 #include "bin/dartutils.h"
13 #include "bin/file.h" 13 #include "bin/file.h"
14 #include "bin/globals.h" 14 #include "bin/globals.h"
15 #include "bin/platform.h" 15 #include "bin/platform.h"
16 16
17 // snapshot_buffer points to a snapshot if we link in a snapshot otherwise 17 // snapshot_buffer points to a snapshot if we link in a snapshot otherwise
18 // it is initialized to NULL. 18 // it is initialized to NULL.
19 extern const uint8_t* snapshot_buffer; 19 extern const uint8_t* snapshot_buffer;
20 20
21 21
22 // Global state that stores a pointer to the application script file.
23 static char* canonical_script_name = NULL;
24
25
22 // Global state that indicates whether pprof symbol information is 26 // Global state that indicates whether pprof symbol information is
23 // to be generated or not. 27 // to be generated or not.
24 static const char* generate_pprof_symbols_filename = NULL; 28 static const char* generate_pprof_symbols_filename = NULL;
25 29
26 30
27 static bool IsValidFlag(const char* name, 31 static bool IsValidFlag(const char* name,
28 const char* prefix, 32 const char* prefix,
29 intptr_t prefix_length) { 33 intptr_t prefix_length) {
30 intptr_t name_length = strlen(name); 34 intptr_t name_length = strlen(name);
31 return ((name_length > prefix_length) && 35 return ((name_length > prefix_length) &&
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 return url; 184 return url;
181 } 185 }
182 // Create a canonical path based on the including library and current url. 186 // Create a canonical path based on the including library and current url.
183 return DartUtils::CanonicalizeURL(NULL, library, url_string); 187 return DartUtils::CanonicalizeURL(NULL, library, url_string);
184 } 188 }
185 if (is_dart_scheme_url) { 189 if (is_dart_scheme_url) {
186 return Dart_Error("Do not know how to load '%s'", url_string); 190 return Dart_Error("Do not know how to load '%s'", url_string);
187 } 191 }
188 result = DartUtils::LoadSource(NULL, library, url, tag, url_string); 192 result = DartUtils::LoadSource(NULL, library, url, tag, url_string);
189 if (!Dart_IsError(result) && (tag == kImportTag)) { 193 if (!Dart_IsError(result) && (tag == kImportTag)) {
190 Builtin_ImportLibrary(result); 194 Builtin::ImportLibrary(result);
191 } 195 }
192 return result; 196 return result;
193 } 197 }
194 198
195 199
196 static Dart_Handle LoadScript(const char* script_name) { 200 static Dart_Handle LoadScript(const char* script_name) {
197 Dart_Handle source = DartUtils::ReadStringFromFile(script_name); 201 Dart_Handle source = DartUtils::ReadStringFromFile(script_name);
198 if (Dart_IsError(source)) { 202 if (Dart_IsError(source)) {
199 return source; 203 return source;
200 } 204 }
201 Dart_Handle url = Dart_NewString(script_name); 205 Dart_Handle url = Dart_NewString(script_name);
202 206
203 return Dart_LoadScript(url, source, LibraryTagHandler); 207 return Dart_LoadScript(url, source, LibraryTagHandler);
204 } 208 }
205 209
206 210
207 static void* MainIsolateInitCallback(void* data) { 211 static bool CreateIsolateAndSetup(void* data, Dart_ErrorBuffer error) {
208 const char* script_name = reinterpret_cast<const char*>(data); 212 Dart_Isolate isolate = Dart_CreateIsolate(snapshot_buffer, data, error);
213 if (isolate == NULL) {
214 return false;
215 }
216
209 Dart_Handle library; 217 Dart_Handle library;
210 Dart_EnterScope(); 218 Dart_EnterScope();
211 219
212 // Load the specified script. 220 // Load the specified application script into the newly created isolate.
213 library = LoadScript(script_name); 221 library = LoadScript(canonical_script_name);
214 if (Dart_IsError(library)) { 222 if (Dart_IsError(library)) {
215 const char* err_msg = Dart_GetError(library); 223 snprintf(const_cast<char*>(error.buffer), error.length,
216 fprintf(stderr, "Errors encountered while loading script: %s\n", err_msg); 224 "%s", Dart_GetError(library));
217 Dart_ExitScope(); 225 Dart_ExitScope();
218 exit(255); 226 Dart_ShutdownIsolate();
227 return false;
219 } 228 }
220 if (!Dart_IsLibrary(library)) { 229 if (!Dart_IsLibrary(library)) {
221 fprintf(stderr, 230 snprintf(const_cast<char*>(error.buffer), error.length,
222 "Expected a library when loading script: %s", 231 "Expected a library when loading script: %s",
223 script_name); 232 canonical_script_name);
224 Dart_ExitScope(); 233 Dart_ExitScope();
225 exit(255); 234 Dart_ShutdownIsolate();
235 return false;
226 } 236 }
227 Builtin_ImportLibrary(library); // Import builtin library. 237 Builtin::ImportLibrary(library); // Implicitly import builtin into app.
228 238 if (snapshot_buffer != NULL) {
239 // Setup the native resolver as the snapshot does not carry it.
240 Builtin::SetNativeResolver();
241 }
229 Dart_ExitScope(); 242 Dart_ExitScope();
230 return data; 243 return true;
231 } 244 }
232 245
233 246
234 static void PrintUsage() { 247 static void PrintUsage() {
235 fprintf(stderr, 248 fprintf(stderr,
236 "dart [<vm-flags>] <dart-script-file> [<dart-options>]\n"); 249 "dart [<vm-flags>] <dart-script-file> [<dart-options>]\n");
237 } 250 }
238 251
239 252
240 static bool HasCompileAll(const CommandLineOptions& options) { 253 static bool HasCompileAll(const CommandLineOptions& options) {
(...skipping 23 matching lines...) Expand all
264 &script_name, 277 &script_name,
265 &dart_options) < 0) { 278 &dart_options) < 0) {
266 PrintUsage(); 279 PrintUsage();
267 return 255; 280 return 255;
268 } 281 }
269 282
270 // Initialize the Dart VM (TODO(asiva) - remove const_cast once 283 // Initialize the Dart VM (TODO(asiva) - remove const_cast once
271 // dart API is fixed to take a const char** in Dart_Initialize). 284 // dart API is fixed to take a const char** in Dart_Initialize).
272 Dart_Initialize(vm_options.count(), 285 Dart_Initialize(vm_options.count(),
273 vm_options.arguments(), 286 vm_options.arguments(),
274 MainIsolateInitCallback); 287 CreateIsolateAndSetup);
275 288
276 // Create an isolate. As a side effect, MainIsolateInitCallback 289 canonical_script_name = File::GetCanonicalPath(script_name);
277 // gets called, which loads the scripts and libraries.
278 char* canonical_script_name = File::GetCanonicalPath(script_name);
279 if (canonical_script_name == NULL) { 290 if (canonical_script_name == NULL) {
280 fprintf(stderr, "Unable to find '%s'\n", script_name); 291 fprintf(stderr, "Unable to find '%s'\n", script_name);
281 return 255; // Indicates we encountered an error. 292 return 255; // Indicates we encountered an error.
282 } 293 }
283 Dart_Isolate isolate = Dart_CreateIsolate(snapshot_buffer, 294
284 canonical_script_name); 295 // Call CreateIsolateAndSetup which creates an isolate and loads up
285 if (isolate == NULL) { 296 // the specified application script.
297 Dart_ErrorBuffer error;
298 static const int kErrorMsgLength = 256;
299 error.buffer = reinterpret_cast<char*>(malloc(kErrorMsgLength));
300 error.length = kErrorMsgLength;
301 if (!CreateIsolateAndSetup(NULL, error)) {
302 fprintf(stderr, "%s\n", error.buffer);
286 free(canonical_script_name); 303 free(canonical_script_name);
287 return 255; 304 free(error.buffer);
305 return 255; // Indicates we encountered an error.
288 } 306 }
307 free(error.buffer);
308
309 Dart_Isolate isolate = Dart_CurrentIsolate();
310 ASSERT(isolate != NULL);
311 Dart_Handle result;
289 312
290 Dart_EnterScope(); 313 Dart_EnterScope();
291 314
292 if (snapshot_buffer != NULL) {
293 // Setup the native resolver as the snapshot does not carry it.
294 Builtin_SetNativeResolver();
295 }
296
297 if (HasCompileAll(vm_options)) { 315 if (HasCompileAll(vm_options)) {
298 Dart_Handle result = Dart_CompileAll(); 316 result = Dart_CompileAll();
299 if (Dart_IsError(result)) { 317 if (Dart_IsError(result)) {
300 fprintf(stderr, "%s\n", Dart_GetError(result)); 318 fprintf(stderr, "%s\n", Dart_GetError(result));
301 Dart_ExitScope(); 319 Dart_ExitScope();
302 Dart_ShutdownIsolate(); 320 Dart_ShutdownIsolate();
303 free(canonical_script_name); 321 free(canonical_script_name);
304 return 255; // Indicates we encountered an error. 322 return 255; // Indicates we encountered an error.
305 } 323 }
306 } 324 }
307 325
308 // Create a dart options object that can be accessed from dart code. 326 // Create a dart options object that can be accessed from dart code.
309 Dart_Handle options_result = SetupRuntimeOptions(&dart_options); 327 Dart_Handle options_result = SetupRuntimeOptions(&dart_options);
310 if (Dart_IsError(options_result)) { 328 if (Dart_IsError(options_result)) {
311 fprintf(stderr, "%s\n", Dart_GetError(options_result)); 329 fprintf(stderr, "%s\n", Dart_GetError(options_result));
312 Dart_ExitScope(); 330 Dart_ExitScope();
313 Dart_ShutdownIsolate(); 331 Dart_ShutdownIsolate();
314 free(canonical_script_name); 332 free(canonical_script_name);
315 return 255; // Indicates we encountered an error. 333 return 255; // Indicates we encountered an error.
316 } 334 }
317 335
318 // Lookup and invoke the top level main function. 336 // Lookup and invoke the top level main function.
319 Dart_Handle script_url = Dart_NewString(canonical_script_name); 337 Dart_Handle script_url = Dart_NewString(canonical_script_name);
320 Dart_Handle library = Dart_LookupLibrary(script_url); 338 Dart_Handle library = Dart_LookupLibrary(script_url);
321 if (Dart_IsError(library)) { 339 if (Dart_IsError(library)) {
322 fprintf(stderr, "%s\n", Dart_GetError(library)); 340 fprintf(stderr, "%s\n", Dart_GetError(library));
323 Dart_ExitScope(); 341 Dart_ExitScope();
324 Dart_ShutdownIsolate(); 342 Dart_ShutdownIsolate();
325 free(canonical_script_name); 343 free(canonical_script_name);
326 return 255; // Indicates we encountered an error. 344 return 255; // Indicates we encountered an error.
327 } 345 }
328 Dart_Handle result = Dart_InvokeStatic(library, 346 result = Dart_InvokeStatic(library,
329 Dart_NewString(""), 347 Dart_NewString(""),
330 Dart_NewString("main"), 348 Dart_NewString("main"),
331 0, 349 0,
332 NULL); 350 NULL);
333 if (Dart_IsError(result)) { 351 if (Dart_IsError(result)) {
334 fprintf(stderr, "%s\n", Dart_GetError(result)); 352 fprintf(stderr, "%s\n", Dart_GetError(result));
335 Dart_ExitScope(); 353 Dart_ExitScope();
336 Dart_ShutdownIsolate(); 354 Dart_ShutdownIsolate();
337 free(canonical_script_name); 355 free(canonical_script_name);
338 return 255; // Indicates we encountered an error. 356 return 255; // Indicates we encountered an error.
339 } 357 }
340 // Keep handling messages until the last active receive port is closed. 358 // Keep handling messages until the last active receive port is closed.
341 result = Dart_RunLoop(); 359 result = Dart_RunLoop();
342 if (Dart_IsError(result)) { 360 if (Dart_IsError(result)) {
343 fprintf(stderr, "%s\n", Dart_GetError(result)); 361 fprintf(stderr, "%s\n", Dart_GetError(result));
344 Dart_ExitScope(); 362 Dart_ExitScope();
345 Dart_ShutdownIsolate(); 363 Dart_ShutdownIsolate();
346 free(canonical_script_name); 364 free(canonical_script_name);
347 return 255; // Indicates we encountered an error. 365 return 255; // Indicates we encountered an error.
348 } 366 }
349 free(canonical_script_name); 367 free(canonical_script_name);
350 Dart_ExitScope(); 368 Dart_ExitScope();
351 // Dump symbol information for the profiler. 369 // Dump symbol information for the profiler.
352 DumpPprofSymbolInfo(); 370 DumpPprofSymbolInfo();
353 // Shutdown the isolate. 371 // Shutdown the isolate.
354 Dart_ShutdownIsolate(); 372 Dart_ShutdownIsolate();
355 return 0; 373 return 0;
356 } 374 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698