Replace deprecated API's

This commit is contained in:
James Roseborough
2007-11-05 17:19:46 +00:00
parent dda1af0570
commit c93301506a
7 changed files with 101 additions and 96 deletions

View File

@@ -187,28 +187,38 @@ public class LuaCompat extends LFunction {
this.id = id; this.id = id;
} }
private static void setResult( VM vm, LValue value ) {
vm.settop(0);
vm.pushlvalue( value );
}
private static void setErrorResult( VM vm, String message ) {
vm.settop(0);
vm.pushnil();
vm.pushstring( message );
}
public boolean luaStackCall( VM vm ) { public boolean luaStackCall( VM vm ) {
switch ( id ) { switch ( id ) {
case LOADFILE: case LOADFILE:
loadfile(vm, vm.getArgAsString(0)); loadfile(vm, vm.tostring(2));
break; break;
case TONUMBER: case TONUMBER:
vm.setResult( toNumber( vm ) ); setResult( vm, toNumber( vm ) );
break; break;
case RAWGET: { case RAWGET: {
LValue t = vm.getArg(0); LValue t = vm.topointer(2);
LValue k = vm.getArg(1); LValue k = vm.topointer(3);
LValue result = LNil.NIL; vm.settop(0);
if ( t instanceof LTable ) { if ( t instanceof LTable ) {
result = ( (LTable) t ).get( k ); vm.pushlvalue(( (LTable) t ).get( k ));
} }
vm.setResult( result );
} break; } break;
case RAWSET: { case RAWSET: {
LValue t = vm.getArg(0); LValue t = vm.topointer(2);
LValue k = vm.getArg(1); LValue k = vm.topointer(3);
LValue v = vm.getArg(2); LValue v = vm.topointer(4);
vm.setResult(); vm.settop(0);
if ( t instanceof LTable ) { if ( t instanceof LTable ) {
( (LTable) t ).put( k, v ); ( (LTable) t ).put( k, v );
} else { } else {
@@ -223,25 +233,25 @@ public class LuaCompat extends LFunction {
break; break;
case COLLECTGARBAGE: case COLLECTGARBAGE:
System.gc(); System.gc();
vm.setResult(); vm.settop(0);
break; break;
case DOFILE: case DOFILE:
dofile(vm); dofile(vm);
break; break;
case LOADSTRING: case LOADSTRING:
loadstring(vm, vm.getArg(0), vm.getArgAsString(1)); loadstring(vm, vm.topointer(2), vm.tostring(3));
break; break;
case LOAD: case LOAD:
load(vm, vm.getArg(0), vm.getArgAsString(1)); load(vm, vm.topointer(2), vm.tostring(3));
break; break;
case TOSTRING: case TOSTRING:
vm.setResult( tostring(vm, vm.getArg(0)) ); setResult( vm, tostring(vm, vm.topointer(2)) );
break; break;
case UNPACK: case UNPACK:
unpack(vm); unpack(vm);
break; break;
case NEXT: case NEXT:
vm.setResult( next(vm, vm.getArg(0), vm.getArgAsInt(1)) ); setResult( vm, next(vm, vm.topointer(2), vm.tointeger(3)) );
break; break;
case MODULE: case MODULE:
module(vm); module(vm);
@@ -252,31 +262,31 @@ public class LuaCompat extends LFunction {
// Math functions // Math functions
case ABS: case ABS:
vm.setResult( abs( vm.getArg( 0 ) ) ); setResult( vm, abs( vm.topointer( 2 ) ) );
break; break;
case COS: case COS:
vm.setResult( new LDouble( Math.cos ( vm.getArgAsDouble( 0 ) ) ) ); setResult( vm, new LDouble( Math.cos ( vm.tonumber(2) ) ) );
break; break;
case MAX: case MAX:
vm.setResult( max( vm.getArg( 0 ), vm.getArg( 1 ) ) ); setResult( vm, max( vm.topointer(2), vm.topointer(3) ) );
break; break;
case MIN: case MIN:
vm.setResult( min( vm.getArg( 0 ), vm.getArg( 1 ) ) ); setResult( vm, min( vm.topointer(2), vm.topointer(3) ) );
break; break;
case MODF: case MODF:
modf( vm ); modf( vm );
break; break;
case SIN: case SIN:
vm.setResult( new LDouble( Math.sin( vm.getArgAsDouble( 0 ) ) ) ); setResult( vm, new LDouble( Math.sin( vm.tonumber(2) ) ) );
break; break;
case SQRT: case SQRT:
vm.setResult( new LDouble( Math.sqrt( vm.getArgAsDouble( 0 ) ) ) ); setResult( vm, new LDouble( Math.sqrt( vm.tonumber(2) ) ) );
break; break;
case CEIL: case CEIL:
vm.setResult( LInteger.valueOf( (int) Math.ceil( vm.getArgAsDouble( 0 ) ) ) ); setResult( vm, LInteger.valueOf( (int) Math.ceil( vm.tonumber(2) ) ) );
break; break;
case FLOOR: case FLOOR:
vm.setResult( LInteger.valueOf( (int) Math.floor( vm.getArgAsDouble( 0 ) ) ) ); setResult( vm, LInteger.valueOf( (int) Math.floor( vm.tonumber(2) ) ) );
break; break;
// String functions // String functions
@@ -355,28 +365,16 @@ public class LuaCompat extends LFunction {
} }
private void select( VM vm ) { private void select( VM vm ) {
LValue arg = vm.getArg( 0 ); LValue arg = vm.topointer(2);
if ( arg instanceof LNumber ) { if ( arg instanceof LNumber ) {
final int start; final int start = Math.min(arg.toJavaInt(),vm.gettop());
final int numResults; for ( int i=0; i<=start; i++ )
if ( ( start = arg.toJavaInt() ) > 0 && vm.remove(1);
( numResults = Math.max( vm.getArgCount() - start,
vm.getExpectedResultCount() ) ) > 0 ) {
// since setResult trashes the arguments, we have to save them somewhere.
LValue[] results = new LValue[numResults];
for ( int i = 0; i < numResults; ++i ) {
results[i] = vm.getArg( i+start );
}
vm.setResult();
for ( int i = 0; i < numResults; ++i ) {
vm.push( results[i] );
}
return; return;
}
} else if ( arg.toJavaString().equals( "#" ) ) { } else if ( arg.toJavaString().equals( "#" ) ) {
vm.setResult( LInteger.valueOf( vm.getArgCount() - 1 ) ); setResult( vm, LInteger.valueOf( vm.gettop() - 2 ) );
} }
vm.setResult(); vm.settop(0);
} }
private LValue abs( final LValue v ) { private LValue abs( final LValue v ) {
@@ -393,23 +391,23 @@ public class LuaCompat extends LFunction {
} }
private void modf( VM vm ) { private void modf( VM vm ) {
LValue arg = vm.getArg( 0 ); LValue arg = vm.topointer(2);
double v = arg.toJavaDouble(); double v = arg.toJavaDouble();
double intPart = ( v > 0 ) ? Math.floor( v ) : Math.ceil( v ); double intPart = ( v > 0 ) ? Math.floor( v ) : Math.ceil( v );
double fracPart = v - intPart; double fracPart = v - intPart;
vm.setResult(); vm.settop(0);
vm.push( intPart ); vm.pushnumber( intPart );
vm.push( fracPart ); vm.pushnumber( fracPart );
} }
private LValue toNumber( VM vm ) { private LValue toNumber( VM vm ) {
LValue input = vm.getArg(0); LValue input = vm.topointer(2);
if ( input instanceof LNumber ) { if ( input instanceof LNumber ) {
return input; return input;
} else if ( input instanceof LString ) { } else if ( input instanceof LString ) {
int base = 10; int base = 10;
if ( vm.getArgCount()>1 ) { if ( vm.gettop()>2 ) {
base = vm.getArgAsInt(1); base = vm.tointeger(3);
} }
return ( (LString) input ).luaToNumber( base ); return ( (LString) input ).luaToNumber( base );
} }
@@ -417,8 +415,8 @@ public class LuaCompat extends LFunction {
} }
private void setfenv( StackState state ) { private void setfenv( StackState state ) {
LValue f = state.getArg(0); LValue f = state.topointer(2);
LValue newenv = state.getArg(1); LValue newenv = state.topointer(3);
Closure c = null; Closure c = null;
@@ -445,11 +443,12 @@ public class LuaCompat extends LFunction {
if ( newenv instanceof LTable ) { if ( newenv instanceof LTable ) {
c.env = (LTable) newenv; c.env = (LTable) newenv;
} }
state.setResult( c ); state.settop(0);
state.pushlvalue( c );
return; return;
} }
state.setResult(); state.settop(0);
return; return;
} }
@@ -476,9 +475,9 @@ public class LuaCompat extends LFunction {
// return true if laoded, false if error put onto the stack // return true if laoded, false if error put onto the stack
private static boolean loadis(VM vm, InputStream is, String chunkname ) { private static boolean loadis(VM vm, InputStream is, String chunkname ) {
try { try {
vm.setResult(); vm.settop(0);
if ( 0 != vm.load(is, chunkname) ) { if ( 0 != vm.load(is, chunkname) ) {
vm.setErrorResult( LNil.NIL, "cannot load "+chunkname+": "+vm.topointer(-1) ); setErrorResult( vm, "cannot load "+chunkname+": "+vm.topointer(-1) );
return false; return false;
} else { } else {
return true; return true;
@@ -498,7 +497,7 @@ public class LuaCompat extends LFunction {
script = fileName; script = fileName;
is = Platform.getInstance().openFile(fileName); is = Platform.getInstance().openFile(fileName);
if ( is == null ) { if ( is == null ) {
vm.setErrorResult( LNil.NIL, "cannot open "+fileName+": No such file or directory" ); setErrorResult( vm, "cannot open "+fileName+": No such file or directory" );
return false; return false;
} }
} else { } else {
@@ -512,10 +511,10 @@ public class LuaCompat extends LFunction {
// if load succeeds, return 0 for success, 1 for error (as per lua spec) // if load succeeds, return 0 for success, 1 for error (as per lua spec)
private void dofile( VM vm ) { private void dofile( VM vm ) {
String filename = vm.getArgAsString(0); String filename = vm.tostring(2);
if ( loadfile( vm, filename ) ) { if ( loadfile( vm, filename ) ) {
int s = vm.pcall(1, 0, 0); int s = vm.pcall(1, 0, 0);
vm.setResult( LInteger.valueOf( s!=0? 1: 0 ) ); setResult( vm, LInteger.valueOf( s!=0? 1: 0 ) );
} else { } else {
vm.error("cannot open "+filename); vm.error("cannot open "+filename);
} }
@@ -539,12 +538,12 @@ public class LuaCompat extends LFunction {
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
try { try {
while ( true ) { while ( true ) {
vm.setResult(c); setResult(vm,c);
if ( 0 != vm.pcall(0, 1, 0) ) { if ( 0 != vm.pcall(0, 1, 0) ) {
vm.setErrorResult(LNil.NIL, vm.getArgAsString(0)); setErrorResult(vm, vm.tostring(2));
return false; return false;
} }
LValue v = vm.getArg(0); LValue v = vm.topointer(2);
if ( v == LNil.NIL ) if ( v == LNil.NIL )
break; break;
LString s = v.luaAsString(); LString s = v.luaAsString();
@@ -557,7 +556,7 @@ public class LuaCompat extends LFunction {
("".equals(chunkname)? "=(load)": chunkname) ); ("".equals(chunkname)? "=(load)": chunkname) );
} catch (IOException ioe) { } catch (IOException ioe) {
vm.setErrorResult(LNil.NIL, ioe.getMessage()); setErrorResult(vm, ioe.getMessage());
return false; return false;
} finally { } finally {
closeSafely( baos ); closeSafely( baos );
@@ -577,17 +576,17 @@ public class LuaCompat extends LFunction {
* By default, i is 1 and j is the length of the list, as defined by the length operator (see §2.5.5). * By default, i is 1 and j is the length of the list, as defined by the length operator (see §2.5.5).
*/ */
private void unpack(VM vm) { private void unpack(VM vm) {
LValue v = vm.getArg(0); LValue v = vm.topointer(2);
int i = vm.getArgAsInt(1); int i = vm.tointeger(3);
int j = vm.getArgAsInt(2); int j = vm.tointeger(4);
LTable list = (LTable) v; LTable list = (LTable) v;
if ( i == 0 ) if ( i == 0 )
i = 1; i = 1;
if ( j == 0 ) if ( j == 0 )
j = list.luaLength(); j = list.luaLength();
vm.setResult(); vm.settop(0);
for ( int k=i; k<=j; k++ ) for ( int k=i; k<=j; k++ )
vm.push( list.get(k) ); vm.pushlvalue( list.get(k) );
} }
private LValue next(VM vm, LValue table, int index) { private LValue next(VM vm, LValue table, int index) {
@@ -628,9 +627,9 @@ public class LuaCompat extends LFunction {
* the module, then require signals an error. * the module, then require signals an error.
*/ */
public static void require( VM vm ) { public static void require( VM vm ) {
LString modname = vm.getArgAsLuaString(0); LString modname = vm.tolstring(2);
if ( LOADED.containsKey(modname) ) if ( LOADED.containsKey(modname) )
vm.setResult( LOADED.get(modname) ); setResult( vm, LOADED.get(modname) );
else { else {
String s = modname.toJavaString(); String s = modname.toJavaString();
if ( ! loadfile(vm, s+".luac") && ! loadfile(vm, s+".lua") ) if ( ! loadfile(vm, s+".luac") && ! loadfile(vm, s+".lua") )
@@ -641,7 +640,7 @@ public class LuaCompat extends LFunction {
LOADED.put(modname, result); LOADED.put(modname, result);
else if ( ! LOADED.containsKey(modname) ) else if ( ! LOADED.containsKey(modname) )
LOADED.put(modname, result = LBoolean.TRUE); LOADED.put(modname, result = LBoolean.TRUE);
vm.setResult( result ); setResult( vm, result );
} }
} }
} }

View File

@@ -58,16 +58,17 @@ public final class LuaJava extends LFunction {
String className; String className;
switch ( id ) { switch ( id ) {
case BINDCLASS: case BINDCLASS:
className = vm.getArgAsString(0); className = vm.tostring(2);
try { try {
Class clazz = Class.forName(className); Class clazz = Class.forName(className);
vm.setResult( new LInstance( clazz, clazz ) ); vm.settop(0);
vm.pushlvalue( new LInstance( clazz, clazz ) );
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
break; break;
case NEWINSTANCE: case NEWINSTANCE:
className = vm.getArgAsString(0); className = vm.tostring(2);
try { try {
// get constructor // get constructor
Class clazz = Class.forName(className); Class clazz = Class.forName(className);
@@ -79,7 +80,8 @@ public final class LuaJava extends LFunction {
Object o = con.newInstance( args ); Object o = con.newInstance( args );
// set the result // set the result
vm.setResult( new LInstance( o, clazz ) ); vm.settop(0);
vm.pushlvalue( new LInstance( o, clazz ) );
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
@@ -96,11 +98,11 @@ public final class LuaJava extends LFunction {
public final Class[] classes; public final Class[] classes;
public int hash; public int hash;
ParamsList( VM vm ) { ParamsList( VM vm ) {
int n = vm.getArgCount()-1; int n = vm.gettop()-2;
values = new LValue[n]; values = new LValue[n];
classes = new Class[n]; classes = new Class[n];
for ( int i=0; i<n; i++ ) { for ( int i=0; i<n; i++ ) {
values[i] = vm.getArg(i+1); values[i] = vm.topointer(i+3);
classes[i] = values[i].getClass(); classes[i] = values[i].getClass();
hash += classes[i].hashCode(); hash += classes[i].hashCode();
} }
@@ -127,9 +129,9 @@ public final class LuaJava extends LFunction {
Field f = clazz.getField(s); Field f = clazz.getField(s);
Object o = f.get(m_instance); Object o = f.get(m_instance);
LValue v = CoerceJavaToLua.coerce( o ); LValue v = CoerceJavaToLua.coerce( o );
vm.push( v ); vm.pushlvalue( v );
} catch (NoSuchFieldException nsfe) { } catch (NoSuchFieldException nsfe) {
vm.push( new LMethod(m_instance,clazz,s) ); vm.pushlvalue( new LMethod(m_instance,clazz,s) );
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@@ -141,7 +143,7 @@ public final class LuaJava extends LFunction {
Field f = c.getField(s); Field f = c.getField(s);
Object v = CoerceLuaToJava.coerceArg(val, f.getType()); Object v = CoerceLuaToJava.coerceArg(val, f.getType());
f.set(m_instance,v); f.set(m_instance,v);
vm.setResult(); vm.settop(0);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@@ -177,7 +179,8 @@ public final class LuaJava extends LFunction {
Object result = meth.invoke( instance, args ); Object result = meth.invoke( instance, args );
// coerce the result // coerce the result
vm.setResult( CoerceJavaToLua.coerce(result) ); vm.settop(0);
vm.pushlvalue( CoerceJavaToLua.coerce(result) );
return false; return false;
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);

View File

@@ -11,17 +11,17 @@ public class LFunction extends LValue {
} }
public void luaSetTable(VM vm, LValue table, LValue key, LValue val) { public void luaSetTable(VM vm, LValue table, LValue key, LValue val) {
vm.push( this ); vm.pushlvalue( this );
vm.push( table ); vm.pushlvalue( table );
vm.push( key ); vm.pushlvalue( key );
vm.push( val ); vm.pushlvalue( val );
vm.call( 3, 0 ); vm.call( 3, 0 );
} }
public void luaGetTable(VM vm, LValue table, LValue key) { public void luaGetTable(VM vm, LValue table, LValue key) {
vm.push( this ); vm.pushlvalue( this );
vm.push( table ); vm.pushlvalue( table );
vm.push( key ); vm.pushlvalue( key );
vm.call( 2, 1 ); vm.call( 2, 1 );
} }

View File

@@ -218,7 +218,7 @@ public class LTable extends LValue {
if ( v == LNil.NIL && m_metatable != null ) { if ( v == LNil.NIL && m_metatable != null ) {
super.luaGetTable( vm, table, key ); super.luaGetTable( vm, table, key );
} else { } else {
vm.push(v); vm.pushlvalue(v);
} }
} }
@@ -282,20 +282,20 @@ public class LTable extends LValue {
// perform a lua call // perform a lua call
public boolean luaStackCall(VM vm) { public boolean luaStackCall(VM vm) {
vm.setResult(); vm.settop(0);
int i; int i;
while ( ( i = arrayIndex++ ) < m_vector.length ) { while ( ( i = arrayIndex++ ) < m_vector.length ) {
if ( m_vector[i] != LNil.NIL ) { if ( m_vector[i] != LNil.NIL ) {
vm.push( LInteger.valueOf( arrayIndex ) ); vm.pushinteger( arrayIndex );
vm.push( m_vector[ i ] ); vm.pushlvalue( m_vector[ i ] );
return false; return false;
} }
} }
if ( isPairs && (m_hashKeys != null) ) { if ( isPairs && (m_hashKeys != null) ) {
while ( ( i = hashIndex++ ) < m_hashKeys.length ) { while ( ( i = hashIndex++ ) < m_hashKeys.length ) {
if ( m_hashKeys[i] != null ) { if ( m_hashKeys[i] != null ) {
vm.push( m_hashKeys[i] ); vm.pushlvalue( m_hashKeys[i] );
vm.push( m_hashValues[i] ); vm.pushlvalue( m_hashValues[i] );
return false; return false;
} }
} }

View File

@@ -100,7 +100,7 @@ public class LValue {
return; return;
} }
} }
vm.push( LNil.NIL ); vm.pushnil();
} }
/** Get a value from a table /** Get a value from a table
@@ -117,7 +117,7 @@ public class LValue {
return; return;
} }
} }
vm.push(LNil.NIL); vm.pushnil();
} }
/** Get the value as a LString /** Get the value as a LString

View File

@@ -21,5 +21,8 @@ print( select(2, "a", "b", "c"))
print((select(3, "a", "b", "c"))) print((select(3, "a", "b", "c")))
print( select(3, "a", "b", "c")) print( select(3, "a", "b", "c"))
print((select(4, "a", "b", "c")))
print( select(4, "a", "b", "c"))
-- f("hello", "world") -- f("hello", "world")
-- g(1, 2, 3, 4, 5, 6, 7) -- g(1, 2, 3, 4, 5, 6, 7)

Binary file not shown.