Improve argument type checking for coroutine, module libraries.

This commit is contained in:
James Roseborough
2008-07-19 00:31:04 +00:00
parent 0c02382673
commit fdf4dc0d56
6 changed files with 42 additions and 13 deletions

View File

@@ -85,12 +85,12 @@ public class CoroutineLib extends LFunction {
break;
}
case CREATE: {
LClosure c = (LClosure) vm.topointer(2);
vm.pushlvalue( new LThread(c) );
LFunction c = vm.checkfunction(2);
vm.pushlvalue( new LThread( c, c.luaGetEnv(vm._G) ) );
break;
}
case RESUME: {
LThread t = (LThread) vm.topointer(2);
LThread t = vm.checkthread(2);
t.resumeFrom( vm, vm.gettop()-2 );
return false;
}
@@ -104,12 +104,12 @@ public class CoroutineLib extends LFunction {
break;
}
case STATUS: {
vm.pushstring( ((LThread) vm.topointer(2)).getStatus() );
vm.pushstring( vm.checkthread(2).getStatus() );
break;
}
case WRAP: {
LClosure c = (LClosure) vm.topointer(2);
vm.pushlvalue( new CoroutineLib(WRAPPED,new LThread(c)) );
LFunction c = vm.checkfunction(2);
vm.pushlvalue( new CoroutineLib(WRAPPED,new LThread(c, c.luaGetEnv(vm._G))) );
break;
}
case YIELD: {

View File

@@ -126,9 +126,7 @@ public class PackageLib extends LFunction {
loadlib(vm);
break;
case SEEALL: {
if ( ! vm.istable(2) )
vm.error( "table expected, got "+vm.typename(2) );
LTable t = vm.totable(2);
LTable t = vm.checktable(2);
LTable m = t.luaGetMetatable();
if ( m == null )
t.luaSetMetatable(m = new LTable());
@@ -178,7 +176,7 @@ public class PackageLib extends LFunction {
* each option is a function to be applied over the module.
*/
public static void module(LuaState vm) {
LString modname = vm.tolstring(2);
LString modname = vm.checklstring(2);
int n = vm.gettop();
LValue value = LOADED.get(modname);
LTable module;
@@ -277,7 +275,7 @@ public class PackageLib extends LFunction {
* the module, then require signals an error.
*/
public void require( LuaState vm ) {
LString name = vm.tolstring(2);
LString name = vm.checklstring(2);
LValue loaded = LOADED.get(name);
if ( loaded.toJavaBoolean() ) {
if ( loaded == _SENTINEL )

View File

@@ -53,4 +53,10 @@ public class LClosure extends LFunction {
this.env = t;
return 1;
}
/** Get the enviroment for this closure */
public LTable luaGetEnv(LTable d) {
return env;
}
}

View File

@@ -37,6 +37,18 @@ public class LFunction extends LValue {
return Lua.LUA_TFUNCTION;
}
/**
* Get the environment of the object if it is a closure, or d if not a closure.
*
* @param d global environment
* @return environment for this object, or d if it is not an LClosure
* @see LClosure
* @see LTable
*/
public LTable luaGetEnv(LTable d) {
return d;
}
/**
* Set up a Java invocation, and leave the results on the stack
* starting at base. The default implementation for LFunction
@@ -91,5 +103,6 @@ public class LFunction extends LValue {
public void __newindex(LuaState vm, LValue table, LValue key, LValue val) {
vm.call(this, table, key, val);
}
}

View File

@@ -46,8 +46,8 @@ public class LThread extends LValue implements Runnable {
static LThread running;
public LThread(LClosure c) {
threadVm = new LuaState(c.env);
public LThread(LFunction c, LTable env) {
threadVm = new LuaState(env);
threadVm.pushlvalue(c);
}

View File

@@ -2680,6 +2680,18 @@ public class LuaState extends Lua {
return (LFunction) checktype(narg, Lua.LUA_TFUNCTION);
}
/**
* Checks whether the function argument <code>narg</code> is a thread and
* returns this thread.
* @see LThread
* @param narg the argument number
* @throws LuaErrorException if the value is not a thread
* @return LThread value if the argument is a thread
*/
public LThread checkthread(int narg) {
return (LThread) checktype(narg, Lua.LUA_TTHREAD);
}
/**
* Checks whether the function argument <code>narg</code> is a number and
* returns this number cast to an <code>int</code>.