| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 library dart2js.io.source_file; | 5 library dart2js.io.source_file; |
| 6 | 6 |
| 7 import 'dart:convert' show UTF8; | 7 import 'dart:convert' show UTF8; |
| 8 import 'dart:math'; | 8 import 'dart:math'; |
| 9 import 'dart:typed_data' show Uint8List; | 9 import 'dart:typed_data' show Uint8List; |
| 10 | 10 |
| 11 import 'package:kernel/ast.dart' as kernel show Location, Source; | 11 import 'package:kernel/ast.dart' as kernel show Location, Source; |
| 12 | |
| 13 import 'location_provider.dart' show LocationProvider; | 12 import 'location_provider.dart' show LocationProvider; |
| 13 import '../../compiler_new.dart'; |
| 14 | 14 |
| 15 /// Represents a file of source code. The content can be either a [String] or | 15 /// Represents a file of source code. The content can be either a [String] or |
| 16 /// a UTF-8 encoded [List<int>] of bytes. | 16 /// a UTF-8 encoded [List<int>] of bytes. |
| 17 abstract class SourceFile implements LocationProvider { | 17 abstract class SourceFile<T> implements Input<T>, LocationProvider { |
| 18 /// The absolute URI of the source file. | 18 /// The absolute URI of the source file. |
| 19 Uri get uri; | 19 Uri get uri; |
| 20 | 20 |
| 21 InputKind get inputKind => InputKind.utf8; |
| 22 |
| 21 kernel.Source cachedKernelSource; | 23 kernel.Source cachedKernelSource; |
| 22 | 24 |
| 23 kernel.Source get kernelSource { | 25 kernel.Source get kernelSource { |
| 24 return cachedKernelSource ??= | 26 return cachedKernelSource ??= |
| 25 new kernel.Source(lineStarts, slowUtf8ZeroTerminatedBytes()) | 27 new kernel.Source(lineStarts, slowUtf8ZeroTerminatedBytes()) |
| 26 ..cachedText = slowText(); | 28 ..cachedText = slowText(); |
| 27 } | 29 } |
| 28 | 30 |
| 29 /// The name of the file. | 31 /// The name of the file. |
| 30 /// | 32 /// |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 } | 161 } |
| 160 | 162 |
| 161 List<int> _zeroTerminateIfNecessary(List<int> bytes) { | 163 List<int> _zeroTerminateIfNecessary(List<int> bytes) { |
| 162 if (bytes.length > 0 && bytes.last == 0) return bytes; | 164 if (bytes.length > 0 && bytes.last == 0) return bytes; |
| 163 List<int> result = new Uint8List(bytes.length + 1); | 165 List<int> result = new Uint8List(bytes.length + 1); |
| 164 result.setRange(0, bytes.length, bytes); | 166 result.setRange(0, bytes.length, bytes); |
| 165 result[result.length - 1] = 0; | 167 result[result.length - 1] = 0; |
| 166 return result; | 168 return result; |
| 167 } | 169 } |
| 168 | 170 |
| 169 class Utf8BytesSourceFile extends SourceFile { | 171 class Utf8BytesSourceFile extends SourceFile<List<int>> { |
| 170 final Uri uri; | 172 final Uri uri; |
| 171 | 173 |
| 172 /// The UTF-8 encoded content of the source file. | 174 /// The UTF-8 encoded content of the source file. |
| 173 final List<int> zeroTerminatedContent; | 175 final List<int> zeroTerminatedContent; |
| 174 | 176 |
| 175 /// Creates a Utf8BytesSourceFile. | 177 /// Creates a Utf8BytesSourceFile. |
| 176 /// | 178 /// |
| 177 /// If possible, the given [content] should be zero-terminated. If it isn't, | 179 /// If possible, the given [content] should be zero-terminated. If it isn't, |
| 178 /// the constructor clones the content and adds a trailing 0. | 180 /// the constructor clones the content and adds a trailing 0. |
| 179 Utf8BytesSourceFile(this.uri, List<int> content) | 181 Utf8BytesSourceFile(this.uri, List<int> content) |
| 180 : this.zeroTerminatedContent = _zeroTerminateIfNecessary(content); | 182 : this.zeroTerminatedContent = _zeroTerminateIfNecessary(content); |
| 181 | 183 |
| 184 List<int> get data => zeroTerminatedContent; |
| 185 |
| 182 String slowText() { | 186 String slowText() { |
| 183 // Don't convert the trailing zero byte. | 187 // Don't convert the trailing zero byte. |
| 184 return UTF8.decoder | 188 return UTF8.decoder |
| 185 .convert(zeroTerminatedContent, 0, zeroTerminatedContent.length - 1); | 189 .convert(zeroTerminatedContent, 0, zeroTerminatedContent.length - 1); |
| 186 } | 190 } |
| 187 | 191 |
| 188 List<int> slowUtf8ZeroTerminatedBytes() => zeroTerminatedContent; | 192 List<int> slowUtf8ZeroTerminatedBytes() => zeroTerminatedContent; |
| 189 | 193 |
| 190 String slowSubstring(int start, int end) { | 194 String slowSubstring(int start, int end) { |
| 191 // TODO(lry): to make this faster, the scanner could record the UTF-8 slack | 195 // TODO(lry): to make this faster, the scanner could record the UTF-8 slack |
| (...skipping 21 matching lines...) Expand all Loading... |
| 213 : super(uri, content); | 217 : super(uri, content); |
| 214 | 218 |
| 215 String slowText() { | 219 String slowText() { |
| 216 if (cachedText == null) { | 220 if (cachedText == null) { |
| 217 cachedText = super.slowText(); | 221 cachedText = super.slowText(); |
| 218 } | 222 } |
| 219 return cachedText; | 223 return cachedText; |
| 220 } | 224 } |
| 221 } | 225 } |
| 222 | 226 |
| 223 class StringSourceFile extends SourceFile { | 227 class StringSourceFile extends SourceFile<String> { |
| 224 final Uri uri; | 228 final Uri uri; |
| 225 final String filename; | 229 final String filename; |
| 226 final String text; | 230 final String text; |
| 227 | 231 |
| 228 StringSourceFile(this.uri, this.filename, this.text); | 232 StringSourceFile(this.uri, this.filename, this.text); |
| 229 | 233 |
| 230 StringSourceFile.fromUri(Uri uri, String text) | 234 StringSourceFile.fromUri(Uri uri, String text) |
| 231 : this(uri, uri.toString(), text); | 235 : this(uri, uri.toString(), text); |
| 232 | 236 |
| 233 StringSourceFile.fromName(String filename, String text) | 237 StringSourceFile.fromName(String filename, String text) |
| 234 : this(new Uri(path: filename), filename, text); | 238 : this(new Uri(path: filename), filename, text); |
| 235 | 239 |
| 240 String get data => text; |
| 241 |
| 236 int get length => text.length; | 242 int get length => text.length; |
| 237 set length(int v) {} | 243 set length(int v) {} |
| 238 | 244 |
| 239 String slowText() => text; | 245 String slowText() => text; |
| 240 | 246 |
| 241 List<int> slowUtf8ZeroTerminatedBytes() { | 247 List<int> slowUtf8ZeroTerminatedBytes() { |
| 242 return _zeroTerminateIfNecessary(UTF8.encode(text)); | 248 return _zeroTerminateIfNecessary(UTF8.encode(text)); |
| 243 } | 249 } |
| 244 | 250 |
| 245 String slowSubstring(int start, int end) => text.substring(start, end); | 251 String slowSubstring(int start, int end) => text.substring(start, end); |
| 246 } | 252 } |
| 253 |
| 254 /// Binary input data. |
| 255 class Binary implements Input<List<int>> { |
| 256 final Uri uri; |
| 257 final List<int> data; |
| 258 |
| 259 Binary(this.uri, this.data); |
| 260 |
| 261 InputKind get inputKind => InputKind.binary; |
| 262 } |
| OLD | NEW |