Util.java

  1. /***** BEGIN LICENSE BLOCK *****
  2.  * Version: EPL 1.0/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Eclipse Public
  5.  * License Version 1.0 (the "License"); you may not use this file
  6.  * except in compliance with the License. You may obtain a copy of
  7.  * the License at http://www.eclipse.org/legal/epl-v10.html
  8.  *
  9.  * Software distributed under the License is distributed on an "AS
  10.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  11.  * implied. See the License for the specific language governing
  12.  * rights and limitations under the License.
  13.  *
  14.  * Copyright (C) 2008 JRuby project
  15.  *
  16.  * Alternatively, the contents of this file may be used under the terms of
  17.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  18.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  19.  * in which case the provisions of the GPL or the LGPL are applicable instead
  20.  * of those above. If you wish to allow use of your version of this file only
  21.  * under the terms of either the GPL or the LGPL, and not to allow others to
  22.  * use your version of this file under the terms of the EPL, indicate your
  23.  * decision by deleting the provisions above and replace them with the notice
  24.  * and other provisions required by the GPL or the LGPL. If you do not delete
  25.  * the provisions above, a recipient may use your version of this file under
  26.  * the terms of any one of the EPL, the GPL or the LGPL.
  27.  ***** END LICENSE BLOCK *****/

  28. package org.jruby.ext.ffi;

  29. import java.math.BigInteger;
  30. import java.nio.ByteBuffer;
  31. import java.nio.ByteOrder;
  32. import org.jruby.Ruby;
  33. import org.jruby.RubyBignum;
  34. import org.jruby.RubyHash;
  35. import org.jruby.RubyInteger;
  36. import org.jruby.RubyNumeric;
  37. import org.jruby.RubyString;
  38. import org.jruby.RubySymbol;
  39. import org.jruby.javasupport.JavaObject;
  40. import org.jruby.runtime.ThreadContext;
  41. import org.jruby.runtime.builtin.IRubyObject;

  42. /**
  43.  *
  44.  */
  45. public final class Util {
  46.     private Util() {}
  47.     public static final byte int8Value(IRubyObject parameter) {
  48.         return (byte) longValue(parameter);
  49.     }

  50.     public static final short uint8Value(IRubyObject parameter) {
  51.         return (short) longValue(parameter);
  52.     }

  53.     public static final short int16Value(IRubyObject parameter) {
  54.         return (short) longValue(parameter);
  55.     }
  56.    
  57.     public static final int uint16Value(IRubyObject parameter) {
  58.         return (int) longValue(parameter);
  59.     }

  60.     public static final int int32Value(IRubyObject parameter) {
  61.         return (int) longValue(parameter);
  62.     }

  63.     public static final long uint32Value(IRubyObject parameter) {
  64.         return longValue(parameter);
  65.     }

  66.     public static final long int64Value(IRubyObject parameter) {
  67.         return longValue(parameter);
  68.     }

  69.     public static final long uint64Value(IRubyObject parameter) {
  70.         final long value = parameter instanceof RubyBignum
  71.                 ? ((RubyBignum) parameter).getValue().longValue()
  72.                 :longValue(parameter);
  73.         return value;
  74.     }

  75.     public static final float floatValue(IRubyObject parameter) {
  76.         return (float) RubyNumeric.num2dbl(parameter);
  77.     }

  78.     public static final double doubleValue(IRubyObject parameter) {
  79.         return RubyNumeric.num2dbl(parameter);
  80.     }

  81.     /**
  82.      * Converts characters like 'a' or 't' to an integer value
  83.      *
  84.      * @param parameter
  85.      * @return
  86.      */
  87.     public static final long longValue(IRubyObject parameter) {
  88.         return RubyNumeric.num2long(parameter);
  89.     }

  90.     public static int intValue(IRubyObject obj, RubyHash enums) {
  91.         if (obj instanceof RubyInteger) {
  92.                 return (int) ((RubyInteger) obj).getLongValue();

  93.         } else if (obj instanceof RubySymbol) {
  94.             IRubyObject value = enums.fastARef(obj);
  95.             if (value.isNil()) {
  96.                 throw obj.getRuntime().newArgumentError("invalid enum value, " + obj.inspect());
  97.             }
  98.             return (int) longValue(value);
  99.         } else {
  100.             return (int) longValue(obj);
  101.         }
  102.     }

  103.     public static final IRubyObject newSigned8(Ruby runtime, byte value) {
  104.         return runtime.newFixnum(value);
  105.     }

  106.     public static final IRubyObject newUnsigned8(Ruby runtime, byte value) {
  107.         return runtime.newFixnum(value < 0 ? (long)((value & 0x7FL) + 0x80L) : value);
  108.     }

  109.     public static final IRubyObject newSigned16(Ruby runtime, short value) {
  110.         return runtime.newFixnum(value);
  111.     }

  112.     public static final IRubyObject newUnsigned16(Ruby runtime, short value) {
  113.         return runtime.newFixnum(value < 0 ? (long)((value & 0x7FFFL) + 0x8000L) : value);
  114.     }

  115.     public static final IRubyObject newSigned32(Ruby runtime, int value) {
  116.         return runtime.newFixnum(value);
  117.     }

  118.     public static final IRubyObject newUnsigned32(Ruby runtime, int value) {
  119.         return runtime.newFixnum(value < 0 ? (long)((value & 0x7FFFFFFFL) + 0x80000000L) : value);
  120.     }

  121.     public static final IRubyObject newSigned64(Ruby runtime, long value) {
  122.         return runtime.newFixnum(value);
  123.     }

  124.     private static final BigInteger UINT64_BASE = BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE);
  125.     public static final IRubyObject newUnsigned64(Ruby runtime, long value) {
  126.         return value < 0
  127.                     ? RubyBignum.newBignum(runtime, BigInteger.valueOf(value & 0x7fffffffffffffffL).add(UINT64_BASE))
  128.                     : runtime.newFixnum(value);
  129.     }

  130.     @Deprecated
  131.     public static final <T> T convertParameter(IRubyObject parameter, Class<T> paramClass) {
  132.         return paramClass.cast(parameter instanceof JavaObject
  133.             ? ((JavaObject) parameter).getValue()
  134.             : parameter.toJava(paramClass));
  135.     }

  136.     public static final ByteBuffer slice(ByteBuffer buf, int offset) {
  137.         ByteBuffer tmp = buf.duplicate();
  138.         tmp.position((int) offset);
  139.         return tmp.slice();
  140.     }

  141.     public static final void checkBounds(Ruby runtime, long size, long off, long len) {
  142.         if ((off | len | (off + len) | (size - (off + len))) < 0) {
  143.             throw runtime.newIndexError("Memory access offset="
  144.                     + off + " size=" + len + " is out of bounds");
  145.         }
  146.     }

  147.     public static Type findType(ThreadContext context, IRubyObject name) {
  148.         return context.runtime.getFFI().getTypeResolver().findType(context.runtime, name);
  149.     }

  150.     public static Type findType(ThreadContext context, IRubyObject name, IRubyObject typeMap) {
  151.         return context.runtime.getFFI().getTypeResolver().findType(context.runtime, name, typeMap);
  152.     }

  153.     public static ByteOrder parseByteOrder(Ruby runtime, IRubyObject byte_order) {
  154.         if (byte_order instanceof RubySymbol || byte_order instanceof RubyString) {
  155.             String orderName = byte_order.asJavaString();
  156.             if ("network".equals(orderName) || "big".equals(orderName)) {
  157.                 return ByteOrder.BIG_ENDIAN;

  158.             } else if ("little".equals(orderName)) {
  159.                 return ByteOrder.LITTLE_ENDIAN;
  160.            
  161.             } else {
  162.                 return ByteOrder.nativeOrder();
  163.             }

  164.         } else {
  165.             throw runtime.newTypeError(byte_order, runtime.getSymbol());
  166.         }
  167.     }

  168.     public static int roundUpToPowerOfTwo(int v) {
  169.         if (v < 1) return 1;
  170.         v--;
  171.         v |= v >> 1;
  172.         v |= v >> 2;
  173.         v |= v >> 4;
  174.         v |= v >> 8;
  175.         v |= v >> 16;

  176.         return v + 1;
  177.     }
  178. }