| OLD | NEW |
| 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_paths_; | 28 const char** source_paths_; |
| 29 const char* patch_uri_; | 29 const char* patch_uri_; |
| 30 const char* patch_source_; | 30 const char** patch_paths_; |
| 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_paths_, | 37 Bootstrap::corelib_source_paths_, |
| 38 Bootstrap::corelib_patch_), | 38 Bootstrap::corelib_patch_paths_), |
| 39 INIT_LIBRARY(ObjectStore::kAsync, | 39 INIT_LIBRARY(ObjectStore::kAsync, |
| 40 async, | 40 async, |
| 41 Bootstrap::async_source_paths_, | 41 Bootstrap::async_source_paths_, |
| 42 Bootstrap::async_patch_), | 42 Bootstrap::async_patch_paths_), |
| 43 INIT_LIBRARY(ObjectStore::kCollection, | 43 INIT_LIBRARY(ObjectStore::kCollection, |
| 44 collection, | 44 collection, |
| 45 Bootstrap::collection_source_paths_, | 45 Bootstrap::collection_source_paths_, |
| 46 Bootstrap::collection_patch_), | 46 Bootstrap::collection_patch_paths_), |
| 47 INIT_LIBRARY(ObjectStore::kCollectionDev, | 47 INIT_LIBRARY(ObjectStore::kCollectionDev, |
| 48 _collection-dev, | 48 _collection-dev, |
| 49 Bootstrap::collection_dev_source_paths_, | 49 Bootstrap::collection_dev_source_paths_, |
| 50 Bootstrap::collection_dev_patch_), | 50 Bootstrap::collection_dev_patch_paths_), |
| 51 INIT_LIBRARY(ObjectStore::kCrypto, | 51 INIT_LIBRARY(ObjectStore::kCrypto, |
| 52 crypto, | 52 crypto, |
| 53 Bootstrap::crypto_source_paths_, | 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_paths_, | 57 Bootstrap::isolate_source_paths_, |
| 58 Bootstrap::isolate_patch_), | 58 Bootstrap::isolate_patch_paths_), |
| 59 INIT_LIBRARY(ObjectStore::kJson, | 59 INIT_LIBRARY(ObjectStore::kJson, |
| 60 json, | 60 json, |
| 61 Bootstrap::json_source_paths_, | 61 Bootstrap::json_source_paths_, |
| 62 Bootstrap::json_patch_), | 62 Bootstrap::json_patch_paths_), |
| 63 INIT_LIBRARY(ObjectStore::kMath, | 63 INIT_LIBRARY(ObjectStore::kMath, |
| 64 math, | 64 math, |
| 65 Bootstrap::math_source_paths_, | 65 Bootstrap::math_source_paths_, |
| 66 Bootstrap::math_patch_), | 66 Bootstrap::math_patch_paths_), |
| 67 INIT_LIBRARY(ObjectStore::kMirrors, | 67 INIT_LIBRARY(ObjectStore::kMirrors, |
| 68 mirrors, | 68 mirrors, |
| 69 Bootstrap::mirrors_source_paths_, | 69 Bootstrap::mirrors_source_paths_, |
| 70 Bootstrap::mirrors_patch_), | 70 Bootstrap::mirrors_patch_paths_), |
| 71 INIT_LIBRARY(ObjectStore::kTypedData, | 71 INIT_LIBRARY(ObjectStore::kTypedData, |
| 72 typed_data, | 72 typed_data, |
| 73 Bootstrap::typed_data_source_paths_, | 73 Bootstrap::typed_data_source_paths_, |
| 74 Bootstrap::typed_data_patch_), | 74 Bootstrap::typed_data_patch_paths_), |
| 75 INIT_LIBRARY(ObjectStore::kUtf, | 75 INIT_LIBRARY(ObjectStore::kUtf, |
| 76 utf, | 76 utf, |
| 77 Bootstrap::utf_source_paths_, | 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_paths_, | 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 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 95 while (bootstrap_libraries[index].index_ != ObjectStore::kNone) { | 95 while (bootstrap_libraries[index].index_ != ObjectStore::kNone) { |
| 96 if (lib_uri.Equals(bootstrap_libraries[index].uri_)) { | 96 if (lib_uri.Equals(bootstrap_libraries[index].uri_)) { |
| 97 break; | 97 break; |
| 98 } | 98 } |
| 99 index += 1; | 99 index += 1; |
| 100 } | 100 } |
| 101 if (bootstrap_libraries[index].index_ == ObjectStore::kNone) { | 101 if (bootstrap_libraries[index].index_ == ObjectStore::kNone) { |
| 102 return String::null(); // Library is not a boot strap library. | 102 return String::null(); // Library is not a boot strap library. |
| 103 } | 103 } |
| 104 | 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. | 105 // Try to read the source using the path specified for the uri. |
| 113 const char** source_paths = bootstrap_libraries[index].source_paths_; | 106 const char** source_paths = patch ? |
| 107 bootstrap_libraries[index].patch_paths_ : |
| 108 bootstrap_libraries[index].source_paths_; |
| 114 if (source_paths == NULL) { | 109 if (source_paths == NULL) { |
| 115 return String::null(); // No path mapping information exists for library. | 110 return String::null(); // No path mapping information exists for library. |
| 116 } | 111 } |
| 117 intptr_t i = 0; | 112 intptr_t i = 0; |
| 118 const char* source_path = NULL; | 113 const char* source_path = NULL; |
| 119 while (source_paths[i] != NULL) { | 114 while (source_paths[i] != NULL) { |
| 120 if (uri.Equals(source_paths[i])) { | 115 if (uri.Equals(source_paths[i])) { |
| 121 source_path = source_paths[i + 1]; | 116 source_path = source_paths[i + 1]; |
| 122 break; | 117 break; |
| 123 } | 118 } |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 return Dart_NewApiError("Invalid import of '%s' in a bootstrap library", | 222 return Dart_NewApiError("Invalid import of '%s' in a bootstrap library", |
| 228 uri_str.ToCString()); | 223 uri_str.ToCString()); |
| 229 } | 224 } |
| 230 ASSERT(tag == kSourceTag); | 225 ASSERT(tag == kSourceTag); |
| 231 const Library& lib = Api::UnwrapLibraryHandle(isolate, library); | 226 const Library& lib = Api::UnwrapLibraryHandle(isolate, library); |
| 232 ASSERT(!lib.IsNull()); | 227 ASSERT(!lib.IsNull()); |
| 233 return LoadPartSource(isolate, lib, uri_str); | 228 return LoadPartSource(isolate, lib, uri_str); |
| 234 } | 229 } |
| 235 | 230 |
| 236 | 231 |
| 232 static RawError* LoadPatchFiles(Isolate* isolate, |
| 233 const Library& lib, |
| 234 const String& patch_uri, |
| 235 const char** patch_files) { |
| 236 String& patch_file_uri = String::Handle(isolate); |
| 237 String& source = String::Handle(isolate); |
| 238 Script& script = Script::Handle(isolate); |
| 239 Error& error = Error::Handle(isolate); |
| 240 const Array& strings = Array::Handle(isolate, Array::New(3)); |
| 241 strings.SetAt(0, patch_uri); |
| 242 strings.SetAt(1, Symbols::Slash()); |
| 243 intptr_t j = 0; |
| 244 while (patch_files[j] != NULL) { |
| 245 patch_file_uri = String::New(patch_files[j]); |
| 246 source = GetLibrarySource(lib, patch_file_uri, true); |
| 247 if (source.IsNull()) { |
| 248 return Api::UnwrapErrorHandle( |
| 249 isolate, |
| 250 Api::NewError("Unable to find dart patch source for %s", |
| 251 patch_file_uri.ToCString())).raw(); |
| 252 } |
| 253 // Prepend the patch library URI to form a unique script URI for the patch. |
| 254 strings.SetAt(2, patch_file_uri); |
| 255 patch_file_uri = String::ConcatAll(strings); |
| 256 script = Script::New(patch_file_uri, source, RawScript::kPatchTag); |
| 257 error = lib.Patch(script); |
| 258 if (!error.IsNull()) { |
| 259 return error.raw(); |
| 260 } |
| 261 j += 2; |
| 262 } |
| 263 return Error::null(); |
| 264 } |
| 265 |
| 266 |
| 237 RawError* Bootstrap::LoadandCompileScripts() { | 267 RawError* Bootstrap::LoadandCompileScripts() { |
| 238 Isolate* isolate = Isolate::Current(); | 268 Isolate* isolate = Isolate::Current(); |
| 239 String& uri = String::Handle(); | 269 String& uri = String::Handle(); |
| 240 String& patch_uri = String::Handle(); | 270 String& patch_uri = String::Handle(); |
| 241 String& source = String::Handle(); | 271 String& source = String::Handle(); |
| 242 Script& script = Script::Handle(); | 272 Script& script = Script::Handle(); |
| 243 Library& lib = Library::Handle(); | 273 Library& lib = Library::Handle(); |
| 244 Error& error = Error::Handle(); | 274 Error& error = Error::Handle(); |
| 245 Dart_LibraryTagHandler saved_tag_handler = isolate->library_tag_handler(); | 275 Dart_LibraryTagHandler saved_tag_handler = isolate->library_tag_handler(); |
| 246 | 276 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 259 lib = Library::LookupLibrary(uri); | 289 lib = Library::LookupLibrary(uri); |
| 260 if (lib.IsNull()) { | 290 if (lib.IsNull()) { |
| 261 lib = Library::NewLibraryHelper(uri, false); | 291 lib = Library::NewLibraryHelper(uri, false); |
| 262 lib.Register(); | 292 lib.Register(); |
| 263 } | 293 } |
| 264 isolate->object_store()->set_bootstrap_library( | 294 isolate->object_store()->set_bootstrap_library( |
| 265 bootstrap_libraries[i].index_, lib); | 295 bootstrap_libraries[i].index_, lib); |
| 266 i = i + 1; | 296 i = i + 1; |
| 267 } | 297 } |
| 268 | 298 |
| 269 // Load and compile bootstrap libraries. | 299 // Load, compile and patch bootstrap libraries. |
| 270 i = 0; | 300 i = 0; |
| 271 while (bootstrap_libraries[i].index_ != ObjectStore::kNone) { | 301 while (bootstrap_libraries[i].index_ != ObjectStore::kNone) { |
| 272 uri = Symbols::New(bootstrap_libraries[i].uri_); | 302 uri = Symbols::New(bootstrap_libraries[i].uri_); |
| 273 lib = Library::LookupLibrary(uri); | 303 lib = Library::LookupLibrary(uri); |
| 274 ASSERT(!lib.IsNull()); | 304 ASSERT(!lib.IsNull()); |
| 275 source = GetLibrarySource(lib, uri, false); | 305 source = GetLibrarySource(lib, uri, false); |
| 276 if (source.IsNull()) { | 306 if (source.IsNull()) { |
| 277 error ^= Api::UnwrapErrorHandle( | 307 error ^= Api::UnwrapErrorHandle( |
| 278 isolate, Api::NewError("Unable to find dart source for %s", | 308 isolate, Api::NewError("Unable to find dart source for %s", |
| 279 uri.ToCString())).raw(); | 309 uri.ToCString())).raw(); |
| 280 break; | 310 break; |
| 281 } | 311 } |
| 282 script = Script::New(uri, source, RawScript::kLibraryTag); | 312 script = Script::New(uri, source, RawScript::kLibraryTag); |
| 283 error = Compile(lib, script); | 313 error = Compile(lib, script); |
| 284 if (!error.IsNull()) { | 314 if (!error.IsNull()) { |
| 285 break; | 315 break; |
| 286 } | 316 } |
| 287 // If a patch exists, load and patch the script. | 317 // If a patch exists, load and patch the script. |
| 288 if (bootstrap_libraries[i].patch_source_ != NULL) { | 318 if (bootstrap_libraries[i].patch_paths_ != NULL) { |
| 289 patch_uri = String::New(bootstrap_libraries[i].patch_uri_, | 319 patch_uri = Symbols::New(bootstrap_libraries[i].patch_uri_); |
| 290 Heap::kOld); | 320 error = LoadPatchFiles(isolate, |
| 291 source = GetLibrarySource(lib, uri, true); | 321 lib, |
| 292 if (source.IsNull()) { | 322 patch_uri, |
| 293 error ^= Api::UnwrapErrorHandle( | 323 bootstrap_libraries[i].patch_paths_); |
| 294 isolate, Api::NewError("Unable to find dart patch source for %s", | |
| 295 uri.ToCString())).raw(); | |
| 296 break; | |
| 297 } | |
| 298 script = Script::New(patch_uri, source, RawScript::kPatchTag); | |
| 299 error = lib.Patch(script); | |
| 300 if (!error.IsNull()) { | 324 if (!error.IsNull()) { |
| 301 break; | 325 break; |
| 302 } | 326 } |
| 303 } | 327 } |
| 304 i = i + 1; | 328 i = i + 1; |
| 305 } | 329 } |
| 306 if (error.IsNull()) { | 330 if (error.IsNull()) { |
| 307 SetupNativeResolver(); | 331 SetupNativeResolver(); |
| 308 } | 332 } |
| 309 | 333 |
| 310 // Exit the Dart scope. | 334 // Exit the Dart scope. |
| 311 Dart_ExitScope(); | 335 Dart_ExitScope(); |
| 312 | 336 |
| 313 // Restore the library tag handler for the isolate. | 337 // Restore the library tag handler for the isolate. |
| 314 isolate->set_library_tag_handler(saved_tag_handler); | 338 isolate->set_library_tag_handler(saved_tag_handler); |
| 315 | 339 |
| 316 return error.raw(); | 340 return error.raw(); |
| 317 } | 341 } |
| 318 | 342 |
| 319 } // namespace dart | 343 } // namespace dart |
| OLD | NEW |