Let VarArgFunction instances register with Thread on invocation.

This commit is contained in:
James Roseborough
2009-11-03 19:12:48 +00:00
parent 3906ab0711
commit 1b66a91c95
4 changed files with 24 additions and 11 deletions

View File

@@ -138,7 +138,7 @@ public class LuaThread extends LuaValue implements Runnable {
}
public static final LuaFunction getCallstackFunction(int level) {
return level>0 || level<running_thread.calls?
return level>=0 && level<running_thread.calls?
running_thread.callstack[running_thread.calls-level-1]:
null;
}

View File

@@ -351,17 +351,18 @@ public class PackageLib extends LuaTable {
private LuaValue loader_Java( Varargs args ) {
String name = args.checkString(1);
String classname = toClassname( name );
Class c = null;
LuaValue v = null;
try {
c = Class.forName(name.replace('/', '.'));
c = Class.forName(classname);
v = (LuaValue) c.newInstance();
v.setfenv(_G);
return v;
} catch ( ClassNotFoundException cnfe ) {
return valueOf("\n\tno class '"+name+"'" );
return valueOf("\n\tno class '"+classname+"'" );
} catch ( Exception e ) {
return valueOf("\n\tjava load failed on '"+name+"', "+e );
return valueOf("\n\tjava load failed on '"+classname+"', "+e );
}
}
@@ -373,10 +374,10 @@ public class PackageLib extends LuaTable {
j -= 4;
for ( int k=0; k<j; k++ ) {
char c = filename.charAt(k);
if ( (!isClassnamePart(c)) && (c!='/') && (c!='\\') ) {
if ( (!isClassnamePart(c)) || (c=='/') || (c=='\\') ) {
StringBuffer sb = new StringBuffer(j);
for ( int i=0; i<j; i++ ) {
c = filename.charAt(k);
c = filename.charAt(i);
sb.append(
(isClassnamePart(c))? c:
((c=='/') || (c=='\\'))? '.': '_' );

View File

@@ -21,13 +21,11 @@
******************************************************************************/
package org.luaj.vm2.lib;
import org.luaj.vm2.LuaThread;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Varargs;
abstract public class VarArgFunction extends LibFunction {
abstract public Varargs invoke(Varargs args);
public VarArgFunction() {
}
@@ -54,4 +52,18 @@ abstract public class VarArgFunction extends LibFunction {
public LuaValue call(LuaValue arg1, LuaValue arg2, LuaValue arg3) {
return invoke(varargsOf(arg1,arg2,arg3)).arg1();
}
public Varargs invoke(Varargs args) {
LuaThread.onCall(this);
try {
return onInvoke(args);
} finally {
LuaThread.onReturn();
}
}
protected Varargs onInvoke(Varargs args) {
return NONE;
}
}

View File

@@ -254,7 +254,7 @@ public class JavaBytecodeGenerator {
nl = p.maxstacksize;
locals = new LocalVariableGen[nl];
// implement LuaValue.invoke(Varargs args)
// implement LuaValue.onInvoke(Varargs args)
init = new InstructionList();
il = new InstructionList();
mg = new MethodGen(
@@ -262,7 +262,7 @@ public class JavaBytecodeGenerator {
TYPE_VARARGS, // return type
new Type[] { TYPE_VARARGS }, // argument types
new String[] { "args" }, // arg names
"invoke", STR_LUAVALUE, // method, class
"onInvoke", STR_LUAVALUE, // method, class
il, cp);
factory = new InstructionFactory(cg);