| 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_Result ReadStringFromFile(const char* filename) { | 60 static Dart_Handle ReadStringFromFile(const char* filename) { |
| 61 File* file = File::OpenFile(filename, false); | 61 File* file = File::OpenFile(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_ErrorResult(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_ErrorResult("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_ErrorResult("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 Dart_ResultAsObject(str); | 85 return str; |
| 86 } | 86 } |
| 87 | 87 |
| 88 | 88 |
| 89 static Dart_Result LibraryTagHandler(Dart_LibraryTag tag, | 89 static Dart_Handle LibraryTagHandler(Dart_LibraryTag tag, |
| 90 Dart_Handle library, | 90 Dart_Handle library, |
| 91 Dart_Handle url) { | 91 Dart_Handle url) { |
| 92 if (!Dart_IsLibrary(library)) { | 92 if (!Dart_IsLibrary(library)) { |
| 93 return Dart_ErrorResult("not a library"); | 93 return Dart_Error("not a library"); |
| 94 } | 94 } |
| 95 if (!Dart_IsString8(url)) { | 95 if (!Dart_IsString8(url)) { |
| 96 return Dart_ErrorResult("url is not a string"); | 96 return Dart_Error("url is not a string"); |
| 97 } | 97 } |
| 98 Dart_Result result = Dart_StringToCString(url); | 98 const char* url_chars = NULL; |
| 99 if (!Dart_IsValidResult(result)) { | 99 Dart_Handle result = Dart_StringToCString(url, &url_chars); |
| 100 return Dart_ErrorResult("accessing url characters failed"); | 100 if (!Dart_IsValid(result)) { |
| 101 return Dart_Error("accessing url characters failed"); |
| 101 } | 102 } |
| 102 const char* url_chars = Dart_GetResultAsCString(result); | |
| 103 | 103 |
| 104 if (tag == kCanonicalizeUrl) { | 104 if (tag == kCanonicalizeUrl) { |
| 105 // Create the full path based on the including library and the current url. | 105 // Create the full path based on the including library and the current url. |
| 106 | 106 |
| 107 // Get the url of the calling library. | 107 // Get the url of the calling library. |
| 108 result = Dart_LibraryUrl(library); | 108 Dart_Handle library_url = Dart_LibraryUrl(library); |
| 109 if (!Dart_IsValidResult(result)) { | 109 if (!Dart_IsValid(library_url)) { |
| 110 return Dart_ErrorResult("accessing library url failed"); | 110 return Dart_Error("accessing library url failed"); |
| 111 } | 111 } |
| 112 Dart_Handle library_url = Dart_GetResult(result); | |
| 113 if (!Dart_IsString8(library_url)) { | 112 if (!Dart_IsString8(library_url)) { |
| 114 return Dart_ErrorResult("library url is not a string"); | 113 return Dart_Error("library url is not a string"); |
| 115 } | 114 } |
| 116 result = Dart_StringToCString(library_url); | 115 const char* library_url_chars = NULL; |
| 117 if (!Dart_IsValidResult(result)) { | 116 result = Dart_StringToCString(library_url, &library_url_chars); |
| 118 return Dart_ErrorResult("accessing library url characters failed"); | 117 if (!Dart_IsValid(result)) { |
| 118 return Dart_Error("accessing library url characters failed"); |
| 119 } | 119 } |
| 120 const char* library_url_chars = Dart_GetResultAsCString(result); | |
| 121 | 120 |
| 122 // Calculate the path. | 121 // Calculate the path. |
| 123 const char* canon_url_chars = CanonicalizeUrl(library_url_chars, url_chars); | 122 const char* canon_url_chars = CanonicalizeUrl(library_url_chars, url_chars); |
| 124 Dart_Handle canon_url = Dart_NewString(canon_url_chars); | 123 Dart_Handle canon_url = Dart_NewString(canon_url_chars); |
| 125 free(const_cast<char*>(canon_url_chars)); | 124 free(const_cast<char*>(canon_url_chars)); |
| 126 | 125 |
| 127 return Dart_ResultAsObject(canon_url); | 126 return canon_url; |
| 128 } | 127 } |
| 129 | 128 |
| 130 // The tag is either an import or a source tag. Read the file based on the | 129 // The tag is either an import or a source tag. Read the file based on the |
| 131 // url chars. | 130 // url chars. |
| 132 result = ReadStringFromFile(url_chars); | 131 Dart_Handle source = ReadStringFromFile(url_chars); |
| 133 if (!Dart_IsValidResult(result)) { | 132 if (!Dart_IsValid(source)) { |
| 134 return result; | 133 return result; |
| 135 } | 134 } |
| 136 Dart_Handle source = Dart_GetResult(result); | |
| 137 | 135 |
| 138 if (tag == kImportTag) { | 136 if (tag == kImportTag) { |
| 139 result = Dart_LoadLibrary(url, source); | 137 Dart_Handle new_lib = Dart_LoadLibrary(url, source); |
| 140 if (Dart_IsValidResult(result)) { | 138 if (Dart_IsValid(new_lib)) { |
| 141 // TODO(iposva): Should the builtin library be added to all libraries? | 139 // TODO(iposva): Should the builtin library be added to all libraries? |
| 142 Dart_Handle new_lib = Dart_GetResult(result); | |
| 143 Builtin_ImportLibrary(new_lib); | 140 Builtin_ImportLibrary(new_lib); |
| 144 } | 141 } |
| 145 return result; | 142 return result; |
| 146 } else if (tag == kSourceTag) { | 143 } else if (tag == kSourceTag) { |
| 147 return Dart_LoadSource(library, url, source); | 144 return Dart_LoadSource(library, url, source); |
| 148 } | 145 } |
| 149 return Dart_ErrorResult("wrong tag"); | 146 return Dart_Error("wrong tag"); |
| 150 } | 147 } |
| 151 | 148 |
| 152 | 149 |
| 153 Dart_Result LoadScript(const char* script_name) { | 150 Dart_Handle LoadScript(const char* script_name) { |
| 154 Dart_Result result = ReadStringFromFile(script_name); | 151 Dart_Handle source = ReadStringFromFile(script_name); |
| 155 if (!Dart_IsValidResult(result)) { | 152 if (!Dart_IsValid(source)) { |
| 156 return result; | 153 return source; |
| 157 } | 154 } |
| 158 Dart_Handle source = Dart_GetResult(result); | |
| 159 Dart_Handle url = Dart_NewString(script_name); | 155 Dart_Handle url = Dart_NewString(script_name); |
| 160 | 156 |
| 161 return Dart_LoadScript(url, source, LibraryTagHandler); | 157 return Dart_LoadScript(url, source, LibraryTagHandler); |
| 162 } | 158 } |
| OLD | NEW |