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

Side by Side Diff: runtime/bin/dartutils.cc

Issue 10871092: Added option --script-snapshot in gen_snapshot to generate the snapshot of a scipt into the file. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 3 months 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 | « runtime/bin/dartutils.h ('k') | runtime/bin/gen_snapshot.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 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 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 "bin/dartutils.h" 5 #include "bin/dartutils.h"
6 6
7 #include "bin/extensions.h"
8 #include "bin/directory.h"
7 #include "bin/file.h" 9 #include "bin/file.h"
8 #include "include/dart_api.h" 10 #include "include/dart_api.h"
9 #include "platform/assert.h" 11 #include "platform/assert.h"
10 #include "platform/globals.h" 12 #include "platform/globals.h"
11 13
14 const char* DartUtils::original_working_directory = NULL;
12 const char* DartUtils::kDartScheme = "dart:"; 15 const char* DartUtils::kDartScheme = "dart:";
13 const char* DartUtils::kDartExtensionScheme = "dart-ext:"; 16 const char* DartUtils::kDartExtensionScheme = "dart-ext:";
14 const char* DartUtils::kBuiltinLibURL = "dart:builtin"; 17 const char* DartUtils::kBuiltinLibURL = "dart:builtin";
15 const char* DartUtils::kCoreLibURL = "dart:core"; 18 const char* DartUtils::kCoreLibURL = "dart:core";
16 const char* DartUtils::kCoreImplLibURL = "dart:coreimpl"; 19 const char* DartUtils::kCoreImplLibURL = "dart:coreimpl";
17 const char* DartUtils::kCryptoLibURL = "dart:crypto"; 20 const char* DartUtils::kCryptoLibURL = "dart:crypto";
18 const char* DartUtils::kIOLibURL = "dart:io"; 21 const char* DartUtils::kIOLibURL = "dart:io";
19 const char* DartUtils::kJsonLibURL = "dart:json"; 22 const char* DartUtils::kJsonLibURL = "dart:json";
20 const char* DartUtils::kUriLibURL = "dart:uri"; 23 const char* DartUtils::kUriLibURL = "dart:uri";
21 const char* DartUtils::kUtfLibURL = "dart:utf"; 24 const char* DartUtils::kUtfLibURL = "dart:utf";
22 const char* DartUtils::kIsolateLibURL = "dart:isolate"; 25 const char* DartUtils::kIsolateLibURL = "dart:isolate";
23 const char* DartUtils::kWebLibURL = "dart:web"; 26 const char* DartUtils::kWebLibURL = "dart:web";
24 27
25 28
26 const char* DartUtils::kIdFieldName = "_id"; 29 const char* DartUtils::kIdFieldName = "_id";
27 30
28 31
32 static bool IsWindowsHost() {
33 #if defined(TARGET_OS_WINDOWS)
34 return true;
35 #else // defined(TARGET_OS_WINDOWS)
36 return false;
37 #endif // defined(TARGET_OS_WINDOWS)
38 }
39
40
29 static const char* MapLibraryUrl(CommandLineOptions* url_mapping, 41 static const char* MapLibraryUrl(CommandLineOptions* url_mapping,
30 const char* url_string) { 42 const char* url_string) {
31 ASSERT(url_mapping != NULL); 43 ASSERT(url_mapping != NULL);
32 // We need to check if the passed in url is found in the url_mapping array, 44 // We need to check if the passed in url is found in the url_mapping array,
33 // in that case use the mapped entry. 45 // in that case use the mapped entry.
34 int len = strlen(url_string); 46 int len = strlen(url_string);
35 for (int idx = 0; idx < url_mapping->count(); idx++) { 47 for (int idx = 0; idx < url_mapping->count(); idx++) {
36 const char* url_name = url_mapping->GetArgument(idx); 48 const char* url_name = url_mapping->GetArgument(idx);
37 if (!strncmp(url_string, url_name, len) && (url_name[len] == ',')) { 49 if (!strncmp(url_string, url_name, len) && (url_name[len] == ',')) {
38 const char* url_mapped_name = url_name + len + 1; 50 const char* url_mapped_name = url_name + len + 1;
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 return Dart_Error("Unable to fully read contents"); 225 return Dart_Error("Unable to fully read contents");
214 } 226 }
215 text_buffer[len] = '\0'; 227 text_buffer[len] = '\0';
216 delete file; 228 delete file;
217 Dart_Handle str = Dart_NewString(text_buffer); 229 Dart_Handle str = Dart_NewString(text_buffer);
218 free(text_buffer); 230 free(text_buffer);
219 return str; 231 return str;
220 } 232 }
221 233
222 234
235 static Dart_Handle ResolveScriptUri(Dart_Handle script_uri,
236 Dart_Handle builtin_lib) {
237 const int kNumArgs = 3;
238 Dart_Handle dart_args[kNumArgs];
239 dart_args[0] = Dart_NewString(DartUtils::original_working_directory);
240 dart_args[1] = script_uri;
241 dart_args[2] = (IsWindowsHost() ? Dart_True() : Dart_False());
242 return Dart_Invoke(
243 builtin_lib, Dart_NewString("_resolveScriptUri"), kNumArgs, dart_args);
244 }
245
246
247 static Dart_Handle FilePathFromUri(Dart_Handle script_uri,
248 Dart_Handle builtin_lib) {
249 const int kNumArgs = 2;
250 Dart_Handle dart_args[kNumArgs];
251 dart_args[0] = script_uri;
252 dart_args[1] = (IsWindowsHost() ? Dart_True() : Dart_False());
253 Dart_Handle script_path = Dart_Invoke(
254 builtin_lib, Dart_NewString("_filePathFromUri"), kNumArgs, dart_args);
255 return script_path;
256 }
257
258
259 Dart_Handle DartUtils::LibraryTagHandler(Dart_LibraryTag tag,
260 Dart_Handle library,
261 Dart_Handle url) {
262 if (!Dart_IsLibrary(library)) {
263 return Dart_Error("not a library");
264 }
265 if (!Dart_IsString8(url)) {
266 return Dart_Error("url is not a string");
267 }
268 const char* url_string = NULL;
269 Dart_Handle result = Dart_StringToCString(url, &url_string);
270 if (Dart_IsError(result)) {
271 return result;
272 }
273 bool is_dart_scheme_url = DartUtils::IsDartSchemeURL(url_string);
274 bool is_dart_extension_url = DartUtils::IsDartExtensionSchemeURL(url_string);
275 if (tag == kCanonicalizeUrl) {
276 // If this is a Dart Scheme URL then it is not modified as it will be
277 // handled by the VM internally.
278 if (is_dart_scheme_url) {
279 return url;
280 }
281 // Resolve the url within the context of the library's URL.
282 Dart_Handle builtin_lib = Builtin::LoadLibrary(Builtin::kBuiltinLibrary);
283 Dart_Handle library_url = Dart_LibraryUrl(library);
284 if (Dart_IsError(library_url)) {
285 return library_url;
286 }
287 const int kNumArgs = 2;
288 Dart_Handle dart_args[kNumArgs];
289 dart_args[0] = library_url;
290 dart_args[1] = url;
291 return Dart_Invoke(
292 builtin_lib, Dart_NewString("_resolveUri"), kNumArgs, dart_args);
293 }
294 if (is_dart_scheme_url) {
295 ASSERT(tag == kImportTag);
296 // Handle imports of other built-in libraries present in the SDK.
297 Builtin::BuiltinLibraryId id;
298 if (DartUtils::IsDartCryptoLibURL(url_string)) {
299 id = Builtin::kCryptoLibrary;
300 } else if (DartUtils::IsDartIOLibURL(url_string)) {
301 id = Builtin::kIOLibrary;
302 } else if (DartUtils::IsDartJsonLibURL(url_string)) {
303 id = Builtin::kJsonLibrary;
304 } else if (DartUtils::IsDartUriLibURL(url_string)) {
305 id = Builtin::kUriLibrary;
306 } else if (DartUtils::IsDartUtfLibURL(url_string)) {
307 id = Builtin::kUtfLibrary;
308 } else if (DartUtils::IsDartWebLibURL(url_string)) {
309 id = Builtin::kWebLibrary;
310 } else {
311 return Dart_Error("Do not know how to load '%s'", url_string);
312 }
313 return Builtin::LoadLibrary(id);
314 } else {
315 // Get the file path out of the url.
316 Dart_Handle builtin_lib = Builtin::LoadLibrary(Builtin::kBuiltinLibrary);
317 Dart_Handle file_path = FilePathFromUri(url, builtin_lib);
318 if (Dart_IsError(file_path)) {
319 return file_path;
320 }
321 Dart_StringToCString(file_path, &url_string);
322 }
323 if (is_dart_extension_url) {
324 if (tag != kImportTag) {
325 return Dart_Error("Dart extensions must use import: '%s'", url_string);
326 }
327 return Extensions::LoadExtension(url_string, library);
328 }
329 result = DartUtils::LoadSource(NULL,
330 library,
331 url,
332 tag,
333 url_string);
334 return result;
335 }
336
337
338 static Dart_Handle ReadSource(Dart_Handle script_uri,
339 Dart_Handle builtin_lib) {
340 Dart_Handle script_path = FilePathFromUri(script_uri, builtin_lib);
341 if (Dart_IsError(script_path)) {
342 return script_path;
343 }
344 const char* script_path_cstr;
345 Dart_StringToCString(script_path, &script_path_cstr);
346 Dart_Handle source = DartUtils::ReadStringFromFile(script_path_cstr);
347 return source;
348 }
349
350
351 Dart_Handle DartUtils::LoadScript(const char* script_uri,
352 bool resolve_script,
353 Dart_Handle builtin_lib) {
354 Dart_Handle resolved_script_uri;
355 if (resolve_script) {
356 resolved_script_uri = ResolveScriptUri(Dart_NewString(script_uri),
357 builtin_lib);
358 if (Dart_IsError(resolved_script_uri)) {
359 return resolved_script_uri;
360 }
361 } else {
362 resolved_script_uri = Dart_NewString(script_uri);
363 }
364 Dart_Handle source = ReadSource(resolved_script_uri, builtin_lib);
365 if (Dart_IsError(source)) {
366 return source;
367 }
368 return Dart_LoadScript(resolved_script_uri, source);
369 }
370
371
223 Dart_Handle DartUtils::LoadSource(CommandLineOptions* url_mapping, 372 Dart_Handle DartUtils::LoadSource(CommandLineOptions* url_mapping,
224 Dart_Handle library, 373 Dart_Handle library,
225 Dart_Handle url, 374 Dart_Handle url,
226 Dart_LibraryTag tag, 375 Dart_LibraryTag tag,
227 const char* url_string) { 376 const char* url_string) {
228 if (url_mapping != NULL && IsDartSchemeURL(url_string)) { 377 if (url_mapping != NULL && IsDartSchemeURL(url_string)) {
229 const char* mapped_url_string = MapLibraryUrl(url_mapping, url_string); 378 const char* mapped_url_string = MapLibraryUrl(url_mapping, url_string);
230 if (mapped_url_string == NULL) { 379 if (mapped_url_string == NULL) {
231 return Dart_Error("Do not know how to load %s", url_string); 380 return Dart_Error("Do not know how to load %s", url_string);
232 } 381 }
(...skipping 10 matching lines...) Expand all
243 if (tag == kImportTag) { 392 if (tag == kImportTag) {
244 // Return library object or an error string. 393 // Return library object or an error string.
245 return Dart_LoadLibrary(url, source); 394 return Dart_LoadLibrary(url, source);
246 } else if (tag == kSourceTag) { 395 } else if (tag == kSourceTag) {
247 return Dart_LoadSource(library, url, source); 396 return Dart_LoadSource(library, url, source);
248 } 397 }
249 return Dart_Error("wrong tag"); 398 return Dart_Error("wrong tag");
250 } 399 }
251 400
252 401
402 Dart_Handle DartUtils::PrepareForScriptLoading(const char* package_root,
403 Dart_Handle builtin_lib) {
404 // Setup the corelib 'print' function.
405 Dart_Handle print =
406 Dart_Invoke(builtin_lib, Dart_NewString("_getPrintClosure"), 0, 0);
407 Dart_Handle coreimpl = Dart_LookupLibrary(Dart_NewString("dart:coreimpl"));
408 Dart_Handle print_impl =
409 Dart_GetClass(coreimpl, Dart_NewString("PrintImplementation"));
410 Dart_Handle result = Dart_SetField(print_impl,
411 Dart_NewString("_printClosure"), print);
412
413 // Setup the IO library.
414 Dart_Handle io_lib = Builtin::LoadLibrary(Builtin::kIOLibrary);
415 Builtin::SetupIOLibrary(io_lib);
416
417 // Set up package root if specified.
418 if (package_root != NULL) {
419 result = Dart_NewString(package_root);
420 if (!Dart_IsError(result)) {
421 const int kNumArgs = 1;
422 Dart_Handle dart_args[kNumArgs];
423 dart_args[0] = result;
424 return Dart_Invoke(builtin_lib,
425 Dart_NewString("_setPackageRoot"),
426 kNumArgs,
427 dart_args);
428 }
429 }
430 return result;
431 }
432
433
253 const char* DartUtils::GetCanonicalPath(const char* reference_dir, 434 const char* DartUtils::GetCanonicalPath(const char* reference_dir,
254 const char* filename) { 435 const char* filename) {
255 if (File::IsAbsolutePath(filename)) { 436 if (File::IsAbsolutePath(filename)) {
256 return strdup(filename); 437 return strdup(filename);
257 } 438 }
258 439
259 char* canonical_path = File::GetCanonicalPath(reference_dir); 440 char* canonical_path = File::GetCanonicalPath(reference_dir);
260 if (canonical_path == NULL) { 441 if (canonical_path == NULL) {
261 canonical_path = strdup(reference_dir); 442 canonical_path = strdup(reference_dir);
262 ASSERT(canonical_path != NULL); 443 ASSERT(canonical_path != NULL);
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 Dart_Handle args[2]; 505 Dart_Handle args[2];
325 args[0] = Dart_NewString(os_error->message()); 506 args[0] = Dart_NewString(os_error->message());
326 if (Dart_IsError(args[0])) return args[0]; 507 if (Dart_IsError(args[0])) return args[0];
327 args[1] = Dart_NewInteger(os_error->code()); 508 args[1] = Dart_NewInteger(os_error->code());
328 if (Dart_IsError(args[1])) return args[1]; 509 if (Dart_IsError(args[1])) return args[1];
329 Dart_Handle err = Dart_New(clazz, Dart_Null(), 2, args); 510 Dart_Handle err = Dart_New(clazz, Dart_Null(), 2, args);
330 return err; 511 return err;
331 } 512 }
332 513
333 514
515 void DartUtils::SetOriginalWorkingDirectory() {
516 original_working_directory = Directory::Current();
517 }
518
519
334 // Statically allocated Dart_CObject instances for immutable 520 // Statically allocated Dart_CObject instances for immutable
335 // objects. As these will be used by different threads the use of 521 // objects. As these will be used by different threads the use of
336 // these depends on the fact that the marking internally in the 522 // these depends on the fact that the marking internally in the
337 // Dart_CObject structure is not marking simple value objects. 523 // Dart_CObject structure is not marking simple value objects.
338 Dart_CObject CObject::api_null_ = { Dart_CObject::kNull , { 0 } }; 524 Dart_CObject CObject::api_null_ = { Dart_CObject::kNull , { 0 } };
339 Dart_CObject CObject::api_true_ = { Dart_CObject::kBool , { true } }; 525 Dart_CObject CObject::api_true_ = { Dart_CObject::kBool , { true } };
340 Dart_CObject CObject::api_false_ = { Dart_CObject::kBool, { false } }; 526 Dart_CObject CObject::api_false_ = { Dart_CObject::kBool, { false } };
341 CObject CObject::null_ = CObject(&api_null_); 527 CObject CObject::null_ = CObject(&api_null_);
342 CObject CObject::true_ = CObject(&api_true_); 528 CObject CObject::true_ = CObject(&api_true_);
343 CObject CObject::false_ = CObject(&api_false_); 529 CObject CObject::false_ = CObject(&api_false_);
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
472 658
473 CObject* CObject::NewOSError(OSError* os_error) { 659 CObject* CObject::NewOSError(OSError* os_error) {
474 CObject* error_message = 660 CObject* error_message =
475 new CObjectString(CObject::NewString(os_error->message())); 661 new CObjectString(CObject::NewString(os_error->message()));
476 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); 662 CObjectArray* result = new CObjectArray(CObject::NewArray(3));
477 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); 663 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError)));
478 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); 664 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code())));
479 result->SetAt(2, error_message); 665 result->SetAt(2, error_message);
480 return result; 666 return result;
481 } 667 }
OLDNEW
« no previous file with comments | « runtime/bin/dartutils.h ('k') | runtime/bin/gen_snapshot.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698