This commit is contained in:
Andreas Gal 2014-07-27 23:39:41 -07:00
Родитель eabe4d5a5c
Коммит 2f4d32f420
5 изменённых файлов: 100 добавлений и 6 удалений

1
jvm.js
Просмотреть файл

@ -47,6 +47,7 @@ JVM.prototype.run = function(className) {
CLASSES.java_lang_Thread = CLASSES.loadClass("java/lang/Thread");
ctx.thread = CLASSES.mainThread = CLASSES.newObject(CLASSES.java_lang_Thread);
ctx.thread.running = true;
caller.stack.push(CLASSES.mainThread);
caller.stack.push(ctx.newString("main"));
ctx.pushFrame(CLASSES.getMethod(CLASSES.java_lang_Thread, "<init>", "(Ljava/lang/String;)V"), 2);

Просмотреть файл

@ -31,11 +31,11 @@ console.log = function() {
document.getElementById("log").textContent += s;
}
runTest("RunTests");
//runTest("RunTests");
//runTest("gnu/testlet/vm/SystemTest");
//runTest("TestThread");
//runTest("TestRuntime");
//runTest("Andreas");
runTest("Andreas");
//runTest("TestDate");
//runTest("RunAll");
//runTest("TestArrays");

Просмотреть файл

@ -320,16 +320,49 @@ Native["java/lang/Thread.start0.()V"] = function(ctx, stack) {
ctx.raiseException("java/lang/IllegalThreadStateException");
thread.running = true;
var run = CLASSES.getMethod(thread.class, "run", "()V", false, true);
// Spawn a new thread.
// Create a context for the thread and start it.
var ctx = new Context();
ctx.thread = thread;
var caller = new Frame();
caller.stack.push(thread);
ctx.frames.push(caller);
ctx.pushFrame(run, 1);
caller.stack.push(thread);
var syntheticMethod = {
classInfo: {
constant_pool: [
null,
{ class_index: 2, name_and_type_index: 4 },
{ name_index: 3 },
{ bytes: "java/lang/Thread" },
{ name_index: 5, signature_index: 6 },
{ bytes: "run" },
{ bytes: "()V" },
{ class_index: 2, name_and_type_index: 8 },
{ name_index: 9, signature_index: 10 },
{ bytes: "internalExit" },
{ bytes: "()V" },
],
},
code: [
0x2a, // aload_0
0x59, // dup
0xb7, 0x00, 0x01, // invokespecial <idx=1>
0xb7, 0x00, 0x07, // invokespecial <idx=7>
0xb1, // return
],
exception_table: [],
};
ctx.pushFrame(syntheticMethod, 1);
ctx.start(caller);
}
Native["java/lang/Thread.internalExit.()V"] = function(ctx, stack) {
stack.pop().running = false;
}
Native["java/lang/Thread.isAlive.()Z"] = function(ctx, stack) {
stack.push(stack.pop().running ? 1 : 0);
}
Native["java/lang/Thread.sleep.(J)V"] = function(ctx, stack) {
var delay = stack.pop2().toNumber();
window.setTimeout(function() {

Просмотреть файл

@ -0,0 +1,58 @@
// Tags: JDK1.0
// Copyright (C) 2002 Free Software Foundation, Inc.
// Written by Mark Wielaard (mark@klomp.org)
// This file is part of Mauve.
// Mauve is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2, or (at your option)
// any later version.
// Mauve is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Mauve; see the file COPYING. If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA. */
package gnu.testlet.java.lang.Thread;
import gnu.testlet.Testlet;
import gnu.testlet.TestHarness;
public class name implements Testlet
{
public void test (TestHarness harness)
{
Thread current = Thread.currentThread();
harness.check(current.getName() != null,
"Every Thread has a non-null name");
Thread t = new Thread("Test-Thread");
harness.check(t.getName().equals("Test-Thread"),
"Create thread with name");
boolean null_exception = false;
try
{
new Thread((String)null);
}
catch (NullPointerException npe)
{
null_exception = true;
}
harness.check(null_exception, "Cannot create Thread with null name");
t = new Thread();
String name = t.getName();
harness.check(name != null, "New Thread has non-null name");
}
}

4
vm.js
Просмотреть файл

@ -109,7 +109,7 @@ VM.execute = function(ctx) {
while (true) {
var op = frame.read8();
// console.log("PID" + ctx.pid, frame.methodInfo.classInfo.className + " " + frame.methodInfo.name + " " + (frame.ip - 1) + " " + OPCODES[op] + " " + stack.join(","));
// console.log(ctx.pid, frame.methodInfo.classInfo.className + " " + frame.methodInfo.name + " " + (frame.ip - 1) + " " + OPCODES[op] + " " + stack.join(","));
switch (op) {
case 0x00: // nop
break;
@ -983,6 +983,8 @@ VM.execute = function(ctx) {
case OPCODES.invokeinterface:
if (methodInfo.classInfo != obj.class)
methodInfo = CLASSES.getMethod(obj.class, methodInfo.name, methodInfo.signature, false, true);
if (!methodInfo)
console.log(obj.class.className, classInfo.className, methodName, signature, isStatic);
break;
}
}