Chromium Code Reviews| 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 | 15 |
| 15 namespace dart { | 16 namespace dart { |
| 16 | 17 |
| 17 DEFINE_FLAG(bool, print_bootstrap, false, "Print the bootstrap source."); | 18 DEFINE_FLAG(bool, print_bootstrap, false, "Print the bootstrap source."); |
| 18 | 19 |
| 20 #define INIT_LIBRARY(index, name, source, patch) \ | |
| 21 { index, \ | |
| 22 "dart:"#name, source, \ | |
| 23 "dart:"#name"-patch", patch } \ | |
| 19 | 24 |
| 20 RawScript* Bootstrap::LoadScript(const char* url, | 25 typedef struct { |
| 21 const char* source, | 26 intptr_t index_; |
| 22 bool patch) { | 27 const char* uri_; |
| 23 return Script::New(String::Handle(String::New(url, Heap::kOld)), | 28 const char* source_; |
| 24 String::Handle(String::New(source, Heap::kOld)), | 29 const char* patch_uri_; |
| 25 patch ? RawScript::kPatchTag : RawScript::kLibraryTag); | 30 const char* patch_source_; |
| 31 } bootstrap_lib_props; | |
| 32 | |
| 33 | |
| 34 static bootstrap_lib_props bootstrap_libraries[] = { | |
| 35 INIT_LIBRARY(ObjectStore::kCore, | |
| 36 core, | |
| 37 Bootstrap::corelib_source_, | |
| 38 Bootstrap::corelib_patch_), | |
| 39 INIT_LIBRARY(ObjectStore::kAsync, | |
| 40 async, | |
| 41 Bootstrap::async_source_, | |
| 42 Bootstrap::async_patch_), | |
| 43 INIT_LIBRARY(ObjectStore::kCollection, | |
| 44 collection, | |
| 45 Bootstrap::collection_source_, | |
| 46 Bootstrap::collection_patch_), | |
| 47 INIT_LIBRARY(ObjectStore::kCollectionDev, | |
| 48 _collection-dev, | |
| 49 Bootstrap::collection_dev_source_, | |
| 50 Bootstrap::collection_dev_patch_), | |
| 51 INIT_LIBRARY(ObjectStore::kCrypto, | |
| 52 crypto, | |
| 53 Bootstrap::crypto_source_, | |
| 54 NULL), | |
| 55 INIT_LIBRARY(ObjectStore::kIsolate, | |
| 56 isolate, | |
| 57 Bootstrap::isolate_source_, | |
| 58 Bootstrap::isolate_patch_), | |
| 59 INIT_LIBRARY(ObjectStore::kJson, | |
| 60 json, | |
| 61 Bootstrap::json_source_, | |
| 62 Bootstrap::json_patch_), | |
| 63 INIT_LIBRARY(ObjectStore::kMath, | |
| 64 math, | |
| 65 Bootstrap::math_source_, | |
| 66 Bootstrap::math_patch_), | |
| 67 INIT_LIBRARY(ObjectStore::kMirrors, | |
| 68 mirrors, | |
| 69 Bootstrap::mirrors_source_, | |
| 70 Bootstrap::mirrors_patch_), | |
| 71 INIT_LIBRARY(ObjectStore::kTypedData, | |
| 72 typed_data, | |
| 73 Bootstrap::typed_data_source_, | |
| 74 Bootstrap::typed_data_patch_), | |
| 75 INIT_LIBRARY(ObjectStore::kUtf, | |
| 76 utf, | |
| 77 Bootstrap::utf_source_, | |
| 78 NULL), | |
| 79 INIT_LIBRARY(ObjectStore::kUri, | |
| 80 uri, | |
| 81 Bootstrap::uri_source_, | |
| 82 NULL), | |
| 83 | |
| 84 { ObjectStore::kNone, NULL, NULL, NULL, NULL } | |
| 85 }; | |
| 86 | |
| 87 | |
| 88 static RawString* GetLibrarySource(intptr_t index, bool patch) { | |
| 89 // TODO(asiva): Replace with actual read of the source file. | |
| 90 const char* source = patch ? bootstrap_libraries[index].patch_source_ : | |
| 91 bootstrap_libraries[index].source_; | |
| 92 ASSERT(source != NULL); | |
| 93 return String::New(source, Heap::kOld); | |
| 26 } | 94 } |
| 27 | 95 |
| 28 | 96 |
| 29 RawScript* Bootstrap::LoadAsyncScript(bool patch) { | 97 static Dart_Handle LoadPartSource(Isolate* isolate, |
| 30 const char* url = patch ? "dart:async-patch" : "dart:async"; | 98 const Library& lib, |
| 31 const char* source = patch ? async_patch_ : async_source_; | 99 const String& uri) { |
| 32 return LoadScript(url, source, patch); | 100 // TODO(asiva): For now we return an error here, once we start |
| 101 // loading libraries from the real source this would have to call the | |
| 102 // file read callback here and invoke Compiler::Compile on it. | |
| 103 return Dart_NewApiError("Unable to load source '%s' ", uri.ToCString()); | |
| 33 } | 104 } |
| 34 | 105 |
| 35 | 106 |
| 36 RawScript* Bootstrap::LoadCoreScript(bool patch) { | 107 Dart_Handle BootstrapLibraryTagHandler(Dart_LibraryTag tag, |
|
Ivan Posva
2013/04/29 21:13:47
static
siva
2013/04/29 21:47:38
Done.
| |
| 37 // TODO(iposva): Use proper library name. | 108 Dart_Handle library, |
| 38 const char* url = patch ? "dart:core-patch" : "bootstrap"; | 109 Dart_Handle uri) { |
| 39 const char* source = patch ? corelib_patch_ : corelib_source_; | 110 Isolate* isolate = Isolate::Current(); |
| 40 return LoadScript(url, source, patch); | 111 if (!Dart_IsLibrary(library)) { |
| 112 return Dart_NewApiError("not a library"); | |
| 113 } | |
| 114 if (!Dart_IsString(uri)) { | |
| 115 return Dart_NewApiError("uri is not a string"); | |
| 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 } | |
| 125 if (tag == kCanonicalizeUrl) { | |
| 126 // Dart Scheme URIs do not need any canonicalization. | |
| 127 return uri; | |
| 128 } | |
| 129 if (tag == kImportTag) { | |
| 130 // We expect the core bootstrap libraries to only import other | |
| 131 // core bootstrap libraries. | |
| 132 // We have precreated all the bootstrap library objects hence | |
| 133 // 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. | |
| 135 return Dart_NewApiError("Invalid import of '%s' in a bootstrap library", | |
| 136 uri_str.ToCString()); | |
| 137 } | |
| 138 ASSERT(tag == kSourceTag); | |
| 139 const Library& lib = Api::UnwrapLibraryHandle(isolate, library); | |
| 140 ASSERT(!lib.IsNull()); | |
| 141 return LoadPartSource(isolate, lib, uri_str); | |
| 41 } | 142 } |
| 42 | 143 |
| 43 | 144 |
| 44 RawScript* Bootstrap::LoadCollectionScript(bool patch) { | 145 RawError* Compile(const Library& library, const Script& script) { |
| 45 const char* url = patch ? "dart:collection-patch" : "dart:collection"; | |
| 46 const char* source = patch ? collection_patch_ : collection_source_; | |
| 47 return LoadScript(url, source, patch); | |
| 48 } | |
| 49 | |
| 50 | |
| 51 RawScript* Bootstrap::LoadCollectionDevScript(bool patch) { | |
| 52 const char* url = | |
| 53 patch ? "dart:_collection-dev-patch" : "dart:_collection-dev"; | |
| 54 const char* source = patch ? collection_dev_patch_ : collection_dev_source_; | |
| 55 return LoadScript(url, source, patch); | |
| 56 } | |
| 57 | |
| 58 | |
| 59 RawScript* Bootstrap::LoadCryptoScript(bool patch) { | |
| 60 const char* url = patch ? "dart:crypto-patch" : "dart:crypto"; | |
| 61 const char* source = patch ? crypto_source_ : crypto_source_; | |
| 62 return LoadScript(url, source, patch); | |
| 63 } | |
| 64 | |
| 65 | |
| 66 RawScript* Bootstrap::LoadIsolateScript(bool patch) { | |
| 67 const char* url = patch ? "dart:isolate-patch" : "dart:isolate"; | |
| 68 const char* source = patch ? isolate_patch_ : isolate_source_; | |
| 69 return LoadScript(url, source, patch); | |
| 70 } | |
| 71 | |
| 72 | |
| 73 RawScript* Bootstrap::LoadJsonScript(bool patch) { | |
| 74 const char* url = patch ? "dart:json-patch" : "dart:json"; | |
| 75 const char* source = patch ? json_patch_ : json_source_; | |
| 76 return LoadScript(url, source, patch); | |
| 77 } | |
| 78 | |
| 79 | |
| 80 RawScript* Bootstrap::LoadMathScript(bool patch) { | |
| 81 const char* url = patch ? "dart:math-patch" : "dart:math"; | |
| 82 const char* source = patch ? math_patch_ : math_source_; | |
| 83 return LoadScript(url, source, patch); | |
| 84 } | |
| 85 | |
| 86 | |
| 87 RawScript* Bootstrap::LoadMirrorsScript(bool patch) { | |
| 88 const char* url = patch ? "dart:mirrors-patch" : "dart:mirrors"; | |
| 89 const char* source = patch ? mirrors_patch_ : mirrors_source_; | |
| 90 return LoadScript(url, source, patch); | |
| 91 } | |
| 92 | |
| 93 | |
| 94 RawScript* Bootstrap::LoadTypedDataScript(bool patch) { | |
| 95 const char* url = patch ? "dart:typed_data_patch" : "dart:typed_data"; | |
| 96 const char* source = patch ? typed_data_patch_ : typed_data_source_; | |
| 97 return LoadScript(url, source, patch); | |
| 98 } | |
| 99 | |
| 100 | |
| 101 RawScript* Bootstrap::LoadUriScript(bool patch) { | |
| 102 const char* url = patch ? "dart:uri-patch" : "dart:uri"; | |
| 103 const char* source = patch ? uri_source_ : uri_source_; | |
| 104 return LoadScript(url, source, patch); | |
| 105 } | |
| 106 | |
| 107 | |
| 108 RawScript* Bootstrap::LoadUtfScript(bool patch) { | |
| 109 const char* url = patch ? "dart:utf-patch" : "dart:utf"; | |
| 110 const char* source = patch ? utf_source_ : utf_source_; | |
| 111 return LoadScript(url, source, patch); | |
| 112 } | |
| 113 | |
| 114 | |
| 115 RawError* Bootstrap::Compile(const Library& library, const Script& script) { | |
| 116 if (FLAG_print_bootstrap) { | 146 if (FLAG_print_bootstrap) { |
| 117 OS::Print("Bootstrap source '%s':\n%s\n", | 147 OS::Print("Bootstrap source '%s':\n%s\n", |
| 118 String::Handle(script.url()).ToCString(), | 148 String::Handle(script.url()).ToCString(), |
| 119 String::Handle(script.Source()).ToCString()); | 149 String::Handle(script.Source()).ToCString()); |
| 120 } | 150 } |
| 121 library.SetLoadInProgress(); | 151 library.SetLoadInProgress(); |
| 122 const Error& error = Error::Handle(Compiler::Compile(library, script)); | 152 const Error& error = Error::Handle(Compiler::Compile(library, script)); |
| 123 if (error.IsNull()) { | 153 if (error.IsNull()) { |
| 124 library.SetLoaded(); | 154 library.SetLoaded(); |
| 125 } else { | 155 } else { |
| 126 library.SetLoadError(); | 156 library.SetLoadError(); |
| 127 } | 157 } |
| 128 return error.raw(); | 158 return error.raw(); |
| 129 } | 159 } |
| 130 | 160 |
| 161 | |
| 162 RawError* Bootstrap::LoadandCompileScripts() { | |
| 163 Isolate* isolate = Isolate::Current(); | |
| 164 String& uri = String::Handle(); | |
| 165 String& patch_uri = String::Handle(); | |
| 166 String& source = String::Handle(); | |
| 167 Script& script = Script::Handle(); | |
| 168 Library& lib = Library::Handle(); | |
| 169 Error& error = Error::Handle(); | |
| 170 Dart_LibraryTagHandler saved_tag_handler = isolate->library_tag_handler(); | |
| 171 | |
| 172 // Set the library tag handler for the isolate to the bootstrap | |
| 173 // library tag handler so that we can load all the bootstrap libraries. | |
| 174 isolate->set_library_tag_handler(BootstrapLibraryTagHandler); | |
| 175 | |
| 176 // Enter the Dart Scope as we will be calling back into the library | |
| 177 // tag handler when compiling the bootstrap libraries. | |
| 178 Dart_EnterScope(); | |
| 179 | |
| 180 // Create library objects for all the bootstrap libraries. | |
| 181 intptr_t i = 0; | |
| 182 while (bootstrap_libraries[i].index_ != ObjectStore::kNone) { | |
| 183 uri = Symbols::New(bootstrap_libraries[i].uri_); | |
| 184 lib = Library::LookupLibrary(uri); | |
| 185 if (lib.IsNull()) { | |
| 186 lib = Library::NewLibraryHelper(uri, false); | |
| 187 lib.Register(); | |
| 188 } | |
| 189 isolate->object_store()->set_bootstrap_library( | |
| 190 bootstrap_libraries[i].index_, lib); | |
| 191 i = i + 1; | |
| 192 } | |
| 193 | |
| 194 // Load and compile bootstrap libraries. | |
| 195 i = 0; | |
| 196 while (bootstrap_libraries[i].index_ != ObjectStore::kNone) { | |
| 197 uri = Symbols::New(bootstrap_libraries[i].uri_); | |
| 198 lib = Library::LookupLibrary(uri); | |
| 199 ASSERT(!lib.IsNull()); | |
| 200 source = GetLibrarySource(i, false); | |
| 201 script = Script::New(uri, source, RawScript::kLibraryTag); | |
| 202 error = Compile(lib, script); | |
| 203 if (!error.IsNull()) { | |
| 204 break; | |
| 205 } | |
| 206 // If a patch exists, load and patch the script. | |
| 207 if (bootstrap_libraries[i].patch_source_ != NULL) { | |
| 208 patch_uri = String::New(bootstrap_libraries[i].patch_uri_, | |
| 209 Heap::kOld); | |
| 210 source = GetLibrarySource(i, true); | |
| 211 script = Script::New(patch_uri, source, RawScript::kPatchTag); | |
| 212 error = lib.Patch(script); | |
| 213 if (!error.IsNull()) { | |
| 214 break; | |
| 215 } | |
| 216 } | |
| 217 i = i + 1; | |
| 218 } | |
| 219 if (error.IsNull()) { | |
| 220 SetupNativeResolver(); | |
| 221 } | |
| 222 | |
| 223 // Exit the Dart scope. | |
| 224 Dart_ExitScope(); | |
| 225 | |
| 226 // Restore the library tag handler for the isolate. | |
| 227 isolate->set_library_tag_handler(saved_tag_handler); | |
| 228 | |
| 229 return error.raw(); | |
| 230 } | |
| 231 | |
| 131 } // namespace dart | 232 } // namespace dart |
| OLD | NEW |