Improve documentation and samples.
This commit is contained in:
@@ -56,36 +56,59 @@ import org.luaj.vm2.lib.ResourceFinder;
|
||||
*/
|
||||
public class Globals extends LuaTable {
|
||||
|
||||
/** The current default input stream. */
|
||||
public InputStream STDIN = null;
|
||||
|
||||
/** The current default output stream. */
|
||||
public PrintStream STDOUT = System.out;
|
||||
|
||||
/** The current default error stream. */
|
||||
public PrintStream STDERR = System.err;
|
||||
|
||||
/** The installed ResourceFinder for looking files by name. */
|
||||
public ResourceFinder FINDER;
|
||||
|
||||
/** The installed compiler. */
|
||||
public LuaCompiler compiler = null;
|
||||
|
||||
/** The currently running thread. Should not be changed by non-library code. */
|
||||
public LuaThread running = new LuaThread(this);
|
||||
|
||||
/** The BaseLib instance loaded into this Globals */
|
||||
public BaseLib baselib;
|
||||
|
||||
public LuaValue errorfunc;
|
||||
|
||||
public LuaThread running_thread = new LuaThread(this);
|
||||
|
||||
public DebugLib debuglib;
|
||||
|
||||
/** The PackageLib instance loaded into this Globals */
|
||||
public PackageLib package_;
|
||||
|
||||
/** The DebugLib instance loaded into this Globals, or null if debugging is not enabled */
|
||||
public DebugLib debuglib;
|
||||
|
||||
/** The current error handler for this Globals */
|
||||
public LuaValue errorfunc;
|
||||
|
||||
/** Check that this object is a Globals object, and return it, otherwise throw an error. */
|
||||
public Globals checkglobals() {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Varargs loadFile(String filename) {
|
||||
return baselib.loadFile(filename, "bt", this);
|
||||
/** Convenience function for loading a file.
|
||||
* @param filename Name of the file to load.
|
||||
* @return LuaValue that can be call()'ed or invoke()'ed.
|
||||
* @throws LuaError if the file could not be loaded.
|
||||
*/
|
||||
public LuaValue loadFile(String filename) {
|
||||
Varargs v = baselib.loadFile(filename, "bt", this);
|
||||
return !v.isnil(1)? v.arg1(): error(v.arg(2).tojstring());
|
||||
}
|
||||
|
||||
/** Function which yields the current thread.
|
||||
* @param args Arguments to supply as return values in the resume function of the resuming thread.
|
||||
* @return Values supplied as arguments to the resume() call that reactivates this thread.
|
||||
*/
|
||||
public Varargs yield(Varargs args) {
|
||||
if (running_thread == null || running_thread.isMainThread())
|
||||
if (running == null || running.isMainThread())
|
||||
throw new LuaError("cannot yield main thread");
|
||||
final LuaThread.State s = running_thread.state;
|
||||
final LuaThread.State s = running.state;
|
||||
return s.lua_yield(args);
|
||||
}
|
||||
|
||||
|
||||
@@ -192,9 +192,9 @@ public class LuaThread extends LuaValue {
|
||||
}
|
||||
|
||||
public synchronized Varargs lua_resume(LuaThread new_thread, Varargs args) {
|
||||
LuaThread previous_thread = globals.running_thread;
|
||||
LuaThread previous_thread = globals.running;
|
||||
try {
|
||||
globals.running_thread = new_thread;
|
||||
globals.running = new_thread;
|
||||
this.args = args;
|
||||
if (this.status == STATUS_INITIAL) {
|
||||
this.status = STATUS_RUNNING;
|
||||
@@ -215,9 +215,9 @@ public class LuaThread extends LuaValue {
|
||||
this.args = LuaValue.NONE;
|
||||
this.result = LuaValue.NONE;
|
||||
this.error = null;
|
||||
globals.running_thread = previous_thread;
|
||||
globals.running = previous_thread;
|
||||
if (previous_thread != null)
|
||||
globals.running_thread.state.status =STATUS_RUNNING;
|
||||
globals.running.state.status =STATUS_RUNNING;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -93,7 +93,7 @@ public class CoroutineLib extends OneArgFunction {
|
||||
|
||||
final class running extends VarArgFunction {
|
||||
public Varargs invoke(Varargs args) {
|
||||
final LuaThread r = globals.running_thread;
|
||||
final LuaThread r = globals.running;
|
||||
return varargsOf(r, valueOf(r.isMainThread()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,7 +129,7 @@ public class DebugLib extends OneArgFunction {
|
||||
// debug.gethook ([thread])
|
||||
final class gethook extends VarArgFunction {
|
||||
public Varargs invoke(Varargs args) {
|
||||
LuaThread t = args.narg() > 0 ? args.checkthread(1): globals.running_thread;
|
||||
LuaThread t = args.narg() > 0 ? args.checkthread(1): globals.running;
|
||||
return varargsOf(
|
||||
t.hookfunc != null? t.hookfunc: NIL,
|
||||
valueOf((t.hookcall?"c":"")+(t.hookline?"l":"")+(t.hookrtrn?"r":"")),
|
||||
@@ -141,7 +141,7 @@ public class DebugLib extends OneArgFunction {
|
||||
final class getinfo extends VarArgFunction {
|
||||
public Varargs invoke(Varargs args) {
|
||||
int a=1;
|
||||
LuaThread thread = args.isthread(a)? args.checkthread(a++): globals.running_thread;
|
||||
LuaThread thread = args.isthread(a)? args.checkthread(a++): globals.running;
|
||||
LuaValue func = args.arg(a++);
|
||||
String what = args.optjstring(a++, "flnStu");
|
||||
DebugLib.CallStack callstack = callstack(thread);
|
||||
@@ -204,7 +204,7 @@ public class DebugLib extends OneArgFunction {
|
||||
final class getlocal extends VarArgFunction {
|
||||
public Varargs invoke(Varargs args) {
|
||||
int a=1;
|
||||
LuaThread thread = args.isthread(a)? args.checkthread(a++): globals.running_thread;
|
||||
LuaThread thread = args.isthread(a)? args.checkthread(a++): globals.running;
|
||||
int level = args.checkint(a++);
|
||||
int local = args.checkint(a++);
|
||||
CallFrame f = callstack(thread).getCallFrame(level);
|
||||
@@ -255,7 +255,7 @@ public class DebugLib extends OneArgFunction {
|
||||
final class sethook extends VarArgFunction {
|
||||
public Varargs invoke(Varargs args) {
|
||||
int a=1;
|
||||
LuaThread t = args.isthread(a)? args.checkthread(a++): globals.running_thread;
|
||||
LuaThread t = args.isthread(a)? args.checkthread(a++): globals.running;
|
||||
LuaValue func = args.optfunction(a++, null);
|
||||
String str = args.optjstring(a++,"");
|
||||
int count = args.optint(a++,0);
|
||||
@@ -279,7 +279,7 @@ public class DebugLib extends OneArgFunction {
|
||||
final class setlocal extends VarArgFunction {
|
||||
public Varargs invoke(Varargs args) {
|
||||
int a=1;
|
||||
LuaThread thread = args.isthread(a)? args.checkthread(a++): globals.running_thread;
|
||||
LuaThread thread = args.isthread(a)? args.checkthread(a++): globals.running;
|
||||
int level = args.checkint(a++);
|
||||
int local = args.checkint(a++);
|
||||
LuaValue value = args.arg(a++);
|
||||
@@ -339,7 +339,7 @@ public class DebugLib extends OneArgFunction {
|
||||
final class traceback extends VarArgFunction {
|
||||
public Varargs invoke(Varargs args) {
|
||||
int a=1;
|
||||
LuaThread thread = args.isthread(a)? args.checkthread(a++): globals.running_thread;
|
||||
LuaThread thread = args.isthread(a)? args.checkthread(a++): globals.running;
|
||||
String message = args.optjstring(a++, null);
|
||||
int level = args.optint(a++,1);
|
||||
String tb = callstack(thread).traceback(level);
|
||||
@@ -379,7 +379,7 @@ public class DebugLib extends OneArgFunction {
|
||||
}
|
||||
|
||||
public void onCall(LuaFunction f) {
|
||||
LuaThread t = globals.running_thread;
|
||||
LuaThread t = globals.running;
|
||||
if (t.inhook) return;
|
||||
callstack().onCall(f);
|
||||
if (t.hookcall && t.hookfunc != null)
|
||||
@@ -387,7 +387,7 @@ public class DebugLib extends OneArgFunction {
|
||||
}
|
||||
|
||||
public void onCall(LuaClosure c, Varargs varargs, LuaValue[] stack) {
|
||||
LuaThread t = globals.running_thread;
|
||||
LuaThread t = globals.running;
|
||||
if (t.inhook) return;
|
||||
callstack().onCall(c, varargs, stack);
|
||||
if (t.hookcall && t.hookfunc != null)
|
||||
@@ -395,7 +395,7 @@ public class DebugLib extends OneArgFunction {
|
||||
}
|
||||
|
||||
public void onInstruction(int pc, Varargs v, int top) {
|
||||
LuaThread t = globals.running_thread;
|
||||
LuaThread t = globals.running;
|
||||
if (t.inhook) return;
|
||||
callstack().onInstruction(pc, v, top);
|
||||
if (t.hookfunc == null) return;
|
||||
@@ -412,7 +412,7 @@ public class DebugLib extends OneArgFunction {
|
||||
}
|
||||
|
||||
public void onReturn() {
|
||||
LuaThread t = globals.running_thread;
|
||||
LuaThread t = globals.running;
|
||||
if (t.inhook) return;
|
||||
callstack().onReturn();
|
||||
if (t.hookcall && t.hookfunc != null)
|
||||
@@ -424,7 +424,7 @@ public class DebugLib extends OneArgFunction {
|
||||
}
|
||||
|
||||
void callHook(LuaValue type, LuaValue arg) {
|
||||
LuaThread t = globals.running_thread;
|
||||
LuaThread t = globals.running;
|
||||
t.inhook = true;
|
||||
try {
|
||||
t.hookfunc.call(type, arg);
|
||||
@@ -436,7 +436,7 @@ public class DebugLib extends OneArgFunction {
|
||||
}
|
||||
|
||||
CallStack callstack() {
|
||||
return callstack(globals.running_thread);
|
||||
return callstack(globals.running);
|
||||
}
|
||||
|
||||
CallStack callstack(LuaThread t) {
|
||||
|
||||
Reference in New Issue
Block a user