org.luaj.vm2.lib
Class PackageLib

java.lang.Object
  extended by org.luaj.vm2.Varargs
      extended by org.luaj.vm2.LuaValue
          extended by org.luaj.vm2.LuaFunction
              extended by org.luaj.vm2.lib.LibFunction
                  extended by org.luaj.vm2.lib.OneArgFunction
                      extended by org.luaj.vm2.lib.PackageLib

public class PackageLib
extends OneArgFunction

Subclass of LibFunction which implements the lua standard package and module library functions.

Typically, this library is included as part of a call to either JsePlatform#standardGlobals() or JmePlatform#standardGlobals()

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

 LuaTable _G = new LuaTable();
 LuaThread.setGlobals(_G);
 _G.load(new BaseLib());
 _G.load(new PackageLib());
 System.out.println( _G.get("require").call(LuaValue.valueOf("hyperbolic")) );
  
In practice, the first 4 lines of the above are minimal requirements to get and initialize a globals table capable of basic reqire, print, and other functions, so it is much more convenient to use the JsePlatform and JmePlatform utility classes instead.

This has been implemented to match as closely as possible the behavior in the corresponding library in C. However, the default filesystem search semantics are different and delegated to the bas library as outlined in the BaseLib and JseBaseLib documetnation.

See Also:
LibFunction, BaseLib, JseBaseLib, JsePlatform, JmePlatform, http://www.lua.org/manual/5.1/manual.html#5.3

Field Summary
static java.lang.String DEFAULT_LUA_PATH
           
static PackageLib instance
          Most recent instance of PackageLib
 LuaValue java_loader
          Loader that loads as a Java class.
 LuaTable LOADED
           
 LuaValue lua_loader
          Loader that loads as a lua script using the LUA_PATH
 LuaTable PACKAGE
           
 LuaValue preload_loader
          Loader that loads from preload table if found there
 java.io.InputStream STDIN
           
 java.io.PrintStream STDOUT
           
 
Fields inherited from class org.luaj.vm2.lib.LibFunction
name, opcode
 
Fields inherited from class org.luaj.vm2.LuaFunction
env, s_metatable
 
Fields inherited from class org.luaj.vm2.LuaValue
ADD, CALL, CONCAT, DIV, EMPTYSTRING, EQ, FALSE, INDEX, LE, LEN, LT, METATABLE, MINUSONE, MOD, MODE, MUL, NEWINDEX, NIL, 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
PackageLib()
           
 
Method Summary
 LuaValue call(LuaValue arg)
          Call this with 1 argument, including metatag processing, and return only the first return value.
static Varargs loadlib(Varargs args)
           
 Varargs module(Varargs args)
          module (name [, ...]) Creates a module.
 LuaValue require(LuaValue arg)
          require (modname) Loads the given module.
 void setIsLoaded(java.lang.String name, LuaTable value)
          Allow packages to mark themselves as loaded
 void setLuaPath(java.lang.String newLuaPath)
           
static java.lang.String toClassname(java.lang.String filename)
          Convert lua filename to valid class name
 java.lang.String tojstring()
          Convert to human readable String for any type.
 
Methods inherited from class org.luaj.vm2.lib.OneArgFunction
call, call, call, invoke
 
Methods inherited from class org.luaj.vm2.lib.LibFunction
bind, bind, newupe, newupl, newupn
 
Methods inherited from class org.luaj.vm2.LuaFunction
checkfunction, getfenv, getmetatable, isfunction, optfunction, setfenv, type, typename
 
Methods inherited from class org.luaj.vm2.LuaValue
add, add, add, and, arg, arg1, argerror, argerror, aritherror, aritherror, arithmt, arithmtwith, assert_, buffer, callmt, checkboolean, checkclosure, checkdouble, checkint, checkinteger, checkjstring, checklong, checkmetatag, checknotnil, checknumber, checknumber, checkstring, checktable, checkthread, checkuserdata, checkuserdata, checkvalidkey, compareerror, compareerror, comparemt, concat, concat, concatmt, concatTo, concatTo, concatTo, div, div, div, divInto, eq_b, eq, eqmtcall, equals, error, get, get, get, getn, gettable, gt_b, gt_b, gt_b, gt, gt, gt, gteq_b, gteq_b, gteq_b, gteq, gteq, gteq, illegal, inext, invoke, invoke, invoke, invoke, invoke, invokemethod, invokemethod, invokemethod, invokemethod, invokemethod, invokemethod, isboolean, isclosure, isint, isinttype, islong, isnil, isnumber, isstring, istable, isthread, isuserdata, isuserdata, isweaknil, len, lenerror, length, listOf, listOf, load, lt_b, lt_b, lt_b, lt, lt, lt, lteq_b, lteq_b, lteq_b, lteq, lteq, lteq, metatag, method, method, method, method, method, method, mod, mod, mod, modFrom, mul, mul, mul, narg, neg, neq_b, neq, next, not, onInvoke, optboolean, optclosure, optdouble, optint, optinteger, optjstring, optlong, optnumber, optstring, opttable, optthread, optuserdata, optuserdata, optvalue, or, pow, pow, pow, powWith, powWith, presize, raweq, raweq, raweq, raweq, raweq, rawget, rawget, rawget, rawset, rawset, rawset, rawset, rawset, rawset, rawset, rawsetlist, set, set, set, set, set, set, set, setmetatable, settable, strcmp, strcmp, strongkey, strongvalue, strvalue, sub, sub, sub, 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, varargsOf, varargsOf, varargsOf, varargsOf, varargsOf, varargsOf
 
