Skip to content

Commit 2890a7a

Browse files
committed
Add Resource class. Currently unimplemented.
[email protected], [email protected] Review URL: https://codereview.chromium.org//1181663002.
1 parent d907a42 commit 2890a7a

File tree

6 files changed

+148
-1
lines changed

6 files changed

+148
-1
lines changed

runtime/lib/core_patch.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,27 @@ class _SyncIterator implements Iterator {
205205
}
206206
}
207207
}
208+
209+
patch class Resource {
210+
/* patch */ const factory Resource(String uri) = _Resource;
211+
}
212+
213+
class _Resource implements Resource {
214+
final String _location;
215+
216+
const _Resource(String uri) : _location = uri;
217+
218+
Uri get uri => Uri.base.resolve(_location);
219+
220+
Stream<List<int>> openRead() {
221+
throw new UnimplementedError("openRead");
222+
}
223+
224+
Future<List<int>> readAsBytes() {
225+
throw new UnimplementedError("readAsBytes");
226+
}
227+
228+
Future<String> readAsString({Encoding encoding}) {
229+
throw new UnimplementedError("readAsString");
230+
}
231+
}

runtime/lib/internal_patch.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ patch class CodeUnits {
2121

2222
final bool is64Bit = _inquireIs64Bit();
2323

24-
bool _inquireIs64Bit() native "Internal_inquireIs64Bit";
24+
bool _inquireIs64Bit() native "Internal_inquireIs64Bit";

sdk/lib/_internal/compiler/js_lib/core_patch.dart

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,3 +510,71 @@ class Uri {
510510
throw new UnsupportedError("'Uri.base' is not supported");
511511
}
512512
}
513+
514+
@patch
515+
class Resource {
516+
@patch
517+
const factory Resource(String uri) = _Resource;
518+
}
519+
520+
Uri _resolvePackageUri(Uri packageUri) {
521+
assert(packageUri.scheme == "package");
522+
if (packageUri.hasAuthority) {
523+
throw new ArgumentError("Package-URI must not have a host: $packageUri");
524+
}
525+
var resolved = Uri.base.resolve("packages/${packageUri.path}");
526+
return resolved;
527+
}
528+
529+
class _Resource implements Resource {
530+
final String _location;
531+
532+
const _Resource(String uri) : _location = uri;
533+
534+
Uri get uri => Uri.base.resolve(_location);
535+
536+
Stream<List<int>> openRead() {
537+
Uri uri = this.uri;
538+
if (uri.scheme == "package") {
539+
uri = _resolvePackageUri(uri);
540+
}
541+
if (uri.scheme == "http" || uri.scheme == "https") {
542+
return _readAsStream(uri);
543+
}
544+
throw new StateError("Unable to find resource, unknown scheme: $_location");
545+
}
546+
547+
Future<List<int>> readAsBytes() {
548+
Uri uri = this.uri;
549+
if (uri.scheme == "package") {
550+
uri = _resolvePackageUri(uri);
551+
}
552+
if (uri.scheme == "http" || uri.scheme == "https") {
553+
return _readAsBytes(uri);
554+
}
555+
throw new StateError("Unable to find resource, unknown scheme: $_location");
556+
}
557+
558+
Future<String> readAsString({Encoding encoding: UTF8}) {
559+
Uri uri = this.uri;
560+
if (uri.scheme == "package") {
561+
uri = _resolvePackageUri(uri);
562+
}
563+
if (uri.scheme == "http" || uri.scheme == "https") {
564+
return _readAsString(uri);
565+
}
566+
throw new StateError("Unable to find resource, unknown scheme: $_location");
567+
}
568+
569+
Stream<List<int>> _readAsStream(Uri uri) {
570+
throw new UnimplementedError("Streaming bytes via HTTP");
571+
}
572+
573+
Future<List<int>> _readAsBytes(Uri uri) {
574+
throw new UnimplementedError("Reading bytes via HTTP");
575+
}
576+
577+
Future<String> _readAsString(Uri uri) {
578+
throw new UnimplementedError("Reading string via HTTP");
579+
}
580+
}

sdk/lib/core/core.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ import "dart:_internal" hide Symbol;
157157
import "dart:_internal" as internal show Symbol;
158158
import "dart:convert" show UTF8, LATIN1, Encoding;
159159
import "dart:math" show Random; // Used by List.shuffle.
160+
import "dart:async" show Stream, Future; // Used by Resource.
160161

161162
part "annotations.dart";
162163
part "bool.dart";
@@ -181,6 +182,7 @@ part "object.dart";
181182
part "pattern.dart";
182183
part "print.dart";
183184
part "regexp.dart";
185+
part "resource.dart";
184186
part "set.dart";
185187
part "sink.dart";
186188
part "stacktrace.dart";

sdk/lib/core/core_sources.gypi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
'pattern.dart',
3030
'print.dart',
3131
'regexp.dart',
32+
'resource.dart',
3233
'set.dart',
3334
'sink.dart',
3435
'stacktrace.dart',

sdk/lib/core/resource.dart

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
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.
4+
5+
part of dart.core;
6+
7+
/**
8+
* A resource that can be read into the program.
9+
*
10+
* A resource is data that can be located using a URI and read into
11+
* the program at runtime.
12+
* The URI may use the `package` scheme to read resources provided
13+
* along with package sources.
14+
*/
15+
abstract class Resource {
16+
/**
17+
* Creates a resource object with the given [uri] as location.
18+
*
19+
* The `uri` is a string containing a valid URI.
20+
* If the string is not a valid URI, using any of the functions on
21+
* the resource object will fail.
22+
*
23+
* The URI may be relative, in which case it will be resolved
24+
* against [Uri.base] before being used.
25+
*
26+
* The URI may use the `package` scheme, which is always supported.
27+
* Other schemes may also be supported where possible.
28+
*/
29+
external const factory Resource(String uri);
30+
31+
/**
32+
* The location `uri` of this resource.
33+
*
34+
* This is a [Uri] of the `uri` parameter given to the constructor.
35+
* If the parameter was not a valid URI, reading `uri` may fail.
36+
*/
37+
Uri get uri;
38+
39+
/** Read the resource content as a stream of bytes. */
40+
Stream<List<int>> openRead();
41+
42+
/** Read the resource content. */
43+
Future<List<int>> readAsBytes();
44+
45+
/**
46+
* Read the resource content as a string.
47+
*
48+
* The content is decoded into a string using an [Encoding].
49+
* If no other encoding is provided, it defaults to UTF-8.
50+
*/
51+
Future<String> readAsString({Encoding encoding});
52+
}

0 commit comments

Comments
 (0)