Package lukyt

Class LuaObject

java.lang.Object
lukyt.LuaObject

public class LuaObject
extends java.lang.Object
Java class wrapping a Lua value. Can be used for Lua interoptability.
  • Field Summary

    Fields 
    Modifier and Type Field Description
    static LuaObject _ENV
    The LuaObject corresponding to the Lua variable _ENV
    static LuaObject _G
    The LuaObject corresponding to the Lua variable _G
    static LuaObject FALSE
    The LuaObject corresponding to the Lua primitive false
    protected long handle  
    static LuaObject NIL
    The LuaObject corresponding to Lua nil
    static LuaObject TRUE
    The LuaObject corresponding to the Lua primitive true
  • Constructor Summary

    Constructors 
    Constructor Description
    LuaObject()
    Init the LuaObject with a new nil value.
    LuaObject​(java.lang.String path)
    Use path path to find the LuaObject through _ENV.
    Childrens are separated by dots (.)

    Example: os.time returns the child named time of the os table which is a child of the _ENV table.
  • Method Summary

    Modifier and Type Method Description
    boolean asBoolean()
    Returns this LuaObject as a primitive boolean value.
    double asDouble()
    Returns this LuaObject as a primitive double value.
    If the Lua object isn't a number, this returns 0.
    long asLong()
    Returns this LuaObject as a primitive long value.
    If the Lua object isn't a number, this returns 0.
    The number is floored if necessary.
    java.lang.Object asObject()
    Returns this LuaObject as either a String, a Double, a Boolean or a null value.
    java.lang.String asString()
    Returns this LuaObject as a String object.
    If the Lua object isn't a string, this returns the result of tostring().
    boolean equals​(java.lang.Object o)  
    LuaObject execute()
    Execute this Lua Object as a function no arguments and return the first return value.
    LuaObject execute​(LuaObject arg)
    Execute this Lua Object as a function with one argument arg and return the first return value.
    LuaObject execute​(LuaObject[] args)
    Execute this Lua Object as a function with arguments args and return the first return value.
    LuaObject[] executeAll​(LuaObject[] args)
    Execute this Lua Object as a function with arguments args and return all of its results as LuaObjects.
    LuaObject executeChild​(java.lang.String key)
    Execute the child with key key as a function with zero arguments.
    LuaObject executeChild​(java.lang.String key, LuaObject[] args)
    Execute the child with key key as a function
    static LuaObject from​(java.lang.Object o)
    Auto-cast object o to a LuaObject.
    static LuaObject fromBoolean​(boolean b)  
    static LuaObject fromDouble​(double d)
    Cast a primitive double value to a LuaObject.
    static LuaObject fromLong​(long l)
    Cast a primitive long value to a LuaObject.
    static LuaObject fromString​(java.lang.String str)
    Cast a String to a LuaObject.
    LuaObject get​(java.lang.String key)
    Returns the LuaObject corresponding to the child of the lua variable corresponding to this LuaObject.
    java.lang.String getType()  
    boolean isArray()
    Returns true if this object only contains numerical keys.
    boolean isNil()
    Returns true if getType() returns "nil".
    boolean isTable()
    Returns true if getType() returns "table".
    java.util.List<java.lang.String> keys()
    Returns the list of all the keys used in this Lua table object.
    void set​(java.lang.String key, LuaObject lua)
    Set the child with this key to the lua variable corresponding to the given LuaObject.
    java.util.List<LuaObject> values()
    Returns the list of all the values in this Lua table object.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • LuaObject

      public LuaObject()
      Init the LuaObject with a new nil value.
    • LuaObject

      public LuaObject​(java.lang.String path)
      Use path path to find the LuaObject through _ENV.
      Childrens are separated by dots (.)

      Example: os.time returns the child named time of the os table which is a child of the _ENV table.
  • Method Details

    • fromString

      public static LuaObject fromString​(java.lang.String str)
      Cast a String to a LuaObject.
    • fromLong

      public static LuaObject fromLong​(long l)
      Cast a primitive long value to a LuaObject.
    • fromDouble

      public static LuaObject fromDouble​(double d)
      Cast a primitive double value to a LuaObject.
    • fromBoolean

      public static LuaObject fromBoolean​(boolean b)
    • from

      public static LuaObject from​(java.lang.Object o)
      Auto-cast object o to a LuaObject. Object must be one of:
      • Double
      • Long
      • Integer
      • String
    • executeAll

      public LuaObject[] executeAll​(LuaObject[] args)
      Execute this Lua Object as a function with arguments args and return all of its results as LuaObjects.
    • getType

      public java.lang.String getType()
    • asDouble

      public double asDouble()
      Returns this LuaObject as a primitive double value.
      If the Lua object isn't a number, this returns 0.
    • asLong

      public long asLong()
      Returns this LuaObject as a primitive long value.
      If the Lua object isn't a number, this returns 0.
      The number is floored if necessary.
    • asBoolean

      public boolean asBoolean()
      Returns this LuaObject as a primitive boolean value.
    • asString

      public java.lang.String asString()
      Returns this LuaObject as a String object.
      If the Lua object isn't a string, this returns the result of tostring().
    • asObject

      public java.lang.Object asObject()
      Returns this LuaObject as either a String, a Double, a Boolean or a null value.
    • set

      public void set​(java.lang.String key, LuaObject lua)
      Set the child with this key to the lua variable corresponding to the given LuaObject.
      Parameters:
      key - the key of the LuaObject to set
    • get

      public LuaObject get​(java.lang.String key)
      Returns the LuaObject corresponding to the child of the lua variable corresponding to this LuaObject.
      Parameters:
      key - the key of the LuaObject to get
      Returns:
      the result
      Throws:
      ChildNotFoundException - if the child is not found
    • executeChild

      public LuaObject executeChild​(java.lang.String key, LuaObject[] args)
      Execute the child with key key as a function
      Parameters:
      key - the key of the function to execute
      args - the arguments of the function to execute
      Returns:
      the first result of the function, or null if there isn't any
      Throws:
      ChildNotFoundException - if the child is not found
      java.lang.TypeNotPresentException - if the child isn't a function
    • executeChild

      public LuaObject executeChild​(java.lang.String key)
      Execute the child with key key as a function with zero arguments.
      Parameters:
      key - the key of the function to execute
      Returns:
      the first result of the function, or null if there isn't any
      Throws:
      ChildNotFoundException - if the child is not found
      java.lang.TypeNotPresentException - if the child isn't a function
    • keys

      public java.util.List<java.lang.String> keys()
      Returns the list of all the keys used in this Lua table object.
    • values

      public java.util.List<LuaObject> values()
      Returns the list of all the values in this Lua table object.
    • isArray

      public boolean isArray()
      Returns true if this object only contains numerical keys. The keys do not have to be in sequencial.
      Example: {1, 2, 3, [5]=4} -> luaObject.isArray() -> true
    • isNil

      public boolean isNil()
      Returns true if getType() returns "nil".
    • isTable

      public boolean isTable()
      Returns true if getType() returns "table".
    • execute

      public LuaObject execute()
      Execute this Lua Object as a function no arguments and return the first return value.
    • execute

      public LuaObject execute​(LuaObject arg)
      Execute this Lua Object as a function with one argument arg and return the first return value.
    • execute

      public LuaObject execute​(LuaObject[] args)
      Execute this Lua Object as a function with arguments args and return the first return value.
    • equals

      public boolean equals​(java.lang.Object o)
      Overrides:
      equals in class java.lang.Object