Methods inherited from class org.luaj.vm2.Varargs
argcheck, checkboolean, checkclosure, checkdouble, checkfunction, checkint, checkinteger, checkjstring, checklong, checknotnil, checknumber, checkstring, checktable, checkthread, checkuserdata, checkuserdata, checkvalue, eval, isfunction, isnil, isnoneornil, isnumber, isstring, istable, isTailcall, isthread, isuserdata, isvalue, optboolean, optclosure, optdouble, optfunction, optint, optinteger, optjstring, optlong, optnumber, optstring, opttable, optthread, optuserdata, optuserdata, optvalue, subargs, toboolean, tobyte, tochar, todouble, tofloat, toint, tojstring, tolong, toshort, touserdata, touserdata, type
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_LUA_PATH

public static java.lang.String DEFAULT_LUA_PATH

STDIN

public java.io.InputStream STDIN

STDOUT

public java.io.PrintStream STDOUT

LOADED

public LuaTable LOADED

PACKAGE

public LuaTable PACKAGE

instance

public static PackageLib instance
Most recent instance of PackageLib


preload_loader

public LuaValue preload_loader
Loader that loads from preload table if found there


lua_loader

public LuaValue lua_loader
Loader that loads as a lua script using the LUA_PATH


java_loader

public LuaValue java_loader
Loader that loads as a Java class. Class must have public constructor and be a LuaValue

Constructor Detail

PackageLib

public PackageLib()
Method Detail

call

public LuaValue call(LuaValue arg)
Description copied from class: LuaValue
Call this with 1 argument, including metatag processing, and return only the first return value.

If this is a LuaFunction, call it, and return only its first return value, dropping any others. Otherwise, look for the CALL metatag and call that.

If the return value is a Varargs, only the 1st value will be returned. To get multiple values, use LuaValue.invoke() instead.

To call this as a method call, use LuaValue.method(LuaValue) instead.

Specified by:
call in class OneArgFunction
Parameters:
arg - First argument to supply to the called function
Returns:
First return value (this(arg)), or NIL if there were none.
See Also:
LuaValue.call(), LuaValue.call(LuaValue,LuaValue), LuaValue.call(LuaValue, LuaValue, LuaValue), #invoke(LuaValue), LuaValue.method(String,LuaValue), LuaValue.method(LuaValue,LuaValue)

setIsLoaded

public void setIsLoaded(java.lang.String name,
                        LuaTable value)
Allow packages to mark themselves as loaded


setLuaPath

public void setLuaPath(java.lang.String newLuaPath)

tojstring

public java.lang.String tojstring()
Description copied from class: LuaValue
Convert to human readable String for any type.

Overrides:
tojstring in class LibFunction
Returns:
String for use by human readers based on type.
See Also:
LuaValue.tostring(), LuaValue.optjstring(String), LuaValue.checkjstring(), LuaValue.isstring(), TSTRING

module

public Varargs module(Varargs args)
module (name [, ...]) Creates a module. If there is a table in package.loaded[name], this table is the module. Otherwise, if there is a global table t with the given name, this table is the module. Otherwise creates a new table t and sets it as the value of the global name and the value of package.loaded[name]. This function also initializes t._NAME with the given name, t._M with the module (t itself), and t._PACKAGE with the package name (the full module name minus last component; see below). Finally, module sets t as the new environment of the current function and the new value of package.loaded[name], so that require returns t. If name is a compound name (that is, one with components separated by dots), module creates (or reuses, if they already exist) tables for each component. For instance, if name is a.b.c, then module stores the module table in field c of field b of global a. This function may receive optional options after the module name, where each option is a function to be applied over the module.


require

public LuaValue require(LuaValue arg)
require (modname) Loads the given module. The function starts by looking into the package.loaded table to determine whether modname is already loaded. If it is, then require returns the value stored at package.loaded[modname]. Otherwise, it tries to find a loader for the module. To find a loader, require is guided by the package.loaders array. By changing this array, we can change how require looks for a module. The following explanation is based on the default configuration for package.loaders. First require queries package.preload[modname]. If it has a value, this value (which should be a function) is the loader. Otherwise require searches for a Lua loader using the path stored in package.path. If that also fails, it searches for a C loader using the path stored in package.cpath. If that also fails, it tries an all-in-one loader (see package.loaders). Once a loader is found, require calls the loader with a single argument, modname. If the loader returns any value, require assigns the returned value to package.loaded[modname]. If the loader returns no value and has not assigned any value to package.loaded[modname], then require assigns true to this entry. In any case, require returns the final value of package.loaded[modname]. If there is any error loading or running the module, or if it cannot find any loader for the module, then require signals an error.


loadlib

public static Varargs loadlib(Varargs args)

toClassname

public static final java.lang.String toClassname(java.lang.String filename)
Convert lua filename to valid class name



Copyright © 2007-2008 Luaj.org. All Rights Reserved.