| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 // Handle dart scripts. | |
| 6 | |
| 7 #include <stdlib.h> | |
| 8 #include <string.h> | |
| 9 #include <stdio.h> | |
| 10 | |
| 11 #include "include/dart_api.h" | |
| 12 | |
| 13 #include "bin/builtin.h" | |
| 14 #include "bin/file.h" | |
| 15 #include "bin/globals.h" | |
| 16 #include "bin/process_script.h" | |
| 17 | |
| 18 const char* GetCanonicalPath(const char* reference_dir, | |
| 19 const char* filename) { | |
| 20 if (File::IsAbsolutePath(filename)) { | |
| 21 return strdup(filename); | |
| 22 } | |
| 23 | |
| 24 char* path = strdup(reference_dir); | |
| 25 if (path == NULL) { | |
| 26 return NULL; | |
| 27 } | |
| 28 char* path_sep = strrchr(path, File::PathSeparator()[0]); | |
| 29 if (path_sep == NULL) { | |
| 30 // No separator found: Reference is a file in local directory. | |
| 31 return strdup(filename); | |
| 32 } | |
| 33 *path_sep = '\0'; | |
| 34 intptr_t len = snprintf(NULL, 0, "%s%s%s", | |
| 35 path, File::PathSeparator(), filename); | |
| 36 char* absolute_filename = reinterpret_cast<char*>(malloc(len + 1)); | |
| 37 ASSERT(absolute_filename != NULL); | |
| 38 | |
| 39 snprintf(absolute_filename, len + 1, "%s%s%s", | |
| 40 path, File::PathSeparator(), filename); | |
| 41 | |
| 42 free(path); | |
| 43 char* canonical_filename = File::GetCanonicalPath(absolute_filename); | |
| 44 if (canonical_filename == NULL) { | |
| 45 return absolute_filename; | |
| 46 } | |
| 47 free(absolute_filename); | |
| 48 return canonical_filename; | |
| 49 } | |
| 50 | |
| 51 | |
| 52 static Dart_Handle LibraryTagHandler(Dart_LibraryTag tag, | |
| 53 Dart_Handle library, | |
| 54 Dart_Handle url) { | |
| 55 if (!Dart_IsLibrary(library)) { | |
| 56 return Dart_Error("not a library"); | |
| 57 } | |
| 58 if (!Dart_IsString8(url)) { | |
| 59 return Dart_Error("url is not a string"); | |
| 60 } | |
| 61 const char* url_chars = NULL; | |
| 62 Dart_Handle result = Dart_StringToCString(url, &url_chars); | |
| 63 if (Dart_IsError(result)) { | |
| 64 return Dart_Error("accessing url characters failed"); | |
| 65 } | |
| 66 | |
| 67 static const char* kDartScheme = "dart:"; | |
| 68 static const intptr_t kDartSchemeLen = strlen(kDartScheme); | |
| 69 // If the URL starts with "dart:" then it is not modified as it will be | |
| 70 // handled by the VM internally. | |
| 71 if (strncmp(url_chars, kDartScheme, kDartSchemeLen) == 0) { | |
| 72 if (tag == kCanonicalizeUrl) { | |
| 73 return url; | |
| 74 } | |
| 75 return Dart_Error("Do not know how to load '%s'", url_chars); | |
| 76 } | |
| 77 if (tag == kCanonicalizeUrl) { | |
| 78 // Create a canonical path based on the including library and current url. | |
| 79 | |
| 80 // Get the url of the including library. | |
| 81 Dart_Handle library_url = Dart_LibraryUrl(library); | |
| 82 if (Dart_IsError(library_url)) { | |
| 83 return Dart_Error("accessing library url failed"); | |
| 84 } | |
| 85 if (!Dart_IsString8(library_url)) { | |
| 86 return Dart_Error("library url is not a string"); | |
| 87 } | |
| 88 const char* library_url_chars = NULL; | |
| 89 result = Dart_StringToCString(library_url, &library_url_chars); | |
| 90 if (Dart_IsError(result)) { | |
| 91 return Dart_Error("accessing library url characters failed"); | |
| 92 } | |
| 93 | |
| 94 // Calculate the canonical path. | |
| 95 const char* canon_url_chars = GetCanonicalPath(library_url_chars, | |
| 96 url_chars); | |
| 97 Dart_Handle canon_url = Dart_NewString(canon_url_chars); | |
| 98 free(const_cast<char*>(canon_url_chars)); | |
| 99 | |
| 100 return canon_url; | |
| 101 } | |
| 102 | |
| 103 // The tag is either an import or a source tag. Read the file based on the | |
| 104 // url chars. | |
| 105 Dart_Handle source = ReadStringFromFile(url_chars); | |
| 106 if (Dart_IsError(source)) { | |
| 107 return source; // source contains the error string. | |
| 108 } | |
| 109 if (tag == kImportTag) { | |
| 110 Dart_Handle new_lib = Dart_LoadLibrary(url, source); | |
| 111 if (!Dart_IsError(new_lib)) { | |
| 112 Builtin_ImportLibrary(new_lib); | |
| 113 } | |
| 114 return new_lib; // Return library object or an error string. | |
| 115 } else if (tag == kSourceTag) { | |
| 116 return Dart_LoadSource(library, url, source); | |
| 117 } | |
| 118 return Dart_Error("wrong tag"); | |
| 119 } | |
| 120 | |
| 121 | |
| 122 Dart_Handle ReadStringFromFile(const char* filename) { | |
| 123 File* file = File::Open(filename, false); | |
| 124 if (file == NULL) { | |
| 125 const char* format = "Unable to open file: %s"; | |
| 126 intptr_t len = snprintf(NULL, 0, format, filename); | |
| 127 // TODO(iposva): Allocate from the zone instead of leaking error string | |
| 128 // here. On the other hand the binary is about the exit anyway. | |
| 129 char* error_msg = reinterpret_cast<char*>(malloc(len + 1)); | |
| 130 snprintf(error_msg, len + 1, format, filename); | |
| 131 return Dart_Error(error_msg); | |
| 132 } | |
| 133 intptr_t len = file->Length(); | |
| 134 char* text_buffer = reinterpret_cast<char*>(malloc(len + 1)); | |
| 135 if (text_buffer == NULL) { | |
| 136 delete file; | |
| 137 return Dart_Error("Unable to allocate buffer"); | |
| 138 } | |
| 139 if (!file->ReadFully(text_buffer, len)) { | |
| 140 delete file; | |
| 141 return Dart_Error("Unable to fully read contents"); | |
| 142 } | |
| 143 text_buffer[len] = '\0'; | |
| 144 delete file; | |
| 145 Dart_Handle str = Dart_NewString(text_buffer); | |
| 146 free(text_buffer); | |
| 147 return str; | |
| 148 } | |
| 149 | |
| 150 | |
| 151 Dart_Handle LoadScript(const char* script_name) { | |
| 152 Dart_Handle source = ReadStringFromFile(script_name); | |
| 153 if (Dart_IsError(source)) { | |
| 154 return source; | |
| 155 } | |
| 156 Dart_Handle url = Dart_NewString(script_name); | |
| 157 | |
| 158 return Dart_LoadScript(url, source, LibraryTagHandler); | |
| 159 } | |
| OLD | NEW |