Skip to content

Commit

Permalink
Showing 5 changed files with 46 additions and 13 deletions.
5 changes: 5 additions & 0 deletions truffle/src/main/java/org/jruby/truffle/nodes/RubyNode.java
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.nodes.UnexpectedResultException;
import com.oracle.truffle.api.source.SourceSection;
import jnr.ffi.provider.MemoryManager;
import jnr.posix.POSIX;
import org.jruby.truffle.nodes.instrument.RubyWrapperNode;
import org.jruby.truffle.runtime.RubyArguments;
@@ -269,6 +270,10 @@ public RubyContext getContext() {
return context;
}

public MemoryManager getMemoryManager() {
return jnr.ffi.Runtime.getSystemRuntime().getMemoryManager();
}

// ruby() helper

protected Object ruby(VirtualFrame frame, String expression, Object... arguments) {
Original file line number Diff line number Diff line change
@@ -23,6 +23,8 @@

public abstract class PointerPrimitiveNodes {

public static final Pointer NULL_POINTER = jnr.ffi.Runtime.getSystemRuntime().getMemoryManager().newOpaquePointer(0);

private static final HiddenKey POINTER_IDENTIFIER = new HiddenKey("pointer");
private static final Property POINTER_PROPERTY;
private static final DynamicObjectFactory POINTER_FACTORY;
@@ -34,10 +36,12 @@ public abstract class PointerPrimitiveNodes {
}

public static RubyBasicObject createPointer(RubyClass rubyClass, Pointer pointer) {
assert pointer != null;
return new RubyBasicObject(rubyClass, POINTER_FACTORY.newInstance(pointer));
}

public static void setPointer(RubyBasicObject pointer, Pointer newPointer) {
assert newPointer != null;
assert pointer.getDynamicObject().getShape().hasProperty(POINTER_IDENTIFIER);

try {
@@ -182,7 +186,8 @@ public PointerSetAtOffsetPrimitiveNode(RubyContext context, SourceSection source
}

@Specialization
public int setAtOffset(RubyBasicObject pointer, int offset, Object type, int value) {
public int setAtOffset(RubyBasicObject pointer, int offset, int type, int value) {
assert type == 5;
// TODO CS 13-May-15 what does the type parameter do?
getPointer(pointer).putInt(offset, value);
return value;
@@ -199,7 +204,7 @@ public PointerReadPointerPrimitiveNode(RubyContext context, SourceSection source

@Specialization
public RubyBasicObject readPointer(RubyBasicObject pointer) {
return createPointer(pointer.getLogicalClass(), getPointer(pointer).getPointer(0));
return createPointer(pointer.getLogicalClass(), nullOrPointer(getPointer(pointer).getPointer(0)));
}

}
@@ -218,4 +223,28 @@ public long address(RubyBasicObject pointer) {

}

@RubiniusPrimitive(name = "pointer_get_at_offset")
public static abstract class PointerGetAtOffsetPrimitiveNode extends RubiniusPrimitiveNode {

public PointerGetAtOffsetPrimitiveNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

@Specialization
public int getAtOffset(RubyBasicObject pointer, int offset, int type) {
assert type == 5;
// TODO CS 13-May-15 not sure about int/long here
return getPointer(pointer).getInt(offset);
}

}

private static Pointer nullOrPointer(Pointer pointer) {
if (pointer == null) {
return NULL_POINTER;
} else {
return pointer;
}
}

}
Original file line number Diff line number Diff line change
@@ -25,8 +25,4 @@ public RubiniusPrimitiveNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

public MemoryManager getMemoryManager() {
return jnr.ffi.Runtime.getSystemRuntime().getMemoryManager();
}

}
Original file line number Diff line number Diff line change
@@ -18,10 +18,12 @@

public class RubiniusConfiguration {

private static final int SIZE_OF_SHORT = 2;
private static final int SIZE_OF_INT = 4;
private static final int SIZE_OF_LONG = 8;
private static final int SIZE_OF_POINTER = 8;
public static final int SIZE_OF_SHORT = 2;
public static final int SIZE_OF_INT = 4;
public static final int SIZE_OF_LONG = 8;
public static final int SIZE_OF_POINTER = 8;

public static final int SIZE_OF_ADDRINFO = 4 * SIZE_OF_INT + 4 * SIZE_OF_POINTER;

private final RubyContext context;

7 changes: 4 additions & 3 deletions truffle/src/main/ruby/core/library.rb
Original file line number Diff line number Diff line change
@@ -55,13 +55,14 @@ def attach_function(name, a2, a3, a4=nil, a5=nil)

mname = name.to_sym

# But we've already implemented the actual methods elsewhere
# The difference is we already have the methods available in our version of
# the POSIX class

caller = Truffle::Primitive.source_of_caller

if caller.end_with? 'ruby/truffle/rubysl/rubysl-socket/lib/rubysl/socket.rb'
if Rubinius::FFI::Platform::Socket.respond_to? mname
define_method mname, Rubinius::FFI::Platform::Socket.method(mname)
if Rubinius::FFI::Platform::POSIX.respond_to? mname
define_method mname, Rubinius::FFI::Platform::POSIX.method(mname)
module_function mname
return
end

0 comments on commit da24a36

Please sign in to comment.