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: 32801a3465e8
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: cfb0156ebfbf
Choose a head ref
  • 4 commits
  • 2 files changed
  • 1 contributor

Commits on Apr 5, 2016

  1. Copy the full SHA
    749875e View commit details
  2. Copy the full SHA
    332d0a2 View commit details
  3. Copy the full SHA
    9b9ac57 View commit details
  4. Copy the full SHA
    cfb0156 View commit details
Showing with 24 additions and 18 deletions.
  1. +16 −10 truffle/src/main/java/org/jruby/truffle/language/dispatch/CachedForeignDispatchNode.java
  2. +8 −8 truffle/src/test/ruby/tck.rb
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@
import com.oracle.truffle.api.interop.ForeignAccess;
import com.oracle.truffle.api.interop.Message;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.interop.UnknownIdentifierException;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.interop.UnsupportedTypeException;
import com.oracle.truffle.api.nodes.ExplodeLoop;
@@ -26,13 +27,16 @@
import org.jruby.truffle.interop.RubyToForeignNodeGen;
import org.jruby.truffle.language.RubyNode;

import java.util.Arrays;

public final class CachedForeignDispatchNode extends CachedDispatchNode {

private final String name;
private final String nameForMessage;
private final int arity;

@Child private Node directArray;
@Child private Node directArrayRead;
@Child private Node directArrayWrite;
@Child private Node directField;
@Child private Node directCall;
@Child private Node nullCheck;
@@ -55,9 +59,9 @@ public CachedForeignDispatchNode(RubyContext context, DispatchNode next, Object

private void initializeNodes(RubyContext context, int arity) {
if (name.equals("[]")) {
directArray = Message.READ.createNode();
directArrayRead = Message.READ.createNode();
} else if (name.equals("[]=")) {
directArray = Message.WRITE.createNode();
directArrayWrite = Message.WRITE.createNode();
} else if (name.endsWith("=") && arity == 1) {
directField = Message.WRITE.createNode();
} else if (name.equals("call")) {
@@ -103,29 +107,31 @@ private Object doDispatch(VirtualFrame frame, TruffleObject receiverObject, Obje
}

try {
if (directArray != null) {
if (directArrayRead != null) {
Object[] args = prepareArguments.convertArguments(frame, arguments, 0);
return ForeignAccess.sendRead(directArrayRead, frame, receiverObject, args[0]);
} else if (directArrayWrite != null) {
Object[] args = prepareArguments.convertArguments(frame, arguments, 0);
return ForeignAccess.sendExecute(directArray, frame, receiverObject, args);
return ForeignAccess.sendWrite(directArrayWrite, frame, receiverObject, args[0], args[1]);
} else if (directField != null) {
Object[] args = prepareArguments.convertArguments(frame, arguments, 1);
args[0] = nameForMessage;
return ForeignAccess.sendExecute(directField, frame, receiverObject, args);
return ForeignAccess.sendWrite(directField, frame, receiverObject, args[0], args[1]);
} else if (directCall != null) {
Object[] args;
args = prepareArguments.convertArguments(frame, arguments, 0);
return ForeignAccess.sendExecute(directCall, frame, receiverObject, args);
} else if (nullCheck != null) {
Object[] args = prepareArguments.convertArguments(frame, arguments, 0);
return ForeignAccess.sendExecute(nullCheck, frame, receiverObject, args);
return ForeignAccess.sendIsNull(nullCheck, frame, receiverObject);
} else if (access != null) {
Object[] args = prepareArguments.convertArguments(frame, arguments, 1);
args[0] = name;
return ForeignAccess.sendExecute(access, frame, receiverObject, args);
return ForeignAccess.sendInvoke(access, frame, receiverObject, name, Arrays.copyOfRange(args, 1, args.length - 1));
} else {
CompilerDirectives.transferToInterpreter();
throw new IllegalStateException();
}
} catch (ArityException | UnsupportedMessageException | UnsupportedTypeException e) {
} catch (ArityException | UnsupportedMessageException | UnsupportedTypeException | UnknownIdentifierException e) {
CompilerDirectives.transferToInterpreter();
throw new RuntimeException(e);
}
16 changes: 8 additions & 8 deletions truffle/src/test/ruby/tck.rb
Original file line number Diff line number Diff line change
@@ -33,7 +33,7 @@ def count_invocations
Truffle::Interop.export_method(:count_invocations)

def apply_numbers(f)
Truffle::Interop.execute(f, 18, 32) + 10
f.call(18, 32) + 10
end

Truffle::Interop.export_method(:apply_numbers)
@@ -79,23 +79,23 @@ def evaluate_source(mime, source)
Truffle::Interop.export_method(:evaluate_source)

def complex_add(a, b)
Truffle::Interop.write_property a, :imaginary, Truffle::Interop.read_property(a, :imaginary) + Truffle::Interop.read_property(b, :imaginary)
Truffle::Interop.write_property a, :real, Truffle::Interop.read_property(a, :real) + Truffle::Interop.read_property(b, :real)
a.imaginary = a[:imaginary] + b[:imaginary]
a.real = a[:real] + b[:real]
end

Truffle::Interop.export_method(:complex_add)

def complex_add_with_method(a, b)
Truffle::Interop.write_property a, :imaginary, Truffle::Interop.read_property(a, :imaginary) + Truffle::Interop.read_property(b, :imaginary)
Truffle::Interop.write_property a, :real, Truffle::Interop.read_property(a, :real) + Truffle::Interop.read_property(b, :real)
a.imaginary = a[:imaginary] + b[:imaginary]
a.real = a[:real] + b[:real]
end

Truffle::Interop.export_method(:complex_add_with_method)

def complex_sum_real(complexes)
complexes = Truffle::Interop.enumerable(complexes)

complexes.map{ |c| Truffle::Interop.read_property(c, :real) }.inject(&:+)
complexes.map{ |c| c[:real] }.inject(&:+)
end

Truffle::Interop.export_method(:complex_sum_real)
@@ -111,8 +111,8 @@ def complex_copy(a, b)
b = b.to_a

a.zip(b).each do |x, y|
Truffle::Interop.write_property x, :imaginary, Truffle::Interop.read_property(y, :imaginary)
Truffle::Interop.write_property x, :real, Truffle::Interop.read_property(y, :real)
x.imaginary = y[:imaginary]
x.real = y[:real]
end
end