From 7e23498c5b2ad444c0d35e933e5260646229ed77 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Wed, 11 Mar 2015 19:02:32 +0400 Subject: [PATCH] Fix bugs --- .../java/org/teavm/classlib/java/lang/TObject.java | 2 +- .../java/org/teavm/classlib/java/lang/TThread.java | 12 +++++++++--- .../main/java/org/teavm/javascript/Decompiler.java | 2 +- .../src/main/java/org/teavm/javascript/Renderer.java | 11 ++++++----- .../main/resources/org/teavm/javascript/runtime.js | 11 ++++++++--- 5 files changed, 25 insertions(+), 13 deletions(-) diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TObject.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TObject.java index 827b0267d..6229e2073 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TObject.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TObject.java @@ -286,7 +286,7 @@ public class TObject { @Override public void onTimer() { if (!expired()) { - Platform.postpone(this); + run(); } } 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 855382fa4..52e53ba0d 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 @@ -36,6 +36,7 @@ public class TThread extends TObject implements TRunnable { private long id; private int priority = 0; private long timeSliceStart; + private int yieldCount; private TString name; TRunnable target; @@ -101,6 +102,10 @@ public class TThread extends TObject implements TRunnable { } public static void yield() { + if (++currentThread.yieldCount < 30) { + return; + } + currentThread.yieldCount = 0; if (currentThread.timeSliceStart + 100 < System.currentTimeMillis()) { switchContext(currentThread); } @@ -146,12 +151,13 @@ public class TThread extends TObject implements TRunnable { private static void sleep(long millis, final AsyncCallback callback) { final TThread current = currentThread(); - window.setTimeout(new TimerHandler() { - @Override public void onTimer() { + int intMillis = millis < Integer.MAX_VALUE ? (int)millis : Integer.MAX_VALUE; + Platform.schedule(new PlatformRunnable() { + @Override public void run() { setCurrentThread(current); callback.complete(null); } - }, millis); + }, intMillis); } public final void setPriority(int newPriority){ diff --git a/teavm-core/src/main/java/org/teavm/javascript/Decompiler.java b/teavm-core/src/main/java/org/teavm/javascript/Decompiler.java index 4ffb6b11b..0e133d9d0 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/Decompiler.java +++ b/teavm-core/src/main/java/org/teavm/javascript/Decompiler.java @@ -264,6 +264,7 @@ public class Decompiler { generator.indexer = indexer; parentNode = codeTree.getRoot(); currentNode = parentNode.getFirstChild(); + boolean saved = !async; for (int i = 0; i < this.graph.size(); ++i) { Block block = stack.peek(); while (block.end == i) { @@ -292,7 +293,6 @@ public class Decompiler { if (head != -1 && loopSuccessors[head] == next) { next = head; } - boolean saved = !async; if (node >= 0) { generator.currentBlock = program.basicBlockAt(node); int tmp = indexer.nodeAt(next); diff --git a/teavm-core/src/main/java/org/teavm/javascript/Renderer.java b/teavm-core/src/main/java/org/teavm/javascript/Renderer.java index 1f3acc218..402649453 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/Renderer.java +++ b/teavm-core/src/main/java/org/teavm/javascript/Renderer.java @@ -699,7 +699,11 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext writer.append("function $save()").ws().append("{").indent().softNewLine(); writer.append("$rt_nativeThread()"); - for (int i = ref.parameterCount() + 1; i < variableCount; ++i) { + int firstToSave = 0; + if (methodNode.getModifiers().contains(NodeModifier.STATIC)) { + firstToSave = 1; + } + for (int i = firstToSave; i < variableCount; ++i) { writer.append(".push(").append(variableName(i)).append(")"); } writer.append(".push($ptr);"); @@ -710,7 +714,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext writer.append("if").ws().append("($rt_resuming())").ws().append("{").indent().softNewLine(); writer.append("var $T").ws().append('=').ws().append("$rt_nativeThread();").softNewLine(); writer.append("$ptr").ws().append('=').ws().append("$T.pop();"); - for (int i = variableCount - 1; i > ref.parameterCount(); --i) { + for (int i = variableCount - 1; i >= firstToSave; --i) { writer.append(variableName(i)).ws().append('=').ws().append("$T.pop();"); } writer.softNewLine(); @@ -1990,9 +1994,6 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext writer.appendMethodBody(monitorEnterRef).append("("); statement.getObjectRef().acceptVisitor(this); writer.append(");").softNewLine(); - writer.append("if").ws().append("($rt_suspending())").ws().append("{").indent().softNewLine(); - writer.append("return $save();").softNewLine(); - writer.outdent().append("}").softNewLine(); } else { MethodReference monitorEnterRef = new MethodReference( Object.class, "monitorEnterSync", Object.class, void.class); diff --git a/teavm-core/src/main/resources/org/teavm/javascript/runtime.js b/teavm-core/src/main/resources/org/teavm/javascript/runtime.js index d6063734c..c27ddba15 100644 --- a/teavm-core/src/main/resources/org/teavm/javascript/runtime.js +++ b/teavm-core/src/main/resources/org/teavm/javascript/runtime.js @@ -479,11 +479,16 @@ TeaVMThread.prototype.resume = function() { } TeaVMThread.prototype.run = function() { $rt_currentNativeThread = this; - this.runner(); - $rt_currentNativeThread = null; + try { + this.runner(); + } finally { + $rt_currentNativeThread = null; + } if (this.suspendCallback !== null) { var self = this; - this.suspendCallback(function() { + var callback = this.suspendCallback; + this.suspendCallback = null; + callback(function() { self.resume(); }); }