2014-09-24 10:50:23 +04:00
|
|
|
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
|
|
|
|
2015-01-11 12:02:23 +03:00
|
|
|
// 'use strict';
|
2014-09-24 10:50:23 +04:00
|
|
|
|
|
|
|
var Override = {};
|
|
|
|
|
2014-12-19 00:03:59 +03:00
|
|
|
function asyncImpl(returnKind, promise) {
|
|
|
|
var ctx = $.ctx;
|
|
|
|
|
|
|
|
promise.then(function(res) {
|
|
|
|
if (returnKind === "J" || returnKind === "D") {
|
|
|
|
ctx.current().stack.push2(res);
|
|
|
|
} else if (returnKind !== "V") {
|
|
|
|
ctx.current().stack.push(res);
|
|
|
|
} else {
|
|
|
|
// void, do nothing
|
2014-11-27 22:15:28 +03:00
|
|
|
}
|
2014-12-19 00:03:59 +03:00
|
|
|
}, function(exception) {
|
2014-12-16 06:38:08 +03:00
|
|
|
var syntheticMethod = new MethodInfo({
|
|
|
|
name: "RaiseExceptionSynthetic",
|
|
|
|
signature: "()V",
|
|
|
|
isStatic: true,
|
2015-01-15 04:13:14 +03:00
|
|
|
classInfo: Object.create(J2ME.ClassInfo.prototype, {
|
|
|
|
className: {value: "java/lang/Object"},
|
|
|
|
vmc: {value: {}},
|
|
|
|
vfc: {value: {}},
|
|
|
|
constant_pool: {value: [
|
2014-12-19 00:03:59 +03:00
|
|
|
null
|
2015-01-15 04:13:14 +03:00
|
|
|
]}
|
|
|
|
}),
|
2014-12-16 06:38:08 +03:00
|
|
|
code: new Uint8Array([
|
2014-12-19 00:03:59 +03:00
|
|
|
0x2a, // aload_0
|
2014-12-16 06:38:08 +03:00
|
|
|
0xbf // athrow
|
|
|
|
])
|
|
|
|
});
|
2015-01-16 12:45:59 +03:00
|
|
|
var callee = Frame.create(syntheticMethod, [exception], 0);
|
2014-12-16 06:38:08 +03:00
|
|
|
ctx.frames.push(callee);
|
2014-12-17 23:30:14 +03:00
|
|
|
}).then(ctx.resume.bind(ctx));
|
2015-01-11 12:02:23 +03:00
|
|
|
$.pause("Async");
|
2014-11-04 04:47:51 +03:00
|
|
|
}
|
|
|
|
|
2014-12-19 00:03:59 +03:00
|
|
|
Override["com/ibm/oti/connection/file/Connection.decode.(Ljava/lang/String;)Ljava/lang/String;"] = function(string) {
|
2014-12-23 23:17:16 +03:00
|
|
|
return J2ME.newString(decodeURIComponent(string.str));
|
2014-12-19 00:03:59 +03:00
|
|
|
};
|
2014-10-14 20:42:01 +04:00
|
|
|
|
2014-12-19 00:03:59 +03:00
|
|
|
Override["com/ibm/oti/connection/file/Connection.encode.(Ljava/lang/String;)Ljava/lang/String;"] = function(string) {
|
2014-12-23 23:17:16 +03:00
|
|
|
return J2ME.newString(string.str.replace(/[^a-zA-Z0-9-_\.!~\*\\'()/:]/g, encodeURIComponent));
|
2014-12-19 00:03:59 +03:00
|
|
|
};
|
2014-10-14 20:42:01 +04:00
|
|
|
|
2014-12-19 00:03:59 +03:00
|
|
|
Override["java/lang/Math.min.(II)I"] = function(a, b) {
|
2014-10-14 20:42:01 +04:00
|
|
|
return Math.min(a, b);
|
2014-12-19 00:03:59 +03:00
|
|
|
};
|
2014-09-26 21:37:25 +04:00
|
|
|
|
2014-12-19 00:03:59 +03:00
|
|
|
Override["java/io/ByteArrayOutputStream.write.([BII)V"] = function(b, off, len) {
|
2014-10-14 20:42:01 +04:00
|
|
|
if ((off < 0) || (off > b.length) || (len < 0) ||
|
|
|
|
((off + len) > b.length)) {
|
2014-12-18 05:12:21 +03:00
|
|
|
throw $.newIndexOutOfBoundsException();
|
2014-10-14 20:42:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (len == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-27 05:40:02 +03:00
|
|
|
var count = this.klass.classInfo.getField("I.count.I").get(this);
|
|
|
|
var buf = this.klass.classInfo.getField("I.buf.[B").get(this);
|
2014-10-14 20:42:01 +04:00
|
|
|
|
|
|
|
var newcount = count + len;
|
|
|
|
if (newcount > buf.length) {
|
2014-11-27 05:35:12 +03:00
|
|
|
var newbuf = J2ME.newByteArray(Math.max(buf.length << 1, newcount));
|
2014-10-14 20:42:01 +04:00
|
|
|
newbuf.set(buf);
|
|
|
|
buf = newbuf;
|
2014-11-27 05:40:02 +03:00
|
|
|
this.klass.classInfo.getField("I.buf.[B").set(this, buf);
|
2014-10-14 20:42:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
buf.set(b.subarray(off, off + len), count);
|
2014-11-27 05:40:02 +03:00
|
|
|
this.klass.classInfo.getField("I.count.I").set(this, newcount);
|
2014-12-19 00:03:59 +03:00
|
|
|
};
|
2014-10-14 20:42:01 +04:00
|
|
|
|
2014-12-19 00:03:59 +03:00
|
|
|
Override["java/io/ByteArrayOutputStream.write.(I)V"] = function(value) {
|
2014-11-27 05:40:02 +03:00
|
|
|
var count = this.klass.classInfo.getField("I.count.I").get(this);
|
|
|
|
var buf = this.klass.classInfo.getField("I.buf.[B").get(this);
|
2014-09-30 20:49:36 +04:00
|
|
|
|
|
|
|
var newcount = count + 1;
|
|
|
|
if (newcount > buf.length) {
|
2014-11-27 05:35:12 +03:00
|
|
|
var newbuf = J2ME.newByteArray(Math.max(buf.length << 1, newcount));
|
2014-09-30 20:49:36 +04:00
|
|
|
newbuf.set(buf);
|
|
|
|
buf = newbuf;
|
2014-11-27 05:40:02 +03:00
|
|
|
this.klass.classInfo.getField("I.buf.[B").set(this, buf);
|
2014-09-30 20:49:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
buf[count] = value;
|
2014-11-27 05:40:02 +03:00
|
|
|
this.klass.classInfo.getField("I.count.I").set(this, newcount);
|
2014-12-19 00:03:59 +03:00
|
|
|
};
|
2014-09-26 05:14:38 +04:00
|
|
|
|
2014-12-19 00:03:59 +03:00
|
|
|
Override["java/io/ByteArrayInputStream.<init>.([B)V"] = function(buf) {
|
2014-09-26 05:14:38 +04:00
|
|
|
if (!buf) {
|
2014-12-18 05:12:21 +03:00
|
|
|
throw $.newNullPointerException();
|
2014-09-26 05:14:38 +04:00
|
|
|
}
|
|
|
|
|
2014-10-14 21:29:34 +04:00
|
|
|
this.buf = buf;
|
|
|
|
this.pos = this.mark = 0;
|
|
|
|
this.count = buf.length;
|
2014-12-19 00:03:59 +03:00
|
|
|
};
|
2014-09-26 05:14:38 +04:00
|
|
|
|
2014-12-19 00:03:59 +03:00
|
|
|
Override["java/io/ByteArrayInputStream.<init>.([BII)V"] = function(buf, offset, length) {
|
2014-09-26 05:14:38 +04:00
|
|
|
if (!buf) {
|
2014-12-18 05:12:21 +03:00
|
|
|
throw $.newNullPointerException();
|
2014-09-26 05:14:38 +04:00
|
|
|
}
|
|
|
|
|
2014-10-14 21:29:34 +04:00
|
|
|
this.buf = buf;
|
|
|
|
this.pos = this.mark = offset;
|
|
|
|
this.count = (offset + length <= buf.length) ? (offset + length) : buf.length;
|
2014-12-19 00:03:59 +03:00
|
|
|
};
|
2014-09-26 05:14:38 +04:00
|
|
|
|
2014-12-19 00:03:59 +03:00
|
|
|
Override["java/io/ByteArrayInputStream.read.()I"] = function() {
|
2014-10-14 21:29:34 +04:00
|
|
|
return (this.pos < this.count) ? (this.buf[this.pos++] & 0xFF) : -1;
|
2014-12-19 00:03:59 +03:00
|
|
|
};
|
2014-09-26 05:14:38 +04:00
|
|
|
|
2014-12-19 00:03:59 +03:00
|
|
|
Override["java/io/ByteArrayInputStream.read.([BII)I"] = function(b, off, len) {
|
2014-09-26 05:14:38 +04:00
|
|
|
if (!b) {
|
2014-12-18 05:12:21 +03:00
|
|
|
throw $.newNullPointerException();
|
2014-09-27 07:58:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((off < 0) || (off > b.length) || (len < 0) ||
|
|
|
|
((off + len) > b.length)) {
|
2014-12-18 05:12:21 +03:00
|
|
|
throw $.newIndexOutOfBoundsException();
|
2014-09-26 05:14:38 +04:00
|
|
|
}
|
|
|
|
|
2014-10-14 21:29:34 +04:00
|
|
|
if (this.pos >= this.count) {
|
|
|
|
return -1;
|
2014-09-26 05:14:38 +04:00
|
|
|
}
|
2014-10-14 21:29:34 +04:00
|
|
|
if (this.pos + len > this.count) {
|
|
|
|
len = this.count - this.pos;
|
2014-09-26 05:14:38 +04:00
|
|
|
}
|
2014-09-27 07:58:43 +04:00
|
|
|
if (len === 0) {
|
2014-10-14 21:29:34 +04:00
|
|
|
return 0;
|
2014-09-26 05:14:38 +04:00
|
|
|
}
|
|
|
|
|
2014-10-14 21:29:34 +04:00
|
|
|
b.set(this.buf.subarray(this.pos, this.pos + len), off);
|
2014-09-26 05:14:38 +04:00
|
|
|
|
2014-10-14 21:29:34 +04:00
|
|
|
this.pos += len;
|
|
|
|
return len;
|
2014-12-19 00:03:59 +03:00
|
|
|
};
|
2014-09-26 05:14:38 +04:00
|
|
|
|
2014-12-19 00:03:59 +03:00
|
|
|
Override["java/io/ByteArrayInputStream.skip.(J)J"] = function(long) {
|
2014-10-14 21:29:34 +04:00
|
|
|
var n = long.toNumber();
|
2014-09-26 04:06:43 +04:00
|
|
|
|
2014-10-14 21:29:34 +04:00
|
|
|
if (this.pos + n > this.count) {
|
|
|
|
n = this.count - this.pos;
|
2014-09-26 04:06:43 +04:00
|
|
|
}
|
2014-09-26 05:14:38 +04:00
|
|
|
|
|
|
|
if (n < 0) {
|
2014-10-14 21:29:34 +04:00
|
|
|
return Long.fromNumber(0);
|
2014-09-26 05:14:38 +04:00
|
|
|
}
|
|
|
|
|
2014-10-14 21:29:34 +04:00
|
|
|
this.pos += n;
|
2014-09-26 05:14:38 +04:00
|
|
|
|
2014-10-14 21:29:34 +04:00
|
|
|
return Long.fromNumber(n);
|
2014-12-19 00:03:59 +03:00
|
|
|
};
|
2014-09-26 05:14:38 +04:00
|
|
|
|
2014-12-19 00:03:59 +03:00
|
|
|
Override["java/io/ByteArrayInputStream.available.()I"] = function() {
|
2014-10-14 21:29:34 +04:00
|
|
|
return this.count - this.pos;
|
2014-12-19 00:03:59 +03:00
|
|
|
};
|
2014-09-26 05:14:38 +04:00
|
|
|
|
2014-12-19 00:03:59 +03:00
|
|
|
Override["java/io/ByteArrayInputStream.mark.(I)V"] = function(readAheadLimit) {
|
2014-10-14 21:29:34 +04:00
|
|
|
this.mark = this.pos;
|
2014-12-19 00:03:59 +03:00
|
|
|
};
|
2014-09-26 05:14:38 +04:00
|
|
|
|
2014-12-19 00:03:59 +03:00
|
|
|
Override["java/io/ByteArrayInputStream.reset.()V"] = function() {
|
2014-10-14 21:29:34 +04:00
|
|
|
this.pos = this.mark;
|
2014-12-19 00:03:59 +03:00
|
|
|
};
|
2014-09-26 21:57:15 +04:00
|
|
|
|
2014-10-01 06:41:50 +04:00
|
|
|
// The following Permissions methods are overriden to avoid expensive calls to
|
|
|
|
// DomainPolicy.loadValues. This has the added benefit that we avoid many other
|
|
|
|
// computations.
|
|
|
|
|
2014-12-19 00:03:59 +03:00
|
|
|
Override["com/sun/midp/security/Permissions.forDomain.(Ljava/lang/String;)[[B"] = function(name) {
|
2014-09-30 22:13:40 +04:00
|
|
|
// NUMBER_OF_PERMISSIONS = PermissionsStrings.PERMISSION_STRINGS.length + 2
|
2014-10-01 06:29:25 +04:00
|
|
|
// The 2 is the two hardcoded MIPS and AMS permissions.
|
2014-09-30 22:13:40 +04:00
|
|
|
var NUMBER_OF_PERMISSIONS = 61;
|
|
|
|
var ALLOW = 1;
|
|
|
|
|
2014-11-27 05:35:12 +03:00
|
|
|
var maximums = J2ME.newByteArray(NUMBER_OF_PERMISSIONS);
|
|
|
|
var defaults = J2ME.newByteArray(NUMBER_OF_PERMISSIONS);
|
2014-09-30 22:13:40 +04:00
|
|
|
|
|
|
|
for (var i = 0; i < NUMBER_OF_PERMISSIONS; i++) {
|
2014-10-01 06:30:09 +04:00
|
|
|
maximums[i] = defaults[i] = ALLOW;
|
2014-09-30 22:13:40 +04:00
|
|
|
}
|
|
|
|
|
2014-12-01 06:49:39 +03:00
|
|
|
var permissions = J2ME.newArray(J2ME.PrimitiveArrayClassInfo.B.klass, 2);
|
2014-10-01 06:30:09 +04:00
|
|
|
permissions[0] = maximums;
|
2014-10-01 06:41:50 +04:00
|
|
|
permissions[1] = defaults;
|
2014-09-30 22:13:40 +04:00
|
|
|
|
2014-10-14 21:29:34 +04:00
|
|
|
return permissions;
|
2014-12-19 00:03:59 +03:00
|
|
|
};
|
2014-09-30 22:13:40 +04:00
|
|
|
|
2014-10-01 06:41:50 +04:00
|
|
|
// Always return true to make Java think the MIDlet domain is trusted.
|
2014-12-19 00:03:59 +03:00
|
|
|
Override["com/sun/midp/security/Permissions.isTrusted.(Ljava/lang/String;)Z"] = function(name) {
|
|
|
|
return 1;
|
|
|
|
};
|
2014-09-30 22:13:40 +04:00
|
|
|
|
2014-10-01 06:41:50 +04:00
|
|
|
// Returns the ID of the permission. The callers will use this ID to check the
|
|
|
|
// permission in the permissions array returned by Permissions::forDomain.
|
2014-12-19 00:03:59 +03:00
|
|
|
Override["com/sun/midp/security/Permissions.getId.(Ljava/lang/String;)I"] = function(name) {
|
2014-10-14 21:29:34 +04:00
|
|
|
return 0;
|
2014-12-19 00:03:59 +03:00
|
|
|
};
|
2014-09-30 22:13:40 +04:00
|
|
|
|
2014-10-01 06:41:50 +04:00
|
|
|
// 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.
|
2014-12-19 00:03:59 +03:00
|
|
|
Override["com/sun/midp/security/Permissions.getName.(I)Ljava/lang/String;"] = function(id) {
|
2014-12-23 23:17:16 +03:00
|
|
|
return J2ME.newString("com.sun.midp");
|
2014-12-19 00:03:59 +03:00
|
|
|
};
|
2015-01-06 17:46:05 +03:00
|
|
|
|
2015-01-09 20:37:38 +03:00
|
|
|
Override["com/sun/cldc/i18n/uclc/DefaultCaseConverter.toLowerCase.(C)C"] = function(char) {
|
2015-01-06 17:46:05 +03:00
|
|
|
return String.fromCharCode(char).toLowerCase().charCodeAt(0);
|
2015-01-09 20:37:38 +03:00
|
|
|
};
|
2015-01-06 17:46:05 +03:00
|
|
|
|
2015-01-09 20:37:38 +03:00
|
|
|
Override["com/sun/cldc/i18n/uclc/DefaultCaseConverter.toUpperCase.(C)C"] = function(char) {
|
2015-01-06 17:46:05 +03:00
|
|
|
return String.fromCharCode(char).toUpperCase().charCodeAt(0);
|
2015-01-09 20:37:38 +03:00
|
|
|
};
|