Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.jruby.ext.ffi.jffi;
  
  import org.jruby.Ruby;
  import org.jruby.RubyProc;
 
Wraps a ruby proc in a JFFI Closure
 
 final class NativeClosureProxy implements Closure {
     private static final int LONG_SIZE = Platform.getPlatform().longSize();
     protected final Ruby runtime;
     protected final NativeFunctionInfo closureInfo;
     private final WeakReference<Objectproc;
 
     NativeClosureProxy(Ruby runtimeNativeFunctionInfo closureInfoObject proc) {
         this. = runtime;
         this. = closureInfo;
         this. = new WeakReference<Object>(proc);
     }
 
     public void invoke(Buffer buffer) {
         Object recv = .get();
         if (recv == null) {
             buffer.setIntReturn(0);
             return;
         }
         invoke(bufferrecv);
     }
 
     protected final void invoke(Buffer bufferObject recv) {
         ThreadContext context = .getCurrentContext();
 
         IRubyObject[] params = new IRubyObject[..];
         for (int i = 0; i < params.length; ++i) {
             params[i] = fromNative(.[i], bufferi);
         }
 
         IRubyObject retVal;
         if (recv instanceof RubyProc) {
             retVal = ((RubyProcrecv).call(contextparams);
         } else if (recv instanceof Block) {
             retVal = ((Blockrecv).call(contextparams);
         } else {
             retVal = ((IRubyObjectrecv).callMethod(context"call"params);
         }
 
         setReturnValue(.bufferretVal);
     }


    
Extracts the primitive value from a Ruby object. This is similar to Util.longValue(), except it won't throw exceptions for invalid values.

Parameters:
value The Ruby object to convert
Returns:
a java long value.
 
     private static final long longValue(IRubyObject value) {
         if (value instanceof RubyNumeric) {
             return ((RubyNumericvalue).getLongValue();
         } else if (value.isNil()) {
             return 0L;
         }
         return 0;
     }

    
Extracts the primitive value from a Ruby object. This is similar to Util.longValue(), except it won't throw exceptions for invalid values.

Parameters:
value The Ruby object to convert
Returns:
a java long value.
 
     private static final long addressValue(IRubyObject value) {
         if (value instanceof RubyNumeric) {
             return ((RubyNumericvalue).getLongValue();
         } else if (value instanceof Pointer) {
             return ((Pointervalue).getAddress();
         } else if (value.isNil()) {
             return 0L;
         }
         return 0;
     }

    
Converts a ruby return value into a native callback return value.

Parameters:
runtime The ruby runtime the callback is attached to
type The ruby type of the return value
buffer The native parameter buffer
value The ruby value
    private static final void setReturnValue(Ruby runtimeType type,
            Closure.Buffer bufferIRubyObject value) {
        if (type instanceof Type.Builtin) {
            switch (type.getNativeType()) {
                case :
                    break;
                case :
                    buffer.setByteReturn((bytelongValue(value)); break;
                case :
                    buffer.setByteReturn((bytelongValue(value)); break;
                case :
                    buffer.setShortReturn((shortlongValue(value)); break;
                case :
                    buffer.setShortReturn((shortlongValue(value)); break;
                case :
                    buffer.setIntReturn((intlongValue(value)); break;
                case :
                    buffer.setIntReturn((intlongValue(value)); break;
                case :
                    buffer.setLongReturn(Util.int64Value(value)); break;
                case :
                    buffer.setLongReturn(Util.uint64Value(value)); break;
                case :
                    if ( == 32) {
                        buffer.setIntReturn((intlongValue(value));
                    } else {
                        buffer.setLongReturn(Util.int64Value(value));
                    }
                    break;
                case :
                    if ( == 32) {
                        buffer.setIntReturn((intlongValue(value));
                    } else {
                        buffer.setLongReturn(Util.uint64Value(value));
                    }
                    break;
                case :
                    buffer.setFloatReturn((float) RubyNumeric.num2dbl(value)); break;
                case :
                    buffer.setDoubleReturn(RubyNumeric.num2dbl(value)); break;
                case :
                    buffer.setAddressReturn(addressValue(value)); break;
                case :
                    buffer.setIntReturn(value.isTrue() ? 1 : 0); break;
                default:
            }
        } else if (type instanceof CallbackInfo) {
            if (value instanceof RubyProc || value.respondsTo("call")) {
                Pointer cb = Factory.getInstance().getCallbackManager().getCallback(runtime, (CallbackInfotypevalue);
                buffer.setAddressReturn(addressValue(cb));
            } else {
                buffer.setAddressReturn(0L);
                throw runtime.newTypeError("invalid callback return value, expected Proc or callable object");
            }
        } else if (type instanceof StructByValue) {
            if (value instanceof Struct) {
                Struct s = (Structvalue;
                MemoryIO memory = s.getMemory().getMemoryIO();
                if (memory instanceof DirectMemoryIO) {
                    long address = ((DirectMemoryIOmemory).getAddress();
                    if (address != 0) {
                        buffer.setStructReturn(address);
                    } else {
                        // Zero it out
                        buffer.setStructReturn(new byte[type.getNativeSize()], 0);
                    }
                } else if (memory instanceof ArrayMemoryIO) {
                    ArrayMemoryIO arrayMemory = (ArrayMemoryIOmemory;
                    if (arrayMemory.arrayLength() < type.getNativeSize()) {
                        throw runtime.newRuntimeError("size of struct returned from callback too small");
                    }
                    buffer.setStructReturn(arrayMemory.array(), arrayMemory.arrayOffset());
                } else {
                    throw runtime.newRuntimeError("struct return value has illegal backing memory");
                }
            } else if (value.isNil()) {
                // Zero it out
                buffer.setStructReturn(new byte[type.getNativeSize()], 0);
            } else {
                throw runtime.newTypeError(valueruntime.getFFI().);
            }
        } else if (type instanceof MappedType) {
            MappedType mappedType = (MappedTypetype;
            setReturnValue(runtimemappedType.getRealType(), buffermappedType.toNative(runtime.getCurrentContext(), value));
        } else {
            buffer.setLongReturn(0L);
            throw runtime.newRuntimeError("unsupported return type from struct: " + type);
        }
    }

    
Converts a native value into a ruby object.

Parameters:
runtime The ruby runtime to create the ruby object in
type The type of the native parameter
buffer The JFFI Closure parameter buffer.
index The index of the parameter in the buffer.
Returns:
A new Ruby object.
    private static final IRubyObject fromNative(Ruby runtimeType type,
            Closure.Buffer bufferint index) {
        if (type instanceof Type.Builtin) {
            switch (type.getNativeType()) {
                case :
                    return runtime.getNil();
                case :
                    return Util.newSigned8(runtimebuffer.getByte(index));
                case :
                    return Util.newUnsigned8(runtimebuffer.getByte(index));
                case :
                    return Util.newSigned16(runtimebuffer.getShort(index));
                case :
                    return Util.newUnsigned16(runtimebuffer.getShort(index));
                case :
                    return Util.newSigned32(runtimebuffer.getInt(index));
                case :
                    return Util.newUnsigned32(runtimebuffer.getInt(index));
                case :
                    return Util.newSigned64(runtimebuffer.getLong(index));
                case :
                    return Util.newUnsigned64(runtimebuffer.getLong(index));
                case :
                    return  == 32
                            ? Util.newSigned32(runtimebuffer.getInt(index))
                            : Util.newSigned64(runtimebuffer.getLong(index));
                case :
                    return  == 32
                            ? Util.newUnsigned32(runtimebuffer.getInt(index))
                            : Util.newUnsigned64(runtimebuffer.getLong(index));
                case :
                    return runtime.newFloat(buffer.getFloat(index));
                case :
                    return runtime.newFloat(buffer.getDouble(index));
                case :
                    return new Pointer(runtime, NativeMemoryIO.wrap(runtimebuffer.getAddress(index)));
                case :
                case :
                    return getStringParameter(runtimebufferindex);
                case :
                    return runtime.newBoolean(buffer.getByte(index) != 0);
                default:
                    throw runtime.newTypeError("invalid callback parameter type " + type);
            }
        } else if (type instanceof CallbackInfo) {
            final CallbackInfo cbInfo = (CallbackInfotype;
            final long address = buffer.getAddress(index);
            return address != 0
                ? new Function(runtimecbInfo.getMetaClass(),
                    new CodeMemoryIO(runtimeaddress),
                    cbInfo.getReturnType(), cbInfo.getParameterTypes(),
                    cbInfo.isStdcall() ? . : .runtime.getNil(), false)
                : runtime.getNil();
        } else if (type instanceof StructByValue) {
            StructByValue sbv = (StructByValuetype;
            final long address = buffer.getStruct(index);
            DirectMemoryIO memory = address != 0
                    ? new BoundedNativeMemoryIO(runtimeaddresstype.getNativeSize())
                    : runtime.getFFI().getNullMemoryIO();
            return sbv.getStructClass().newInstance(runtime.getCurrentContext(),
                        new IRubyObject[] { new Pointer(runtimememory) },
                        .);
        } else if (type instanceof MappedType) {
            MappedType mappedType = (MappedTypetype;
            return mappedType.fromNative(runtime.getCurrentContext(), fromNative(runtimemappedType.getRealType(), bufferindex));
        } else {
            throw runtime.newTypeError("unsupported callback parameter type: " + type);
        }
    }

    
Converts a native string value into a ruby string object.

Parameters:
runtime The ruby runtime to create the ruby string in
buffer The JFFI Closure parameter buffer.
index The index of the parameter in the buffer.
Returns:
A new Ruby string object or nil if string is NULL.
    private static final IRubyObject getStringParameter(Ruby runtimeClosure.Buffer bufferint index) {
        return FFIUtil.getString(runtimebuffer.getAddress(index));
    }
New to GrepCode? Check out our FAQ X