This repository has been archived by the owner on Mar 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
jvm-lib.js
135 lines (123 loc) · 3.65 KB
/
jvm-lib.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
var nextLockId = 0;
function addFactory(description) {
factories[description.className] = description;
}
addFactory({
className: "java/lang/Object",
create: function() {
return new JavaObject();
},
instanceMethods: ["toString", "hashCode", "equals"]
});
function JavaObject() {
this.$lockId = nextLockId++ | 0;
}
JavaObject.prototype.$factory = factories["java/lang/Object"];
JavaObject.prototype.toString = function() { return "[object: " + this.$lockId + "]"; };
JavaObject.prototype.hashCode = function() { return this.$lockId; };
JavaObject.prototype.equals = function(other) { return this == normalizeObject(other, "java/lang/Object"); };
JavaObject.prototype["<init>"] = function() {};
var systemOut = {
print: function(s) { log(s); },
println : function(s) { log((s||"") + "\n"); }
};
addFactory({
className: "java/lang/System",
statics: {
"out": systemOut,
"err": systemOut,
"exit": function() {
throw "Exit was called";
}
}
});
addFactory({
className: "java/lang/StringBuilder",
superFactory: factories["java/lang/Object"],
create: function() {
var buffer;
return {
"<init>": function() {
buffer = "";
},
append: function(value) {
if (typeof value === "object" && "value" in value) {
buffer += value.value;
} else if (value != null) {
buffer += value.toString();
}
return this;
},
toString: function() {
return buffer;
}
};
},
instanceMethods: ["toString", "append"]
});
addFactory({
className: "java/lang/String",
superFactory: factories["java/lang/Object"],
create: function () {
return new JavaString();
},
instanceMethods: ["toString", "hashCode", "equals"]
});
function JavaString(s) {
if (typeof s === "string") {
this.s = s;
this.$self = this;
var super_= new JavaObject();
this.$super = super_;
super_.$self = this;
super_.$upcast = this;
}
};
JavaString.prototype.$factory = factories["java/lang/String"];
JavaString.prototype.toString = function() { return this.s; };
JavaString.prototype.hashCode = function() { return this.s.length; };
JavaString.prototype.equals = function(other) { return this.s == normalizeObject(other, "java/lang/String").s; };
JavaString.prototype["<init>"] = function(s) { this.s = s; };
addFactory({
className: "java/lang/Throwable",
superFactory: factories["java/lang/Object"],
create: function () {
return {
"<init>" : function(message, cause) {
this.$super["<init>"]();
this.message = message || null;
this.cause = cause || null;
},
getMessage: function() { return this.message; },
getCause: function() { return this.cause; },
toString: function() {
return this.$self.$factory.className.replace(/\//g, ".") + ": " + this.$self.getMessage();
}
};
},
instanceMethods: ["getMessage", "getCause", "toString"]
});
addFactory({
className: "java/lang/Exception",
superFactory: factories["java/lang/Throwable"],
create: function() {
return { "<init>" : function(message, cause) { this.$super["<init>"](message, cause); } };
},
instanceMethods: []
});
addFactory({
className: "java/lang/RuntimeException",
superFactory: factories["java/lang/Exception"],
create: function() {
return { "<init>" : function(message, cause) { this.$super["<init>"](message, cause); } };
},
instanceMethods: []
});
addFactory({
className: "java/lang/ArithmeticException",
superFactory: factories["java/lang/RuntimeException"],
create: function() {
return { "<init>" : function(message) { this.$super["<init>"](message); } };
},
instanceMethods: []
});