mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 16:14:10 -08:00
Merge branch 'master' into async-irreducible
This commit is contained in:
commit
59408e40e5
|
@ -25,7 +25,7 @@ public abstract class Charset {
|
|||
public abstract void decode(ByteBuffer source, CharBuffer dest);
|
||||
|
||||
public static Charset get(String name) {
|
||||
if (name.equals("UTF-8")) {
|
||||
if (name.toUpperCase().equals("UTF-8")) {
|
||||
return new UTF8Charset();
|
||||
}
|
||||
return null;
|
||||
|
|
|
@ -144,12 +144,12 @@ public class UnicodeSupport {
|
|||
int j = 0;
|
||||
int t = 0;
|
||||
while (true) {
|
||||
if (i == a.length) {
|
||||
if (j == b.length) {
|
||||
while (i < a.length) {
|
||||
result[t++] = a[i++];
|
||||
}
|
||||
break;
|
||||
} else if (j == b.length) {
|
||||
} else if (i == a.length) {
|
||||
while (j < b.length) {
|
||||
result[t++] = b[j++];
|
||||
}
|
||||
|
|
|
@ -39,7 +39,7 @@ public class TBufferedInputStream extends TFilterInputStream {
|
|||
}
|
||||
|
||||
@Override
|
||||
public synchronized int available() throws TIOException {
|
||||
public int available() throws TIOException {
|
||||
TInputStream localIn = in;
|
||||
if (buf == null || localIn == null) {
|
||||
throw new TIOException(TString.wrap("Stream is closed"));
|
||||
|
|
|
@ -51,7 +51,7 @@ public class TByteArrayOutputStream extends TOutputStream {
|
|||
|
||||
private void ensureCapacity(int capacity) {
|
||||
if (buf.length < capacity) {
|
||||
capacity = TMath.min(capacity, buf.length * 3 / 2);
|
||||
capacity = TMath.max(capacity, buf.length * 3 / 2);
|
||||
buf = TArrays.copyOf(buf, capacity);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
|
||||
package org.teavm.classlib.java.io;
|
||||
import org.teavm.classlib.java.lang.*;
|
||||
|
||||
/**
|
||||
* A data output stream lets an application write primitive Java data types to an output stream in a portable way. An application can then use a data input stream to read the data back in.
|
||||
* Since: JDK1.0, CLDC 1.0 See Also:DataInputStream
|
||||
|
@ -117,6 +118,7 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu
|
|||
* if an error occurs while writing to the target stream.
|
||||
* @see DataInputStream#readBoolean()
|
||||
*/
|
||||
@Override
|
||||
public final void writeBoolean(boolean val) throws TIOException {
|
||||
out.write(val ? 1 : 0);
|
||||
written++;
|
||||
|
@ -133,6 +135,7 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu
|
|||
* @see DataInputStream#readByte()
|
||||
* @see DataInputStream#readUnsignedByte()
|
||||
*/
|
||||
@Override
|
||||
public final void writeByte(int val) throws TIOException {
|
||||
out.write(val);
|
||||
written++;
|
||||
|
@ -171,6 +174,7 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu
|
|||
* if an error occurs while writing to the target stream.
|
||||
* @see DataInputStream#readChar()
|
||||
*/
|
||||
@Override
|
||||
public final void writeChar(int val) throws TIOException {
|
||||
buff[0] = (byte) (val >> 8);
|
||||
buff[1] = (byte) val;
|
||||
|
@ -189,6 +193,7 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu
|
|||
* if an error occurs while writing to the target stream.
|
||||
* @see DataInputStream#readChar()
|
||||
*/
|
||||
@Override
|
||||
public final void writeChars(TString str) throws TIOException {
|
||||
byte newBytes[] = new byte[str.length() * 2];
|
||||
for (int index = 0; index < str.length(); index++) {
|
||||
|
@ -210,6 +215,7 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu
|
|||
* if an error occurs while writing to the target stream.
|
||||
* @see DataInputStream#readDouble()
|
||||
*/
|
||||
@Override
|
||||
public final void writeDouble(double val) throws TIOException {
|
||||
writeLong(TDouble.doubleToLongBits(val));
|
||||
}
|
||||
|
@ -224,6 +230,7 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu
|
|||
* if an error occurs while writing to the target stream.
|
||||
* @see DataInputStream#readFloat()
|
||||
*/
|
||||
@Override
|
||||
public final void writeFloat(float val) throws TIOException {
|
||||
writeInt(TFloat.floatToIntBits(val));
|
||||
}
|
||||
|
@ -238,6 +245,7 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu
|
|||
* if an error occurs while writing to the target stream.
|
||||
* @see DataInputStream#readInt()
|
||||
*/
|
||||
@Override
|
||||
public final void writeInt(int val) throws TIOException {
|
||||
buff[0] = (byte) (val >> 24);
|
||||
buff[1] = (byte) (val >> 16);
|
||||
|
@ -257,6 +265,7 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu
|
|||
* if an error occurs while writing to the target stream.
|
||||
* @see DataInputStream#readLong()
|
||||
*/
|
||||
@Override
|
||||
public final void writeLong(long val) throws TIOException {
|
||||
buff[0] = (byte) (val >> 56);
|
||||
buff[1] = (byte) (val >> 48);
|
||||
|
@ -295,6 +304,7 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu
|
|||
* @see DataInputStream#readShort()
|
||||
* @see DataInputStream#readUnsignedShort()
|
||||
*/
|
||||
@Override
|
||||
public final void writeShort(int val) throws TIOException {
|
||||
buff[0] = (byte) (val >> 8);
|
||||
buff[1] = (byte) val;
|
||||
|
@ -322,6 +332,7 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu
|
|||
* if the encoded string is longer than 65535 bytes.
|
||||
* @see DataInputStream#readUTF()
|
||||
*/
|
||||
@Override
|
||||
public final void writeUTF(TString str) throws TIOException {
|
||||
long utfCount = countUTFBytes(str);
|
||||
if (utfCount > 65535) {
|
||||
|
@ -330,7 +341,7 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu
|
|||
byte[] buffer = new byte[(int)utfCount + 2];
|
||||
int offset = 0;
|
||||
offset = writeShortToBuffer((int) utfCount, buffer, offset);
|
||||
offset = writeUTFBytesToBuffer(str, (int) utfCount, buffer, offset);
|
||||
offset = writeUTFBytesToBuffer(str, buffer, offset);
|
||||
write(buffer, 0, offset);
|
||||
}
|
||||
|
||||
|
@ -349,8 +360,7 @@ public class TDataOutputStream extends TFilterOutputStream implements TDataOutpu
|
|||
return utfCount;
|
||||
}
|
||||
|
||||
int writeUTFBytesToBuffer(TString str, long count,
|
||||
byte[] buffer, int offset) throws TIOException {
|
||||
int writeUTFBytesToBuffer(TString str, byte[] buffer, int offset) throws TIOException {
|
||||
int length = str.length();
|
||||
for (int i = 0; i < length; i++) {
|
||||
int charValue = str.charAt(i);
|
||||
|
|
|
@ -34,7 +34,7 @@ public class TFilterInputStream extends TInputStream {
|
|||
}
|
||||
|
||||
@Override
|
||||
public synchronized void mark(int readlimit) {
|
||||
public void mark(int readlimit) {
|
||||
in.mark(readlimit);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,92 +1,125 @@
|
|||
/*
|
||||
* Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Codename One designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
* Copyright 2015 Alexey Andreev.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
* 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
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Please contact Codename One through http://www.codenameone.com/ if you
|
||||
* need additional information or have any questions.
|
||||
* 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.*;
|
||||
/**
|
||||
* An OutputStreamWriter is a bridge from character streams to byte streams: Characters written to it are translated into bytes. The encoding that it uses may be specified by name, or the platform's default encoding may be accepted.
|
||||
* Each invocation of a write() method causes the encoding converter to be invoked on the given character(s). The resulting bytes are accumulated in a buffer before being written to the underlying output stream. The size of this buffer may be specified, but by default it is large enough for most purposes. Note that the characters passed to the write() methods are not buffered.
|
||||
* Since: CLDC 1.0 See Also:Writer, UnsupportedEncodingException
|
||||
*/
|
||||
public class TOutputStreamWriter extends TWriter{
|
||||
private TOutputStream os;
|
||||
private TString enc;
|
||||
|
||||
/**
|
||||
* Create an OutputStreamWriter that uses the default character encoding.
|
||||
* os - An OutputStream
|
||||
*/
|
||||
public TOutputStreamWriter(TOutputStream os){
|
||||
this.os = os;
|
||||
enc = TString.wrap("UTF-8");
|
||||
import org.teavm.classlib.impl.charset.ByteBuffer;
|
||||
import org.teavm.classlib.impl.charset.CharBuffer;
|
||||
import org.teavm.classlib.impl.charset.Charset;
|
||||
import org.teavm.classlib.java.lang.TString;
|
||||
|
||||
public class TOutputStreamWriter extends TWriter {
|
||||
private TOutputStream out;
|
||||
private String encoding;
|
||||
private Charset charset;
|
||||
private byte[] bufferData = new byte[512];
|
||||
private ByteBuffer buffer = new ByteBuffer(bufferData);
|
||||
|
||||
public TOutputStreamWriter(TOutputStream out) {
|
||||
this(out, "UTF-8");
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an OutputStreamWriter that uses the named character encoding.
|
||||
* os - An OutputStreamenc - The name of a supported
|
||||
* - If the named encoding is not supported
|
||||
*/
|
||||
public TOutputStreamWriter(TOutputStream os, TString enc) throws TUnsupportedEncodingException{
|
||||
this.os = os;
|
||||
this.enc = enc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the stream.
|
||||
*/
|
||||
public void close() throws TIOException{
|
||||
os.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush the stream.
|
||||
*/
|
||||
public void flush() throws TIOException{
|
||||
os.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a portion of an array of characters.
|
||||
*/
|
||||
public void write(char[] cbuf, int off, int len) throws TIOException{
|
||||
write(new TString(cbuf, off, len));
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a single character.
|
||||
*/
|
||||
public void write(int c) throws TIOException{
|
||||
write(new TString(new char[] {(char)c}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a portion of a string.
|
||||
*/
|
||||
public void write(TString str, int off, int len) throws TIOException{
|
||||
if(off > 0 || len != str.length()) {
|
||||
str = str.substring(off, len);
|
||||
public TOutputStreamWriter(TOutputStream out, final String enc) throws TUnsupportedEncodingException {
|
||||
super(out);
|
||||
if (enc == null) {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
os.write(str.getBytes(enc));
|
||||
this.out = out;
|
||||
charset = Charset.get(enc);
|
||||
if (charset == null) {
|
||||
throw new TUnsupportedEncodingException(TString.wrap(enc));
|
||||
}
|
||||
encoding = enc;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws TIOException {
|
||||
if (charset != null) {
|
||||
flush();
|
||||
charset = null;
|
||||
out.flush();
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() throws TIOException {
|
||||
checkStatus();
|
||||
if (buffer.position() > 0) {
|
||||
out.write(bufferData, 0, buffer.position());
|
||||
buffer.rewind(0);
|
||||
}
|
||||
out.flush();
|
||||
}
|
||||
|
||||
private void checkStatus() throws TIOException {
|
||||
if (charset == null) {
|
||||
throw new TIOException(TString.wrap("Writer already closed"));
|
||||
}
|
||||
}
|
||||
|
||||
public String getEncoding() {
|
||||
return encoding;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(char[] buf, int offset, int count) throws TIOException {
|
||||
synchronized (lock) {
|
||||
checkStatus();
|
||||
if (buf == null) {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
if (offset < 0 || offset > buf.length - count || count < 0) {
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
CharBuffer input = new CharBuffer(buf, offset, offset + count);
|
||||
while (!input.end()) {
|
||||
if (buffer.available() < 6) {
|
||||
out.write(bufferData, 0, buffer.position());
|
||||
buffer.rewind(0);
|
||||
}
|
||||
charset.encode(input, buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(int oneChar) throws TIOException {
|
||||
synchronized (lock) {
|
||||
checkStatus();
|
||||
CharBuffer input = new CharBuffer(new char[] { (char)oneChar }, 0, 1);
|
||||
while (!input.end()) {
|
||||
if (buffer.available() < 6) {
|
||||
out.write(bufferData, 0, buffer.position());
|
||||
buffer.rewind(0);
|
||||
}
|
||||
charset.encode(input, buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(String str, int offset, int count) throws TIOException {
|
||||
if (str == null) {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
if (count < 0) {
|
||||
throw new IndexOutOfBoundsException("Negative count: " + count);
|
||||
}
|
||||
char[] chars = new char[count];
|
||||
str.getChars(offset, offset + count, chars, 0);
|
||||
write(chars);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,108 +1,82 @@
|
|||
/*
|
||||
* Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Codename One designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
* Copyright 2015 Alexey Andreev.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
* 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
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Please contact Codename One through http://www.codenameone.com/ if you
|
||||
* need additional information or have any questions.
|
||||
* 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.*;
|
||||
/**
|
||||
* Abstract class for writing to character streams. The only methods that a subclass must implement are write(char[], int, int), flush(), and close(). Most subclasses, however, will override some of the methods defined here in order to provide higher efficiency, additional functionality, or both.
|
||||
* Since: JDK1.1, CLDC 1.0 See Also:OutputStreamWriter, Reader
|
||||
*/
|
||||
public abstract class TWriter extends TObject{
|
||||
/**
|
||||
* The object used to synchronize operations on this stream. For efficiency, a character-stream object may use an object other than itself to protect critical sections. A subclass should therefore use the object in this field rather than this or a synchronized method.
|
||||
*/
|
||||
protected TObject lock;
|
||||
import org.teavm.classlib.java.lang.TAppendable;
|
||||
import org.teavm.classlib.java.lang.TCharSequence;
|
||||
|
||||
/**
|
||||
* Create a new character-stream writer whose critical sections will synchronize on the writer itself.
|
||||
*/
|
||||
protected TWriter(){
|
||||
public abstract class TWriter implements TAppendable, TCloseable, TFlushable {
|
||||
protected Object lock;
|
||||
|
||||
protected TWriter() {
|
||||
super();
|
||||
lock = this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new character-stream writer whose critical sections will synchronize on the given object.
|
||||
* lock - Object to synchronize on.
|
||||
*/
|
||||
protected TWriter(TObject lock){
|
||||
protected TWriter(Object lock) {
|
||||
if (lock == null) {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
this.lock = lock;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the stream, flushing it first. Once a stream has been closed, further write() or flush() invocations will cause an IOException to be thrown. Closing a previously-closed stream, however, has no effect.
|
||||
*/
|
||||
public abstract void close() throws TIOException;
|
||||
|
||||
/**
|
||||
* Flush the stream. If the stream has saved any characters from the various write() methods in a buffer, write them immediately to their intended destination. Then, if that destination is another character or byte stream, flush it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams.
|
||||
*/
|
||||
public abstract void flush() throws TIOException;
|
||||
|
||||
/**
|
||||
* Write an array of characters.
|
||||
*/
|
||||
public void write(char[] cbuf) throws TIOException{
|
||||
write(cbuf, 0, cbuf.length);
|
||||
public void write(char buf[]) throws TIOException {
|
||||
write(buf, 0, buf.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a portion of an array of characters.
|
||||
*/
|
||||
public abstract void write(char[] cbuf, int off, int len) throws TIOException;
|
||||
public abstract void write(char buf[], int offset, int count) throws TIOException;
|
||||
|
||||
/**
|
||||
* Write a single character. The character to be written is contained in the 16 low-order bits of the given integer value; the 16 high-order bits are ignored.
|
||||
* Subclasses that intend to support efficient single-character output should override this method.
|
||||
*/
|
||||
public void write(int c) throws TIOException{
|
||||
public void write(int oneChar) throws TIOException {
|
||||
synchronized (lock) {
|
||||
char oneCharArray[] = new char[1];
|
||||
oneCharArray[0] = (char) c;
|
||||
oneCharArray[0] = (char) oneChar;
|
||||
write(oneCharArray);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a string.
|
||||
*/
|
||||
public void write(TString str) throws TIOException{
|
||||
public void write(String str) throws TIOException {
|
||||
write(str, 0, str.length());
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a portion of a string.
|
||||
*/
|
||||
public void write(TString str, int off, int len) throws TIOException{
|
||||
if (len < 0) {
|
||||
public void write(String str, int offset, int count) throws TIOException {
|
||||
if (count < 0) {
|
||||
throw new StringIndexOutOfBoundsException();
|
||||
}
|
||||
char buf[] = new char[len];
|
||||
str.getChars(off, off + len, buf, 0);
|
||||
|
||||
char buf[] = new char[count];
|
||||
str.getChars(offset, offset + count, buf, 0);
|
||||
synchronized (lock) {
|
||||
write(buf, 0, buf.length);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public TWriter append(char c) throws TIOException {
|
||||
write(c);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TWriter append(TCharSequence csq) throws TIOException {
|
||||
write(csq != null ? csq.toString() : "null");
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TWriter append(TCharSequence csq, int start, int end) throws TIOException {
|
||||
write(csq != null ? csq.subSequence(start, end).toString() : "null");
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -249,7 +249,7 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ
|
|||
exp = 0;
|
||||
float digit = 1;
|
||||
for (int i = powersOfTen.length - 1; i >= 0; --i) {
|
||||
if ((exp | bit) <= FLOAT_MAX_EXPONENT && powersOfTen[i] * digit < value) {
|
||||
if ((exp | bit) <= FLOAT_MAX_EXPONENT && powersOfTen[i] * digit <= value) {
|
||||
digit *= powersOfTen[i];
|
||||
exp |= bit;
|
||||
}
|
||||
|
@ -290,7 +290,6 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ
|
|||
exp = 0;
|
||||
}
|
||||
}
|
||||
sz += digits;
|
||||
|
||||
// Extend buffer to store exponent
|
||||
if (exp != 0) {
|
||||
|
@ -303,6 +302,11 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ
|
|||
}
|
||||
}
|
||||
|
||||
if (exp != 0 && digits == intPart) {
|
||||
digits++;
|
||||
}
|
||||
sz += digits;
|
||||
|
||||
// Print mantissa
|
||||
insertSpace(target, target + sz);
|
||||
if (negative) {
|
||||
|
@ -399,7 +403,7 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ
|
|||
exp = 0;
|
||||
double digit = 1;
|
||||
for (int i = doublePowersOfTen.length - 1; i >= 0; --i) {
|
||||
if ((exp | bit) <= DOUBLE_MAX_EXPONENT && doublePowersOfTen[i] * digit < value) {
|
||||
if ((exp | bit) <= DOUBLE_MAX_EXPONENT && doublePowersOfTen[i] * digit <= value) {
|
||||
digit *= doublePowersOfTen[i];
|
||||
exp |= bit;
|
||||
}
|
||||
|
@ -440,7 +444,6 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ
|
|||
exp = 0;
|
||||
}
|
||||
}
|
||||
sz += digits;
|
||||
|
||||
// Extend buffer to store exponent
|
||||
if (exp != 0) {
|
||||
|
@ -456,6 +459,11 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ
|
|||
}
|
||||
}
|
||||
|
||||
if (exp != 0 && digits == intPart) {
|
||||
digits++;
|
||||
}
|
||||
sz += digits;
|
||||
|
||||
// Print mantissa
|
||||
insertSpace(target, target + sz);
|
||||
if (negative) {
|
||||
|
|
|
@ -96,16 +96,20 @@ public class TObject {
|
|||
}
|
||||
|
||||
o.monitor.owner = null;
|
||||
Platform.startThread(new PlatformRunnable() {
|
||||
@Override public void run() {
|
||||
if (o.isEmptyMonitor() || o.monitor.owner != null) {
|
||||
return;
|
||||
if (!o.monitor.enteringThreads.isEmpty()) {
|
||||
Platform.startThread(new PlatformRunnable() {
|
||||
@Override public void run() {
|
||||
if (o.isEmptyMonitor() || o.monitor.owner != null) {
|
||||
return;
|
||||
}
|
||||
if (!o.monitor.enteringThreads.isEmpty()) {
|
||||
o.monitor.enteringThreads.remove().run();
|
||||
}
|
||||
}
|
||||
if (!o.monitor.enteringThreads.isEmpty()) {
|
||||
o.monitor.enteringThreads.remove().run();
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
} else {
|
||||
o.isEmptyMonitor();
|
||||
}
|
||||
}
|
||||
|
||||
boolean isEmptyMonitor() {
|
||||
|
@ -216,6 +220,9 @@ public class TObject {
|
|||
|
||||
@Rename("wait")
|
||||
public final void wait0(long timeout, int nanos, final AsyncCallback<Void> callback) {
|
||||
if (!holdsLock(this)) {
|
||||
throw new TIllegalMonitorStateException();
|
||||
}
|
||||
final NotifyListenerImpl listener = new NotifyListenerImpl(this, callback, monitor.count);
|
||||
monitor.notifyListeners.add(listener);
|
||||
if (timeout > 0 || nanos > 0) {
|
||||
|
|
|
@ -101,7 +101,7 @@ public class TThrowable extends RuntimeException {
|
|||
}
|
||||
|
||||
@Override
|
||||
public synchronized Throwable fillInStackTrace() {
|
||||
public Throwable fillInStackTrace() {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -116,7 +116,7 @@ public class TThrowable extends RuntimeException {
|
|||
}
|
||||
|
||||
@Override
|
||||
public synchronized TThrowable getCause() {
|
||||
public TThrowable getCause() {
|
||||
return cause != this ? cause : null;
|
||||
}
|
||||
|
||||
|
@ -126,7 +126,7 @@ public class TThrowable extends RuntimeException {
|
|||
@Remove
|
||||
public native TString toString0();
|
||||
|
||||
public synchronized TThrowable initCause(TThrowable cause) {
|
||||
public TThrowable initCause(TThrowable cause) {
|
||||
if (this.cause != this && this.cause != null) {
|
||||
throw new TIllegalStateException(TString.wrap("Cause already set"));
|
||||
}
|
||||
|
|
|
@ -293,7 +293,7 @@ public abstract class TCalendar implements TSerializable, TCloneable, TComparabl
|
|||
return value;
|
||||
}
|
||||
|
||||
public static synchronized TLocale[] getAvailableLocales() {
|
||||
public static TLocale[] getAvailableLocales() {
|
||||
return TLocale.getAvailableLocales();
|
||||
}
|
||||
|
||||
|
@ -303,11 +303,11 @@ public abstract class TCalendar implements TSerializable, TCloneable, TComparabl
|
|||
|
||||
abstract public int getGreatestMinimum(int field);
|
||||
|
||||
public static synchronized TCalendar getInstance() {
|
||||
public static TCalendar getInstance() {
|
||||
return new TGregorianCalendar();
|
||||
}
|
||||
|
||||
public static synchronized TCalendar getInstance(TLocale locale) {
|
||||
public static TCalendar getInstance(TLocale locale) {
|
||||
return new TGregorianCalendar(locale);
|
||||
}
|
||||
|
||||
|
|
|
@ -244,11 +244,11 @@ public final class TLocale implements TCloneable, TSerializable {
|
|||
}
|
||||
|
||||
@Override
|
||||
public synchronized int hashCode() {
|
||||
public int hashCode() {
|
||||
return countryCode.hashCode() + languageCode.hashCode() + variantCode.hashCode();
|
||||
}
|
||||
|
||||
public synchronized static void setDefault(TLocale locale) {
|
||||
public static void setDefault(TLocale locale) {
|
||||
if (locale != null) {
|
||||
defaultLocale = locale;
|
||||
} else {
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* 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.impl.unicode;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import org.junit.Test;
|
||||
|
||||
public class UnicodeSupportTest {
|
||||
private static boolean pairsEqual(final int[] pairs, final int index1, final int index2) {
|
||||
return pairs[index1] == pairs[index2] && pairs[index1 + 1] == pairs[index2 + 1];
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_getDigitValues() {
|
||||
final int[] digitValues = UnicodeSupport.getDigitValues();
|
||||
if (digitValues.length >= 4) {
|
||||
// there are no duplicates, so the last two pairs should not be identical
|
||||
assertFalse(pairsEqual(digitValues, digitValues.length - 4, digitValues.length - 2));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -80,6 +80,11 @@ public class DefaultNamingStrategy implements NamingStrategy {
|
|||
return getFullNameFor(method, 'S');
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFullNameForAsync(MethodReference method) throws NamingException {
|
||||
return getFullNameFor(method, 'A');
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getNameForInit(MethodReference method) throws NamingException {
|
||||
return getFullNameFor(method, 'I');
|
||||
|
|
|
@ -33,5 +33,7 @@ public interface NamingStrategy {
|
|||
|
||||
String getFullNameFor(MethodReference method) throws NamingException;
|
||||
|
||||
String getFullNameForAsync(MethodReference method) throws NamingException;
|
||||
|
||||
String getNameFor(FieldReference field) throws NamingException;
|
||||
}
|
||||
|
|
|
@ -496,8 +496,7 @@ class DependencyGraphBuilder {
|
|||
@Override
|
||||
public void monitorEnter(VariableReader objectRef) {
|
||||
MethodDependency methodDep = dependencyChecker.linkMethod(
|
||||
new MethodReference(Object.class, "monitorEnter", Object.class, void.class),
|
||||
new CallLocation(caller.getMethod(), currentLocation));
|
||||
new MethodReference(Object.class, "monitorEnter", Object.class, void.class), null);
|
||||
nodes[objectRef.getIndex()].connect(methodDep.getVariable(1));
|
||||
methodDep.use();
|
||||
}
|
||||
|
@ -505,8 +504,7 @@ class DependencyGraphBuilder {
|
|||
@Override
|
||||
public void monitorExit(VariableReader objectRef) {
|
||||
MethodDependency methodDep = dependencyChecker.linkMethod(
|
||||
new MethodReference(Object.class, "monitorExit", Object.class, void.class),
|
||||
new CallLocation(caller.getMethod(), currentLocation));
|
||||
new MethodReference(Object.class, "monitorExit", Object.class, void.class), null);
|
||||
nodes[objectRef.getIndex()].connect(methodDep.getVariable(1));
|
||||
methodDep.use();
|
||||
}
|
||||
|
|
|
@ -71,34 +71,6 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
|
|||
boolean wasGrouped;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(MonitorEnterStatement statement) {
|
||||
try {
|
||||
MethodReference monitorEnterRef = new MethodReference(
|
||||
Object.class, "monitorEnter", Object.class, void.class);
|
||||
writer.appendMethodBody(monitorEnterRef).append("(");
|
||||
statement.getObjectRef().acceptVisitor(this);
|
||||
writer.append(",").ws();
|
||||
writer.append("$rt_continue($part_").append(statement.getAsyncTarget()).append(')');
|
||||
writer.append(");").softNewLine();
|
||||
} catch (IOException ex){
|
||||
throw new RenderingException("IO error occured", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(MonitorExitStatement statement) {
|
||||
try {
|
||||
MethodReference monitorExitRef = new MethodReference(
|
||||
Object.class, "monitorExit", Object.class, void.class);
|
||||
writer.appendMethodBody(monitorExitRef).append("(");
|
||||
statement.getObjectRef().acceptVisitor(this);
|
||||
writer.append(");").softNewLine();
|
||||
} catch (IOException ex){
|
||||
throw new RenderingException("IO error occured", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static class InjectorHolder {
|
||||
public final Injector injector;
|
||||
|
||||
|
@ -431,7 +403,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
|
|||
writer.append("],").ws();
|
||||
int flags = 0;
|
||||
if (cls.getModifiers().contains(NodeModifier.ENUM)) {
|
||||
flags &= 1;
|
||||
flags |= 1;
|
||||
}
|
||||
writer.append(flags).append(',').ws();
|
||||
MethodHolder clinit = classSource.get(cls.getName()).getMethod(
|
||||
|
@ -448,7 +420,12 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
|
|||
for (MethodNode method : cls.getMethods()) {
|
||||
if (clinit != null && (method.getModifiers().contains(NodeModifier.STATIC) ||
|
||||
method.getReference().getName().equals("<init>"))) {
|
||||
stubNames.add(naming.getFullNameFor(method.getReference()));
|
||||
if (!method.isAsync()) {
|
||||
stubNames.add(naming.getFullNameFor(method.getReference()));
|
||||
}
|
||||
if (asyncFamilyMethods.contains(method.getReference())) {
|
||||
stubNames.add(naming.getFullNameForAsync(method.getReference()));
|
||||
}
|
||||
}
|
||||
if (!method.getModifiers().contains(NodeModifier.STATIC)) {
|
||||
virtualMethods.add(method);
|
||||
|
@ -537,51 +514,58 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
|
|||
writer.append(",").ws();
|
||||
}
|
||||
first = false;
|
||||
String methodName = method.isAsync() ? naming.getNameForAsync(ref) : naming.getNameFor(ref);
|
||||
writer.append("\"").append(methodName).append("\"");
|
||||
writer.append(",").ws().append("function(");
|
||||
List<String> args = new ArrayList<>();
|
||||
for (int i = 1; i <= ref.parameterCount(); ++i) {
|
||||
args.add(variableName(i));
|
||||
}
|
||||
if (method.isAsync()) {
|
||||
args.add("$return");
|
||||
}
|
||||
for (int i = 0; i < args.size(); ++i) {
|
||||
if (i > 0) {
|
||||
emitVirtualDeclaration(ref, true);
|
||||
} else {
|
||||
emitVirtualDeclaration(ref, false);
|
||||
if (asyncFamilyMethods.contains(ref)) {
|
||||
writer.append(",").ws();
|
||||
emitVirtualDeclaration(ref, true);
|
||||
}
|
||||
writer.append(args.get(i));
|
||||
}
|
||||
writer.append(")").ws().append("{").ws();
|
||||
if (ref.getDescriptor().getResultType() != ValueType.VOID) {
|
||||
writer.append("return ");
|
||||
}
|
||||
writer.appendMethodBody(ref).append("(");
|
||||
writer.append("this");
|
||||
for (int i = 0; i < args.size(); ++i) {
|
||||
writer.append(",").ws().append(args.get(i));
|
||||
}
|
||||
writer.append(");").ws().append("}");
|
||||
debugEmitter.emitMethod(null);
|
||||
|
||||
if (!method.isAsync() && asyncFamilyMethods.contains(method.getReference())) {
|
||||
writer.append(",").newLine();
|
||||
writer.append("\"").append(naming.getNameForAsync(ref)).append("\",").ws();
|
||||
writer.append("$rt_asyncAdapter(").appendMethodBody(ref).append(')');
|
||||
}
|
||||
}
|
||||
writer.append("]");
|
||||
}
|
||||
|
||||
private void emitVirtualDeclaration(MethodReference ref, boolean async) throws IOException {
|
||||
String methodName = async ? naming.getNameForAsync(ref) : naming.getNameFor(ref);
|
||||
writer.append("\"").append(methodName).append("\"");
|
||||
writer.append(",").ws().append("function(");
|
||||
List<String> args = new ArrayList<>();
|
||||
for (int i = 1; i <= ref.parameterCount(); ++i) {
|
||||
args.add(variableName(i));
|
||||
}
|
||||
if (async) {
|
||||
args.add("$return");
|
||||
}
|
||||
for (int i = 0; i < args.size(); ++i) {
|
||||
if (i > 0) {
|
||||
writer.append(",").ws();
|
||||
}
|
||||
writer.append(args.get(i));
|
||||
}
|
||||
writer.append(")").ws().append("{").ws();
|
||||
if (ref.getDescriptor().getResultType() != ValueType.VOID) {
|
||||
writer.append("return ");
|
||||
}
|
||||
writer.append(async ? naming.getFullNameForAsync(ref) : naming.getFullNameFor(ref)).append("(");
|
||||
writer.append("this");
|
||||
for (int i = 0; i < args.size(); ++i) {
|
||||
writer.append(",").ws().append(args.get(i));
|
||||
}
|
||||
writer.append(");").ws().append("}");
|
||||
}
|
||||
|
||||
public void renderBody(MethodNode method, boolean inner) throws IOException {
|
||||
blockIdMap.clear();
|
||||
MethodReference ref = method.getReference();
|
||||
debugEmitter.emitMethod(ref.getDescriptor());
|
||||
String name = method.isAsync() ? naming.getFullNameForAsync(ref) : naming.getFullNameFor(ref);
|
||||
if (inner) {
|
||||
writer.appendMethodBody(ref).ws().append("=").ws().append("function(");
|
||||
writer.append(name).ws().append("=").ws().append("function(");
|
||||
} else {
|
||||
writer.append("function ").appendMethodBody(ref).append("(");
|
||||
writer.append("function ").append(name).append("(");
|
||||
}
|
||||
int startParam = 0;
|
||||
if (method.getModifiers().contains(NodeModifier.STATIC)) {
|
||||
|
@ -606,6 +590,40 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
|
|||
writer.append(';');
|
||||
}
|
||||
writer.newLine();
|
||||
|
||||
if (!method.isAsync() && asyncFamilyMethods.contains(method.getReference())) {
|
||||
if (inner) {
|
||||
writer.append(naming.getFullNameForAsync(ref)).ws().append("=").ws().append("function(");
|
||||
} else {
|
||||
writer.append("function ").append(naming.getFullNameForAsync(ref)).append("(");
|
||||
}
|
||||
for (int i = startParam; i <= ref.parameterCount(); ++i) {
|
||||
writer.append(variableName(i));
|
||||
writer.append(",").ws();
|
||||
}
|
||||
writer.append("$return)").ws().append("{").softNewLine().indent();
|
||||
|
||||
writer.append("var $r;").softNewLine();
|
||||
writer.append("try").ws().append('{').indent().softNewLine();
|
||||
writer.append("$r").ws().append("=").ws().appendMethodBody(ref).append('(');
|
||||
for (int i = startParam; i <= ref.parameterCount(); ++i) {
|
||||
if (i > startParam) {
|
||||
writer.append(",").ws();
|
||||
}
|
||||
writer.append(variableName(i));
|
||||
}
|
||||
writer.append(");").softNewLine();
|
||||
writer.outdent().append("}").ws().append("catch").ws().append("($e)").ws()
|
||||
.append("{").indent().softNewLine();
|
||||
writer.append("return $return($rt_asyncError($e));").softNewLine();
|
||||
writer.outdent().append("}");
|
||||
writer.append("$return($rt_asyncResult($r));").softNewLine();
|
||||
writer.outdent().append("}");
|
||||
if (inner) {
|
||||
writer.append(';');
|
||||
}
|
||||
writer.newLine();
|
||||
}
|
||||
debugEmitter.emitMethod(null);
|
||||
}
|
||||
|
||||
|
@ -752,6 +770,11 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
|
|||
return asyncMethods.contains(method);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAsyncFamily(MethodReference method) {
|
||||
return asyncFamilyMethods.contains(method);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCompleteContinuation() {
|
||||
return "$return";
|
||||
|
@ -1071,6 +1094,14 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
|
|||
|
||||
@Override
|
||||
public void visit(InitClassStatement statement) {
|
||||
ClassReader cls = classSource.get(statement.getClassName());
|
||||
if (cls == null) {
|
||||
return;
|
||||
}
|
||||
MethodReader method = cls.getMethod(new MethodDescriptor("<clinit>", void.class));
|
||||
if (method == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
debugEmitter.emitStatementStart();
|
||||
if (statement.getLocation() != null) {
|
||||
|
@ -1287,7 +1318,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
|
|||
exitPriority();
|
||||
break;
|
||||
case NEGATE:
|
||||
enterPriority(Priority.UNARY, Associativity.RIGHT, true);
|
||||
enterPriority(Priority.MULTIPLICATION, Associativity.RIGHT, true);
|
||||
writer.append("-");
|
||||
expr.getOperand().acceptVisitor(this);
|
||||
exitPriority();
|
||||
|
@ -1339,20 +1370,26 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
|
|||
writer.append(')');
|
||||
exitPriority();
|
||||
break;
|
||||
case BYTE_TO_INT:
|
||||
case INT_TO_BYTE:
|
||||
enterPriority(Priority.BITWISE_SHIFT, Associativity.LEFT, true);
|
||||
writer.append("(");
|
||||
expr.getOperand().acceptVisitor(this);
|
||||
writer.ws().append("<<").ws().append("24)").ws().append(">>").ws().append("24");
|
||||
exitPriority();
|
||||
break;
|
||||
case SHORT_TO_INT:
|
||||
case INT_TO_SHORT:
|
||||
enterPriority(Priority.BITWISE_SHIFT, Associativity.LEFT, true);
|
||||
writer.append("(");
|
||||
expr.getOperand().acceptVisitor(this);
|
||||
writer.ws().append("<<").ws().append("16)").ws().append(">>").ws().append("16");
|
||||
exitPriority();
|
||||
break;
|
||||
case INT_TO_CHAR:
|
||||
enterPriority(Priority.BITWISE_AND, Associativity.LEFT, true);
|
||||
expr.getOperand().acceptVisitor(this);
|
||||
writer.ws().append("&").ws().append("65535");
|
||||
exitPriority();
|
||||
break;
|
||||
case NULL_CHECK:
|
||||
enterPriority(Priority.COMMA, Associativity.NONE, false);
|
||||
writer.append("$rt_nullCheck(");
|
||||
|
@ -1396,7 +1433,14 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
|
|||
if (expr.getLocation() != null) {
|
||||
pushLocation(expr.getLocation());
|
||||
}
|
||||
writer.append(constantToString(expr.getValue()));
|
||||
String str = constantToString(expr.getValue());
|
||||
if (str.startsWith("-")) {
|
||||
enterPriority(Priority.MULTIPLICATION, Associativity.RIGHT, true);
|
||||
}
|
||||
writer.append(str);
|
||||
if (str.startsWith("-")) {
|
||||
exitPriority();
|
||||
}
|
||||
if (expr.getLocation() != null) {
|
||||
popLocation();
|
||||
}
|
||||
|
@ -1596,14 +1640,15 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
|
|||
if (injector != null) {
|
||||
injector.generate(new InjectorContextImpl(expr.getArguments()), expr.getMethod());
|
||||
} else {
|
||||
if (expr.getAsyncTarget() != null) {
|
||||
boolean asyncCall = expr.getAsyncTarget() != null;
|
||||
if (asyncCall) {
|
||||
writer.append("return ");
|
||||
}
|
||||
if (expr.getType() == InvocationType.DYNAMIC) {
|
||||
expr.getArguments().get(0).acceptVisitor(this);
|
||||
}
|
||||
String name = expr.getAsyncTarget() == null ? naming.getNameFor(expr.getMethod()) :
|
||||
naming.getNameForAsync(expr.getMethod());
|
||||
MethodReference method = expr.getMethod();
|
||||
String name = asyncCall ? naming.getNameForAsync(method) : naming.getNameFor(method);
|
||||
DeferredCallSite callSite = prevCallSite;
|
||||
boolean shouldEraseCallSite = lastCallSite == null;
|
||||
if (lastCallSite == null) {
|
||||
|
@ -1614,7 +1659,8 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
|
|||
enterPriority(Priority.COMMA, Associativity.NONE, false);
|
||||
switch (expr.getType()) {
|
||||
case STATIC:
|
||||
writer.appendMethodBody(expr.getMethod()).append("(");
|
||||
writer.append(asyncCall ? naming.getFullNameForAsync(method) :
|
||||
naming.getFullNameFor(method)).append("(");
|
||||
prevCallSite = debugEmitter.emitCallSite();
|
||||
for (int i = 0; i < expr.getArguments().size(); ++i) {
|
||||
if (i > 0) {
|
||||
|
@ -1625,7 +1671,8 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
|
|||
}
|
||||
break;
|
||||
case SPECIAL:
|
||||
writer.appendMethodBody(expr.getMethod()).append("(");
|
||||
writer.append(asyncCall ? naming.getFullNameForAsync(method) :
|
||||
naming.getFullNameFor(method)).append("(");
|
||||
prevCallSite = debugEmitter.emitCallSite();
|
||||
expr.getArguments().get(0).acceptVisitor(this);
|
||||
hasParams = true;
|
||||
|
@ -1952,16 +1999,54 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(MonitorEnterStatement statement) {
|
||||
if (!async) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
MethodReference monitorEnterRef = new MethodReference(
|
||||
Object.class, "monitorEnter", Object.class, void.class);
|
||||
writer.append("return ").append(naming.getFullNameForAsync(monitorEnterRef)).append("(");
|
||||
statement.getObjectRef().acceptVisitor(this);
|
||||
writer.append(",").ws();
|
||||
writer.append("$rt_continue($part_").append(statement.getAsyncTarget()).append(')');
|
||||
writer.append(");").softNewLine();
|
||||
} catch (IOException ex){
|
||||
throw new RenderingException("IO error occured", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(MonitorExitStatement statement) {
|
||||
if (!async) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
MethodReference monitorExitRef = new MethodReference(
|
||||
Object.class, "monitorExit", Object.class, void.class);
|
||||
writer.appendMethodBody(monitorExitRef).append("(");
|
||||
statement.getObjectRef().acceptVisitor(this);
|
||||
writer.append(");").softNewLine();
|
||||
} catch (IOException ex){
|
||||
throw new RenderingException("IO error occured", ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private Injector getInjector(MethodReference ref) {
|
||||
InjectorHolder holder = injectorMap.get(ref);
|
||||
if (holder == null) {
|
||||
MethodHolder method = classSource.get(ref.getClassName()).getMethod(ref.getDescriptor());
|
||||
holder = new InjectorHolder(null);
|
||||
if (method != null) {
|
||||
AnnotationHolder injectedByAnnot = method.getAnnotations().get(InjectedBy.class.getName());
|
||||
if (injectedByAnnot != null) {
|
||||
ValueType type = injectedByAnnot.getValues().get("value").getJavaClass();
|
||||
holder = new InjectorHolder(instantiateInjector(((ValueType.Object)type).getClassName()));
|
||||
ClassHolder cls = classSource.get(ref.getClassName());
|
||||
if (cls != null) {
|
||||
MethodHolder method = cls.getMethod(ref.getDescriptor());
|
||||
if (method != null) {
|
||||
AnnotationHolder injectedByAnnot = method.getAnnotations().get(InjectedBy.class.getName());
|
||||
if (injectedByAnnot != null) {
|
||||
ValueType type = injectedByAnnot.getValues().get("value").getJavaClass();
|
||||
holder = new InjectorHolder(instantiateInjector(((ValueType.Object)type).getClassName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
injectorMap.put(ref, holder);
|
||||
|
|
|
@ -304,25 +304,17 @@ class StatementGenerator implements InstructionVisitor {
|
|||
case FROM_INTEGER:
|
||||
switch (insn.getTargetType()) {
|
||||
case BYTE:
|
||||
value = Expr.binary(BinaryOperation.BITWISE_AND, value, Expr.constant(0xFF));
|
||||
value = Expr.unary(UnaryOperation.INT_TO_BYTE, value);
|
||||
break;
|
||||
case SHORT:
|
||||
value = Expr.unary(UnaryOperation.INT_TO_SHORT, value);
|
||||
break;
|
||||
case CHARACTER:
|
||||
value = Expr.binary(BinaryOperation.BITWISE_AND, value, Expr.constant(0xFFFF));
|
||||
value = Expr.unary(UnaryOperation.INT_TO_CHAR, value);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case TO_INTEGER:
|
||||
switch (insn.getTargetType()) {
|
||||
case BYTE:
|
||||
value = Expr.unary(UnaryOperation.BYTE_TO_INT, value);
|
||||
break;
|
||||
case SHORT:
|
||||
value = Expr.unary(UnaryOperation.SHORT_TO_INT, value);
|
||||
break;
|
||||
case CHARACTER:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
assign(value, insn.getReceiver());
|
||||
|
|
|
@ -29,7 +29,8 @@ public enum UnaryOperation {
|
|||
LONG_TO_INT,
|
||||
NUM_TO_LONG,
|
||||
INT_TO_LONG,
|
||||
BYTE_TO_INT,
|
||||
SHORT_TO_INT,
|
||||
INT_TO_BYTE,
|
||||
INT_TO_SHORT,
|
||||
INT_TO_CHAR,
|
||||
NULL_CHECK
|
||||
}
|
||||
|
|
|
@ -40,5 +40,7 @@ public interface GeneratorContext extends ServiceRepository {
|
|||
|
||||
boolean isAsync(MethodReference method);
|
||||
|
||||
boolean isAsyncFamily(MethodReference method);
|
||||
|
||||
Diagnostics getDiagnostics();
|
||||
}
|
||||
|
|
|
@ -24,7 +24,6 @@ import org.teavm.javascript.spi.Async;
|
|||
import org.teavm.javascript.spi.InjectedBy;
|
||||
import org.teavm.javascript.spi.Sync;
|
||||
import org.teavm.model.*;
|
||||
import org.teavm.model.instructions.*;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -62,17 +61,6 @@ public class AsyncMethodFinder {
|
|||
}
|
||||
if (method.getAnnotations().get(Async.class.getName()) != null) {
|
||||
add(method.getReference());
|
||||
} else if (method.getProgram() != null) {
|
||||
ProgramReader program = method.getProgram();
|
||||
AsyncInstructionFinder insnFinder = new AsyncInstructionFinder();
|
||||
for (int i = 0; i < program.basicBlockCount(); ++i) {
|
||||
BasicBlockReader block = program.basicBlockAt(i);
|
||||
block.readAllInstructions(insnFinder);
|
||||
if (insnFinder.hasAsync) {
|
||||
add(method.getReference());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -197,166 +185,4 @@ public class AsyncMethodFinder {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class AsyncInstructionFinder implements InstructionReader {
|
||||
boolean hasAsync;
|
||||
|
||||
@Override
|
||||
public void location(InstructionLocation location) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void nop() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void classConstant(VariableReader receiver, ValueType cst) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void nullConstant(VariableReader receiver) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void integerConstant(VariableReader receiver, int cst) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void longConstant(VariableReader receiver, long cst) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void floatConstant(VariableReader receiver, float cst) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doubleConstant(VariableReader receiver, double cst) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stringConstant(VariableReader receiver, String cst) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void binary(BinaryOperation op, VariableReader receiver, VariableReader first, VariableReader second,
|
||||
NumericOperandType type) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void negate(VariableReader receiver, VariableReader operand, NumericOperandType type) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void assign(VariableReader receiver, VariableReader assignee) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cast(VariableReader receiver, VariableReader value, ValueType targetType) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cast(VariableReader receiver, VariableReader value, NumericOperandType sourceType,
|
||||
NumericOperandType targetType) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cast(VariableReader receiver, VariableReader value, IntegerSubtype type,
|
||||
CastIntegerDirection targetType) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jumpIf(BranchingCondition cond, VariableReader operand, BasicBlockReader consequent,
|
||||
BasicBlockReader alternative) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jumpIf(BinaryBranchingCondition cond, VariableReader first, VariableReader second,
|
||||
BasicBlockReader consequent, BasicBlockReader alternative) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jump(BasicBlockReader target) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void choose(VariableReader condition, List<? extends SwitchTableEntryReader> table,
|
||||
BasicBlockReader defaultTarget) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exit(VariableReader valueToReturn) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void raise(VariableReader exception) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createArray(VariableReader receiver, ValueType itemType, VariableReader size) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createArray(VariableReader receiver, ValueType itemType,
|
||||
List<? extends VariableReader> dimensions) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void create(VariableReader receiver, String type) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getField(VariableReader receiver, VariableReader instance, FieldReference field,
|
||||
ValueType fieldType) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putField(VariableReader instance, FieldReference field, VariableReader value) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void arrayLength(VariableReader receiver, VariableReader array) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cloneArray(VariableReader receiver, VariableReader array) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unwrapArray(VariableReader receiver, VariableReader array, ArrayElementType elementType) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getElement(VariableReader receiver, VariableReader array, VariableReader index) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putElement(VariableReader array, VariableReader index, VariableReader value) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke(VariableReader receiver, VariableReader instance, MethodReference method,
|
||||
List<? extends VariableReader> arguments, InvocationType type) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void isInstance(VariableReader receiver, VariableReader value, ValueType type) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initClass(String className) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void nullCheck(VariableReader receiver, VariableReader value) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void monitorEnter(VariableReader objectRef) {
|
||||
hasAsync = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void monitorExit(VariableReader objectRef) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -436,13 +436,13 @@ public class TeaVM implements TeaVMHost, ServiceRepository {
|
|||
renderer.renderStringPool();
|
||||
for (Map.Entry<String, TeaVMEntryPoint> entry : entryPoints.entrySet()) {
|
||||
sourceWriter.append("var ").append(entry.getKey()).ws().append("=").ws();
|
||||
boolean wrapAsync = !asyncMethods.contains(entry.getValue().reference) && entry.getValue().isAsync();
|
||||
MethodReference ref = entry.getValue().reference;
|
||||
boolean asyncMethod = asyncMethods.contains(ref);
|
||||
boolean wrapAsync = !asyncMethod && entry.getValue().isAsync();
|
||||
if (wrapAsync) {
|
||||
sourceWriter.append("$rt_staticAsyncAdapter(");
|
||||
}
|
||||
sourceWriter.appendMethodBody(entry.getValue().reference);
|
||||
if (wrapAsync) {
|
||||
sourceWriter.append(")");
|
||||
sourceWriter.append("$rt_staticAsyncAdapter(").appendMethodBody(ref).append(')');
|
||||
} else {
|
||||
sourceWriter.append(asyncMethod ? naming.getFullNameForAsync(ref) : naming.getFullNameFor(ref));
|
||||
}
|
||||
sourceWriter.append(";").newLine();
|
||||
}
|
||||
|
|
|
@ -328,23 +328,6 @@ function $rt_assertNotNaN(value) {
|
|||
}
|
||||
return value;
|
||||
}
|
||||
function $rt_methodStubs(data) {
|
||||
for (var i = 0; i < data.length; i += 2) {
|
||||
var clinit = data[i + 0];
|
||||
var names = data[i + 1];
|
||||
if (!(names instanceof Array)) {
|
||||
names = [names];
|
||||
}
|
||||
for (var j = 0; j < names.length; j = (j + 1) | 0) {
|
||||
window[names[j]] = (function(name, clinit) {
|
||||
return function() {
|
||||
clinit();
|
||||
return window[name].apply(window, arguments);
|
||||
}
|
||||
})(names[j], clinit);
|
||||
}
|
||||
}
|
||||
}
|
||||
var $rt_stdoutBuffer = "";
|
||||
function $rt_putStdout(ch) {
|
||||
if (ch == 0xA) {
|
||||
|
@ -420,35 +403,6 @@ function $rt_metadata(data) {
|
|||
}
|
||||
}
|
||||
}
|
||||
function $rt_virtualMethods(cls) {
|
||||
for (var i = 1; i < arguments.length; i += 2) {
|
||||
var name = arguments[i];
|
||||
var func = arguments[i + 1];
|
||||
if (typeof name === 'string') {
|
||||
cls.prototype[name] = func;
|
||||
} else {
|
||||
for (var j = 0; j < name.length; ++j) {
|
||||
cls.prototype[name[j]] = func;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
function $rt_virtualMethods(data) {
|
||||
for (var i = 0; i < data.length; i += 2) {
|
||||
var cls = data[i + 0];
|
||||
var methods = data[i + 1];
|
||||
for (var j = 0; j < methods.length; j += 2) {
|
||||
var name = methods[j + 0];
|
||||
var func = methods[j + 1];
|
||||
if (typeof name === 'string') {
|
||||
name = [name];
|
||||
}
|
||||
for (var k = 0; k < name.length; ++k) {
|
||||
cls.prototype[name[k]] = func;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
function $rt_asyncResult(value) {
|
||||
return function() {
|
||||
return value;
|
||||
|
|
|
@ -391,14 +391,19 @@ JUnitClient.run = function() {
|
|||
}
|
||||
JUnitClient.makeErrorMessage = function(message, e) {
|
||||
message.status = "exception";
|
||||
var stack = "";
|
||||
while (e instanceof TeaVMAsyncError) {
|
||||
stack += e.message + "\n" + e.stack + "\n";
|
||||
e = e.cause;
|
||||
}
|
||||
if (e.$javaException && e.$javaException.constructor.$meta) {
|
||||
message.exception = e.$javaException.constructor.$meta.name;
|
||||
message.stack = e.$javaException.constructor.$meta.name + ": ";
|
||||
var exceptionMessage = extractException(e.$javaException);
|
||||
message.stack += exceptionMessage ? $rt_ustr(exceptionMessage) : "";
|
||||
message.stack += "\n" + e.stack;
|
||||
message.stack += e.stack + "\n" + stack;
|
||||
} else {
|
||||
message.stack = e.stack;
|
||||
message.stack = stack;
|
||||
}
|
||||
}
|
||||
JUnitClient.reportError = function(error) {
|
||||
|
|
|
@ -28,12 +28,17 @@ public class JSObjectClassTransformer implements ClassHolderTransformer {
|
|||
|
||||
@Override
|
||||
public void transformClass(ClassHolder cls, ClassReaderSource innerSource, Diagnostics diagnostics) {
|
||||
processor = new JavascriptNativeProcessor(innerSource);
|
||||
if (processor == null || processor.getClassSource() != innerSource) {
|
||||
processor = new JavascriptNativeProcessor(innerSource);
|
||||
}
|
||||
processor.setDiagnostics(diagnostics);
|
||||
processor.processClass(cls);
|
||||
if (processor.isNative(cls.getName())) {
|
||||
processor.processFinalMethods(cls);
|
||||
}
|
||||
if (processor.isNativeImplementation(cls.getName())) {
|
||||
processor.makeSync(cls);
|
||||
}
|
||||
for (MethodHolder method : cls.getMethods().toArray(new MethodHolder[0])) {
|
||||
if (method.getAnnotations().get(JSBody.class.getName()) != null) {
|
||||
processor.processJSBody(cls, method);
|
||||
|
|
|
@ -18,6 +18,7 @@ package org.teavm.jso.plugin;
|
|||
import java.util.*;
|
||||
import org.teavm.diagnostics.Diagnostics;
|
||||
import org.teavm.javascript.spi.GeneratedBy;
|
||||
import org.teavm.javascript.spi.Sync;
|
||||
import org.teavm.jso.*;
|
||||
import org.teavm.model.*;
|
||||
import org.teavm.model.instructions.*;
|
||||
|
@ -41,10 +42,18 @@ class JavascriptNativeProcessor {
|
|||
nativeRepos = new NativeJavascriptClassRepository(classSource);
|
||||
}
|
||||
|
||||
public ClassReaderSource getClassSource() {
|
||||
return classSource;
|
||||
}
|
||||
|
||||
public boolean isNative(String className) {
|
||||
return nativeRepos.isJavaScriptClass(className);
|
||||
}
|
||||
|
||||
public boolean isNativeImplementation(String className) {
|
||||
return nativeRepos.isJavaScriptImplementation(className);
|
||||
}
|
||||
|
||||
public void setDiagnostics(Diagnostics diagnostics) {
|
||||
this.diagnostics = diagnostics;
|
||||
}
|
||||
|
@ -111,6 +120,44 @@ class JavascriptNativeProcessor {
|
|||
}
|
||||
}
|
||||
|
||||
public void makeSync(ClassHolder cls) {
|
||||
Set<MethodDescriptor> methods = new HashSet<>();
|
||||
findInheritedMethods(cls, methods, new HashSet<String>());
|
||||
for (MethodHolder method : cls.getMethods()) {
|
||||
if (methods.contains(method.getDescriptor()) && method.getAnnotations().get(Sync.class.getName()) == null) {
|
||||
AnnotationHolder annot = new AnnotationHolder(Sync.class.getName());
|
||||
method.getAnnotations().add(annot);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void findInheritedMethods(ClassReader cls, Set<MethodDescriptor> methods, Set<String> visited) {
|
||||
if (!visited.add(cls.getName())) {
|
||||
return;
|
||||
}
|
||||
if (isNative(cls.getName())) {
|
||||
for (MethodReader method : cls.getMethods()) {
|
||||
if (!method.hasModifier(ElementModifier.STATIC) && !method.hasModifier(ElementModifier.FINAL) &&
|
||||
method.getLevel() != AccessLevel.PRIVATE) {
|
||||
methods.add(method.getDescriptor());
|
||||
}
|
||||
}
|
||||
} else if (isNativeImplementation(cls.getName())) {
|
||||
if (cls.getParent() != null && !cls.getParent().equals(cls.getName())) {
|
||||
ClassReader parentCls = classSource.get(cls.getParent());
|
||||
if (parentCls != null) {
|
||||
findInheritedMethods(parentCls, methods, visited);
|
||||
}
|
||||
}
|
||||
for (String iface : cls.getInterfaces()) {
|
||||
ClassReader parentCls = classSource.get(iface);
|
||||
if (parentCls != null) {
|
||||
findInheritedMethods(parentCls, methods, visited);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static ValueType[] getStaticSignature(MethodReference method) {
|
||||
ValueType[] signature = method.getSignature();
|
||||
ValueType[] staticSignature = new ValueType[signature.length + 1];
|
||||
|
@ -137,7 +184,7 @@ class JavascriptNativeProcessor {
|
|||
}
|
||||
replacement.clear();
|
||||
MethodReader method = getMethod(invoke.getMethod());
|
||||
if (method.hasModifier(ElementModifier.STATIC)) {
|
||||
if (method == null || method.hasModifier(ElementModifier.STATIC)) {
|
||||
continue;
|
||||
}
|
||||
if (method.hasModifier(ElementModifier.FINAL)) {
|
||||
|
|
|
@ -29,6 +29,7 @@ import org.teavm.model.ElementModifier;
|
|||
class NativeJavascriptClassRepository {
|
||||
private ClassReaderSource classSource;
|
||||
private Map<String, Boolean> knownJavaScriptClasses = new HashMap<>();
|
||||
private Map<String, Boolean> knownJavaScriptImplementations = new HashMap<>();
|
||||
|
||||
public NativeJavascriptClassRepository(ClassReaderSource classSource) {
|
||||
this.classSource = classSource;
|
||||
|
@ -38,13 +39,22 @@ class NativeJavascriptClassRepository {
|
|||
public boolean isJavaScriptClass(String className) {
|
||||
Boolean known = knownJavaScriptClasses.get(className);
|
||||
if (known == null) {
|
||||
known = figureOutIfJavaScriptClass(className);
|
||||
known = examineIfJavaScriptClass(className);
|
||||
knownJavaScriptClasses.put(className, known);
|
||||
}
|
||||
return known;
|
||||
}
|
||||
|
||||
private boolean figureOutIfJavaScriptClass(String className) {
|
||||
public boolean isJavaScriptImplementation(String className) {
|
||||
Boolean known = knownJavaScriptImplementations.get(className);
|
||||
if (known == null) {
|
||||
known = examineIfJavaScriptImplementation(className);
|
||||
knownJavaScriptImplementations.put(className, known);
|
||||
}
|
||||
return known;
|
||||
}
|
||||
|
||||
private boolean examineIfJavaScriptClass(String className) {
|
||||
ClassReader cls = classSource.get(className);
|
||||
if (cls == null || !(cls.hasModifier(ElementModifier.INTERFACE) || cls.hasModifier(ElementModifier.ABSTRACT))) {
|
||||
return false;
|
||||
|
@ -56,4 +66,25 @@ class NativeJavascriptClassRepository {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean examineIfJavaScriptImplementation(String className) {
|
||||
if (isJavaScriptClass(className)) {
|
||||
return false;
|
||||
}
|
||||
ClassReader cls = classSource.get(className);
|
||||
if (cls == null) {
|
||||
return false;
|
||||
}
|
||||
if (cls.getParent() != null && !cls.getParent().equals(cls.getName())) {
|
||||
if (isJavaScriptClass(cls.getParent())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
for (String iface : cls.getInterfaces()) {
|
||||
if (isJavaScriptClass(iface)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@ import org.teavm.javascript.spi.GeneratedBy;
|
|||
import org.teavm.javascript.spi.InjectedBy;
|
||||
import org.teavm.jso.JS;
|
||||
import org.teavm.platform.metadata.ClassResource;
|
||||
import org.teavm.platform.metadata.StaticFieldResource;
|
||||
import org.teavm.platform.plugin.PlatformGenerator;
|
||||
|
||||
/**
|
||||
|
@ -90,6 +91,10 @@ public final class Platform {
|
|||
|
||||
@InjectedBy(PlatformGenerator.class)
|
||||
@PluggableDependency(PlatformGenerator.class)
|
||||
public static native Object objectFromResource(StaticFieldResource resource);
|
||||
|
||||
@GeneratedBy(PlatformGenerator.class)
|
||||
@PluggableDependency(PlatformGenerator.class)
|
||||
public static native Enum<?>[] getEnumConstants(PlatformClass cls);
|
||||
|
||||
@GeneratedBy(PlatformGenerator.class)
|
||||
|
|
|
@ -17,6 +17,7 @@ package org.teavm.platform.metadata;
|
|||
|
||||
import java.util.Properties;
|
||||
import org.teavm.common.ServiceRepository;
|
||||
import org.teavm.model.FieldReference;
|
||||
import org.teavm.model.ListableClassReaderSource;
|
||||
import org.teavm.platform.Platform;
|
||||
import org.teavm.vm.TeaVM;
|
||||
|
@ -55,6 +56,12 @@ public interface MetadataGeneratorContext extends ServiceRepository {
|
|||
*/
|
||||
ClassResource createClassResource(String className);
|
||||
|
||||
/**
|
||||
* Creates a new resource that represents static field. Client code then may use
|
||||
* {@link Platform#objectFromResource(StaticFieldResource)} to get actual field value.
|
||||
*/
|
||||
StaticFieldResource createFieldResource(FieldReference field);
|
||||
|
||||
/**
|
||||
* Creates a new resource array.
|
||||
*/
|
||||
|
|
|
@ -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.platform.metadata;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Alexey Andreev
|
||||
*/
|
||||
public interface StaticFieldResource extends Resource {
|
||||
}
|
|
@ -39,12 +39,12 @@ public class AsyncMethodGenerator implements Generator, DependencyPlugin {
|
|||
public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) throws IOException {
|
||||
MethodReference asyncRef = getAsyncReference(methodRef);
|
||||
writer.append("var callback").ws().append("=").ws().append("function()").ws().append("{};").softNewLine();
|
||||
writer.append("callback.").appendMethod(completeMethod).ws().append("=").ws().append("function($this,").ws()
|
||||
.append("val)").ws().append("{").indent().softNewLine();
|
||||
writer.append("callback.").appendMethod(completeMethod).ws().append("=").ws().append("function(val)").ws()
|
||||
.append("{").indent().softNewLine();
|
||||
writer.append("return $return($rt_asyncResult(val));").softNewLine();
|
||||
writer.outdent().append("};").softNewLine();
|
||||
writer.append("callback.").appendMethod(errorMethod).ws().append("=").ws().append("function($this,").ws()
|
||||
.append("e)").ws().append("{").indent().softNewLine();
|
||||
writer.append("callback.").appendMethod(errorMethod).ws().append("=").ws().append("function(e)").ws()
|
||||
.append("{").indent().softNewLine();
|
||||
writer.append("return $return($rt_asyncError(e));").softNewLine();
|
||||
writer.outdent().append("};").softNewLine();
|
||||
writer.append("try").ws().append("{").indent().softNewLine();
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* 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.platform.plugin;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.teavm.codegen.SourceWriter;
|
||||
import org.teavm.model.FieldReference;
|
||||
import org.teavm.platform.metadata.StaticFieldResource;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Alexey Andreev
|
||||
*/
|
||||
class BuildTimeStaticFieldResource implements StaticFieldResource, ResourceWriter {
|
||||
private FieldReference field;
|
||||
|
||||
public BuildTimeStaticFieldResource(FieldReference field) {
|
||||
this.field = field;
|
||||
}
|
||||
|
||||
public FieldReference getField() {
|
||||
return field;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(SourceWriter writer) throws IOException {
|
||||
writer.appendField(field);
|
||||
}
|
||||
}
|
|
@ -18,6 +18,7 @@ package org.teavm.platform.plugin;
|
|||
import java.lang.reflect.Proxy;
|
||||
import java.util.Properties;
|
||||
import org.teavm.common.ServiceRepository;
|
||||
import org.teavm.model.FieldReference;
|
||||
import org.teavm.model.ListableClassReaderSource;
|
||||
import org.teavm.platform.metadata.*;
|
||||
|
||||
|
@ -72,6 +73,11 @@ class DefaultMetadataGeneratorContext implements MetadataGeneratorContext {
|
|||
return new BuildTimeClassResource(className);
|
||||
}
|
||||
|
||||
@Override
|
||||
public StaticFieldResource createFieldResource(FieldReference field) {
|
||||
return new BuildTimeStaticFieldResource(field);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends Resource> ResourceMap<T> createResourceMap() {
|
||||
return new BuildTimeResourceMap<>();
|
||||
|
|
|
@ -16,11 +16,7 @@
|
|||
package org.teavm.platform.plugin;
|
||||
|
||||
import org.teavm.dependency.*;
|
||||
import org.teavm.model.CallLocation;
|
||||
import org.teavm.model.ClassReader;
|
||||
import org.teavm.model.MethodDescriptor;
|
||||
import org.teavm.model.MethodReader;
|
||||
import org.teavm.model.ValueType;
|
||||
import org.teavm.model.*;
|
||||
import org.teavm.platform.Platform;
|
||||
|
||||
/**
|
||||
|
@ -29,7 +25,6 @@ import org.teavm.platform.Platform;
|
|||
*/
|
||||
public class EnumDependencySupport implements DependencyListener {
|
||||
private DependencyNode allEnums;
|
||||
private boolean unlocked;
|
||||
|
||||
@Override
|
||||
public void started(DependencyAgent agent) {
|
||||
|
@ -43,21 +38,25 @@ public class EnumDependencySupport implements DependencyListener {
|
|||
return;
|
||||
}
|
||||
allEnums.propagate(agent.getType(className));
|
||||
if (unlocked) {
|
||||
MethodReader method = cls.getMethod(new MethodDescriptor("values",
|
||||
ValueType.arrayOf(ValueType.object(cls.getName()))));
|
||||
if (method != null) {
|
||||
agent.linkMethod(method.getReference(), location).use();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void methodAchieved(DependencyAgent agent, MethodDependency method, CallLocation location) {
|
||||
public void methodAchieved(final DependencyAgent agent, MethodDependency method, CallLocation location) {
|
||||
if (method.getReference().getClassName().equals(Platform.class.getName()) &&
|
||||
method.getReference().getName().equals("getEnumConstants")) {
|
||||
unlocked = true;
|
||||
allEnums.connect(method.getResult().getArrayItem());
|
||||
final MethodReference ref = method.getReference();
|
||||
allEnums.addConsumer(new DependencyConsumer() {
|
||||
@Override public void consume(DependencyAgentType type) {
|
||||
ClassReader cls = agent.getClassSource().get(type.getName());
|
||||
MethodReader method = cls.getMethod(new MethodDescriptor("values",
|
||||
ValueType.arrayOf(ValueType.object(cls.getName()))));
|
||||
if (method != null) {
|
||||
agent.linkMethod(method.getReference(), new CallLocation(ref)).use();
|
||||
}
|
||||
}
|
||||
});
|
||||
method.getResult().propagate(agent.getType("[java.lang.Enum"));
|
||||
for (String cls : agent.getAchievableClasses()) {
|
||||
classAchieved(agent, cls, location);
|
||||
|
|
|
@ -47,13 +47,14 @@ public class NewInstanceDependencySupport implements DependencyListener {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void methodAchieved(final DependencyAgent agent, MethodDependency method, final CallLocation location) {
|
||||
public void methodAchieved(final DependencyAgent agent, MethodDependency method, CallLocation location) {
|
||||
MethodReader reader = method.getMethod();
|
||||
if (reader.getOwnerName().equals(Platform.class.getName()) && reader.getName().equals("newInstance")) {
|
||||
allClassesNode.connect(method.getResult());
|
||||
final MethodReference methodRef = reader.getReference();
|
||||
method.getResult().addConsumer(new DependencyConsumer() {
|
||||
@Override public void consume(DependencyAgentType type) {
|
||||
attachConstructor(agent, type.getName(), location);
|
||||
attachConstructor(agent, type.getName(), new CallLocation(methodRef));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
* 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.platform.plugin;
|
||||
|
||||
import org.teavm.dependency.*;
|
||||
import org.teavm.model.CallLocation;
|
||||
import org.teavm.platform.Platform;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Alexey Andreev
|
||||
*/
|
||||
public class PlatformDependencyListener implements DependencyListener {
|
||||
private DependencyNode allClasses;
|
||||
|
||||
@Override
|
||||
public void started(DependencyAgent agent) {
|
||||
allClasses = agent.createNode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void classAchieved(DependencyAgent agent, String className, CallLocation location) {
|
||||
allClasses.propagate(agent.getType(className));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void methodAchieved(final DependencyAgent agent, MethodDependency method, CallLocation location) {
|
||||
if (!method.getReference().getClassName().equals(Platform.class.getName())) {
|
||||
return;
|
||||
}
|
||||
switch (method.getReference().getName()) {
|
||||
case "objectFromResource":
|
||||
allClasses.connect(method.getResult());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fieldAchieved(DependencyAgent agent, FieldDependency field, CallLocation location) {
|
||||
}
|
||||
}
|
|
@ -26,6 +26,7 @@ import org.teavm.javascript.spi.Injector;
|
|||
import org.teavm.javascript.spi.InjectorContext;
|
||||
import org.teavm.model.*;
|
||||
import org.teavm.platform.Platform;
|
||||
import org.teavm.platform.PlatformClass;
|
||||
import org.teavm.platform.PlatformRunnable;
|
||||
|
||||
/**
|
||||
|
@ -58,12 +59,9 @@ public class PlatformGenerator implements Generator, Injector, DependencyPlugin
|
|||
switch (methodRef.getName()) {
|
||||
case "asJavaClass":
|
||||
case "classFromResource":
|
||||
case "objectFromResource":
|
||||
context.writeExpr(context.getArgument(0));
|
||||
return;
|
||||
case "getEnumConstants":
|
||||
context.writeExpr(context.getArgument(0));
|
||||
context.getWriter().append(".values()");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -85,30 +83,82 @@ public class PlatformGenerator implements Generator, Injector, DependencyPlugin
|
|||
case "schedule":
|
||||
generateSchedule(context, writer, true);
|
||||
break;
|
||||
case "getEnumConstants":
|
||||
generateEnumConstants(context, writer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void generateNewInstance(GeneratorContext context, SourceWriter writer, MethodReference methodRef)
|
||||
throws IOException {
|
||||
writer.append("var c").ws().append("=").ws().append("'$$constructor$$';").softNewLine();
|
||||
if (context.isAsync()) {
|
||||
writer.append("function async(cls, init) {").indent().softNewLine();
|
||||
writer.append("return function($return) {").indent().softNewLine();
|
||||
writer.append("var r = new cls;").softNewLine();
|
||||
writer.append("init(r, $rt_guardAsync(function($restore) {").indent().softNewLine();
|
||||
writer.append("$restore();").softNewLine();
|
||||
writer.append("$return($rt_asyncResult(r))").softNewLine();
|
||||
writer.outdent().append("}));").softNewLine();
|
||||
writer.outdent().append("};").softNewLine();
|
||||
writer.outdent().append("}").softNewLine();
|
||||
|
||||
writer.append("function sync(cls, init) {").indent().softNewLine();
|
||||
writer.append("return function($return) {").indent().softNewLine();
|
||||
writer.append("var r = new cls;").softNewLine();
|
||||
writer.append("try {").indent().softNewLine();
|
||||
writer.append("init(r);").softNewLine();
|
||||
writer.append("$return($rt_asyncResult(r));").softNewLine();
|
||||
writer.outdent().append("} catch (e) {").indent().softNewLine();
|
||||
writer.append("$return($rt_asyncError(e));").softNewLine();
|
||||
writer.outdent().append("}").softNewLine();
|
||||
writer.outdent().append("};").softNewLine();
|
||||
writer.outdent().append("}").softNewLine();
|
||||
}
|
||||
for (String clsName : context.getClassSource().getClassNames()) {
|
||||
ClassReader cls = context.getClassSource().get(clsName);
|
||||
MethodReader method = cls.getMethod(new MethodDescriptor("<init>", void.class));
|
||||
if (method != null) {
|
||||
writer.appendClass(clsName).append("[c]").ws().append("=").ws()
|
||||
.append(writer.getNaming().getNameForInit(method.getReference()))
|
||||
.append(";").softNewLine();
|
||||
writer.appendClass(clsName).append("[c]").ws().append("=").ws();
|
||||
if (!context.isAsync()) {
|
||||
writer.append(writer.getNaming().getNameForInit(method.getReference()));
|
||||
} else {
|
||||
String function = context.isAsync(method.getReference()) ? "async" : "sync";
|
||||
String methodName = context.isAsync(method.getReference()) ?
|
||||
writer.getNaming().getFullNameForAsync(method.getReference()) :
|
||||
writer.getNaming().getFullNameFor(method.getReference());
|
||||
writer.append(function).append("(").appendClass(clsName).append(',').ws()
|
||||
.append(methodName).append(")");
|
||||
}
|
||||
writer.append(";").softNewLine();
|
||||
}
|
||||
}
|
||||
writer.appendMethodBody(methodRef).ws().append("=").ws().append("function(cls)").ws().append("{")
|
||||
.softNewLine().indent();
|
||||
String selfName = context.isAsync() ? writer.getNaming().getFullNameForAsync(methodRef) :
|
||||
writer.getNaming().getFullNameFor(methodRef);
|
||||
writer.append(selfName).ws().append("=").ws().append("function(cls");
|
||||
if (context.isAsync()) {
|
||||
writer.append(',').ws().append("$return");
|
||||
}
|
||||
writer.append(")").ws().append("{").softNewLine().indent();
|
||||
writer.append("if").ws().append("(!cls.hasOwnProperty(c))").ws().append("{").indent().softNewLine();
|
||||
writer.append("return null;").softNewLine();
|
||||
if (!context.isAsync()) {
|
||||
writer.append("return null;").softNewLine();
|
||||
} else {
|
||||
writer.append("return $return($rt_asyncResult(null));").softNewLine();
|
||||
}
|
||||
writer.outdent().append("}").softNewLine();
|
||||
writer.append("return cls[c]();").softNewLine();
|
||||
if (!context.isAsync()) {
|
||||
writer.append("return cls[c]();").softNewLine();
|
||||
} else {
|
||||
writer.append("return cls[c]($return);").softNewLine();
|
||||
}
|
||||
writer.outdent().append("}").softNewLine();
|
||||
writer.append("return ").appendMethodBody(methodRef).append("(")
|
||||
.append(context.getParameterName(1)).append(");").softNewLine();
|
||||
|
||||
writer.append("return ").append(selfName).append("(").append(context.getParameterName(1));
|
||||
if (context.isAsync()) {
|
||||
writer.append(',').ws().append("$return");
|
||||
}
|
||||
writer.append(");").softNewLine();
|
||||
}
|
||||
|
||||
private void generateLookup(GeneratorContext context, SourceWriter writer) throws IOException {
|
||||
|
@ -135,11 +185,49 @@ public class PlatformGenerator implements Generator, Injector, DependencyPlugin
|
|||
}
|
||||
|
||||
private void generateSchedule(GeneratorContext context, SourceWriter writer, boolean timeout) throws IOException {
|
||||
MethodReference launchRef = new MethodReference(Platform.class, "launchThread",
|
||||
PlatformRunnable.class, void.class);
|
||||
String runnable = context.getParameterName(1);
|
||||
writer.append("return window.setTimeout(function()").ws().append("{").indent().softNewLine();
|
||||
writer.append("$rt_rootInvocationAdapter(").appendMethodBody(Platform.class, "launchThread",
|
||||
PlatformRunnable.class, void.class).append(")(").append(runnable).append(");").softNewLine();
|
||||
boolean async = context.isAsyncFamily(launchRef);
|
||||
String methodName = async ? writer.getNaming().getFullNameForAsync(launchRef) :
|
||||
writer.getNaming().getFullNameFor(launchRef);
|
||||
if (async) {
|
||||
writer.append("$rt_rootInvocationAdapter(");
|
||||
}
|
||||
writer.append(methodName);
|
||||
if (async) {
|
||||
writer.append(")");
|
||||
}
|
||||
writer.append("(").append(runnable).append(");")
|
||||
.softNewLine();
|
||||
writer.outdent().append("},").ws().append(timeout ? context.getParameterName(2) : "0")
|
||||
.append(");").softNewLine();
|
||||
}
|
||||
|
||||
private void generateEnumConstants(GeneratorContext context, SourceWriter writer) throws IOException {
|
||||
writer.append("var c").ws().append("=").ws().append("'$$enumConstants$$';").softNewLine();
|
||||
for (String clsName : context.getClassSource().getClassNames()) {
|
||||
ClassReader cls = context.getClassSource().get(clsName);
|
||||
MethodReader method = cls.getMethod(new MethodDescriptor("values",
|
||||
ValueType.arrayOf(ValueType.object(clsName))));
|
||||
if (method != null) {
|
||||
writer.appendClass(clsName).append("[c]").ws().append("=").ws();
|
||||
writer.appendMethodBody(method.getReference());
|
||||
writer.append(";").softNewLine();
|
||||
}
|
||||
}
|
||||
|
||||
String selfName = writer.getNaming().getFullNameFor(new MethodReference(Platform.class, "getEnumConstants",
|
||||
PlatformClass.class, Enum[].class));
|
||||
writer.append(selfName).ws().append("=").ws().append("function(cls)").ws().append("{").softNewLine().indent();
|
||||
writer.append("if").ws().append("(!cls.hasOwnProperty(c))").ws().append("{").indent().softNewLine();
|
||||
writer.append("return null;").softNewLine();
|
||||
writer.outdent().append("}").softNewLine();
|
||||
writer.append("return cls[c]();").softNewLine();
|
||||
writer.outdent().append("}").softNewLine();
|
||||
|
||||
writer.append("return ").append(selfName).append("(").append(context.getParameterName(1))
|
||||
.append(");").softNewLine();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,5 +33,6 @@ public class PlatformPlugin implements TeaVMPlugin {
|
|||
host.add(new NewInstanceDependencySupport());
|
||||
host.add(new ClassLookupDependencySupport());
|
||||
host.add(new EnumDependencySupport());
|
||||
host.add(new PlatformDependencyListener());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,6 +36,9 @@ class ResourceAccessorDependencyListener implements DependencyListener {
|
|||
|
||||
@Override
|
||||
public void methodAchieved(DependencyAgent agent, MethodDependency method, CallLocation location) {
|
||||
if (!method.getReference().getClassName().equals(ResourceAccessor.class.getName())) {
|
||||
return;
|
||||
}
|
||||
switch (method.getReference().getName()) {
|
||||
case "castToString":
|
||||
method.getResult().propagate(agent.getType("java.lang.String"));
|
||||
|
|
|
@ -0,0 +1,506 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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 java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class OutputStreamWriterTest {
|
||||
|
||||
private static final int UPPER = 0xd800;
|
||||
|
||||
private static final int BUFFER_SIZE = 10000;
|
||||
|
||||
private ByteArrayOutputStream out;
|
||||
|
||||
private OutputStreamWriter writer;
|
||||
|
||||
static private final String source = "This is a test message with Unicode character. "
|
||||
+ "\u4e2d\u56fd is China's name in Chinese";
|
||||
|
||||
static private final String[] MINIMAL_CHARSETS = { "UTF-8" };
|
||||
|
||||
OutputStreamWriter osw;
|
||||
|
||||
InputStreamReader isr;
|
||||
|
||||
private ByteArrayOutputStream fos;
|
||||
|
||||
String testString = "Test_All_Tests\nTest_java_io_BufferedInputStream\nTest_java_io_BufferedOutputStream"
|
||||
+ "\nTest_java_io_ByteArrayInputStream\nTest_java_io_ByteArrayOutputStream\nTest_java_io_DataInputStream\n";
|
||||
|
||||
public OutputStreamWriterTest() throws UnsupportedEncodingException {
|
||||
out = new ByteArrayOutputStream();
|
||||
writer = new OutputStreamWriter(out, "utf-8");
|
||||
|
||||
fos = new ByteArrayOutputStream();
|
||||
osw = new OutputStreamWriter(fos);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClose() throws Exception {
|
||||
writer.flush();
|
||||
writer.close();
|
||||
try {
|
||||
writer.flush();
|
||||
fail();
|
||||
} catch (IOException e) {
|
||||
// Expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFlush() throws Exception {
|
||||
writer.write(source);
|
||||
writer.flush();
|
||||
String result = out.toString("utf-8");
|
||||
assertEquals(source, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWritecharArrayintint() throws IOException {
|
||||
char[] chars = source.toCharArray();
|
||||
|
||||
// Throws IndexOutOfBoundsException if offset is negative
|
||||
try {
|
||||
writer.write((char[])null, -1, -1);
|
||||
fail("should throw IndexOutOfBoundsException");
|
||||
} catch (NullPointerException | IndexOutOfBoundsException e) {
|
||||
// Expected
|
||||
}
|
||||
|
||||
// throws NullPointerException though count is negative
|
||||
try {
|
||||
writer.write((char[])null, 1, -1);
|
||||
fail("should throw NullPointerException");
|
||||
} catch (NullPointerException | IndexOutOfBoundsException e) {
|
||||
// Expected
|
||||
}
|
||||
|
||||
try {
|
||||
writer.write((char[])null, 1, 1);
|
||||
fail();
|
||||
} catch (NullPointerException e) {
|
||||
// Expected
|
||||
}
|
||||
try {
|
||||
writer.write(new char[0], 0, 1);
|
||||
fail();
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// Expected
|
||||
}
|
||||
try {
|
||||
writer.write(chars, -1, 1);
|
||||
fail();
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// Expected
|
||||
}
|
||||
try {
|
||||
writer.write(chars, 0, -1);
|
||||
fail();
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// Expected
|
||||
}
|
||||
try {
|
||||
writer.write(chars, 1, chars.length);
|
||||
fail();
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// Expected
|
||||
}
|
||||
writer.write(chars, 1, 2);
|
||||
writer.flush();
|
||||
assertEquals("hi", out.toString("utf-8"));
|
||||
writer.write(chars, 0, chars.length);
|
||||
writer.flush();
|
||||
assertEquals("hi" + source, out.toString("utf-8"));
|
||||
|
||||
writer.close();
|
||||
// After the stream is closed, should throw IOException first
|
||||
try {
|
||||
writer.write((char[])null, -1, -1);
|
||||
fail("should throw IOException");
|
||||
} catch (IOException e) {
|
||||
// Expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteint() throws IOException {
|
||||
writer.write(1);
|
||||
writer.flush();
|
||||
String str = new String(out.toByteArray(), "utf-8");
|
||||
assertEquals("\u0001", str);
|
||||
|
||||
writer.write(2);
|
||||
writer.flush();
|
||||
str = new String(out.toByteArray(), "utf-8");
|
||||
assertEquals("\u0001\u0002", str);
|
||||
|
||||
writer.write(-1);
|
||||
writer.flush();
|
||||
str = new String(out.toByteArray(), "utf-8");
|
||||
assertEquals("\u0001\u0002\uffff", str);
|
||||
|
||||
writer.write(0xfedcb);
|
||||
writer.flush();
|
||||
str = new String(out.toByteArray(), "utf-8");
|
||||
assertEquals("\u0001\u0002\uffff\uedcb", str);
|
||||
|
||||
writer.close();
|
||||
// After the stream is closed, should throw IOException
|
||||
try {
|
||||
writer.write(1);
|
||||
fail("should throw IOException");
|
||||
} catch (IOException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteStringintint() throws IOException {
|
||||
try {
|
||||
writer.write((String)null, 1, 1);
|
||||
fail();
|
||||
} catch (NullPointerException e) {
|
||||
// Expected
|
||||
}
|
||||
try {
|
||||
writer.write("", 0, 1);
|
||||
fail();
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// Expected
|
||||
}
|
||||
try {
|
||||
writer.write("abc", -1, 1);
|
||||
fail();
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// Expected
|
||||
}
|
||||
try {
|
||||
writer.write("abc", 0, -1);
|
||||
fail();
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// Expected
|
||||
}
|
||||
try {
|
||||
writer.write("abc", 1, 3);
|
||||
fail();
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// Expected
|
||||
}
|
||||
|
||||
// Throws IndexOutOfBoundsException before NullPointerException if count
|
||||
// is negative
|
||||
try {
|
||||
writer.write((String)null, -1, -1);
|
||||
fail("should throw IndexOutOfBoundsException");
|
||||
} catch (IndexOutOfBoundsException | NullPointerException e) {
|
||||
// Expected
|
||||
}
|
||||
|
||||
// Throws NullPointerException before StringIndexOutOfBoundsException
|
||||
try {
|
||||
writer.write((String)null, -1, 0);
|
||||
fail("should throw NullPointerException");
|
||||
} catch (IndexOutOfBoundsException | NullPointerException e) {
|
||||
// expected
|
||||
}
|
||||
|
||||
writer.write("abc", 1, 2);
|
||||
writer.flush();
|
||||
assertEquals("bc", out.toString("utf-8"));
|
||||
writer.write(source, 0, source.length());
|
||||
writer.flush();
|
||||
assertEquals("bc" + source, out.toString("utf-8"));
|
||||
|
||||
writer.close();
|
||||
// Throws IndexOutOfBoundsException first if count is negative
|
||||
try {
|
||||
writer.write((String)null, 0, -1);
|
||||
fail("should throw IndexOutOfBoundsException");
|
||||
} catch (NullPointerException | IndexOutOfBoundsException e) {
|
||||
// Expected
|
||||
}
|
||||
|
||||
try {
|
||||
writer.write((String)null, -1, 0);
|
||||
fail("should throw NullPointerException");
|
||||
} catch (NullPointerException | IndexOutOfBoundsException e) {
|
||||
// Expected
|
||||
}
|
||||
|
||||
try {
|
||||
writer.write("abc", -1, 0);
|
||||
fail("should throw StringIndexOutOfBoundsException");
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// Expected
|
||||
}
|
||||
|
||||
// Throws IOException
|
||||
try {
|
||||
writer.write("abc", 0, 1);
|
||||
fail("should throw IOException");
|
||||
} catch (IOException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOutputStreamWriterOutputStream() throws IOException {
|
||||
try {
|
||||
writer = new OutputStreamWriter(null);
|
||||
fail();
|
||||
} catch (NullPointerException e) {
|
||||
// Expected
|
||||
}
|
||||
OutputStreamWriter writer2 = new OutputStreamWriter(out);
|
||||
writer2.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOutputStreamWriterOutputStreamString() throws IOException {
|
||||
try {
|
||||
writer = new OutputStreamWriter(null, "utf-8");
|
||||
fail();
|
||||
} catch (NullPointerException e) {
|
||||
// Expected
|
||||
}
|
||||
try {
|
||||
writer = new OutputStreamWriter(out, "");
|
||||
fail();
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
// Expected
|
||||
}
|
||||
try {
|
||||
writer = new OutputStreamWriter(out, "badname");
|
||||
fail();
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
// Expected
|
||||
}
|
||||
try {
|
||||
writer = new OutputStreamWriter(out, (String)null);
|
||||
fail();
|
||||
} catch (NullPointerException e) {
|
||||
// Expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSingleCharIO() throws Exception {
|
||||
InputStreamReader isr = null;
|
||||
for (int i = 0; i < MINIMAL_CHARSETS.length; ++i) {
|
||||
try {
|
||||
out = new ByteArrayOutputStream();
|
||||
writer = new OutputStreamWriter(out, MINIMAL_CHARSETS[i]);
|
||||
|
||||
int upper = UPPER;
|
||||
switch (i) {
|
||||
case 0:
|
||||
upper = 128;
|
||||
break;
|
||||
case 1:
|
||||
upper = 256;
|
||||
break;
|
||||
}
|
||||
|
||||
for (int c = 0; c < upper; ++c) {
|
||||
writer.write(c);
|
||||
}
|
||||
writer.flush();
|
||||
byte[] result = out.toByteArray();
|
||||
|
||||
isr = new InputStreamReader(new ByteArrayInputStream(result), MINIMAL_CHARSETS[i]);
|
||||
for (int expected = 0; expected < upper; ++expected) {
|
||||
assertEquals("Error when reading bytes in " + MINIMAL_CHARSETS[i], expected, isr.read());
|
||||
}
|
||||
} finally {
|
||||
try {
|
||||
isr.close();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
try {
|
||||
writer.close();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBlockIO() throws Exception {
|
||||
InputStreamReader isr = null;
|
||||
char[] largeBuffer = new char[BUFFER_SIZE];
|
||||
for (int i = 0; i < MINIMAL_CHARSETS.length; ++i) {
|
||||
try {
|
||||
out = new ByteArrayOutputStream();
|
||||
writer = new OutputStreamWriter(out, MINIMAL_CHARSETS[i]);
|
||||
|
||||
int upper = UPPER;
|
||||
switch (i) {
|
||||
case 0:
|
||||
upper = 128;
|
||||
break;
|
||||
case 1:
|
||||
upper = 256;
|
||||
break;
|
||||
}
|
||||
|
||||
int m = 0;
|
||||
for (int c = 0; c < upper; ++c) {
|
||||
largeBuffer[m++] = (char)c;
|
||||
if (m == BUFFER_SIZE) {
|
||||
writer.write(largeBuffer);
|
||||
m = 0;
|
||||
}
|
||||
}
|
||||
writer.write(largeBuffer, 0, m);
|
||||
writer.flush();
|
||||
byte[] result = out.toByteArray();
|
||||
|
||||
isr = new InputStreamReader(new ByteArrayInputStream(result), MINIMAL_CHARSETS[i]);
|
||||
int expected = 0, read = 0, j = 0;
|
||||
while (expected < upper) {
|
||||
if (j == read) {
|
||||
read = isr.read(largeBuffer);
|
||||
j = 0;
|
||||
}
|
||||
assertEquals("Error when reading bytes in " + MINIMAL_CHARSETS[i], expected++, largeBuffer[j++]);
|
||||
}
|
||||
} finally {
|
||||
try {
|
||||
isr.close();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
try {
|
||||
writer.close();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_ConstructorLjava_io_OutputStream() {
|
||||
assertTrue("Used in tests", true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_ConstructorLjava_io_OutputStreamLjava_lang_String() throws UnsupportedEncodingException {
|
||||
osw = new OutputStreamWriter(fos, "UTF-8");
|
||||
try {
|
||||
osw = new OutputStreamWriter(fos, "Bogus");
|
||||
fail("Failed to throw Unsupported Encoding exception");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
// Expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_close() throws IOException {
|
||||
osw.close();
|
||||
|
||||
try {
|
||||
osw.write(testString, 0, testString.length());
|
||||
fail("Chars written after close");
|
||||
} catch (IOException e) {
|
||||
// Expected
|
||||
}
|
||||
|
||||
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
||||
try {
|
||||
OutputStreamWriter writer = new OutputStreamWriter(bout, "ISO2022JP");
|
||||
writer.write(new char[] { 'a' });
|
||||
writer.close();
|
||||
// the default is ASCII, there should not be any mode changes
|
||||
String converted = new String(bout.toByteArray(), "ISO8859_1");
|
||||
assertTrue("invalid conversion 1: " + converted, converted.equals("a"));
|
||||
|
||||
bout.reset();
|
||||
writer = new OutputStreamWriter(bout, "ISO2022JP");
|
||||
writer.write(new char[] { '\u3048' });
|
||||
writer.flush();
|
||||
// the byte sequence should not switch to ASCII mode until the
|
||||
// stream is closed
|
||||
converted = new String(bout.toByteArray(), "ISO8859_1");
|
||||
assertTrue("invalid conversion 2: " + converted, converted.equals("\u001b$B$("));
|
||||
writer.close();
|
||||
converted = new String(bout.toByteArray(), "ISO8859_1");
|
||||
assertTrue("invalid conversion 3: " + converted, converted.equals("\u001b$B$(\u001b(B"));
|
||||
|
||||
bout.reset();
|
||||
writer = new OutputStreamWriter(bout, "ISO2022JP");
|
||||
writer.write(new char[] { '\u3048' });
|
||||
writer.write(new char[] { '\u3048' });
|
||||
writer.close();
|
||||
// there should not be a mode switch between writes
|
||||
assertEquals("invalid conversion 4", "\u001b$B$($(\u001b(B", new String(bout.toByteArray(), "ISO8859_1"));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
// Can't test missing converter
|
||||
System.out.println(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_flush() throws IOException {
|
||||
char[] buf = new char[testString.length()];
|
||||
osw.write(testString, 0, testString.length());
|
||||
osw.flush();
|
||||
openInputStream();
|
||||
isr.read(buf, 0, buf.length);
|
||||
assertTrue("Chars not flushed", new String(buf, 0, buf.length).equals(testString));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_write$CII() throws IOException {
|
||||
char[] buf = new char[testString.length()];
|
||||
osw.write(testString, 0, testString.length());
|
||||
osw.close();
|
||||
openInputStream();
|
||||
isr.read(buf, 0, buf.length);
|
||||
assertTrue("Incorrect chars returned", new String(buf, 0, buf.length).equals(testString));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_writeI() throws IOException {
|
||||
osw.write('T');
|
||||
osw.close();
|
||||
openInputStream();
|
||||
int c = isr.read();
|
||||
assertEquals("Incorrect char returned", 'T', (char)c);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_writeLjava_lang_StringII() throws IOException {
|
||||
char[] buf = new char[testString.length()];
|
||||
osw.write(testString, 0, testString.length());
|
||||
osw.close();
|
||||
openInputStream();
|
||||
isr.read(buf);
|
||||
assertEquals("Incorrect chars returned", testString, new String(buf, 0, buf.length));
|
||||
}
|
||||
|
||||
private void openInputStream() {
|
||||
isr = new InputStreamReader(new ByteArrayInputStream(fos.toByteArray()));
|
||||
}
|
||||
}
|
|
@ -225,6 +225,37 @@ public class StringBuilderTest {
|
|||
assertEquals("1.23456789E150", sb.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void powTenDoubleAppended() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(10.0);
|
||||
assertEquals("10.0", sb.toString());
|
||||
sb.setLength(0);
|
||||
sb.append(20.0);
|
||||
assertEquals("20.0", sb.toString());
|
||||
sb.setLength(0);
|
||||
sb.append(100.0);
|
||||
assertEquals("100.0", sb.toString());
|
||||
sb.setLength(0);
|
||||
sb.append(1000.0);
|
||||
assertEquals("1000.0", sb.toString());
|
||||
sb.setLength(0);
|
||||
sb.append(0.1);
|
||||
assertEquals("0.1", sb.toString());
|
||||
sb.setLength(0);
|
||||
sb.append(0.01);
|
||||
assertEquals("0.01", sb.toString());
|
||||
sb.setLength(0);
|
||||
sb.append(1e20);
|
||||
assertEquals("1.0E20", sb.toString());
|
||||
sb.setLength(0);
|
||||
sb.append(2e20);
|
||||
assertEquals("2.0E20", sb.toString());
|
||||
sb.setLength(0);
|
||||
sb.append(1e-12);
|
||||
assertEquals("1.0E-12", sb.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void negativeDoubleAppended() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
|
|
@ -75,7 +75,7 @@ public class PatternTest {
|
|||
s = pat.split("", -1);
|
||||
assertEquals(s.length, 1);
|
||||
s = pat.split("abccbadfe", -1);
|
||||
assertEquals(s.length, 11);
|
||||
//assertEquals(s.length, 11);
|
||||
// zero limit
|
||||
pat = Pattern.compile("b");
|
||||
s = pat.split("abccbadfebb", 0);
|
||||
|
@ -130,7 +130,7 @@ public class PatternTest {
|
|||
s = pat.split("");
|
||||
assertEquals(s.length, 1);
|
||||
s = pat.split("abccbadfe");
|
||||
assertEquals(s.length, 10);
|
||||
//assertEquals(s.length, 10);
|
||||
// bug6544
|
||||
String s1 = "";
|
||||
String[] arr = s1.split(":");
|
||||
|
|
|
@ -130,47 +130,4 @@ public class SplitTest {
|
|||
assertTrue(tokens[1].equals(""));
|
||||
assertEquals("dle z", tokens[2]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSplit2() {
|
||||
Pattern p = Pattern.compile("");
|
||||
String s[];
|
||||
s = p.split("a", -1);
|
||||
assertEquals(3, s.length);
|
||||
assertEquals("", s[0]);
|
||||
assertEquals("a", s[1]);
|
||||
assertEquals("", s[2]);
|
||||
|
||||
s = p.split("", -1);
|
||||
assertEquals(1, s.length);
|
||||
assertEquals("", s[0]);
|
||||
|
||||
s = p.split("abcd", -1);
|
||||
assertEquals(6, s.length);
|
||||
assertEquals("", s[0]);
|
||||
assertEquals("a", s[1]);
|
||||
assertEquals("b", s[2]);
|
||||
assertEquals("c", s[3]);
|
||||
assertEquals("d", s[4]);
|
||||
assertEquals("", s[5]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSplitSupplementaryWithEmptyString() {
|
||||
|
||||
/*
|
||||
* See http://www.unicode.org/reports/tr18/#Supplementary_Characters We
|
||||
* have to treat text as code points not code units.
|
||||
*/
|
||||
Pattern p = Pattern.compile("");
|
||||
String s[];
|
||||
s = p.split("a\ud869\uded6b", -1);
|
||||
assertEquals(6, s.length);
|
||||
assertEquals("", s[0]);
|
||||
assertEquals("a", s[1]);
|
||||
assertEquals("\ud869", s[2]);
|
||||
assertEquals("\uded6", s[3]);
|
||||
assertEquals("b", s[4]);
|
||||
assertEquals("", s[5]);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user