diff --git a/core/src/main/java/org/jruby/runtime/load/CompiledScriptLoader.java b/core/src/main/java/org/jruby/runtime/load/CompiledScriptLoader.java index a7ce1d68df4..7f39d835191 100644 --- a/core/src/main/java/org/jruby/runtime/load/CompiledScriptLoader.java +++ b/core/src/main/java/org/jruby/runtime/load/CompiledScriptLoader.java @@ -6,10 +6,8 @@ package org.jruby.runtime.load; import org.jruby.Ruby; -import org.jruby.RubyFile; import org.jruby.ir.IRScope; import org.jruby.util.JRubyClassLoader; -import org.jruby.util.JRubyFile; import org.jruby.util.OneShotClassLoader; import org.objectweb.asm.ClassReader; @@ -28,57 +26,63 @@ */ public class CompiledScriptLoader { public static IRScope loadScriptFromFile(Ruby runtime, InputStream inStream, File resourcePath, String resourceName, boolean isAbsolute) { - InputStream in = null; + String name = getFilenameFromPathAndName(resourcePath, resourceName, isAbsolute); try { - in = new BufferedInputStream(inStream, 8192); - String name = normalizeSeps(resourceName); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - byte[] buf = new byte[8196]; - int read = 0; - while ((read = in.read(buf)) != -1) { - baos.write(buf, 0, read); - } - buf = baos.toByteArray(); - JRubyClassLoader jcl = runtime.getJRubyClassLoader(); - OneShotClassLoader oscl = new OneShotClassLoader(jcl); - - ClassReader cr = new ClassReader(buf); - String className = cr.getClassName().replace('/', '.'); - - Class clazz = oscl.defineClass(className, buf); - - File path = resourcePath; - - if(path != null && !isAbsolute) { - // Note: We use RubyFile's canonicalize rather than Java's, - // because Java's will follow symlinks and result in __FILE__ - // being set to the target of the symlink rather than the - // filename provided. - name = normalizeSeps(canonicalize(path.getPath())); - } + Class clazz = loadCompiledScriptFromClass(runtime, inStream); try { Method method = clazz.getMethod("loadIR", Ruby.class, String.class); return (IRScope)method.invoke(null, runtime, name); } catch (Exception e) { - e.printStackTrace(); - // fall through + if (runtime.getDebug().isTrue()) { + e.printStackTrace(); + } + throw runtime.newLoadError(name + " is not compiled Ruby; use java_import to load normal classes"); } - - throw runtime.newLoadError("use `java_import' to load normal Java classes: "+className); } catch (IOException e) { throw runtime.newIOErrorFromException(e); } catch (LinkageError le) { if (runtime.getDebug().isTrue()) { le.printStackTrace(); } - throw runtime.newLoadError("Linkage error loading compiled script; you may need to recompile '" + resourceName + "': " + le); + throw runtime.newLoadError("Linkage error loading compiled script; you may need to recompile '" + name + "': " + le); } finally { try { - in.close(); + inStream.close(); } catch (IOException ioe) { throw runtime.newIOErrorFromException(ioe); } } } + + private static Class loadCompiledScriptFromClass(Ruby runtime, InputStream in) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + byte[] buf = new byte[8192]; + int read; + while ((read = in.read(buf)) != -1) { + baos.write(buf, 0, read); + } + buf = baos.toByteArray(); + JRubyClassLoader jcl = runtime.getJRubyClassLoader(); + OneShotClassLoader oscl = new OneShotClassLoader(jcl); + + ClassReader cr = new ClassReader(buf); + String className = cr.getClassName().replace('/', '.'); + + return oscl.defineClass(className, buf); + } + + public static String getFilenameFromPathAndName(File resourcePath, String resourceName, boolean isAbsolute) { + String name = normalizeSeps(resourceName); + File path = resourcePath; + + if(path != null && !isAbsolute) { + // Note: We use RubyFile's canonicalize rather than Java's, + // because Java's will follow symlinks and result in __FILE__ + // being set to the target of the symlink rather than the + // filename provided. + name = normalizeSeps(canonicalize(path.getPath())); + } + return name; + } } diff --git a/core/src/main/java/org/jruby/runtime/load/ExternalScript.java b/core/src/main/java/org/jruby/runtime/load/ExternalScript.java index 5b0d79e2c12..9e0a547e04f 100644 --- a/core/src/main/java/org/jruby/runtime/load/ExternalScript.java +++ b/core/src/main/java/org/jruby/runtime/load/ExternalScript.java @@ -48,25 +48,15 @@ public void load(Ruby runtime, boolean wrap) { InputStream in = null; try { in = resource.getInputStream(); - String name = normalizeSeps(resource.getName()); + String name = resource.getName(); if (runtime.getInstanceConfig().getCompileMode().shouldPrecompileAll()) { runtime.compileAndLoadFile(name, in, wrap); } else { - java.io.File path = resource.getPath(); - - if(path != null && !resource.isAbsolute()) { - // Note: We use RubyFile's canonicalize rather than Java's, - // because Java's will follow symlinks and result in __FILE__ - // being set to the target of the symlink rather than the - // filename provided. - name = normalizeSeps(canonicalize(path.getPath())); - } + name = CompiledScriptLoader.getFilenameFromPathAndName(resource.getPath(), name, resource.isAbsolute()); runtime.loadFile(name, new LoadServiceResourceInputStream(in), wrap); } - - } catch (IOException e) { throw runtime.newIOErrorFromException(e); } finally { diff --git a/core/src/main/java/org/jruby/runtime/load/LibrarySearcher.java b/core/src/main/java/org/jruby/runtime/load/LibrarySearcher.java index 1a6aaa28fd5..9b8f7d4bff2 100644 --- a/core/src/main/java/org/jruby/runtime/load/LibrarySearcher.java +++ b/core/src/main/java/org/jruby/runtime/load/LibrarySearcher.java @@ -268,7 +268,7 @@ public void load(Ruby runtime, boolean wrap) { // Depending on the side-effect of the load, which loads the class but does not turn it into a script. // I don't like it, but until we restructure the code a bit more, we'll need to quietly let it by here. if (script == null) return; - + runtime.loadScope(script, wrap); } catch(IOException e) { throw runtime.newLoadError("no such file to load -- " + searchName, searchName);