Skip to content

Commit 60eab65

Browse files
committed
- Implement package map parameter when spawning isolate.
BUG= [email protected] Review URL: https://codereview.chromium.org/1403693002 .
1 parent 77eb3fb commit 60eab65

15 files changed

+289
-91
lines changed

runtime/bin/builtin.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,34 @@ void _loadPackagesMap(String packagesParam) {
514514
}
515515

516516

517+
// Embedder Entrypoint:
518+
// Add mapping from package name to URI.
519+
void _addPackageMapEntry(String key, String value) {
520+
if (!_setupCompleted) {
521+
_setupHooks();
522+
}
523+
if (_traceLoading) {
524+
_log("Adding packages map entry: $key -> $value");
525+
}
526+
if (_packageRoot != null) {
527+
if (_traceLoading) {
528+
_log("_packageRoot already set: $_packageRoot");
529+
}
530+
throw "Cannot add package map entry to an exisiting package root.";
531+
}
532+
if (_packagesPort != null) {
533+
if (_traceLoading) {
534+
_log("Package map load request already pending.");
535+
}
536+
throw "Cannot add package map entry during package map resolution.";
537+
}
538+
if (_packageMap == null) {
539+
_packageMap = new Map<String, Uri>();
540+
}
541+
_packageMap[key] = _workingDirectory.resolve(value);
542+
}
543+
544+
517545
void _asyncLoadError(_LoadRequest req, _LoadError error, StackTrace stack) {
518546
if (_traceLoading) {
519547
_log("_asyncLoadError(${req._uri}), error: $error\nstack: $stack");

runtime/bin/dartutils.cc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,7 @@ Dart_Handle DartUtils::PrepareBuiltinLibrary(Dart_Handle builtin_lib,
637637
bool is_service_isolate,
638638
bool trace_loading,
639639
const char* package_root,
640+
const char** package_map,
640641
const char* packages_file) {
641642
// Setup the internal library's 'internalPrint' function.
642643
Dart_Handle print = Dart_Invoke(
@@ -670,6 +671,7 @@ Dart_Handle DartUtils::PrepareBuiltinLibrary(Dart_Handle builtin_lib,
670671

671672
// Set up package root if specified.
672673
if (package_root != NULL) {
674+
ASSERT(package_map == NULL);
673675
ASSERT(packages_file == NULL);
674676
result = NewString(package_root);
675677
RETURN_IF_ERROR(result);
@@ -681,6 +683,33 @@ Dart_Handle DartUtils::PrepareBuiltinLibrary(Dart_Handle builtin_lib,
681683
kNumArgs,
682684
dart_args);
683685
RETURN_IF_ERROR(result);
686+
} else if (package_map != NULL) {
687+
ASSERT(packages_file == NULL);
688+
Dart_Handle func_name = NewString("_addPackageMapEntry");
689+
RETURN_IF_ERROR(func_name);
690+
691+
for (int i = 0; package_map[i] != NULL; i +=2) {
692+
const int kNumArgs = 2;
693+
Dart_Handle dart_args[kNumArgs];
694+
// Get the key.
695+
result = NewString(package_map[i]);
696+
RETURN_IF_ERROR(result);
697+
dart_args[0] = result;
698+
if (package_map[i + 1] == NULL) {
699+
return Dart_NewUnhandledExceptionError(
700+
NewDartArgumentError("Adding package map entry without value."));
701+
}
702+
// Get the value.
703+
result = NewString(package_map[i + 1]);
704+
RETURN_IF_ERROR(result);
705+
dart_args[1] = result;
706+
// Setup the next package map entry.
707+
result = Dart_Invoke(builtin_lib,
708+
func_name,
709+
kNumArgs,
710+
dart_args);
711+
RETURN_IF_ERROR(result);
712+
}
684713
} else if (packages_file != NULL) {
685714
result = NewString(packages_file);
686715
RETURN_IF_ERROR(result);
@@ -738,6 +767,7 @@ Dart_Handle DartUtils::PrepareIsolateLibrary(Dart_Handle isolate_lib) {
738767

739768

740769
Dart_Handle DartUtils::PrepareForScriptLoading(const char* package_root,
770+
const char** package_map,
741771
const char* packages_file,
742772
bool is_service_isolate,
743773
bool trace_loading,
@@ -772,6 +802,7 @@ Dart_Handle DartUtils::PrepareForScriptLoading(const char* package_root,
772802
is_service_isolate,
773803
trace_loading,
774804
package_root,
805+
package_map,
775806
packages_file);
776807
RETURN_IF_ERROR(result);
777808

runtime/bin/dartutils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ class DartUtils {
129129
bool is_service_isolate,
130130
bool trace_loading,
131131
const char* package_root,
132+
const char** package_map,
132133
const char* packages_file);
133134
static Dart_Handle PrepareCoreLibrary(Dart_Handle core_lib,
134135
Dart_Handle builtin_lib,
@@ -138,6 +139,7 @@ class DartUtils {
138139
static Dart_Handle PrepareIOLibrary(Dart_Handle io_lib);
139140
static Dart_Handle PrepareIsolateLibrary(Dart_Handle isolate_lib);
140141
static Dart_Handle PrepareForScriptLoading(const char* package_root,
142+
const char** package_map,
141143
const char* packages_file,
142144
bool is_service_isolate,
143145
bool trace_loading,

runtime/bin/gen_snapshot.cc

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

runtime/bin/main.cc

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,7 @@ static Dart_Handle EnvironmentCallback(Dart_Handle name) {
706706
static Dart_Isolate CreateIsolateAndSetupHelper(const char* script_uri,
707707
const char* main,
708708
const char* package_root,
709+
const char** package_map,
709710
const char* packages_file,
710711
Dart_IsolateFlags* flags,
711712
char** error,
@@ -767,6 +768,7 @@ static Dart_Isolate CreateIsolateAndSetupHelper(const char* script_uri,
767768
// Prepare for script loading by setting up the 'print' and 'timer'
768769
// closures and setting up 'package root' for URI resolution.
769770
result = DartUtils::PrepareForScriptLoading(package_root,
771+
package_map,
770772
packages_file,
771773
false,
772774
has_trace_loading,
@@ -815,13 +817,18 @@ static Dart_Isolate CreateIsolateAndSetupHelper(const char* script_uri,
815817
static Dart_Isolate CreateIsolateAndSetup(const char* script_uri,
816818
const char* main,
817819
const char* package_root,
820+
const char** package_map,
818821
Dart_IsolateFlags* flags,
819822
void* data, char** error) {
820823
// The VM should never call the isolate helper with a NULL flags.
821824
ASSERT(flags != NULL);
822825
ASSERT(flags->version == DART_FLAGS_CURRENT_VERSION);
826+
if ((package_root != NULL) && (package_map != NULL)) {
827+
*error = strdup("Invalid arguments - Cannot simultaneously specify "
828+
"package root and package map.");
829+
return NULL;
830+
}
823831
IsolateData* parent_isolate_data = reinterpret_cast<IsolateData*>(data);
824-
int exit_code = 0;
825832
if (script_uri == NULL) {
826833
if (data == NULL) {
827834
*error = strdup("Invalid 'callback_data' - Unable to spawn new isolate");
@@ -834,15 +841,20 @@ static Dart_Isolate CreateIsolateAndSetup(const char* script_uri,
834841
}
835842
}
836843
const char* packages_file = NULL;
837-
if (package_root == NULL) {
844+
// If neither a package root nor a package map are requested pass on the
845+
// inherited values.
846+
if ((package_root == NULL) && (package_map == NULL)) {
838847
if (parent_isolate_data != NULL) {
839848
package_root = parent_isolate_data->package_root;
840849
packages_file = parent_isolate_data->packages_file;
841850
}
842851
}
852+
853+
int exit_code = 0;
843854
return CreateIsolateAndSetupHelper(script_uri,
844855
main,
845856
package_root,
857+
package_map,
846858
packages_file,
847859
flags,
848860
error,
@@ -1116,6 +1128,7 @@ bool RunMainIsolate(const char* script_name,
11161128
Dart_Isolate isolate = CreateIsolateAndSetupHelper(script_name,
11171129
"main",
11181130
commandline_package_root,
1131+
NULL,
11191132
commandline_packages_file,
11201133
NULL,
11211134
&error,

runtime/bin/vmservice_dartium.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Dart_Isolate VmServiceServer::CreateIsolate(const uint8_t* snapshot_buffer) {
6565
// Prepare for script loading by setting up the 'print' and 'timer'
6666
// closures and setting up 'package root' for URI resolution.
6767
result = DartUtils::PrepareForScriptLoading(
68-
NULL, NULL, true, false, builtin_lib);
68+
NULL, NULL, NULL, true, false, builtin_lib);
6969
CHECK_RESULT(result);
7070

7171
ASSERT(Dart_IsServiceIsolate(isolate));

runtime/bin/vmservice_impl.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ bool VmService::Setup(const char* server_ip, intptr_t server_port) {
174174
// Prepare for script loading by setting up the 'print' and 'timer'
175175
// closures and setting up 'package root' for URI resolution.
176176
result = DartUtils::PrepareForScriptLoading(
177-
NULL, NULL, true, false, builtin_lib);
177+
NULL, NULL, NULL, true, false, builtin_lib);
178178
SHUTDOWN_ON_ERROR(result);
179179

180180
// Load main script.

runtime/include/dart_api.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -755,8 +755,14 @@ typedef struct {
755755
* improve debugging messages. The main function is not invoked by
756756
* this function.
757757
* \param package_root The package root path for this isolate to resolve
758-
* package imports against. If this parameter is NULL, the package root path
759-
* of the parent isolate should be used.
758+
* package imports against. Only one of package_root and package_map
759+
* parameters is non-NULL. If neither parameter is passed the package
760+
* resolution of the parent isolate should be used.
761+
* \param package_map The package map for this isolate to resolve package
762+
* imports against. The array contains alternating keys and values,
763+
* terminated by a NULL key. Only one of package_root and package_map
764+
* parameters is non-NULL. If neither parameter is passed the package
765+
* resolution of the parent isolate should be used.
760766
* \param flags Default flags for this isolate being spawned. Either inherited
761767
* from the spawning isolate or passed as parameters when spawning the
762768
* isolate from Dart code.
@@ -771,6 +777,7 @@ typedef struct {
771777
typedef Dart_Isolate (*Dart_IsolateCreateCallback)(const char* script_uri,
772778
const char* main,
773779
const char* package_root,
780+
const char** package_map,
774781
Dart_IsolateFlags* flags,
775782
void* callback_data,
776783
char** error);

0 commit comments

Comments
 (0)