示例#1
0
文件: Util.java 项目: jlieske/jruby
 private static final long longValue(RubyString parameter) {
   CharSequence cs = parameter.asJavaString();
   if (cs.length() == 1) {
     return cs.charAt(0);
   }
   throw parameter.getRuntime().newRangeError("Value " + parameter + " is not an integer");
 }
示例#2
0
  /**
   * this method uses the appropriate lookup strategy to find a file. It is used by Kernel#require.
   *
   * @mri rb_find_file
   * @param name the file to find, this is a path name
   * @return the correct file
   */
  protected LoadServiceResource findFileInClasspath(String name) {
    // Look in classpath next (we do not use File as a test since UNC names will match)
    // Note: Jar resources must NEVER begin with an '/'. (previous code said "always begin with a
    // /")
    ClassLoader classLoader = runtime.getJRubyClassLoader();

    // handle security-sensitive case
    if (Ruby.isSecurityRestricted() && classLoader == null) {
      classLoader = runtime.getInstanceConfig().getLoader();
    }

    // absolute classpath URI, no need to iterate over loadpaths
    if (name.startsWith("classpath:/")) {
      LoadServiceResource foundResource = getClassPathResource(classLoader, name);
      if (foundResource != null) {
        return foundResource;
      }
    } else if (name.startsWith("classpath:")) {
      // "relative" classpath URI
      name = name.substring("classpath:".length());
    }

    for (int i = 0; i < loadPath.size(); i++) {
      // TODO this is really inefficient, and potentially a problem everytime anyone require's
      // something.
      // we should try to make LoadPath a special array object.
      RubyString entryString = loadPath.eltInternal(i).convertToString();
      String entry = entryString.asJavaString();

      // if entry is an empty string, skip it
      if (entry.length() == 0) continue;

      // if entry starts with a slash, skip it since classloader resources never start with a /
      if (entry.charAt(0) == '/' || (entry.length() > 1 && entry.charAt(1) == ':')) continue;

      if (entry.startsWith("classpath:/")) {
        entry = entry.substring("classpath:/".length());
      } else if (entry.startsWith("classpath:")) {
        entry = entry.substring("classpath:".length());
      }

      // otherwise, try to load from classpath (Note: Jar resources always uses '/')
      LoadServiceResource foundResource = getClassPathResource(classLoader, entry + "/" + name);
      if (foundResource != null) {
        return foundResource;
      }
    }

    // if name starts with a / we're done (classloader resources won't load with an initial /)
    if (name.charAt(0) == '/' || (name.length() > 1 && name.charAt(1) == ':')) return null;

    // Try to load from classpath without prefix. "A/b.rb" will not load as
    // "./A/b.rb" in a jar file.
    LoadServiceResource foundResource = getClassPathResource(classLoader, name);
    if (foundResource != null) {
      return foundResource;
    }

    return null;
  }
示例#3
0
  public static String expandPath(RubyContext context, String fileName) {
    // TODO (nirvdrum 11-Feb-15) This needs to work on Windows without calling into non-Truffle
    // JRuby.
    if (context.isRunningOnWindows()) {
      final org.jruby.RubyString path =
          context.toJRubyString(
              StringNodes.createString(context.getCoreLibrary().getStringClass(), fileName));
      final org.jruby.RubyString expanded =
          (org.jruby.RubyString)
              org.jruby.RubyFile.expand_path19(
                  context.getRuntime().getCurrentContext(),
                  null,
                  new org.jruby.runtime.builtin.IRubyObject[] {path});

      return expanded.asJavaString();
    } else {
      return expandPath(fileName, null);
    }
  }
示例#4
0
  protected LoadServiceResource tryResourceFromLoadPathOrURL(
      SearchState state, String baseName, SuffixType suffixType) {
    LoadServiceResource foundResource = null;

    // if it's a ./ baseName, use CWD logic
    if (baseName.startsWith("./")) {
      foundResource = tryResourceFromCWD(state, baseName, suffixType);

      if (foundResource != null) {
        state.loadName = resolveLoadName(foundResource, foundResource.getName());
        return foundResource;
      }
    }

    // if it's a ~/ baseName use HOME logic
    if (baseName.startsWith("~/")) {
      foundResource = tryResourceFromHome(state, baseName, suffixType);

      if (foundResource != null) {
        state.loadName = resolveLoadName(foundResource, foundResource.getName());
        return foundResource;
      }
    }

    // if given path is absolute, just try it as-is (with extensions) and no load path
    if (new File(baseName).isAbsolute() || baseName.startsWith("../")) {
      for (String suffix : suffixType.getSuffixes()) {
        String namePlusSuffix = baseName + suffix;
        foundResource = tryResourceAsIs(namePlusSuffix);

        if (foundResource != null) {
          state.loadName = resolveLoadName(foundResource, namePlusSuffix);
          return foundResource;
        }
      }

      return null;
    }

    Outer:
    for (int i = 0; i < loadPath.size(); i++) {
      // TODO this is really inefficient, and potentially a problem everytime anyone require's
      // something.
      // we should try to make LoadPath a special array object.
      RubyString entryString = loadPath.eltInternal(i).convertToString();
      String loadPathEntry = entryString.asJavaString();

      if (loadPathEntry.equals(".") || loadPathEntry.equals("")) {
        foundResource = tryResourceFromCWD(state, baseName, suffixType);

        if (foundResource != null) {
          String ss = foundResource.getName();
          if (ss.startsWith("./")) {
            ss = ss.substring(2);
          }
          state.loadName = resolveLoadName(foundResource, ss);
          break Outer;
        }
      } else {
        boolean looksLikeJarURL = loadPathLooksLikeJarURL(loadPathEntry);
        for (String suffix : suffixType.getSuffixes()) {
          String namePlusSuffix = baseName + suffix;

          if (looksLikeJarURL) {
            foundResource = tryResourceFromJarURLWithLoadPath(namePlusSuffix, loadPathEntry);
          } else if (namePlusSuffix.startsWith("./")) {
            throw runtime.newLoadError("");
          } else {
            foundResource = tryResourceFromLoadPath(namePlusSuffix, loadPathEntry);
          }

          if (foundResource != null) {
            String ss = namePlusSuffix;
            if (ss.startsWith("./")) {
              ss = ss.substring(2);
            }
            state.loadName = resolveLoadName(foundResource, ss);
            break Outer; // end suffix iteration
          }
        }
      }
    }

    return foundResource;
  }