mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 16:14:10 -08:00
Minor improvements
This commit is contained in:
parent
0cdf960ba5
commit
9d112817b8
|
@ -124,7 +124,8 @@ public class TObject {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Rename("notify")
|
@Rename("notify")
|
||||||
public final void notify0(){
|
public final void notify0() {
|
||||||
|
TThread thread = TThread.currentThread();
|
||||||
if (notifyListeners != null) {
|
if (notifyListeners != null) {
|
||||||
while (notifyListeners.getLength() > 0 && notifyListeners.shift().handleNotify()) {
|
while (notifyListeners.getLength() > 0 && notifyListeners.shift().handleNotify()) {
|
||||||
// repeat loop
|
// repeat loop
|
||||||
|
@ -133,6 +134,7 @@ public class TObject {
|
||||||
notifyListeners = null;
|
notifyListeners = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
TThread.setCurrentThread(thread);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Rename("notifyAll")
|
@Rename("notifyAll")
|
||||||
|
@ -167,8 +169,7 @@ public class TObject {
|
||||||
if (notifyListeners == null) {
|
if (notifyListeners == null) {
|
||||||
notifyListeners = window.newArray();
|
notifyListeners = window.newArray();
|
||||||
}
|
}
|
||||||
final TThread currentThread = TThread.currentThread();
|
final NotifyListenerImpl listener = new NotifyListenerImpl(callback);
|
||||||
final NotifyListenerImpl listener = new NotifyListenerImpl(callback, currentThread);
|
|
||||||
notifyListeners.push(listener);
|
notifyListeners.push(listener);
|
||||||
if (timeout == 0 && nanos == 0) {
|
if (timeout == 0 && nanos == 0) {
|
||||||
return;
|
return;
|
||||||
|
@ -178,13 +179,12 @@ public class TObject {
|
||||||
|
|
||||||
private static class NotifyListenerImpl implements NotifyListener, TimerHandler {
|
private static class NotifyListenerImpl implements NotifyListener, TimerHandler {
|
||||||
final AsyncCallback<Void> callback;
|
final AsyncCallback<Void> callback;
|
||||||
final TThread currentThread;
|
final TThread currentThread = TThread.currentThread();
|
||||||
int timerId = -1;
|
int timerId = -1;
|
||||||
boolean finished;
|
boolean finished;
|
||||||
|
|
||||||
public NotifyListenerImpl(AsyncCallback<Void> callback, TThread currentThread) {
|
public NotifyListenerImpl(AsyncCallback<Void> callback) {
|
||||||
this.callback = callback;
|
this.callback = callback;
|
||||||
this.currentThread = currentThread;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -264,7 +264,9 @@ public class TeaVMTool {
|
||||||
if (mainClass != null) {
|
if (mainClass != null) {
|
||||||
MethodDescriptor mainMethodDesc = new MethodDescriptor("main", String[].class, void.class);
|
MethodDescriptor mainMethodDesc = new MethodDescriptor("main", String[].class, void.class);
|
||||||
vm.entryPoint("main", new MethodReference(mainClass, mainMethodDesc))
|
vm.entryPoint("main", new MethodReference(mainClass, mainMethodDesc))
|
||||||
.withValue(1, "java.lang.String").async();
|
.withValue(1, "[java.lang.String")
|
||||||
|
.withArrayValue(1, "java.lang.String")
|
||||||
|
.async();
|
||||||
}
|
}
|
||||||
for (ClassAlias alias : classAliases) {
|
for (ClassAlias alias : classAliases) {
|
||||||
vm.exportType(alias.getAlias(), alias.getClassName());
|
vm.exportType(alias.getAlias(), alias.getClassName());
|
||||||
|
@ -300,7 +302,7 @@ public class TeaVMTool {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (mainClass != null) {
|
if (mainClass != null) {
|
||||||
writer.append("main = $rt_rootInvocationAdapter(main);\n");
|
writer.append("main = $rt_mainWrapper(main);\n");
|
||||||
}
|
}
|
||||||
ProblemProvider problemProvider = vm.getProblemProvider();
|
ProblemProvider problemProvider = vm.getProblemProvider();
|
||||||
if (problemProvider.getProblems().isEmpty()) {
|
if (problemProvider.getProblems().isEmpty()) {
|
||||||
|
|
|
@ -431,28 +431,6 @@ public class TeaVM implements TeaVMHost, ServiceRepository {
|
||||||
listener.begin(renderer, target);
|
listener.begin(renderer, target);
|
||||||
}
|
}
|
||||||
sourceWriter.append("\"use strict\";").newLine();
|
sourceWriter.append("\"use strict\";").newLine();
|
||||||
|
|
||||||
|
|
||||||
// Keep track of current running thread by overriding setTimeout
|
|
||||||
sourceWriter.append("function $rt_setTimeout(f,interval){").indent().softNewLine();
|
|
||||||
MethodReference currentThreadRef = new MethodReference(
|
|
||||||
Thread.class, "currentThread", Thread.class);
|
|
||||||
MethodReference setCurrentThreadRef = new MethodReference(
|
|
||||||
Thread.class, "setCurrentThread", Thread.class, void.class);
|
|
||||||
MethodReference getMainThreadRef = new MethodReference(Thread.class, "getMainThread", Thread.class);
|
|
||||||
|
|
||||||
sourceWriter.append("var currThread = ").appendMethodBody(currentThreadRef).append("();").softNewLine();
|
|
||||||
sourceWriter.append("var callback = function(){").indent().softNewLine();
|
|
||||||
sourceWriter.appendMethodBody(setCurrentThreadRef).append("(currThread);").softNewLine();
|
|
||||||
sourceWriter.append("try{f();} finally {").softNewLine();
|
|
||||||
sourceWriter.appendMethodBody(setCurrentThreadRef).append("(").
|
|
||||||
appendMethodBody(getMainThreadRef).append("());}").softNewLine();
|
|
||||||
sourceWriter.outdent().append("};").softNewLine();
|
|
||||||
sourceWriter.append("setTimeout(callback, interval);").softNewLine();
|
|
||||||
sourceWriter.outdent().append("};").softNewLine();
|
|
||||||
|
|
||||||
// END Thread stuff
|
|
||||||
|
|
||||||
renderer.renderRuntime();
|
renderer.renderRuntime();
|
||||||
renderer.render(clsNodes);
|
renderer.render(clsNodes);
|
||||||
renderer.renderStringPool();
|
renderer.renderStringPool();
|
||||||
|
|
|
@ -97,6 +97,14 @@ public class TeaVMEntryPoint {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public TeaVMEntryPoint withArrayValue(int argument, String type) {
|
||||||
|
if (argument > reference.parameterCount()) {
|
||||||
|
throw new IllegalArgumentException("Illegal argument #" + argument + " of " + reference.parameterCount());
|
||||||
|
}
|
||||||
|
method.getVariable(argument).getArrayItem().propagate(method.getDependencyAgent().getType(type));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public TeaVMEntryPoint async() {
|
public TeaVMEntryPoint async() {
|
||||||
this.async = true;
|
this.async = true;
|
||||||
return this;
|
return this;
|
||||||
|
|
|
@ -481,6 +481,18 @@ function $rt_rootInvocationAdapter(f) {
|
||||||
return f.apply(this, args);
|
return f.apply(this, args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
function $rt_mainWrapper(f) {
|
||||||
|
return function(args) {
|
||||||
|
if (!args) {
|
||||||
|
args = [];
|
||||||
|
}
|
||||||
|
var javaArgs = $rt_createArray($rt_objcls(), args.length);
|
||||||
|
for (var i = 0; i < args.length; ++i) {
|
||||||
|
javaArgs.data[i] = $rt_str(args[i]);
|
||||||
|
}
|
||||||
|
$rt_rootInvocationAdapter(f)(javaArgs);
|
||||||
|
};
|
||||||
|
}
|
||||||
var $rt_stringPool_instance;
|
var $rt_stringPool_instance;
|
||||||
function $rt_stringPool(strings) {
|
function $rt_stringPool(strings) {
|
||||||
$rt_stringPool_instance = new Array(strings.length);
|
$rt_stringPool_instance = new Array(strings.length);
|
||||||
|
|
|
@ -1,19 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project-shared-configuration>
|
|
||||||
<!--
|
|
||||||
This file contains additional configuration written by modules in the NetBeans IDE.
|
|
||||||
The configuration is intended to be shared among all the users of project and
|
|
||||||
therefore it is assumed to be part of version control checkout.
|
|
||||||
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
|
|
||||||
-->
|
|
||||||
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
|
|
||||||
<!--
|
|
||||||
Properties that influence various parts of the IDE, especially code formatting and the like.
|
|
||||||
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
|
|
||||||
That way multiple projects can share the same settings (useful for formatting rules for example).
|
|
||||||
Any value defined here will override the pom.xml file value but is only applicable to the current project.
|
|
||||||
-->
|
|
||||||
<ca-weblite-netbeans-mirah.project_5f_type>maven</ca-weblite-netbeans-mirah.project_5f_type>
|
|
||||||
<org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_deploy_2e_server>gfv3ee6</org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_deploy_2e_server>
|
|
||||||
</properties>
|
|
||||||
</project-shared-configuration>
|
|
|
@ -1,17 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<actions>
|
|
||||||
<action>
|
|
||||||
<actionName>build</actionName>
|
|
||||||
<packagings>
|
|
||||||
<packaging>*</packaging>
|
|
||||||
</packagings>
|
|
||||||
<goals>
|
|
||||||
<goal>install</goal>
|
|
||||||
</goals>
|
|
||||||
<properties>
|
|
||||||
<skipTests>true</skipTests>
|
|
||||||
<jpda.listen>maven</jpda.listen>
|
|
||||||
|
|
||||||
</properties>
|
|
||||||
</action>
|
|
||||||
</actions>
|
|
|
@ -15,20 +15,25 @@
|
||||||
*/
|
*/
|
||||||
package org.teavm.samples.async;
|
package org.teavm.samples.async;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author Alexey Andreev <konsoletyper@gmail.com>
|
* @author Alexey Andreev <konsoletyper@gmail.com>
|
||||||
*/
|
*/
|
||||||
public final class AsyncProgram {
|
public final class AsyncProgram {
|
||||||
|
private static long start = System.currentTimeMillis();
|
||||||
|
|
||||||
private AsyncProgram() {
|
private AsyncProgram() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws InterruptedException {
|
public static void main(String[] args) throws InterruptedException {
|
||||||
|
report(Arrays.toString(args));
|
||||||
withoutAsync();
|
withoutAsync();
|
||||||
System.out.println();
|
report("");
|
||||||
withAsync();
|
withAsync();
|
||||||
|
|
||||||
System.out.println();
|
report("");
|
||||||
final Object lock = new Object();
|
final Object lock = new Object();
|
||||||
Thread t = new Thread(new Runnable() {
|
Thread t = new Thread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -36,7 +41,7 @@ public final class AsyncProgram {
|
||||||
try {
|
try {
|
||||||
doRun(lock);
|
doRun(lock);
|
||||||
} catch (InterruptedException ex) {
|
} catch (InterruptedException ex) {
|
||||||
System.out.println(ex.getMessage());
|
report("Exception caught: " + ex.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,82 +54,87 @@ public final class AsyncProgram {
|
||||||
try {
|
try {
|
||||||
doRun(lock);
|
doRun(lock);
|
||||||
} catch (InterruptedException ex) {
|
} catch (InterruptedException ex) {
|
||||||
System.out.println(ex.getMessage());
|
report("Exception caught: " + ex.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, "Test Thread 2");
|
}, "Test Thread 2");
|
||||||
t2.start();
|
t2.start();
|
||||||
|
|
||||||
System.out.println("Should be main -> Current thread is " + Thread.currentThread().getName());
|
report("Should be main");
|
||||||
System.out.println("Now trying wait...");
|
report("Now trying wait...");
|
||||||
|
|
||||||
|
synchronized (lock) {
|
||||||
lock.wait(20000);
|
lock.wait(20000);
|
||||||
System.out.println("Finished waiting");
|
}
|
||||||
|
report("Finished main thread");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void report(String message) {
|
||||||
|
long current = System.currentTimeMillis() - start;
|
||||||
|
System.out.println("[" + Thread.currentThread().getName() + "]/" + current + ": " + message);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void doRun(Object lock) throws InterruptedException {
|
private static void doRun(Object lock) throws InterruptedException {
|
||||||
System.out.println("Current thread is " + Thread.currentThread().getName());
|
report("Executing timer task");
|
||||||
System.out.println("Executing timer task");
|
|
||||||
Thread.sleep(2000);
|
Thread.sleep(2000);
|
||||||
System.out.println("Current thread is " + Thread.currentThread().getName());
|
report("Calling lock.notify()");
|
||||||
System.out.println("Calling lock.notify()");
|
synchronized (lock) {
|
||||||
lock.notify();
|
lock.notify();
|
||||||
System.out.println("Current thread is " + Thread.currentThread().getName());
|
}
|
||||||
System.out.println("Finished calling lock.notify()");
|
report("Finished calling lock.notify()");
|
||||||
|
report("Waiting 5 seconds");
|
||||||
Thread.sleep(5000);
|
Thread.sleep(5000);
|
||||||
System.out.println("Current thread is " + Thread.currentThread().getName());
|
report("Finished another 5 second sleep");
|
||||||
System.out.println("Finished another 5 second sleep");
|
|
||||||
|
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
System.out.println("Inside locked section of thread " + Thread.currentThread().getName());
|
report("Sleep inside locked section");
|
||||||
Thread.sleep(2000);
|
Thread.sleep(2000);
|
||||||
System.out.println("Finished locked section of thread " + Thread.currentThread().getName());
|
report("Finished locked section");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void withoutAsync() {
|
private static void withoutAsync() {
|
||||||
System.out.println("Start sync");
|
report("Start sync");
|
||||||
for (int i = 0; i < 20; ++i) {
|
for (int i = 0; i < 20; ++i) {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
for (int j = 0; j <= i; ++j) {
|
for (int j = 0; j <= i; ++j) {
|
||||||
System.out.print(j);
|
sb.append(j);
|
||||||
System.out.print(' ');
|
sb.append(' ');
|
||||||
}
|
}
|
||||||
System.out.println();
|
report(sb.toString());
|
||||||
}
|
}
|
||||||
System.out.println("Complete sync");
|
report("Complete sync");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void withAsync() throws InterruptedException {
|
private static void withAsync() throws InterruptedException {
|
||||||
System.out.println("Start async");
|
report("Start async");
|
||||||
for (int i = 0; i < 20; ++i) {
|
for (int i = 0; i < 20; ++i) {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
for (int j = 0; j <= i; ++j) {
|
for (int j = 0; j <= i; ++j) {
|
||||||
System.out.print(j);
|
sb.append(j);
|
||||||
System.out.print(' ');
|
sb.append(' ');
|
||||||
}
|
}
|
||||||
System.out.println();
|
report(sb.toString());
|
||||||
if (i % 3 == 0) {
|
if (i % 3 == 0) {
|
||||||
System.out.println("Suspend for a second");
|
report("Suspend for a second");
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
System.out.println("2nd Thread.sleep in same method");
|
report("2nd Thread.sleep in same method");
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
|
|
||||||
System.out.println("Complete async");
|
report("Throwing exception");
|
||||||
|
|
||||||
System.out.println("Throwing exception");
|
|
||||||
try {
|
try {
|
||||||
throwException();
|
throwException();
|
||||||
} catch (IllegalStateException e) {
|
} catch (IllegalStateException e) {
|
||||||
System.out.println("Exception caught");
|
report("Exception caught");
|
||||||
}
|
}
|
||||||
|
report("Complete async");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void throwException() {
|
private static void throwException() {
|
||||||
Thread.yield();
|
Thread.yield();
|
||||||
System.out.println("Thread.yield called");
|
report("Thread.yield called");
|
||||||
throw new IllegalStateException();
|
throw new IllegalStateException();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,7 @@
|
||||||
<script type="text/javascript" charset="utf-8" src="teavm/runtime.js"></script>
|
<script type="text/javascript" charset="utf-8" src="teavm/runtime.js"></script>
|
||||||
<script type="text/javascript" charset="utf-8" src="teavm/classes.js"></script>
|
<script type="text/javascript" charset="utf-8" src="teavm/classes.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body onload="main(null)">
|
<body onload="main()">
|
||||||
<p>Please, open developer's console to view program's output</p>
|
<p>Please, open developer's console to view program's output</p>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
Loading…
Reference in New Issue
Block a user