Fix Thread.start()

This commit is contained in:
konsoletyper 2015-02-14 00:41:29 +04:00
parent f93b35ce80
commit 4cf084d848
4 changed files with 41 additions and 16 deletions

View File

@ -19,6 +19,7 @@ import org.teavm.dom.browser.TimerHandler;
import org.teavm.dom.browser.Window;
import org.teavm.javascript.spi.Async;
import org.teavm.jso.JS;
import org.teavm.platform.Platform;
import org.teavm.platform.async.AsyncCallback;
@ -56,22 +57,19 @@ public class TThread extends TObject implements TRunnable {
}
public void start(){
window.setTimeout(new TimerHandler() {
@Override public void onTimer() {
launch(TThread.this);
Platform.startThread(new Runnable() {
@Override
public void run() {
try {
activeCount++;
setCurrentThread(TThread.this);
target.run();
} finally {
activeCount--;
setCurrentThread(mainThread);
}
}
}, 0);
}
private static void launch(TThread thread) {
try {
activeCount++;
setCurrentThread(thread);
thread.run();
} finally {
activeCount--;
setCurrentThread(mainThread);
}
});
}
static void setCurrentThread(TThread thread){

View File

@ -92,6 +92,14 @@ public final class Platform {
@PluggableDependency(PlatformGenerator.class)
public static native Enum<?>[] getEnumConstants(PlatformClass cls);
@GeneratedBy(PlatformGenerator.class)
@PluggableDependency(PlatformGenerator.class)
public static native void startThread(Runnable runnable);
private static void launchThread(Runnable runnable) {
runnable.run();
}
public static PlatformString stringFromCharCode(int charCode) {
return ((PlatformHelper)JS.getGlobal()).getStringClass().fromCharCode(charCode);
}

View File

@ -38,7 +38,7 @@ public class AsyncMethodGenerator implements Generator, DependencyPlugin {
@Override
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("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("return $return($rt_asyncResult(val));").softNewLine();

View File

@ -25,6 +25,7 @@ import org.teavm.javascript.spi.GeneratorContext;
import org.teavm.javascript.spi.Injector;
import org.teavm.javascript.spi.InjectorContext;
import org.teavm.model.*;
import org.teavm.platform.Platform;
/**
*
@ -40,6 +41,13 @@ public class PlatformGenerator implements Generator, Injector, DependencyPlugin
case "clone":
method.getVariable(1).connect(method.getResult());
break;
case "startThread": {
MethodDependency launchMethod = agent.linkMethod(new MethodReference(Platform.class,
"launchThread", Runnable.class, void.class), null);
method.getVariable(1).connect(launchMethod.getVariable(1));
launchMethod.use();
break;
}
}
}
@ -69,6 +77,9 @@ public class PlatformGenerator implements Generator, Injector, DependencyPlugin
case "clone":
generateClone(context, writer);
break;
case "startThread":
generateStartThread(context, writer);
break;
}
}
@ -117,4 +128,12 @@ public class PlatformGenerator implements Generator, Injector, DependencyPlugin
.softNewLine().outdent().append("}").softNewLine();
writer.append("return copy;").softNewLine();
}
private void generateStartThread(GeneratorContext context, SourceWriter writer) throws IOException {
String runnable = context.getParameterName(1);
writer.append("window.setTimeout(function()").ws().append("{").indent().softNewLine();
writer.appendMethodBody(Platform.class, "launchThread", Runnable.class, void.class).append("(")
.append(runnable).append(");").softNewLine();
writer.outdent().append("},").ws().append("0);").softNewLine();
}
}