mirror of
https://github.com/dart-lang/sdk
synced 2024-11-02 15:17:07 +00:00
Add implementation of Isolate.resolvePackageUri for dart2js.
This uses 'packages/' as the default base to resolve uris from, but also exposes a hook to allow users to overwrite it. BUG= https://github.com/dart-lang/sdk/issues/25594 R=het@google.com, lrn@google.com Review URL: https://codereview.chromium.org/2495383003 .
This commit is contained in:
parent
5ed13d3298
commit
f460a8706d
7 changed files with 102 additions and 2 deletions
|
@ -32,4 +32,8 @@ const String HOOKS_API_USAGE = """
|
|||
// is loaded. It should load and eval the javascript of `uri`, and call
|
||||
// successCallback. If it fails to do so, it should call errorCallback with
|
||||
// an error.
|
||||
//
|
||||
// defaultPackagesBase:
|
||||
// Override the location where `package:` uris are resolved from. By default
|
||||
// they are resolved under "packages/" from the current window location.
|
||||
""";
|
||||
|
|
|
@ -751,6 +751,14 @@ class IsolateNatives {
|
|||
|
||||
static String thisScript = computeThisScript();
|
||||
|
||||
/// Returns the base path added to Uri.base to resolve `package:` Uris.
|
||||
///
|
||||
/// This is used by `Isolate.resolvePackageUri` to load resources. The default
|
||||
/// value is `packages/` but users can override this by using the
|
||||
/// `defaultPackagesBase` hook.
|
||||
static String get packagesBase =>
|
||||
JS('String', r'self.defaultPackagesBase || "packages/"');
|
||||
|
||||
/// Associates an ID with a native worker object.
|
||||
static final Expando<int> workerIds = new Expando<int>();
|
||||
|
||||
|
|
|
@ -31,9 +31,12 @@ class Isolate {
|
|||
throw new UnsupportedError("Isolate.packageConfig");
|
||||
}
|
||||
|
||||
static Uri _packageBase = Uri.base.resolve(IsolateNatives.packagesBase);
|
||||
|
||||
@patch
|
||||
static Future<Uri> resolvePackageUri(Uri packageUri) {
|
||||
throw new UnsupportedError("Isolate.resolvePackageUri");
|
||||
static Future<Uri> resolvePackageUri(Uri packageUri) async {
|
||||
if (packageUri.scheme != 'package') return packageUri;
|
||||
return _packageBase.resolveUri(packageUri.replace(scheme: ''));
|
||||
}
|
||||
|
||||
@patch
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import 'dart:js';
|
||||
import 'dart:isolate';
|
||||
import 'package:unittest/unittest.dart';
|
||||
import 'package:unittest/html_config.dart';
|
||||
|
||||
main() async {
|
||||
useHtmlConfiguration();
|
||||
|
||||
setUp(() {
|
||||
context['defaultPackagesBase'] = 'path1/';
|
||||
});
|
||||
|
||||
test('hook overrides package-uri resolution', () async {
|
||||
var uri = await Isolate.resolvePackageUri(Uri.parse('package:foo/bar.txt'));
|
||||
expect(uri, Uri.base.resolve('path1/foo/bar.txt'));
|
||||
});
|
||||
|
||||
test('hook is read once, on the first use of resolvePackageUri', () async {
|
||||
await Isolate.resolvePackageUri(Uri.parse('package:foo/bar.txt'));
|
||||
context['defaultPackagesBase'] = 'path2/';
|
||||
var uri = await Isolate.resolvePackageUri(Uri.parse('package:foo/bar.txt'));
|
||||
expect(uri, Uri.base.resolve('path1/foo/bar.txt'));
|
||||
});
|
||||
}
|
16
tests/isolate/browser/package_resolve_browser_hook_test.dart
Normal file
16
tests/isolate/browser/package_resolve_browser_hook_test.dart
Normal file
|
@ -0,0 +1,16 @@
|
|||
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import 'dart:isolate';
|
||||
import 'package:unittest/unittest.dart';
|
||||
import 'package:unittest/html_config.dart';
|
||||
|
||||
main() async {
|
||||
useHtmlConfiguration();
|
||||
|
||||
test('defaultPackagesBase hook overrides package-uri resolution', () async {
|
||||
var uri = await Isolate.resolvePackageUri(Uri.parse('package:foo/bar.txt'));
|
||||
expect(uri, Uri.base.resolve('path/set/from/hook/foo/bar.txt'));
|
||||
});
|
||||
}
|
25
tests/isolate/browser/package_resolve_browser_hook_test.html
Normal file
25
tests/isolate/browser/package_resolve_browser_hook_test.html
Normal file
|
@ -0,0 +1,25 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="dart.unittest" content="full-stack-traces">
|
||||
<title> mirrors_test </title>
|
||||
<style>
|
||||
.unittest-table { font-family:monospace; border:1px; }
|
||||
.unittest-pass { background: #6b3;}
|
||||
.unittest-fail { background: #d55;}
|
||||
.unittest-error { background: #a11;}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1> Running mirrors_test </h1>
|
||||
<script type="text/javascript"
|
||||
src="/root_dart/tools/testing/dart/test_controller.js"></script>
|
||||
<script>
|
||||
// Dart2js exposes this hook to override the default base path where resource
|
||||
// package uris are resolved from.
|
||||
defaultPackagesBase = 'path/set/from/hook/';
|
||||
</script>
|
||||
%TEST_SCRIPTS%
|
||||
</body>
|
||||
</html>
|
16
tests/isolate/browser/package_resolve_browser_test.dart
Normal file
16
tests/isolate/browser/package_resolve_browser_test.dart
Normal file
|
@ -0,0 +1,16 @@
|
|||
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import 'dart:isolate';
|
||||
import 'package:unittest/unittest.dart';
|
||||
import 'package:unittest/html_config.dart';
|
||||
|
||||
main() {
|
||||
useHtmlConfiguration();
|
||||
|
||||
test('by default package-uri resolve under base/packages/', () async {
|
||||
var uri = await Isolate.resolvePackageUri(Uri.parse('package:foo/bar.txt'));
|
||||
expect(uri, Uri.base.resolve('packages/foo/bar.txt'));
|
||||
});
|
||||
}
|
Loading…
Reference in a new issue