Example #1
0
 Field(Ruby runtime, RubyClass klass, FieldIO io) {
   this(
       runtime,
       klass,
       (Type) runtime.fastGetModule("FFI").fastGetClass("Type").fastGetConstant("VOID"),
       -1,
       io);
 }
Example #2
0
 ArrayProxy(Ruby runtime, IRubyObject ptr, long offset, Type.Array type, MemoryOp aio) {
   this(
       runtime,
       runtime.fastGetModule("FFI").fastGetClass(CLASS_NAME).fastGetClass("ArrayProxy"),
       ptr,
       offset,
       type,
       aio);
 }
Example #3
0
 CharArrayProxy(Ruby runtime, IRubyObject ptr, long offset, Type.Array type, MemoryOp aio) {
   super(
       runtime,
       runtime.fastGetModule("FFI").fastGetClass("StructLayout").fastGetClass("CharArrayProxy"),
       ptr,
       offset,
       type,
       aio);
 }
Example #4
0
 Buffer(Ruby runtime, byte[] data, int offset, int size) {
   this(
       runtime,
       runtime.fastGetModule("FFI").fastGetClass("Buffer"),
       new ArrayMemoryIO(runtime, data, offset, size),
       size,
       1,
       IN | OUT);
 }
Example #5
0
 Buffer(Ruby runtime, int size, int flags) {
   this(
       runtime,
       runtime.fastGetModule("FFI").fastGetClass("Buffer"),
       new ArrayMemoryIO(runtime, size),
       size,
       1,
       flags);
 }
Example #6
0
  /**
   * Creates a new <tt>StructLayout</tt> instance.
   *
   * @param runtime The runtime for the <tt>StructLayout</tt>.
   * @param fields The fields map for this struct.
   * @param size the total size of the struct.
   * @param alignment The minimum alignment required when allocating memory.
   */
  private StructLayout(
      Ruby runtime, RubyClass klass, Collection<IRubyObject> fields, int size, int alignment) {
    super(runtime, klass, NativeType.STRUCT, size, alignment);

    int cfCount = 0, refCount = 0;
    List<Field> fieldList = new ArrayList<Field>(fields.size());
    List<IRubyObject> names = new ArrayList<IRubyObject>(fields.size());
    List<Member> memberList = new ArrayList<Member>(fields.size());
    Map<IRubyObject, Member> memberStringMap = new HashMap<IRubyObject, Member>(fields.size());
    Map<IRubyObject, Member> memberSymbolMap =
        new IdentityHashMap<IRubyObject, Member>(fields.size() * 2);

    int index = 0;
    for (IRubyObject obj : fields) {

      if (!(obj instanceof Field)) {
        throw runtime.newTypeError(
            obj, runtime.fastGetModule("FFI").fastGetClass("StructLayout").fastGetClass("Field"));
      }

      Field f = (Field) obj;
      if (!(f.name instanceof RubySymbol)) {
        throw runtime.newTypeError("fields list contains field with invalid name");
      }

      names.add(f.name);
      fieldList.add(f);

      Member m =
          new Member(
              f,
              index,
              f.isCacheable() ? cfCount++ : -1,
              f.isValueReferenceNeeded() ? refCount++ : -1);
      memberSymbolMap.put(f.name, m);

      // Allow fields to be accessed as ['name'] as well as [:name] for legacy code
      memberStringMap.put(f.name, m);
      memberStringMap.put(f.name.asString(), m);
      memberList.add(m);
    }

    this.cacheableFieldCount = cfCount;
    this.referenceFieldCount = refCount;

    // Create the ordered list of field names from the map
    this.fieldNames = Collections.unmodifiableList(new ArrayList<IRubyObject>(names));
    this.fields = Collections.unmodifiableList(fieldList);
    this.fieldStringMap = Collections.unmodifiableMap(memberStringMap);
    this.fieldSymbolMap = Collections.unmodifiableMap(memberSymbolMap);
    this.members = Collections.unmodifiableList(memberList);
  }
Example #7
0
  public static RubyClass createJavaProxy(ThreadContext context) {
    Ruby runtime = context.getRuntime();

    RubyClass javaProxy =
        runtime.defineClass(
            "JavaProxy",
            runtime.getObject(),
            new ObjectAllocator() {
              public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
                return new JavaProxy(runtime, klazz);
              }
            });

    RubyClass singleton = javaProxy.getSingletonClass();

    singleton.addReadWriteAttribute(context, "java_class");

    javaProxy.defineAnnotatedMethods(JavaProxy.class);
    javaProxy.includeModule(runtime.fastGetModule("JavaProxyMethods"));

    return javaProxy;
  }
Example #8
0
 public static RubyModule getModule(Ruby runtime) {
   return (RubyModule) runtime.fastGetModule("FFI");
 }
Example #9
0
 public static ConsoleHolder getHolder(Ruby runtime) {
   return (ConsoleHolder) (runtime.fastGetModule("Readline").dataGetStruct());
 }