Merge branch 'master' of dice:git/vm

This commit is contained in:
Joel Dice 2007-07-23 07:11:36 -06:00
commit 18387a8591
62 changed files with 1898 additions and 58 deletions

View File

@ -1,6 +1,6 @@
package java.lang;
public final class Byte {
public final class Byte extends Number {
private final byte value;
public Byte(byte value) {
@ -10,4 +10,24 @@ public final class Byte {
public byte byteValue() {
return value;
}
public short shortValue() {
return value;
}
public int intValue() {
return value;
}
public long longValue() {
return value;
}
public float floatValue() {
return (float) value;
}
public double doubleValue() {
return (double) value;
}
}

View File

@ -0,0 +1,11 @@
package java.lang;
public class ClassCastException extends RuntimeException {
public ClassCastException(String message) {
super(message);
}
public ClassCastException() {
super();
}
}

View File

@ -0,0 +1,5 @@
package java.lang;
public abstract class ClassLoader {
}

View File

@ -0,0 +1,15 @@
package java.lang;
public class ClassNotFoundException extends Exception {
public ClassNotFoundException(String message, Throwable cause) {
super(message, cause);
}
public ClassNotFoundException(String message) {
this(message, null);
}
public ClassNotFoundException() {
this(null, null);
}
}

View File

@ -0,0 +1,11 @@
package java.lang;
public class CloneNotSupportedException extends Exception {
public CloneNotSupportedException(String message) {
super(message);
}
public CloneNotSupportedException() {
super();
}
}

View File

@ -7,6 +7,26 @@ public final class Double {
this.value = value;
}
public byte byteValue() {
return (byte) value;
}
public short shortValue() {
return (short) value;
}
public int intValue() {
return (int) value;
}
public long longValue() {
return (long) value;
}
public float floatValue() {
return (float) value;
}
public double doubleValue() {
return value;
}

View File

@ -1,13 +1,33 @@
package java.lang;
public final class Float {
public final class Float extends Number {
private final float value;
public Float(float value) {
this.value = value;
}
public byte byteValue() {
return (byte) value;
}
public short shortValue() {
return (short) value;
}
public int intValue() {
return (int) value;
}
public long longValue() {
return (long) value;
}
public float floatValue() {
return value;
}
public double doubleValue() {
return (double) value;
}
}

View File

@ -0,0 +1,11 @@
package java.lang;
public class IllegalAccessException extends Exception {
public IllegalAccessException(String message) {
super(message);
}
public IllegalAccessException() {
super();
}
}

View File

@ -0,0 +1,19 @@
package java.lang;
public class IllegalArgumentException extends RuntimeException {
public IllegalArgumentException(String message, Throwable cause) {
super(message, cause);
}
public IllegalArgumentException(String message) {
this(message, null);
}
public IllegalArgumentException(Throwable cause) {
this(null, cause);
}
public IllegalArgumentException() {
this(null, null);
}
}

View File

@ -0,0 +1,19 @@
package java.lang;
public class IllegalStateException extends RuntimeException {
public IllegalStateException(String message, Throwable cause) {
super(message, cause);
}
public IllegalStateException(String message) {
this(message, null);
}
public IllegalStateException(Throwable cause) {
this(null, cause);
}
public IllegalStateException() {
this(null, null);
}
}

View File

@ -0,0 +1,19 @@
package java.lang;
public class IndexOutOfBoundsException extends RuntimeException {
public IndexOutOfBoundsException(String message, Throwable cause) {
super(message, cause);
}
public IndexOutOfBoundsException(String message) {
this(message, null);
}
public IndexOutOfBoundsException(Throwable cause) {
this(null, cause);
}
public IndexOutOfBoundsException() {
this(null, null);
}
}

View File

@ -0,0 +1,9 @@
package java.lang;
import java.util.Map;
public class InheritableThreadLocal<T> extends ThreadLocal<T> {
protected T childValue(T parentValue) {
return parentValue;
}
}

View File

@ -0,0 +1,11 @@
package java.lang;
public class InstantiationException extends Exception {
public InstantiationException(String message) {
super(message);
}
public InstantiationException() {
super();
}
}

View File

@ -1,13 +1,33 @@
package java.lang;
public final class Integer {
public final class Integer extends Number {
private final int value;
public Integer(int value) {
this.value = value;
}
public byte byteValue() {
return (byte) value;
}
public short shortValue() {
return (short) value;
}
public int intValue() {
return value;
}
public long longValue() {
return value;
}
public float floatValue() {
return (float) value;
}
public double doubleValue() {
return (double) value;
}
}

View File

@ -0,0 +1,7 @@
package java.lang;
import java.util.Iterator;
public interface Iterable<T> {
public Iterator<T> iterator();
}

View File

@ -1,13 +1,33 @@
package java.lang;
public final class Long {
public final class Long extends Number {
private final long value;
public Long(long value) {
this.value = value;
}
public byte byteValue() {
return (byte) value;
}
public short shortValue() {
return (short) value;
}
public int intValue() {
return (int) value;
}
public long longValue() {
return value;
}
public float floatValue() {
return (float) value;
}
public double doubleValue() {
return (double) value;
}
}

View File

@ -0,0 +1,11 @@
package java.lang;
public class NoClassDefFoundError extends LinkageError {
public NoClassDefFoundError(String message) {
super(message);
}
public NoClassDefFoundError() {
super();
}
}

View File

@ -0,0 +1,11 @@
package java.lang;
public class NoSuchFieldException extends Exception {
public NoSuchFieldException(String message) {
super(message);
}
public NoSuchFieldException() {
super();
}
}

View File

@ -0,0 +1,11 @@
package java.lang;
public class NoSuchMethodException extends Exception {
public NoSuchMethodException(String message) {
super(message);
}
public NoSuchMethodException() {
super();
}
}

View File

@ -0,0 +1,10 @@
package java.lang;
public abstract class Number {
public abstract byte byteValue();
public abstract short shortValue();
public abstract int intValue();
public abstract long longValue();
public abstract float floatValue();
public abstract double doubleValue();
}

View File

@ -0,0 +1,11 @@
package java.lang;
public class NumberFormatException extends IllegalArgumentException {
public NumberFormatException(String message) {
super(message);
}
public NumberFormatException() {
super();
}
}

View File

@ -0,0 +1,17 @@
package java.lang;
public class Runtime {
private static final Runtime instance = new Runtime();
private Runtime() { }
public static Runtime getRuntime() {
return instance;
}
public native void loadLibrary(String name);
public native void gc();
public native void exit(int code);
}

View File

@ -0,0 +1,19 @@
package java.lang;
public class SecurityException extends RuntimeException {
public SecurityException(String message, Throwable cause) {
super(message, cause);
}
public SecurityException(String message) {
this(message, null);
}
public SecurityException(Throwable cause) {
this(null, cause);
}
public SecurityException() {
this(null, null);
}
}

View File

@ -1,13 +1,33 @@
package java.lang;
public final class Short {
public final class Short extends Number {
private final short value;
public Short(short value) {
this.value = value;
}
public byte byteValue() {
return (byte) value;
}
public short shortValue() {
return value;
}
public int intValue() {
return value;
}
public long longValue() {
return value;
}
public float floatValue() {
return (float) value;
}
public double doubleValue() {
return (double) value;
}
}

View File

@ -11,11 +11,19 @@ public abstract class System {
public static native void arraycopy(Object src, int srcOffset, Object dst,
int dstOffset, int length);
public static native void loadLibrary(String name);
public static native String getProperty(String name);
public static native void gc();
public static void loadLibrary(String name) {
Runtime.getRuntime().loadLibrary(name);
}
public static void gc() {
Runtime.getRuntime().gc();
}
public static void exit(int code) {
Runtime.getRuntime().exit(code);
}
public static class Output {
public synchronized native void print(String s);

View File

@ -1,14 +1,31 @@
package java.lang;
import java.util.Map;
import java.util.WeakHashMap;
public class Thread implements Runnable {
private final Runnable task;
private Map<ThreadLocal, Object> locals;
private long peer;
public Thread(Runnable task) {
this.task = task;
}
public synchronized native void start();
public synchronized void start() {
Map<ThreadLocal, Object> map = currentThread().locals;
if (map != null) {
for (Map.Entry<ThreadLocal, Object> e: map.entrySet()) {
if (e.getKey() instanceof InheritableThreadLocal) {
locals().put(e.getKey(), e.getValue());
}
}
}
doStart();
}
private native void doStart();
public void run() {
if (task != null) {
@ -16,6 +33,15 @@ public class Thread implements Runnable {
}
}
public Map<ThreadLocal, Object> locals() {
if (locals == null) {
locals = new WeakHashMap();
}
return locals;
}
public static native Thread currentThread();
public static native void sleep(long milliseconds)
throws InterruptedException;
}

View File

@ -0,0 +1,36 @@
package java.lang;
import java.util.Map;
public class ThreadLocal<T> {
private static final Object Null = new Object();
protected T initialValue() {
return null;
}
public T get() {
Map<ThreadLocal, Object> map = Thread.currentThread().locals();
Object o = map.get(this);
if (o == null) {
o = initialValue();
if (o == null) {
o = Null;
}
map.put(this, o);
}
if (o == Null) {
o = null;
}
return (T) o;
}
public void set(T value) {
Map<ThreadLocal, Object> map = Thread.currentThread().locals();
Object o = value;
if (o == null) {
o = Null;
}
map.put(this, o);
}
}

View File

@ -23,6 +23,10 @@ public class Throwable {
this(null, null);
}
public Throwable getCause() {
return cause;
}
private static native Object trace(int skipCount);
private static native StackTraceElement[] resolveTrace(Object trace);

View File

@ -0,0 +1,19 @@
package java.lang;
public class UnsupportedOperationException extends RuntimeException {
public UnsupportedOperationException(String message, Throwable cause) {
super(message, cause);
}
public UnsupportedOperationException(String message) {
this(message, null);
}
public UnsupportedOperationException(Throwable cause) {
this(null, cause);
}
public UnsupportedOperationException() {
this(null, null);
}
}

View File

@ -1,9 +1,9 @@
package java.lang.ref;
public abstract class Reference<T> {
private Object vmNext;
private T target;
private ReferenceQueue<? super T> queue;
private Object vmNext;
Reference next;
protected Reference(T target, ReferenceQueue<? super T> queue) {

View File

@ -0,0 +1,9 @@
package java.lang.reflect;
public abstract class AccessibleObject {
protected static final int Accessible = 1 << 0;
public abstract boolean isAccessible();
public abstract void setAccessible(boolean v);
}

View File

@ -0,0 +1,7 @@
package java.lang.reflect;
public final class Array {
private Array() { }
}

View File

@ -0,0 +1,32 @@
package java.lang.reflect;
public class Constructor<T> extends AccessibleObject implements Member {
private Method<T> method;
private Constructor() { }
public boolean equals(Object o) {
return o instanceof Constructor
&& ((Constructor) o).method.equals(method);
}
public boolean isAccessible() {
return method.isAccessible();
}
public void setAccessible(boolean v) {
method.setAccessible(v);
}
public Class<T> getDeclaringClass() {
return method.getDeclaringClass();
}
public int getModifiers() {
return method.getModifiers();
}
public String getName() {
return method.getName();
}
}

View File

@ -0,0 +1,33 @@
package java.lang.reflect;
public class Field<T> extends AccessibleObject {
private byte vmFlags;
private byte code;
private short flags;
private short offset;
private byte[] name;
private byte[] spec;
private Class<T> class_;
private Field() { }
public boolean isAccessible() {
return (vmFlags & Accessible) != 0;
}
public void setAccessible(boolean v) {
if (v) vmFlags |= Accessible; else vmFlags &= ~Accessible;
}
public Class<T> getDeclaringClass() {
return class_;
}
public int getModifiers() {
return flags;
}
public String getName() {
return new String(name, 0, name.length - 1, false);
}
}

View File

@ -0,0 +1,19 @@
package java.lang.reflect;
public class InvocationTargetException extends Exception {
public InvocationTargetException(Throwable targetException, String message) {
super(message, targetException);
}
public InvocationTargetException(Throwable targetException) {
this(targetException, null);
}
public InvocationTargetException() {
this(null, null);
}
public Throwable getTargetException() {
return getCause();
}
}

View File

@ -0,0 +1,12 @@
package java.lang.reflect;
public interface Member {
public static final int PUBLIC = 0;
public static final int DECLARED = 1;
public Class getDeclaringClass();
public int getModifiers();
public String getName();
}

View File

@ -0,0 +1,35 @@
package java.lang.reflect;
public class Method<T> extends AccessibleObject implements Member {
private byte vmFlags;
private byte parameterCount;
private short parameterFootprint;
private short flags;
private short offset;
private byte[] name;
private byte[] spec;
private Class<T> class_;
private Object code;
private Method() { }
public boolean isAccessible() {
return (vmFlags & Accessible) != 0;
}
public void setAccessible(boolean v) {
if (v) vmFlags |= Accessible; else vmFlags &= ~Accessible;
}
public Class<T> getDeclaringClass() {
return class_;
}
public int getModifiers() {
return flags;
}
public String getName() {
return new String(name, 0, name.length - 1, false);
}
}

View File

@ -0,0 +1,19 @@
package java.lang.reflect;
public final class Modifier {
public static final int PUBLIC = 1 << 0;
public static final int PRIVATE = 1 << 1;
public static final int PROTECTED = 1 << 2;
public static final int STATIC = 1 << 3;
public static final int FINAL = 1 << 4;
public static final int SUPER = 1 << 5;
public static final int SYNCHRONIZED = SUPER;
public static final int VOLATILE = 1 << 6;
public static final int TRANSIENT = 1 << 7;
public static final int NATIVE = 1 << 8;
public static final int INTERFACE = 1 << 9;
public static final int ABSTRACT = 1 << 10;
public static final int STRICT = 1 << 11;
private Modifier() { }
}

View File

@ -0,0 +1,95 @@
package java.util;
public class ArrayList<T> implements List<T> {
private Object[] array;
private int size;
public ArrayList(int capacity) {
if (capacity != 0) {
array = new Object[capacity];
}
}
public ArrayList() {
this(0);
}
private void resize() {
if (array == null || size >= array.length - 1) {
resize(array == null ? 16 : array.length * 2);
} else if (size <= array.length / 3) {
resize(array.length / 2);
}
}
private void resize(int capacity) {
Object[] newArray = null;
if (capacity != 0) {
if (array != null && array.length == capacity) {
return;
}
newArray = new Object[capacity];
if (array != null) {
System.arraycopy(array, 0, newArray, 0, size);
}
}
array = newArray;
}
private static boolean equal(Object a, Object b) {
return (a == null && b == null) || (a != null && a.equals(b));
}
public int size() {
return size;
}
public boolean add(T element) {
resize();
array[size++] = element;
return true;
}
public T get(int index) {
if (index >= 0 && index < size) {
return (T) array[index];
} else {
throw new IndexOutOfBoundsException(index + " not in [0, " + size + ")");
}
}
public T remove(int index) {
T v = get(index);
if (index == size - 1) {
array[index] = null;
} else {
System.arraycopy(array, index + 1, array, index, size - index);
}
-- size;
resize();
return v;
}
public boolean remove(T element) {
for (int i = 0; i < size; ++i) {
if (equal(element, array[i])) {
remove(i);
return true;
}
}
return false;
}
public void clear() {
array = null;
size = 0;
}
public Iterator<T> iterator() {
return new Collections.ArrayListIterator(this);
}
}

View File

@ -0,0 +1,37 @@
package java.util;
public class Arrays {
private Arrays() { }
public static <T> List<T> asList(final T ... array) {
return new List<T>() {
public int size() {
return array.length;
}
public boolean add(T element) {
throw new UnsupportedOperationException();
}
public T get(int index) {
return array[index];
}
public T remove(int index) {
throw new UnsupportedOperationException();
}
public boolean remove(T element) {
throw new UnsupportedOperationException();
}
public void clear() {
throw new UnsupportedOperationException();
}
public Iterator<T> iterator() {
return new Collections.ArrayListIterator(this);
}
};
}
}

View File

@ -0,0 +1,11 @@
package java.util;
public interface Collection<T> extends Iterable<T> {
public int size();
public boolean add(T element);
public boolean remove(T element);
public void clear();
}

View File

@ -0,0 +1,114 @@
package java.util;
public class Collections {
private Collections() { }
static class IteratorEnumeration<T> implements Enumeration<T> {
private final Iterator<T> it;
public IteratorEnumeration(Iterator<T> it) {
this.it = it;
}
public T nextElement() {
return it.next();
}
public boolean hasMoreElements() {
return it.hasNext();
}
}
static class SynchronizedCollection<T> implements Collection<T> {
private final Object lock;
private final Collection<T> collection;
public SynchronizedCollection(Object lock, Collection<T> collection) {
this.lock = lock;
this.collection = collection;
}
public synchronized int size() {
synchronized (lock) { return collection.size(); }
}
public synchronized boolean add(T e) {
synchronized (lock) { return collection.add(e); }
}
public synchronized boolean remove(T e) {
synchronized (lock) { return collection.remove(e); }
}
public synchronized void clear() {
synchronized (lock) { collection.clear(); }
}
public Iterator<T> iterator() {
return new SynchronizedIterator(lock, collection.iterator());
}
}
static class SynchronizedSet<T>
extends SynchronizedCollection<T>
implements Set<T>
{
public SynchronizedSet(Object lock, Set<T> set) {
super(lock, set);
}
}
static class SynchronizedIterator<T> implements Iterator<T> {
private final Object lock;
private final Iterator<T> it;
public SynchronizedIterator(Object lock, Iterator<T> it) {
this.lock = lock;
this.it = it;
}
public T next() {
synchronized (lock) { return it.next(); }
}
public boolean hasNext() {
synchronized (lock) { return it.hasNext(); }
}
public void remove() {
synchronized (lock) { it.remove(); }
}
}
static class ArrayListIterator<T> implements Iterator<T> {
private final List<T> list;
private boolean canRemove = false;
private int index = -1;
public ArrayListIterator(List<T> list) {
this.list = list;
}
public T next() {
if (hasNext()) {
canRemove = true;
return list.get(++index);
} else {
throw new NoSuchElementException();
}
}
public boolean hasNext() {
return index + 1 < list.size();
}
public void remove() {
if (canRemove) {
canRemove = false;
list.remove(index--);
} else {
throw new IllegalStateException();
}
}
}
}

View File

@ -0,0 +1,7 @@
package java.util;
public interface Enumeration<T> {
public T nextElement();
public boolean hasMoreElements();
}

View File

@ -0,0 +1,416 @@
package java.util;
public class HashMap<K, V> implements Map<K, V> {
private int size;
private Cell[] array;
private Cell<K, V> nullCell;
private final CellFactory factory;
HashMap(int capacity, CellFactory<K, V> factory) {
if (capacity > 0) {
array = new Cell[nextPowerOfTwo(capacity)];
}
this.factory = factory;
}
public HashMap(int capacity) {
this(capacity, new MyCellFactory());
}
public HashMap() {
this(0);
}
private static int nextPowerOfTwo(int n) {
int r = 1;
while (r < n) r <<= 1;
return r;
}
public int size() {
return size;
}
private void resize() {
if (array == null || size >= array.length * 2) {
resize(array == null ? 16 : array.length * 2);
} else if (size <= array.length / 3) {
resize(array.length / 2);
}
}
private void resize(int capacity) {
Cell<K, V>[] newArray = null;
if (capacity != 0) {
capacity = nextPowerOfTwo(capacity);
if (array != null && array.length == capacity) {
return;
}
newArray = new Cell[capacity];
if (array != null) {
for (int i = 0; i < array.length; ++i) {
Cell<K, V> next;
for (Cell<K, V> c = array[i]; c != null; c = next) {
next = c.next();
int index = c.getKey().hashCode() & (capacity - 1);
c.setNext(array[index]);
array[index] = c;
}
}
}
}
array = newArray;
}
private Cell<K, V> find(K key) {
if (key == null) {
return nullCell;
} else {
if (array != null) {
int index = key.hashCode() & (array.length - 1);
for (Cell<K, V> c = array[index]; c != null; c = c.next()) {
if (key.equals(c.getKey())) {
return c;
}
}
}
return null;
}
}
private void insert(Cell<K, V> cell) {
++ size;
if (cell.getKey() == null) {
nullCell = cell;
} else {
resize();
int index = cell.hashCode() & (array.length - 1);
cell.setNext(array[index]);
array[index] = cell;
}
}
// primarily for use by WeakHashMap:
void remove(Cell<K, V> cell) {
if (cell == nullCell) {
nullCell = null;
-- size;
} else {
int index = cell.hashCode() & (array.length - 1);
Cell<K, V> p = null;
for (Cell<K, V> c = array[index]; c != null; c = c.next()) {
if (c == cell) {
if (p == null) {
array[index] = c.next();
} else {
p.setNext(c.next());
}
-- size;
break;
}
}
resize();
}
}
private Cell<K, V> putCell(K key, V value) {
Cell<K, V> c = find(key);
if (c == null) {
insert(factory.make(key, value, null));
} else {
V old = c.getValue();
c.setValue(value);
}
return c;
}
public V get(K key) {
Cell<K, V> c = find(key);
return (c == null ? null : c.getValue());
}
public Cell<K, V> removeCell(K key) {
Cell<K, V> old = null;
if (key == null) {
if (nullCell != null) {
old = nullCell;
nullCell = null;
-- size;
}
} else {
if (array != null) {
int index = key.hashCode() & (array.length - 1);
Cell<K, V> p = null;
for (Cell<K, V> c = array[index]; c != null; c = c.next()) {
if (key.equals(c.getKey())) {
old = c;
if (p == null) {
array[index] = c.next();
} else {
p.setNext(c.next());
}
-- size;
break;
}
}
resize();
}
}
return old;
}
public V put(K key, V value) {
Cell<K, V> c = putCell(key, value);
return (c == null ? null : c.getValue());
}
public V remove(K key) {
Cell<K, V> c = removeCell(key);
return (c == null ? null : c.getValue());
}
public void clear() {
array = null;
size = 0;
}
public Set<Entry<K, V>> entrySet() {
return new EntrySet();
}
public Set<K> keySet() {
return new KeySet();
}
public Collection<V> values() {
return new Values();
}
Iterator<Entry<K, V>> iterator() {
return new MyIterator();
}
interface Cell<K, V> extends Entry<K, V> {
public HashMap.Cell<K, V> next();
public void setNext(HashMap.Cell<K, V> next);
}
interface CellFactory<K, V> {
public Cell<K, V> make(K key, V value, Cell<K, V> next);
}
private static class MyCell<K, V> implements Cell<K, V> {
public final K key;
public V value;
public Cell<K, V> next;
public MyCell(K key, V value, Cell<K, V> next) {
this.key = key;
this.value = value;
this.next = next;
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
public void setValue(V value) {
this.value = value;
}
public HashMap.Cell<K, V> next() {
return next;
}
public void setNext(HashMap.Cell<K, V> next) {
this.next = next;
}
public int hashCode() {
return key.hashCode();
}
}
private static class MyCellFactory<K, V> implements CellFactory<K, V> {
public Cell<K, V> make(K key, V value, Cell<K, V> next) {
return new MyCell(key, value, next);
}
}
private class EntrySet implements Set<Entry<K, V>> {
public int size() {
return HashMap.this.size();
}
public boolean add(Entry<K, V> e) {
return putCell(e.getKey(), e.getValue()) != null;
}
public boolean remove(Entry<K, V> e) {
return removeCell(e.getKey()) != null;
}
public void clear() {
HashMap.this.clear();
}
public Iterator<Entry<K, V>> iterator() {
return new MyIterator();
}
}
private class KeySet implements Set<K> {
public int size() {
return HashMap.this.size();
}
public boolean add(K key) {
return putCell(key, null) != null;
}
public boolean remove(K key) {
return removeCell(key) != null;
}
public void clear() {
HashMap.this.clear();
}
public Iterator<K> iterator() {
return new KeyIterator(new MyIterator());
}
}
private class Values implements Collection<V> {
public int size() {
return HashMap.this.size();
}
public boolean add(V value) {
throw new UnsupportedOperationException();
}
public boolean remove(V value) {
throw new UnsupportedOperationException();
}
public void clear() {
HashMap.this.clear();
}
public Iterator<V> iterator() {
return new ValueIterator(new MyIterator());
}
}
private class MyIterator implements Iterator<Entry<K, V>> {
private int currentIndex = -1;
private int nextIndex = -1;
private Cell<K, V> previousCell;
private Cell<K, V> currentCell;
private Cell<K, V> nextCell;
public MyIterator() {
hasNext();
}
public Entry<K, V> next() {
if (hasNext()) {
if (currentCell != null && currentCell.next() != null) {
previousCell = currentCell;
} else {
previousCell = null;
}
currentCell = nextCell;
currentIndex = nextIndex;
nextCell = nextCell.next();
return currentCell;
} else {
throw new NoSuchElementException();
}
}
public boolean hasNext() {
if (array != null) {
while (nextCell == null && ++ nextIndex < array.length) {
if (array[nextIndex] != null) {
nextCell = array[nextIndex];
return true;
}
}
}
return false;
}
public void remove() {
if (currentCell != null) {
if (previousCell == null) {
array[currentIndex] = currentCell.next();
} else {
previousCell.setNext(currentCell.next());
if (previousCell.next() == null) {
previousCell = null;
}
}
currentCell = null;
} else {
throw new IllegalStateException();
}
}
}
private static class KeyIterator<K, V> implements Iterator<K> {
private final Iterator<Entry<K, V>> it;
public KeyIterator(Iterator<Entry<K, V>> it) {
this.it = it;
}
public K next() {
return it.next().getKey();
}
public boolean hasNext() {
return it.hasNext();
}
public void remove() {
it.remove();
}
}
private static class ValueIterator<K, V> implements Iterator<V> {
private final Iterator<Entry<K, V>> it;
public ValueIterator(Iterator<Entry<K, V>> it) {
this.it = it;
}
public V next() {
return it.next().getValue();
}
public boolean hasNext() {
return it.hasNext();
}
public void remove() {
it.remove();
}
}
}

View File

@ -0,0 +1,55 @@
package java.util;
public class HashSet<T> implements Set<T> {
private static final Object Value = new Object();
private final HashMap<T, Object> map;
public HashSet(int capacity) {
map = new HashMap(capacity);
}
public HashSet() {
this(0);
}
public int size() {
return map.size();
}
public boolean add(T element) {
return map.put(element, Value) != Value;
}
public boolean remove(T element) {
return map.remove(element) != Value;
}
public void clear() {
map.clear();
}
public Iterator<T> iterator() {
return new MyIterator(map.iterator());
}
private static class MyIterator<T> implements Iterator<T> {
private final Iterator<Map.Entry<T, Object>> it;
public MyIterator(Iterator<Map.Entry<T, Object>> it) {
this.it = it;
}
public T next() {
return it.next().getKey();
}
public boolean hasNext() {
return it.hasNext();
}
public void remove() {
it.remove();
}
}
}

View File

@ -0,0 +1,54 @@
package java.util;
public class Hashtable<K, V> implements Map<K, V> {
private final HashMap<K, V> map;
public Hashtable(int capacity) {
map = new HashMap(capacity);
}
public Hashtable() {
this(0);
}
public synchronized int size() {
return map.size();
}
public synchronized V get(K key) {
return map.get(key);
}
public synchronized V put(K key, V value) {
return map.put(key, value);
}
public synchronized V remove(K key) {
return map.remove(key);
}
public synchronized void clear() {
map.clear();
}
public Enumeration<K> keys() {
return new Collections.IteratorEnumeration(keySet().iterator());
}
public Enumeration<V> elements() {
return new Collections.IteratorEnumeration(values().iterator());
}
public Set<Entry<K, V>> entrySet() {
return new Collections.SynchronizedSet(this, map.entrySet());
}
public Set<K> keySet() {
return new Collections.SynchronizedSet(this, map.keySet());
}
public Collection<V> values() {
return new Collections.SynchronizedCollection(this, map.values());
}
}

View File

@ -0,0 +1,9 @@
package java.util;
public interface Iterator<T> {
public T next();
public boolean hasNext();
public void remove();
}

View File

@ -0,0 +1,140 @@
package java.util;
public class LinkedList<T> implements List<T> {
private Cell<T> front;
private Cell<T> rear;
private int size;
private Cell<T> find(int index) {
int i = 0;
for (Cell<T> c = front; c != null; c = c.next) {
if (i == index) {
return c;
}
++ i;
}
throw new IndexOutOfBoundsException(index + " not in [0, " + size + ")");
}
private static boolean equal(Object a, Object b) {
return (a == null && b == null) || (a != null && a.equals(b));
}
private void add(Cell<T> c) {
++ size;
if (front == null) {
front = rear = c;
} else {
c.prev = rear;
rear = c;
}
}
private Cell<T> find(T element) {
for (Cell<T> c = front; c != null; c = c.next) {
if (equal(c.value, element)) {
return c;
}
}
return null;
}
private void remove(Cell<T> c) {
-- size;
if (c.prev == null) {
front = c.next;
} else {
c.prev.next = c.next;
}
if (c.next == null) {
rear = c.prev;
} else {
c.next.prev = c.prev;
}
}
public int size() {
return size;
}
public boolean add(T element) {
add(new Cell(element, null, null));
return true;
}
public T get(int index) {
return find(index).value;
}
public T remove(int index) {
Cell<T> c = find(index);
remove(c);
return c.value;
}
public boolean remove(T element) {
Cell<T> c = find(element);
if (c == null) {
return false;
} else {
remove(c);
return true;
}
}
public void clear() {
front = rear = null;
size = 0;
}
public Iterator<T> iterator() {
return new MyIterator(front);
}
private static class Cell<T> {
public T value;
public Cell<T> prev;
public Cell<T> next;
public Cell(T value, Cell<T> prev, Cell<T> next) {
this.value = value;
this.prev = prev;
this.next = next;
}
}
private class MyIterator implements Iterator<T> {
private Cell<T> current;
private Cell<T> next;
public MyIterator(Cell<T> start) {
next = start;
}
public T next() {
if (hasNext()) {
current = next;
next = next.next;
return current.value;
} else {
throw new NoSuchElementException();
}
}
public boolean hasNext() {
return next != null;
}
public void remove() {
if (current != null) {
LinkedList.this.remove(current);
current = null;
} else {
throw new IllegalStateException();
}
}
}
}

View File

@ -0,0 +1,7 @@
package java.util;
public interface List<T> extends Collection<T> {
public T get(int index);
public T remove(int index);
}

View File

@ -0,0 +1,27 @@
package java.util;
public interface Map<K, V> {
public int size();
public V get(K key);
public V put(K key, V value);
public V remove(K key);
public void clear();
public Set<Entry<K, V>> entrySet();
public Set<K> keySet();
public Collection<V> values();
public interface Entry<K, V> {
public K getKey();
public V getValue();
public void setValue(V value);
}
}

View File

@ -0,0 +1,11 @@
package java.util;
public class NoSuchElementException extends RuntimeException {
public NoSuchElementException(String message) {
super(message);
}
public NoSuchElementException() {
super();
}
}

View File

@ -0,0 +1,3 @@
package java.util;
public interface Set<T> extends Collection<T> { }

View File

@ -0,0 +1,20 @@
package java.util;
public class Stack<T> extends Vector<T> {
public boolean empty() {
return size() != 0;
}
public T peek() {
return get(size() - 1);
}
public T pop() {
return remove(size() - 1);
}
public T push(T element) {
add(element);
return element;
}
}

View File

@ -0,0 +1,54 @@
package java.util;
public class Vector<T> implements List<T> {
private final ArrayList<T> list;
public Vector(int capacity) {
list = new ArrayList(capacity);
}
public Vector() {
this(0);
}
public synchronized int size() {
return list.size();
}
public synchronized boolean add(T element) {
return list.add(element);
}
public void addElement(T element) {
add(element);
}
public synchronized T get(int index) {
return list.get(index);
}
public T elementAt(int index) {
return get(index);
}
public synchronized T remove(int index) {
return list.remove(index);
}
public synchronized boolean remove(T element) {
return list.remove(element);
}
public synchronized void clear() {
list.clear();
}
public Iterator<T> iterator() {
return new Collections.ArrayListIterator(this);
}
public Enumeration<T> elements() {
return new Collections.IteratorEnumeration(iterator());
}
}

View File

@ -0,0 +1,103 @@
package java.util;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
public class WeakHashMap<K, V> implements Map<K, V> {
private final HashMap<K, V> map;
private final ReferenceQueue queue;
public WeakHashMap(int capacity) {
map = new HashMap(capacity, new MyCellFactory());
queue = new ReferenceQueue();
}
public WeakHashMap() {
this(0);
}
private void poll() {
for (MyCell<K, V> c = (MyCell<K, V>) queue.poll();
c != null;
c = (MyCell<K, V>) queue.poll())
{
map.remove(c);
}
}
public int size() {
return map.size();
}
public V get(K key) {
poll();
return map.get(key);
}
public V put(K key, V value) {
poll();
return map.put(key, value);
}
public V remove(K key) {
poll();
return map.remove(key);
}
public void clear() {
map.clear();
}
public Set<Entry<K, V>> entrySet() {
return map.entrySet();
}
private static class MyCell<K, V>
extends WeakReference<K>
implements HashMap.Cell<K, V>
{
public V value;
public HashMap.Cell<K, V> next;
public int hashCode;
public MyCell(K key, V value, HashMap.Cell<K, V> next) {
super(key);
this.value = value;
this.next = next;
this.hashCode = key.hashCode();
}
public K getKey() {
return get();
}
public V getValue() {
return value;
}
public void setValue(V value) {
this.value = value;
}
public HashMap.Cell<K, V> next() {
return next;
}
public void setNext(HashMap.Cell<K, V> next) {
this.next = next;
}
public int hashCode() {
return hashCode;
}
}
private static class MyCellFactory<K, V>
implements HashMap.CellFactory<K, V>
{
public HashMap.Cell<K, V> make(K key, V value, HashMap.Cell<K, V> next) {
return new MyCell(key, value, next);
}
}
}

View File

@ -1,4 +1,4 @@
#MAKEFLAGS = -s
MAKEFLAGS = -s
arch = $(shell uname -m)
ifeq ($(arch),i586)

View File

@ -41,6 +41,12 @@ notifyAll(Thread* t, jobject this_)
vm::notifyAll(t, *this_);
}
jobject
currentThread(Thread* t)
{
return pushReference(t, t->javaThread);
}
void
sleep(Thread* t, jlong milliseconds)
{
@ -51,28 +57,6 @@ sleep(Thread* t, jlong milliseconds)
t->vm->system->sleep(milliseconds);
}
void
loadLibrary(Thread* t, jstring nameString)
{
if (LIKELY(nameString)) {
object n = *nameString;
char name[stringLength(t, n) + 1];
stringChars(t, n, name);
System::Library* lib;
if (LIKELY(t->vm->system->success
(t->vm->system->load(&lib, name, t->vm->libraries))))
{
t->vm->libraries = lib;
} else {
object message = makeString(t, "library not found: %s", name);
t->exception = makeRuntimeException(t, message);
}
} else {
t->exception = makeNullPointerException(t);
}
}
void
arraycopy(Thread* t, jobject src, jint srcOffset, jobject dst, jint dstOffset,
jint length)
@ -92,9 +76,15 @@ arraycopy(Thread* t, jobject src, jint srcOffset, jobject dst, jint dstOffset,
{
uint8_t* sbody = &cast<uint8_t>(s, 2 * BytesPerWord);
uint8_t* dbody = &cast<uint8_t>(d, 2 * BytesPerWord);
memcpy(dbody + (dstOffset * elementSize),
sbody + (srcOffset * elementSize),
length * elementSize);
if (src == dst) {
memmove(dbody + (dstOffset * elementSize),
sbody + (srcOffset * elementSize),
length * elementSize);
} else {
memcpy(dbody + (dstOffset * elementSize),
sbody + (srcOffset * elementSize),
length * elementSize);
}
return;
}
}
@ -108,13 +98,48 @@ arraycopy(Thread* t, jobject src, jint srcOffset, jobject dst, jint dstOffset,
}
void
gc(Thread* t)
loadLibrary(Thread* t, jobject, jstring nameString)
{
if (LIKELY(nameString)) {
object n = *nameString;
char name[stringLength(t, n) + 1];
stringChars(t, n, name);
for (System::Library* lib = t->vm->libraries; lib; lib = lib->next()) {
if (::strcmp(lib->name(), name) == 0) {
// already loaded
return;
}
}
System::Library* lib;
if (LIKELY(t->vm->system->success
(t->vm->system->load(&lib, name, t->vm->libraries))))
{
t->vm->libraries = lib;
} else {
object message = makeString(t, "library not found: %s", name);
t->exception = makeRuntimeException(t, message);
}
} else {
t->exception = makeNullPointerException(t);
}
}
void
gc(Thread* t, jobject)
{
ENTER(t, Thread::ExclusiveState);
collect(t, Heap::MajorCollection);
}
void
exit(Thread* t, jobject, jint code)
{
t->vm->system->exit(code);
}
jobject
trace(Thread* t, jint skipCount)
{
@ -223,13 +248,18 @@ populate(Thread* t, object map)
} builtins[] = {
{ "Java_java_lang_System_arraycopy",
reinterpret_cast<void*>(arraycopy) },
{ "Java_java_lang_System_loadLibrary",
reinterpret_cast<void*>(loadLibrary) },
{ "Java_java_lang_System_gc",
reinterpret_cast<void*>(gc) },
{ "Java_java_lang_Thread_start",
{ "Java_java_lang_Runtime_loadLibrary",
reinterpret_cast<void*>(loadLibrary) },
{ "Java_java_lang_Runtime_gc",
reinterpret_cast<void*>(gc) },
{ "Java_java_lang_Runtiime_exit",
reinterpret_cast<void*>(exit) },
{ "Java_java_lang_Thread_doStart",
reinterpret_cast<void*>(start) },
{ "Java_java_lang_Thread_currentThread",
reinterpret_cast<void*>(currentThread) },
{ "Java_java_lang_Thread_sleep",
reinterpret_cast<void*>(sleep) },

View File

@ -139,6 +139,9 @@ visitRoots(Thread* t, Heap::Visitor* v)
void
referenceTargetUnreachable(Thread* t, object* p, Heap::Visitor* v)
{
// fprintf(stderr, "target %p unreachable for reference %p\n",
// jreferenceTarget(t, *p), *p);
v->visit(p);
jreferenceTarget(t, *p) = 0;
@ -166,6 +169,9 @@ referenceTargetUnreachable(Thread* t, object* p, Heap::Visitor* v)
void
referenceUnreachable(Thread* t, object* p, Heap::Visitor* v)
{
// fprintf(stderr, "reference %p unreachable\n",
// *p);
if (jreferenceQueue(t, *p)
and t->vm->heap->status(jreferenceQueue(t, *p)) != Heap::Unreachable)
{
@ -177,6 +183,9 @@ referenceUnreachable(Thread* t, object* p, Heap::Visitor* v)
void
referenceTargetReachable(Thread* t, object* p, Heap::Visitor* v)
{
// fprintf(stderr, "target %p reachable for reference %p\n",
// jreferenceTarget(t, *p), *p);
v->visit(p);
v->visit(&jreferenceTarget(t, *p));
@ -711,9 +720,10 @@ parseFieldTable(Thread* t, Stream& s, object class_, object pool)
object field = makeField
(t,
0, // vm flags
fieldCode(t, byteArrayBody(t, arrayBody(t, pool, spec - 1), 0)),
flags,
0, // offset
fieldCode(t, byteArrayBody(t, arrayBody(t, pool, spec - 1), 0)),
arrayBody(t, pool, name - 1),
arrayBody(t, pool, spec - 1),
class_);
@ -914,10 +924,11 @@ parseMethodTable(Thread* t, Stream& s, object class_, object pool)
}
object method = makeMethod(t,
flags,
0, // offset
0, // vm flags
parameterCount,
parameterFootprint,
flags,
0, // offset
arrayBody(t, pool, name - 1),
arrayBody(t, pool, spec - 1),
class_,
@ -1365,7 +1376,7 @@ Thread::Thread(Machine* m, object javaThread, Thread* parent):
builtin::populate(t, m->builtinMap);
javaThread = makeThread(t, 0, reinterpret_cast<int64_t>(t));
javaThread = makeThread(t, 0, 0, reinterpret_cast<int64_t>(t));
} else {
threadPeer(this, javaThread) = reinterpret_cast<jlong>(this);
parent->child = this;
@ -1730,6 +1741,10 @@ hashMapResize(Thread* t, object map, uint32_t (*hash)(Thread*, object),
PROTECT(t, oldArray);
unsigned newLength = nextPowerOfTwo(size);
if (oldArray and arrayLength(t, oldArray) == newLength) {
return;
}
newArray = makeArray(t, newLength, true);
if (oldArray) {
@ -1783,7 +1798,7 @@ hashMapInsert(Thread* t, object map, object key, object value,
PROTECT(t, value);
t->vm->weakReferences = makeWeakReference
(t, 0, 0, t->vm->weakReferences, 0);
(t, t->vm->weakReferences, 0, 0, 0);
jreferenceTarget(t, t->vm->weakReferences) = key;
key = t->vm->weakReferences;
}
@ -1813,6 +1828,7 @@ hashMapRemove(Thread* t, object map, object key,
o = tripleSecond(t, *n);
set(t, *n, tripleThird(t, *n));
-- hashMapSize(t, map);
break;
} else {
n = &tripleThird(t, *n);
}

View File

@ -91,8 +91,8 @@ make(Thread* t, object class_)
ACQUIRE(t, t->vm->referenceLock);
// jreferenceNext(t, r)
cast<object>(instance, 3 * BytesPerWord) = t->vm->weakReferences;
// jreferenceNext(t, instance)
cast<object>(instance, BytesPerWord) = t->vm->weakReferences;
t->vm->weakReferences = instance;
}

View File

@ -236,9 +236,10 @@ class MySystem: public System {
class Library: public System::Library {
public:
Library(System* s, void* p, System::Library* next):
Library(System* s, void* p, const char* name, System::Library* next):
s(s),
p(p),
name_(name),
next_(next)
{ }
@ -246,6 +247,10 @@ class MySystem: public System {
return dlsym(p, function);
}
virtual const char* name() {
return name_;
}
virtual System::Library* next() {
return next_;
}
@ -260,11 +265,14 @@ class MySystem: public System {
if (next_) {
next_->dispose();
}
s->free(name_);
s->free(this);
}
System* s;
void* p;
const char* name_;
System::Library* next_;
};
@ -359,7 +367,8 @@ class MySystem: public System {
const char* name,
System::Library* next)
{
unsigned size = strlen(name) + 7;
unsigned nameLength = strlen(name);
unsigned size = nameLength + 7;
char buffer[size];
snprintf(buffer, size, "lib%s.so", name);
@ -369,13 +378,19 @@ class MySystem: public System {
fprintf(stderr, "open %s as %p\n", buffer, p);
}
*lib = new (System::allocate(sizeof(Library))) Library(this, p, next);
char* n = static_cast<char*>(System::allocate(nameLength + 1));
memcpy(n, name, nameLength + 1);
*lib = new (System::allocate(sizeof(Library))) Library(this, p, n, next);
return 0;
} else {
return 1;
}
}
virtual void exit(int code) {
::exit(code);
}
virtual void abort() {
::abort();
}

View File

@ -49,6 +49,7 @@ class System: public Allocator {
public:
virtual ~Library() { }
virtual void* resolve(const char* function) = 0;
virtual const char* name() = 0;
virtual Library* next() = 0;
virtual void dispose() = 0;
};
@ -64,6 +65,7 @@ class System: public Allocator {
unsigned count, unsigned size,
unsigned returnType) = 0;
virtual Status load(Library**, const char* name, Library* next) = 0;
virtual void exit(int code) = 0;
virtual void abort() = 0;
virtual void dispose() = 0;

View File

@ -17,19 +17,26 @@
(object staticTable)
(object initializer))
(type field
(type accessibleObject java/lang/reflect/AccessibleObject
(extends jobject))
(type field java/lang/reflect/Field
(extends accessibleObject)
(uint8_t vmFlags)
(uint8_t code)
(uint16_t flags)
(uint16_t offset)
(uint8_t code)
(object name)
(object spec)
(object class))
(type method
(type method java/lang/reflect/Method
(extends accessibleObject)
(uint8_t vmFlags)
(uint8_t parameterCount)
(uint16_t parameterFootprint)
(uint16_t flags)
(uint16_t offset)
(uint16_t parameterCount)
(uint16_t parameterFootprint)
(object name)
(object spec)
(object class)
@ -120,6 +127,7 @@
(type thread java/lang/Thread
(extends jobject)
(object task)
(object locals)
(int64_t peer))
(type stackTraceElement java/lang/StackTraceElement
@ -223,9 +231,9 @@
(type jreference java/lang/ref/Reference
(extends jobject)
(void* next)
(void* target)
(void* queue)
(void* next)
(object jnext))
(type weakReference java/lang/ref/WeakReference