org.luaj.vm2
Class LuaThread

java.lang.Object
  extended by org.luaj.vm2.Varargs
      extended by org.luaj.vm2.LuaValue
          extended by org.luaj.vm2.LuaThread
All Implemented Interfaces:
java.lang.Runnable

public class LuaThread
extends LuaValue
implements java.lang.Runnable

Subclass of LuaValue that implements a lua coroutine thread using Java Threads.

A LuaThread is typically created in response to a scripted call to coroutine.create()

The threads must be initialized with the globals, so that the global environment may be passed along according to rules of lua. This is done via a call to setGlobals(LuaValue) at some point during globals initialization. See BaseLib for additional documentation and example code.

The utility classes JsePlatform and JmePlatform see to it that this initialization is done properly. For this reason it is highly recommended to use one of these classes when initializing globals.

The behavior of coroutine threads matches closely the behavior of C coroutine library. However, because of the use of Java threads to manage call state, it is possible to yield from anywhere in luaj. On the other hand, if a LuaThread is created, then yields without ever entering a completed state, then the garbage collector may not be able to determine that the thread needs to be collected, and this could cause a memory and resource leak. It is recommended that all coroutines that are created are resumed until they are in a completed state.

See Also:
LuaValue, JsePlatform, JmePlatform, CoroutineLib

Field Summary
 int calls
           
 LuaFunction[] callstack
           
 java.lang.Object debugState
           
 LuaValue err
           
static int MAX_CALLSTACK
           
static LuaValue 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
LuaThread(LuaValue func, LuaValue env)
          Create a LuaThread around a function and environment
 
Method Summary
 LuaThread checkthread()
          Check that this is a LuaThread, or throw LuaError if it is not
static int getCallstackDepth()
          Get number of calls in stack
static LuaFunction getCallstackFunction(int level)
          Get the function called as a specific location on the stack.
 LuaValue getfenv()
          Get the environemnt for an instance.
static LuaValue getGlobals()
          Get the current thread's environment
 LuaValue getmetatable()
          Get the metatable for this LuaValue
static LuaThread getRunning()
          Get the currently running thread.
 java.lang.String getStatus()
           
static boolean isMainThread(LuaThread r)
          Test if this is the main thread
 boolean isthread()
          Check if this is a thread
static void onCall(LuaFunction function)
          Callback used at the beginning of a call
static void onReturn()
          Callback used at the end of a call
 LuaThread optthread(LuaThread defval)
          Check that optional argument is a thread and return as LuaThread
 Varargs resume(Varargs args)
          Start or resume this thread
 void run()
           
 void setfenv(LuaValue env)
          Set the environment on an object.
static void setGlobals(LuaValue globals)
          Set the globals of the current thread.
 int type()
          Get the enumeration value for the type of this value.
 java.lang.String typename()
          Get the String name of the type of this value.
 Varargs yield(Varargs args)
          Yield this thread with arguments
 
Methods inherited from class org.luaj.vm2.LuaValue
add, add, add, and, arg, arg1, argerror, argerror, aritherror, aritherror, arithmt, assert_, buffer, call, call, call, call, callmt, checkboolean, checkclosure, checkdouble, checkfunction, checkint, checkinteger, checkjstring, checklong, checkmetatag, checknotnil, checknumber, checknumber, checkstring, checktable, 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, invoke, invokemethod, invokemethod, invokemethod, invokemethod, invokemethod, invokemethod, isboolean, isclosure, isfunction, isint, isinttype, islong, isnil, isnumber, isstring, istable, 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, optfunction, optint, optinteger, optjstring, optlong, optnumber, optstring, opttable, 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, tojstring, 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

s_metatable

public static LuaValue s_metatable

err

public LuaValue err

MAX_CALLSTACK

public static final int MAX_CALLSTACK
See Also:
Constant Field Values

callstack

public final LuaFunction[] callstack

calls

public int calls

debugState

public java.lang.Object debugState
Constructor Detail

LuaThread

public LuaThread(LuaValue func,
                 LuaValue env)
Create a LuaThread around a function and environment

Parameters:
func - The function to execute
env - The environment to apply to the thread
Method Detail

type

public int type()
Description copied from class: LuaValue
Get the enumeration value for the type of this value.

Specified by:
type in class LuaValue
Returns:
value for this type, one of TNIL, TBOOLEAN, TNUMBER, TSTRING, TTABLE, TFUNCTION, TUSERDATA, TTHREAD
See Also:
LuaValue.typename()

