Make soem methods final, let loadsting use "string" for chunk name

This commit is contained in:
James Roseborough
2009-10-30 01:10:15 +00:00
parent dd82fe6d2d
commit f18134bf74
4 changed files with 24 additions and 23 deletions

View File

@@ -26,7 +26,7 @@ package org.luaj.vm2;
* String buffer for use in string library methods, optimized for production * String buffer for use in string library methods, optimized for production
* of StrValue instances. * of StrValue instances.
*/ */
public class Buffer { public final class Buffer {
private static final int DEFAULT_CAPACITY = 64; private static final int DEFAULT_CAPACITY = 64;
private byte[] bytes; private byte[] bytes;
@@ -36,32 +36,32 @@ public class Buffer {
this(DEFAULT_CAPACITY); this(DEFAULT_CAPACITY);
} }
public String toString() {
return new LuaString(bytes, 0, length).toString();
}
public Buffer( int initialCapacity ) { public Buffer( int initialCapacity ) {
bytes = new byte[ initialCapacity ]; bytes = new byte[ initialCapacity ];
length = 0; length = 0;
} }
public void append( byte b ) { public final String toString() {
return new LuaString(bytes, 0, length).toString();
}
public final void append( byte b ) {
ensureCapacity( length + 1 ); ensureCapacity( length + 1 );
bytes[ length++ ] = b; bytes[ length++ ] = b;
} }
public void append( LuaValue val ) { public final void append( LuaValue val ) {
append( val.strvalue() ); append( val.strvalue() );
} }
public void append( LuaString str ) { public final void append( LuaString str ) {
final int alen = str.length(); final int alen = str.length();
ensureCapacity( length + alen ); ensureCapacity( length + alen );
str.copyInto( 0, bytes, length, alen ); str.copyInto( 0, bytes, length, alen );
length += alen; length += alen;
} }
public void append( String str ) { public final void append( String str ) {
char[] chars = str.toCharArray(); char[] chars = str.toCharArray();
final int alen = LuaString.lengthAsUtf8( chars ); final int alen = LuaString.lengthAsUtf8( chars );
ensureCapacity( length + alen ); ensureCapacity( length + alen );
@@ -69,25 +69,25 @@ public class Buffer {
length += alen; length += alen;
} }
public void setLength( int length ) { public final void setLength( int length ) {
ensureCapacity( length ); ensureCapacity( length );
this.length = length; this.length = length;
} }
public LuaString tostrvalue() { public final LuaString tostrvalue() {
return new LuaString( realloc( bytes, length ) ); return new LuaString( realloc( bytes, length ) );
} }
public void ensureCapacity( int minSize ) { public final void ensureCapacity( int minSize ) {
if ( minSize > bytes.length ) if ( minSize > bytes.length )
realloc( minSize ); realloc( minSize );
} }
private void realloc( int minSize ) { private final void realloc( int minSize ) {
bytes = realloc( bytes, Math.max( bytes.length * 2, minSize ) ); bytes = realloc( bytes, Math.max( bytes.length * 2, minSize ) );
} }
private static byte[] realloc( byte[] b, int newSize ) { private final static byte[] realloc( byte[] b, int newSize ) {
byte[] newBytes = new byte[ newSize ]; byte[] newBytes = new byte[ newSize ];
System.arraycopy( b, 0, newBytes, 0, Math.min( b.length, newSize ) ); System.arraycopy( b, 0, newBytes, 0, Math.min( b.length, newSize ) );
return newBytes; return newBytes;

View File

@@ -177,6 +177,7 @@ public class LuaValue extends Varargs {
public void rawset( String key, double value ) { rawset(valueOf(key),valueOf(value)); } public void rawset( String key, double value ) { rawset(valueOf(key),valueOf(value)); }
public void rawset( String key, int value ) { rawset(valueOf(key),valueOf(value)); } public void rawset( String key, int value ) { rawset(valueOf(key),valueOf(value)); }
public void rawset( String key, String value ) { rawset(valueOf(key),valueOf(value)); } public void rawset( String key, String value ) { rawset(valueOf(key),valueOf(value)); }
public void rawsetlist( int key0, Varargs values ) { for ( int i=0, n=values.narg(); i<n; i++ ) rawset(key0+i,values.arg(i+1)); }
public void presize( int i) { unimplemented("presize"); } public void presize( int i) { unimplemented("presize"); }
public Varargs next(LuaValue index) { unimplemented("next"); return null; } public Varargs next(LuaValue index) { unimplemented("next"); return null; }
public Varargs inext(LuaValue index) { unimplemented("inext"); return null; } public Varargs inext(LuaValue index) { unimplemented("inext"); return null; }
@@ -286,10 +287,10 @@ public class LuaValue extends Varargs {
// for loop helpers // for loop helpers
/** test numeric for loop */ /** test numeric for loop */
public boolean testfor_b(LuaValue limit, boolean stepgtzero) { return stepgtzero? lteq_b(limit): gteq_b(limit); } public boolean testfor_b(LuaValue limit, boolean stepgtzero) { return stepgtzero? lteq_b(limit): gteq_b(limit); }
/** used in for loop only */
public boolean testfor_b(LuaValue limit, LuaValue step) { return step.gt_b(0)? lteq_b(limit): gteq_b(limit); }
/** @deprecated - used in samples only */ /** @deprecated - used in samples only */
public boolean testfor_b(LuaValue limit) { return lteq(limit).toboolean(); } public boolean testfor_b(LuaValue limit) { return lteq(limit).toboolean(); }
/** @deprecated - used in samples only */
public boolean testfor_b(LuaValue limit, LuaValue step) { return step.gt_b(0)? lteq_b(limit): gteq_b(limit); }
/** @deprecated - used in samples only, use add(1) instead */ /** @deprecated - used in samples only, use add(1) instead */
public LuaValue incr() { return add(ONE); } public LuaValue incr() { return add(ONE); }

View File

@@ -23,7 +23,7 @@ package org.luaj.vm2;
/** Upvalue used with Closure formulation */ /** Upvalue used with Closure formulation */
public class UpValue { public final class UpValue {
LuaValue[] array; // initially the stack, becomes a holder LuaValue[] array; // initially the stack, becomes a holder
int index; int index;
@@ -40,15 +40,15 @@ public class UpValue {
return (closed? "-": "+") + array[index]; return (closed? "-": "+") + array[index];
} }
public LuaValue getValue() { public final LuaValue getValue() {
return array[index]; return array[index];
} }
public void setValue( LuaValue value ) { public final void setValue( LuaValue value ) {
array[index] = value; array[index] = value;
} }
public boolean close( int limit ) { public final boolean close( int limit ) {
if ( (!closed) && (index>=limit) ) { if ( (!closed) && (index>=limit) ) {
array = new LuaValue[] { array[index] }; array = new LuaValue[] { array[index] };
index = 0; index = 0;
@@ -58,7 +58,7 @@ public class UpValue {
} }
} }
public boolean isClosed() { public final boolean isClosed() {
return closed; return closed;
} }
} }

View File

@@ -238,8 +238,8 @@ public class BaseLib extends LuaTable implements ResourceFinder {
case 4: // "loadstring", // ( string [,chunkname] ) -> chunk | nil, msg case 4: // "loadstring", // ( string [,chunkname] ) -> chunk | nil, msg
try { try {
LuaString script = args.checkstring(1); LuaString script = args.checkstring(1);
LuaString chunkname = args.optstring(2, script); String chunkname = args.optString(2, "string");
return LoadState.load(script.toInputStream(), chunkname.toString(),LuaThread.getRunningEnv(env)); return LoadState.load(script.toInputStream(),chunkname,LuaThread.getRunningEnv(env));
} catch ( Exception e ) { } catch ( Exception e ) {
return varargsOf(NIL, valueOf(e.getMessage())); return varargsOf(NIL, valueOf(e.getMessage()));
} }