From 0dda29ae0adaebac81f279fe7fabd28e14deb097 Mon Sep 17 00:00:00 2001
From: Mick Jordan <mick.jordan@oracle.com>
Date: Tue, 4 Oct 2016 12:01:10 -0700
Subject: [PATCH] avoid capturing URLs that reference src locations for
 internal R code

---
 .../com/oracle/truffle/r/runtime/RSource.java    |  3 ++-
 .../src/com/oracle/truffle/r/runtime/Utils.java  | 16 +++++++++++-----
 2 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RSource.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RSource.java
index 305be53ab8..75b14bc3ea 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RSource.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RSource.java
@@ -76,7 +76,8 @@ public class RSource {
         LAPPLY("<lapply>"),
         R_PARSEVECTOR("<R_ParseVector>"),
         PAIRLIST_DEPARSE("<pairlist deparse>"),
-        INIT_EMBEDDED("<init embedded>");
+        INIT_EMBEDDED("<init embedded>"),
+        R_IMPL("<internal R code>");
 
         public final String string;
 
diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/Utils.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/Utils.java
index cf2fbd7b49..01108debdb 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/Utils.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/Utils.java
@@ -26,7 +26,6 @@ import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
-import java.net.URL;
 import java.nio.file.FileSystem;
 import java.nio.file.FileSystems;
 import java.nio.file.Path;
@@ -91,13 +90,20 @@ public final class Utils {
         graphPrinter.printToNetwork(true);
     }
 
+    /**
+     * Locates a resource that is used within the implementation, e.g. a file of R code, and returns
+     * a {@link Source} instance that represents it. Since the location may vary between
+     * implementations and, in particular may not be a persistently accessible URL, we read the
+     * content and store it as an "internal" instance.
+     */
     public static Source getResourceAsSource(Class<?> clazz, String resourceName) {
         try {
-            URL url = ResourceHandlerFactory.getHandler().getResource(clazz, resourceName);
-            if (url == null) {
-                throw RInternalError.shouldNotReachHere("resource " + resourceName + " not found, context: " + clazz);
+            InputStream is = ResourceHandlerFactory.getHandler().getResourceAsStream(clazz, resourceName);
+            if (is == null) {
+                throw new IOException();
             }
-            return RSource.fromURL(url, resourceName);
+            String content = getResourceAsString(is);
+            return RSource.fromTextInternal(content, RSource.Internal.R_IMPL);
         } catch (IOException ex) {
             throw RInternalError.shouldNotReachHere("resource " + resourceName + " not found, context: " + clazz);
         }
-- 
GitLab