typename

public java.lang.String typename()
Description copied from class: LuaValue
Get the String name of the type of this value.

Specified by:
typename in class LuaValue
Returns:
name from type name list LuaValue.TYPE_NAMES corresponding to the type of this value: "nil", "boolean", "number", "string", "table", "function", "userdata", "thread"
See Also:
LuaValue.type()

isthread

public boolean isthread()
Description copied from class: LuaValue
Check if this is a thread

Overrides:
isthread in class LuaValue
Returns:
true if this is a thread, otherwise false
See Also:
LuaValue.checkthread(), LuaValue.optthread(LuaThread), LuaValue.TTHREAD

optthread

public LuaThread optthread(LuaThread defval)
Description copied from class: LuaValue
Check that optional argument is a thread and return as LuaThread

Overrides:
optthread in class LuaValue
Parameters:
defval - LuaThread to return if this is nil or none
Returns:
this cast to LuaTable if a thread, defval if nil or none, throws LuaError if some other type
See Also:
LuaValue.checkthread(), LuaValue.isthread(), LuaValue.TTHREAD

checkthread

public LuaThread checkthread()
Description copied from class: LuaValue
Check that this is a LuaThread, or throw LuaError if it is not

Overrides:
checkthread in class LuaValue
Returns:
this if it is a LuaThread
See Also:
LuaValue.isthread(), LuaValue.optthread(LuaThread), LuaValue.TTHREAD

getmetatable

public LuaValue getmetatable()
Description copied from class: LuaValue
Get the metatable for this LuaValue

For LuaTable and LuaUserdata instances, the metatable returned is this instance metatable. For all other types, the class metatable value will be returned.

Overrides:
getmetatable in class LuaValue
Returns:
metatable, or null if it there is none
See Also:
LuaBoolean.s_metatable, LuaNumber.s_metatable, LuaNil.s_metatable, LuaFunction.s_metatable, s_metatable

getfenv

public LuaValue getfenv()
Description copied from class: LuaValue
Get the environemnt for an instance.

Overrides:
getfenv in class LuaValue
Returns:
LuaValue currently set as the instances environent.

setfenv

public void setfenv(LuaValue env)
Description copied from class: LuaValue
Set the environment on an object.

Typically the environment is created once per application via a platform helper method such as JsePlatform.standardGlobals() However, any object can serve as an environment if it contains suitable metatag values to implement LuaValue.get(LuaValue) to provide the environment values.

Overrides:
setfenv in class LuaValue
Parameters:
env - LuaValue (typically a LuaTable) containing the environment.
See Also:
JmePlatform, JsePlatform

getStatus

public java.lang.String getStatus()

getRunning

public static LuaThread getRunning()
Get the currently running thread.

Returns:
LuaThread that is currenly running

isMainThread

public static boolean isMainThread(LuaThread r)
Test if this is the main thread

Returns:
true if this is the main thread

setGlobals

public static void setGlobals(LuaValue globals)
Set the globals of the current thread.

This must be done once before any other code executes.

Parameters:
globals - The global variables for the main ghread.

getGlobals

public static LuaValue getGlobals()
Get the current thread's environment

Returns:
LuaValue containing the global variables of the current thread.

onCall

public static final void onCall(LuaFunction function)
Callback used at the beginning of a call

Parameters:
function - Function being called
See Also:
DebugLib

onReturn

public static final void onReturn()
Callback used at the end of a call

See Also:
DebugLib

getCallstackDepth

public static int getCallstackDepth()
Get number of calls in stack

Returns:
number of calls in current call stack
See Also:
DebugLib

getCallstackFunction

public static final LuaFunction getCallstackFunction(int level)
Get the function called as a specific location on the stack.

Parameters:
level - 1 for the function calling this one, 2 for the next one.
Returns:
LuaFunction on the call stack, or null if outside of range of active stack

run

public void run()
Specified by:
run in interface java.lang.Runnable

yield

public Varargs yield(Varargs args)
Yield this thread with arguments

Parameters:
args - The arguments to send as return values to resume(Varargs)
Returns:
Varargs provided as arguments to resume(Varargs)

resume

public Varargs resume(Varargs args)
Start or resume this thread

Parameters:
args - The arguments to send as return values to yield(Varargs)
Returns:
Varargs provided as arguments to yield(Varargs)


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