Class LuaValues

java.lang.Object
dan200.computercraft.api.lua.LuaValues

public final class LuaValues extends Object
Various utility functions for operating with Lua values.
See Also:
  • Method Details

    • encode

      public static ByteBuffer encode(String string)
      Encode a Lua string into a read-only ByteBuffer.
      Parameters:
      string - The string to encode.
      Returns:
      The encoded string.
    • getNumericType

      public static String getNumericType(double value)
      Returns a more detailed representation of this number's type. If this is finite, it will just return "number", otherwise it returns whether it is infinite or NaN.
      Parameters:
      value - The value to extract the type for.
      Returns:
      This value's numeric type.
    • getType

      public static String getType(@Nullable Object value)
      Get a string representation of the given value's type.
      Parameters:
      value - The value whose type we are trying to compute.
      Returns:
      A string representation of the given value's type, in a similar format to that provided by Lua's type function.
    • badArgumentOf

      public static LuaException badArgumentOf(IArguments arguments, int index, String expected)
      Construct a "bad argument" exception, from an IArguments argument and an expected type.
      Parameters:
      arguments - The current arguments.
      index - The argument number, starting from 0.
      expected - The expected type for this argument.
      Returns:
      The constructed exception, which should be thrown immediately.
    • badArgument

      public static LuaException badArgument(int index, String expected, String actual)
      Construct a "bad argument" exception, from an expected and actual type.
      Parameters:
      index - The argument number, starting from 0.
      expected - The expected type for this argument.
      actual - The provided type for this argument.
      Returns:
      The constructed exception, which should be thrown immediately.
    • badTableItem

      public static LuaException badTableItem(int index, String expected, String actual)
      Construct a table item exception, from an expected and actual type.
      Parameters:
      index - The index into the table, starting from 1.
      expected - The expected type for this table item.
      actual - The provided type for this table item.
      Returns:
      The constructed exception, which should be thrown immediately.
    • badField

      public static LuaException badField(String key, String expected, String actual)
      Construct a field exception, from an expected and actual type.
      Parameters:
      key - The name of the field.
      expected - The expected type for this table item.
      actual - The provided type for this table item.
      Returns:
      The constructed exception, which should be thrown immediately.
    • checkFiniteNum

      public static Number checkFiniteNum(int index, Number value) throws LuaException
      Ensure a numeric argument is finite (i.e. not infinite or Double.NaN.
      Parameters:
      index - The argument index to check.
      value - The value to check.
      Returns:
      The input value.
      Throws:
      LuaException - If this is not a finite number.
    • checkFinite

      public static double checkFinite(int index, double value) throws LuaException
      Ensure a numeric argument is finite (i.e. not infinite or Double.NaN.
      Parameters:
      index - The argument index to check.
      value - The value to check.
      Returns:
      The input value.
      Throws:
      LuaException - If this is not a finite number.
    • checkEnum

      public static <T extends Enum<T>> T checkEnum(int index, Class<T> klass, String value) throws LuaException
      Ensure a string is a valid enum value.
      Type Parameters:
      T - The type of enum we are extracting.
      Parameters:
      index - The argument index to check.
      klass - The class of the enum instance.
      value - The value to extract.
      Returns:
      The parsed enum value.
      Throws:
      LuaException - If this is not a known enum value.