diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TThread.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TThread.java index 578444e65..5a66d1575 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TThread.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TThread.java @@ -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){ diff --git a/teavm-platform/src/main/java/org/teavm/platform/Platform.java b/teavm-platform/src/main/java/org/teavm/platform/Platform.java index 38c43fcfd..8e9976669 100644 --- a/teavm-platform/src/main/java/org/teavm/platform/Platform.java +++ b/teavm-platform/src/main/java/org/teavm/platform/Platform.java @@ -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); } diff --git a/teavm-platform/src/main/java/org/teavm/platform/plugin/AsyncMethodGenerator.java b/teavm-platform/src/main/java/org/teavm/platform/plugin/AsyncMethodGenerator.java index 2dfbc2878..68ae8a10a 100644 --- a/teavm-platform/src/main/java/org/teavm/platform/plugin/AsyncMethodGenerator.java +++ b/teavm-platform/src/main/java/org/teavm/platform/plugin/AsyncMethodGenerator.java @@ -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(); diff --git a/teavm-platform/src/main/java/org/teavm/platform/plugin/PlatformGenerator.java b/teavm-platform/src/main/java/org/teavm/platform/plugin/PlatformGenerator.java index ac744de90..808e304bb 100644 --- a/teavm-platform/src/main/java/org/teavm/platform/plugin/PlatformGenerator.java +++ b/teavm-platform/src/main/java/org/teavm/platform/plugin/PlatformGenerator.java @@ -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(); + } }