| OLD | NEW |
| 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 "bin/dartutils.h" | 5 #include "bin/dartutils.h" |
| 6 | 6 |
| 7 #include "bin/file.h" |
| 8 #include "bin/globals.h" |
| 9 |
| 10 const char* DartUtils::kDartScheme = "dart:"; |
| 7 const char* DartUtils::kBuiltinLibURL = "dart:builtin"; | 11 const char* DartUtils::kBuiltinLibURL = "dart:builtin"; |
| 8 const char* DartUtils::kCoreLibURL = "dart:core"; | 12 const char* DartUtils::kCoreLibURL = "dart:core"; |
| 9 const char* DartUtils::kCoreImplLibURL = "dart:coreimpl"; | 13 const char* DartUtils::kCoreImplLibURL = "dart:coreimpl"; |
| 10 const char* DartUtils::kCoreNativeWrappersLibURL = "dart:nativewrappers"; | 14 const char* DartUtils::kCoreNativeWrappersLibURL = "dart:nativewrappers"; |
| 11 const char* DartUtils::kIdFieldName = "_id"; | 15 const char* DartUtils::kIdFieldName = "_id"; |
| 12 | 16 |
| 13 | 17 |
| 18 static const char* MapLibraryUrl(CommandLineOptions* url_mapping, |
| 19 const char* url_string) { |
| 20 ASSERT(url_mapping != NULL); |
| 21 // We need to check if the passed in url is found in the url_mapping array, |
| 22 // in that case use the mapped entry. |
| 23 int len = strlen(url_string); |
| 24 for (int idx = 0; idx < url_mapping->count(); idx++) { |
| 25 const char* url_name = url_mapping->GetArgument(idx); |
| 26 if (!strncmp(url_string, url_name, len) && (url_name[len] == ',')) { |
| 27 const char* url_mapped_name = url_name + len + 1; |
| 28 if (strlen(url_mapped_name) != 0) { |
| 29 return url_mapped_name; // Found a mapping for this URL. |
| 30 } |
| 31 } |
| 32 } |
| 33 return NULL; // Did not find a mapping for this URL. |
| 34 } |
| 35 |
| 36 |
| 14 int64_t DartUtils::GetIntegerValue(Dart_Handle value_obj) { | 37 int64_t DartUtils::GetIntegerValue(Dart_Handle value_obj) { |
| 15 ASSERT(Dart_IsInteger(value_obj)); | 38 ASSERT(Dart_IsInteger(value_obj)); |
| 16 int64_t value = 0; | 39 int64_t value = 0; |
| 17 Dart_Handle result = Dart_IntegerValue(value_obj, &value); | 40 Dart_Handle result = Dart_IntegerValue(value_obj, &value); |
| 18 ASSERT(!Dart_IsError(result)); | 41 ASSERT(!Dart_IsError(result)); |
| 19 return value; | 42 return value; |
| 20 } | 43 } |
| 21 | 44 |
| 22 | 45 |
| 23 const char* DartUtils::GetStringValue(Dart_Handle str_obj) { | 46 const char* DartUtils::GetStringValue(Dart_Handle str_obj) { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 | 80 |
| 58 | 81 |
| 59 void DartUtils::SetStringInstanceField(Dart_Handle handle, | 82 void DartUtils::SetStringInstanceField(Dart_Handle handle, |
| 60 const char* name, | 83 const char* name, |
| 61 const char* val) { | 84 const char* val) { |
| 62 Dart_Handle result = Dart_SetInstanceField(handle, | 85 Dart_Handle result = Dart_SetInstanceField(handle, |
| 63 Dart_NewString(name), | 86 Dart_NewString(name), |
| 64 Dart_NewString(val)); | 87 Dart_NewString(val)); |
| 65 ASSERT(!Dart_IsError(result)); | 88 ASSERT(!Dart_IsError(result)); |
| 66 } | 89 } |
| 90 |
| 91 |
| 92 bool DartUtils::IsDartSchemeURL(const char* url_name) { |
| 93 static const intptr_t kDartSchemeLen = strlen(kDartScheme); |
| 94 // If the URL starts with "dart:" then it is considered as a special |
| 95 // library URL which is handled differently from other URLs. |
| 96 return (strncmp(url_name, kDartScheme, kDartSchemeLen) == 0); |
| 97 } |
| 98 |
| 99 |
| 100 Dart_Handle DartUtils::CanonicalizeURL(CommandLineOptions* url_mapping, |
| 101 Dart_Handle library, |
| 102 const char* url_str) { |
| 103 // Get the url of the including library. |
| 104 Dart_Handle library_url = Dart_LibraryUrl(library); |
| 105 if (Dart_IsError(library_url)) { |
| 106 return Dart_Error("accessing library url failed"); |
| 107 } |
| 108 if (!Dart_IsString8(library_url)) { |
| 109 return Dart_Error("library url is not a string"); |
| 110 } |
| 111 const char* library_url_str = NULL; |
| 112 Dart_Handle result = Dart_StringToCString(library_url, &library_url_str); |
| 113 if (Dart_IsError(result)) { |
| 114 return Dart_Error("accessing library url characters failed"); |
| 115 } |
| 116 if (url_mapping != NULL) { |
| 117 const char* mapped_library_url_str = MapLibraryUrl(url_mapping, |
| 118 library_url_str); |
| 119 if (mapped_library_url_str != NULL) { |
| 120 library_url_str = mapped_library_url_str; |
| 121 } |
| 122 } |
| 123 // Calculate the canonical path. |
| 124 const char* canon_url_str = GetCanonicalPath(library_url_str, url_str); |
| 125 Dart_Handle canon_url = Dart_NewString(canon_url_str); |
| 126 free(const_cast<char*>(canon_url_str)); |
| 127 |
| 128 return canon_url; |
| 129 } |
| 130 |
| 131 |
| 132 Dart_Handle DartUtils::ReadStringFromFile(const char* filename) { |
| 133 File* file = File::Open(filename, false); |
| 134 if (file == NULL) { |
| 135 const char* format = "Unable to open file: %s"; |
| 136 intptr_t len = snprintf(NULL, 0, format, filename); |
| 137 // TODO(iposva): Allocate from the zone instead of leaking error string |
| 138 // here. On the other hand the binary is about the exit anyway. |
| 139 char* error_msg = reinterpret_cast<char*>(malloc(len + 1)); |
| 140 snprintf(error_msg, len + 1, format, filename); |
| 141 return Dart_Error(error_msg); |
| 142 } |
| 143 intptr_t len = file->Length(); |
| 144 char* text_buffer = reinterpret_cast<char*>(malloc(len + 1)); |
| 145 if (text_buffer == NULL) { |
| 146 delete file; |
| 147 return Dart_Error("Unable to allocate buffer"); |
| 148 } |
| 149 if (!file->ReadFully(text_buffer, len)) { |
| 150 delete file; |
| 151 return Dart_Error("Unable to fully read contents"); |
| 152 } |
| 153 text_buffer[len] = '\0'; |
| 154 delete file; |
| 155 Dart_Handle str = Dart_NewString(text_buffer); |
| 156 free(text_buffer); |
| 157 return str; |
| 158 } |
| 159 |
| 160 |
| 161 Dart_Handle DartUtils::LoadSource(CommandLineOptions* url_mapping, |
| 162 Dart_Handle library, |
| 163 Dart_Handle url, |
| 164 Dart_LibraryTag tag, |
| 165 const char* url_string) { |
| 166 if (url_mapping != NULL && IsDartSchemeURL(url_string)) { |
| 167 const char* mapped_url_string = MapLibraryUrl(url_mapping, url_string); |
| 168 if (mapped_url_string == NULL) { |
| 169 return Dart_Error("Do not know how to load %s", url_string); |
| 170 } |
| 171 // We have a URL mapping specified, just read the file that the |
| 172 // URL mapping specifies and load it. |
| 173 url_string = mapped_url_string; |
| 174 } |
| 175 // The tag is either an import or a source tag. |
| 176 // Read the file and load it according to the specified tag. |
| 177 Dart_Handle source = DartUtils::ReadStringFromFile(url_string); |
| 178 if (Dart_IsError(source)) { |
| 179 return source; // source contains the error string. |
| 180 } |
| 181 if (tag == kImportTag) { |
| 182 // Return library object or an error string. |
| 183 return Dart_LoadLibrary(url, source); |
| 184 } else if (tag == kSourceTag) { |
| 185 return Dart_LoadSource(library, url, source); |
| 186 } |
| 187 return Dart_Error("wrong tag"); |
| 188 } |
| 189 |
| 190 |
| 191 const char* DartUtils::GetCanonicalPath(const char* reference_dir, |
| 192 const char* filename) { |
| 193 if (File::IsAbsolutePath(filename)) { |
| 194 return strdup(filename); |
| 195 } |
| 196 |
| 197 char* path = strdup(reference_dir); |
| 198 if (path == NULL) { |
| 199 return NULL; |
| 200 } |
| 201 char* path_sep = strrchr(path, File::PathSeparator()[0]); |
| 202 if (path_sep == NULL) { |
| 203 // No separator found: Reference is a file in local directory. |
| 204 return strdup(filename); |
| 205 } |
| 206 *path_sep = '\0'; |
| 207 intptr_t len = snprintf(NULL, 0, "%s%s%s", |
| 208 path, File::PathSeparator(), filename); |
| 209 char* absolute_filename = reinterpret_cast<char*>(malloc(len + 1)); |
| 210 ASSERT(absolute_filename != NULL); |
| 211 |
| 212 snprintf(absolute_filename, len + 1, "%s%s%s", |
| 213 path, File::PathSeparator(), filename); |
| 214 |
| 215 free(path); |
| 216 char* canonical_filename = File::GetCanonicalPath(absolute_filename); |
| 217 if (canonical_filename == NULL) { |
| 218 return absolute_filename; |
| 219 } |
| 220 free(absolute_filename); |
| 221 return canonical_filename; |
| 222 } |
| OLD | NEW |