Merge remote-tracking branch 'origin/master'

This commit is contained in:
Alexey Andreev 2015-07-22 22:29:37 +03:00
commit 6f3d80ffe1
13 changed files with 364 additions and 31 deletions

View File

@ -64,7 +64,7 @@ You can try its TeaVM-compiled version [here](http://xelfi.cz/minesweeper/teavm/
[source code](http://source.apidesign.org/hg/html~demo/file/4dce5ea7e13a/minesweeper/src/main/java/org/apidesign/demo/minesweeper/MinesModel.java)
and [HTML page](http://source.apidesign.org/hg/html~demo/file/4dce5ea7e13a/minesweeper/src/main/webapp/pages/index.html).
Another example is avaialble [here](http://graphhopper.com/teavm/).
Another example is available [here](http://graphhopper.com/teavm/).
It uses [GraphHopper](https://github.com/graphhopper/graphhopper/) to build route in browser.
Unlike original GraphHopper example it works completely in browser instead of querying server.
Thanks to [Peter Karich](https://github.com/karussell).

View File

@ -17,6 +17,9 @@ package org.teavm.classlib.impl.unicode;
import java.util.Arrays;
import org.teavm.classlib.impl.Base46;
import org.teavm.classlib.impl.CharFlow;
/**
*
* @author Alexey Andreev
@ -43,30 +46,20 @@ public class UnicodeHelper {
}
public static String encodeIntByte(int[] data) {
char[] chars = new char[data.length / 2 * 5];
int j = 0;
for (int i = 0; i < data.length;) {
int val = data[i++];
int shift = 32;
for (int k = 0; k < 4; ++k) {
shift -= 8;
chars[j++] = (char)('z' + ((val >> shift) & 0xFF));
}
chars[j++] = (char)('z' + (data[i++] & 0xFF));
StringBuilder sb = new StringBuilder();
Base46.encode(sb, data.length);
for (int i = 0; i < data.length; i++) {
Base46.encode(sb, data[i]);
}
return new String(chars);
return sb.toString();
}
public static int[] decodeIntByte(String text) {
int[] data = new int[2 * (text.length() / 5)];
int j = 0;
for (int i = 0; i < data.length;) {
int val = 0;
for (int k = 0; k < 4; ++k) {
val = (val << 8) | (text.charAt(j++) - 'z');
}
data[i++] = val;
data[i++] = text.charAt(j++) - 'z';
CharFlow flow = new CharFlow(text.toCharArray());
int sz = Base46.decode(flow);
int[] data = new int[sz];
for (int i = 0; i < sz; i++) {
data[i] = Base46.decode(flow);
}
return data;
}

View File

@ -0,0 +1,23 @@
/*
* Copyright 2015 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.java.io;
import org.teavm.classlib.java.lang.TObject;
public class TConsole extends TObject {
}

View File

@ -17,6 +17,7 @@ package org.teavm.classlib.java.lang;
import java.util.HashMap;
import java.util.Map;
import org.teavm.classlib.impl.DeclaringClassMetadataGenerator;
import org.teavm.classlib.java.lang.annotation.TAnnotation;
import org.teavm.classlib.java.lang.reflect.TAnnotatedElement;
@ -32,6 +33,7 @@ import org.teavm.platform.metadata.ClassScopedMetadataProvider;
*/
public class TClass<T> extends TObject implements TAnnotatedElement {
TString name;
TString simpleName;
private TClass<?> componentType;
private boolean componentTypeDirty = true;
private PlatformClass platformClass;
@ -73,6 +75,30 @@ public class TClass<T> extends TObject implements TAnnotatedElement {
return name;
}
public TString getSimpleName() {
if (simpleName == null) {
if (isArray()) {
simpleName = getComponentType().getSimpleName().concat(TString.wrap("[]"));
return simpleName;
}
String name = platformClass.getMetadata().getName();
int lastDollar = name.lastIndexOf('$');
if (lastDollar != -1) {
name = name.substring(lastDollar + 1);
if (name.charAt(0) >= '0' && name.charAt(0) <= '9') {
name = "";
}
} else {
int lastDot = name.lastIndexOf('.');
if (lastDot != -1) {
name = name.substring(lastDot + 1);
}
}
simpleName = TString.wrap(name);
}
return simpleName;
}
public boolean isPrimitive() {
return platformClass.getMetadata().isPrimitive();
}

View File

@ -15,6 +15,7 @@
*/
package org.teavm.classlib.java.lang;
import org.teavm.classlib.java.io.TConsole;
import org.teavm.classlib.java.io.TPrintStream;
import org.teavm.classlib.java.lang.reflect.TArray;
import org.teavm.dependency.PluggableDependency;
@ -31,6 +32,10 @@ public final class TSystem extends TObject {
private TSystem() {
}
public static TConsole console() {
return null;
}
public static void arraycopy(TObject src, int srcPos, TObject dest, int destPos, int length) {
if (src == null || dest == null) {
throw new TNullPointerException(TString.wrap("Either src or dest is null"));

View File

@ -86,6 +86,9 @@ public abstract class TByteBuffer extends TBuffer implements TComparable<TByteBu
}
public TByteBuffer put(byte[] src, int offset, int length) {
if (length == 0) {
return this;
}
if (isReadOnly()) {
throw new TReadOnlyBufferException();
}
@ -229,6 +232,14 @@ public abstract class TByteBuffer extends TBuffer implements TComparable<TByteBu
public abstract TIntBuffer asIntBuffer();
public abstract long getLong();
public abstract TByteBuffer putLong(long value);
public abstract long getLong(int index);
public abstract TByteBuffer putLong(int index, long value);
public abstract TLongBuffer asLongBuffer();
public abstract TFloatBuffer asFloatBuffer();

View File

@ -264,9 +264,9 @@ class TByteBufferImpl extends TByteBuffer {
int d = array[start + position + 3] & 0xFF;
position += 4;
if (order == TByteOrder.BIG_ENDIAN) {
return (short)((a << 24) | (b << 16) | (c << 8) | d);
return (int)((a << 24) | (b << 16) | (c << 8) | d);
} else {
return (short)((d << 24) | (c << 16) | (b << 8) | a);
return (int)((d << 24) | (c << 16) | (b << 8) | a);
}
}
@ -297,14 +297,14 @@ class TByteBufferImpl extends TByteBuffer {
if (index < 0 || index + 3 >= limit) {
throw new IndexOutOfBoundsException("Index " + index + " is outside of range [0;" + (limit - 3) + ")");
}
int a = array[start + position] & 0xFF;
int b = array[start + position + 1] & 0xFF;
int c = array[start + position + 2] & 0xFF;
int d = array[start + position + 3] & 0xFF;
int a = array[start + index] & 0xFF;
int b = array[start + index + 1] & 0xFF;
int c = array[start + index + 2] & 0xFF;
int d = array[start + index + 3] & 0xFF;
if (order == TByteOrder.BIG_ENDIAN) {
return (short)((a << 24) | (b << 16) | (c << 8) | d);
return (int)((a << 24) | (b << 16) | (c << 8) | d);
} else {
return (short)((d << 24) | (c << 16) | (b << 8) | a);
return (int)((d << 24) | (c << 16) | (b << 8) | a);
}
}
@ -340,6 +340,108 @@ class TByteBufferImpl extends TByteBuffer {
}
}
@Override
public long getLong() {
if (position + 7 >= limit) {
throw new TBufferUnderflowException();
}
long a = array[start + position] & 0xFF;
long b = array[start + position + 1] & 0xFF;
long c = array[start + position + 2] & 0xFF;
long d = array[start + position + 3] & 0xFF;
long e = array[start + position + 4] & 0xFF;
long f = array[start + position + 5] & 0xFF;
long g = array[start + position + 6] & 0xFF;
long h = array[start + position + 7] & 0xFF;
position += 8;
if (order == TByteOrder.BIG_ENDIAN) {
return (long)((a << 56) | (b << 48) | (c << 40) | (d << 32) | (e << 24) | (f << 16) | (g << 8) | h);
} else {
return (long)((h << 56) | (g << 48) | (f << 40) | (e << 32) | (d << 24) | (c << 16) | (b << 8) | a);
}
}
@Override
public TByteBuffer putLong(long value) {
if (readOnly) {
throw new TReadOnlyBufferException();
}
if (position + 7 >= limit) {
throw new TBufferOverflowException();
}
if (order == TByteOrder.BIG_ENDIAN) {
array[start + position++] = (byte)(value >> 56);
array[start + position++] = (byte)(value >> 48);
array[start + position++] = (byte)(value >> 40);
array[start + position++] = (byte)(value >> 32);
array[start + position++] = (byte)(value >> 24);
array[start + position++] = (byte)(value >> 16);
array[start + position++] = (byte)(value >> 8);
array[start + position++] = (byte)value;
} else {
array[start + position++] = (byte)value;
array[start + position++] = (byte)(value >> 8);
array[start + position++] = (byte)(value >> 16);
array[start + position++] = (byte)(value >> 24);
array[start + position++] = (byte)(value >> 32);
array[start + position++] = (byte)(value >> 40);
array[start + position++] = (byte)(value >> 48);
array[start + position++] = (byte)(value >> 56);
}
return this;
}
@Override
public long getLong(int index) {
if (index < 0 || index + 7 >= limit) {
throw new IndexOutOfBoundsException("Index " + index + " is outside of range [0;" + (limit - 7) + ")");
}
long a = array[start + index] & 0xFF;
long b = array[start + index + 1] & 0xFF;
long c = array[start + index + 2] & 0xFF;
long d = array[start + index + 3] & 0xFF;
long e = array[start + index + 4] & 0xFF;
long f = array[start + index + 5] & 0xFF;
long g = array[start + index + 6] & 0xFF;
long h = array[start + index + 7] & 0xFF;
position += 8;
if (order == TByteOrder.BIG_ENDIAN) {
return (long)((a << 56) | (b << 48) | (c << 40) | (d << 32) | (e << 24) | (f << 16) | (g << 8) | h);
} else {
return (long)((h << 56) | (g << 48) | (f << 40) | (e << 32) | (d << 24) | (c << 16) | (b << 8) | a);
}
}
@Override
public TByteBuffer putLong(int index, long value) {
if (readOnly) {
throw new TReadOnlyBufferException();
}
if (index < 0 || index + 3 >= limit) {
throw new IndexOutOfBoundsException("Index " + index + " is outside of range [0;" + (limit - 3) + ")");
}
if (order == TByteOrder.BIG_ENDIAN) {
array[start + index + 0] = (byte)(value >> 56);
array[start + index + 1] = (byte)(value >> 48);
array[start + index + 2] = (byte)(value >> 40);
array[start + index + 3] = (byte)(value >> 32);
array[start + index + 4] = (byte)(value >> 24);
array[start + index + 5] = (byte)(value >> 16);
array[start + index + 6] = (byte)(value >> 8);
array[start + index + 7] = (byte)value;
} else {
array[start + index + 0] = (byte)value;
array[start + index + 1] = (byte)(value >> 8);
array[start + index + 2] = (byte)(value >> 16);
array[start + index + 3] = (byte)(value >> 24);
array[start + index + 4] = (byte)(value >> 24);
array[start + index + 5] = (byte)(value >> 24);
array[start + index + 6] = (byte)(value >> 24);
array[start + index + 7] = (byte)(value >> 24);
}
return this;
}
@Override
public TLongBuffer asLongBuffer() {
int sz = remaining() / 8;

View File

@ -259,10 +259,10 @@ public class TCollections extends TObject {
while (true) {
int i = (l + u) / 2;
T e = list.get(i);
int cmp = c.compare(key, e);
int cmp = c.compare(e, key);
if (cmp == 0) {
return i;
} else if (cmp < 0) {
} else if (cmp > 0) {
u = i - 1;
if (u < l) {
return -i - 1;

View File

@ -50,6 +50,8 @@ public interface XMLHttpRequest extends JSObject {
String getAllResponseHeaders();
String getResponseHeader(String name);
@JSProperty("onreadystatechange")
void setOnReadyStateChange(ReadyStateChangeHandler handler);

View File

@ -0,0 +1,24 @@
/*
* Copyright 2015 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.dom.media;
import org.teavm.jso.JSObject;
public interface MediaSource extends JSObject {
SourceBuffer addSourceBuffer(String type);
void endOfStream();
}

View File

@ -0,0 +1,28 @@
/*
* Copyright 2015 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.dom.media;
import org.teavm.dom.typedarrays.Uint8Array;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
public interface SourceBuffer extends JSObject {
void appendBuffer(Uint8Array array);
@JSProperty
boolean getUpdating();
}

View File

@ -38,6 +38,16 @@ public class ClassTest {
assertEquals("[I", int[].class.getName());
}
@Test
public void classSimpleNameEvaluated() {
assertEquals("Object", Object.class.getSimpleName());
assertEquals("Object[]", Object[].class.getSimpleName());
assertEquals("int", int.class.getSimpleName());
assertEquals("int[]", int[].class.getSimpleName());
assertEquals("InnerClass", InnerClass.class.getSimpleName());
assertEquals("", new Object(){}.getClass().getSimpleName());
}
@Test
public void objectClassNameEvaluated() {
assertEquals("java.lang.Object", new Object().getClass().getName());
@ -205,4 +215,7 @@ public class ClassTest {
Class<?> n();
}
static class InnerClass {
}
}

View File

@ -345,6 +345,17 @@ public class ByteBufferTest {
assertThat(array, is(new byte[] {0, 2, 3, 0 }));
}
@Test
public void putsBytesWithZeroLengthArray() {
byte[] array = new byte[4];
ByteBuffer buffer = ByteBuffer.wrap(array);
buffer.get();
byte[] data = { };
buffer.put(data, 0, 0);
assertThat(buffer.position(), is(1));
assertThat(array, is(new byte[] {0, 0, 0, 0 }));
}
@Test
public void compacts() {
byte[] array = { 2, 3, 5, 7 };
@ -498,4 +509,99 @@ public class ByteBufferTest {
// expected
}
}
@Test
public void getsInt() {
byte[] array = { 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30 };
ByteBuffer buffer = ByteBuffer.wrap(array);
assertThat(buffer.getInt(), is(0x23242526));
assertThat(buffer.getInt(), is(0x27282930));
try {
buffer.getInt();
fail("Exception expected");
} catch (BufferUnderflowException e) {
// expected
}
buffer.position(7);
try {
buffer.getInt();
fail("Exception expected");
} catch (BufferUnderflowException e) {
// expected
}
assertThat(buffer.getInt(0), is(0x23242526));
assertThat(buffer.getInt(4), is(0x27282930));
try {
buffer.getInt(7);
fail("Exception expected");
} catch (IndexOutOfBoundsException e) {
// expected
}
}
@Test
public void getsLong() {
byte[] array = { 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38 };
ByteBuffer buffer = ByteBuffer.wrap(array);
assertThat(buffer.getLong(), is(0x2324252627282930L));
assertThat(buffer.getLong(), is(0x3132333435363738L));
try {
buffer.getLong();
fail("Exception expected");
} catch (BufferUnderflowException e) {
// expected
}
buffer.position(15);
try {
buffer.getLong();
fail("Exception expected");
} catch (BufferUnderflowException e) {
// expected
}
assertThat(buffer.getLong(0), is(0x2324252627282930L));
assertThat(buffer.getLong(8), is(0x3132333435363738L));
try {
buffer.getLong(16);
fail("Exception expected");
} catch (IndexOutOfBoundsException e) {
// expected
}
}
@Test
public void putsLong() {
byte[] array = new byte[16];
ByteBuffer buffer = ByteBuffer.wrap(array);
buffer.putLong(0x2324252627282930L);
buffer.putLong(0x3132333435363738L);
try {
buffer.putLong(0L);
fail("Exception expected");
} catch (BufferOverflowException e) {
// expected
}
buffer.position(15);
try {
buffer.putLong(0L);
fail("Exception expected");
} catch (BufferOverflowException e) {
// expected
}
assertThat(buffer.get(0), is((byte)0x23));
assertThat(buffer.get(1), is((byte)0x24));
assertThat(buffer.get(2), is((byte)0x25));
assertThat(buffer.get(3), is((byte)0x26));
assertThat(buffer.get(4), is((byte)0x27));
assertThat(buffer.get(5), is((byte)0x28));
assertThat(buffer.get(6), is((byte)0x29));
assertThat(buffer.get(7), is((byte)0x30));
buffer.putLong(0, 0xAABBCCDDEEFF0000L);
assertThat(buffer.get(1), is((byte)0xBB));
try {
buffer.putLong(15, 0x0L);
fail("Exception expected");
} catch (IndexOutOfBoundsException e) {
// expected
}
}
}