Class Varargs

java.lang.Object
com.prineside.luaj.Varargs
Direct Known Subclasses:
LuaTable.IntKeyEntry, LuaTable.NormalEntry, LuaTable.NumberValueEntry, LuaValue, TailcallVarargs, Varargs.ArrayVarargs

public abstract class Varargs extends Object
Class to encapsulate varargs values, either as part of a variable argument list, or multiple return values. To construct varargs, use one of the static methods such as LuaValue.varargsOf(LuaValue,LuaValue) Any LuaValue can be used as a stand-in for Varargs, for both calls and return values. When doing so, nargs() will return 1 and arg1() or arg(1) will return this. This simplifies the case when calling or implementing varargs functions with only 1 argument or 1 return value. Varargs can also be derived from other varargs by appending to the front with a call such as LuaValue.varargsOf(LuaValue,Varargs) or by taking a portion of the args using Varargs.subargs(int start)
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    Varargs implemenation backed by an array of LuaValues
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    abstract LuaValue
    arg(int i)
    Get the n-th argument value (1-based).
    abstract LuaValue
    Get the first argument in the list.
    void
    argcheck(boolean test, int i, String msg)
    Performs test on argument i as a LuaValue when a user-supplied assertion passes, or throw an error.
    boolean
    checkboolean(int i)
    Return argument i as a boolean value, or throw an error if any other type.
    checkclosure(int i)
    Return argument i as a closure, or throw an error if any other type.
    double
    checkdouble(int i)
    Return argument i as a double, or throw an error if it cannot be converted to one.
    Return argument i as a function, or throw an error if an incompatible type.
    int
    checkint(int i)
    Return argument i as a java int value, or throw an error if it cannot be converted to one.
    checkjstring(int i)
    Return argument i as a java String if a string or number, or throw an error if any other type
    long
    checklong(int i)
    Return argument i as a java long value, or throw an error if it cannot be converted to one.
    checknotnil(int i)
    Return argument i as a LuaValue if it is not nil, or throw an error if it is nil.
    checknumber(int i)
    Return argument i as a LuaNumber, or throw an error if not a number or string that can be converted to a number.
    checkstring(int i)
    Return argument i as a LuaString if a string or number, or throw an error if any other type
    checktable(int i)
    Return argument i as a LuaTable if a lua table, or throw an error if any other type.
    checkthread(int i)
    Return argument i as a LuaThread if a lua thread, or throw an error if any other type.
    Return argument i as a java Object if a userdata, or throw an error if any other type.
    checkuserdata(int i, Class c)
    Return argument i as a java Object if it is a userdata whose instance Class c or a subclass, or throw an error if any other type.
    checkvalue(int i)
    Return argument i as a LuaValue if it exists, or throw an error.
    Return Varargs that cannot be using a shared array for the storage, and is flattened.
    Evaluate any pending tail call and return result.
    boolean
    isfunction(int i)
    Tests if argument i is a function.
    boolean
    isnil(int i)
    Tests if argument i is nil.
    boolean
    isnoneornil(int i)
    Return true if there is no argument or nil at argument i.
    boolean
    isnumber(int i)
    Tests if argument i is a number.
    boolean
    isstring(int i)
    Tests if argument i is a string.
    boolean
    istable(int i)
    Tests if argument i is a table.
    boolean
    Return true if this is a TailcallVarargs
    boolean
    isthread(int i)
    Tests if argument i is a thread.
    boolean
    isuserdata(int i)
    Tests if argument i is a userdata.
    boolean
    isvalue(int i)
    Tests if a value exists at argument i.
    abstract int
    Get the number of arguments, or 0 if there are none.
    boolean
    optboolean(int i, boolean defval)
    Return argument i as a boolean value, defval if nil, or throw a LuaError if any other type.
    optclosure(int i, LuaClosure defval)
    Return argument i as a closure, defval if nil, or throw a LuaError if any other type.
    double
    optdouble(int i, double defval)
    Return argument i as a double, defval if nil, or throw a LuaError if it cannot be converted to one.
    optfunction(int i, LuaFunction defval)
    Return argument i as a function, defval if nil, or throw a LuaError if an incompatible type.
    int
    optint(int i, int defval)
    Return argument i as a java int value, discarding any fractional part, defval if nil, or throw a LuaError if not a number.
    optjstring(int i, String defval)
    Return argument i as a java String if a string or number, defval if nil, or throw a LuaError if any other type
    long
    optlong(int i, long defval)
    Return argument i as a java long value, discarding any fractional part, defval if nil, or throw a LuaError if not a number.
    optnumber(int i, LuaNumber defval)
    Return argument i as a LuaNumber, defval if nil, or throw a LuaError if not a number or string that can be converted to a number.
    optstring(int i, LuaString defval)
    Return argument i as a LuaString if a string or number, defval if nil, or throw a LuaError if any other type
    opttable(int i, LuaTable defval)
    Return argument i as a LuaTable if a lua table, defval if nil, or throw a LuaError if any other type.
    optthread(int i, LuaThread defval)
    Return argument i as a LuaThread if a lua thread, defval if nil, or throw a LuaError if any other type.
    optuserdata(int i, Class c, Object defval)
    Return argument i as a java Object if it is a userdata whose instance Class c or a subclass, defval if nil, or throw a LuaError if any other type.
    optuserdata(int i, Object defval)
    Return argument i as a java Object if a userdata, defval if nil, or throw a LuaError if any other type.
    optvalue(int i, LuaValue defval)
    Return argument i as a LuaValue if it exists, or defval.
    abstract Varargs
    subargs(int start)
    Create a Varargs instance containing arguments starting at index start
    boolean
    toboolean(int i)
    Convert argument i to java boolean based on lua rules for boolean evaluation.
    byte
    tobyte(int i)
    Return argument i as a java byte value, discarding any fractional part and truncating, or 0 if not a number.
    char
    tochar(int i)
    Return argument i as a java char value, discarding any fractional part and truncating, or 0 if not a number.
    double
    todouble(int i)
    Return argument i as a java double value or 0 if not a number.
    float
    tofloat(int i)
    Return argument i as a java float value, discarding excess fractional part and truncating, or 0 if not a number.
    int
    toint(int i)
    Return argument i as a java int value, discarding any fractional part and truncating, or 0 if not a number.
    Convert the list of varargs values to a human readable java String.
    tojstring(int i)
    Return argument i as a java String based on the type of the argument.
    long
    tolong(int i)
    Return argument i as a java long value, discarding any fractional part and truncating, or 0 if not a number.
    short
    toshort(int i)
    Return argument i as a java short value, discarding any fractional part and truncating, or 0 if not a number.
    Convert the value or values to a java String using Varargs.tojstring()
    touserdata(int i)
    Return argument i as a java Object if a userdata, or null.
    <T> T
    touserdata(int i, Class<T> c)
    Return argument i as a java Object if it is a userdata whose instance Class c or a subclass, or null.
    int
    type(int i)
    Gets the type of argument i

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Varargs

      public Varargs()
  • Method Details

    • arg

      public abstract LuaValue arg(int i)
      Get the n-th argument value (1-based).
      Parameters:
      i - the index of the argument to get, 1 is the first argument
      Returns:
      Value at position i, or LuaValue.NIL if there is none.
      See Also:
    • narg

      public abstract int narg()
      Get the number of arguments, or 0 if there are none.
      Returns:
      number of arguments.
    • arg1

      public abstract LuaValue arg1()
      Get the first argument in the list.
      Returns:
      LuaValue which is first in the list, or LuaValue.NIL if there are no values.
      See Also:
    • eval

      public Varargs eval()
      Evaluate any pending tail call and return result.
      Returns:
      the evaluated tail call result
    • isTailcall

      public boolean isTailcall()
      Return true if this is a TailcallVarargs
      Returns:
      true if a tail call, false otherwise
    • type

      public int type(int i)
      Gets the type of argument i
      Parameters:
      i - the index of the argument to convert, 1 is the first argument
      Returns:
      int value corresponding to one of the LuaValue integer type values
      See Also:
    • isnil

      public boolean isnil(int i)
      Tests if argument i is nil.
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      true if the argument is nil or does not exist, false otherwise
      See Also:
    • isfunction

      public boolean isfunction(int i)
      Tests if argument i is a function.
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      true if the argument exists and is a function or closure, false otherwise
      See Also:
    • isnumber

      public boolean isnumber(int i)
      Tests if argument i is a number. Since anywhere a number is required, a string can be used that is a number, this will return true for both numbers and strings that can be interpreted as numbers.
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      true if the argument exists and is a number or string that can be interpreted as a number, false otherwise
      See Also:
    • isstring

      public boolean isstring(int i)
      Tests if argument i is a string. Since all lua numbers can be used where strings are used, this will return true for both strings and numbers.
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      true if the argument exists and is a string or number, false otherwise
      See Also:
    • istable

      public boolean istable(int i)
      Tests if argument i is a table.
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      true if the argument exists and is a lua table, false otherwise
      See Also:
    • isthread

      public boolean isthread(int i)
      Tests if argument i is a thread.
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      true if the argument exists and is a lua thread, false otherwise
      See Also:
    • isuserdata

      public boolean isuserdata(int i)
      Tests if argument i is a userdata.
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      true if the argument exists and is a userdata, false otherwise
      See Also:
    • isvalue

      public boolean isvalue(int i)
      Tests if a value exists at argument i.
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      true if the argument exists, false otherwise
    • optboolean

      public boolean optboolean(int i, boolean defval)
      Return argument i as a boolean value, defval if nil, or throw a LuaError if any other type.
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      true if argument i is boolean true, false if it is false, or defval if not supplied or nil
      Throws:
      LuaError - if the argument is not a lua boolean
    • optclosure

      public LuaClosure optclosure(int i, LuaClosure defval)
      Return argument i as a closure, defval if nil, or throw a LuaError if any other type.
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      LuaClosure if argument i is a closure, or defval if not supplied or nil
      Throws:
      LuaError - if the argument is not a lua closure
    • optdouble

      public double optdouble(int i, double defval)
      Return argument i as a double, defval if nil, or throw a LuaError if it cannot be converted to one.
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      java double value if argument i is a number or string that converts to a number, or defval if not supplied or nil
      Throws:
      LuaError - if the argument is not a number
    • optfunction

      public LuaFunction optfunction(int i, LuaFunction defval)
      Return argument i as a function, defval if nil, or throw a LuaError if an incompatible type.
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      LuaValue that can be called if argument i is lua function or closure, or defval if not supplied or nil
      Throws:
      LuaError - if the argument is not a lua function or closure
    • optint

      public int optint(int i, int defval)
      Return argument i as a java int value, discarding any fractional part, defval if nil, or throw a LuaError if not a number.
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      int value with fraction discarded and truncated if necessary if argument i is number, or defval if not supplied or nil
      Throws:
      LuaError - if the argument is not a number
    • optlong

      public long optlong(int i, long defval)
      Return argument i as a java long value, discarding any fractional part, defval if nil, or throw a LuaError if not a number.
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      long value with fraction discarded and truncated if necessary if argument i is number, or defval if not supplied or nil
      Throws:
      LuaError - if the argument is not a number
    • optnumber

      public LuaNumber optnumber(int i, LuaNumber defval)
      Return argument i as a LuaNumber, defval if nil, or throw a LuaError if not a number or string that can be converted to a number.
      Parameters:
      i - the index of the argument to test, 1 is the first argument, or defval if not supplied or nil
      Returns:
      LuaNumber if argument i is number or can be converted to a number
      Throws:
      LuaError - if the argument is not a number
    • optjstring

      public String optjstring(int i, String defval)
      Return argument i as a java String if a string or number, defval if nil, or throw a LuaError if any other type
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      String value if argument i is a string or number, or defval if not supplied or nil
      Throws:
      LuaError - if the argument is not a string or number
    • optstring

      public LuaString optstring(int i, LuaString defval)
      Return argument i as a LuaString if a string or number, defval if nil, or throw a LuaError if any other type
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      LuaString value if argument i is a string or number, or defval if not supplied or nil
      Throws:
      LuaError - if the argument is not a string or number
    • opttable

      public LuaTable opttable(int i, LuaTable defval)
      Return argument i as a LuaTable if a lua table, defval if nil, or throw a LuaError if any other type.
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      LuaTable value if a table, or defval if not supplied or nil
      Throws:
      LuaError - if the argument is not a lua table
    • optthread

      public LuaThread optthread(int i, LuaThread defval)
      Return argument i as a LuaThread if a lua thread, defval if nil, or throw a LuaError if any other type.
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      LuaThread value if a thread, or defval if not supplied or nil
      Throws:
      LuaError - if the argument is not a lua thread
    • optuserdata

      public Object optuserdata(int i, Object defval)
      Return argument i as a java Object if a userdata, defval if nil, or throw a LuaError if any other type.
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      java Object value if argument i is a userdata, or defval if not supplied or nil
      Throws:
      LuaError - if the argument is not a userdata
    • optuserdata

      public Object optuserdata(int i, Class c, Object defval)
      Return argument i as a java Object if it is a userdata whose instance Class c or a subclass, defval if nil, or throw a LuaError if any other type.
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      c - the class to which the userdata instance must be assignable
      Returns:
      java Object value if argument i is a userdata whose instance Class c or a subclass, or defval if not supplied or nil
      Throws:
      LuaError - if the argument is not a userdata or from whose instance c is not assignable
    • optvalue

      public LuaValue optvalue(int i, LuaValue defval)
      Return argument i as a LuaValue if it exists, or defval.
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      LuaValue value if the argument exists, defval if not
      Throws:
      LuaError - if the argument does not exist.
    • checkboolean

      public boolean checkboolean(int i)
      Return argument i as a boolean value, or throw an error if any other type.
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      true if argument i is boolean true, false if it is false
      Throws:
      LuaError - if the argument is not a lua boolean
    • checkclosure

      public LuaClosure checkclosure(int i)
      Return argument i as a closure, or throw an error if any other type.
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      LuaClosure if argument i is a closure.
      Throws:
      LuaError - if the argument is not a lua closure
    • checkdouble

      public double checkdouble(int i)
      Return argument i as a double, or throw an error if it cannot be converted to one.
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      java double value if argument i is a number or string that converts to a number
      Throws:
      LuaError - if the argument is not a number
    • checkfunction

      public LuaFunction checkfunction(int i)
      Return argument i as a function, or throw an error if an incompatible type.
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      LuaValue that can be called if argument i is lua function or closure
      Throws:
      LuaError - if the argument is not a lua function or closure
    • checkint

      public int checkint(int i)
      Return argument i as a java int value, or throw an error if it cannot be converted to one.
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      int value if argument i is a number or string that converts to a number
      Throws:
      LuaError - if the argument cannot be represented by a java int value
    • checklong

      public long checklong(int i)
      Return argument i as a java long value, or throw an error if it cannot be converted to one.
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      long value if argument i is a number or string that converts to a number
      Throws:
      LuaError - if the argument cannot be represented by a java long value
    • checknumber

      public LuaNumber checknumber(int i)
      Return argument i as a LuaNumber, or throw an error if not a number or string that can be converted to a number.
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      LuaNumber if argument i is number or can be converted to a number
      Throws:
      LuaError - if the argument is not a number
    • checkjstring

      public String checkjstring(int i)
      Return argument i as a java String if a string or number, or throw an error if any other type
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      String value if argument i is a string or number
      Throws:
      LuaError - if the argument is not a string or number
    • checkstring

      public LuaString checkstring(int i)
      Return argument i as a LuaString if a string or number, or throw an error if any other type
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      LuaString value if argument i is a string or number
      Throws:
      LuaError - if the argument is not a string or number
    • checktable

      public LuaTable checktable(int i)
      Return argument i as a LuaTable if a lua table, or throw an error if any other type.
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      LuaTable value if a table
      Throws:
      LuaError - if the argument is not a lua table
    • checkthread

      public LuaThread checkthread(int i)
      Return argument i as a LuaThread if a lua thread, or throw an error if any other type.
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      LuaThread value if a thread
      Throws:
      LuaError - if the argument is not a lua thread
    • checkuserdata

      public Object checkuserdata(int i)
      Return argument i as a java Object if a userdata, or throw an error if any other type.
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      java Object value if argument i is a userdata
      Throws:
      LuaError - if the argument is not a userdata
    • checkuserdata

      public Object checkuserdata(int i, Class c)
      Return argument i as a java Object if it is a userdata whose instance Class c or a subclass, or throw an error if any other type.
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      c - the class to which the userdata instance must be assignable
      Returns:
      java Object value if argument i is a userdata whose instance Class c or a subclass
      Throws:
      LuaError - if the argument is not a userdata or from whose instance c is not assignable
    • checkvalue

      public LuaValue checkvalue(int i)
      Return argument i as a LuaValue if it exists, or throw an error.
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      LuaValue value if the argument exists
      Throws:
      LuaError - if the argument does not exist.
    • checknotnil

      public LuaValue checknotnil(int i)
      Return argument i as a LuaValue if it is not nil, or throw an error if it is nil.
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      LuaValue value if the argument is not nil
      Throws:
      LuaError - if the argument doesn't exist or evaluates to nil.
    • argcheck

      public void argcheck(boolean test, int i, String msg)
      Performs test on argument i as a LuaValue when a user-supplied assertion passes, or throw an error. Returns normally if the value of test is true, otherwise throws and argument error with the supplied message, msg.
      Parameters:
      test - user supplied assertion to test against
      i - the index to report in any error message
      msg - the error message to use when the test fails
      Throws:
      LuaError - if the the value of test is false
    • isnoneornil

      public boolean isnoneornil(int i)
      Return true if there is no argument or nil at argument i.
      Parameters:
      i - the index of the argument to test, 1 is the first argument
      Returns:
      true if argument i contains either no argument or nil
    • toboolean

      public boolean toboolean(int i)
      Convert argument i to java boolean based on lua rules for boolean evaluation.
      Parameters:
      i - the index of the argument to convert, 1 is the first argument
      Returns:
      false if argument i is nil or false, otherwise true
    • tobyte

      public byte tobyte(int i)
      Return argument i as a java byte value, discarding any fractional part and truncating, or 0 if not a number.
      Parameters:
      i - the index of the argument to convert, 1 is the first argument
      Returns:
      byte value with fraction discarded and truncated if necessary if argument i is number, otherwise 0
    • tochar

      public char tochar(int i)
      Return argument i as a java char value, discarding any fractional part and truncating, or 0 if not a number.
      Parameters:
      i - the index of the argument to convert, 1 is the first argument
      Returns:
      char value with fraction discarded and truncated if necessary if argument i is number, otherwise 0
    • todouble

      public double todouble(int i)
      Return argument i as a java double value or 0 if not a number.
      Parameters:
      i - the index of the argument to convert, 1 is the first argument
      Returns:
      double value if argument i is number, otherwise 0
    • tofloat

      public float tofloat(int i)
      Return argument i as a java float value, discarding excess fractional part and truncating, or 0 if not a number.
      Parameters:
      i - the index of the argument to convert, 1 is the first argument
      Returns:
      float value with excess fraction discarded and truncated if necessary if argument i is number, otherwise 0
    • toint

      public int toint(int i)
      Return argument i as a java int value, discarding any fractional part and truncating, or 0 if not a number.
      Parameters:
      i - the index of the argument to convert, 1 is the first argument
      Returns:
      int value with fraction discarded and truncated if necessary if argument i is number, otherwise 0
    • tolong

      public long tolong(int i)
      Return argument i as a java long value, discarding any fractional part and truncating, or 0 if not a number.
      Parameters:
      i - the index of the argument to convert, 1 is the first argument
      Returns:
      long value with fraction discarded and truncated if necessary if argument i is number, otherwise 0
    • tojstring

      public String tojstring(int i)
      Return argument i as a java String based on the type of the argument.
      Parameters:
      i - the index of the argument to convert, 1 is the first argument
      Returns:
      String value representing the type
    • toshort

      public short toshort(int i)
      Return argument i as a java short value, discarding any fractional part and truncating, or 0 if not a number.
      Parameters:
      i - the index of the argument to convert, 1 is the first argument
      Returns:
      short value with fraction discarded and truncated if necessary if argument i is number, otherwise 0
    • touserdata

      public Object touserdata(int i)
      Return argument i as a java Object if a userdata, or null.
      Parameters:
      i - the index of the argument to convert, 1 is the first argument
      Returns:
      java Object value if argument i is a userdata, otherwise null
    • touserdata

      public <T> T touserdata(int i, Class<T> c)
      Return argument i as a java Object if it is a userdata whose instance Class c or a subclass, or null.
      Parameters:
      i - the index of the argument to convert, 1 is the first argument
      c - the class to which the userdata instance must be assignable
      Returns:
      java Object value if argument i is a userdata whose instance Class c or a subclass, otherwise null
    • tojstring

      public String tojstring()
      Convert the list of varargs values to a human readable java String.
      Returns:
      String value in human readable form such as {1,2}.
    • toString

      public String toString()
      Convert the value or values to a java String using Varargs.tojstring()
      Overrides:
      toString in class Object
      Returns:
      String value in human readable form.
      See Also:
    • subargs

      public abstract Varargs subargs(int start)
      Create a Varargs instance containing arguments starting at index start
      Parameters:
      start - the index from which to include arguments, where 1 is the first argument.
      Returns:
      Varargs containing argument { start, start+1, ... , narg-start-1 }
    • dealias

      public Varargs dealias()
      Return Varargs that cannot be using a shared array for the storage, and is flattened. Internal utility method not intended to be called directly from user code.
      Returns:
      Varargs containing same values, but flattened and with a new array if needed.