Fix some tests of html4j TCK

This commit is contained in:
Alexey Andreev 2015-03-01 11:10:59 +03:00
parent 140c5298db
commit 18ac2b2b25
6 changed files with 70 additions and 92 deletions

View File

@ -50,6 +50,24 @@ public class TObject {
boolean expired(); boolean expired();
} }
static void monitorEnterSync(TObject o) {
if (o.monitor == null) {
o.monitor = new Monitor();
}
if (o.monitor.owner == null) {
o.monitor.owner = TThread.currentThread();
}
o.monitor.count++;
}
static void monitorExitSync(TObject o) {
if (o.isEmptyMonitor() || o.monitor.owner != TThread.currentThread()) {
throw new TIllegalMonitorStateException();
}
--o.monitor.count;
o.isEmptyMonitor();
}
static void monitorEnter(TObject o) { static void monitorEnter(TObject o) {
monitorEnter(o, 1); monitorEnter(o, 1);
} }

View File

@ -18,8 +18,8 @@ package org.teavm.classlib.java.util;
import org.teavm.classlib.java.lang.TIllegalStateException; import org.teavm.classlib.java.lang.TIllegalStateException;
import org.teavm.classlib.java.lang.TObject; import org.teavm.classlib.java.lang.TObject;
import org.teavm.classlib.java.lang.TString; import org.teavm.classlib.java.lang.TString;
import org.teavm.dependency.PluggableDependency; import org.teavm.platform.Platform;
import org.teavm.javascript.spi.GeneratedBy; import org.teavm.platform.PlatformRunnable;
/** /**
* *
@ -53,7 +53,11 @@ public class TTimer extends TObject {
task.nativeTimerId = scheduleOnce(task, (int)delay); task.nativeTimerId = scheduleOnce(task, (int)delay);
} }
@GeneratedBy(TimerNativeGenerator.class) private static int scheduleOnce(final TTimerTask task, int delay) {
@PluggableDependency(TimerNativeGenerator.class) return Platform.schedule(new PlatformRunnable() {
private static native int scheduleOnce(TTimerTask task, int delay); @Override public void run() {
TTimerTask.performOnce(task);
}
}, delay);
}
} }

View File

@ -34,11 +34,11 @@ public abstract class TTimerTask extends TObject implements TRunnable {
return true; return true;
} }
void performOnce() { static void performOnce(TTimerTask task) {
if (timer != null) { if (task.timer != null) {
run(); task.run();
timer.tasks.remove(this); task.timer.tasks.remove(task);
timer = null; task.timer = null;
} }
} }
} }

View File

@ -1,64 +0,0 @@
/*
* Copyright 2014 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.classlib.java.util;
import java.io.IOException;
import java.util.TimerTask;
import org.teavm.codegen.SourceWriter;
import org.teavm.dependency.DependencyAgent;
import org.teavm.dependency.DependencyPlugin;
import org.teavm.dependency.MethodDependency;
import org.teavm.javascript.spi.Generator;
import org.teavm.javascript.spi.GeneratorContext;
import org.teavm.model.CallLocation;
import org.teavm.model.MethodReference;
/**
*
* @author Alexey Andreev
*/
public class TimerNativeGenerator implements Generator, DependencyPlugin {
private static final MethodReference performOnceRef = new MethodReference(TimerTask.class,
"performOnce", void.class);
@Override
public void methodAchieved(DependencyAgent agent, MethodDependency method, CallLocation location) {
switch (method.getReference().getName()) {
case "scheduleOnce": {
MethodDependency performMethod = agent.linkMethod(performOnceRef, null);
performMethod.use();
method.getVariable(1).connect(performMethod.getVariable(1));
break;
}
}
}
@Override
public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) throws IOException {
switch (methodRef.getName()) {
case "scheduleOnce":
generateScheduleNative(context, writer);
break;
}
}
private void generateScheduleNative(GeneratorContext context, SourceWriter writer) throws IOException {
writer.append("return setTimeout(function() {").indent().softNewLine();
writer.append(context.getParameterName(1)).append(".").appendMethod(performOnceRef)
.append("();").softNewLine();
writer.outdent().append("},").ws().append(context.getParameterName(2)).append(");").softNewLine();
}
}

