# HG changeset patch # User Michael Van De Vanter # Date 1425434110 28800 # Node ID c152a485d747a97ce9f981a7b6fa7fe7cc50189d # Parent 9d2ff2e8360dd39b8835b60802d873e2eb08a221 Truffle: new method Source.getLength() and semantic adjustments to the new factory method for creating files whose contents have already been read. diff -r 9d2ff2e8360d -r c152a485d747 graal/com.oracle.truffle.api/src/com/oracle/truffle/api/source/Source.java --- a/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/source/Source.java Tue Mar 03 17:13:51 2015 -0800 +++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/source/Source.java Tue Mar 03 17:55:10 2015 -0800 @@ -124,15 +124,16 @@ /** * Gets the canonical representation of a source file, whose contents have already been read and * need not be read again. It is confirmed that the file resolves to a file name, so it can be - * indexed by canonical path, but it is not confirmed to be readable. + * indexed by canonical path. It is not confirmed that the text supplied agrees with the file's + * contents or even whether the file is readable. * * @param chars textual source code already read from the file * @param fileName * @return canonical representation of the file's contents. * @throws IOException if the file cannot be found - * @throws IllegalArgumentException if there is already a Source indexed under this file name */ - public static Source fromFileName(CharSequence chars, String fileName) throws IOException, IllegalArgumentException { + public static Source fromFileName(CharSequence chars, String fileName) throws IOException { + final WeakReference nameRef = filePathToSource.get(fileName); Source source = nameRef == null ? null : nameRef.get(); if (source == null) { @@ -144,10 +145,9 @@ if (source == null) { source = new FileSource(file, fileName, path, chars); filePathToSource.put(path, new WeakReference<>(source)); - return source; } } - throw new IOException("Source already exists for file:" + fileName); + return source; } /** @@ -304,6 +304,13 @@ } /** + * Gets the number of characters in the source. + */ + public final int getLength() { + return checkTextMap().length(); + } + + /** * Returns the complete text of the code. */ public abstract String getCode(); @@ -1136,6 +1143,13 @@ } /** + * The number of characters in the mapped text. + */ + public int length() { + return textLength; + } + + /** * The number of lines in the text; if characters appear after the final newline, then they * also count as a line, even though not newline-terminated. */