Merge branch 'indexeddb'

This commit is contained in:
Alexey Andreev 2015-04-05 21:50:39 +03:00
commit 8a9ea907ed
32 changed files with 1092 additions and 62 deletions

View File

@ -174,7 +174,7 @@ public class TClass<T> extends TObject {
return forName(name);
}
@SuppressWarnings("unchecked")
@SuppressWarnings({ "unchecked", "unused" })
public T newInstance() throws TInstantiationException, TIllegalAccessException {
Object instance = Platform.newInstance(platformClass);
if (instance == null) {

View File

@ -101,10 +101,11 @@ public class TThread extends TObject implements TRunnable {
}
public static void yield() {
TThread currentThread = currentThread();
if (++currentThread.yieldCount < 30) {
return;
}
currentThread.yieldCount = 0;
currentThread().yieldCount = 0;
if (currentThread.timeSliceStart + 100 < System.currentTimeMillis()) {
switchContext(currentThread);
}

View File

@ -473,7 +473,11 @@ TeaVMThread.prototype.start = function(callback) {
throw new Error("Another thread is running");
}
this.status = 0;
this.completeCallback = callback ? callback : function() {};
this.completeCallback = callback ? callback : function(result) {
if (result instanceof Error) {
throw result;
}
};
this.run();
}
TeaVMThread.prototype.resume = function() {

View File

@ -23,6 +23,6 @@ import org.teavm.jso.JSObject;
* @author Alexey Andreev
*/
@JSFunctor
public interface EventListener extends JSObject {
void handleEvent(Event evt);
public interface EventListener<E extends Event> extends JSObject {
void handleEvent(E evt);
}

View File

@ -22,13 +22,13 @@ import org.teavm.jso.JSObject;
* @author Alexey Andreev
*/
public interface EventTarget extends JSObject {
void addEventListener(String type, EventListener listener, boolean useCapture);
void addEventListener(String type, EventListener<?> listener, boolean useCapture);
void addEventListener(String type, EventListener listener);
void addEventListener(String type, EventListener<?> listener);
void removeEventListener(String type, EventListener listener, boolean useCapture);
void removeEventListener(String type, EventListener<?> listener, boolean useCapture);
void removeEventListener(String type, EventListener listener);
void removeEventListener(String type, EventListener<?> listener);
boolean dispatchEvent(Event evt);
}

View File

@ -15,6 +15,7 @@
*/
package org.teavm.dom.html;
import org.teavm.dom.events.Event;
import org.teavm.dom.events.EventListener;
import org.teavm.jso.JSProperty;
@ -24,24 +25,24 @@ import org.teavm.jso.JSProperty;
*/
public interface HTMLBodyElement extends HTMLElement {
@JSProperty("onbeforeunload")
void setOnBeforeUnload(EventListener listener);
void setOnBeforeUnload(EventListener<Event> listener);
@JSProperty("onerror")
void setOnError(EventListener listener);
void setOnError(EventListener<Event> listener);
@JSProperty("onload")
void setOnLoad(EventListener listener);
void setOnLoad(EventListener<Event> listener);
@JSProperty("onmessage")
void setOnMessage(EventListener listener);
void setOnMessage(EventListener<Event> listener);
@JSProperty("onoffline")
void setOnOffline(EventListener listener);
void setOnOffline(EventListener<Event> listener);
@JSProperty("ononline")
void setOnOnline(EventListener listener);
void setOnOnline(EventListener<Event> listener);
@JSProperty("ononunload")
void setOnUnload(EventListener listener);
void setOnUnload(EventListener<Event> listener);
}

View File

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

View File

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

View File

@ -0,0 +1,58 @@
/*
* Copyright 2015 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.dom.indexeddb;
import org.teavm.jso.JSMethod;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
/**
*
* @author Alexey Andreev
*/
public interface IDBCursor extends JSObject {
String DIRECTION_NEXT = "next";
String DIRECTION_NEXT_UNIQUE = "nextunique";
String DIRECTION_PREVIOUS = "prev";
String DIRECTION_PREVIOUS_UNIQUE = "prevunique";
@JSProperty
IDBCursorSource getSource();
@JSProperty
String getDirection();
@JSProperty
JSObject getKey();
@JSProperty
JSObject getValue();
@JSProperty
JSObject getPrimaryKey();
IDBRequest update(JSObject value);
void advance(int count);
@JSMethod("continue")
void doContinue();
IDBRequest delete();
}

View File

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

View File

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

View File

@ -0,0 +1,65 @@
/*
* Copyright 2015 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.dom.indexeddb;
import org.teavm.dom.events.EventTarget;
import org.teavm.jso.JSProperty;
/**
*
* @author Alexey Andreev
*/
public interface IDBDatabase extends EventTarget {
String TRANSACTION_READONLY = "readonly";
String TRANSACTION_READWRITE = "readwrite";
String TRANSACTION_VERSIONCHANGE = "versionchange";
@JSProperty
String getName();
@JSProperty
int getVersion();
@JSProperty
String[] getObjectStoreNames();
IDBObjectStore createObjectStore(String name, IDBObjectStoreParameters optionalParameters);
IDBObjectStore createObjectStore(String name);
void deleteObjectStore(String name);
IDBTransaction transaction(String storeName, String transactionMode);
IDBTransaction transaction(String storeName);
IDBTransaction transaction(String[] storeNames, String transactionMode);
IDBTransaction transaction(String[] storeNames);
void close();
@JSProperty("onabort")
void setOnAbort(EventHandler handler);
@JSProperty("onerror")
void setOnError(EventHandler handler);
@JSProperty("onversionchange")
void setOnVersionChange(EventHandler handler);
}

View File

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

View File

@ -0,0 +1,48 @@
/*
* Copyright 2015 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.dom.indexeddb;
import org.teavm.jso.JS;
import org.teavm.jso.JSBody;
import org.teavm.jso.JSObject;
/**
*
* @author Alexey Andreev
*/
public abstract class IDBFactory implements JSObject {
public static boolean isSupported() {
return !JS.isUndefined(getInstanceImpl());
}
public static IDBFactory getInstance() {
IDBFactory factory = getInstanceImpl();
if (JS.isUndefined(factory)) {
throw new IllegalStateException("IndexedDB is not supported in this browser");
}
return factory;
}
@JSBody(params = {}, script = "return window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || " +
"window.msIndexedDB;")
static native IDBFactory getInstanceImpl();
public abstract IDBOpenDBRequest open(String name, int version);
public abstract IDBOpenDBRequest deleteDatabase(String name);
public abstract int cmp(JSObject a, JSObject b);
}

View File

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

View File

@ -0,0 +1,63 @@
/*
* Copyright 2015 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.dom.indexeddb;
import org.teavm.jso.JS;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
import org.teavm.jso.JSStringArray;
import org.teavm.jso.JSType;
/**
*
* @author Alexey Andreev
*/
public abstract class IDBIndex implements JSObject, IDBCursorSource {
@JSProperty
public abstract String getName();
@JSProperty("keyPath")
abstract JSObject getKeyPathImpl();
public final String[] getKeyPath() {
JSObject result = getKeyPathImpl();
if (JS.getType(result) == JSType.STRING) {
return new String[] { JS.unwrapString(result) };
} else {
return JS.unwrapStringArray((JSStringArray)result);
}
}
@JSProperty
public abstract boolean isMultiEntry();
@JSProperty
public abstract boolean isUnique();
public abstract IDBCursorRequest openCursor();
public abstract IDBCursorRequest openCursor(IDBKeyRange range);
public abstract IDBCursorRequest openKeyCursor();
public abstract IDBGetRequest get(JSObject key);
public abstract IDBGetRequest getKey(JSObject key);
public abstract IDBCountRequest count(JSObject key);
public abstract IDBCountRequest count();
}

View File

@ -0,0 +1,63 @@
/*
* Copyright 2015 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.dom.indexeddb;
import org.teavm.jso.JSBody;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
/**
*
* @author Alexey Andreev
*/
public abstract class IDBKeyRange implements JSObject {
@JSProperty
public abstract JSObject getLower();
@JSProperty
public abstract JSObject getUpper();
@JSProperty
public abstract boolean isLowerOpen();
@JSProperty
public abstract boolean isUpperOpen();
@JSBody(params = "value", script = "return IDBKeyRange.only(value);")
public static native IDBKeyRange only(JSObject value);
@JSBody(params = { "lower", "open" }, script = "return IDBKeyRange.lowerBound(lower, open);")
public static native IDBKeyRange lowerBound(JSObject lower, boolean open);
public static IDBKeyRange lowerBound(JSObject lower) {
return lowerBound(lower, false);
}
@JSBody(params = { "upper", "open" }, script = "return IDBKeyRange.upperBound(upper, open);")
public static native IDBKeyRange upperBound(JSObject upper, boolean open);
public static IDBKeyRange upperBound(JSObject upper) {
return upperBound(upper, false);
}
@JSBody(params = { "lower", "upper", "lowerOpen", "upperOpen" },
script = "return IDBKeyRange.bound(lower, upper, lowerOpen, upperOpen);")
public static native IDBKeyRange bound(JSObject lower, JSObject upper, boolean lowerOpen, boolean upperOpen);
public static IDBKeyRange bound(JSObject lower, JSObject upper) {
return bound(lower, upper, false, false);
}
}

View File

@ -0,0 +1,75 @@
/*
* Copyright 2015 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.dom.indexeddb;
import org.teavm.jso.*;
/**
*
* @author Alexey Andreev
*/
public abstract class IDBObjectStore implements JSObject, IDBCursorSource {
@JSProperty
public abstract String getName();
@JSProperty("keyPath")
abstract JSObject getKeyPathImpl();
public final String[] getKeyPath() {
JSObject result = getKeyPathImpl();
if (JS.getType(result) == JSType.STRING) {
return new String[] { JS.unwrapString(result) };
} else {
return JS.unwrapStringArray((JSStringArray)result);
}
}
@JSProperty
public abstract String[] getIndexNames();
@JSProperty
public abstract boolean isAutoIncrement();
public abstract IDBRequest put(JSObject value, JSObject key);
public abstract IDBRequest put(JSObject value);
public abstract IDBRequest add(JSObject value, JSObject key);
public abstract IDBRequest add(JSObject value);
public abstract IDBRequest delete(JSObject key);
public abstract IDBGetRequest get(JSObject key);
public abstract IDBRequest clear();
public abstract IDBCursorRequest openCursor();
public abstract IDBCursorRequest openCursor(IDBKeyRange range);
public abstract IDBIndex createIndex(String name, String key);
public abstract IDBIndex createIndex(String name, String[] keys);
public abstract IDBIndex index(String name);
public abstract void deleteIndex(String name);
public abstract IDBCountRequest count();
public abstract IDBCountRequest count(JSObject key);
}

View File

@ -0,0 +1,45 @@
/*
* Copyright 2015 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.dom.indexeddb;
import org.teavm.jso.JSBody;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
/**
*
* @author Alexey Andreev
*/
public abstract class IDBObjectStoreParameters implements JSObject {
@JSBody(params = {}, script = "return {};")
public static native IDBObjectStoreParameters create();
public final IDBObjectStoreParameters keyPath(String... keys) {
setKeyPath(keys);
return this;
}
public final IDBObjectStoreParameters autoIncrement(boolean autoIncrement) {
setAutoIncrement(autoIncrement);
return this;
}
@JSProperty
abstract void setKeyPath(String[] keys);
@JSProperty
abstract void setAutoIncrement(boolean autoIncrement);
}

View File

@ -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.dom.indexeddb;
import org.teavm.dom.events.EventListener;
import org.teavm.jso.JSProperty;
/**
*
* @author Alexey Andreev
*/
public interface IDBOpenDBRequest extends IDBRequest {
@JSProperty
IDBDatabase getResult();
@JSProperty
void setOnBlocked(EventHandler handler);
@JSProperty("onupgradeneeded")
void setOnUpgradeNeeded(EventListener<IDBVersionChangeEvent> listener);
}

View File

@ -0,0 +1,46 @@
/*
* Copyright 2015 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.dom.indexeddb;
import org.teavm.dom.events.EventTarget;
import org.teavm.jso.JSProperty;
/**
*
* @author Alexey Andreev
*/
public interface IDBRequest extends EventTarget {
String STATE_PENDING = "pending";
String STATE_DONE = "done";
@JSProperty
IDBError getError();
@JSProperty
IDBRequestSource getSource();
@JSProperty
IDBTransaction getTransaction();
@JSProperty
String getReadyState();
@JSProperty("onerror")
void setOnError(EventHandler handler);
@JSProperty("onsuccess")
void setOnSuccess(EventHandler handler);
}

View File

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

View File

@ -0,0 +1,48 @@
/*
* Copyright 2015 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.dom.indexeddb;
import org.teavm.dom.events.EventTarget;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
/**
*
* @author Alexey Andreev
*/
public interface IDBTransaction extends JSObject, EventTarget {
@JSProperty
String getMode();
@JSProperty
IDBDatabase getDb();
@JSProperty
IDBError getError();
IDBObjectStore objectStore(String name);
void abort();
@JSProperty("onabort")
void setOnAbort(EventHandler handler);
@JSProperty("oncomplete")
void setOnComplete(EventHandler handler);
@JSProperty("onerror")
void setOnError(EventHandler handler);
}

View File

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

View File

@ -15,6 +15,7 @@
*/
package org.teavm.jso;
import java.lang.reflect.Array;
import java.util.Iterator;
import org.teavm.dependency.PluggableDependency;
import org.teavm.javascript.spi.InjectedBy;
@ -347,6 +348,58 @@ public final class JS {
@InjectedBy(JSNativeGenerator.class)
public static native char unwrapCharacter(JSObject obj);
public static <T extends JSObject> T[] unwrapArray(Class<T> type, JSArray<T> array) {
@SuppressWarnings("unchecked")
T[] result = (T[])Array.newInstance(type, array.getLength());
for (int i = 0; i < result.length; ++i) {
result[i] = array.get(i);
}
return result;
}
public static <T extends JSObject> T[][] unwrapArray2(Class<T> type, JSArray<JSArray<T>> array) {
@SuppressWarnings("unchecked")
T[][] result = (T[][])Array.newInstance(Array.newInstance(type, 0).getClass(), array.getLength());
for (int i = 0; i < result.length; ++i) {
result[i] = unwrapArray(type, array.get(i));
}
return result;
}
public static <T extends JSObject> T[][][] unwrapArray3(Class<T> type, JSArray<JSArray<JSArray<T>>> array) {
Class<?> baseType = Array.newInstance(type, 0).getClass();
@SuppressWarnings("unchecked")
T[][][] result = (T[][][])Array.newInstance(Array.newInstance(baseType, 0).getClass(), array.getLength());
for (int i = 0; i < result.length; ++i) {
result[i] = unwrapArray2(type, array.get(i));
}
return result;
}
public static String[] unwrapStringArray(JSStringArray array) {
String[] result = new String[array.getLength()];
for (int i = 0; i < result.length; ++i) {
result[i] = array.get(i);
}
return result;
}
public static String[][] unwrapStringArray2(JSArray<JSStringArray> array) {
String[][] result = new String[array.getLength()][];
for (int i = 0; i < result.length; ++i) {
result[i] = unwrapStringArray(array.get(i));
}
return result;
}
public static String[][][] unwrapStringArray3(JSArray<JSArray<JSStringArray>> array) {
String[][][] result = new String[array.getLength()][][];
for (int i = 0; i < result.length; ++i) {
result[i] = unwrapStringArray2(array.get(i));
}
return result;
}
@InjectedBy(JSNativeGenerator.class)
public static native boolean isUndefined(JSObject obj);

View File

@ -502,19 +502,26 @@ class JavascriptNativeProcessor {
if (type instanceof ValueType.Primitive) {
switch (((ValueType.Primitive)type).getKind()) {
case BOOLEAN:
return unwrap(var, "unwrapBoolean", ValueType.BOOLEAN, location.getSourceLocation());
return unwrap(var, "unwrapBoolean", ValueType.parse(JSObject.class), ValueType.BOOLEAN,
location.getSourceLocation());
case BYTE:
return unwrap(var, "unwrapByte", ValueType.BYTE, location.getSourceLocation());
return unwrap(var, "unwrapByte", ValueType.parse(JSObject.class), ValueType.BYTE,
location.getSourceLocation());
case SHORT:
return unwrap(var, "unwrapShort", ValueType.SHORT, location.getSourceLocation());
return unwrap(var, "unwrapShort", ValueType.parse(JSObject.class), ValueType.SHORT,
location.getSourceLocation());
case INTEGER:
return unwrap(var, "unwrapInt", ValueType.INTEGER, location.getSourceLocation());
return unwrap(var, "unwrapInt", ValueType.parse(JSObject.class), ValueType.INTEGER,
location.getSourceLocation());
case CHARACTER:
return unwrap(var, "unwrapCharacter", ValueType.CHARACTER, location.getSourceLocation());
return unwrap(var, "unwrapCharacter", ValueType.parse(JSObject.class), ValueType.CHARACTER,
location.getSourceLocation());
case DOUBLE:
return unwrap(var, "unwrapDouble", ValueType.DOUBLE, location.getSourceLocation());
return unwrap(var, "unwrapDouble", ValueType.parse(JSObject.class), ValueType.DOUBLE,
location.getSourceLocation());
case FLOAT:
return unwrap(var, "unwrapFloat", ValueType.FLOAT, location.getSourceLocation());
return unwrap(var, "unwrapFloat", ValueType.parse(JSObject.class), ValueType.FLOAT,
location.getSourceLocation());
case LONG:
break;
}
@ -523,8 +530,9 @@ class JavascriptNativeProcessor {
if (className.equals(JSObject.class.getName())) {
return var;
} else if (className.equals("java.lang.String")) {
return unwrap(var, "unwrapString", ValueType.object("java.lang.String"), location.getSourceLocation());
} else {
return unwrap(var, "unwrapString", ValueType.parse(JSObject.class), ValueType.parse(String.class),
location.getSourceLocation());
} else if (isNative(className)) {
Variable result = program.createVariable();
CastInstruction castInsn = new CastInstruction();
castInsn.setReceiver(result);
@ -534,16 +542,58 @@ class JavascriptNativeProcessor {
replacement.add(castInsn);
return result;
}
} else if (type instanceof ValueType.Array) {
return unwrapArray(location, var, (ValueType.Array)type);
}
diagnostics.error(location, "Unsupported type: {{t0}}", type);
return var;
}
private Variable unwrap(Variable var, String methodName, ValueType resultType, InstructionLocation location) {
private Variable unwrapArray(CallLocation location, Variable var, ValueType.Array type) {
ValueType itemType = type;
int degree = 0;
while (itemType instanceof ValueType.Array) {
++degree;
itemType = ((ValueType.Array)itemType).getItemType();
}
if (degree > 3) {
diagnostics.error(location, "Unsupported type: {{t0}}", type);
return var;
}
if (itemType instanceof ValueType.Object) {
String className = ((ValueType.Object)itemType).getClassName();
if (className.equals("java.lang.String")) {
String methodName = "unwrapStringArray";
if (degree > 1) {
methodName += degree;
}
ValueType argType = degree == 1 ? ValueType.parse(JSStringArray.class) :
ValueType.parse(JSArray.class);
return unwrap(var, methodName, argType, type, location.getSourceLocation());
} else if (isNative(className)) {
return unwrapObjectArray(location, var, degree, itemType, type);
}
}
diagnostics.error(location, "Unsupported type: {{t0}}", type);
return var;
}
private Variable unwrap(Variable var, String methodName, ValueType argType, ValueType resultType,
InstructionLocation location) {
if (!argType.isObject(JSObject.class.getName())) {
Variable castValue = program.createVariable();
CastInstruction castInsn = new CastInstruction();
castInsn.setValue(var);
castInsn.setReceiver(castValue);
castInsn.setLocation(location);
castInsn.setTargetType(argType);
replacement.add(castInsn);
var = castValue;
}
Variable result = program.createVariable();
InvokeInstruction insn = new InvokeInstruction();
insn.setMethod(new MethodReference(JS.class.getName(), methodName, ValueType.object(JSObject.class.getName()),
resultType));
insn.setMethod(new MethodReference(JS.class.getName(), methodName, argType, resultType));
insn.getArguments().add(var);
insn.setReceiver(result);
insn.setType(InvocationType.SPECIAL);
@ -552,6 +602,57 @@ class JavascriptNativeProcessor {
return result;
}
private Variable unwrapObjectArray(CallLocation location, Variable var, int degree, ValueType itemType,
ValueType expectedType) {
String methodName = "unwrapArray";
if (degree > 1) {
methodName += degree;
}
ValueType resultType = ValueType.parse(JSObject.class);
for (int i = 0; i < degree; ++i) {
resultType = ValueType.arrayOf(resultType);
}
Variable classVar = program.createVariable();
ClassConstantInstruction classInsn = new ClassConstantInstruction();
classInsn.setConstant(itemType);
classInsn.setReceiver(classVar);
classInsn.setLocation(location.getSourceLocation());
replacement.add(classInsn);
Variable castValue = program.createVariable();
CastInstruction castInsn = new CastInstruction();
castInsn.setValue(var);
castInsn.setReceiver(castValue);
castInsn.setLocation(location.getSourceLocation());
castInsn.setTargetType(ValueType.parse(JSArray.class));
replacement.add(castInsn);
var = castValue;
Variable result = program.createVariable();
InvokeInstruction insn = new InvokeInstruction();
insn.setMethod(new MethodReference(JS.class.getName(), methodName, ValueType.parse(Class.class),
ValueType.parse(JSArray.class), resultType));
insn.getArguments().add(classVar);
insn.getArguments().add(var);
insn.setReceiver(result);
insn.setType(InvocationType.SPECIAL);
insn.setLocation(location.getSourceLocation());
replacement.add(insn);
var = result;
Variable castResult = program.createVariable();
castInsn = new CastInstruction();
castInsn.setValue(var);
castInsn.setReceiver(castResult);
castInsn.setLocation(location.getSourceLocation());
castInsn.setTargetType(expectedType);
replacement.add(castInsn);
var = castResult;
return var;
}
private Variable wrapArgument(CallLocation location, Variable var, ValueType type) {
if (type instanceof ValueType.Object) {
String className = ((ValueType.Object)type).getClassName();

View File

@ -0,0 +1,44 @@
/*
* Copyright 2014 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.platform.async.AsyncCallback;
/**
*
* @author Alexey Andreev
*/
class AsyncCallbackWrapper<T> implements AsyncCallback<T> {
private AsyncCallback<T> realAsyncCallback;
AsyncCallbackWrapper(AsyncCallback<T> realAsyncCallback) {
this.realAsyncCallback = realAsyncCallback;
}
public static <S> AsyncCallbackWrapper<S> create(AsyncCallback<S> realAsyncCallback) {
return new AsyncCallbackWrapper<>(realAsyncCallback);
}
@Override
public void complete(T result) {
realAsyncCallback.complete(result);
}
@Override
public void error(Throwable e) {
realAsyncCallback.error(e);
}
}

View File

@ -19,6 +19,8 @@ import java.io.IOException;
import org.teavm.codegen.SourceWriter;
import org.teavm.dependency.DependencyAgent;
import org.teavm.dependency.DependencyPlugin;
import org.teavm.dependency.DependencyType;
import org.teavm.dependency.DependencyTypeFilter;
import org.teavm.dependency.MethodDependency;
import org.teavm.javascript.spi.Generator;
import org.teavm.javascript.spi.GeneratorContext;
@ -39,6 +41,7 @@ public class AsyncMethodGenerator implements Generator, DependencyPlugin {
public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) throws IOException {
MethodReference asyncRef = getAsyncReference(methodRef);
writer.append("var thread").ws().append('=').ws().append("$rt_nativeThread();").softNewLine();
writer.append("var javaThread").ws().append('=').ws().append("$rt_getThread();").softNewLine();
writer.append("if").ws().append("(thread.isResuming())").ws().append("{").indent().softNewLine();
writer.append("thread.status").ws().append("=").ws().append("0;").softNewLine();
writer.append("var result").ws().append("=").ws().append("thread.attribute;").softNewLine();
@ -52,13 +55,17 @@ public class AsyncMethodGenerator implements Generator, DependencyPlugin {
writer.append("callback.").appendMethod(completeMethod.getDescriptor()).ws().append("=").ws()
.append("function(val)").ws().append("{").indent().softNewLine();
writer.append("thread.attribute").ws().append('=').ws().append("val;").softNewLine();
writer.append("$rt_setThread(javaThread);").softNewLine();
writer.append("thread.resume();").softNewLine();
writer.outdent().append("};").softNewLine();
writer.append("callback.").appendMethod(errorMethod.getDescriptor()).ws().append("=").ws()
.append("function(e)").ws().append("{").indent().softNewLine();
writer.append("thread.attribute").ws().append('=').ws().append("$rt_exception(e);").softNewLine();
writer.append("$rt_setThread(javaThread);").softNewLine();
writer.append("thread.resume();").softNewLine();
writer.outdent().append("};").softNewLine();
writer.append("callback").ws().append("=").ws().appendMethodBody(AsyncCallbackWrapper.class, "create",
AsyncCallback.class, AsyncCallbackWrapper.class).append("(callback);").softNewLine();
writer.append("return thread.suspend(function()").ws().append("{").indent().softNewLine();
writer.append("try").ws().append("{").indent().softNewLine();
writer.appendMethodBody(asyncRef).append('(');
@ -88,13 +95,66 @@ public class AsyncMethodGenerator implements Generator, DependencyPlugin {
}
@Override
public void methodAchieved(DependencyAgent checker, MethodDependency method, CallLocation location) {
public void methodAchieved(final DependencyAgent checker, final MethodDependency method, CallLocation location) {
MethodReference asyncRef = getAsyncReference(method.getReference());
MethodDependency asyncMethod = checker.linkMethod(asyncRef, location);
int paramCount = method.getReference().parameterCount();
for (int i = 0; i <= paramCount; ++i) {
method.getVariable(i).connect(asyncMethod.getVariable(i));
}
asyncMethod.getVariable(paramCount + 1).propagate(checker.getType(AsyncCallbackWrapper.class.getName()));
MethodDependency completeMethod = checker.linkMethod(
new MethodReference(AsyncCallbackWrapper.class, "complete", Object.class, void.class), null);
if (method.getResult() != null) {
completeMethod.getVariable(1).connect(method.getResult(), new DependencyTypeFilter() {
@Override
public boolean match(DependencyType type) {
return isSubtype(checker.getClassSource(), type.getName(), method.getReference().getReturnType());
}
});
}
completeMethod.use();
MethodDependency errorMethod = checker.linkMethod(new MethodReference(AsyncCallbackWrapper.class, "error",
Throwable.class, void.class), null);
errorMethod.getVariable(1).connect(method.getThrown());
errorMethod.use();
checker.linkMethod(new MethodReference(AsyncCallbackWrapper.class, "create",
AsyncCallback.class, AsyncCallbackWrapper.class), null).use();
asyncMethod.use();
}
private boolean isSubtype(ClassReaderSource classSource, String className, ValueType returnType) {
if (returnType instanceof ValueType.Primitive) {
return false;
} else if (returnType instanceof ValueType.Array) {
return className.startsWith("[");
} else {
return isSubclass(classSource, className, ((ValueType.Object)returnType).getClassName());
}
}
private boolean isSubclass(ClassReaderSource classSource, String className, String baseClass) {
if (className.equals(baseClass)) {
return true;
}
ClassReader cls = classSource.get(className);
if (cls == null) {
return false;
}
if (cls.getParent() != null && !cls.getParent().equals(cls.getName())) {
if (isSubclass(classSource, cls.getParent(), baseClass)) {
return true;
}
}
for (String iface : cls.getInterfaces()) {
if (isSubclass(classSource, iface, baseClass)) {
return true;
}
}
return false;
}
}

View File

@ -38,7 +38,6 @@ public class EnumDependencySupport implements DependencyListener {
return;
}
allEnums.propagate(agent.getType(className));
}
@Override

View File

@ -51,6 +51,9 @@ public class PlatformGenerator implements Generator, Injector, DependencyPlugin
launchMethod.use();
break;
}
case "getCurrentThread":
method.getResult().propagate(agent.getType("java.lang.Thread"));
break;
}
}

View File

@ -18,8 +18,8 @@ package org.teavm.samples.hello;
import org.teavm.dom.ajax.ReadyStateChangeHandler;
import org.teavm.dom.ajax.XMLHttpRequest;
import org.teavm.dom.browser.Window;
import org.teavm.dom.events.Event;
import org.teavm.dom.events.EventListener;
import org.teavm.dom.events.MouseEvent;
import org.teavm.dom.html.HTMLButtonElement;
import org.teavm.dom.html.HTMLDocument;
import org.teavm.dom.html.HTMLElement;
@ -36,8 +36,8 @@ public final class Client {
}
public static void main(String[] args) {
helloButton.addEventListener("click", new EventListener() {
@Override public void handleEvent(Event evt) {
helloButton.addEventListener("click", new EventListener<MouseEvent>() {
@Override public void handleEvent(MouseEvent evt) {
sayHello();
}
});

View File

@ -16,8 +16,8 @@
package org.teavm.samples.video;
import org.teavm.dom.browser.Window;
import org.teavm.dom.events.Event;
import org.teavm.dom.events.EventListener;
import org.teavm.dom.events.MouseEvent;
import org.teavm.dom.html.HTMLBodyElement;
import org.teavm.dom.html.HTMLButtonElement;
import org.teavm.dom.html.HTMLDocument;
@ -69,126 +69,126 @@ public final class Player {
HTMLButtonElement loadButton = (HTMLButtonElement)document.createElement("button");
loadButton.appendChild(document.createTextNode("load()"));
loadButton.addEventListener("click", new EventListener() {
loadButton.addEventListener("click", new EventListener<MouseEvent>() {
@Override
public void handleEvent(Event evt) {
public void handleEvent(MouseEvent evt) {
video.load();
}
});
HTMLButtonElement playButton = (HTMLButtonElement)document.createElement("button");
playButton.appendChild(document.createTextNode("play()"));
playButton.addEventListener("click", new EventListener() {
playButton.addEventListener("click", new EventListener<MouseEvent>() {
@Override
public void handleEvent(Event evt) {
public void handleEvent(MouseEvent evt) {
video.play();
}
});
HTMLButtonElement pauseButton = (HTMLButtonElement)document.createElement("button");
pauseButton.appendChild(document.createTextNode("pause()"));
pauseButton.addEventListener("click", new EventListener() {
pauseButton.addEventListener("click", new EventListener<MouseEvent>() {
@Override
public void handleEvent(Event evt) {
public void handleEvent(MouseEvent evt) {
video.pause();
}
});
HTMLButtonElement currentTimePlusButton = (HTMLButtonElement)document.createElement("button");
currentTimePlusButton.appendChild(document.createTextNode("currentTime+=10"));
currentTimePlusButton.addEventListener("click", new EventListener() {
currentTimePlusButton.addEventListener("click", new EventListener<MouseEvent>() {
@Override
public void handleEvent(Event evt) {
public void handleEvent(MouseEvent evt) {
video.setCurrentTime(video.getCurrentTime() + 10);
}
});
HTMLButtonElement currentTimeMinusButton = (HTMLButtonElement)document.createElement("button");
currentTimeMinusButton.appendChild(document.createTextNode("currentTime-=10"));
currentTimeMinusButton.addEventListener("click", new EventListener() {
currentTimeMinusButton.addEventListener("click", new EventListener<MouseEvent>() {
@Override
public void handleEvent(Event evt) {
public void handleEvent(MouseEvent evt) {
video.setCurrentTime(video.getCurrentTime() - 10);
}
});
HTMLButtonElement currentTime50Button = (HTMLButtonElement)document.createElement("button");
currentTime50Button.appendChild(document.createTextNode("currentTime=50"));
currentTime50Button.addEventListener("click", new EventListener() {
currentTime50Button.addEventListener("click", new EventListener<MouseEvent>() {
@Override
public void handleEvent(Event evt) {
public void handleEvent(MouseEvent evt) {
video.setCurrentTime(50);
}
});
HTMLButtonElement playbackRateIncrementButton = (HTMLButtonElement)document.createElement("button");
playbackRateIncrementButton.appendChild(document.createTextNode("playbackRate++"));
playbackRateIncrementButton.addEventListener("click", new EventListener() {
playbackRateIncrementButton.addEventListener("click", new EventListener<MouseEvent>() {
@Override
public void handleEvent(Event evt) {
public void handleEvent(MouseEvent evt) {
video.setPlaybackRate(video.getPlaybackRate() + 1);
}
});
HTMLButtonElement playbackRateDecrementButton = (HTMLButtonElement)document.createElement("button");
playbackRateDecrementButton.appendChild(document.createTextNode("playbackRate--"));
playbackRateDecrementButton.addEventListener("click", new EventListener() {
playbackRateDecrementButton.addEventListener("click", new EventListener<MouseEvent>() {
@Override
public void handleEvent(Event evt) {
public void handleEvent(MouseEvent evt) {
video.setPlaybackRate(video.getPlaybackRate() - 1);
}
});
HTMLButtonElement playbackRatePlusButton = (HTMLButtonElement)document.createElement("button");
playbackRatePlusButton.appendChild(document.createTextNode("playbackRate+=0.1"));
playbackRatePlusButton.addEventListener("click", new EventListener() {
playbackRatePlusButton.addEventListener("click", new EventListener<MouseEvent>() {
@Override
public void handleEvent(Event evt) {
public void handleEvent(MouseEvent evt) {
video.setPlaybackRate(video.getPlaybackRate() + 0.1);
}
});
HTMLButtonElement playbackRateMinusButton = (HTMLButtonElement)document.createElement("button");
playbackRateMinusButton.appendChild(document.createTextNode("playbackRate-=0.1"));
playbackRateMinusButton.addEventListener("click", new EventListener() {
playbackRateMinusButton.addEventListener("click", new EventListener<MouseEvent>() {
@Override
public void handleEvent(Event evt) {
public void handleEvent(MouseEvent evt) {
video.setPlaybackRate(video.getPlaybackRate() - 0.1);
}
});
HTMLButtonElement volumePlusButton = (HTMLButtonElement)document.createElement("button");
volumePlusButton.appendChild(document.createTextNode("volume+=0.1"));
volumePlusButton.addEventListener("click", new EventListener() {
volumePlusButton.addEventListener("click", new EventListener<MouseEvent>() {
@Override
public void handleEvent(Event evt) {
public void handleEvent(MouseEvent evt) {
video.setVolume(video.getVolume() + 0.1f);
}
});
HTMLButtonElement volumeMinusButton = (HTMLButtonElement)document.createElement("button");
volumeMinusButton.appendChild(document.createTextNode("volume-=0.1"));
volumeMinusButton.addEventListener("click", new EventListener() {
volumeMinusButton.addEventListener("click", new EventListener<MouseEvent>() {
@Override
public void handleEvent(Event evt) {
public void handleEvent(MouseEvent evt) {
video.setVolume(video.getVolume() - 0.1f);
}
});
HTMLButtonElement muteButton = (HTMLButtonElement)document.createElement("button");
muteButton.appendChild(document.createTextNode("muted=true"));
muteButton.addEventListener("click", new EventListener() {
muteButton.addEventListener("click", new EventListener<MouseEvent>() {
@Override
public void handleEvent(Event evt) {
public void handleEvent(MouseEvent evt) {
video.setMuted(true);
}
});
HTMLButtonElement unmuteButton = (HTMLButtonElement)document.createElement("button");
unmuteButton.appendChild(document.createTextNode("muted=false"));
unmuteButton.addEventListener("click", new EventListener() {
unmuteButton.addEventListener("click", new EventListener<MouseEvent>() {
@Override
public void handleEvent(Event evt) {
public void handleEvent(MouseEvent evt) {
video.setMuted(false);
}
});