Improve string lib factoring, javadoc on lib loading functions.

This commit is contained in:
James Roseborough
2015-04-05 16:19:13 +00:00
parent 591de13a46
commit 878bf5ac78
12 changed files with 122 additions and 29 deletions

View File

@@ -62,7 +62,12 @@ import org.luaj.vm2.lib.StringLib;
*/
public class LuaString extends LuaValue {
/** The singleton instance for string metatables that forwards to the string functions */
/** The singleton instance for string metatables that forwards to the string functions.
* Typically, this is set to the string metatable as a side effect of loading the string
* library, and is read-write to provide flexible behavior by default. When used in a
* server environment where there may be roge scripts, this should be replaced with a
* read-only table since it is shared across all lua code in this Java VM.
*/
public static LuaValue s_metatable;
/** The bytes for the string. These <em><b>must not be mutated directly</b></em> because

View File

@@ -79,6 +79,12 @@ public class BaseLib extends TwoArgFunction implements ResourceFinder {
Globals globals;
/** Perform one-time initialization on the library by adding base functions
* to the supplied environment, and returning it as the return value.
* @param modname the module name supplied if this is loaded via 'require'.
* @param env the environment to load into, which must be a Globals instance.
*/
public LuaValue call(LuaValue modname, LuaValue env) {
globals = env.checkglobals();
globals.finder = this;

View File

@@ -56,6 +56,12 @@ public class Bit32Lib extends TwoArgFunction {
public Bit32Lib() {
}
/** 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.
* @param modname the module name supplied if this is loaded via 'require'.
* @param env the environment to load into, which must be a Globals instance.
*/
public LuaValue call(LuaValue modname, LuaValue env) {
LuaTable t = new LuaTable();
bind(t, Bit32LibV.class, new String[] {

View File

@@ -66,6 +66,12 @@ public class CoroutineLib extends TwoArgFunction {
Globals globals;
/** 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.
* @param modname the module name supplied if this is loaded via 'require'.
* @param env the environment to load into, which must be a Globals instance.
*/
public LuaValue call(LuaValue modname, LuaValue env) {
globals = env.checkglobals();
LuaTable coroutine = new LuaTable();

View File

@@ -66,6 +66,10 @@ import org.luaj.vm2.Varargs;
* System.out.println( globals.get("debug").get("traceback").call() );
* } </pre>
* <p>
* This library exposes the entire state of lua code, and provides method to see and modify
* all underlying lua values within a Java VM so should not be exposed to client code
* in a shared server environment.
*
* @see LibFunction
* @see JsePlatform
* @see JmePlatform
@@ -103,6 +107,12 @@ public class DebugLib extends TwoArgFunction {
Globals globals;
/** 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.
* @param modname the module name supplied if this is loaded via 'require'.
* @param env the environment to load into, which must be a Globals instance.
*/
public LuaValue call(LuaValue modname, LuaValue env) {
globals = env.checkglobals();
globals.debuglib = this;

View File

@@ -79,13 +79,25 @@ import org.luaj.vm2.Varargs;
* @see <a href="http://www.lua.org/manual/5.2/manual.html#6.6">Lua 5.2 Math Lib Reference</a>
*/
public class MathLib extends TwoArgFunction {
/** Pointer to the latest MathLib instance, used only to dispatch
* math.exp to tha correct platform math library.
*/
public static MathLib MATHLIB = null;
/** Construct a MathLib, which can be initialized by calling it with a
* modname string, and a global environment table as arguments using
* {@link #call(LuaValue, LuaValue)}. */
public MathLib() {
MATHLIB = this;
}
/** 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.
* @param modname the module name supplied if this is loaded via 'require'.
* @param env the environment to load into, typically a Globals instance.
*/
public LuaValue call(LuaValue modname, LuaValue env) {
LuaTable math = new LuaTable(0,30);
math.set("abs", new abs());

View File

@@ -120,6 +120,12 @@ public class OsLib extends TwoArgFunction {
public OsLib() {
}
/** 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.
* @param modname the module name supplied if this is loaded via 'require'.
* @param env the environment to load into, typically a Globals instance.
*/
public LuaValue call(LuaValue modname, LuaValue env) {
globals = env.checkglobals();
LuaTable os = new LuaTable();

View File

@@ -120,6 +120,13 @@ public class PackageLib extends TwoArgFunction {
public PackageLib() {}
/** Perform one-time initialization on the library by adding package functions
* to the supplied environment, and returning it as the return value.
* It also creates the package.preload and package.loaded tables for use by
* other libraries.
* @param modname the module name supplied if this is loaded via 'require'.
* @param env the environment to load into, typically a Globals instance.
*/
public LuaValue call(LuaValue modname, LuaValue env) {
globals = env.checkglobals();
globals.set("require", new require());

View File

@@ -29,7 +29,6 @@ import org.luaj.vm2.Buffer;
import org.luaj.vm2.LuaString;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.ReadOnlyTable;
import org.luaj.vm2.Varargs;
import org.luaj.vm2.compiler.DumpState;
@@ -62,35 +61,49 @@ import org.luaj.vm2.compiler.DumpState;
*/
public class StringLib extends TwoArgFunction {
public static final ReadOnlyTable lib_functions;
static {
LuaTable t = new LuaTable();
t.set("byte", new byte_());
t.set("char", new char_());
t.set("dump", new dump());
t.set("find", new find());
t.set("format", new format());
t.set("gmatch", new gmatch());
t.set("gsub", new gsub());
t.set("len", new len());
t.set("lower", new lower());
t.set("match", new match());
t.set("rep", new rep());
t.set("reverse", new reverse());
t.set("sub", new sub());
t.set("upper", new upper());
lib_functions = new ReadOnlyTable(t);
LuaString.s_metatable = new ReadOnlyTable(
new LuaValue[] { INDEX, lib_functions });
}
/** Construct a StringLib, which can be initialized by calling it with a
* modname string, and a global environment table as arguments using
* {@link #call(LuaValue, LuaValue)}. */
public StringLib() {
}
/** 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.
* Creates a metatable that uses __INDEX to fall back on itself to support string
* method operations.
* If the shared strings metatable instance is null, will set the metatable as
* the global shared metatable for strings.
* <P>
* All tables and metatables are read-write by default so if this will be used in
* a server environment, sandboxing should be used. In particular, the
* {@link LuaString#s_metatable} table should probably be made read-only.
* @param modname the module name supplied if this is loaded via 'require'.
* @param env the environment to load into, typically a Globals instance.
*/
public LuaValue call(LuaValue modname, LuaValue env) {
env.set("string", lib_functions);
env.get("package").get("loaded").set("string", lib_functions);
return lib_functions;
LuaTable string = new LuaTable();
string.set("byte", new byte_());
string.set("char", new char_());
string.set("dump", new dump());
string.set("find", new find());
string.set("format", new format());
string.set("gmatch", new gmatch());
string.set("gsub", new gsub());
string.set("len", new len());
string.set("lower", new lower());
string.set("match", new match());
string.set("rep", new rep());
string.set("reverse", new reverse());
string.set("sub", new sub());
string.set("upper", new upper());
LuaTable mt = LuaValue.tableOf(
new LuaValue[] { INDEX, string });
env.set("string", string);
env.get("package").get("loaded").set("string", string);
if (LuaString.s_metatable == null)
LuaString.s_metatable = mt;
return string;
}
/**

View File

@@ -55,6 +55,12 @@ import org.luaj.vm2.Varargs;
*/
public class TableLib extends TwoArgFunction {
/** 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.
* @param modname the module name supplied if this is loaded via 'require'.
* @param env the environment to load into, typically a Globals instance.
*/
public LuaValue call(LuaValue modname, LuaValue env) {
LuaTable table = new LuaTable();
table.set("concat", new concat());

View File

@@ -71,7 +71,14 @@ import org.luaj.vm2.lib.ResourceFinder;
public class JseBaseLib extends org.luaj.vm2.lib.BaseLib {
/** Extend the library loading to set the default value for {@link Globals.STDIN} */
/** 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.
* <P>Specifically, extend the library loading to set the default value for {@link Globals.STDIN}
* @param modname the module name supplied if this is loaded via 'require'.
* @param env the environment to load into, which must be a Globals instance.
*/
public LuaValue call(LuaValue modname, LuaValue env) {
super.call(modname, env);
env.checkglobals().STDIN = System.in;

View File

@@ -61,6 +61,15 @@ public class JseMathLib extends org.luaj.vm2.lib.MathLib {
public JseMathLib() {}
/** 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.
* <P>Specifically, adds all library functions that can be implemented directly
* in JSE but not JME: acos, asin, atan, atan2, cosh, exp, log, pow, sinh, and tanh.
* @param modname the module name supplied if this is loaded via 'require'.
* @param env the environment to load into, which must be a Globals instance.
*/
public LuaValue call(LuaValue modname, LuaValue env) {
super.call(modname, env);
LuaValue math = env.get("math");