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

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

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

Powered by Google App Engine
This is Rietveld 408576698