pluotsorbet/classes.js

168 строки
5.7 KiB
JavaScript
Исходник Обычный вид История

2014-07-06 12:29:36 +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 Classes = function() {
if (this instanceof Classes) {
this.classfiles = [];
this.mainclass = [];
this.classes = {};
} else {
return new Classes();
}
}
Classes.prototype.addPath = function(name, data) {
if (name.substr(-4) === ".jar") {
data = new ZipFile(data);
}
this.classfiles[name] = data;
}
Classes.prototype.loadFile = function(fileName) {
2014-07-06 13:03:36 +04:00
var classfiles = this.classfiles;
var data = classfiles[fileName];
2014-07-06 12:29:36 +04:00
if (data)
return data;
2014-07-06 13:03:36 +04:00
Object.keys(classfiles).every(function (name) {
2014-07-06 12:29:36 +04:00
if (name.substr(-4) !== ".jar")
return true;
2014-07-06 13:03:36 +04:00
var zip = classfiles[name];
if (fileName in zip.directory) {
var bytes = zip.read(fileName);
data = bytes.buffer.slice(0, bytes.length);
}
return !data;
2014-07-06 12:29:36 +04:00
});
2014-07-06 13:03:36 +04:00
classfiles[fileName] = data;
2014-07-06 12:29:36 +04:00
return data;
}
Classes.prototype.loadClassBytes = function(bytes) {
2014-07-10 10:17:03 +04:00
var classData = new ClassData(bytes);
this.classes[classData.getClassName()] = classData;
return classData;
2014-07-06 12:29:36 +04:00
}
Classes.prototype.loadClassFile = function(fileName) {
LOG.debug("loading " + fileName + " ...");
var bytes = this.loadFile(fileName);
if (!bytes)
return null;
2014-07-10 10:17:03 +04:00
var classData = this.loadClassBytes(bytes);
var classes = classData.getClasses();
2014-07-06 12:29:36 +04:00
for (var i=0; i<classes.length; i++) {
if (!this.classes[classes[i]]) {
this.loadClassFile(path.dirname(fileName) + path.sep + classes[i] + ".class");
}
}
2014-07-10 10:17:03 +04:00
return classData;
2014-07-06 12:29:36 +04:00
}
Classes.prototype.getEntryPoint = function(className, methodName) {
for(var name in this.classes) {
2014-07-10 10:17:03 +04:00
var classData = this.classes[name];
if (classData instanceof ClassData) {
if (!className || (className === classData.getClassName())) {
if (ACCESS_FLAGS.isPublic(classData.getAccessFlags())) {
var methods = classData.getMethods();
var cp = classData.getConstantPool();
2014-07-10 09:23:17 +04:00
for (var i=0; i<methods.length; i++) {
if (ACCESS_FLAGS.isPublic(methods[i].access_flags) &&
ACCESS_FLAGS.isStatic(methods[i].access_flags) &&
!ACCESS_FLAGS.isNative(methods[i].access_flags) &&
cp[methods[i].name_index].bytes === methodName) {
2014-07-10 10:17:03 +04:00
return new Frame(classData, methods[i]);
2014-07-10 09:23:17 +04:00
}
2014-07-06 12:29:36 +04:00
}
}
}
}
2014-07-12 20:48:05 +04:00
}
2014-07-06 12:29:36 +04:00
}
2014-07-09 11:12:58 +04:00
Classes.prototype.initClass = function(className) {
var clinit = this.getStaticMethod(className, "<clinit>", "()V");
2014-07-09 12:07:14 +04:00
if (!clinit)
return;
2014-07-13 01:40:38 +04:00
LOG.debug("call " + className + ".<clinit> ...");
clinit.run(THREADS.current.stack);
2014-07-09 11:12:58 +04:00
}
Classes.prototype.getClass = function(className, initialize) {
2014-07-10 10:17:03 +04:00
var classData = this.classes[className];
if (classData)
return classData;
if (!!(classData = this.loadClassFile(className + ".class"))) {
2014-07-09 11:12:58 +04:00
if (initialize) {
2014-07-10 10:17:03 +04:00
classData.staticFields = {};
2014-07-09 11:12:58 +04:00
this.initClass(className);
}
2014-07-10 10:17:03 +04:00
return classData;
2014-07-09 11:12:58 +04:00
}
2014-07-06 12:29:36 +04:00
throw new Error(util.format("Implementation of the %s class is not found.", className));
};
Classes.prototype.getStaticField = function(className, fieldName) {
2014-07-09 11:12:58 +04:00
return this.getClass(className, true).staticFields[fieldName];
2014-07-06 12:29:36 +04:00
}
Classes.prototype.setStaticField = function(className, fieldName, value) {
2014-07-09 11:12:58 +04:00
this.getClass(className, true).staticFields[fieldName] = value;
2014-07-06 12:29:36 +04:00
}
2014-07-09 12:07:14 +04:00
Classes.prototype.getMethod = function(className, methodName, signature, staticFlag) {
// Only force initialization when accessing a static method.
2014-07-10 10:17:03 +04:00
var classData = this.getClass(className, staticFlag);
var methods = classData.getMethods();
var cp = classData.getConstantPool();
2014-07-10 09:23:17 +04:00
for (var i=0; i<methods.length; i++) {
if (ACCESS_FLAGS.isStatic(methods[i].access_flags) === !!staticFlag) {
if (cp[methods[i].name_index].bytes === methodName) {
2014-07-13 01:07:11 +04:00
if (signature === cp[methods[i].signature_index].bytes) {
2014-07-10 09:23:17 +04:00
if (ACCESS_FLAGS.isNative(methods[i].access_flags)) {
2014-07-13 01:07:11 +04:00
return NATIVE.getMethod(className, methodName, signature);
2014-07-10 09:23:17 +04:00
}
2014-07-12 08:24:24 +04:00
return new Frame(classData, methods[i]);
2014-07-10 09:23:17 +04:00
}
}
2014-07-06 12:29:36 +04:00
}
}
return null;
};
2014-07-09 12:07:14 +04:00
Classes.prototype.getStaticMethod = function(className, methodName, signature) {
return this.getMethod(className, methodName, signature, true);
}
Classes.prototype.newObject = function(className) {
// Force initialization of the class (if not already done).
2014-07-09 12:28:03 +04:00
return { class: this.getClass(className, true) };
}
2014-07-09 12:28:03 +04:00
Classes.prototype.newArray = function(type, size) {
switch (type) {
case ARRAY_TYPE.T_BOOLEAN: return new Uint8Array(size);
case ARRAY_TYPE.T_CHAR: return new Uint16Array(size);
case ARRAY_TYPE.T_FLOAT: return new Float32Array(size);
case ARRAY_TYPE.T_DOUBLE: return new Float64Array(size);
case ARRAY_TYPE.T_BYTE: return new Int8Array(size);
case ARRAY_TYPE.T_SHORT: return new Int16Array(size);
case ARRAY_TYPE.T_INT: return new Int32Array(size);
case ARRAY_TYPE.T_LONG: return new Int64Array(size);
}
2014-07-06 12:29:36 +04:00
}
2014-07-09 12:28:03 +04:00
Classes.prototype.newString = function(s) {
var obj = this.newObject("java/lang/String");
var length = s.length;
var chars = this.newArray(ARRAY_TYPE.T_CHAR, length);
for (var n = 0; n < length; ++n)
chars[n] = s.charCodeAt(n);
obj.value = chars;
obj.offset = 0;
obj.count = length;
return obj;
2014-07-12 20:48:05 +04:00
}