From cccd855674b5d976802e6d54b5fa64a9695baf72 Mon Sep 17 00:00:00 2001 From: Andreas Gal Date: Mon, 14 Jul 2014 21:18:56 -0700 Subject: [PATCH] iteratively find methods up along the inheritance chain --- classes.js | 21 +++++++++++++-------- frame.js | 10 ++++++---- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/classes.js b/classes.js index 3c41aa0b..4dc0bb62 100644 --- a/classes.js +++ b/classes.js @@ -89,7 +89,7 @@ Classes.prototype.getClass = function(caller, className) { return this.getArrayClass(caller, className); if (!!(classInfo = this.loadClassFile(className + ".class"))) { classInfo.staticFields = {}; - var clinit = this.getMethod(classInfo, "", "()V", true); + var clinit = this.getMethod(caller, classInfo, "", "()V", true, false); if (clinit) caller.invoke(OPCODES.invokestatic, clinit); classInfo.constructor = function () { @@ -128,17 +128,22 @@ Classes.prototype.setStaticField = function(caller, className, fieldName, value) this.getClass(caller, className).staticFields[fieldName] = value; } -Classes.prototype.getMethod = function(classInfo, methodName, signature, staticFlag) { +Classes.prototype.getMethod = function(caller, classInfo, methodName, signature, staticFlag, inheritFlag) { console.log(classInfo.className, methodName, signature); - var methods = classInfo.methods; - for (var i=0; i", "(Ljava/lang/String;)V", false); + var ctor = CLASSES.getMethod(this, ex.class, "", "(Ljava/lang/String;)V", false, false); this.stack.push(ex); this.stack.push(message); this.invoke(OPCODES.invokespecial, ctor); @@ -124,7 +124,7 @@ Frame.prototype.invoke = function(op, methodInfo) { case OPCODES.invokevirtual: console.log("virtual dispatch", methodInfo.classInfo.className, obj.class.className, methodInfo.name, methodInfo.signature); if (methodInfo.classInfo != obj.class) - methodInfo = CLASSES.getMethod(obj.class, methodInfo.name, methodInfo.signature, op === OPCODES.invokestatic); + methodInfo = CLASSES.getMethod(this, obj.class, methodInfo.name, methodInfo.signature, op === OPCODES.invokestatic); break; } } @@ -133,9 +133,11 @@ Frame.prototype.invoke = function(op, methodInfo) { callee.locals = this.stack; callee.localsBase = this.stack.length - consumes; + console.log("consumes:", consumes, "localsBase:", callee.localsBase); + while (true) { var op = callee.read8(); - console.log(callee.methodInfo.classInfo.className, callee.methodInfo.name, callee.ip - 1, OPCODES[op], callee.stack.length); + console.log(callee.methodInfo.classInfo.className, callee.methodInfo.name, callee.ip - 1, OPCODES[op], callee.stack.join(",")); switch (op) { case OPCODES.return: this.stack.length -= consumes; @@ -1000,7 +1002,7 @@ Frame.prototype.invokestatic = Frame.prototype.invokevirtual = Frame.prototype.i var signature = cp[cp[cp[idx].name_and_type_index].signature_index].bytes; var classInfo = CLASSES.getClass(this, className); - var method = CLASSES.getMethod(classInfo, methodName, signature, op === OPCODES.invokestatic); + var method = CLASSES.getMethod(this, classInfo, methodName, signature, op === OPCODES.invokestatic); this.invoke(op, method); }