Example #1
0
 @JRubyMethod(
     name = {"create_invoker", "createInvoker"},
     required = 5)
 public IRubyObject createInvoker(ThreadContext context, IRubyObject[] args) {
   RubyArray paramTypes = (RubyArray) args[3];
   NativeParam[] nativeParamTypes = new NativeParam[paramTypes.size()];
   for (int i = 0; i < paramTypes.size(); ++i) {
     IRubyObject obj = (IRubyObject) paramTypes.entry(i);
     if (obj instanceof NativeParam) {
       nativeParamTypes[i] = (NativeParam) obj;
     } else if (obj instanceof RubyInteger) {
       nativeParamTypes[i] = NativeType.valueOf(Util.int32Value(obj));
     } else {
       context.getRuntime().newArgumentError("Invalid parameter type");
     }
   }
   try {
     return createInvoker(
         context.getRuntime(),
         args[0].isNil() ? null : args[0].toString(),
         args[1].toString(),
         NativeType.valueOf(Util.int32Value(args[2])),
         nativeParamTypes,
         args[4].toString());
   } catch (UnsatisfiedLinkError ex) {
     return context.getRuntime().getNil();
   }
 }
Example #2
0
    public void put(
        ThreadContext context,
        StructLayout.Storage cache,
        Member m,
        IRubyObject ptr,
        IRubyObject value) {
      if (value instanceof Pointer) {
        m.getMemoryIO(ptr).putMemoryIO(m.offset, ((Pointer) value).getMemoryIO());
      } else if (value instanceof Struct) {
        MemoryIO mem = ((Struct) value).getMemoryIO();

        if (!(mem instanceof DirectMemoryIO)) {
          throw context
              .getRuntime()
              .newArgumentError("Struct memory not backed by a native pointer");
        }
        m.getMemoryIO(ptr).putMemoryIO(m.offset, mem);

      } else if (value instanceof RubyInteger) {
        m.getMemoryIO(ptr).putAddress(m.offset, Util.int64Value(ptr));
      } else if (value.respondsTo("to_ptr")) {
        IRubyObject addr = value.callMethod(context, "to_ptr");
        if (addr instanceof Pointer) {
          m.getMemoryIO(ptr).putMemoryIO(m.offset, ((Pointer) addr).getMemoryIO());
        } else {
          throw context.getRuntime().newArgumentError("Invalid pointer value");
        }
      } else if (value.isNil()) {
        m.getMemoryIO(ptr).putAddress(m.offset, 0L);
      } else {
        throw context.getRuntime().newArgumentError("Invalid pointer value");
      }
      cache.putReference(m, value);
    }
Example #3
0
 public final void marshal(
     ThreadContext context, InvocationBuffer buffer, IRubyObject parameter) {
   if (parameter instanceof RubyString) {
     Util.checkStringSafety(context.getRuntime(), parameter);
     ByteList bl = ((RubyString) parameter).getByteList();
     buffer.putArray(
         bl.unsafeBytes(), bl.begin(), bl.length(), ArrayFlags.IN | ArrayFlags.NULTERMINATE);
   } else if (parameter.isNil()) {
     buffer.putAddress(0);
   } else {
     throw context.getRuntime().newArgumentError("Invalid string parameter");
   }
 }
Example #4
0
 public final void marshal(
     Invocation invocation, InvocationBuffer buffer, IRubyObject parameter) {
   buffer.putLong(Util.uint64Value(parameter));
 }
Example #5
0
 public final void marshal(
     ThreadContext context, InvocationBuffer buffer, IRubyObject parameter) {
   buffer.putLong(Util.uint64Value(parameter));
 }
Example #6
0
 public final void marshal(
     Invocation invocation, InvocationBuffer buffer, IRubyObject parameter) {
   buffer.putInt((int) Util.uint32Value(parameter));
 }
Example #7
0
 public final void marshal(
     Invocation invocation, InvocationBuffer buffer, IRubyObject parameter) {
   buffer.putShort(Util.int16Value(parameter));
 }
Example #8
0
 public final void marshal(
     ThreadContext context, InvocationBuffer buffer, IRubyObject parameter) {
   buffer.putShort(Util.int16Value(parameter));
 }
Example #9
0
 public final IRubyObject invoke(Ruby runtime, Function function, HeapInvocationBuffer args) {
   return Util.newSigned32(runtime, invoker.invokeInt(function, args));
 }
Example #10
0
 public final IRubyObject invoke(
     ThreadContext context, Function function, HeapInvocationBuffer args) {
   return Util.newUnsigned64(context.getRuntime(), invoker.invokeLong(function, args));
 }
Example #11
0
 public final IRubyObject invoke(
     ThreadContext context, Function function, HeapInvocationBuffer args) {
   return Util.newSigned16(context.getRuntime(), (short) invoker.invokeInt(function, args));
 }
Example #12
0
 public final int intValue(ThreadContext context, IRubyObject obj) {
   return (int) Util.uint32Value(obj);
 }
Example #13
0
 public final int intValue(ThreadContext context, IRubyObject obj) {
   return Util.int16Value(obj);
 }
Example #14
0
 public final IRubyObject fromNative(ThreadContext context, int value) {
   return Util.newUnsigned32(context.getRuntime(), value);
 }
Example #15
0
 public final IRubyObject fromNative(ThreadContext context, int value) {
   return Util.newSigned16(context.getRuntime(), (short) value);
 }
Example #16
0
 public final IRubyObject invoke(Ruby runtime, Function function, HeapInvocationBuffer args) {
   return Util.newUnsigned64(runtime, invoker.invokeLong(function, args));
 }
Example #17
0
 private final void checkBounds(long off, long len) {
   Util.checkBounds(runtime, size, off, len);
 }
Example #18
0
 public void marshal(Invocation invocation, InvocationBuffer buffer, IRubyObject parameter) {
   buffer.putByte(Util.int8Value(parameter));
 }
Example #19
0
 private final long getOffset(IRubyObject index) {
   return getOffset(Util.int32Value(index));
 }
Example #20
0
 @JRubyMethod(name = {"error=", "last_error="})
 public IRubyObject getLastError(ThreadContext context, IRubyObject error) {
   setLastError(Util.int32Value(error));
   return context.getRuntime().getNil();
 }