Ejemplo n.º 1
0
    public void put(
        ThreadContext context,
        StructLayout.Storage cache,
        Member m,
        IRubyObject ptr,
        IRubyObject value) {

      if (isCharArray() && value instanceof RubyString) {
        ByteList bl = value.convertToString().getByteList();
        m.getMemoryIO(ptr)
            .putZeroTerminatedByteArray(
                m.offset,
                bl.getUnsafeBytes(),
                bl.begin(),
                Math.min(bl.length(), arrayType.length() - 1));

      } else if (false) {
        RubyArray ary = value.convertToArray();
        int count = ary.size();
        if (count > arrayType.length()) {
          throw context.getRuntime().newIndexError("array too big");
        }
        AbstractMemory memory = (AbstractMemory) ptr;

        // Clear any elements that will not be filled by the array
        if (count < arrayType.length()) {
          memory
              .getMemoryIO()
              .setMemory(
                  m.offset + (count * arrayType.getComponentType().getNativeSize()),
                  (arrayType.length() - count) * arrayType.getComponentType().getNativeSize(),
                  (byte) 0);
        }

        for (int i = 0; i < count; ++i) {
          op.put(
              context.getRuntime(),
              memory,
              m.offset + (i * arrayType.getComponentType().getNativeSize()),
              ary.entry(i));
        }
      } else {
        throw context.getRuntime().newNotImplementedError("cannot set array field");
      }
    }
Ejemplo n.º 2
0
  /**
   * Registers FFI ruby classes/modules
   *
   * @param module the module to register the classes under
   */
  public void init(Ruby runtime, RubyModule ffi) {
    synchronized (ffi) {
      if (ffi.getClass("Type") == null) {
        Type.createTypeClass(runtime, ffi);
      }
      DataConverter.createDataConverterModule(runtime, ffi);

      if (ffi.getClass(AbstractMemory.ABSTRACT_MEMORY_RUBY_CLASS) == null) {
        AbstractMemory.createAbstractMemoryClass(runtime, ffi);
      }
      if (ffi.getClass("Buffer") == null) {
        Buffer.createBufferClass(runtime, ffi);
      }
      if (ffi.getClass("Pointer") == null) {
        Pointer.createPointerClass(runtime, ffi);
      }
      if (ffi.getClass("AutoPointer") == null) {
        AutoPointer.createAutoPointerClass(runtime, ffi);
      }
      if (ffi.getClass("MemoryPointer") == null) {
        MemoryPointer.createMemoryPointerClass(runtime, ffi);
      }
      if (ffi.getClass("Struct") == null) {
        Struct.createStructClass(runtime, ffi);
      }
      if (ffi.getClass(StructLayout.CLASS_NAME) == null) {
        StructLayout.createStructLayoutClass(runtime, ffi);
      }
      if (ffi.getClass("StructByValue") == null) {
        StructByValue.createStructByValueClass(runtime, ffi);
      }
      if (ffi.getClass(AbstractInvoker.CLASS_NAME) == null) {
        AbstractInvoker.createAbstractInvokerClass(runtime, ffi);
      }
      if (ffi.getClass(CallbackInfo.CLASS_NAME) == null) {
        CallbackInfo.createCallbackInfoClass(runtime, ffi);
      }
      if (ffi.getClass("Enum") == null) {
        Enum.createEnumClass(runtime, ffi);
      }
      if (ffi.getClass("Type").getClass("Mapped") == null) {
        MappedType.createConverterTypeClass(runtime, ffi);
      }
      if (ffi.getClass(FileDescriptorIO.CLASS_NAME) == null) {
        FileDescriptorIO.createFileDescriptorIOClass(runtime, ffi);
      }

      ffi.setConstant("TypeDefs", RubyHash.newHash(runtime));

      Platform.createPlatformModule(runtime, ffi);
      IOModule.createIOModule(runtime, ffi);

      StructByReference.createStructByReferenceClass(runtime, ffi);
    }
  }