Class OsLib
- All Implemented Interfaces:
com.esotericsoftware.kryo.KryoSerializable
- Direct Known Subclasses:
JseOsLib
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() );
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from class com.prineside.luaj.LuaValue
LuaValue.NillableSerializer, LuaValue.None
Nested classes/interfaces inherited from class com.prineside.luaj.Varargs
Varargs.ArrayVarargs
-
Field Summary
FieldsFields inherited from class com.prineside.luaj.lib.LibFunction
name, opcode
Fields inherited from class com.prineside.luaj.LuaFunction
s_metatable
Fields inherited from class com.prineside.luaj.LuaValue
ADD, CALL, CONCAT, DIV, EMPTYSTRING, ENV, EQ, FALSE, INDEX, LE, LEN, LT, METATABLE, MINUSONE, MOD, MODE, MUL, NEWINDEX, NIL, NILLABLE_SERIALIZER, NILS, NONE, NOVALS, ONE, POW, SUB, TBOOLEAN, TFUNCTION, TINT, TLIGHTUSERDATA, TNIL, TNONE, TNUMBER, TOSTRING, TRUE, TSTRING, TTABLE, TTHREAD, TUSERDATA, TVALUE, TYPE_NAMES, UNM, ZERO
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionPerform 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.protected double
clock()
If the time argument is present, this is the time to be formatted (see the os.time function for a description of this value).protected double
difftime
(double t2, double t1) Returns the number of seconds from time t1 to time t2.protected Varargs
This function is equivalent to the C function system.protected void
exit
(int code) Calls the C function exit, with an optional code, to terminate the host program.protected String
Returns the value of the process environment variable varname, or the System property value for varname, or null if the variable is not defined in either environment.void
read
(com.esotericsoftware.kryo.Kryo kryo, com.esotericsoftware.kryo.io.Input input) protected void
Deletes the file or directory with the given name.protected void
Renames file or directory named oldname to newname.protected String
Sets the current locale of the program.protected double
Returns the current time when called without arguments, or a time representing the date and time specified by the given table.protected String
tmpname()
Returns a string with a file name that can be used for a temporary file.void
write
(com.esotericsoftware.kryo.Kryo kryo, com.esotericsoftware.kryo.io.Output output) Methods inherited from class com.prineside.luaj.lib.TwoArgFunction
call, call, call, invoke
Methods inherited from class com.prineside.luaj.lib.LibFunction
bind, bind, call, newupe, newupl, newupn, tojstring
Methods inherited from class com.prineside.luaj.LuaFunction
checkfunction, classnamestub, getmetatable, isfunction, name, optfunction, strvalue, type, typename
Methods inherited from class com.prineside.luaj.LuaValue
add, add, add, and, arg, arg1, argerror, argerror, aritherror, aritherror, arithmt, arithmtwith, assert_, buffer, call, callmt, cBool, cDouble, cFloat, checkboolean, checkclosure, checkdouble, checkglobals, checkint, checkjstring, checklong, checkmetatag, checknotnil, checknumber, checknumber, checkstring, checktable, checkthread, checkuserdata, checkuserdata, cInt, cNcDouble, cNcFloat, cNcInt, cObject, compareerror, compareerror, comparemt, concat, concat, concatmt, concatTo, concatTo, concatTo, cRegObject, cVarargs, cVarargs, cVarargs, cVarargs, cVarargs, cVarargs, cVarargs, cVarargs, div, div, div, divInto, eq, eq_b, eqmtcall, equals, error, get, get, get, gettable, gt, gt, gt, gt_b, gt_b, gt_b, gteq, gteq, gteq, gteq_b, gteq_b, gteq_b, illegal, inext, initupvalue1, invoke, invoke, invoke, invokemethod, invokemethod, invokemethod, invokemethod, isboolean, isclosure, isint, isinttype, islong, isnil, isnumber, isRegObject, isstring, istable, isthread, isuserdata, isuserdata, isvalidkey, len, lenerror, length, listOf, listOf, load, lt, lt, lt, lt_b, lt_b, lt_b, lteq, lteq, lteq, lteq_b, lteq_b, lteq_b, markAsConst, metatableOf, metatag, method, method, method, method, method, method, mod, mod, mod, modFrom, mul, mul, mul, narg, neg, neq, neq_b, next, not, onInvoke, optboolean, optclosure, optdouble, optint, optjstring, optlong, optnumber, optstring, opttable, optthread, optuserdata, optuserdata, optvalue, or, pow, pow, pow, powWith, powWith, presize, raweq, raweq, raweq, raweq, raweq, rawget, rawget, rawget, rawlen, rawset, rawset, rawset, rawset, rawset, rawset, rawset, rawsetlist, set, set, set, set, set, set, set, setmetatable, settable, strcmp, strcmp, strongvalue, sub, sub, sub, subargs, subFrom, subFrom, tableOf, tableOf, tableOf, tableOf, tableOf, tableOf, tailcallOf, testfor_b, toboolean, tobyte, tochar, todouble, tofloat, toint, tolong, tonumber, toshort, tostring, toString, touserdata, touserdata, typerror, unimplemented, userdataOf, userdataOf, valueOf, valueOf, valueOf, valueOf, valueOf, valueOf
Methods inherited from class com.prineside.luaj.Varargs
argcheck, checkboolean, checkclosure, checkdouble, checkfunction, checkint, checkjstring, checklong, checknotnil, checknumber, checkstring, checktable, checkthread, checkuserdata, checkuserdata, checkvalue, dealias, eval, isfunction, isnil, isnoneornil, isnumber, isstring, istable, isTailcall, isthread, isuserdata, isvalue, optboolean, optclosure, optdouble, optfunction, optint, optjstring, optlong, optnumber, optstring, opttable, optthread, optuserdata, optuserdata, optvalue, toboolean, tobyte, tochar, todouble, tofloat, toint, tojstring, tolong, toshort, touserdata, touserdata, type
-
Field Details
-
TMP_PREFIX
- See Also:
-
TMP_SUFFIX
- See Also:
-
globals
-
-
Constructor Details
-
OsLib
public OsLib()Create and OsLib instance.
-
-
Method Details
-
write
public void write(com.esotericsoftware.kryo.Kryo kryo, com.esotericsoftware.kryo.io.Output output) - Specified by:
write
in interfacecom.esotericsoftware.kryo.KryoSerializable
- Overrides:
write
in classLibFunction
-
read
public void read(com.esotericsoftware.kryo.Kryo kryo, com.esotericsoftware.kryo.io.Input input) - Specified by:
read
in interfacecom.esotericsoftware.kryo.KryoSerializable
- Overrides:
read
in classLibFunction
-
call
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 classTwoArgFunction
- 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))
, orLuaValue.NIL
if there were none. - See Also:
-
clock
protected double clock()- Returns:
- an approximation of the amount in seconds of CPU time used by the program. For luaj this simple returns the elapsed time since the OsLib class was loaded.
-
difftime
protected double difftime(double t2, double t1) Returns the number of seconds from time t1 to time t2. In POSIX, Windows, and some other systems, this value is exactly t2-t1.- Parameters:
t2
-t1
-- Returns:
- diffeence in time values, in seconds
-
date
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.
-
execute
This function is equivalent to the C function system. It passes command to be executed by an operating system shell. It returns a status code, which is system-dependent. If command is absent, then it returns nonzero if a shell is available and zero otherwise.- Parameters:
command
- command to pass to the system
-
exit
protected void exit(int code) Calls the C function exit, with an optional code, to terminate the host program.- Parameters:
code
-
-
getenv
Returns the value of the process environment variable varname, or the System property value for varname, or null if the variable is not defined in either environment. The default implementation, which is used by the JmePlatform, only queryies System.getProperty(). The JsePlatform overrides this behavior and returns the environment variable value using System.getenv() if it exists, or the System property value if it does not. A SecurityException may be thrown if access is not allowed for 'varname'.- Parameters:
varname
-- Returns:
- String value, or null if not defined
-
remove
Deletes the file or directory with the given name. Directories must be empty to be removed. If this function fails, it throws and IOException- Parameters:
filename
-- Throws:
IOException
- if it fails
-
rename
Renames file or directory named oldname to newname. If this function fails,it throws and IOException- Parameters:
oldname
- old file namenewname
- new file name- Throws:
IOException
- if it fails
-
setlocale
Sets the current locale of the program. locale is a string specifying a locale; category is an optional string describing which category to change: "all", "collate", "ctype", "monetary", "numeric", or "time"; the default category is "all". If locale is the empty string, the current locale is set to an implementation- defined native locale. If locale is the string "C", the current locale is set to the standard C locale. When called with null as the first argument, this function only returns the name of the current locale for the given category.- Parameters:
locale
-category
-- Returns:
- the name of the new locale, or null if the request cannot be honored.
-
time
Returns the current time when called without arguments, or a time representing the date and time specified by the given table. This table must have fields year, month, and day, and may have fields hour, min, sec, and isdst (for a description of these fields, see the os.date function).- Parameters:
table
-- Returns:
- long value for the time
-
tmpname
Returns a string with a file name that can be used for a temporary file. The file must be explicitly opened before its use and explicitly removed when no longer needed. On some systems (POSIX), this function also creates a file with that name, to avoid security risks. (Someone else might create the file with wrong permissions in the time between getting the name and creating the file.) You still have to open the file to use it and to remove it (even if you do not use it).- Returns:
- String filename to use
-