| 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_; |
| 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_paths_, | 37 Bootstrap::corelib_source_, |
| 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_paths_, | 41 Bootstrap::async_source_, |
| 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_paths_, | 45 Bootstrap::collection_source_, |
| 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_paths_, | 49 Bootstrap::collection_dev_source_, |
| 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_paths_, | 53 Bootstrap::crypto_source_, |
| 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_, |
| 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_paths_, | 61 Bootstrap::json_source_, |
| 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_paths_, | 65 Bootstrap::math_source_, |
| 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_paths_, | 69 Bootstrap::mirrors_source_, |
| 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_paths_, | 73 Bootstrap::typed_data_source_, |
| 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_paths_, | 77 Bootstrap::utf_source_, |
| 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_, |
| 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(const Library& lib, | 88 static RawString* GetLibrarySource(intptr_t index, bool patch) { |
| 89 const String& uri, | 89 // TODO(asiva): Replace with actual read of the source file. |
| 90 bool patch) { | 90 const char* source = patch ? bootstrap_libraries[index].patch_source_ : |
| 91 // First check if this is a valid boot strap library and find it's index | 91 bootstrap_libraries[index].source_; |
| 92 // in the 'bootstrap_libraries' table above. | 92 ASSERT(source != NULL); |
| 93 intptr_t index = 0; | 93 return String::New(source, Heap::kOld); |
| 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(); | |
| 176 } | 94 } |
| 177 | 95 |
| 178 | 96 |
| 179 static Dart_Handle LoadPartSource(Isolate* isolate, | 97 static Dart_Handle LoadPartSource(Isolate* isolate, |
| 180 const Library& lib, | 98 const Library& lib, |
| 181 const String& uri) { | 99 const String& uri) { |
| 182 const String& part_source = String::Handle( | 100 // TODO(asiva): For now we return an error here, once we start |
| 183 isolate, GetLibrarySource(lib, uri, false)); | 101 // loading libraries from the real source this would have to call the |
| 184 const String& lib_uri = String::Handle(isolate, lib.url()); | 102 // file read callback here and invoke Compiler::Compile on it. |
| 185 if (part_source.IsNull()) { | 103 return Dart_NewApiError("Unable to load source '%s' ", uri.ToCString()); |
| 186 return Dart_NewApiError("Unable to read part file '%s' of library '%s'", | |
| 187 uri.ToCString(), lib_uri.ToCString()); | |
| 188 } | |
| 189 | |
| 190 // Prepend the library URI to form a unique script URI for the part. | |
| 191 const Array& strings = Array::Handle(isolate, Array::New(3)); | |
| 192 strings.SetAt(0, lib_uri); | |
| 193 strings.SetAt(1, Symbols::Slash()); | |
| 194 strings.SetAt(2, uri); | |
| 195 const String& part_uri = String::Handle(isolate, String::ConcatAll(strings)); | |
| 196 | |
| 197 // Create a script object and compile the part. | |
| 198 const Script& part_script = Script::Handle( | |
| 199 isolate, Script::New(part_uri, part_source, RawScript::kSourceTag)); | |
| 200 const Error& error = Error::Handle(isolate, Compile(lib, part_script)); | |
| 201 return Api::NewHandle(isolate, error.raw()); | |
| 202 } | 104 } |
| 203 | 105 |
| 204 | 106 |
| 205 static Dart_Handle BootstrapLibraryTagHandler(Dart_LibraryTag tag, | 107 static Dart_Handle BootstrapLibraryTagHandler(Dart_LibraryTag tag, |
| 206 Dart_Handle library, | 108 Dart_Handle library, |
| 207 Dart_Handle uri) { | 109 Dart_Handle uri) { |
| 208 Isolate* isolate = Isolate::Current(); | 110 Isolate* isolate = Isolate::Current(); |
| 209 if (!Dart_IsLibrary(library)) { | 111 if (!Dart_IsLibrary(library)) { |
| 210 return Dart_NewApiError("not a library"); | 112 return Dart_NewApiError("not a library"); |
| 211 } | 113 } |
| 212 if (!Dart_IsString(uri)) { | 114 if (!Dart_IsString(uri)) { |
| 213 return Dart_NewApiError("uri is not a string"); | 115 return Dart_NewApiError("uri is not a string"); |
| 214 } | 116 } |
| 117 const String& uri_str = Api::UnwrapStringHandle(isolate, uri); |
| 118 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 } |
| 215 if (tag == kCanonicalizeUrl) { | 125 if (tag == kCanonicalizeUrl) { |
| 216 // In the boot strap loader we do not try and do any canonicalization. | 126 // Dart Scheme URIs do not need any canonicalization. |
| 217 return uri; | 127 return uri; |
| 218 } | 128 } |
| 219 const String& uri_str = Api::UnwrapStringHandle(isolate, uri); | |
| 220 ASSERT(!uri_str.IsNull()); | |
| 221 if (tag == kImportTag) { | 129 if (tag == kImportTag) { |
| 222 // We expect the core bootstrap libraries to only import other | 130 // We expect the core bootstrap libraries to only import other |
| 223 // core bootstrap libraries. | 131 // core bootstrap libraries. |
| 224 // We have precreated all the bootstrap library objects hence | 132 // We have precreated all the bootstrap library objects hence |
| 225 // we do not expect to be called back with the tag set to kImportTag. | 133 // we do not expect to be called back with the tag set to kImportTag. |
| 226 // The bootstrap process explicitly loads all the libraries one by one. | 134 // The bootstrap process explicitly loads all the libraries one by one. |
| 227 return Dart_NewApiError("Invalid import of '%s' in a bootstrap library", | 135 return Dart_NewApiError("Invalid import of '%s' in a bootstrap library", |
| 228 uri_str.ToCString()); | 136 uri_str.ToCString()); |
| 229 } | 137 } |
| 230 ASSERT(tag == kSourceTag); | 138 ASSERT(tag == kSourceTag); |
| 231 const Library& lib = Api::UnwrapLibraryHandle(isolate, library); | 139 const Library& lib = Api::UnwrapLibraryHandle(isolate, library); |
| 232 ASSERT(!lib.IsNull()); | 140 ASSERT(!lib.IsNull()); |
| 233 return LoadPartSource(isolate, lib, uri_str); | 141 return LoadPartSource(isolate, lib, uri_str); |
| 234 } | 142 } |
| 235 | 143 |
| 236 | 144 |
| 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 |
| 237 RawError* Bootstrap::LoadandCompileScripts() { | 162 RawError* Bootstrap::LoadandCompileScripts() { |
| 238 Isolate* isolate = Isolate::Current(); | 163 Isolate* isolate = Isolate::Current(); |
| 239 String& uri = String::Handle(); | 164 String& uri = String::Handle(); |
| 240 String& patch_uri = String::Handle(); | 165 String& patch_uri = String::Handle(); |
| 241 String& source = String::Handle(); | 166 String& source = String::Handle(); |
| 242 Script& script = Script::Handle(); | 167 Script& script = Script::Handle(); |
| 243 Library& lib = Library::Handle(); | 168 Library& lib = Library::Handle(); |
| 244 Error& error = Error::Handle(); | 169 Error& error = Error::Handle(); |
| 245 Dart_LibraryTagHandler saved_tag_handler = isolate->library_tag_handler(); | 170 Dart_LibraryTagHandler saved_tag_handler = isolate->library_tag_handler(); |
| 246 | 171 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 265 bootstrap_libraries[i].index_, lib); | 190 bootstrap_libraries[i].index_, lib); |
| 266 i = i + 1; | 191 i = i + 1; |
| 267 } | 192 } |
| 268 | 193 |
| 269 // Load and compile bootstrap libraries. | 194 // Load and compile bootstrap libraries. |
| 270 i = 0; | 195 i = 0; |
| 271 while (bootstrap_libraries[i].index_ != ObjectStore::kNone) { | 196 while (bootstrap_libraries[i].index_ != ObjectStore::kNone) { |
| 272 uri = Symbols::New(bootstrap_libraries[i].uri_); | 197 uri = Symbols::New(bootstrap_libraries[i].uri_); |
| 273 lib = Library::LookupLibrary(uri); | 198 lib = Library::LookupLibrary(uri); |
| 274 ASSERT(!lib.IsNull()); | 199 ASSERT(!lib.IsNull()); |
| 275 source = GetLibrarySource(lib, uri, false); | 200 source = GetLibrarySource(i, false); |
| 276 if (source.IsNull()) { | |
| 277 error ^= Api::UnwrapErrorHandle( | |
| 278 isolate, Api::NewError("Unable to find dart source for %s", | |
| 279 uri.ToCString())).raw(); | |
| 280 break; | |
| 281 } | |
| 282 script = Script::New(uri, source, RawScript::kLibraryTag); | 201 script = Script::New(uri, source, RawScript::kLibraryTag); |
| 283 error = Compile(lib, script); | 202 error = Compile(lib, script); |
| 284 if (!error.IsNull()) { | 203 if (!error.IsNull()) { |
| 285 break; | 204 break; |
| 286 } | 205 } |
| 287 // If a patch exists, load and patch the script. | 206 // If a patch exists, load and patch the script. |
| 288 if (bootstrap_libraries[i].patch_source_ != NULL) { | 207 if (bootstrap_libraries[i].patch_source_ != NULL) { |
| 289 patch_uri = String::New(bootstrap_libraries[i].patch_uri_, | 208 patch_uri = String::New(bootstrap_libraries[i].patch_uri_, |
| 290 Heap::kOld); | 209 Heap::kOld); |
| 291 source = GetLibrarySource(lib, uri, true); | 210 source = GetLibrarySource(i, true); |
| 292 if (source.IsNull()) { | |
| 293 error ^= Api::UnwrapErrorHandle( | |
| 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); | 211 script = Script::New(patch_uri, source, RawScript::kPatchTag); |
| 299 error = lib.Patch(script); | 212 error = lib.Patch(script); |
| 300 if (!error.IsNull()) { | 213 if (!error.IsNull()) { |
| 301 break; | 214 break; |
| 302 } | 215 } |
| 303 } | 216 } |
| 304 i = i + 1; | 217 i = i + 1; |
| 305 } | 218 } |
| 306 if (error.IsNull()) { | 219 if (error.IsNull()) { |
| 307 SetupNativeResolver(); | 220 SetupNativeResolver(); |
| 308 } | 221 } |
| 309 | 222 |
| 310 // Exit the Dart scope. | 223 // Exit the Dart scope. |
| 311 Dart_ExitScope(); | 224 Dart_ExitScope(); |
| 312 | 225 |
| 313 // Restore the library tag handler for the isolate. | 226 // Restore the library tag handler for the isolate. |
| 314 isolate->set_library_tag_handler(saved_tag_handler); | 227 isolate->set_library_tag_handler(saved_tag_handler); |
| 315 | 228 |
| 316 return error.raw(); | 229 return error.raw(); |
| 317 } | 230 } |
| 318 | 231 |
| 319 } // namespace dart | 232 } // namespace dart |
| OLD | NEW |