Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 483bc56196c1
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 9c5e0c4d212f
Choose a head ref
  • 10 commits
  • 5 files changed
  • 1 contributor

Commits on May 1, 2016

  1. Copy the full SHA
    a8f92dc View commit details
  2. Copy the full SHA
    0420fc3 View commit details
  3. [Truffle] In the implementations I looked at, MessageDigest#digest is…

    … simple enough to compile.
    chrisseaton committed May 1, 2016
    Copy the full SHA
    9f3f821 View commit details
  4. Copy the full SHA
    774c4e6 View commit details
  5. [Truffle] After looking through the digests implementation, it looks …

    …like it's normal to be cloning them.
    chrisseaton committed May 1, 2016
    Copy the full SHA
    b553656 View commit details
  6. Copy the full SHA
    bc4d627 View commit details
  7. Copy the full SHA
    339bdf4 View commit details
  8. Copy the full SHA
    6c5cd0e View commit details
  9. Copy the full SHA
    2e2e972 View commit details
  10. [Truffle] Get the digest_length from the algorithm, rather than the J…

    …ava digest class which won't compile.
    chrisseaton committed May 1, 2016
    Copy the full SHA
    9c5e0c4 View commit details
5 changes: 2 additions & 3 deletions truffle/src/main/java/org/jruby/truffle/core/CoreLibrary.java
Original file line number Diff line number Diff line change
@@ -102,8 +102,7 @@
import org.jruby.truffle.platform.signal.SignalManager;
import org.jruby.truffle.stdlib.BigDecimalNodesFactory;
import org.jruby.truffle.stdlib.CoverageNodesFactory;
import org.jruby.truffle.stdlib.DigestLayoutImpl;
import org.jruby.truffle.stdlib.DigestNodesFactory;
import org.jruby.truffle.stdlib.digest.DigestNodesFactory;
import org.jruby.truffle.stdlib.EtcNodesFactory;
import org.jruby.truffle.stdlib.ObjSpaceNodesFactory;
import org.jruby.truffle.stdlib.psych.PsychEmitterNodesFactory;
@@ -643,7 +642,7 @@ public CoreLibrary(RubyContext context) {
globalVariables = new GlobalVariables(nilObject);

digestClass = defineClass(truffleModule, basicObjectClass, "Digest");
Layouts.CLASS.setInstanceFactoryUnsafe(digestClass, DigestLayoutImpl.INSTANCE.createDigestShape(digestClass, digestClass));
Layouts.CLASS.setInstanceFactoryUnsafe(digestClass, Layouts.DIGEST.createDigestShape(digestClass, digestClass));

// No need for new version since it's null before which is not cached
assert Layouts.CLASS.getSuperclass(basicObjectClass) == null;
3 changes: 3 additions & 0 deletions truffle/src/main/java/org/jruby/truffle/core/Layouts.java
Original file line number Diff line number Diff line change
@@ -82,6 +82,8 @@
import org.jruby.truffle.core.tracepoint.TracePointLayoutImpl;
import org.jruby.truffle.stdlib.BigDecimalLayout;
import org.jruby.truffle.stdlib.BigDecimalLayoutImpl;
import org.jruby.truffle.stdlib.digest.DigestLayout;
import org.jruby.truffle.stdlib.digest.DigestLayoutImpl;
import org.jruby.truffle.stdlib.psych.EmitterLayout;
import org.jruby.truffle.stdlib.psych.EmitterLayoutImpl;

@@ -127,6 +129,7 @@ public abstract class Layouts {
public static final AtomicReferenceLayout ATOMIC_REFERENCE = AtomicReferenceLayoutImpl.INSTANCE;
public static final HandleLayout HANDLE = HandleLayoutImpl.INSTANCE;
public static final TracePointLayout TRACE_POINT = TracePointLayoutImpl.INSTANCE;
public static final DigestLayout DIGEST = DigestLayoutImpl.INSTANCE;

// Other standard identifiers

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2015, 2016 Oracle and/or its affiliates. All rights reserved. This
* code is released under a tri EPL/GPL/LGPL license. You can use it,
* redistribute it and/or modify it under the terms of the:
*
* Eclipse Public License version 1.0
* GNU General Public License version 2
* GNU Lesser General Public License version 2.1
*/
package org.jruby.truffle.stdlib.digest;

enum DigestAlgorithm {
MD5("MD5", 16),
SHA1("SHA1", 20),
SHA256("SHA-256", 32),
SHA384("SHA-384", 48),
SHA512("SHA-512", 64);

private final String name;
private final int length;

DigestAlgorithm(String name, int length) {
this.name = name;
this.length = length;
}

public String getName() {
return name;
}

public int getLength() {
return length;
}
}
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@
* GNU General Public License version 2
* GNU Lesser General Public License version 2.1
*/
package org.jruby.truffle.stdlib;
package org.jruby.truffle.stdlib.digest;

import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.api.object.DynamicObjectFactory;
@@ -25,8 +25,11 @@ DynamicObjectFactory createDigestShape(

DynamicObject createDigest(
DynamicObjectFactory factory,
DigestAlgorithm algorithm,
MessageDigest digest);

DigestAlgorithm getAlgorithm(DynamicObject object);

MessageDigest getDigest(DynamicObject object);

}
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@
* GNU General Public License version 2
* GNU Lesser General Public License version 2.1
*/
package org.jruby.truffle.stdlib;
package org.jruby.truffle.stdlib.digest;

import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Specialization;
@@ -31,100 +31,78 @@
@CoreClass(name = "Truffle::Digest")
public abstract class DigestNodes {

private enum Algorithm {
MD5("MD5"),
SHA1("SHA1"),
SHA256("SHA-256"),
SHA384("SHA-384"),
SHA512("SHA-512");

private final String name;

Algorithm(String name) {
this.name = name;
}

public String getName() {
return name;
}
}

private static DynamicObject createDigest(RubyContext context, Algorithm algorithm) {
final MessageDigest digest;

@TruffleBoundary
private static MessageDigest getMessageDigestInstance(String name) {
try {
digest = MessageDigest.getInstance(algorithm.getName());
return MessageDigest.getInstance(name);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}

final DynamicObject rubyClass = context.getCoreLibrary().getDigestClass();

return DigestLayoutImpl.INSTANCE.createDigest(Layouts.CLASS.getInstanceFactory(rubyClass), digest);
private static DynamicObject createDigest(RubyContext context, DigestAlgorithm algorithm) {
return Layouts.DIGEST.createDigest(
Layouts.CLASS.getInstanceFactory(context.getCoreLibrary().getDigestClass()),
algorithm,
getMessageDigestInstance(algorithm.getName()));
}

@CoreMethod(names = "md5", onSingleton = true)
public abstract static class MD5Node extends CoreMethodArrayArgumentsNode {

@TruffleBoundary
@Specialization
public DynamicObject md5() {
return createDigest(getContext(), Algorithm.MD5);
return createDigest(getContext(), DigestAlgorithm.MD5);
}

}

@CoreMethod(names = "sha1", onSingleton = true)
public abstract static class SHA1Node extends CoreMethodArrayArgumentsNode {

@TruffleBoundary
@Specialization
public DynamicObject sha1() {
return createDigest(getContext(), Algorithm.SHA1);
return createDigest(getContext(), DigestAlgorithm.SHA1);
}

}

@CoreMethod(names = "sha256", onSingleton = true)
public abstract static class SHA256Node extends CoreMethodArrayArgumentsNode {

@TruffleBoundary
@Specialization
public DynamicObject sha256() {
return createDigest(getContext(), Algorithm.SHA256);
return createDigest(getContext(), DigestAlgorithm.SHA256);
}

}

@CoreMethod(names = "sha384", onSingleton = true)
public abstract static class SHA384Node extends CoreMethodArrayArgumentsNode {

@TruffleBoundary
@Specialization
public DynamicObject sha384() {
return createDigest(getContext(), Algorithm.SHA384);
return createDigest(getContext(), DigestAlgorithm.SHA384);
}

}

@CoreMethod(names = "sha512", onSingleton = true)
public abstract static class SHA512Node extends CoreMethodArrayArgumentsNode {

@TruffleBoundary
@Specialization
public DynamicObject sha512() {
return createDigest(getContext(), Algorithm.SHA512);
return createDigest(getContext(), DigestAlgorithm.SHA512);
}

}

@CoreMethod(names = "update", onSingleton = true, required = 2)
public abstract static class UpdateNode extends CoreMethodArrayArgumentsNode {

@TruffleBoundary
@Specialization(guards = "isRubyString(message)")
public DynamicObject update(DynamicObject digestObject, DynamicObject message) {
final MessageDigest digest = DigestLayoutImpl.INSTANCE.getDigest(digestObject);
final MessageDigest digest = Layouts.DIGEST.getDigest(digestObject);

RopeOperations.visitBytes(StringOperations.rope(message), new BytesVisitor() {

@@ -143,10 +121,9 @@ public void accept(byte[] bytes, int offset, int length) {
@CoreMethod(names = "reset", onSingleton = true, required = 1)
public abstract static class ResetNode extends CoreMethodArrayArgumentsNode {

@TruffleBoundary
@Specialization
public DynamicObject reset(DynamicObject digestObject) {
DigestLayoutImpl.INSTANCE.getDigest(digestObject).reset();
Layouts.DIGEST.getDigest(digestObject).reset();
return digestObject;
}

@@ -155,13 +132,16 @@ public DynamicObject reset(DynamicObject digestObject) {
@CoreMethod(names = "digest", onSingleton = true, required = 1)
public abstract static class DigestNode extends CoreMethodArrayArgumentsNode {

@TruffleBoundary
@Specialization
public DynamicObject digest(DynamicObject digestObject) {
final MessageDigest digest = DigestLayoutImpl.INSTANCE.getDigest(digestObject);
final MessageDigest digest = Layouts.DIGEST.getDigest(digestObject);

// TODO CS 18-May-15 this cloning isn't ideal for the key operation
return createString(RopeOperations.create(
cloneAndDigest(digest), ASCIIEncoding.INSTANCE, CodeRange.CR_VALID));
}

@TruffleBoundary
private static byte[] cloneAndDigest(MessageDigest digest) {
final MessageDigest clonedDigest;

try {
@@ -170,19 +150,17 @@ public DynamicObject digest(DynamicObject digestObject) {
throw new RuntimeException(e);
}

return createString(RopeOperations.create(
clonedDigest.digest(), ASCIIEncoding.INSTANCE, CodeRange.CR_VALID));
return clonedDigest.digest();
}

}

@CoreMethod(names = "digest_length", onSingleton = true, required = 1)
public abstract static class DigestLengthNode extends CoreMethodArrayArgumentsNode {

@TruffleBoundary
@Specialization
public int digestLength(DynamicObject digestObject) {
return DigestLayoutImpl.INSTANCE.getDigest(digestObject).getDigestLength();
return Layouts.DIGEST.getAlgorithm(digestObject).getLength();
}

}