forked from YaroslavGaponov/node-jvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses.js
226 lines (197 loc) · 7.22 KB
/
classes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
node-jvm
Copyright (c) 2013 Yaroslav Gaponov <yaroslav.gaponov@gmail.com>
*/
var util = require("util");
var fs = require("fs");
var path = require("path");
var ClassArea = require("./classfile/classarea.js");
var Frame = require("./frame.js");
var ACCESS_FLAGS = require("./classfile/accessflags.js");
var Classes = module.exports = function() {
if (this instanceof Classes) {
this.paths = [ __dirname ];
this.classes = {};
this.staticFields = {};
} else {
return new Classes();
}
}
Classes.prototype.addPath = function(path) {
if (this.paths.indexOf(path) === -1) {
this.paths.push(path);
}
}
Classes.prototype.clinit = function() {
for(var className in this.classes) {
classArea = this.classes[className];
var clinit = this.getStaticMethod(className, "<clinit>", "()V");
if (clinit instanceof Frame) {
SCHEDULER.sync(function() {
LOG.debug("call " + className + ".<clinit> ...");
clinit.run([], function() {
LOG.debug("call " + className + ".<clinit> ... done");
});
});
}
}
}
Classes.prototype.loadClassBytes = function(bytes) {
var classArea = new ClassArea(bytes);
this.classes[classArea.getClassName()] = classArea;
var classes = classArea.getClasses();
for (var i=0; i<classes.length; i++) {
if (!this.classes[classes[i]]) {
this.loadClassFile(path.dirname(fileName) + path.sep + classes[i] + ".class");
}
}
return classArea;
}
Classes.prototype.loadClassFile = function(fileName) {
LOG.debug("loading " + fileName + " ...");
var bytes = fs.readFileSync(fileName);
return this.loadClassBytes(bytes);
}
Classes.prototype.loadJSFile = function(fileName) {
LOG.debug("loading " + fileName + " ...");
var classArea = require(fileName);
this.classes[classArea.getClassName()] = classArea;
return classArea;
}
Classes.prototype.loadJarFile = function(fileName) {
var self = this;
var AdmZip = require("adm-zip");
var zip = new AdmZip(fileName);
var zipEntries = zip.getEntries();
var mainClass;
zipEntries.forEach(function(zipEntry) {
if (!zipEntry.isDirectory) {
if (path.extname(zipEntry.entryName) === ".class") {
LOG.debug("loading " + fileName + '@' + zipEntry.entryName + " ...");
self.loadClassBytes(zipEntry.getData());
} else if (zipEntry.entryName === "META-INF/MANIFEST.MF") {
var manifest = zipEntry.getData().toString().split('\n');
for(var i=0; i<manifest.length; i++) {
var line = [];
manifest[i].split(':').forEach(
function(p) {
line.push(p.trim());
}
);
if (line[0] === "Main-Class") {
mainClass = line[1];
break;
}
}
}
}
return mainClass;
});
}
Classes.prototype.getEntryPoint = function(className, methodName) {
for(var name in this.classes) {
var ca = this.classes[name];
if (ca instanceof ClassArea) {
if (!className || (className === ca.getClassName())) {
if (ACCESS_FLAGS.isPublic(ca.getAccessFlags())) {
var methods = ca.getMethods();
var cp = ca.getConstantPool();
for(var i=0; i<methods.length; i++) {
if
(
ACCESS_FLAGS.isPublic(methods[i].access_flags) &&
ACCESS_FLAGS.isStatic(methods[i].access_flags) &&
cp[methods[i].name_index].bytes === methodName
)
{ return new Frame(ca, methods[i]); }
}
}
}
}
}
}
Classes.prototype.getClass = function(className) {
var ca = this.classes[className];
if (ca) {
return ca;
}
for(var i=0; i<this.paths.length; i++) {
var fileName = util.format("%s/%s", this.paths[i], className);
if (fs.existsSync(fileName + ".js")) {
return this.loadJSFile(fileName + ".js");
}
if(fs.existsSync(fileName + ".class")) {
return this.loadClassFile(fileName + ".class");
}
}
throw new Error(util.format("Implementation of the %s class is not found.", className));
};
Classes.prototype.getStaticField = function(className, fieldName) {
return this.staticFields[className + '.' + fieldName];
}
Classes.prototype.setStaticField = function(className, fieldName, value) {
this.staticFields[className + '.' + fieldName] = value;
}
Classes.prototype.getStaticMethod = function(className, methodName, signature) {
var ca = this.getClass(className);
if (ca instanceof ClassArea) {
var methods = ca.getMethods();
var cp = ca.getConstantPool();
for(var i=0; i<methods.length; i++)
if (ACCESS_FLAGS.isStatic(methods[i].access_flags))
if (cp[methods[i].name_index].bytes === methodName)
if (signature.toString() === cp[methods[i].signature_index].bytes)
return new Frame(ca, methods[i]);
} else {
if (methodName in ca) {
return ca[methodName];
}
}
return null;
};
Classes.prototype.getMethod = function(className, methodName, signature) {
var ca = this.getClass(className);
if (ca instanceof ClassArea) {
var methods = ca.getMethods();
var cp = ca.getConstantPool();
for(var i=0; i<methods.length; i++)
if (!ACCESS_FLAGS.isStatic(methods[i].access_flags))
if (cp[methods[i].name_index].bytes === methodName)
if (signature.toString() === cp[methods[i].signature_index].bytes)
return new Frame(ca, methods[i]);
} else {
var o = new ca();
if (methodName in o) {
return o[methodName];
}
}
return null;
};
Classes.prototype.newObject = function(className) {
var ca = this.getClass(className);
if (ca instanceof ClassArea) {
var ctor = function() {};
ctor.prototype = this.newObject(ca.getSuperClassName());
var o = new ctor();
o.getClassName = new Function(util.format("return \"%s\"", className));
var cp = ca.getConstantPool();
ca.getFields().forEach(function(field) {
var fieldName = cp[field.name_index].bytes;
o[fieldName] = null;
});
ca.getMethods().forEach(function(method) {
var methodName = cp[method.name_index].bytes;
o[methodName] = new Frame(ca, method);
});
return o;
} else {
var o = new ca();
o.getClassName = new Function(util.format("return \"%s\"", className));
return o;
}
}
Classes.prototype.newException = function(className, message, cause) {
var ex = this.newObject(className);
ex["<init>"](message, cause);
return ex;
}