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

Side by Side Diff: runtime/bin/loader.cc

Issue 2146093002: Support loading dart-ext from within a package when using a package map (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: self review Created 4 years, 5 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
« no previous file with comments | « runtime/bin/loader.h ('k') | runtime/bin/vmservice/loader.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, 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 5
6 #include "bin/loader.h" 6 #include "bin/loader.h"
7 7
8 #include "bin/builtin.h" 8 #include "bin/builtin.h"
9 #include "bin/dartutils.h" 9 #include "bin/dartutils.h"
10 #include "bin/extensions.h" 10 #include "bin/extensions.h"
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 Dart_NewStringFromCString(working_directory)); 145 Dart_NewStringFromCString(working_directory));
146 Dart_ListSetAt(request, 7, 146 Dart_ListSetAt(request, 7,
147 (root_script_uri == NULL) ? Dart_Null() : 147 (root_script_uri == NULL) ? Dart_Null() :
148 Dart_NewStringFromCString(root_script_uri)); 148 Dart_NewStringFromCString(root_script_uri));
149 149
150 bool success = Dart_Post(loader_port, request); 150 bool success = Dart_Post(loader_port, request);
151 ASSERT(success); 151 ASSERT(success);
152 } 152 }
153 153
154 154
155 void Loader::SendImportExtensionRequest(Dart_Handle url,
156 Dart_Handle library_url) {
157 // This port delivers loading messages to the service isolate.
158 Dart_Port loader_port = Builtin::LoadPort();
159 ASSERT(loader_port != ILLEGAL_PORT);
160
161 // Keep in sync with loader.dart.
162 const intptr_t _Dart_kImportExtension = 9;
163
164 Dart_Handle request = Dart_NewList(6);
165 Dart_ListSetAt(request, 0, trace_loader ? Dart_True() : Dart_False());
166 Dart_ListSetAt(request, 1, Dart_NewInteger(Dart_GetMainPortId()));
167 Dart_ListSetAt(request, 2, Dart_NewInteger(_Dart_kImportExtension));
168 Dart_ListSetAt(request, 3, Dart_NewSendPort(port_));
169
170 Dart_ListSetAt(request, 4, url);
171 Dart_ListSetAt(request, 5, library_url);
172
173 if (Dart_Post(loader_port, request)) {
174 MonitorLocker ml(monitor_);
175 pending_operations_++;
176 }
177 }
178
179
155 // Forward a request from the tag handler to the service isolate. 180 // Forward a request from the tag handler to the service isolate.
156 void Loader::SendRequest(Dart_LibraryTag tag, 181 void Loader::SendRequest(Dart_LibraryTag tag,
157 Dart_Handle url, 182 Dart_Handle url,
158 Dart_Handle library_url) { 183 Dart_Handle library_url) {
159 // This port delivers loading messages to the service isolate. 184 // This port delivers loading messages to the service isolate.
160 Dart_Port loader_port = Builtin::LoadPort(); 185 Dart_Port loader_port = Builtin::LoadPort();
161 ASSERT(loader_port != ILLEGAL_PORT); 186 ASSERT(loader_port != ILLEGAL_PORT);
162 187
163 Dart_Handle request = Dart_NewList(6); 188 Dart_Handle request = Dart_NewList(6);
164 Dart_ListSetAt(request, 0, trace_loader ? Dart_True() : Dart_False()); 189 Dart_ListSetAt(request, 0, trace_loader ? Dart_True() : Dart_False());
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 Dart_Handle res = Dart_LibraryHandleError(library, error); 247 Dart_Handle res = Dart_LibraryHandleError(library, error);
223 if (Dart_IsNull(res)) { 248 if (Dart_IsNull(res)) {
224 // Error was handled by library. 249 // Error was handled by library.
225 return true; 250 return true;
226 } 251 }
227 } 252 }
228 return false; 253 return false;
229 } 254 }
230 255
231 256
257 static bool IsWindowsHost() {
258 #if defined(TARGET_OS_WINDOWS)
259 return true;
260 #else // defined(TARGET_OS_WINDOWS)
261 return false;
262 #endif // defined(TARGET_OS_WINDOWS)
263 }
264
265
232 bool Loader::ProcessResultLocked(Loader* loader, Loader::IOResult* result) { 266 bool Loader::ProcessResultLocked(Loader* loader, Loader::IOResult* result) {
233 // We have to copy everything we care about out of |result| because after 267 // We have to copy everything we care about out of |result| because after
234 // dropping the lock below |result| may no longer valid. 268 // dropping the lock below |result| may no longer valid.
235 Dart_Handle uri = 269 Dart_Handle uri =
236 Dart_NewStringFromCString(reinterpret_cast<char*>(result->uri)); 270 Dart_NewStringFromCString(reinterpret_cast<char*>(result->uri));
237 Dart_Handle library_uri = Dart_Null(); 271 Dart_Handle library_uri = Dart_Null();
238 if (result->library_uri != NULL) { 272 if (result->library_uri != NULL) {
239 library_uri = 273 library_uri =
240 Dart_NewStringFromCString(reinterpret_cast<char*>(result->library_uri)); 274 Dart_NewStringFromCString(reinterpret_cast<char*>(result->library_uri));
241 } 275 }
242 276
243 // A negative result tag indicates a loading error occurred in the service 277 // A negative result tag indicates a loading error occurred in the service
244 // isolate. The payload is a C string of the error message. 278 // isolate. The payload is a C string of the error message.
245 if (result->tag < 0) { 279 if (result->tag < 0) {
246 Dart_Handle library = Dart_LookupLibrary(uri); 280 Dart_Handle library = Dart_LookupLibrary(uri);
247 Dart_Handle error = Dart_NewStringFromUTF8(result->payload, 281 Dart_Handle error = Dart_NewStringFromUTF8(result->payload,
248 result->payload_length); 282 result->payload_length);
249 // If a library with the given uri exists, give it a chance to handle 283 // If a library with the given uri exists, give it a chance to handle
250 // the error. If the load requests stems from a deferred library load, 284 // the error. If the load requests stems from a deferred library load,
251 // an IO error is not fatal. 285 // an IO error is not fatal.
252 if (LibraryHandleError(library, error)) { 286 if (LibraryHandleError(library, error)) {
253 return true; 287 return true;
254 } 288 }
255 // Fall through 289 // Fall through
256 loader->error_ = Dart_NewUnhandledExceptionError(error); 290 loader->error_ = Dart_NewUnhandledExceptionError(error);
257 return false; 291 return false;
258 } 292 }
259 293
294 const intptr_t _Dart_kImportExtension = 9;
siva 2016/07/13 21:25:14 This is repeated twice in this file whu not put it
Cutch 2016/07/13 22:18:24 Done.
295 if (result->tag == _Dart_kImportExtension) {
296 ASSERT(library_uri != Dart_Null());
297 Dart_Handle library = Dart_LookupLibrary(library_uri);
siva 2016/07/13 21:25:14 The return library object could be an error object
Cutch 2016/07/13 22:18:24 I check for it before scheduling with the service
298 const char* lib_path_str = reinterpret_cast<const char*>(result->payload);
299 const char* extension_uri = reinterpret_cast<const char*>(result->uri);
300 const char* extension_path = DartUtils::RemoveScheme(extension_uri);
301 if (strchr(extension_path, '/') != NULL ||
302 (IsWindowsHost() && strchr(extension_path, '\\') != NULL)) {
303 loader->error_ = DartUtils::NewError(
304 "Relative paths for dart extensions are not supported: '%s'",
305 extension_path);
306 return false;
307 }
308 Extensions::LoadExtension(lib_path_str,
309 extension_path,
310 library);
311 return true;
312 }
260 313
261 // Check for payload and load accordingly. 314 // Check for payload and load accordingly.
262 bool is_snapshot = false; 315 bool is_snapshot = false;
263 const uint8_t* payload = result->payload; 316 const uint8_t* payload = result->payload;
264 intptr_t payload_length = result->payload_length; 317 intptr_t payload_length = result->payload_length;
265 payload = 318 payload =
266 DartUtils::SniffForMagicNumber(payload, 319 DartUtils::SniffForMagicNumber(payload,
267 &payload_length, 320 &payload_length,
268 &is_snapshot); 321 &is_snapshot);
269 Dart_Handle source = Dart_Null(); 322 Dart_Handle source = Dart_Null();
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 } 401 }
349 pending_operations_--; 402 pending_operations_--;
350 ASSERT(hit_error || (pending_operations_ >= 0)); 403 ASSERT(hit_error || (pending_operations_ >= 0));
351 results_[i].Cleanup(); 404 results_[i].Cleanup();
352 } 405 }
353 results_length_ = 0; 406 results_length_ = 0;
354 return !hit_error; 407 return !hit_error;
355 } 408 }
356 409
357 410
358 static bool IsWindowsHost() {
359 #if defined(TARGET_OS_WINDOWS)
360 return true;
361 #else // defined(TARGET_OS_WINDOWS)
362 return false;
363 #endif // defined(TARGET_OS_WINDOWS)
364 }
365
366
367 void Loader::InitForSnapshot(const char* snapshot_uri) { 411 void Loader::InitForSnapshot(const char* snapshot_uri) {
368 IsolateData* isolate_data = 412 IsolateData* isolate_data =
369 reinterpret_cast<IsolateData*>(Dart_CurrentIsolateData()); 413 reinterpret_cast<IsolateData*>(Dart_CurrentIsolateData());
370 ASSERT(isolate_data != NULL); 414 ASSERT(isolate_data != NULL);
371 ASSERT(!isolate_data->HasLoader()); 415 ASSERT(!isolate_data->HasLoader());
372 // Setup a loader. The constructor does a bunch of leg work. 416 // Setup a loader. The constructor does a bunch of leg work.
373 Loader* loader = new Loader(isolate_data); 417 Loader* loader = new Loader(isolate_data);
374 // Send the init message. 418 // Send the init message.
375 loader->Init(isolate_data->package_root, 419 loader->Init(isolate_data->package_root,
376 isolate_data->packages_file, 420 isolate_data->packages_file,
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
458 if (is_dart_scheme_url || is_dart_library) { 502 if (is_dart_scheme_url || is_dart_library) {
459 return DartColonLibraryTagHandler(tag, 503 return DartColonLibraryTagHandler(tag,
460 library, 504 library,
461 url, 505 url,
462 library_url_string, 506 library_url_string,
463 url_string); 507 url_string);
464 } 508 }
465 } 509 }
466 510
467 if (DartUtils::IsDartExtensionSchemeURL(url_string)) { 511 if (DartUtils::IsDartExtensionSchemeURL(url_string)) {
468 // Load a native code shared library to use in a native extension 512 // Handle early error cases for dart-ext: imports.
469 if (tag != Dart_kImportTag) { 513 if (tag != Dart_kImportTag) {
470 return DartUtils::NewError("Dart extensions must use import: '%s'", 514 return DartUtils::NewError("Dart extensions must use import: '%s'",
471 url_string); 515 url_string);
472 } 516 }
473 Dart_Handle library_url = Dart_LibraryUrl(library); 517 Dart_Handle library_url = Dart_LibraryUrl(library);
474 if (Dart_IsError(library_url)) { 518 if (Dart_IsError(library_url)) {
475 return library_url; 519 return library_url;
476 } 520 }
477 Dart_Handle library_file_path = DartUtils::LibraryFilePath(library_url);
478 const char* lib_path_str = NULL;
479 Dart_StringToCString(library_file_path, &lib_path_str);
480 const char* extension_path = DartUtils::RemoveScheme(url_string);
481 if (strchr(extension_path, '/') != NULL ||
482 (IsWindowsHost() && strchr(extension_path, '\\') != NULL)) {
483 return DartUtils::NewError(
484 "Relative paths for dart extensions are not supported: '%s'",
485 extension_path);
486 }
487 return Extensions::LoadExtension(lib_path_str,
488 extension_path,
489 library);
490 } 521 }
491 522
492 IsolateData* isolate_data = 523 IsolateData* isolate_data =
493 reinterpret_cast<IsolateData*>(Dart_CurrentIsolateData()); 524 reinterpret_cast<IsolateData*>(Dart_CurrentIsolateData());
494 ASSERT(isolate_data != NULL); 525 ASSERT(isolate_data != NULL);
495 526
496 // Grab this isolate's loader. 527 // Grab this isolate's loader.
497 Loader* loader = NULL; 528 Loader* loader = NULL;
498 529
499 // The outer invocation of the tag handler for this isolate. We make the outer 530 // The outer invocation of the tag handler for this isolate. We make the outer
(...skipping 16 matching lines...) Expand all
516 } else { 547 } else {
517 ASSERT(tag != Dart_kScriptTag); 548 ASSERT(tag != Dart_kScriptTag);
518 // The isolate has a loader -- this is an inner invocation that will queue 549 // The isolate has a loader -- this is an inner invocation that will queue
519 // work with the service isolate. 550 // work with the service isolate.
520 // Use the existing loader. 551 // Use the existing loader.
521 loader = isolate_data->loader(); 552 loader = isolate_data->loader();
522 } 553 }
523 ASSERT(loader != NULL); 554 ASSERT(loader != NULL);
524 ASSERT(isolate_data->HasLoader()); 555 ASSERT(isolate_data->HasLoader());
525 556
526 loader->SendRequest(tag, 557 if (DartUtils::IsDartExtensionSchemeURL(url_string)) {
527 url, 558 loader->SendImportExtensionRequest(url, Dart_LibraryUrl(library));
528 (library != Dart_Null()) ? 559 } else {
529 Dart_LibraryUrl(library) : Dart_Null()); 560 loader->SendRequest(tag,
561 url,
562 (library != Dart_Null()) ?
563 Dart_LibraryUrl(library) : Dart_Null());
564 }
565
530 566
531 if (blocking_call) { 567 if (blocking_call) {
532 // The outer invocation of the tag handler will block here until all nested 568 // The outer invocation of the tag handler will block here until all nested
533 // invocations complete. 569 // invocations complete.
534 loader->BlockUntilComplete(ProcessResultLocked); 570 loader->BlockUntilComplete(ProcessResultLocked);
535 571
536 // Remember the error (if any). 572 // Remember the error (if any).
537 Dart_Handle error = loader->error(); 573 Dart_Handle error = loader->error();
538 // Destroy the loader. The destructor does a bunch of leg work. 574 // Destroy the loader. The destructor does a bunch of leg work.
539 delete loader; 575 delete loader;
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
695 MutexLocker ml(loader_infos_lock_); 731 MutexLocker ml(loader_infos_lock_);
696 Loader* loader = LoaderForLocked(dest_port_id); 732 Loader* loader = LoaderForLocked(dest_port_id);
697 if (loader == NULL) { 733 if (loader == NULL) {
698 return; 734 return;
699 } 735 }
700 loader->QueueMessage(message); 736 loader->QueueMessage(message);
701 } 737 }
702 738
703 } // namespace bin 739 } // namespace bin
704 } // namespace dart 740 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/bin/loader.h ('k') | runtime/bin/vmservice/loader.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698