diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/ObjectNativeGenerator.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/ObjectNativeGenerator.java index 77212fa66..e90cb051d 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/ObjectNativeGenerator.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/ObjectNativeGenerator.java @@ -127,13 +127,12 @@ public class ObjectNativeGenerator implements Generator, Injector, DependencyPlu writer.append("(function(){").indent().softNewLine(); writer.append("var completed = false;").softNewLine(); writer.append("var retCallback = ").append(context.getCompleteContinuation()).append(";").softNewLine(); - writer.append("console.log(retCallback);").softNewLine(); writer.append("var callback = function(){").indent().softNewLine(); writer.append("if (completed){return;} completed=true;").softNewLine(); - writer.append("retCallback();").softNewLine(); + writer.append("retCallback($rt_asyncResult(null));").softNewLine(); writer.outdent().append("};").softNewLine(); writer.append("if (").append(pname).append(">0){").indent().softNewLine(); - writer.append("setTimeout(callback, ").append(pname).append(");").softNewLine(); + writer.append("$rt_setTimeout(callback, ").append(pname).append(");").softNewLine(); writer.outdent().append("}").softNewLine(); addNotifyListener(context, writer, "callback"); writer.outdent().append("})();").softNewLine(); @@ -162,18 +161,16 @@ public class ObjectNativeGenerator implements Generator, Injector, DependencyPlu private void sendNotify(GeneratorContext context, SourceWriter writer) throws IOException { String lArr = getNotifyListeners(context); - writer.append("setTimeout(function(){").indent().softNewLine(); + writer.append("$rt_setTimeout(function(){").indent().softNewLine(); writer.append("if (!").append(lArr).append(" || ").append(lArr).append(".length===0){return;}").softNewLine(); - writer.append("var m = ").append(lArr).append(".shift();").softNewLine(); - writer.append("console.log('Notify callback : '+m);").softNewLine(); - writer.append("m.apply(null);").softNewLine(); + writer.append(lArr).append(".shift().apply(null);").softNewLine(); writer.outdent().append("}, 0);").softNewLine(); } private void sendNotifyAll(GeneratorContext context, SourceWriter writer) throws IOException { String obj = context.getParameterName(0); String lArr = getNotifyListeners(context); - writer.append("setTimeout(function(){").indent().softNewLine(); + writer.append("$rt_setTimeout(function(){").indent().softNewLine(); writer.append("if (!").append(lArr).append("){return;}").softNewLine(); writer.append("while (").append(lArr).append(".length>0){").indent().softNewLine(); writer.append(lArr).append(".shift().call(null);").softNewLine(); diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/ThreadNativeGenerator.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/ThreadNativeGenerator.java index 2b59714ba..5af53318f 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/ThreadNativeGenerator.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/ThreadNativeGenerator.java @@ -58,20 +58,20 @@ public class ThreadNativeGenerator implements Generator, DependencyPlugin { } private void generateSleep(GeneratorContext context, SourceWriter writer) throws IOException { - writer.append("setTimeout(function() {").indent().softNewLine(); + writer.append("$rt_setTimeout(function() {").indent().softNewLine(); writer.append(context.getCompleteContinuation()).append("($rt_asyncResult(null));").softNewLine(); writer.outdent().append("},").ws().append(context.getParameterName(1)).append(");").softNewLine(); } private void generateYield(GeneratorContext context, SourceWriter writer) throws IOException { - writer.append("setTimeout(function() {").indent().softNewLine(); + writer.append("$rt_setTimeout(function() {").indent().softNewLine(); writer.append(context.getCompleteContinuation()).append("($rt_asyncResult(null));").softNewLine(); writer.outdent().append("},").ws().append("0);").softNewLine(); } private void generateStart(GeneratorContext context, SourceWriter writer) throws IOException { String obj = context.getParameterName(0); - writer.append("setTimeout(function() { $rt_rootInvocationAdapter(").appendMethodBody(launchRef).append(")(") + writer.append("$rt_setTimeout(function() { $rt_rootInvocationAdapter(").appendMethodBody(launchRef).append(")(") .append(obj).append(");},0);").softNewLine(); } } diff --git a/teavm-core/src/main/java/org/teavm/cache/DiskProgramCache.java b/teavm-core/src/main/java/org/teavm/cache/DiskProgramCache.java index 716fa20dc..91f55b9b2 100644 --- a/teavm-core/src/main/java/org/teavm/cache/DiskProgramCache.java +++ b/teavm-core/src/main/java/org/teavm/cache/DiskProgramCache.java @@ -153,5 +153,15 @@ public class DiskProgramCache implements ProgramCache { @Override public void visit(IsInstanceInstruction insn) { } @Override public void visit(InitClassInstruction insn) { } @Override public void visit(NullCheckInstruction insn) { } + + @Override + public void visit(MonitorEnterInstruction insn) { + + } + + @Override + public void visit(MonitorExitInstruction insn) { + + } } } diff --git a/teavm-core/src/main/java/org/teavm/cache/ProgramIO.java b/teavm-core/src/main/java/org/teavm/cache/ProgramIO.java index 2e878c392..19978b16d 100644 --- a/teavm-core/src/main/java/org/teavm/cache/ProgramIO.java +++ b/teavm-core/src/main/java/org/teavm/cache/ProgramIO.java @@ -594,6 +594,16 @@ public class ProgramIO { throw new IOExceptionWrapper(e); } } + + @Override + public void visit(MonitorEnterInstruction insn) { + + } + + @Override + public void visit(MonitorExitInstruction insn) { + + } } private static class IOExceptionWrapper extends RuntimeException { diff --git a/teavm-core/src/main/java/org/teavm/javascript/StatementGenerator.java b/teavm-core/src/main/java/org/teavm/javascript/StatementGenerator.java index 9c3388ff6..3595f4611 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/StatementGenerator.java +++ b/teavm-core/src/main/java/org/teavm/javascript/StatementGenerator.java @@ -666,4 +666,14 @@ class StatementGenerator implements InstructionVisitor { public void visit(NullCheckInstruction insn) { assign(Expr.unary(UnaryOperation.NULL_CHECK, Expr.var(insn.getValue().getIndex())), insn.getReceiver()); } + + @Override + public void visit(MonitorEnterInstruction insn) { + + } + + @Override + public void visit(MonitorExitInstruction insn) { + + } } diff --git a/teavm-core/src/main/java/org/teavm/model/InstructionReadVisitor.java b/teavm-core/src/main/java/org/teavm/model/InstructionReadVisitor.java index 67cb3bbad..446953241 100644 --- a/teavm-core/src/main/java/org/teavm/model/InstructionReadVisitor.java +++ b/teavm-core/src/main/java/org/teavm/model/InstructionReadVisitor.java @@ -201,4 +201,14 @@ class InstructionReadVisitor implements InstructionVisitor { public void visit(NullCheckInstruction insn) { reader.nullCheck(insn.getReceiver(), insn.getValue()); } + + @Override + public void visit(MonitorEnterInstruction insn) { + + } + + @Override + public void visit(MonitorExitInstruction insn) { + + } } diff --git a/teavm-core/src/main/java/org/teavm/model/instructions/InstructionVisitor.java b/teavm-core/src/main/java/org/teavm/model/instructions/InstructionVisitor.java index 865335d5d..7a38d2306 100644 --- a/teavm-core/src/main/java/org/teavm/model/instructions/InstructionVisitor.java +++ b/teavm-core/src/main/java/org/teavm/model/instructions/InstructionVisitor.java @@ -87,4 +87,8 @@ public interface InstructionVisitor { void visit(InitClassInstruction insn); void visit(NullCheckInstruction insn); + + void visit(MonitorEnterInstruction insn); + + void visit(MonitorExitInstruction insn); } diff --git a/teavm-core/src/main/java/org/teavm/model/instructions/MonitorEnterInstruction.java b/teavm-core/src/main/java/org/teavm/model/instructions/MonitorEnterInstruction.java new file mode 100644 index 000000000..8938b5819 --- /dev/null +++ b/teavm-core/src/main/java/org/teavm/model/instructions/MonitorEnterInstruction.java @@ -0,0 +1,50 @@ +/* + * Copyright 2013 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.model.instructions; + +import org.teavm.model.Instruction; +import org.teavm.model.Variable; + +/** + * + * @author shannah + */ +public class MonitorEnterInstruction extends Instruction { + + private Variable objectRef; + + + + @Override + public void acceptVisitor(InstructionVisitor visitor) { + visitor.visit(this); + } + + /** + * @return the objectRef + */ + public Variable getObjectRef() { + return objectRef; + } + + /** + * @param objectRef the objectRef to set + */ + public void setObjectRef(Variable objectRef) { + this.objectRef = objectRef; + } + +} diff --git a/teavm-core/src/main/java/org/teavm/model/instructions/MonitorExitInstruction.java b/teavm-core/src/main/java/org/teavm/model/instructions/MonitorExitInstruction.java new file mode 100644 index 000000000..d705489ae --- /dev/null +++ b/teavm-core/src/main/java/org/teavm/model/instructions/MonitorExitInstruction.java @@ -0,0 +1,48 @@ +/* + * Copyright 2013 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.model.instructions; + +import org.teavm.model.Instruction; +import org.teavm.model.Variable; + +/** + * + * @author shannah + */ +public class MonitorExitInstruction extends Instruction { + + private Variable objectRef; + + @Override + public void acceptVisitor(InstructionVisitor visitor) { + visitor.visit(this); + } + + /** + * @return the objectRef + */ + public Variable getObjectRef() { + return objectRef; + } + + /** + * @param objectRef the objectRef to set + */ + public void setObjectRef(Variable objectRef) { + this.objectRef = objectRef; + } + +} diff --git a/teavm-core/src/main/java/org/teavm/model/util/BasicBlockMapper.java b/teavm-core/src/main/java/org/teavm/model/util/BasicBlockMapper.java index 520b90689..8162a454f 100644 --- a/teavm-core/src/main/java/org/teavm/model/util/BasicBlockMapper.java +++ b/teavm-core/src/main/java/org/teavm/model/util/BasicBlockMapper.java @@ -184,4 +184,18 @@ public abstract class BasicBlockMapper implements InstructionVisitor { @Override public void visit(NullCheckInstruction insn) { } + + @Override + public void visit(MonitorEnterInstruction insn) { + + } + + @Override + public void visit(MonitorExitInstruction insn) { + + } + + + + } diff --git a/teavm-core/src/main/java/org/teavm/model/util/DefinitionExtractor.java b/teavm-core/src/main/java/org/teavm/model/util/DefinitionExtractor.java index 5d0d42f43..7cca20ed0 100644 --- a/teavm-core/src/main/java/org/teavm/model/util/DefinitionExtractor.java +++ b/teavm-core/src/main/java/org/teavm/model/util/DefinitionExtractor.java @@ -202,4 +202,14 @@ public class DefinitionExtractor implements InstructionVisitor { public void visit(NullCheckInstruction insn) { definedVariables = new Variable[] { insn.getReceiver() }; } + + @Override + public void visit(MonitorEnterInstruction insn) { + + } + + @Override + public void visit(MonitorExitInstruction insn) { + + } } diff --git a/teavm-core/src/main/java/org/teavm/model/util/InstructionTransitionExtractor.java b/teavm-core/src/main/java/org/teavm/model/util/InstructionTransitionExtractor.java index 366134a22..6f6ef9856 100644 --- a/teavm-core/src/main/java/org/teavm/model/util/InstructionTransitionExtractor.java +++ b/teavm-core/src/main/java/org/teavm/model/util/InstructionTransitionExtractor.java @@ -204,4 +204,14 @@ public class InstructionTransitionExtractor implements InstructionVisitor { public void visit(NullCheckInstruction insn) { targets = null; } + + @Override + public void visit(MonitorEnterInstruction insn) { + targets = null; + } + + @Override + public void visit(MonitorExitInstruction insn) { + targets = null; + } } diff --git a/teavm-core/src/main/java/org/teavm/model/util/InstructionVariableMapper.java b/teavm-core/src/main/java/org/teavm/model/util/InstructionVariableMapper.java index fc9af0584..97d0e658c 100644 --- a/teavm-core/src/main/java/org/teavm/model/util/InstructionVariableMapper.java +++ b/teavm-core/src/main/java/org/teavm/model/util/InstructionVariableMapper.java @@ -228,4 +228,18 @@ public abstract class InstructionVariableMapper implements InstructionVisitor { insn.setReceiver(map(insn.getReceiver())); insn.setValue(map(insn.getValue())); } + + @Override + public void visit(MonitorEnterInstruction insn) { + + } + + @Override + public void visit(MonitorExitInstruction insn) { + + } + + + + } diff --git a/teavm-core/src/main/java/org/teavm/model/util/MissingItemsProcessor.java b/teavm-core/src/main/java/org/teavm/model/util/MissingItemsProcessor.java index f0885b3ae..b154e3a46 100644 --- a/teavm-core/src/main/java/org/teavm/model/util/MissingItemsProcessor.java +++ b/teavm-core/src/main/java/org/teavm/model/util/MissingItemsProcessor.java @@ -310,5 +310,15 @@ public class MissingItemsProcessor { @Override public void visit(EmptyInstruction insn) { } + + @Override + public void visit(MonitorEnterInstruction insn) { + + } + + @Override + public void visit(MonitorExitInstruction insn) { + + } }; } diff --git a/teavm-core/src/main/java/org/teavm/model/util/UsageExtractor.java b/teavm-core/src/main/java/org/teavm/model/util/UsageExtractor.java index 803ab0fd5..c65d9da33 100644 --- a/teavm-core/src/main/java/org/teavm/model/util/UsageExtractor.java +++ b/teavm-core/src/main/java/org/teavm/model/util/UsageExtractor.java @@ -206,4 +206,14 @@ public class UsageExtractor implements InstructionVisitor { public void visit(NullCheckInstruction insn) { usedVariables = new Variable[] { insn.getValue() }; } + + @Override + public void visit(MonitorEnterInstruction insn) { + usedVariables = new Variable[] {insn.getObjectRef() }; + } + + @Override + public void visit(MonitorExitInstruction insn) { + usedVariables = new Variable[] {insn.getObjectRef()}; + } } diff --git a/teavm-core/src/main/java/org/teavm/optimization/GlobalValueNumbering.java b/teavm-core/src/main/java/org/teavm/optimization/GlobalValueNumbering.java index efdfb2ab7..a6d4ac1ca 100644 --- a/teavm-core/src/main/java/org/teavm/optimization/GlobalValueNumbering.java +++ b/teavm-core/src/main/java/org/teavm/optimization/GlobalValueNumbering.java @@ -418,5 +418,15 @@ public class GlobalValueNumbering implements MethodOptimization { insn.setValue(program.variableAt(val)); bind(insn.getReceiver().getIndex(), "nullCheck @" + val); } + + @Override + public void visit(MonitorEnterInstruction insn) { + + } + + @Override + public void visit(MonitorExitInstruction insn) { + + } }; } diff --git a/teavm-core/src/main/java/org/teavm/optimization/LoopInvariantMotion.java b/teavm-core/src/main/java/org/teavm/optimization/LoopInvariantMotion.java index 74cf5a707..99e9a1cbe 100644 --- a/teavm-core/src/main/java/org/teavm/optimization/LoopInvariantMotion.java +++ b/teavm-core/src/main/java/org/teavm/optimization/LoopInvariantMotion.java @@ -379,6 +379,16 @@ public class LoopInvariantMotion implements MethodOptimization { public void visit(NullCheckInstruction insn) { canMove = true; } + + @Override + public void visit(MonitorEnterInstruction insn) { + + } + + @Override + public void visit(MonitorExitInstruction insn) { + + } } private class CopyConstantVisitor implements InstructionVisitor { @@ -561,5 +571,15 @@ public class LoopInvariantMotion implements MethodOptimization { @Override public void visit(NullCheckInstruction insn) { } + + @Override + public void visit(MonitorEnterInstruction insn) { + + } + + @Override + public void visit(MonitorExitInstruction insn) { + + } } } diff --git a/teavm-core/src/main/java/org/teavm/optimization/UnusedVariableElimination.java b/teavm-core/src/main/java/org/teavm/optimization/UnusedVariableElimination.java index 64f1f4cc5..b4d13841e 100644 --- a/teavm-core/src/main/java/org/teavm/optimization/UnusedVariableElimination.java +++ b/teavm-core/src/main/java/org/teavm/optimization/UnusedVariableElimination.java @@ -248,5 +248,15 @@ public class UnusedVariableElimination implements MethodOptimization { public void visit(NullCheckInstruction insn) { requestUsage(insn.getReceiver()); } + + @Override + public void visit(MonitorEnterInstruction insn) { + + } + + @Override + public void visit(MonitorExitInstruction insn) { + + } } } diff --git a/teavm-core/src/main/java/org/teavm/optimization/VariableEscapeAnalyzer.java b/teavm-core/src/main/java/org/teavm/optimization/VariableEscapeAnalyzer.java index ca185b27b..41deb3cb5 100644 --- a/teavm-core/src/main/java/org/teavm/optimization/VariableEscapeAnalyzer.java +++ b/teavm-core/src/main/java/org/teavm/optimization/VariableEscapeAnalyzer.java @@ -204,5 +204,15 @@ public final class VariableEscapeAnalyzer { @Override public void visit(NullCheckInstruction insn) { } + + @Override + public void visit(MonitorEnterInstruction insn) { + + } + + @Override + public void visit(MonitorExitInstruction insn) { + + } } } diff --git a/teavm-core/src/main/java/org/teavm/optimization/VariableUsageGraphBuilder.java b/teavm-core/src/main/java/org/teavm/optimization/VariableUsageGraphBuilder.java index b16104866..0b5e1c691 100644 --- a/teavm-core/src/main/java/org/teavm/optimization/VariableUsageGraphBuilder.java +++ b/teavm-core/src/main/java/org/teavm/optimization/VariableUsageGraphBuilder.java @@ -210,5 +210,15 @@ public final class VariableUsageGraphBuilder { public void visit(NullCheckInstruction insn) { use(insn.getReceiver(), insn.getValue()); } + + @Override + public void visit(MonitorEnterInstruction insn) { + + } + + @Override + public void visit(MonitorExitInstruction insn) { + + } } } diff --git a/teavm-core/src/main/java/org/teavm/parsing/ClassRefsRenamer.java b/teavm-core/src/main/java/org/teavm/parsing/ClassRefsRenamer.java index 939f2601c..921b9d700 100644 --- a/teavm-core/src/main/java/org/teavm/parsing/ClassRefsRenamer.java +++ b/teavm-core/src/main/java/org/teavm/parsing/ClassRefsRenamer.java @@ -284,4 +284,14 @@ public class ClassRefsRenamer implements InstructionVisitor { @Override public void visit(NullCheckInstruction insn) { } + + @Override + public void visit(MonitorEnterInstruction insn) { + + } + + @Override + public void visit(MonitorExitInstruction insn) { + + } } diff --git a/teavm-core/src/main/java/org/teavm/parsing/SSATransformer.java b/teavm-core/src/main/java/org/teavm/parsing/SSATransformer.java index 1060d5beb..b16afe881 100644 --- a/teavm-core/src/main/java/org/teavm/parsing/SSATransformer.java +++ b/teavm-core/src/main/java/org/teavm/parsing/SSATransformer.java @@ -447,5 +447,15 @@ public class SSATransformer { insn.setValue(use(insn.getValue())); insn.setReceiver(define(insn.getReceiver())); } + + @Override + public void visit(MonitorEnterInstruction insn) { + + } + + @Override + public void visit(MonitorExitInstruction insn) { + + } }; } diff --git a/teavm-core/src/main/java/org/teavm/vm/TeaVM.java b/teavm-core/src/main/java/org/teavm/vm/TeaVM.java index fb2dfa3f0..cf333c617 100644 --- a/teavm-core/src/main/java/org/teavm/vm/TeaVM.java +++ b/teavm-core/src/main/java/org/teavm/vm/TeaVM.java @@ -430,8 +430,7 @@ public class TeaVM implements TeaVMHost, ServiceRepository { // Keep track of current running thread by overriding setTimeout - sourceWriter.append("self.old_setTimeout=self.setTimeout;").softNewLine(); - sourceWriter.append("self.setTimeout=function(f,interval){").indent().softNewLine(); + sourceWriter.append("function $rt_setTimeout(f,interval){").indent().softNewLine(); MethodReference currentThreadRef = new MethodReference( Thread.class, "currentThread", Thread.class); MethodReference setCurrentThreadRef = new MethodReference( @@ -445,7 +444,7 @@ public class TeaVM implements TeaVMHost, ServiceRepository { sourceWriter.appendMethodBody(setCurrentThreadRef).append("("). appendMethodBody(getMainThreadRef).append("());}").softNewLine(); sourceWriter.outdent().append("};").softNewLine(); - sourceWriter.append("self.old_setTimeout(callback, interval);").softNewLine(); + sourceWriter.append("setTimeout(callback, interval);").softNewLine(); sourceWriter.outdent().append("};").softNewLine(); // END Thread stuff 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 d6793042b..ec2a05e12 100644 --- a/teavm-core/src/main/resources/org/teavm/javascript/runtime.js +++ b/teavm-core/src/main/resources/org/teavm/javascript/runtime.js @@ -416,7 +416,7 @@ function $rt_asyncAdapter(f) { return $return($rt_asyncResult(result)); } } -function $rt_rootInvocationAdapter(f) { +function $rt_rootInvocationAdapter(f, extraArgs) { return function() { var args = Array.prototype.slice.apply(arguments); if (extraArgs) {