Skip to content

Commit b07a9dd

Browse files
committed
- Implement .packages specification.
Limitation: Currently only works for file: based .package files. If a script is loaded from http: then the VM currently assumes a co-located packages/ directory. [email protected] Review URL: https://codereview.chromium.org//1232593003 .
1 parent ef11d05 commit b07a9dd

11 files changed

+686
-88
lines changed

runtime/bin/builtin.dart

Lines changed: 228 additions & 36 deletions
Large diffs are not rendered by default.

runtime/bin/dartutils.cc

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -482,12 +482,6 @@ void DartUtils::WriteMagicNumber(File* file) {
482482
Dart_Handle DartUtils::LoadScript(const char* script_uri,
483483
Dart_Handle builtin_lib) {
484484
Dart_Handle uri = Dart_NewStringFromCString(script_uri);
485-
486-
Dart_Port load_port = Dart_ServiceWaitForLoadPort();
487-
if (load_port == ILLEGAL_PORT) {
488-
return NewDartUnsupportedError("Service did not return load port.");
489-
}
490-
Builtin::SetLoadPort(load_port);
491485
IsolateData* isolate_data =
492486
reinterpret_cast<IsolateData*>(Dart_CurrentIsolateData());
493487
Dart_TimelineAsyncBegin("LoadScript", &(isolate_data->load_async_id));
@@ -632,11 +626,12 @@ void FUNCTION_NAME(Builtin_GetCurrentDirectory)(Dart_NativeArguments args) {
632626
}
633627

634628

635-
void DartUtils::PrepareBuiltinLibrary(Dart_Handle builtin_lib,
636-
Dart_Handle internal_lib,
637-
bool is_service_isolate,
638-
bool trace_loading,
639-
const char* package_root) {
629+
Dart_Handle DartUtils::PrepareBuiltinLibrary(Dart_Handle builtin_lib,
630+
Dart_Handle internal_lib,
631+
bool is_service_isolate,
632+
bool trace_loading,
633+
const char* package_root,
634+
const char* packages_file) {
640635
// Setup the internal library's 'internalPrint' function.
641636
Dart_Handle print = Dart_Invoke(
642637
builtin_lib, NewString("_getPrintClosure"), 0, NULL);
@@ -655,16 +650,20 @@ void DartUtils::PrepareBuiltinLibrary(Dart_Handle builtin_lib,
655650
NewString("_traceLoading"), Dart_True());
656651
DART_CHECK_VALID(result);
657652
}
658-
}
659-
660-
if (!is_service_isolate) {
661653
// Set current working directory.
662654
result = SetWorkingDirectory(builtin_lib);
663655
DART_CHECK_VALID(result);
656+
// Wait for the service isolate to initialize the load port.
657+
Dart_Port load_port = Dart_ServiceWaitForLoadPort();
658+
if (load_port == ILLEGAL_PORT) {
659+
return NewDartUnsupportedError("Service did not return load port.");
660+
}
661+
Builtin::SetLoadPort(load_port);
664662
}
665663

666664
// Set up package root if specified.
667665
if (package_root != NULL) {
666+
ASSERT(packages_file == NULL);
668667
result = NewString(package_root);
669668
DART_CHECK_VALID(result);
670669
const int kNumArgs = 1;
@@ -675,7 +674,19 @@ void DartUtils::PrepareBuiltinLibrary(Dart_Handle builtin_lib,
675674
kNumArgs,
676675
dart_args);
677676
DART_CHECK_VALID(result);
677+
} else if (packages_file != NULL) {
678+
result = NewString(packages_file);
679+
DART_CHECK_VALID(result);
680+
const int kNumArgs = 1;
681+
Dart_Handle dart_args[kNumArgs];
682+
dart_args[0] = result;
683+
result = Dart_Invoke(builtin_lib,
684+
NewString("_loadPackagesMap"),
685+
kNumArgs,
686+
dart_args);
687+
DART_CHECK_VALID(result);
678688
}
689+
return Dart_True();
679690
}
680691

681692

@@ -718,6 +729,7 @@ void DartUtils::PrepareIsolateLibrary(Dart_Handle isolate_lib) {
718729

719730

720731
Dart_Handle DartUtils::PrepareForScriptLoading(const char* package_root,
732+
const char* packages_file,
721733
bool is_service_isolate,
722734
bool trace_loading,
723735
Dart_Handle builtin_lib) {
@@ -746,11 +758,14 @@ Dart_Handle DartUtils::PrepareForScriptLoading(const char* package_root,
746758
Dart_Handle result = Dart_FinalizeLoading(false);
747759
DART_CHECK_VALID(result);
748760

749-
PrepareBuiltinLibrary(builtin_lib,
750-
internal_lib,
751-
is_service_isolate,
752-
trace_loading,
753-
package_root);
761+
result = PrepareBuiltinLibrary(builtin_lib,
762+
internal_lib,
763+
is_service_isolate,
764+
trace_loading,
765+
package_root,
766+
packages_file);
767+
DART_CHECK_VALID(result);
768+
754769
PrepareAsyncLibrary(async_lib, isolate_lib);
755770
PrepareCoreLibrary(core_lib, builtin_lib, is_service_isolate);
756771
PrepareIsolateLibrary(isolate_lib);

runtime/bin/dartutils.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,12 @@ class DartUtils {
124124
Dart_Handle url);
125125
static Dart_Handle LoadScript(const char* script_uri,
126126
Dart_Handle builtin_lib);
127-
static void PrepareBuiltinLibrary(Dart_Handle builtin_lib,
128-
Dart_Handle internal_lib,
129-
bool is_service_isolate,
130-
bool trace_loading,
131-
const char* package_root);
127+
static Dart_Handle PrepareBuiltinLibrary(Dart_Handle builtin_lib,
128+
Dart_Handle internal_lib,
129+
bool is_service_isolate,
130+
bool trace_loading,
131+
const char* package_root,
132+
const char* packages_file);
132133
static void PrepareCoreLibrary(Dart_Handle core_lib,
133134
Dart_Handle builtin_lib,
134135
bool is_service_isolate);
@@ -137,6 +138,7 @@ class DartUtils {
137138
static void PrepareIOLibrary(Dart_Handle io_lib);
138139
static void PrepareIsolateLibrary(Dart_Handle isolate_lib);
139140
static Dart_Handle PrepareForScriptLoading(const char* package_root,
141+
const char* packages_file,
140142
bool is_service_isolate,
141143
bool trace_loading,
142144
Dart_Handle builtin_lib);

runtime/bin/gen_snapshot.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,7 @@ int main(int argc, char** argv) {
592592
// closures and setting up 'package root' for URI resolution.
593593
result =
594594
DartUtils::PrepareForScriptLoading(package_root,
595+
NULL,
595596
false,
596597
false,
597598
builtin_lib);

runtime/bin/isolate_data.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define BIN_ISOLATE_DATA_H_
77

88
#include "include/dart_api.h"
9+
#include "platform/assert.h"
910
#include "platform/globals.h"
1011

1112

@@ -20,23 +21,31 @@ class EventHandler;
2021
// when the isolate shuts down.
2122
class IsolateData {
2223
public:
23-
explicit IsolateData(const char* url, const char* package_root)
24+
explicit IsolateData(const char* url,
25+
const char* package_root,
26+
const char* packages_file)
2427
: script_url(strdup(url)),
2528
package_root(NULL),
29+
packages_file(NULL),
2630
udp_receive_buffer(NULL),
2731
load_async_id(-1) {
2832
if (package_root != NULL) {
33+
ASSERT(packages_file == NULL);
2934
this->package_root = strdup(package_root);
35+
} else if (packages_file != NULL) {
36+
this->packages_file = strdup(packages_file);
3037
}
3138
}
3239
~IsolateData() {
3340
free(script_url);
3441
free(package_root);
42+
free(packages_file);
3543
free(udp_receive_buffer);
3644
}
3745

3846
char* script_url;
3947
char* package_root;
48+
char* packages_file;
4049
uint8_t* udp_receive_buffer;
4150
int64_t load_async_id;
4251

runtime/bin/main.cc

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ static const int DEFAULT_DEBUG_PORT = 5858;
6060
// free'd.)
6161
static const char* commandline_package_root = NULL;
6262

63+
// Value of the --packages flag.
64+
// (This pointer points into an argv buffer and does not need to be
65+
// free'd.)
66+
static const char* commandline_packages_file = NULL;
67+
6368

6469
// Global flag that is used to indicate that we want to compile all the
6570
// dart functions and not run anything.
@@ -169,6 +174,17 @@ static bool ProcessPackageRootOption(const char* arg,
169174
}
170175

171176

177+
static bool ProcessPackagesOption(const char* arg,
178+
CommandLineOptions* vm_options) {
179+
ASSERT(arg != NULL);
180+
if (*arg == '\0' || *arg == '-') {
181+
return false;
182+
}
183+
commandline_packages_file = arg;
184+
return true;
185+
}
186+
187+
172188
static void* GetHashmapKeyFromString(char* key) {
173189
return reinterpret_cast<void*>(key);
174190
}
@@ -371,6 +387,7 @@ static struct {
371387
{ "--verbose", ProcessVerboseOption },
372388
{ "-v", ProcessVerboseOption },
373389
{ "--package-root=", ProcessPackageRootOption },
390+
{ "--packages=", ProcessPackagesOption },
374391
{ "-D", ProcessEnvironmentOption },
375392
// VM specific options to the standalone dart program.
376393
{ "--break-at=", ProcessBreakpointOption },
@@ -489,6 +506,14 @@ static int ParseArguments(int argc,
489506
i++;
490507
}
491508

509+
// Verify consistency of arguments.
510+
if ((commandline_package_root != NULL) &&
511+
(commandline_packages_file != NULL)) {
512+
Log::PrintErr("Specifying both a packages directory and a packages "
513+
"file is invalid.");
514+
return -1;
515+
}
516+
492517
return 0;
493518
}
494519

@@ -561,11 +586,14 @@ static Dart_Handle EnvironmentCallback(Dart_Handle name) {
561586
static Dart_Isolate CreateIsolateAndSetupHelper(const char* script_uri,
562587
const char* main,
563588
const char* package_root,
589+
const char* packages_file,
564590
Dart_IsolateFlags* flags,
565591
char** error,
566592
int* exit_code) {
567593
ASSERT(script_uri != NULL);
568-
IsolateData* isolate_data = new IsolateData(script_uri, package_root);
594+
IsolateData* isolate_data = new IsolateData(script_uri,
595+
package_root,
596+
packages_file);
569597
Dart_Isolate isolate = NULL;
570598

571599
isolate = Dart_CreateIsolate(script_uri,
@@ -618,6 +646,7 @@ static Dart_Isolate CreateIsolateAndSetupHelper(const char* script_uri,
618646
// Prepare for script loading by setting up the 'print' and 'timer'
619647
// closures and setting up 'package root' for URI resolution.
620648
result = DartUtils::PrepareForScriptLoading(package_root,
649+
packages_file,
621650
false,
622651
has_trace_loading,
623652
builtin_lib);
@@ -675,16 +704,17 @@ static Dart_Isolate CreateIsolateAndSetup(const char* script_uri,
675704
return NULL;
676705
}
677706
}
707+
const char* packages_file = NULL;
678708
if (package_root == NULL) {
679709
if (parent_isolate_data != NULL) {
680710
package_root = parent_isolate_data->package_root;
681-
} else {
682-
package_root = ".";
711+
packages_file = parent_isolate_data->packages_file;
683712
}
684713
}
685714
return CreateIsolateAndSetupHelper(script_uri,
686715
main,
687716
package_root,
717+
packages_file,
688718
flags,
689719
error,
690720
&exit_code);
@@ -712,6 +742,8 @@ static void PrintUsage() {
712742
" all VM options).\n"
713743
"--package-root=<path> or -p<path>\n"
714744
" Where to find packages, that is, \"package:...\" imports.\n"
745+
"--packages=<path>\n"
746+
" Where to find a package spec file.\n"
715747
"--version\n"
716748
" Print the VM version.\n");
717749
} else {
@@ -989,6 +1021,7 @@ void main(int argc, char** argv) {
9891021
Dart_Isolate isolate = CreateIsolateAndSetupHelper(script_name,
9901022
"main",
9911023
commandline_package_root,
1024+
commandline_packages_file,
9921025
NULL,
9931026
&error,
9941027
&exit_code);

0 commit comments

Comments
 (0)