From 9e3bd1c3c8113535b447d4f641338bf05b9bc885 Mon Sep 17 00:00:00 2001 From: Andreas Gal Date: Sat, 12 Jul 2014 14:40:38 -0700 Subject: [PATCH] more fixes --- classes.js | 8 ++------ frame.js | 20 ++++++++++---------- jvm.js | 7 +------ locals.js | 6 +++--- stack.js | 2 +- thread.js | 5 +++++ threads.js | 6 +++--- 7 files changed, 25 insertions(+), 29 deletions(-) diff --git a/classes.js b/classes.js index 92016389..f9c64f76 100644 --- a/classes.js +++ b/classes.js @@ -86,12 +86,8 @@ Classes.prototype.initClass = function(className) { var clinit = this.getStaticMethod(className, "", "()V"); if (!clinit) return; - SCHEDULER.sync(function() { - LOG.debug("call " + className + ". ..."); - clinit.run([], function() { - LOG.debug("call " + className + ". ... done"); - }); - }); + LOG.debug("call " + className + ". ..."); + clinit.run(THREADS.current.stack); } Classes.prototype.getClass = function(className, initialize) { diff --git a/frame.js b/frame.js index 4bd4d931..e47c5be8 100644 --- a/frame.js +++ b/frame.js @@ -106,24 +106,26 @@ Frame.prototype.run = function(stack) { this.ip = 0; - var result; while (true) { var op = this.read8(); - console.log(this.ip - 1, op, OPCODES[op], stack.length); + console.log(this.ip - 1, OPCODES[op], stack.array.length); switch (op) { case OPCODES.return: - break; + stack.popLocals(locals); + return; case OPCODES.ireturn: case OPCODES.freturn: case OPCODES.areturn: - result = stack.pop(); - break; + var result = stack.pop(); + stack.popLocals(locals); + return result; case OPCODES.lreturn: case OPCODES.dreturn: - result = stack.pop2(); - break; + var result = stack.pop2(); + stack.popLocals(locals); + return result; default: var opName = OPCODES[op]; @@ -134,9 +136,6 @@ Frame.prototype.run = function(stack) { break; } }; - - stack.popLocals(locals); - return result; } Frame.prototype.nop = function(stack, locals) { @@ -364,6 +363,7 @@ Frame.prototype.lastore = Frame.prototype.dastore = function(stack, locals) { } Frame.prototype.pop = function(stack, locals) { + console.trace(); stack.pop(); } diff --git a/jvm.js b/jvm.js index 051afdb0..5dd8284b 100644 --- a/jvm.js +++ b/jvm.js @@ -59,10 +59,5 @@ JVM.prototype.run = function() { throw new Error("Entry point method is not found."); } - console.log(entryPoint); - - var stack = new Stack(); - stack.push(null); // args - var exit = entryPoint.run(stack); - console.log("exit(" + exit + ")"); + THREADS.current.startMain(entryPoint, []); } diff --git a/locals.js b/locals.js index f0f5e473..66df009b 100644 --- a/locals.js +++ b/locals.js @@ -4,14 +4,14 @@ 'use strict'; var Locals = function(stack, base) { - this.stack = stack; + this.array = stack.array; this.base = base; } Locals.prototype.set = function (idx, value) { - this.stack[base + idx] = value; + this.array[this.base + idx] = value; } Locals.prototype.get = function (idx) { - return this.stack[base + idx]; + return this.array[this.base + idx]; } diff --git a/stack.js b/stack.js index 0c67c7da..a9594255 100644 --- a/stack.js +++ b/stack.js @@ -47,7 +47,7 @@ Stack.prototype.popArgs = function (signature) { } Stack.prototype.top = function () { - return this.array[this.array.lengt - 1]; + return this.array[this.array.length - 1]; } Stack.prototype.reserveLocals = function (argc, max_locals) { diff --git a/thread.js b/thread.js index 531a4c86..beef1da3 100644 --- a/thread.js +++ b/thread.js @@ -7,6 +7,7 @@ var Thread = function(name) { if (this instanceof Thread) { this.name = name || "noname"; this.priority = (Thread.MAX_PRIORITY + Thread.MIN_PRIORITY) >> 1; + this.stack = new Stack(); } else { return new Thread(name); } @@ -31,3 +32,7 @@ Thread.prototype.getPriority = function() { return this.priority; } +Thread.prototype.startMain = function(entryPoint, args) { + this.stack.push(null); // args + entryPoint.run(this.stack); +} diff --git a/threads.js b/threads.js index 1dd61c4f..0c7a6c61 100644 --- a/threads.js +++ b/threads.js @@ -6,6 +6,9 @@ var Threads = function() { this.threads = []; this.empty = []; + var mainThread = new Thread("main"); + this.add(mainThread); + this.current = mainThread; } Threads.prototype.add = function(thread) { @@ -30,6 +33,3 @@ Threads.prototype.count = function() { Threads.prototype.getThread = function(pid) { return this.threads[pid]; } - - -