View File

@ -499,6 +499,11 @@ class DependencyGraphBuilder {
new MethodReference(Object.class, "monitorEnter", Object.class, void.class), null); new MethodReference(Object.class, "monitorEnter", Object.class, void.class), null);
nodes[objectRef.getIndex()].connect(methodDep.getVariable(1)); nodes[objectRef.getIndex()].connect(methodDep.getVariable(1));
methodDep.use(); methodDep.use();
methodDep = dependencyChecker.linkMethod(
new MethodReference(Object.class, "monitorEnterSync", Object.class, void.class), null);
nodes[objectRef.getIndex()].connect(methodDep.getVariable(1));
methodDep.use();
} }
@Override @Override
@ -507,6 +512,11 @@ class DependencyGraphBuilder {
new MethodReference(Object.class, "monitorExit", Object.class, void.class), null); new MethodReference(Object.class, "monitorExit", Object.class, void.class), null);
nodes[objectRef.getIndex()].connect(methodDep.getVariable(1)); nodes[objectRef.getIndex()].connect(methodDep.getVariable(1));
methodDep.use(); methodDep.use();
methodDep = dependencyChecker.linkMethod(
new MethodReference(Object.class, "monitorExitSync", Object.class, void.class), null);
nodes[objectRef.getIndex()].connect(methodDep.getVariable(1));
methodDep.use();
} }
}; };
} }

View File

@ -2001,10 +2001,8 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
@Override @Override
public void visit(MonitorEnterStatement statement) { public void visit(MonitorEnterStatement statement) {
if (!async) {
return;
}
try { try {
if (async) {
MethodReference monitorEnterRef = new MethodReference( MethodReference monitorEnterRef = new MethodReference(
Object.class, "monitorEnter", Object.class, void.class); Object.class, "monitorEnter", Object.class, void.class);
writer.append("return ").append(naming.getFullNameForAsync(monitorEnterRef)).append("("); writer.append("return ").append(naming.getFullNameForAsync(monitorEnterRef)).append("(");
@ -2012,6 +2010,13 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
writer.append(",").ws(); writer.append(",").ws();
writer.append("$rt_continue($part_").append(statement.getAsyncTarget()).append(')'); writer.append("$rt_continue($part_").append(statement.getAsyncTarget()).append(')');
writer.append(");").softNewLine(); writer.append(");").softNewLine();
} else {
MethodReference monitorEnterRef = new MethodReference(
Object.class, "monitorEnterSync", Object.class, void.class);
writer.appendMethodBody(monitorEnterRef).append('(');
statement.getObjectRef().acceptVisitor(this);
writer.append(");").softNewLine();
}
} catch (IOException ex){ } catch (IOException ex){
throw new RenderingException("IO error occured", ex); throw new RenderingException("IO error occured", ex);
} }
@ -2019,15 +2024,20 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
@Override @Override
public void visit(MonitorExitStatement statement) { public void visit(MonitorExitStatement statement) {
if (!async) {
return;
}
try { try {
if (async) {
MethodReference monitorExitRef = new MethodReference( MethodReference monitorExitRef = new MethodReference(
Object.class, "monitorExit", Object.class, void.class); Object.class, "monitorExit", Object.class, void.class);
writer.appendMethodBody(monitorExitRef).append("("); writer.appendMethodBody(monitorExitRef).append("(");
statement.getObjectRef().acceptVisitor(this); statement.getObjectRef().acceptVisitor(this);
writer.append(");").softNewLine(); writer.append(");").softNewLine();
} else {
MethodReference monitorEnterRef = new MethodReference(
Object.class, "monitorExitSync", Object.class, void.class);
writer.appendMethodBody(monitorEnterRef).append('(');
statement.getObjectRef().acceptVisitor(this);
writer.append(");").softNewLine();
}
} catch (IOException ex){ } catch (IOException ex){
throw new RenderingException("IO error occured", ex); throw new RenderingException("IO error occured", ex);
} }