|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.luaj.vm2.Varargs org.luaj.vm2.LuaValue org.luaj.vm2.LuaThread
public class LuaThread
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.
Each Java thread wakes up at regular intervals and checks a weak reference
to determine if it can ever be resumed. If not, it throws
OrphanedThread
which is an Error
.
Applications should not catch OrphanedThread
, because it can break
the thread safety of luaj.
LuaValue
,
JsePlatform
,
JmePlatform
,
CoroutineLib
Nested Class Summary | |
---|---|
static class |
LuaThread.CallStack
|
Field Summary | |
---|---|
static int |
coroutine_count
|
java.lang.Object |
debugState
Thread-local used by DebugLib to store debugging state. |
LuaValue |
err
Field to hold state of error condition during debug hook function calls. |
static int |
GC_INTERVAL
Interval to check for LuaThread dereferencing. |
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 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 LuaThread.CallStack |
onCall(LuaFunction function)
Callback used at the beginning of a call to prepare for possible getfenv/setfenv calls |
LuaThread |
optthread(LuaThread defval)
Check that optional argument is a thread and return as LuaThread |
Varargs |
resume(Varargs args)
Start or resume this thread |
static LuaValue |
setErrorFunc(LuaValue errfunc)
Replace the error function of the currently running thread. |
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. |
static Varargs |
yield(Varargs args)
Yield the current thread with arguments |
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 |
---|
public static LuaValue s_metatable
public static int coroutine_count
public LuaValue err
public static final int MAX_CALLSTACK
public static int GC_INTERVAL
public java.lang.Object debugState
Constructor Detail |
---|
public LuaThread(LuaValue func, LuaValue env)
func
- The function to executeenv
- The environment to apply to the threadMethod Detail |
---|
public int type()
LuaValue
type
in class LuaValue
TNIL
,
TBOOLEAN
,
TNUMBER
,
TSTRING
,
TTABLE
,
TFUNCTION
,
TUSERDATA
,
TTHREAD
LuaValue.typename()
public java.lang.String typename()
LuaValue
typename
in class LuaValue
LuaValue.TYPE_NAMES
corresponding to the type of this value:
"nil", "boolean", "number", "string",
"table", "function", "userdata", "thread"LuaValue.type()
public boolean isthread()
LuaValue
this
is a thread
isthread
in class LuaValue
thread
, otherwise falseLuaValue.checkthread()
,
LuaValue.optthread(LuaThread)
,
LuaValue.TTHREAD
public LuaThread optthread(LuaThread defval)
LuaValue
LuaThread
optthread
in class LuaValue
defval
- LuaThread
to return if this
is nil or none
this
cast to LuaTable
if a thread,
defval
if nil or none,
throws LuaError
if some other typeLuaValue.checkthread()
,
LuaValue.isthread()
,
LuaValue.TTHREAD
public LuaThread checkthread()
LuaValue
LuaThread
, or throw LuaError
if it is not
checkthread
in class LuaValue
this
if it is a LuaThread
LuaValue.isthread()
,
LuaValue.optthread(LuaThread)
,
LuaValue.TTHREAD
public LuaValue getmetatable()
LuaValue
LuaValue
For LuaTable
and LuaUserdata
instances,
the metatable returned is this instance metatable.
For all other types, the class metatable value will be returned.
getmetatable
in class LuaValue
LuaBoolean.s_metatable
,
LuaNumber.s_metatable
,
LuaNil.s_metatable
,
LuaFunction.s_metatable
,
s_metatable
public LuaValue getfenv()
LuaValue
getfenv
in class LuaValue
LuaValue
currently set as the instances environent.public void setfenv(LuaValue env)
LuaValue
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.
setfenv
in class LuaValue
env
- LuaValue
(typically a LuaTable
) containing the environment.JmePlatform
,
JsePlatform
public java.lang.String getStatus()
public static LuaThread getRunning()
LuaThread
that is currenly runningpublic static boolean isMainThread(LuaThread r)
public static void setGlobals(LuaValue globals)
This must be done once before any other code executes.
globals
- The global variables for the main ghread.public static LuaValue getGlobals()
LuaValue
containing the global variables of the current thread.public static final LuaThread.CallStack onCall(LuaFunction function)
function
- Function being called
DebugLib
public static final LuaFunction getCallstackFunction(int level)
level
- 1 for the function calling this one, 2 for the next one.
public static LuaValue setErrorFunc(LuaValue errfunc)
errfunc
- the new error function to use.
public static Varargs yield(Varargs args)
args
- The arguments to send as return values to resume(Varargs)
Varargs
provided as arguments to resume(Varargs)
public Varargs resume(Varargs args)
args
- The arguments to send as return values to yield(Varargs)
Varargs
provided as arguments to yield(Varargs)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |