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

View File

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

View File

@@ -53,4 +53,10 @@ public class LClosure extends LFunction {
this.env = t; this.env = t;
return 1; 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; 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 * Set up a Java invocation, and leave the results on the stack
* starting at base. The default implementation for LFunction * 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) { public void __newindex(LuaState vm, LValue table, LValue key, LValue val) {
vm.call(this, table, key, val); vm.call(this, table, key, val);
} }
} }

View File

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

View File

@@ -2680,6 +2680,18 @@ public class LuaState extends Lua {
return (LFunction) checktype(narg, Lua.LUA_TFUNCTION); 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 * Checks whether the function argument <code>narg</code> is a number and
* returns this number cast to an <code>int</code>. * returns this number cast to an <code>int</code>.