Skip to content

Commit

Permalink
Move alternateImpl detection into MethodInfo constructor.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcav committed Oct 8, 2014
1 parent 04a379e commit 0a6b67f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 50 deletions.
24 changes: 20 additions & 4 deletions classinfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ FieldInfo.prototype.toString = function() {
return "[field " + this.name + "]";
}

function missingNativeImpl(key, ctx, stack) {
console.error("Attempted to invoke missing native:", key);
}

function MethodInfo(m, classInfo, constantPool) {
this.classInfo = classInfo;
this.name = constantPool[m.name_index].bytes;
Expand All @@ -50,10 +54,22 @@ function MethodInfo(m, classInfo, constantPool) {
this.isPublic = ACCESS_FLAGS.isPublic(m.access_flags);
this.isStatic = ACCESS_FLAGS.isStatic(m.access_flags);
this.isSynchronized = ACCESS_FLAGS.isSynchronized(m.access_flags);

this.alternateImpl = (this.isNative ? Native :
Override.hasMethod(this) ? Override :
null);
this.key = (this.isStatic ? "S." : "I.") + this.name + "." + this.signature;
this.implKey = this.classInfo.className + "." + this.name + "." + this.signature;

if (this.isNative) {
if (this.implKey in Native) {
this.alternateImpl = Native[this.implKey];
} else {
// Some Native MethodInfos are constructed but never called;
// that's fine, unless we actually try to call them.
this.alternateImpl = missingNativeImpl.bind(null, this.implKey);
}
} else if (this.implKey in Override) {
this.alternateImpl = Override[this.implKey];
} else {
this.alternateImpl = null;
}
}

var ClassInfo = function(classBytes) {
Expand Down
16 changes: 6 additions & 10 deletions instrument.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@ var Instrument = {
profiling: false,
profile: null,

getKey: function(methodInfo) {
return methodInfo.classInfo.className + "." + methodInfo.name + "." + methodInfo.signature;
},

callEnterHooks: function(methodInfo, caller, callee) {
var key = this.getKey(methodInfo);
var key = methodInfo.implKey;
if (Instrument.enter[key]) {
Instrument.enter[key](caller, callee);
}
Expand All @@ -36,7 +32,7 @@ var Instrument = {
},

callExitHooks: function(methodInfo, caller, callee) {
var key = this.getKey(methodInfo);
var key = methodInfo.implKey;

if (this.profiling) {
var now = performance.now();
Expand Down Expand Up @@ -101,16 +97,16 @@ var Instrument = {
this.profiling = false;
},

measure: function(Alt, ctx, methodInfo) {
measure: function(alternateImpl, ctx, methodInfo) {
var key = methodInfo.implKey;
if (this.profiling) {
var then = performance.now();
Alt.invoke(ctx, methodInfo);
var key = this.getKey(methodInfo);
alternateImpl.call(null, ctx, ctx.current().stack);
var methodProfileData = this.profile[key] || (this.profile[key] = { count: 0, cost: 0 });
methodProfileData.count++;
methodProfileData.cost += performance.now() - then;
} else {
Alt.invoke(ctx, methodInfo);
alternateImpl.call(null, ctx, ctx.current().stack);
}
},
};
Expand Down
12 changes: 0 additions & 12 deletions native.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,6 @@

var Native = {};

Native.invoke = function(ctx, methodInfo) {
if (!methodInfo.native) {
var key = methodInfo.classInfo.className + "." + methodInfo.name + "." + methodInfo.signature;
methodInfo.native = Native[key];
if (!methodInfo.native) {
console.error("Missing native: " + key);
ctx.raiseExceptionAndYield("java/lang/RuntimeException", key + " not found");
}
}
methodInfo.native.call(null, ctx, ctx.current().stack);
}

Native["java/lang/System.arraycopy.(Ljava/lang/Object;ILjava/lang/Object;II)V"] = function(ctx, stack) {
var length = stack.pop(), dstOffset = stack.pop(), dst = stack.pop(), srcOffset = stack.pop(), src = stack.pop();
if (!src || !dst)
Expand Down
20 changes: 0 additions & 20 deletions override.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,6 @@

var Override = {};

Override.getKey = function(methodInfo) {
return methodInfo.classInfo.className + "." + methodInfo.name + "." + methodInfo.signature;
}

Override.hasMethod = function(methodInfo) {
return ("override" in methodInfo || Override.getKey(methodInfo) in Override);
}

Override.invoke = function(ctx, methodInfo) {
if (!methodInfo.override) {
var key = Override.getKey(methodInfo);
methodInfo.override = Override[key];
if (!methodInfo.override) {
console.error("Missing override: " + key);
ctx.raiseExceptionAndYield("java/lang/RuntimeException", key + " not found");
}
}
methodInfo.override.call(null, ctx, ctx.current().stack);
}

Override["com/ibm/oti/connection/file/Connection.decode.(Ljava/lang/String;)Ljava/lang/String;"] = function(ctx, stack) {
var string = util.fromJavaString(stack.pop());
stack.push(ctx.newString(decodeURIComponent(string)));
Expand Down
4 changes: 0 additions & 4 deletions vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -1040,10 +1040,6 @@ VM.execute = function(ctx) {
case OPCODES.invokevirtual:
case OPCODES.invokeinterface:
if (methodInfo.classInfo != obj.class) {
if (!methodInfo.key) {
methodInfo.key = "I." + methodInfo.name + "." + methodInfo.signature;
}

// Check if the method is already in the virtual method cache
if (obj.class.vmc[methodInfo.key]) {
methodInfo = obj.class.vmc[methodInfo.key];
Expand Down

0 comments on commit 0a6b67f

Please sign in to comment.