From 6ad1891f7f8a4b856686e9b2e4d04d716ed043a3 Mon Sep 17 00:00:00 2001
From: stepan <stepan.sindelar@oracle.com>
Date: Thu, 25 Jan 2018 16:14:43 +0100
Subject: [PATCH] Use ResourceHandler abstraction in FastRHelp built-ins

---
 .../builtin/EagerResourceHandlerFactory.java  | 27 ++++---------------
 .../r/nodes/builtin/fastr/FastRHelp.java      |  7 ++---
 .../r/runtime/LazyResourceHandlerFactory.java |  4 +--
 3 files changed, 11 insertions(+), 27 deletions(-)

diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/EagerResourceHandlerFactory.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/EagerResourceHandlerFactory.java
index a9e8526f52..039455b5ba 100644
--- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/EagerResourceHandlerFactory.java
+++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/EagerResourceHandlerFactory.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2018, 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
@@ -80,7 +80,6 @@ public class EagerResourceHandlerFactory extends ResourceHandlerFactory implemen
         String fileName = Paths.get(name).getFileName().toString();
         FileInfo fileInfo = files.get(fileName);
         if (fileInfo == null || fileInfo.data == null) {
-            RInternalError.shouldNotReachHere("getResourceAsStream: failed to find resource: " + name);
             return null;
         } else {
             return new ByteArrayInputStream(fileInfo.data);
@@ -92,23 +91,6 @@ public class EagerResourceHandlerFactory extends ResourceHandlerFactory implemen
         return this;
     }
 
-    public static boolean addResource(@SuppressWarnings("unused") Class<?> accessor, String name, String path, boolean capture) {
-        byte[] bytes = null;
-        if (capture) {
-            try {
-                bytes = Files.readAllBytes(Paths.get(path));
-            } catch (IOException ex) {
-                return false;
-            }
-        }
-        try {
-            files.put(name, new FileInfo(name, new URL("file://" + path), bytes));
-        } catch (MalformedURLException ex) {
-            RInternalError.shouldNotReachHere("addResource " + ex.getMessage());
-        }
-        return true;
-    }
-
     private static void gatherResources() {
         CodeSource source = RBuiltinPackage.class.getProtectionDomain().getCodeSource();
         try {
@@ -118,7 +100,7 @@ public class EagerResourceHandlerFactory extends ResourceHandlerFactory implemen
             while (iter.hasMoreElements()) {
                 JarEntry entry = iter.nextElement();
                 String name = entry.getName();
-                if (name.endsWith(".R") || name.endsWith("CONTRIBUTORS")) {
+                if (name.endsWith(".R") || name.endsWith("CONTRIBUTORS") || name.endsWith(".Rd")) {
                     int size = (int) entry.getSize();
                     byte[] buf = new byte[size];
                     InputStream is = fastrJar.getInputStream(entry);
@@ -142,9 +124,10 @@ public class EagerResourceHandlerFactory extends ResourceHandlerFactory implemen
     public Map<String, String> getRFiles(Class<?> accessor, String pkgName) {
         Map<String, String> result = new HashMap<>();
         for (Map.Entry<String, FileInfo> entry : files.entrySet()) {
-            if (entry.getValue().url.toString().contains(pkgName + "/R")) {
+            String url = entry.getValue().url.toString();
+            if (url.endsWith(".R") && url.contains(pkgName + "/R")) {
                 String content = new String(entry.getValue().data);
-                result.put(entry.getValue().url.toString(), content);
+                result.put(url, content);
             }
         }
         return result;
diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/fastr/FastRHelp.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/fastr/FastRHelp.java
index 527e6e9b7b..4f0b1b556e 100644
--- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/fastr/FastRHelp.java
+++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/fastr/FastRHelp.java
@@ -32,6 +32,8 @@ import static com.oracle.truffle.r.nodes.builtin.CastBuilder.Predef.stringValue;
 import com.oracle.truffle.r.nodes.builtin.RBuiltinNode;
 import com.oracle.truffle.r.runtime.RError;
 import static com.oracle.truffle.r.runtime.RVisibility.ON;
+
+import com.oracle.truffle.r.runtime.ResourceHandlerFactory;
 import com.oracle.truffle.r.runtime.builtins.RBuiltin;
 import com.oracle.truffle.r.runtime.data.RNull;
 import java.io.BufferedReader;
@@ -53,12 +55,11 @@ public class FastRHelp {
         @TruffleBoundary
         public Object helpPath(String builtinName) {
             String path = "/com/oracle/truffle/r/nodes/builtin/base/Rd/" + builtinName + ".Rd";
-            try (InputStream in = getClass().getResourceAsStream(path)) {
+            try (InputStream in = ResourceHandlerFactory.getHandler().getResourceAsStream(getClass(), path)) {
                 if (in != null) {
                     return path;
                 }
             } catch (IOException ex) {
-
             }
             return RNull.instance;
         }
@@ -75,7 +76,7 @@ public class FastRHelp {
         @Specialization()
         @TruffleBoundary
         public Object getHelpRdPath(String path) {
-            try (InputStream in = getClass().getResourceAsStream(path)) {
+            try (InputStream in = ResourceHandlerFactory.getHandler().getResourceAsStream(getClass(), path)) {
                 if (in != null) {
                     try (BufferedReader r = new BufferedReader(new InputStreamReader(in))) {
                         StringBuilder sb = new StringBuilder();
diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/LazyResourceHandlerFactory.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/LazyResourceHandlerFactory.java
index 649c8a0e75..800c076625 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/LazyResourceHandlerFactory.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/LazyResourceHandlerFactory.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 2018, 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
@@ -105,7 +105,7 @@ class LazyResourceHandlerFactory extends ResourceHandlerFactory implements Handl
             }
             return result;
         } catch (Exception ex) {
-            Utils.rSuicide(ex.getMessage());
+            Utils.rSuicide(ex, "Could not load R files from resources. Details: " + ex.getMessage());
             return null;
         }
     }
-- 
GitLab