1.1 --- a/emul/compact/src/main/java/java/util/ArrayDeque.java Mon Jan 28 13:30:53 2013 +0100
1.2 +++ b/emul/compact/src/main/java/java/util/ArrayDeque.java Mon Jan 28 13:52:28 2013 +0100
1.3 @@ -34,6 +34,7 @@
1.4
1.5 package java.util;
1.6 import java.io.*;
1.7 +import org.apidesign.bck2brwsr.emul.lang.System;
1.8
1.9 /**
1.10 * Resizable-array implementation of the {@link Deque} interface. Array
1.11 @@ -827,40 +828,4 @@
1.12 */
1.13 private static final long serialVersionUID = 2340985798034038923L;
1.14
1.15 - /**
1.16 - * Serialize this deque.
1.17 - *
1.18 - * @serialData The current size (<tt>int</tt>) of the deque,
1.19 - * followed by all of its elements (each an object reference) in
1.20 - * first-to-last order.
1.21 - */
1.22 - private void writeObject(ObjectOutputStream s) throws IOException {
1.23 - s.defaultWriteObject();
1.24 -
1.25 - // Write out size
1.26 - s.writeInt(size());
1.27 -
1.28 - // Write out elements in order.
1.29 - int mask = elements.length - 1;
1.30 - for (int i = head; i != tail; i = (i + 1) & mask)
1.31 - s.writeObject(elements[i]);
1.32 - }
1.33 -
1.34 - /**
1.35 - * Deserialize this deque.
1.36 - */
1.37 - private void readObject(ObjectInputStream s)
1.38 - throws IOException, ClassNotFoundException {
1.39 - s.defaultReadObject();
1.40 -
1.41 - // Read in size and allocate array
1.42 - int size = s.readInt();
1.43 - allocateElements(size);
1.44 - head = 0;
1.45 - tail = size;
1.46 -
1.47 - // Read in all elements in the proper order.
1.48 - for (int i = 0; i < size; i++)
1.49 - elements[i] = (E)s.readObject();
1.50 - }
1.51 }
2.1 --- a/emul/compact/src/main/java/java/util/Collections.java Mon Jan 28 13:30:53 2013 +0100
2.2 +++ b/emul/compact/src/main/java/java/util/Collections.java Mon Jan 28 13:52:28 2013 +0100
2.3 @@ -25,9 +25,9 @@
2.4
2.5 package java.util;
2.6 import java.io.Serializable;
2.7 -import java.io.ObjectOutputStream;
2.8 import java.io.IOException;
2.9 import java.lang.reflect.Array;
2.10 +import org.apidesign.bck2brwsr.emul.lang.System;
2.11
2.12 /**
2.13 * This class consists exclusively of static methods that operate on or return
2.14 @@ -1655,9 +1655,6 @@
2.15 public String toString() {
2.16 synchronized (mutex) {return c.toString();}
2.17 }
2.18 - private void writeObject(ObjectOutputStream s) throws IOException {
2.19 - synchronized (mutex) {s.defaultWriteObject();}
2.20 - }
2.21 }
2.22
2.23 /**
2.24 @@ -2081,9 +2078,6 @@
2.25 public String toString() {
2.26 synchronized (mutex) {return m.toString();}
2.27 }
2.28 - private void writeObject(ObjectOutputStream s) throws IOException {
2.29 - synchronized (mutex) {s.defaultWriteObject();}
2.30 - }
2.31 }
2.32
2.33 /**
2.34 @@ -3906,12 +3900,6 @@
2.35
2.36 private static final long serialVersionUID = 2454657854757543876L;
2.37
2.38 - private void readObject(java.io.ObjectInputStream stream)
2.39 - throws IOException, ClassNotFoundException
2.40 - {
2.41 - stream.defaultReadObject();
2.42 - s = m.keySet();
2.43 - }
2.44 }
2.45
2.46 /**
3.1 --- a/emul/compact/src/main/java/java/util/Hashtable.java Mon Jan 28 13:30:53 2013 +0100
3.2 +++ b/emul/compact/src/main/java/java/util/Hashtable.java Mon Jan 28 13:52:28 2013 +0100
3.3 @@ -836,116 +836,6 @@
3.4 }
3.5
3.6 /**
3.7 - * Save the state of the Hashtable to a stream (i.e., serialize it).
3.8 - *
3.9 - * @serialData The <i>capacity</i> of the Hashtable (the length of the
3.10 - * bucket array) is emitted (int), followed by the
3.11 - * <i>size</i> of the Hashtable (the number of key-value
3.12 - * mappings), followed by the key (Object) and value (Object)
3.13 - * for each key-value mapping represented by the Hashtable
3.14 - * The key-value mappings are emitted in no particular order.
3.15 - */
3.16 - private void writeObject(java.io.ObjectOutputStream s)
3.17 - throws IOException {
3.18 - Entry<Object, Object> entryStack = null;
3.19 -
3.20 - synchronized (this) {
3.21 - // Write out the length, threshold, loadfactor
3.22 - s.defaultWriteObject();
3.23 -
3.24 - // Write out length, count of elements
3.25 - s.writeInt(table.length);
3.26 - s.writeInt(count);
3.27 -
3.28 - // Stack copies of the entries in the table
3.29 - for (int index = 0; index < table.length; index++) {
3.30 - Entry entry = table[index];
3.31 -
3.32 - while (entry != null) {
3.33 - entryStack =
3.34 - new Entry<>(0, entry.key, entry.value, entryStack);
3.35 - entry = entry.next;
3.36 - }
3.37 - }
3.38 - }
3.39 -
3.40 - // Write out the key/value objects from the stacked entries
3.41 - while (entryStack != null) {
3.42 - s.writeObject(entryStack.key);
3.43 - s.writeObject(entryStack.value);
3.44 - entryStack = entryStack.next;
3.45 - }
3.46 - }
3.47 -
3.48 - /**
3.49 - * Reconstitute the Hashtable from a stream (i.e., deserialize it).
3.50 - */
3.51 - private void readObject(java.io.ObjectInputStream s)
3.52 - throws IOException, ClassNotFoundException
3.53 - {
3.54 - // Read in the length, threshold, and loadfactor
3.55 - s.defaultReadObject();
3.56 -
3.57 - // Read the original length of the array and number of elements
3.58 - int origlength = s.readInt();
3.59 - int elements = s.readInt();
3.60 -
3.61 - // Compute new size with a bit of room 5% to grow but
3.62 - // no larger than the original size. Make the length
3.63 - // odd if it's large enough, this helps distribute the entries.
3.64 - // Guard against the length ending up zero, that's not valid.
3.65 - int length = (int)(elements * loadFactor) + (elements / 20) + 3;
3.66 - if (length > elements && (length & 1) == 0)
3.67 - length--;
3.68 - if (origlength > 0 && length > origlength)
3.69 - length = origlength;
3.70 -
3.71 - Entry[] table = new Entry[length];
3.72 - count = 0;
3.73 -
3.74 - // Read the number of elements and then all the key/value objects
3.75 - for (; elements > 0; elements--) {
3.76 - K key = (K)s.readObject();
3.77 - V value = (V)s.readObject();
3.78 - // synch could be eliminated for performance
3.79 - reconstitutionPut(table, key, value);
3.80 - }
3.81 - this.table = table;
3.82 - }
3.83 -
3.84 - /**
3.85 - * The put method used by readObject. This is provided because put
3.86 - * is overridable and should not be called in readObject since the
3.87 - * subclass will not yet be initialized.
3.88 - *
3.89 - * <p>This differs from the regular put method in several ways. No
3.90 - * checking for rehashing is necessary since the number of elements
3.91 - * initially in the table is known. The modCount is not incremented
3.92 - * because we are creating a new instance. Also, no return value
3.93 - * is needed.
3.94 - */
3.95 - private void reconstitutionPut(Entry[] tab, K key, V value)
3.96 - throws StreamCorruptedException
3.97 - {
3.98 - if (value == null) {
3.99 - throw new java.io.StreamCorruptedException();
3.100 - }
3.101 - // Makes sure the key is not already in the hashtable.
3.102 - // This should not happen in deserialized version.
3.103 - int hash = key.hashCode();
3.104 - int index = (hash & 0x7FFFFFFF) % tab.length;
3.105 - for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) {
3.106 - if ((e.hash == hash) && e.key.equals(key)) {
3.107 - throw new java.io.StreamCorruptedException();
3.108 - }
3.109 - }
3.110 - // Creates the new entry.
3.111 - Entry<K,V> e = tab[index];
3.112 - tab[index] = new Entry<>(hash, key, value, e);
3.113 - count++;
3.114 - }
3.115 -
3.116 - /**
3.117 * Hashtable collision list.
3.118 */
3.119 private static class Entry<K,V> implements Map.Entry<K,V> {
4.1 --- a/emul/compact/src/main/java/java/util/LinkedList.java Mon Jan 28 13:30:53 2013 +0100
4.2 +++ b/emul/compact/src/main/java/java/util/LinkedList.java Mon Jan 28 13:52:28 2013 +0100
4.3 @@ -1097,42 +1097,4 @@
4.4
4.5 private static final long serialVersionUID = 876323262645176354L;
4.6
4.7 - /**
4.8 - * Saves the state of this {@code LinkedList} instance to a stream
4.9 - * (that is, serializes it).
4.10 - *
4.11 - * @serialData The size of the list (the number of elements it
4.12 - * contains) is emitted (int), followed by all of its
4.13 - * elements (each an Object) in the proper order.
4.14 - */
4.15 - private void writeObject(java.io.ObjectOutputStream s)
4.16 - throws java.io.IOException {
4.17 - // Write out any hidden serialization magic
4.18 - s.defaultWriteObject();
4.19 -
4.20 - // Write out size
4.21 - s.writeInt(size);
4.22 -
4.23 - // Write out all elements in the proper order.
4.24 - for (Node<E> x = first; x != null; x = x.next)
4.25 - s.writeObject(x.item);
4.26 - }
4.27 -
4.28 - /**
4.29 - * Reconstitutes this {@code LinkedList} instance from a stream
4.30 - * (that is, deserializes it).
4.31 - */
4.32 - @SuppressWarnings("unchecked")
4.33 - private void readObject(java.io.ObjectInputStream s)
4.34 - throws java.io.IOException, ClassNotFoundException {
4.35 - // Read in any hidden serialization magic
4.36 - s.defaultReadObject();
4.37 -
4.38 - // Read in size
4.39 - int size = s.readInt();
4.40 -
4.41 - // Read in all elements in the proper order.
4.42 - for (int i = 0; i < size; i++)
4.43 - linkLast((E)s.readObject());
4.44 - }
4.45 }
5.1 --- a/emul/compact/src/main/java/java/util/Random.java Mon Jan 28 13:30:53 2013 +0100
5.2 +++ b/emul/compact/src/main/java/java/util/Random.java Mon Jan 28 13:52:28 2013 +0100
5.3 @@ -24,9 +24,8 @@
5.4 */
5.5
5.6 package java.util;
5.7 -import java.io.*;
5.8 -import java.util.concurrent.atomic.AtomicLong;
5.9 -import sun.misc.Unsafe;
5.10 +
5.11 +import org.apidesign.bck2brwsr.emul.lang.System;
5.12
5.13 /**
5.14 * An instance of this class is used to generate a stream of
5.15 @@ -75,7 +74,7 @@
5.16 * (The specs for the methods in this class describe the ongoing
5.17 * computation of this value.)
5.18 */
5.19 - private final AtomicLong seed;
5.20 + private long seed;
5.21
5.22 private static final long multiplier = 0x5DEECE66DL;
5.23 private static final long addend = 0xBL;
5.24 @@ -89,20 +88,17 @@
5.25 public Random() {
5.26 this(seedUniquifier() ^ System.nanoTime());
5.27 }
5.28 -
5.29 - private static long seedUniquifier() {
5.30 +
5.31 + private static synchronized long seedUniquifier() {
5.32 // L'Ecuyer, "Tables of Linear Congruential Generators of
5.33 // Different Sizes and Good Lattice Structure", 1999
5.34 - for (;;) {
5.35 - long current = seedUniquifier.get();
5.36 - long next = current * 181783497276652981L;
5.37 - if (seedUniquifier.compareAndSet(current, next))
5.38 - return next;
5.39 - }
5.40 + long current = seedUniquifier;
5.41 + long next = current * 181783497276652981L;
5.42 + seedUniquifier = next;
5.43 + return next;
5.44 }
5.45
5.46 - private static final AtomicLong seedUniquifier
5.47 - = new AtomicLong(8682522807148012L);
5.48 + private static long seedUniquifier = 8682522807148012L;
5.49
5.50 /**
5.51 * Creates a new random number generator using a single {@code long} seed.
5.52 @@ -118,7 +114,7 @@
5.53 * @see #setSeed(long)
5.54 */
5.55 public Random(long seed) {
5.56 - this.seed = new AtomicLong(initialScramble(seed));
5.57 + this.seed = initialScramble(seed);
5.58 }
5.59
5.60 private static long initialScramble(long seed) {
5.61 @@ -145,7 +141,7 @@
5.62 * @param seed the initial seed
5.63 */
5.64 synchronized public void setSeed(long seed) {
5.65 - this.seed.set(initialScramble(seed));
5.66 + this.seed = initialScramble(seed);
5.67 haveNextNextGaussian = false;
5.68 }
5.69
5.70 @@ -174,13 +170,12 @@
5.71 * generator's sequence
5.72 * @since 1.1
5.73 */
5.74 - protected int next(int bits) {
5.75 + protected synchronized int next(int bits) {
5.76 long oldseed, nextseed;
5.77 - AtomicLong seed = this.seed;
5.78 - do {
5.79 - oldseed = seed.get();
5.80 - nextseed = (oldseed * multiplier + addend) & mask;
5.81 - } while (!seed.compareAndSet(oldseed, nextseed));
5.82 + long seed = this.seed;
5.83 + oldseed = seed;
5.84 + nextseed = (oldseed * multiplier + addend) & mask;
5.85 + this.seed = nextseed;
5.86 return (int)(nextseed >>> (48 - bits));
5.87 }
5.88
5.89 @@ -499,77 +494,10 @@
5.90 v2 = 2 * nextDouble() - 1; // between -1 and 1
5.91 s = v1 * v1 + v2 * v2;
5.92 } while (s >= 1 || s == 0);
5.93 - double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s);
5.94 + double multiplier = Math.sqrt(-2 * Math.log(s)/s);
5.95 nextNextGaussian = v2 * multiplier;
5.96 haveNextNextGaussian = true;
5.97 return v1 * multiplier;
5.98 }
5.99 }
5.100 -
5.101 - /**
5.102 - * Serializable fields for Random.
5.103 - *
5.104 - * @serialField seed long
5.105 - * seed for random computations
5.106 - * @serialField nextNextGaussian double
5.107 - * next Gaussian to be returned
5.108 - * @serialField haveNextNextGaussian boolean
5.109 - * nextNextGaussian is valid
5.110 - */
5.111 - private static final ObjectStreamField[] serialPersistentFields = {
5.112 - new ObjectStreamField("seed", Long.TYPE),
5.113 - new ObjectStreamField("nextNextGaussian", Double.TYPE),
5.114 - new ObjectStreamField("haveNextNextGaussian", Boolean.TYPE)
5.115 - };
5.116 -
5.117 - /**
5.118 - * Reconstitute the {@code Random} instance from a stream (that is,
5.119 - * deserialize it).
5.120 - */
5.121 - private void readObject(java.io.ObjectInputStream s)
5.122 - throws java.io.IOException, ClassNotFoundException {
5.123 -
5.124 - ObjectInputStream.GetField fields = s.readFields();
5.125 -
5.126 - // The seed is read in as {@code long} for
5.127 - // historical reasons, but it is converted to an AtomicLong.
5.128 - long seedVal = fields.get("seed", -1L);
5.129 - if (seedVal < 0)
5.130 - throw new java.io.StreamCorruptedException(
5.131 - "Random: invalid seed");
5.132 - resetSeed(seedVal);
5.133 - nextNextGaussian = fields.get("nextNextGaussian", 0.0);
5.134 - haveNextNextGaussian = fields.get("haveNextNextGaussian", false);
5.135 - }
5.136 -
5.137 - /**
5.138 - * Save the {@code Random} instance to a stream.
5.139 - */
5.140 - synchronized private void writeObject(ObjectOutputStream s)
5.141 - throws IOException {
5.142 -
5.143 - // set the values of the Serializable fields
5.144 - ObjectOutputStream.PutField fields = s.putFields();
5.145 -
5.146 - // The seed is serialized as a long for historical reasons.
5.147 - fields.put("seed", seed.get());
5.148 - fields.put("nextNextGaussian", nextNextGaussian);
5.149 - fields.put("haveNextNextGaussian", haveNextNextGaussian);
5.150 -
5.151 - // save them
5.152 - s.writeFields();
5.153 - }
5.154 -
5.155 - // Support for resetting seed while deserializing
5.156 - private static final Unsafe unsafe = Unsafe.getUnsafe();
5.157 - private static final long seedOffset;
5.158 - static {
5.159 - try {
5.160 - seedOffset = unsafe.objectFieldOffset
5.161 - (Random.class.getDeclaredField("seed"));
5.162 - } catch (Exception ex) { throw new Error(ex); }
5.163 - }
5.164 - private void resetSeed(long seedVal) {
5.165 - unsafe.putObjectVolatile(this, seedOffset, new AtomicLong(seedVal));
5.166 - }
5.167 }
6.1 --- a/emul/compact/src/main/java/java/util/Vector.java Mon Jan 28 13:30:53 2013 +0100
6.2 +++ b/emul/compact/src/main/java/java/util/Vector.java Mon Jan 28 13:52:28 2013 +0100
6.3 @@ -25,6 +25,8 @@
6.4
6.5 package java.util;
6.6
6.7 +import org.apidesign.bck2brwsr.emul.lang.System;
6.8 +
6.9 /**
6.10 * The {@code Vector} class implements a growable array of
6.11 * objects. Like an array, it contains components that can be
6.12 @@ -1055,25 +1057,6 @@
6.13 }
6.14
6.15 /**
6.16 - * Save the state of the {@code Vector} instance to a stream (that
6.17 - * is, serialize it).
6.18 - * This method performs synchronization to ensure the consistency
6.19 - * of the serialized data.
6.20 - */
6.21 - private void writeObject(java.io.ObjectOutputStream s)
6.22 - throws java.io.IOException {
6.23 - final java.io.ObjectOutputStream.PutField fields = s.putFields();
6.24 - final Object[] data;
6.25 - synchronized (this) {
6.26 - fields.put("capacityIncrement", capacityIncrement);
6.27 - fields.put("elementCount", elementCount);
6.28 - data = elementData.clone();
6.29 - }
6.30 - fields.put("elementData", data);
6.31 - s.writeFields();
6.32 - }
6.33 -
6.34 - /**
6.35 * Returns a list iterator over the elements in this list (in proper
6.36 * sequence), starting at the specified position in the list.
6.37 * The specified index indicates the first element that would be
7.1 --- a/emul/compact/src/test/java/org/apidesign/bck2brwsr/compact/tck/CollectionsTest.java Mon Jan 28 13:30:53 2013 +0100
7.2 +++ b/emul/compact/src/test/java/org/apidesign/bck2brwsr/compact/tck/CollectionsTest.java Mon Jan 28 13:52:28 2013 +0100
7.3 @@ -25,6 +25,8 @@
7.4 import java.util.HashSet;
7.5 import java.util.List;
7.6 import java.util.Map;
7.7 +import java.util.Map.Entry;
7.8 +import java.util.Vector;
7.9 import org.apidesign.bck2brwsr.vmtest.Compare;
7.10 import org.apidesign.bck2brwsr.vmtest.VMTest;
7.11 import org.testng.annotations.Factory;
7.12 @@ -92,10 +94,9 @@
7.13 map.put("nine", 9);
7.14 map.put("ten", 10);
7.15
7.16 - Map.Entry<String,Integer>[] arr = map.entrySet().toArray(new Map.Entry[map.size()]);
7.17 - Arrays.sort(arr, new C());
7.18 -
7.19 - return Arrays.asList(arr).toString();
7.20 + List<Entry<String,Integer>> arr = new Vector<>();
7.21 + arr.addAll(map.entrySet());
7.22 + return arr.toString();
7.23 }
7.24
7.25 @Factory
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
8.2 +++ b/emul/compact/src/test/java/org/apidesign/bck2brwsr/compact/tck/RandomTest.java Mon Jan 28 13:52:28 2013 +0100
8.3 @@ -0,0 +1,40 @@
8.4 +/**
8.5 + * Back 2 Browser Bytecode Translator
8.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
8.7 + *
8.8 + * This program is free software: you can redistribute it and/or modify
8.9 + * it under the terms of the GNU General Public License as published by
8.10 + * the Free Software Foundation, version 2 of the License.
8.11 + *
8.12 + * This program is distributed in the hope that it will be useful,
8.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8.15 + * GNU General Public License for more details.
8.16 + *
8.17 + * You should have received a copy of the GNU General Public License
8.18 + * along with this program. Look for COPYING file in the top folder.
8.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
8.20 + */
8.21 +package org.apidesign.bck2brwsr.compact.tck;
8.22 +
8.23 +import java.util.Random;
8.24 +import org.apidesign.bck2brwsr.vmtest.Compare;
8.25 +import org.apidesign.bck2brwsr.vmtest.VMTest;
8.26 +import org.testng.annotations.Factory;
8.27 +
8.28 +/**
8.29 + *
8.30 + * @author Jaroslav Tulach <jtulach@netbeans.org>
8.31 + */
8.32 +public class RandomTest {
8.33 + @Compare public boolean canInstantiateRandom() {
8.34 + Random r = new Random();
8.35 + r.nextInt();
8.36 + return r != null;
8.37 + }
8.38 +
8.39 +
8.40 + @Factory public static Object[] create() {
8.41 + return VMTest.create(RandomTest.class);
8.42 + }
8.43 +}
9.1 --- a/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/lang/System.java Mon Jan 28 13:30:53 2013 +0100
9.2 +++ b/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/lang/System.java Mon Jan 28 13:52:28 2013 +0100
9.3 @@ -44,5 +44,7 @@
9.4 "while (expectedSize-- > arr.length) { arr.push(0); }; return arr;"
9.5 )
9.6 public static native byte[] expandArray(byte[] arr, int expectedSize);
9.7 -
9.8 +
9.9 + @JavaScriptBody(args = {}, body = "new Date().getMilliseconds() * 1000;")
9.10 + public static native long nanoTime();
9.11 }