This commit is contained in:
Alexey Andreev 2015-04-05 18:43:56 +03:00
parent d66595b9c7
commit 33e9fca099
5 changed files with 34 additions and 8 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

@ -21,12 +21,24 @@ import org.teavm.platform.async.AsyncCallback;
*
* @author Alexey Andreev
*/
class FakeAsyncCallback implements AsyncCallback<Object> {
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(Object result) {
public void complete(T result) {
realAsyncCallback.complete(result);
}
@Override
public void error(Throwable e) {
realAsyncCallback.error(e);
}
}

View File

@ -41,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();
@ -54,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('(');
@ -97,11 +102,11 @@ public class AsyncMethodGenerator implements Generator, DependencyPlugin {
for (int i = 0; i <= paramCount; ++i) {
method.getVariable(i).connect(asyncMethod.getVariable(i));
}
asyncMethod.getVariable(paramCount + 1).propagate(checker.getType(FakeAsyncCallback.class.getName()));
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) {
MethodDependency completeMethod = checker.linkMethod(
new MethodReference(FakeAsyncCallback.class, "complete", Object.class, void.class), null);
completeMethod.getVariable(1).connect(method.getResult(), new DependencyTypeFilter() {
@Override
public boolean match(DependencyType type) {
@ -109,10 +114,15 @@ public class AsyncMethodGenerator implements Generator, DependencyPlugin {
}
});
}
completeMethod.use();
MethodDependency errorMethod = checker.linkMethod(new MethodReference(FakeAsyncCallback.class, "error",
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();
}

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;
}
}