| 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 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 free(path); | 50 free(path); |
| 51 char* canonical_filename = File::GetCanonicalPath(absolute_filename); | 51 char* canonical_filename = File::GetCanonicalPath(absolute_filename); |
| 52 if (canonical_filename == NULL) { | 52 if (canonical_filename == NULL) { |
| 53 return absolute_filename; | 53 return absolute_filename; |
| 54 } | 54 } |
| 55 free(absolute_filename); | 55 free(absolute_filename); |
| 56 return canonical_filename; | 56 return canonical_filename; |
| 57 } | 57 } |
| 58 | 58 |
| 59 | 59 |
| 60 static Dart_Handle ReadStringFromFile(const char* filename) { | 60 static Dart_Handle LibraryTagHandler(Dart_LibraryTag tag, |
| 61 File* file = File::Open(filename, false); | 61 Dart_Handle library, |
| 62 if (file == NULL) { | 62 Dart_Handle url) { |
| 63 const char* format = "Unable to open file: %s"; | |
| 64 intptr_t len = snprintf(NULL, 0, format, filename); | |
| 65 // TODO(iposva): Allocate from the zone instead of leaking error string | |
| 66 // here. On the other hand the binary is about the exit anyway. | |
| 67 char* error_msg = reinterpret_cast<char*>(malloc(len + 1)); | |
| 68 snprintf(error_msg, len + 1, format, filename); | |
| 69 return Dart_Error(error_msg); | |
| 70 } | |
| 71 intptr_t len = file->Length(); | |
| 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 } | |
| 87 | |
| 88 | |
| 89 static Dart_Handle LibraryTagHandlerHelper(Dart_LibraryTag tag, | |
| 90 Dart_Handle library, | |
| 91 Dart_Handle url, | |
| 92 bool import_builtin_lib) { | |
| 93 if (!Dart_IsLibrary(library)) { | 63 if (!Dart_IsLibrary(library)) { |
| 94 return Dart_Error("not a library"); | 64 return Dart_Error("not a library"); |
| 95 } | 65 } |
| 96 if (!Dart_IsString8(url)) { | 66 if (!Dart_IsString8(url)) { |
| 97 return Dart_Error("url is not a string"); | 67 return Dart_Error("url is not a string"); |
| 98 } | 68 } |
| 99 const char* url_chars = NULL; | 69 const char* url_chars = NULL; |
| 100 Dart_Handle result = Dart_StringToCString(url, &url_chars); | 70 Dart_Handle result = Dart_StringToCString(url, &url_chars); |
| 101 if (Dart_IsError(result)) { | 71 if (Dart_IsError(result)) { |
| 102 return Dart_Error("accessing url characters failed"); | 72 return Dart_Error("accessing url characters failed"); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 128 } | 98 } |
| 129 | 99 |
| 130 // The tag is either an import or a source tag. Read the file based on the | 100 // The tag is either an import or a source tag. Read the file based on the |
| 131 // url chars. | 101 // url chars. |
| 132 Dart_Handle source = ReadStringFromFile(url_chars); | 102 Dart_Handle source = ReadStringFromFile(url_chars); |
| 133 if (Dart_IsError(source)) { | 103 if (Dart_IsError(source)) { |
| 134 return source; // source contains the error string. | 104 return source; // source contains the error string. |
| 135 } | 105 } |
| 136 if (tag == kImportTag) { | 106 if (tag == kImportTag) { |
| 137 Dart_Handle new_lib = Dart_LoadLibrary(url, source); | 107 Dart_Handle new_lib = Dart_LoadLibrary(url, source); |
| 138 if (import_builtin_lib && !Dart_IsError(new_lib)) { | 108 if (!Dart_IsError(new_lib)) { |
| 139 Builtin_ImportLibrary(new_lib); | 109 Builtin_ImportLibrary(new_lib); |
| 140 } | 110 } |
| 141 return new_lib; // Return library object or an error string. | 111 return new_lib; // Return library object or an error string. |
| 142 } else if (tag == kSourceTag) { | 112 } else if (tag == kSourceTag) { |
| 143 return Dart_LoadSource(library, url, source); | 113 return Dart_LoadSource(library, url, source); |
| 144 } | 114 } |
| 145 return Dart_Error("wrong tag"); | 115 return Dart_Error("wrong tag"); |
| 146 } | 116 } |
| 147 | 117 |
| 148 static Dart_Handle MainLibraryTagHandler(Dart_LibraryTag tag, | 118 |
| 149 Dart_Handle library, | 119 Dart_Handle ReadStringFromFile(const char* filename) { |
| 150 Dart_Handle url) { | 120 File* file = File::Open(filename, false); |
| 151 const bool kImportBuiltinLib = true; // Import builtin library. | 121 if (file == NULL) { |
| 152 return LibraryTagHandlerHelper(tag, library, url, kImportBuiltinLib); | 122 const char* format = "Unable to open file: %s"; |
| 123 intptr_t len = snprintf(NULL, 0, format, filename); |
| 124 // TODO(iposva): Allocate from the zone instead of leaking error string |
| 125 // here. On the other hand the binary is about the exit anyway. |
| 126 char* error_msg = reinterpret_cast<char*>(malloc(len + 1)); |
| 127 snprintf(error_msg, len + 1, format, filename); |
| 128 return Dart_Error(error_msg); |
| 129 } |
| 130 intptr_t len = file->Length(); |
| 131 char* text_buffer = reinterpret_cast<char*>(malloc(len + 1)); |
| 132 if (text_buffer == NULL) { |
| 133 delete file; |
| 134 return Dart_Error("Unable to allocate buffer"); |
| 135 } |
| 136 if (!file->ReadFully(text_buffer, len)) { |
| 137 delete file; |
| 138 return Dart_Error("Unable to fully read contents"); |
| 139 } |
| 140 text_buffer[len] = '\0'; |
| 141 delete file; |
| 142 Dart_Handle str = Dart_NewString(text_buffer); |
| 143 free(text_buffer); |
| 144 return str; |
| 153 } | 145 } |
| 154 | 146 |
| 155 | 147 |
| 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) { | 148 Dart_Handle LoadScript(const char* script_name) { |
| 165 Dart_Handle source = ReadStringFromFile(script_name); | 149 Dart_Handle source = ReadStringFromFile(script_name); |
| 166 if (Dart_IsError(source)) { | 150 if (Dart_IsError(source)) { |
| 167 return source; | 151 return source; |
| 168 } | 152 } |
| 169 Dart_Handle url = Dart_NewString(script_name); | 153 Dart_Handle url = Dart_NewString(script_name); |
| 170 | 154 |
| 171 return Dart_LoadScript(url, source, MainLibraryTagHandler); | 155 return Dart_LoadScript(url, source, LibraryTagHandler); |
| 172 } | 156 } |
| 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 |