diff --git a/src/core/org/luaj/lib/CoroutineLib.java b/src/core/org/luaj/lib/CoroutineLib.java index b1f8346b..20e49e23 100644 --- a/src/core/org/luaj/lib/CoroutineLib.java +++ b/src/core/org/luaj/lib/CoroutineLib.java @@ -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: { diff --git a/src/core/org/luaj/lib/PackageLib.java b/src/core/org/luaj/lib/PackageLib.java index 7ace4c0d..0844e183 100644 --- a/src/core/org/luaj/lib/PackageLib.java +++ b/src/core/org/luaj/lib/PackageLib.java @@ -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 ) diff --git a/src/core/org/luaj/vm/LClosure.java b/src/core/org/luaj/vm/LClosure.java index 2415a0fa..e93ee0e7 100644 --- a/src/core/org/luaj/vm/LClosure.java +++ b/src/core/org/luaj/vm/LClosure.java @@ -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; + } + } diff --git a/src/core/org/luaj/vm/LFunction.java b/src/core/org/luaj/vm/LFunction.java index e2ba54f3..0ae88ad4 100644 --- a/src/core/org/luaj/vm/LFunction.java +++ b/src/core/org/luaj/vm/LFunction.java @@ -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); } + } diff --git a/src/core/org/luaj/vm/LThread.java b/src/core/org/luaj/vm/LThread.java index 8ab03492..95feddac 100644 --- a/src/core/org/luaj/vm/LThread.java +++ b/src/core/org/luaj/vm/LThread.java @@ -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); } diff --git a/src/core/org/luaj/vm/LuaState.java b/src/core/org/luaj/vm/LuaState.java index 02023e6b..c03f099d 100644 --- a/src/core/org/luaj/vm/LuaState.java +++ b/src/core/org/luaj/vm/LuaState.java @@ -2680,6 +2680,18 @@ public class LuaState extends Lua { return (LFunction) checktype(narg, Lua.LUA_TFUNCTION); } + /** + * Checks whether the function argument narg 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 narg is a number and * returns this number cast to an int.