changeset 13564:35f637594acc

Truffle: refinements in the management of source information: a new marker interface for empty SourceSections and fix a bug that kept modified source files from being reloaded.
author Michael Van De Vanter <michael.van.de.vanter@oracle.com>
date Tue, 07 Jan 2014 18:21:20 -0800
parents fb846424299f
children b88852aea6f3
files graal/com.oracle.truffle.api/src/com/oracle/truffle/api/NullSourceSection.java graal/com.oracle.truffle.api/src/com/oracle/truffle/api/SourceSection.java graal/com.oracle.truffle.api/src/com/oracle/truffle/api/source/SourceLineLocation.java graal/com.oracle.truffle.api/src/com/oracle/truffle/api/source/SourceManager.java
diffstat 4 files changed, 70 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/NullSourceSection.java	Tue Jan 07 18:21:20 2014 -0800
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api;
+
+/**
+ * Marker for a special flavor of {@link SourceSection} that has no content and can be ignored.
+ */
+public interface NullSourceSection extends SourceSection {
+
+}
--- a/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/SourceSection.java	Tue Jan 07 18:09:42 2014 -0800
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/SourceSection.java	Tue Jan 07 18:21:20 2014 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -95,7 +95,7 @@
     /**
      * Singleton instance with no content.
      */
-    SourceSection NULL = new SourceSection() {
+    SourceSection NULL = new NullSourceSection() {
 
         public Source getSource() {
             return null;
--- a/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/source/SourceLineLocation.java	Tue Jan 07 18:09:42 2014 -0800
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/source/SourceLineLocation.java	Tue Jan 07 18:21:20 2014 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -38,11 +38,14 @@
 
     public SourceLineLocation(Source source, int line) {
         assert source != null;
-        assert source != SourceSection.NULL;
         this.source = source;
         this.line = line;
     }
 
+    public SourceLineLocation(SourceSection sourceSection) {
+        this(sourceSection.getSource(), sourceSection.getStartLine());
+    }
+
     public Source getSource() {
         return source;
     }
--- a/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/source/SourceManager.java	Tue Jan 07 18:09:42 2014 -0800
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/source/SourceManager.java	Tue Jan 07 18:21:20 2014 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -47,7 +47,7 @@
 public final class SourceManager {
 
     // Only files and fake files are indexed.
-    private final Map<String, SourceImpl> sourceMap = new HashMap<>();
+    private final Map<String, SourceImpl> pathToSource = new HashMap<>();
 
     public SourceManager() {
 
@@ -57,24 +57,29 @@
      * Gets the canonical representation of a source file, whose contents will be read lazily and
      * then cached.
      * 
-     * @param reset forces any existing {@link Source} cache to be cleared, forcing a re-read.
+     * @param reset forces any existing {@link Source} cache to be cleared, forcing a re-read
      */
     public Source get(String fileName, boolean reset) {
-        SourceImpl source = sourceMap.get(fileName);
+
+        SourceImpl source = pathToSource.get(fileName);
         if (source == null) {
-            String path = findPath(fileName);
-            if (path == null) {
-                throw new RuntimeException("Can't find file " + fileName);
+            final File file = new File(fileName);
+            String path = null;
+            if (file.exists()) {
+                try {
+                    path = file.getCanonicalPath();
+                } catch (IOException e) {
+                    throw new RuntimeException("Can't find file " + fileName);
+                }
             }
-            source = sourceMap.get(path);
+            source = pathToSource.get(path);
             if (source == null) {
-                source = new FileSourceImpl(fileName, path);
-                sourceMap.put(path, source);
+                source = new FileSourceImpl(file, fileName, path);
+                pathToSource.put(path, source);
             }
-        } else {
-            if (reset) {
-                source.reset();
-            }
+        }
+        if (reset) {
+            source.reset();
         }
         return source;
     }
@@ -111,22 +116,10 @@
      */
     public Source getFakeFile(String name, String code) {
         final SourceImpl source = new LiteralSourceImpl(name, code);
-        sourceMap.put(name, source);
+        pathToSource.put(name, source);
         return source;
     }
 
-    // If it names a real file, get the (canonical) normalized absolute path.
-    private static String findPath(String name) {
-        final File file = new File(name);
-        if (file.exists()) {
-            try {
-                return file.getCanonicalPath();
-            } catch (IOException e) {
-            }
-        }
-        return null;
-    }
-
     private static String readCode(Reader reader) throws IOException {
         final StringBuilder builder = new StringBuilder();
         final char[] buffer = new char[1024];
@@ -142,8 +135,7 @@
         return builder.toString();
     }
 
-    // TODO (mlvdv) make this private once some related code changes propagate
-    public abstract static class SourceImpl implements Source {
+    private abstract static class SourceImpl implements Source {
 
         protected TextMap textMap = null;
 
@@ -267,16 +259,17 @@
 
     private static class FileSourceImpl extends SourceImpl {
 
+        private final File file;
         private final String name; // Name used originally to describe the source
-        private String code = null;
         private final String path;  // Normalized path description of an actual file
-        private boolean readAttempted;
 
-        public FileSourceImpl(String name, String path) {
+        private String code = null;  // A cache of the file's contents
+        private long timeStamp;      // timestamp of the cache in the file system
+
+        public FileSourceImpl(File file, String name, String path) {
+            this.file = file;
             this.name = name;
             this.path = path;
-            this.readAttempted = false;
-
         }
 
         @Override
@@ -286,10 +279,10 @@
 
         @Override
         public String getCode() {
-            if (code == null && !readAttempted) {
-                readAttempted = true;
+            if (code == null || timeStamp != file.lastModified()) {
                 try {
                     code = readCode(getReader());
+                    timeStamp = file.lastModified();
                 } catch (IOException e) {
                 }
             }
@@ -303,11 +296,11 @@
 
         @Override
         public Reader getReader() {
-            if (code != null) {
+            if (code != null && timeStamp == file.lastModified()) {
                 return new StringReader(code);
             }
             try {
-                return new BufferedReader(new FileReader(path));
+                return new FileReader(file);
             } catch (FileNotFoundException e) {
                 throw new RuntimeException("Can't find file " + path);
             }
@@ -316,7 +309,6 @@
         @Override
         protected void reset() {
             this.code = null;
-            this.readAttempted = false;
         }
 
     }