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