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/dartutils.h" | |
14 #include "bin/file.h" | 15 #include "bin/file.h" |
15 #include "bin/globals.h" | 16 #include "bin/globals.h" |
16 #include "bin/process_script.h" | 17 #include "bin/process_script.h" |
17 | 18 |
18 static const char* CanonicalizeUrl(const char* reference_dir, | 19 static const char* CanonicalizeUrl(const char* reference_dir, |
19 const char* filename) { | 20 const char* filename) { |
20 static const char* kDartScheme = "dart:"; | 21 static const char* kDartScheme = "dart:"; |
21 static const intptr_t kDartSchemeLen = strlen(kDartScheme); | 22 static const intptr_t kDartSchemeLen = strlen(kDartScheme); |
22 // If the URL starts with "dart:" then it is not modified as it will be | 23 // If the URL starts with "dart:" then it is not modified as it will be |
23 // handled by the VM internally. | 24 // handled by the VM internally. |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
79 return Dart_Error("Unable to fully read contents"); | 80 return Dart_Error("Unable to fully read contents"); |
80 } | 81 } |
81 text_buffer[len] = '\0'; | 82 text_buffer[len] = '\0'; |
82 delete file; | 83 delete file; |
83 Dart_Handle str = Dart_NewString(text_buffer); | 84 Dart_Handle str = Dart_NewString(text_buffer); |
84 free(text_buffer); | 85 free(text_buffer); |
85 return str; | 86 return str; |
86 } | 87 } |
87 | 88 |
88 | 89 |
89 static Dart_Handle LibraryTagHandlerHelper(Dart_LibraryTag tag, | 90 Dart_Handle LibraryTagHandlerHelper(Dart_LibraryTag tag, |
90 Dart_Handle library, | 91 Dart_Handle library, |
91 Dart_Handle url, | 92 Dart_Handle url, |
92 bool import_builtin_lib) { | 93 bool import_builtin_lib) { |
93 if (!Dart_IsLibrary(library)) { | 94 if (!Dart_IsLibrary(library)) { |
94 return Dart_Error("not a library"); | 95 return Dart_Error("not a library"); |
95 } | 96 } |
96 if (!Dart_IsString8(url)) { | 97 if (!Dart_IsString8(url)) { |
97 return Dart_Error("url is not a string"); | 98 return Dart_Error("url is not a string"); |
98 } | 99 } |
99 const char* url_chars = NULL; | 100 const char* url_chars = NULL; |
100 Dart_Handle result = Dart_StringToCString(url, &url_chars); | 101 Dart_Handle result = Dart_StringToCString(url, &url_chars); |
101 if (!Dart_IsValid(result)) { | 102 if (!Dart_IsValid(result)) { |
102 return Dart_Error("accessing url characters failed"); | 103 return Dart_Error("accessing url characters failed"); |
103 } | 104 } |
104 | 105 bool is_native_flds_library = !strcmp(url_chars, |
Anton Muhin
2011/11/13 16:19:32
I am somewhat concerned that logic for dart:core_n
siva
2011/11/15 02:16:52
Moved this check to just before invoking the libra
| |
106 DartUtils::kCoreNativeFieldsLibURL); | |
105 if (tag == kCanonicalizeUrl) { | 107 if (tag == kCanonicalizeUrl) { |
108 if (is_native_flds_library) { | |
109 return url; | |
110 } | |
106 // Create the full path based on the including library and the current url. | 111 // Create the full path based on the including library and the current url. |
107 | 112 |
108 // Get the url of the calling library. | 113 // Get the url of the calling library. |
109 Dart_Handle library_url = Dart_LibraryUrl(library); | 114 Dart_Handle library_url = Dart_LibraryUrl(library); |
110 if (!Dart_IsValid(library_url)) { | 115 if (!Dart_IsValid(library_url)) { |
111 return Dart_Error("accessing library url failed"); | 116 return Dart_Error("accessing library url failed"); |
112 } | 117 } |
113 if (!Dart_IsString8(library_url)) { | 118 if (!Dart_IsString8(library_url)) { |
114 return Dart_Error("library url is not a string"); | 119 return Dart_Error("library url is not a string"); |
115 } | 120 } |
116 const char* library_url_chars = NULL; | 121 const char* library_url_chars = NULL; |
117 result = Dart_StringToCString(library_url, &library_url_chars); | 122 result = Dart_StringToCString(library_url, &library_url_chars); |
118 if (!Dart_IsValid(result)) { | 123 if (!Dart_IsValid(result)) { |
119 return Dart_Error("accessing library url characters failed"); | 124 return Dart_Error("accessing library url characters failed"); |
120 } | 125 } |
121 | 126 |
122 // Calculate the path. | 127 // Calculate the path. |
123 const char* canon_url_chars = CanonicalizeUrl(library_url_chars, url_chars); | 128 const char* canon_url_chars = CanonicalizeUrl(library_url_chars, url_chars); |
124 Dart_Handle canon_url = Dart_NewString(canon_url_chars); | 129 Dart_Handle canon_url = Dart_NewString(canon_url_chars); |
125 free(const_cast<char*>(canon_url_chars)); | 130 free(const_cast<char*>(canon_url_chars)); |
126 | 131 |
127 return canon_url; | 132 return canon_url; |
128 } | 133 } |
129 | 134 |
135 if (is_native_flds_library) { | |
136 return Dart_LookupLibrary(url); | |
137 } | |
138 | |
130 // The tag is either an import or a source tag. Read the file based on the | 139 // The tag is either an import or a source tag. Read the file based on the |
131 // url chars. | 140 // url chars. |
132 Dart_Handle source = ReadStringFromFile(url_chars); | 141 Dart_Handle source = ReadStringFromFile(url_chars); |
133 if (!Dart_IsValid(source)) { | 142 if (!Dart_IsValid(source)) { |
134 return source; // source contains the error string. | 143 return source; // source contains the error string. |
135 } | 144 } |
136 if (tag == kImportTag) { | 145 if (tag == kImportTag) { |
137 Dart_Handle new_lib = Dart_LoadLibrary(url, source); | 146 Dart_Handle new_lib = Dart_LoadLibrary(url, source); |
138 if (import_builtin_lib && Dart_IsValid(new_lib)) { | 147 if (import_builtin_lib && Dart_IsValid(new_lib)) { |
139 Builtin_ImportLibrary(new_lib); | 148 Builtin_ImportLibrary(new_lib); |
140 } | 149 } |
141 return new_lib; // Return library object or an error string. | 150 return new_lib; // Return library object or an error string. |
142 } else if (tag == kSourceTag) { | 151 } else if (tag == kSourceTag) { |
143 return Dart_LoadSource(library, url, source); | 152 return Dart_LoadSource(library, url, source); |
144 } | 153 } |
145 return Dart_Error("wrong tag"); | 154 return Dart_Error("wrong tag"); |
146 } | 155 } |
147 | 156 |
157 | |
148 static Dart_Handle MainLibraryTagHandler(Dart_LibraryTag tag, | 158 static Dart_Handle MainLibraryTagHandler(Dart_LibraryTag tag, |
149 Dart_Handle library, | 159 Dart_Handle library, |
150 Dart_Handle url) { | 160 Dart_Handle url) { |
151 const bool kImportBuiltinLib = true; // Import builtin library. | 161 const bool kImportBuiltinLib = true; // Import builtin library. |
152 return LibraryTagHandlerHelper(tag, library, url, kImportBuiltinLib); | 162 return LibraryTagHandlerHelper(tag, library, url, kImportBuiltinLib); |
153 } | 163 } |
154 | 164 |
155 | 165 |
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) { | 166 Dart_Handle LoadScript(const char* script_name) { |
165 Dart_Handle source = ReadStringFromFile(script_name); | 167 Dart_Handle source = ReadStringFromFile(script_name); |
166 if (!Dart_IsValid(source)) { | 168 if (!Dart_IsValid(source)) { |
167 return source; | 169 return source; |
168 } | 170 } |
169 Dart_Handle url = Dart_NewString(script_name); | 171 Dart_Handle url = Dart_NewString(script_name); |
170 | |
171 return Dart_LoadScript(url, source, MainLibraryTagHandler); | 172 return Dart_LoadScript(url, source, MainLibraryTagHandler); |
172 } | 173 } |
173 | 174 |
174 | 175 |
175 Dart_Handle LoadSnapshotCreationScript(const char* script_name) { | 176 Dart_Handle LoadSnapshotCreationScript(const char* script_name, |
177 Dart_LibraryTagHandler handler) { | |
176 Dart_Handle source = ReadStringFromFile(script_name); | 178 Dart_Handle source = ReadStringFromFile(script_name); |
177 if (!Dart_IsValid(source)) { | 179 if (!Dart_IsValid(source)) { |
178 return source; | 180 return source; |
179 } | 181 } |
180 Dart_Handle url = Dart_NewString(script_name); | 182 Dart_Handle url = Dart_NewString(script_name); |
181 | 183 return Dart_LoadScript(url, source, handler); |
182 return Dart_LoadScript(url, source, CreateSnapshotLibraryTagHandler); | |
183 } | 184 } |
OLD | NEW |