forked from mbebenita/j2me.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
override.js
299 lines (248 loc) · 9.42 KB
/
override.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
'use strict';
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)));
}
Override["com/ibm/oti/connection/file/Connection.encode.(Ljava/lang/String;)Ljava/lang/String;"] = function(ctx, stack) {
var string = util.fromJavaString(stack.pop());
stack.push(ctx.newString(string.replace(/[^a-zA-Z0-9-_\.!~\*\\'()/:]/g, encodeURIComponent)));
}
Override["java/lang/Math.min.(II)I"] = function(ctx, stack) {
var b = stack.pop(), a = stack.pop();
stack.push(a <= b ? a : b);
}
Override["java/io/ByteArrayOutputStream.write.([BII)V"] = function(ctx, stack) {
var len = stack.pop(), off = stack.pop(), b = stack.pop(), _this = stack.pop();
if ((off < 0) || (off > b.length) || (len < 0) ||
((off + len) > b.length)) {
ctx.raiseExceptionAndYield("java/lang/IndexOutOfBoundsException");
}
if (len == 0) {
return;
}
var count = _this.class.getField("I.count.I").get(_this);
var buf = _this.class.getField("I.buf.[B").get(_this);
var newcount = count + len;
if (newcount > buf.length) {
var newbuf = ctx.newPrimitiveArray("B", Math.max(buf.length << 1, newcount));
newbuf.set(buf);
buf = newbuf;
_this.class.getField("I.buf.[B").set(_this, buf);
}
buf.set(b.subarray(off, off + len), count);
_this.class.getField("I.count.I").set(_this, newcount);
}
Override["java/io/ByteArrayOutputStream.write.(I)V"] = function(ctx, stack) {
var value = stack.pop(), _this = stack.pop();
var count = _this.class.getField("I.count.I").get(_this);
var buf = _this.class.getField("I.buf.[B").get(_this);
var newcount = count + 1;
if (newcount > buf.length) {
var newbuf = ctx.newPrimitiveArray("B", Math.max(buf.length << 1, newcount));
newbuf.set(buf);
buf = newbuf;
_this.class.getField("I.buf.[B").set(_this, buf);
}
buf[count] = value;
_this.class.getField("I.count.I").set(_this, newcount);
}
Override["java/io/ByteArrayInputStream.<init>.([B)V"] = function(ctx, stack) {
var buf = stack.pop(), _this = stack.pop();
if (!buf) {
ctx.raiseExceptionAndYield("java/lang/NullPointerException");
}
_this.buf = buf;
_this.pos = _this.mark = 0;
_this.count = buf.length;
}
Override["java/io/ByteArrayInputStream.<init>.([BII)V"] = function(ctx, stack) {
var length = stack.pop(), offset = stack.pop(), buf = stack.pop(), _this = stack.pop();
if (!buf) {
ctx.raiseExceptionAndYield("java/lang/NullPointerException");
}
_this.buf = buf;
_this.pos = _this.mark = offset;
_this.count = (offset + length <= buf.length) ? (offset + length) : buf.length;
}
Override["java/io/ByteArrayInputStream.read.()I"] = function(ctx, stack) {
var _this = stack.pop();
stack.push((_this.pos < _this.count) ? (_this.buf[_this.pos++] & 0xFF) : -1);
}
Override["java/io/ByteArrayInputStream.read.([BII)I"] = function(ctx, stack) {
var len = stack.pop(), off = stack.pop(), b = stack.pop(), _this = stack.pop();
if (!b) {
ctx.raiseExceptionAndYield("java/lang/NullPointerException");
}
if ((off < 0) || (off > b.length) || (len < 0) ||
((off + len) > b.length)) {
ctx.raiseExceptionAndYield("java/lang/IndexOutOfBoundsException");
}
if (_this.pos >= _this.count) {
stack.push(-1);
return;
}
if (_this.pos + len > _this.count) {
len = _this.count - _this.pos;
}
if (len === 0) {
stack.push(0);
return;
}
b.set(_this.buf.subarray(_this.pos, _this.pos + len), off);
_this.pos += len;
stack.push(len);
}
Override["java/io/ByteArrayInputStream.skip.(J)J"] = function(ctx, stack) {
var n = stack.pop2().toNumber(), _this = stack.pop();
if (_this.pos + n > _this.count) {
n = _this.count - _this.pos;
}
if (n < 0) {
stack.push2(Long.fromNumber(0));
return;
}
_this.pos += n;
stack.push2(Long.fromNumber(n));
}
Override["java/io/ByteArrayInputStream.available.()I"] = function(ctx, stack) {
var _this = stack.pop();
stack.push(_this.count - _this.pos);
}
Override["java/io/ByteArrayInputStream.mark.(I)V"] = function(ctx, stack) {
var readAheadLimit = stack.pop(), _this = stack.pop();
_this.mark = _this.pos;
}
Override["java/io/ByteArrayInputStream.reset.()V"] = function(ctx, stack) {
var _this = stack.pop();
_this.pos = _this.mark;
}
function JavaException(className, message) {
this.javaClassName = className;
this.message = message;
}
JavaException.prototype = Object.create(Error.prototype);
/**
* A simple wrapper for overriding JVM functions to avoid logic errors
* and simplify implementation:
*
* - Arguments are pushed off the stack based upon the number of
* arguments listed on `fn`.
*
* - The return value is automatically pushed back onto the stack, if
* the method signature does not return void. CAUTION: If you want to
* return a Long or Double, this code needs to be modified
* accordingly to do a `push2`. (Ideally, we'd just scrape the
* method signature and always do the right thing.)
*
* - The object reference ("this") is automatically bound to `fn`,
* unless you specify { static: true } in opts.
*
* - JavaException instances are caught and propagated as Java
exceptions; JS TypeError propagates as a NullPointerException.
*
* Simple overrides don't currently have access to `ctx` or `stack`.
*
* @param {string} key
* The fully-qualified JVM method signature.
* @param {function(args)} fn
* A function taking any number of args. The number of arguments
* this function takes is the number of args popped off of the stack.
* @param {object} opts
* { static: true } if the method is static (and should not receive
* and pop the `this` argument off the stack).
*/
Override.simple = function(key, fn, opts) {
var isStatic = opts && opts.static;
var isVoid = key[key.length - 1] === 'V';
var numArgs = fn.length;
Override[key] = function(ctx, stack) {
var args = new Array(numArgs);
// NOTE: If your function accepts a Long/Double, you must specify
// two arguments (since they take up two stack positions); we
// could sugar this someday.
for (var i = numArgs - 1; i >= 0; i--) {
args[i] = stack.pop();
}
try {
var self = isStatic ? null : stack.pop();
var ret = fn.apply(self, args);
if (!isVoid) {
if (ret === true) {
stack.push(1);
} else if (ret === false) {
stack.push(0);
} else if (typeof ret === "string") {
stack.push(ctx.newString(ret));
} else {
stack.push(ret);
}
}
} catch(e) {
if (e.name === "TypeError") {
// JavaScript's TypeError is analogous to a NullPointerException.
ctx.raiseExceptionAndYield("java/lang/NullPointerException", e);
} else if (e.javaClassName) {
ctx.raiseExceptionAndYield(e.javaClassName, e.message);
} else {
console.error(e, e.stack);
ctx.raiseExceptionAndYield("java/lang/RuntimeException", e);
}
}
};
}
// The following Permissions methods are overriden to avoid expensive calls to
// DomainPolicy.loadValues. This has the added benefit that we avoid many other
// computations.
Override["com/sun/midp/security/Permissions.forDomain.(Ljava/lang/String;)[[B"] = function(ctx, stack) {
var name = stack.pop();
// NUMBER_OF_PERMISSIONS = PermissionsStrings.PERMISSION_STRINGS.length + 2
// The 2 is the two hardcoded MIPS and AMS permissions.
var NUMBER_OF_PERMISSIONS = 61;
var ALLOW = 1;
var maximums = ctx.newPrimitiveArray("B", NUMBER_OF_PERMISSIONS);
var defaults = ctx.newPrimitiveArray("B", NUMBER_OF_PERMISSIONS);
for (var i = 0; i < NUMBER_OF_PERMISSIONS; i++) {
maximums[i] = defaults[i] = ALLOW;
}
var permissions = ctx.newArray("[[B", 2);
permissions[0] = maximums;
permissions[1] = defaults;
stack.push(permissions);
}
// Always return true to make Java think the MIDlet domain is trusted.
Override["com/sun/midp/security/Permissions.isTrusted.(Ljava/lang/String;)Z"] = function(ctx, stack) {
var name = stack.pop();
stack.push(1);
}
// Returns the ID of the permission. The callers will use this ID to check the
// permission in the permissions array returned by Permissions::forDomain.
Override["com/sun/midp/security/Permissions.getId.(Ljava/lang/String;)I"] = function(ctx, stack) {
var name = stack.pop();
stack.push(0);
}
// The Java code that uses this method doesn't actually use the return value, but
// passes it to Permissions.getId. So we can return anything.
Override["com/sun/midp/security/Permissions.getName.(I)Ljava/lang/String;"] = function(ctx, stack) {
var id = stack.pop();
stack.push("com.sun.midp");
}