diff --git a/src/core/org/luaj/vm2/LuaString.java b/src/core/org/luaj/vm2/LuaString.java index 9da5f1b5..363733e9 100644 --- a/src/core/org/luaj/vm2/LuaString.java +++ b/src/core/org/luaj/vm2/LuaString.java @@ -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 must not be mutated directly because diff --git a/src/core/org/luaj/vm2/lib/BaseLib.java b/src/core/org/luaj/vm2/lib/BaseLib.java index 7121ae94..a40b380a 100644 --- a/src/core/org/luaj/vm2/lib/BaseLib.java +++ b/src/core/org/luaj/vm2/lib/BaseLib.java @@ -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; diff --git a/src/core/org/luaj/vm2/lib/Bit32Lib.java b/src/core/org/luaj/vm2/lib/Bit32Lib.java index afce847b..1f076906 100644 --- a/src/core/org/luaj/vm2/lib/Bit32Lib.java +++ b/src/core/org/luaj/vm2/lib/Bit32Lib.java @@ -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[] { diff --git a/src/core/org/luaj/vm2/lib/CoroutineLib.java b/src/core/org/luaj/vm2/lib/CoroutineLib.java index f17e063c..f4df4be3 100644 --- a/src/core/org/luaj/vm2/lib/CoroutineLib.java +++ b/src/core/org/luaj/vm2/lib/CoroutineLib.java @@ -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(); diff --git a/src/core/org/luaj/vm2/lib/DebugLib.java b/src/core/org/luaj/vm2/lib/DebugLib.java index f0c1091f..083b2c95 100644 --- a/src/core/org/luaj/vm2/lib/DebugLib.java +++ b/src/core/org/luaj/vm2/lib/DebugLib.java @@ -66,6 +66,10 @@ import org.luaj.vm2.Varargs; * System.out.println( globals.get("debug").get("traceback").call() ); * } *

+ * 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; diff --git a/src/core/org/luaj/vm2/lib/MathLib.java b/src/core/org/luaj/vm2/lib/MathLib.java index e9e43d57..eff32061 100644 --- a/src/core/org/luaj/vm2/lib/MathLib.java +++ b/src/core/org/luaj/vm2/lib/MathLib.java @@ -79,13 +79,25 @@ import org.luaj.vm2.Varargs; * @see Lua 5.2 Math Lib Reference */ 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()); diff --git a/src/core/org/luaj/vm2/lib/OsLib.java b/src/core/org/luaj/vm2/lib/OsLib.java index 8e50b707..f9da8719 100644 --- a/src/core/org/luaj/vm2/lib/OsLib.java +++ b/src/core/org/luaj/vm2/lib/OsLib.java @@ -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(); diff --git a/src/core/org/luaj/vm2/lib/PackageLib.java b/src/core/org/luaj/vm2/lib/PackageLib.java index c635ea23..0a76c306 100644 --- a/src/core/org/luaj/vm2/lib/PackageLib.java +++ b/src/core/org/luaj/vm2/lib/PackageLib.java @@ -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()); diff --git a/src/core/org/luaj/vm2/lib/StringLib.java b/src/core/org/luaj/vm2/lib/StringLib.java index 62553dbb..86307f99 100644 --- a/src/core/org/luaj/vm2/lib/StringLib.java +++ b/src/core/org/luaj/vm2/lib/StringLib.java @@ -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. + *

+ * 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; } /** diff --git a/src/core/org/luaj/vm2/lib/TableLib.java b/src/core/org/luaj/vm2/lib/TableLib.java index 76fc35e8..03658149 100644 --- a/src/core/org/luaj/vm2/lib/TableLib.java +++ b/src/core/org/luaj/vm2/lib/TableLib.java @@ -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()); diff --git a/src/jse/org/luaj/vm2/lib/jse/JseBaseLib.java b/src/jse/org/luaj/vm2/lib/jse/JseBaseLib.java index 1ff970ce..c1aebc28 100644 --- a/src/jse/org/luaj/vm2/lib/jse/JseBaseLib.java +++ b/src/jse/org/luaj/vm2/lib/jse/JseBaseLib.java @@ -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. + *

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; diff --git a/src/jse/org/luaj/vm2/lib/jse/JseMathLib.java b/src/jse/org/luaj/vm2/lib/jse/JseMathLib.java index 0b4f2ef8..b00e5f41 100644 --- a/src/jse/org/luaj/vm2/lib/jse/JseMathLib.java +++ b/src/jse/org/luaj/vm2/lib/jse/JseMathLib.java @@ -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. + *

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");