Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(308)

Side by Side Diff: runtime/vm/bootstrap.cc

Issue 14786012: Second step towards loading the core library scripts directly from the sources (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 #include "vm/bootstrap.h" 5 #include "vm/bootstrap.h"
6 6
7 #include "include/dart_api.h" 7 #include "include/dart_api.h"
8 8
9 #include "vm/bootstrap_natives.h" 9 #include "vm/bootstrap_natives.h"
10 #include "vm/compiler.h" 10 #include "vm/compiler.h"
11 #include "vm/dart_api_impl.h" 11 #include "vm/dart_api_impl.h"
12 #include "vm/object.h" 12 #include "vm/object.h"
13 #include "vm/object_store.h" 13 #include "vm/object_store.h"
14 #include "vm/symbols.h" 14 #include "vm/symbols.h"
15 15
16 namespace dart { 16 namespace dart {
17 17
18 DEFINE_FLAG(bool, print_bootstrap, false, "Print the bootstrap source."); 18 DEFINE_FLAG(bool, print_bootstrap, false, "Print the bootstrap source.");
19 19
20 #define INIT_LIBRARY(index, name, source, patch) \ 20 #define INIT_LIBRARY(index, name, source, patch) \
21 { index, \ 21 { index, \
22 "dart:"#name, source, \ 22 "dart:"#name, source, \
23 "dart:"#name"-patch", patch } \ 23 "dart:"#name"-patch", patch } \
24 24
25 typedef struct { 25 typedef struct {
26 intptr_t index_; 26 intptr_t index_;
27 const char* uri_; 27 const char* uri_;
28 const char* source_; 28 const char** source_paths_;
29 const char* patch_uri_; 29 const char* patch_uri_;
30 const char* patch_source_; 30 const char* patch_source_;
31 } bootstrap_lib_props; 31 } bootstrap_lib_props;
32 32
33 33
34 static bootstrap_lib_props bootstrap_libraries[] = { 34 static bootstrap_lib_props bootstrap_libraries[] = {
35 INIT_LIBRARY(ObjectStore::kCore, 35 INIT_LIBRARY(ObjectStore::kCore,
36 core, 36 core,
37 Bootstrap::corelib_source_, 37 Bootstrap::corelib_source_paths_,
38 Bootstrap::corelib_patch_), 38 Bootstrap::corelib_patch_),
39 INIT_LIBRARY(ObjectStore::kAsync, 39 INIT_LIBRARY(ObjectStore::kAsync,
40 async, 40 async,
41 Bootstrap::async_source_, 41 Bootstrap::async_source_paths_,
42 Bootstrap::async_patch_), 42 Bootstrap::async_patch_),
43 INIT_LIBRARY(ObjectStore::kCollection, 43 INIT_LIBRARY(ObjectStore::kCollection,
44 collection, 44 collection,
45 Bootstrap::collection_source_, 45 Bootstrap::collection_source_paths_,
46 Bootstrap::collection_patch_), 46 Bootstrap::collection_patch_),
47 INIT_LIBRARY(ObjectStore::kCollectionDev, 47 INIT_LIBRARY(ObjectStore::kCollectionDev,
48 _collection-dev, 48 _collection-dev,
49 Bootstrap::collection_dev_source_, 49 Bootstrap::collection_dev_source_paths_,
50 Bootstrap::collection_dev_patch_), 50 Bootstrap::collection_dev_patch_),
51 INIT_LIBRARY(ObjectStore::kCrypto, 51 INIT_LIBRARY(ObjectStore::kCrypto,
52 crypto, 52 crypto,
53 Bootstrap::crypto_source_, 53 Bootstrap::crypto_source_paths_,
54 NULL), 54 NULL),
55 INIT_LIBRARY(ObjectStore::kIsolate, 55 INIT_LIBRARY(ObjectStore::kIsolate,
56 isolate, 56 isolate,
57 Bootstrap::isolate_source_, 57 Bootstrap::isolate_source_paths_,
58 Bootstrap::isolate_patch_), 58 Bootstrap::isolate_patch_),
59 INIT_LIBRARY(ObjectStore::kJson, 59 INIT_LIBRARY(ObjectStore::kJson,
60 json, 60 json,
61 Bootstrap::json_source_, 61 Bootstrap::json_source_paths_,
62 Bootstrap::json_patch_), 62 Bootstrap::json_patch_),
63 INIT_LIBRARY(ObjectStore::kMath, 63 INIT_LIBRARY(ObjectStore::kMath,
64 math, 64 math,
65 Bootstrap::math_source_, 65 Bootstrap::math_source_paths_,
66 Bootstrap::math_patch_), 66 Bootstrap::math_patch_),
67 INIT_LIBRARY(ObjectStore::kMirrors, 67 INIT_LIBRARY(ObjectStore::kMirrors,
68 mirrors, 68 mirrors,
69 Bootstrap::mirrors_source_, 69 Bootstrap::mirrors_source_paths_,
70 Bootstrap::mirrors_patch_), 70 Bootstrap::mirrors_patch_),
71 INIT_LIBRARY(ObjectStore::kTypedData, 71 INIT_LIBRARY(ObjectStore::kTypedData,
72 typed_data, 72 typed_data,
73 Bootstrap::typed_data_source_, 73 Bootstrap::typed_data_source_paths_,
74 Bootstrap::typed_data_patch_), 74 Bootstrap::typed_data_patch_),
75 INIT_LIBRARY(ObjectStore::kUtf, 75 INIT_LIBRARY(ObjectStore::kUtf,
76 utf, 76 utf,
77 Bootstrap::utf_source_, 77 Bootstrap::utf_source_paths_,
78 NULL), 78 NULL),
79 INIT_LIBRARY(ObjectStore::kUri, 79 INIT_LIBRARY(ObjectStore::kUri,
80 uri, 80 uri,
81 Bootstrap::uri_source_, 81 Bootstrap::uri_source_paths_,
82 NULL), 82 NULL),
83 83
84 { ObjectStore::kNone, NULL, NULL, NULL, NULL } 84 { ObjectStore::kNone, NULL, NULL, NULL, NULL }
85 }; 85 };
86 86
87 87
88 static RawString* GetLibrarySource(intptr_t index, bool patch) { 88 static RawString* GetLibrarySource(const Library& lib,
89 // TODO(asiva): Replace with actual read of the source file. 89 const String& uri,
90 const char* source = patch ? bootstrap_libraries[index].patch_source_ : 90 bool patch) {
91 bootstrap_libraries[index].source_; 91 // First check if this is a valid boot strap library and find it's index
92 ASSERT(source != NULL); 92 // in the 'bootstrap_libraries' table above.
93 return String::New(source, Heap::kOld); 93 intptr_t index = 0;
94 const String& lib_uri = String::Handle(lib.url());
95 while (bootstrap_libraries[index].index_ != ObjectStore::kNone) {
96 if (lib_uri.Equals(bootstrap_libraries[index].uri_)) {
97 break;
98 }
99 index += 1;
100 }
101 if (bootstrap_libraries[index].index_ == ObjectStore::kNone) {
102 return String::null(); // Library is not a boot strap library.
103 }
104
105 if (patch) {
106 // TODO(asiva): Replace with actual read of the source file.
107 const char* source = bootstrap_libraries[index].patch_source_;
108 ASSERT(source != NULL);
109 return String::New(source, Heap::kOld);
110 }
111
112 // Try to read the source using the path specified for the uri.
113 const char** source_paths = bootstrap_libraries[index].source_paths_;
114 if (source_paths == NULL) {
115 return String::null(); // No path mapping information exists for library.
116 }
117 intptr_t i = 0;
118 const char* source_path = NULL;
119 while (source_paths[i] != NULL) {
120 if (uri.Equals(source_paths[i])) {
121 source_path = source_paths[i + 1];
122 break;
123 }
124 i += 2;
125 }
126 if (source_path == NULL) {
127 return String::null(); // Uri does not exist in path mapping information.
128 }
129
130 Dart_FileOpenCallback file_open = Isolate::file_open_callback();
131 Dart_FileReadCallback file_read = Isolate::file_read_callback();
132 Dart_FileCloseCallback file_close = Isolate::file_close_callback();
133 if (file_open == NULL || file_read == NULL || file_close == NULL) {
134 return String::null(); // File operations are not supported.
135 }
136
137 void* stream = (*file_open)(source_path, false);
138 if (stream == NULL) {
139 return String::null();
140 }
141
142 const uint8_t* utf8_array = NULL;
143 intptr_t file_length = -1;
144 (*file_read)(&utf8_array, &file_length, stream);
145 if (file_length == -1) {
146 return String::null();
147 }
148 ASSERT(utf8_array != NULL);
149
150 (*file_close)(stream);
151
152 return String::FromUTF8(utf8_array, file_length);
153 }
154
155
156 static RawError* Compile(const Library& library, const Script& script) {
157 if (FLAG_print_bootstrap) {
158 OS::Print("Bootstrap source '%s':\n%s\n",
159 String::Handle(script.url()).ToCString(),
160 String::Handle(script.Source()).ToCString());
161 }
162 bool update_lib_status = (script.kind() == RawScript::kScriptTag ||
163 script.kind() == RawScript::kLibraryTag);
164 if (update_lib_status) {
165 library.SetLoadInProgress();
166 }
167 const Error& error = Error::Handle(Compiler::Compile(library, script));
168 if (update_lib_status) {
169 if (error.IsNull()) {
170 library.SetLoaded();
171 } else {
172 library.SetLoadError();
173 }
174 }
175 return error.raw();
94 } 176 }
95 177
96 178
97 static Dart_Handle LoadPartSource(Isolate* isolate, 179 static Dart_Handle LoadPartSource(Isolate* isolate,
98 const Library& lib, 180 const Library& lib,
99 const String& uri) { 181 const String& uri) {
100 // TODO(asiva): For now we return an error here, once we start 182 const String& part_source = String::Handle(
101 // loading libraries from the real source this would have to call the 183 isolate, GetLibrarySource(lib, uri, false));
102 // file read callback here and invoke Compiler::Compile on it. 184 if (part_source.IsNull()) {
103 return Dart_NewApiError("Unable to load source '%s' ", uri.ToCString()); 185 const String& lib_uri = String::Handle(lib.url());
186 return Dart_NewApiError("Unable to read part file '%s' of library '%s'",
187 uri.ToCString(), lib_uri.ToCString());
188 }
189 const Script& part_script = Script::Handle(
190 isolate, Script::New(uri, part_source, RawScript::kSourceTag));
191 const Error& error = Error::Handle(isolate, Compile(lib, part_script));
192 return Api::NewHandle(isolate, error.raw());
104 } 193 }
105 194
106 195
107 static Dart_Handle BootstrapLibraryTagHandler(Dart_LibraryTag tag, 196 static Dart_Handle BootstrapLibraryTagHandler(Dart_LibraryTag tag,
108 Dart_Handle library, 197 Dart_Handle library,
109 Dart_Handle uri) { 198 Dart_Handle uri) {
110 Isolate* isolate = Isolate::Current(); 199 Isolate* isolate = Isolate::Current();
111 if (!Dart_IsLibrary(library)) { 200 if (!Dart_IsLibrary(library)) {
112 return Dart_NewApiError("not a library"); 201 return Dart_NewApiError("not a library");
113 } 202 }
114 if (!Dart_IsString(uri)) { 203 if (!Dart_IsString(uri)) {
115 return Dart_NewApiError("uri is not a string"); 204 return Dart_NewApiError("uri is not a string");
116 } 205 }
206 if (tag == kCanonicalizeUrl) {
207 // In the boot strap loader we do not try and do any canonicalization.
Ivan Posva 2013/05/02 22:23:21 We should actually prepend the lib URI if this is
siva 2013/05/03 01:58:32 Instead of prepending the lib URI here, as we woul
208 return uri;
209 }
117 const String& uri_str = Api::UnwrapStringHandle(isolate, uri); 210 const String& uri_str = Api::UnwrapStringHandle(isolate, uri);
118 ASSERT(!uri_str.IsNull()); 211 ASSERT(!uri_str.IsNull());
119 bool is_dart_scheme_uri = uri_str.StartsWith(Symbols::DartScheme());
120 if (!is_dart_scheme_uri) {
121 // The bootstrap tag handler can only handle dart scheme uris.
122 return Dart_NewApiError("Do not know how to load '%s' ",
123 uri_str.ToCString());
124 }
125 if (tag == kCanonicalizeUrl) {
126 // Dart Scheme URIs do not need any canonicalization.
127 return uri;
128 }
129 if (tag == kImportTag) { 212 if (tag == kImportTag) {
130 // We expect the core bootstrap libraries to only import other 213 // We expect the core bootstrap libraries to only import other
131 // core bootstrap libraries. 214 // core bootstrap libraries.
132 // We have precreated all the bootstrap library objects hence 215 // We have precreated all the bootstrap library objects hence
133 // we do not expect to be called back with the tag set to kImportTag. 216 // we do not expect to be called back with the tag set to kImportTag.
134 // The bootstrap process explicitly loads all the libraries one by one. 217 // The bootstrap process explicitly loads all the libraries one by one.
135 return Dart_NewApiError("Invalid import of '%s' in a bootstrap library", 218 return Dart_NewApiError("Invalid import of '%s' in a bootstrap library",
136 uri_str.ToCString()); 219 uri_str.ToCString());
137 } 220 }
138 ASSERT(tag == kSourceTag); 221 ASSERT(tag == kSourceTag);
139 const Library& lib = Api::UnwrapLibraryHandle(isolate, library); 222 const Library& lib = Api::UnwrapLibraryHandle(isolate, library);
140 ASSERT(!lib.IsNull()); 223 ASSERT(!lib.IsNull());
141 return LoadPartSource(isolate, lib, uri_str); 224 return LoadPartSource(isolate, lib, uri_str);
142 } 225 }
143 226
144 227
145 static RawError* Compile(const Library& library, const Script& script) {
146 if (FLAG_print_bootstrap) {
147 OS::Print("Bootstrap source '%s':\n%s\n",
148 String::Handle(script.url()).ToCString(),
149 String::Handle(script.Source()).ToCString());
150 }
151 library.SetLoadInProgress();
152 const Error& error = Error::Handle(Compiler::Compile(library, script));
153 if (error.IsNull()) {
154 library.SetLoaded();
155 } else {
156 library.SetLoadError();
157 }
158 return error.raw();
159 }
160
161
162 RawError* Bootstrap::LoadandCompileScripts() { 228 RawError* Bootstrap::LoadandCompileScripts() {
163 Isolate* isolate = Isolate::Current(); 229 Isolate* isolate = Isolate::Current();
164 String& uri = String::Handle(); 230 String& uri = String::Handle();
165 String& patch_uri = String::Handle(); 231 String& patch_uri = String::Handle();
166 String& source = String::Handle(); 232 String& source = String::Handle();
167 Script& script = Script::Handle(); 233 Script& script = Script::Handle();
168 Library& lib = Library::Handle(); 234 Library& lib = Library::Handle();
169 Error& error = Error::Handle(); 235 Error& error = Error::Handle();
170 Dart_LibraryTagHandler saved_tag_handler = isolate->library_tag_handler(); 236 Dart_LibraryTagHandler saved_tag_handler = isolate->library_tag_handler();
171 237
(...skipping 18 matching lines...) Expand all
190 bootstrap_libraries[i].index_, lib); 256 bootstrap_libraries[i].index_, lib);
191 i = i + 1; 257 i = i + 1;
192 } 258 }
193 259
194 // Load and compile bootstrap libraries. 260 // Load and compile bootstrap libraries.
195 i = 0; 261 i = 0;
196 while (bootstrap_libraries[i].index_ != ObjectStore::kNone) { 262 while (bootstrap_libraries[i].index_ != ObjectStore::kNone) {
197 uri = Symbols::New(bootstrap_libraries[i].uri_); 263 uri = Symbols::New(bootstrap_libraries[i].uri_);
198 lib = Library::LookupLibrary(uri); 264 lib = Library::LookupLibrary(uri);
199 ASSERT(!lib.IsNull()); 265 ASSERT(!lib.IsNull());
200 source = GetLibrarySource(i, false); 266 source = GetLibrarySource(lib, uri, false);
267 if (source.IsNull()) {
268 error ^= Api::UnwrapErrorHandle(
269 isolate, Api::NewError("Unable to find dart source for %s",
270 uri.ToCString())).raw();
271 break;
272 }
201 script = Script::New(uri, source, RawScript::kLibraryTag); 273 script = Script::New(uri, source, RawScript::kLibraryTag);
202 error = Compile(lib, script); 274 error = Compile(lib, script);
203 if (!error.IsNull()) { 275 if (!error.IsNull()) {
204 break; 276 break;
205 } 277 }
206 // If a patch exists, load and patch the script. 278 // If a patch exists, load and patch the script.
207 if (bootstrap_libraries[i].patch_source_ != NULL) { 279 if (bootstrap_libraries[i].patch_source_ != NULL) {
208 patch_uri = String::New(bootstrap_libraries[i].patch_uri_, 280 patch_uri = String::New(bootstrap_libraries[i].patch_uri_,
209 Heap::kOld); 281 Heap::kOld);
210 source = GetLibrarySource(i, true); 282 source = GetLibrarySource(lib, uri, true);
283 if (source.IsNull()) {
284 error ^= Api::UnwrapErrorHandle(
285 isolate, Api::NewError("Unable to find dart patch source for %s",
286 uri.ToCString())).raw();
287 break;
288 }
211 script = Script::New(patch_uri, source, RawScript::kPatchTag); 289 script = Script::New(patch_uri, source, RawScript::kPatchTag);
212 error = lib.Patch(script); 290 error = lib.Patch(script);
213 if (!error.IsNull()) { 291 if (!error.IsNull()) {
214 break; 292 break;
215 } 293 }
216 } 294 }
217 i = i + 1; 295 i = i + 1;
218 } 296 }
219 if (error.IsNull()) { 297 if (error.IsNull()) {
220 SetupNativeResolver(); 298 SetupNativeResolver();
221 } 299 }
222 300
223 // Exit the Dart scope. 301 // Exit the Dart scope.
224 Dart_ExitScope(); 302 Dart_ExitScope();
225 303
226 // Restore the library tag handler for the isolate. 304 // Restore the library tag handler for the isolate.
227 isolate->set_library_tag_handler(saved_tag_handler); 305 isolate->set_library_tag_handler(saved_tag_handler);
228 306
229 return error.raw(); 307 return error.raw();
230 } 308 }
231 309
232 } // namespace dart 310 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698