Class OsLib

All Implemented Interfaces:
KryoSerializable
Direct Known Subclasses:
JseOsLib

public class OsLib extends TwoArgFunction
Subclass of LibFunction which implements the standard lua os library.

It is a usable base with simplified stub functions for library functions that cannot be implemented uniformly on Jse and Jme.

This can be installed as-is on either platform, or extended and refined to be used in a complete Jse implementation.

Because the nature of the os library is to encapsulate os-specific features, the behavior of these functions varies considerably from their counterparts in the C platform.

The following functions have limited implementations of features that are not supported well on Jme:

  • execute()
  • remove()
  • rename()
  • tmpname()

Typically, this library is included as part of a call to either org.luaj.vm2.lib.jse.JsePlatform#standardGlobals() or org.luaj.vm2.lib.jme.JmePlatform#standardGlobals()

 
 Globals globals = JsePlatform.standardGlobals();
 System.out.println( globals.get("os").get("time").call() );
  
In this example the platform-specific org.luaj.vm2.lib.jse.JseOsLib library will be loaded, which will include the base functionality provided by this class.

To instantiate and use it directly, link it into your globals table via LuaValue.load(LuaValue) using code such as:

 
 Globals globals = new Globals();
 globals.load(new JseBaseLib());
 globals.load(new PackageLib());
 globals.load(new OsLib());
 System.out.println( globals.get("os").get("time").call() );
  

See Also:
  • Field Details

  • Constructor Details

    • OsLib

      public OsLib()
      Create and OsLib instance.
  • Method Details

    • write

      public void write(Kryo kryo, Output output)
      Specified by:
      write in interface KryoSerializable
      Overrides:
      write in class LibFunction
    • read

      public void read(Kryo kryo, Input input)
      Specified by:
      read in interface KryoSerializable
      Overrides:
      read in class LibFunction
    • call

      public LuaValue call(LuaValue modname, LuaValue env)
      Perform one-time initialization on the library by creating a table containing the library functions, adding that table to the supplied environment, adding the table to package.loaded, and returning table as the return value.
      Specified by:
      call in class TwoArgFunction
      Parameters:
      modname - the module name supplied if this is loaded via 'require'.
      env - the environment to load into, typically a Globals instance.
      Returns:
      First return value (this(arg1,arg2)), or LuaValue.NIL if there were none.
      See Also:
    • date

      public String date(String format, double time)
      If the time argument is present, this is the time to be formatted (see the os.time function for a description of this value). Otherwise, date formats the current time. Date returns the date as a string, formatted according to the same rules as ANSII strftime, but without support for %g, %G, or %V. When called without arguments, date returns a reasonable date and time representation that depends on the host system and on the current locale (that is, os.date() is equivalent to os.date("%c")).
      Parameters:
      format -
      time - time since epoch, or -1 if not supplied
      Returns:
      a LString or a LTable containing date and time, formatted according to the given string format.