2014-07-13 01:07:11 +04:00
|
|
|
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* vim: set shiftwidth=4 tabstop=4 autoindent cindent expandtab: */
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var Native = function() {
|
|
|
|
}
|
|
|
|
|
2014-07-13 11:00:11 +04:00
|
|
|
Native.prototype.invokeNative = function(caller, methodInfo) {
|
2014-07-13 10:13:53 +04:00
|
|
|
function pushType(type, value) {
|
|
|
|
if (type === "long" || type === "double") {
|
|
|
|
caller.stack.push2(value);
|
|
|
|
return;
|
2014-07-13 01:07:11 +04:00
|
|
|
}
|
2014-07-13 10:13:53 +04:00
|
|
|
caller.stack.push(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
function popType(type) {
|
|
|
|
return (type === "long" || type === "double") ? caller.stack.pop2() : caller.stack.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
function popArgs(types) {
|
2014-07-13 11:12:33 +04:00
|
|
|
var argc = types.length;
|
|
|
|
if (!ACCESS_FLAGS.isStatic(methodInfo.access_flags))
|
|
|
|
++argc;
|
2014-07-13 10:13:53 +04:00
|
|
|
var args = Array(types.length);
|
2014-07-13 11:12:33 +04:00
|
|
|
for (var i=types.length-1, j=args.length-1; i >= 0; --i, --j)
|
|
|
|
args[j] = popType(types[i].type);
|
|
|
|
if (j >= 0)
|
|
|
|
args[0] = caller.stack.pop();
|
2014-07-13 10:13:53 +04:00
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2014-07-13 11:00:11 +04:00
|
|
|
var signature = methodInfo.signature;
|
2014-07-13 10:13:53 +04:00
|
|
|
var args = popArgs(signature.IN);
|
2014-07-13 11:00:11 +04:00
|
|
|
if (!methodInfo.native)
|
|
|
|
methodInfo.native = this.getMethod(methodInfo);
|
2014-07-13 11:12:33 +04:00
|
|
|
result = methodInfo.native.apply(caller, args);
|
2014-07-13 10:13:53 +04:00
|
|
|
if (signature.OUT.length)
|
|
|
|
pushType(signature.OUT[0], result);
|
2014-07-13 01:07:11 +04:00
|
|
|
}
|
|
|
|
|
2014-07-13 11:00:11 +04:00
|
|
|
Native.prototype.getMethod = function (methodInfo) {
|
|
|
|
var classInfo = methodInfo.classInfo;
|
|
|
|
var cp = classInfo.getConstantPool();
|
2014-07-13 11:12:33 +04:00
|
|
|
var className = classInfo.getClassName();
|
|
|
|
var methodName = cp[methodInfo.name_index].bytes;
|
|
|
|
var signature = cp[methodInfo.signature_index].bytes;
|
2014-07-13 10:13:53 +04:00
|
|
|
console.log("Native.getMethod", className, methodName, signature);
|
|
|
|
return this[className + "." + methodName + "." + signature];
|
|
|
|
}
|
|
|
|
|
|
|
|
Native.prototype["java/lang/System.arraycopy.(Ljava/lang/Object;ILjava/lang/Object;II)V"] = function (src, srcOffset, dst, dstOffset, length) {
|
|
|
|
console.log(src, srcOffset, dst, dstOffset, length);
|
|
|
|
var srcProto = Object.getPrototypeOf(src);
|
|
|
|
var dstProto = Object.getPrototypeOf(dst);
|
|
|
|
if (srcProto === dstProto) {
|
|
|
|
}
|
|
|
|
}
|