| 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 Dart_Handle ReadStringFromFile(const char* filename) { |
| 61 File* file = File::Open(filename, false); | 61 File* file = File::Open(filename, false); |
| 62 if (file == NULL) { | 62 if (file == NULL) { |
| 63 const char* format = "Unable to open file: %s"; | 63 const char* format = "Unable to open file: %s"; |
| 64 intptr_t len = snprintf(NULL, 0, format, filename); | 64 intptr_t len = snprintf(NULL, 0, format, filename); |
| 65 // TODO(iposva): Allocate from the zone instead of leaking error string | 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. | 66 // here. On the other hand the binary is about the exit anyway. |
| 67 char* error_msg = reinterpret_cast<char*>(malloc(len + 1)); | 67 char* error_msg = reinterpret_cast<char*>(malloc(len + 1)); |
| 68 snprintf(error_msg, len + 1, format, filename); | 68 snprintf(error_msg, len + 1, format, filename); |
| 69 return Dart_Error(error_msg); | 69 return Dart_Error(error_msg); |
| 70 } | 70 } |
| 71 intptr_t len = file->Length(); | 71 intptr_t len = file->Length(); |
| 72 char* text_buffer = reinterpret_cast<char*>(malloc(len + 1)); | 72 char* text_buffer = reinterpret_cast<char*>(malloc(len + 1)); |
| 73 if (text_buffer == NULL) { | 73 if (text_buffer == NULL) { |
| 74 delete file; | 74 delete file; |
| 75 return Dart_Error("Unable to allocate buffer"); | 75 return Dart_Error("Unable to allocate buffer"); |
| 76 } | 76 } |
| 77 if (!file->ReadFully(text_buffer, len)) { | 77 if (!file->ReadFully(text_buffer, len)) { |
| 78 delete file; | 78 delete file; |
| 79 return Dart_Error("Unable to fully read contents"); | 79 return Dart_Error("Unable to fully read contents"); |
| 80 } | 80 } |
| 81 text_buffer[len] = '\0'; | 81 text_buffer[len] = '\0'; |
| 82 delete file; | 82 delete file; |
| 83 Dart_Handle str = Dart_NewString(text_buffer); | 83 Dart_Handle str = Dart_NewString(text_buffer); |
| 84 free(text_buffer); | 84 free(text_buffer); |
| 85 return str; | 85 return str; |
| 86 } | 86 } |
| 87 | 87 |
| 88 | 88 |
| 89 static Dart_Handle LibraryTagHandlerHelper(Dart_LibraryTag tag, | 89 Dart_Handle LibraryTagHandler(Dart_LibraryTag tag, |
| 90 Dart_Handle library, | 90 Dart_Handle library, |
| 91 Dart_Handle url, | 91 Dart_Handle url, |
| 92 bool import_builtin_lib) { | 92 bool import_builtin_lib) { |
| 93 if (!Dart_IsLibrary(library)) { | 93 if (!Dart_IsLibrary(library)) { |
| 94 return Dart_Error("not a library"); | 94 return Dart_Error("not a library"); |
| 95 } | 95 } |
| 96 if (!Dart_IsString8(url)) { | 96 if (!Dart_IsString8(url)) { |
| 97 return Dart_Error("url is not a string"); | 97 return Dart_Error("url is not a string"); |
| 98 } | 98 } |
| 99 const char* url_chars = NULL; | 99 const char* url_chars = NULL; |
| 100 Dart_Handle result = Dart_StringToCString(url, &url_chars); | 100 Dart_Handle result = Dart_StringToCString(url, &url_chars); |
| 101 if (!Dart_IsValid(result)) { | 101 if (!Dart_IsValid(result)) { |
| 102 return Dart_Error("accessing url characters failed"); | 102 return Dart_Error("accessing url characters failed"); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 } else if (tag == kSourceTag) { | 142 } else if (tag == kSourceTag) { |
| 143 return Dart_LoadSource(library, url, source); | 143 return Dart_LoadSource(library, url, source); |
| 144 } | 144 } |
| 145 return Dart_Error("wrong tag"); | 145 return Dart_Error("wrong tag"); |
| 146 } | 146 } |
| 147 | 147 |
| 148 static Dart_Handle MainLibraryTagHandler(Dart_LibraryTag tag, | 148 static Dart_Handle MainLibraryTagHandler(Dart_LibraryTag tag, |
| 149 Dart_Handle library, | 149 Dart_Handle library, |
| 150 Dart_Handle url) { | 150 Dart_Handle url) { |
| 151 const bool kImportBuiltinLib = true; // Import builtin library. | 151 const bool kImportBuiltinLib = true; // Import builtin library. |
| 152 return LibraryTagHandlerHelper(tag, library, url, kImportBuiltinLib); | 152 return LibraryTagHandler(tag, library, url, kImportBuiltinLib); |
| 153 } | 153 } |
| 154 | 154 |
| 155 | 155 |
| 156 static Dart_Handle CreateSnapshotLibraryTagHandler(Dart_LibraryTag tag, | 156 static Dart_Handle CreateSnapshotLibraryTagHandler(Dart_LibraryTag tag, |
| 157 Dart_Handle library, | 157 Dart_Handle library, |
| 158 Dart_Handle url) { | 158 Dart_Handle url) { |
| 159 const bool kDontImportBuiltinLib = false; // Do not import builtin lib. | 159 const bool kDontImportBuiltinLib = false; // Do not import builtin lib. |
| 160 return LibraryTagHandlerHelper(tag, library, url, kDontImportBuiltinLib); | 160 return LibraryTagHandler(tag, library, url, kDontImportBuiltinLib); |
| 161 } | 161 } |
| 162 | 162 |
| 163 | 163 |
| 164 Dart_Handle LoadScript(const char* script_name) { | 164 Dart_Handle LoadScript(const char* script_name) { |
| 165 Dart_Handle source = ReadStringFromFile(script_name); | 165 Dart_Handle source = ReadStringFromFile(script_name); |
| 166 if (!Dart_IsValid(source)) { | 166 if (!Dart_IsValid(source)) { |
| 167 return source; | 167 return source; |
| 168 } | 168 } |
| 169 Dart_Handle url = Dart_NewString(script_name); | 169 Dart_Handle url = Dart_NewString(script_name); |
| 170 | 170 |
| 171 return Dart_LoadScript(url, source, MainLibraryTagHandler); | 171 return Dart_LoadScript(url, source, MainLibraryTagHandler); |
| 172 } | 172 } |
| 173 | 173 |
| 174 | 174 |
| 175 Dart_Handle LoadSnapshotCreationScript(const char* script_name) { | 175 Dart_Handle LoadSnapshotCreationScript(const char* script_name) { |
| 176 Dart_Handle source = ReadStringFromFile(script_name); | 176 Dart_Handle source = ReadStringFromFile(script_name); |
| 177 if (!Dart_IsValid(source)) { | 177 if (!Dart_IsValid(source)) { |
| 178 return source; | 178 return source; |
| 179 } | 179 } |
| 180 Dart_Handle url = Dart_NewString(script_name); | 180 Dart_Handle url = Dart_NewString(script_name); |
| 181 | 181 |
| 182 return Dart_LoadScript(url, source, CreateSnapshotLibraryTagHandler); | 182 return Dart_LoadScript(url, source, CreateSnapshotLibraryTagHandler); |
| 183 } | 183 } |
| OLD | NEW |