Skip to content

Commit

Permalink
JRUBY-4717: Import digest.rb from CRuby stdlib.
Browse files Browse the repository at this point in the history
I broke digest lib for 1.8 at 51bf9d9 and Nick fixed it at 288ef4a.
And this is a new try for re-fixing digest library for JRUBY-4717.

 * Update tool/globals_1_8_7.rb to import digest.rb for 1.8 as same as
   1.9.
 * Remove Java implementation of Digest.const_missing. It's defined in
   lib/digest.rb for both 1.8 and 1.9.
 * Revert 288ef1ae. It does not mean that 288ef4a is wrong. It's
   51bf9d9 which should be fixed and needs above fixes. 288ef4a was
   needed.

With importing lib/digest.rb for 1.8 mode, Digest function start working
for 1.8 as same as CRuby.

% ruby187 -rdigest -e 'p Digest("SHA2")'
Digest::SHA2

% jruby -rdigest -e 'p Digest("SHA2")'
Digest::SHA2

% jruby163 -rdigest -e 'p Digest("SHA2")'
NoMethodError: undefined method `Digest' for main:Object
  (root) at -e:1
  • Loading branch information
Hiroshi Nakamura committed Aug 2, 2011
1 parent 1ca723b commit 08c38b4
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 26 deletions.
50 changes: 50 additions & 0 deletions lib/ruby/1.8/digest.rb
@@ -0,0 +1,50 @@
require 'digest.so'

module Digest
def self.const_missing(name)
case name
when :SHA256, :SHA384, :SHA512
lib = 'digest/sha2.so'
else
lib = File.join('digest', name.to_s.downcase)
end

begin
require lib
rescue LoadError => e
raise LoadError, "library not found for class Digest::#{name} -- #{lib}", caller(1)
end
unless Digest.const_defined?(name)
raise NameError, "uninitialized constant Digest::#{name}", caller(1)
end
Digest.const_get(name)
end

class ::Digest::Class
# creates a digest object and reads a given file, _name_.
#
# p Digest::SHA256.file("X11R6.8.2-src.tar.bz2").hexdigest
# # => "f02e3c85572dc9ad7cb77c2a638e3be24cc1b5bea9fdbb0b0299c9668475c534"
def self.file(name)
new.file(name)
end
end

module Instance
# updates the digest with the contents of a given file _name_ and
# returns self.
def file(name)
File.open(name, "rb") {|f|
buf = ""
while f.read(16384, buf)
update buf
end
}
self
end
end
end

def Digest(name)
Digest.const_get(name)
end
7 changes: 1 addition & 6 deletions src/org/jruby/Ruby.java
Expand Up @@ -1440,12 +1440,7 @@ private void initBuiltins() {
addLazyBuiltin("readline.jar", "readline", "org.jruby.ext.ReadlineService"); addLazyBuiltin("readline.jar", "readline", "org.jruby.ext.ReadlineService");
addLazyBuiltin("thread.jar", "thread", "org.jruby.libraries.ThreadLibrary"); addLazyBuiltin("thread.jar", "thread", "org.jruby.libraries.ThreadLibrary");
addLazyBuiltin("thread.rb", "thread", "org.jruby.libraries.ThreadLibrary"); addLazyBuiltin("thread.rb", "thread", "org.jruby.libraries.ThreadLibrary");
if (is1_9()) { // JRUBY-4717: for 1.9 we should use methods in 1.9/digest.rb addLazyBuiltin("digest.jar", "digest.so", "org.jruby.libraries.DigestLibrary");
addLazyBuiltin("digest.jar", "digest.so", "org.jruby.libraries.DigestLibrary");
} else {
addLazyBuiltin("digest.jar", "digest", "org.jruby.libraries.DigestLibrary");
addLazyBuiltin("digest.rb", "digest", "org.jruby.libraries.DigestLibrary");
}
addLazyBuiltin("digest/md5.jar", "digest/md5", "org.jruby.libraries.MD5"); addLazyBuiltin("digest/md5.jar", "digest/md5", "org.jruby.libraries.MD5");
addLazyBuiltin("digest/rmd160.jar", "digest/rmd160", "org.jruby.libraries.RMD160"); addLazyBuiltin("digest/rmd160.jar", "digest/rmd160", "org.jruby.libraries.RMD160");
addLazyBuiltin("digest/sha1.jar", "digest/sha1", "org.jruby.libraries.SHA1"); addLazyBuiltin("digest/sha1.jar", "digest/sha1", "org.jruby.libraries.SHA1");
Expand Down
20 changes: 0 additions & 20 deletions src/org/jruby/RubyDigest.java
Expand Up @@ -122,26 +122,6 @@ public static IRubyObject s_hexencode(IRubyObject recv, IRubyObject arg) {
return toHexString(recv.getRuntime(), arg.convertToString().getBytes()); return toHexString(recv.getRuntime(), arg.convertToString().getBytes());
} }


@JRubyMethod(name = "const_missing", required = 1, module = true)
public static IRubyObject const_missing(ThreadContext ctx, IRubyObject recv, IRubyObject symbol) {
Ruby runtime = ctx.getRuntime();
String sym = ((RubySymbol)symbol).asJavaString();
String libName;
if("SHA256".equals(sym) || "SHA384".equals(sym) || "SHA512".equals(sym)) {
libName = "digest/sha2.jar";
}
else {
libName = "digest/" + sym.toLowerCase();
}

runtime.getLoadService().require(libName);
RubyModule digest = runtime.getModule("Digest");
if(!digest.hasConstant(sym)) {
throw runtime.newNameError("unitialized constant Digest::" + sym, "Digest::" + sym);
}
return digest.getConstant(sym);
}



private static class Metadata { private static class Metadata {


Expand Down
1 change: 1 addition & 0 deletions tool/globals_1_8_7.rb
Expand Up @@ -103,6 +103,7 @@
'ext/pty/lib/expect.rb' => 'expect.rb', 'ext/pty/lib/expect.rb' => 'expect.rb',
'ext/io/wait/lib/nonblock.rb' => 'io/nonblock.rb', 'ext/io/wait/lib/nonblock.rb' => 'io/nonblock.rb',
'ext/nkf/lib/kconv.rb' => 'kconv.rb', 'ext/nkf/lib/kconv.rb' => 'kconv.rb',
'ext/digest/lib/digest.rb' => 'digest.rb',
'ext/digest/lib/md5.rb' => 'md5.rb', 'ext/digest/lib/md5.rb' => 'md5.rb',
'ext/digest/lib/sha1.rb' => 'sha1.rb', 'ext/digest/lib/sha1.rb' => 'sha1.rb',
'ext/digest/sha2/lib/sha2.rb' => 'digest/sha2.rb', 'ext/digest/sha2/lib/sha2.rb' => 'digest/sha2.rb',
Expand Down

0 comments on commit 08c38b4

Please sign in to comment.