Index all lua strings created from java strings.
This commit is contained in:
@@ -42,7 +42,7 @@ public final class Buffer {
|
||||
}
|
||||
|
||||
public final String toString() {
|
||||
return new LuaString(bytes, 0, length).toString();
|
||||
return LuaString.valueOf(bytes, 0, length).toString();
|
||||
}
|
||||
|
||||
public final void append( byte b ) {
|
||||
@@ -75,7 +75,7 @@ public final class Buffer {
|
||||
}
|
||||
|
||||
public final LuaString tostring() {
|
||||
return new LuaString( realloc( bytes, length ) );
|
||||
return LuaString.valueOf( realloc( bytes, length ) );
|
||||
}
|
||||
|
||||
public final void ensureCapacity( int minSize ) {
|
||||
|
||||
@@ -157,7 +157,7 @@ public class LoadState {
|
||||
return null;
|
||||
byte[] bytes = new byte[size];
|
||||
is.readFully( bytes, 0, size );
|
||||
return new LuaString( bytes, 0, bytes.length - 1 );
|
||||
return LuaString.valueOf( bytes, 0, bytes.length - 1 );
|
||||
}
|
||||
|
||||
public static LuaValue longBitsToLuaNumber( long bits ) {
|
||||
|
||||
@@ -26,6 +26,7 @@ import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.Hashtable;
|
||||
|
||||
import org.luaj.vm2.lib.StringLib;
|
||||
|
||||
@@ -37,58 +38,50 @@ public class LuaString extends LuaValue {
|
||||
public final int m_offset;
|
||||
public final int m_length;
|
||||
|
||||
private static final int STRINGCACHE_POW2 = 10;
|
||||
private static final WeakReference[] stringcache
|
||||
= new WeakReference[1<<STRINGCACHE_POW2];
|
||||
private static final Hashtable index_java = new Hashtable();
|
||||
|
||||
private static class LuaJavaString extends LuaString {
|
||||
private final String string;
|
||||
private LuaJavaString(String value, byte[] bytes) {
|
||||
super(bytes);
|
||||
string = value.intern();
|
||||
}
|
||||
public String toString() {
|
||||
return string;
|
||||
}
|
||||
private final static LuaString index_get(Hashtable indextable, Object key) {
|
||||
WeakReference w = (WeakReference) indextable.get(key);
|
||||
return w!=null? (LuaString) w.get(): null;
|
||||
}
|
||||
|
||||
private final static void index_set(Hashtable indextable, Object key, LuaString value) {
|
||||
indextable.put(key, new WeakReference(value));
|
||||
}
|
||||
|
||||
public static LuaString valueOf(String string) {
|
||||
int h = string.hashCode();
|
||||
int i = h & ((1<<STRINGCACHE_POW2)-1);
|
||||
if ( stringcache[i] != null ) {
|
||||
LuaJavaString s = (LuaJavaString) stringcache[i].get();
|
||||
if ( s != null && s.string == string ) {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
LuaString s = index_get( index_java, string );
|
||||
if ( s != null ) return s;
|
||||
char[] c = string.toCharArray();
|
||||
byte[] b = new byte[lengthAsUtf8(c)];
|
||||
encodeToUtf8(c, b, 0);
|
||||
LuaJavaString s = new LuaJavaString(string,b);
|
||||
stringcache[i] = new WeakReference(s);
|
||||
s = valueOf(b, 0, b.length);
|
||||
index_set( index_java, string, s );
|
||||
return s;
|
||||
}
|
||||
|
||||
public LuaString(byte[] bytes, int offset, int length) {
|
||||
this.m_bytes = bytes;
|
||||
this.m_offset = offset;
|
||||
this.m_length = length;
|
||||
|
||||
public static LuaString valueOf(byte[] bytes, int off, int len) {
|
||||
return new LuaString(bytes, off, len);
|
||||
}
|
||||
|
||||
public LuaString(byte[] bytes) {
|
||||
this.m_bytes = bytes;
|
||||
this.m_offset = 0;
|
||||
this.m_length = bytes.length;
|
||||
}
|
||||
|
||||
public static LuaString valueOf(char[] bytes) {
|
||||
int n = bytes.length;
|
||||
byte[] b = new byte[n];
|
||||
for ( int i=0; i<n; i++ )
|
||||
b[i] = (byte) bytes[i];
|
||||
return new LuaString(b, 0, n);
|
||||
return valueOf(b, 0, n);
|
||||
}
|
||||
|
||||
public static LuaString valueOf(byte[] bytes) {
|
||||
return valueOf(bytes, 0, bytes.length);
|
||||
}
|
||||
|
||||
private LuaString(byte[] bytes, int offset, int length) {
|
||||
this.m_bytes = bytes;
|
||||
this.m_offset = offset;
|
||||
this.m_length = length;
|
||||
}
|
||||
|
||||
public boolean isstring() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -306,9 +306,9 @@ public class LuaValue extends Varargs {
|
||||
public static LuaInteger valueOf(int i) { return LuaInteger.valueOf(i); }
|
||||
public static LuaNumber valueOf(double d) { return LuaDouble.valueOf(d); };
|
||||
public static LuaString valueOf(String s) { return LuaString.valueOf(s); }
|
||||
public static LuaString valueOf(byte[] bytes) { return new LuaString(bytes); }
|
||||
public static LuaString valueOf(byte[] bytes) { return LuaString.valueOf(bytes); }
|
||||
public static LuaString valueOf(byte[] bytes, int off, int len) {
|
||||
return new LuaString(bytes,off,len);
|
||||
return LuaString.valueOf(bytes,off,len);
|
||||
}
|
||||
|
||||
// table initializers
|
||||
|
||||
@@ -398,7 +398,7 @@ public class IoLib extends OneArgFunction {
|
||||
int r;
|
||||
if ( ( r = f.read(b,0,b.length) ) < 0 )
|
||||
return NIL;
|
||||
return valueOf(b, 0, r);
|
||||
return LuaString.valueOf(b, 0, r);
|
||||
}
|
||||
public static LuaValue freaduntil(File f,int delim) throws IOException {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
@@ -415,7 +415,7 @@ public class IoLib extends OneArgFunction {
|
||||
}
|
||||
return ( c < 0 && baos.size() == 0 )?
|
||||
(LuaValue) NIL:
|
||||
(LuaValue) valueOf(baos.toByteArray());
|
||||
(LuaValue) LuaString.valueOf(baos.toByteArray());
|
||||
}
|
||||
public static LuaValue freadline(File f) throws IOException {
|
||||
return freaduntil(f,'\n');
|
||||
|
||||
@@ -127,7 +127,7 @@ public class StringLib extends OneArgFunction {
|
||||
if (c<0 || c>=256) error(a, "invalid value");
|
||||
bytes[i] = (byte) c;
|
||||
}
|
||||
return valueOf( bytes );
|
||||
return LuaString.valueOf( bytes );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -144,7 +144,7 @@ public class StringLib extends OneArgFunction {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
try {
|
||||
DumpState.dump( ((LuaClosure)f).p, baos, true );
|
||||
return valueOf(baos.toByteArray());
|
||||
return LuaString.valueOf(baos.toByteArray());
|
||||
} catch (IOException e) {
|
||||
return error( e.getMessage() );
|
||||
}
|
||||
@@ -625,7 +625,7 @@ public class StringLib extends OneArgFunction {
|
||||
for ( int offset = 0; offset < bytes.length; offset += len ) {
|
||||
s.copyInto( 0, bytes, offset, len );
|
||||
}
|
||||
return valueOf( bytes );
|
||||
return LuaString.valueOf( bytes );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -639,7 +639,7 @@ public class StringLib extends OneArgFunction {
|
||||
byte[] b = new byte[n];
|
||||
for ( int i=0, j=n-1; i<n; i++, j-- )
|
||||
b[j] = (byte) s.luaByte(i);
|
||||
return valueOf( b );
|
||||
return LuaString.valueOf( b );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user