2014-07-06 12:29:36 +04:00
|
|
|
/* -*- 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 util = (function () {
|
|
|
|
var Utf8TextDecoder;
|
|
|
|
|
|
|
|
function decodeUtf8(arrayBuffer) {
|
|
|
|
if (!Utf8TextDecoder) {
|
2014-08-05 06:54:58 +04:00
|
|
|
Utf8TextDecoder = new TextDecoder("utf-8");
|
2014-07-06 12:29:36 +04:00
|
|
|
}
|
2014-07-29 06:13:32 +04:00
|
|
|
return Utf8TextDecoder.decode(new Uint8Array(arrayBuffer));
|
2014-07-06 12:29:36 +04:00
|
|
|
}
|
|
|
|
|
2014-10-28 23:09:30 +03:00
|
|
|
/**
|
|
|
|
* Provides a UTF-8 decoder that will throw an exception on error rather
|
|
|
|
* than silently sanitizing the output.
|
|
|
|
*/
|
|
|
|
var fallibleUtf8Decoder = new TextDecoder("utf-8", { fatal: true });
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decodes a UTF-8 string stored in an ArrayBufferView.
|
|
|
|
*
|
|
|
|
* @param arr An ArrayBufferView to decode (such as a Uint8Array).
|
|
|
|
* @returns The decoded string.
|
|
|
|
* @throws An invalid enoding is encountered, see
|
|
|
|
* TextDecoder.prototype.decode().
|
|
|
|
*/
|
|
|
|
function decodeUtf8Array(arr) {
|
|
|
|
return fallibleUtf8Decoder.decode(arr);
|
|
|
|
}
|
|
|
|
|
2014-11-14 00:24:59 +03:00
|
|
|
// Decode Java's modified UTF-8 (JVM specs, $ 4.4.7)
|
|
|
|
// http://docs.oracle.com/javase/specs/jvms/se5.0/html/ClassFile.doc.html#7963
|
|
|
|
function javaUTF8Decode(arr) {
|
|
|
|
var str = '';
|
|
|
|
var i = 0;
|
|
|
|
while (i < arr.length) {
|
|
|
|
var x = arr[i++];
|
|
|
|
|
|
|
|
if (x <= 0x7f) {
|
|
|
|
// Code points in the range '\u0001' to '\u007F' are represented by a
|
|
|
|
// single byte.
|
|
|
|
// The 7 bits of data in the byte give the value of the code point
|
|
|
|
// represented.
|
|
|
|
str += String.fromCharCode(x);
|
|
|
|
} else if (x <= 0xdf) {
|
|
|
|
// The null code point ('\u0000') and code points in the range '\u0080'
|
|
|
|
// to '\u07FF' are represented by a pair of bytes x and y.
|
|
|
|
var y = arr[i++];
|
|
|
|
str += String.fromCharCode(((x & 0x1f) << 6) + (y & 0x3f));
|
|
|
|
} else {
|
|
|
|
// Code points in the range '\u0800' to '\uFFFF' are represented by 3
|
|
|
|
// bytes x, y, and z.
|
|
|
|
var y = arr[i++];
|
|
|
|
var z = arr[i++];
|
|
|
|
str += String.fromCharCode(((x & 0xf) << 12) + ((y & 0x3f) << 6) + (z & 0x3f));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2014-07-11 10:21:12 +04:00
|
|
|
function defaultValue(type) {
|
|
|
|
if (type === 'J')
|
2014-07-17 01:13:18 +04:00
|
|
|
return Long.ZERO;
|
2014-07-11 10:21:12 +04:00
|
|
|
if (type[0] === '[' || type[0] === 'L')
|
|
|
|
return null;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-12 07:20:41 +04:00
|
|
|
var INT_MAX = Math.pow(2, 31) - 1;
|
|
|
|
var INT_MIN = -INT_MAX - 1;
|
|
|
|
|
|
|
|
function double2int(d) {
|
2014-07-19 05:23:49 +04:00
|
|
|
if (d > INT_MAX)
|
2014-07-12 07:20:41 +04:00
|
|
|
return INT_MAX;
|
2014-07-19 05:23:49 +04:00
|
|
|
if (d < INT_MIN)
|
2014-07-12 07:20:41 +04:00
|
|
|
return INT_MIN;
|
2014-07-19 05:23:49 +04:00
|
|
|
return d|0;
|
2014-07-12 07:20:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function double2long(d) {
|
|
|
|
if (d === Number.POSITIVE_INFINITY)
|
2014-07-17 01:13:18 +04:00
|
|
|
return Long.MAX_VALUE;
|
2014-07-12 07:20:41 +04:00
|
|
|
if (d === Number.NEGATIVE_INFINITY)
|
2014-07-17 01:13:18 +04:00
|
|
|
return Long.MIN_VALUE;
|
|
|
|
return Long.fromNumber(d);
|
2014-07-12 07:20:41 +04:00
|
|
|
}
|
|
|
|
|
2014-09-29 21:04:09 +04:00
|
|
|
var jStringEncoder = new TextEncoder('utf-16');
|
|
|
|
var jStringDecoder = new TextDecoder('utf-16');
|
|
|
|
|
2014-08-01 20:52:24 +04:00
|
|
|
function fromJavaChars(chars, offset, count) {
|
2014-09-26 21:57:15 +04:00
|
|
|
if (!chars) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-09-05 04:06:48 +04:00
|
|
|
if (typeof count !== 'number')
|
2014-08-01 20:52:24 +04:00
|
|
|
count = chars.length;
|
2014-09-05 04:06:48 +04:00
|
|
|
if (typeof offset !== 'number')
|
2014-08-01 20:52:24 +04:00
|
|
|
offset = 0;
|
2014-09-29 21:04:09 +04:00
|
|
|
return jStringDecoder.decode(chars.subarray(offset, offset + count));
|
2014-08-01 20:52:24 +04:00
|
|
|
}
|
|
|
|
|
2014-09-26 21:57:15 +04:00
|
|
|
function fromJavaString(jStr) {
|
2014-11-26 12:21:11 +03:00
|
|
|
return J2ME.fromJavaString(jStr);
|
2014-07-15 09:13:01 +04:00
|
|
|
}
|
|
|
|
|
2014-11-26 12:21:11 +03:00
|
|
|
function newMultiArray(classInfo, lengths) {
|
2014-10-27 15:04:14 +03:00
|
|
|
var length = lengths[0];
|
2015-01-28 01:59:21 +03:00
|
|
|
var array = J2ME.newArray(classInfo.elementClass.klass, length);
|
2014-10-27 15:04:14 +03:00
|
|
|
if (lengths.length > 1) {
|
|
|
|
lengths = lengths.slice(1);
|
|
|
|
for (var i=0; i<length; i++)
|
2014-11-26 12:21:11 +03:00
|
|
|
array[i] = newMultiArray(classInfo.elementClass, lengths);
|
2014-10-27 15:04:14 +03:00
|
|
|
}
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2014-09-24 23:52:39 +04:00
|
|
|
/**
|
|
|
|
* Returns an ArrayBufferView of the underlying code points
|
|
|
|
* represented by the given Java string.
|
|
|
|
*
|
|
|
|
* NOTE: Do not modify the ArrayBuffer; it may be shared between
|
|
|
|
* multiple string instances.
|
|
|
|
*/
|
2014-09-30 21:00:20 +04:00
|
|
|
function stringToCharArray(str) {
|
|
|
|
return new Uint16Array(jStringEncoder.encode(str).buffer);
|
2014-09-24 23:52:39 +04:00
|
|
|
}
|
|
|
|
|
2014-07-28 11:53:37 +04:00
|
|
|
var id = (function() {
|
|
|
|
var gen = 0;
|
|
|
|
return function() {
|
|
|
|
return ++gen;
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
|
|
function tag(obj) {
|
|
|
|
if (!obj.tag)
|
|
|
|
obj.tag = id();
|
|
|
|
return obj.tag;
|
|
|
|
}
|
|
|
|
|
2014-09-03 09:31:28 +04:00
|
|
|
/**
|
|
|
|
* Compare two typed arrays, returning *true* if they have the same length
|
|
|
|
* and values, *false* otherwise. Note that we compare by value, not by byte,
|
|
|
|
* so:
|
|
|
|
* compareTypedArrays(new Uint8Array([0x00, 0xFF]), new Uint8Array[0x00, 0xFF])
|
|
|
|
* returns *true*;
|
|
|
|
*
|
|
|
|
* and:
|
|
|
|
* compareTypedArrays(new Uint8Array([0x00, 0xFF]), new Uint32Array[0x00000000, 0x000000FF])
|
|
|
|
* also returns *true*;
|
|
|
|
*
|
|
|
|
* but:
|
|
|
|
* compareTypedArrays(new Uint8Array([0x00, 0xFF]), new Uint16Array([0x00FF]))
|
|
|
|
* returns *false*.
|
|
|
|
*/
|
|
|
|
function compareTypedArrays(ary1, ary2) {
|
|
|
|
if (ary1.length != ary2.length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < ary1.length; i++) {
|
|
|
|
if (ary1[i] !== ary2[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2014-09-03 10:19:21 +04:00
|
|
|
}
|
2014-09-03 09:31:28 +04:00
|
|
|
|
2014-09-06 03:06:38 +04:00
|
|
|
function pad(num, len) {
|
|
|
|
return "0".repeat(len - num.toString().length) + num;
|
|
|
|
}
|
|
|
|
|
2014-12-14 21:01:24 +03:00
|
|
|
function toCodePointArray(str) {
|
|
|
|
var chars = [];
|
|
|
|
|
|
|
|
var str = str.slice();
|
|
|
|
|
|
|
|
while (str.length > 0) {
|
|
|
|
var ucsChars = String.fromCodePoint(str.codePointAt(0));
|
|
|
|
chars.push(ucsChars);
|
|
|
|
str = str.substr(ucsChars.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
return chars;
|
|
|
|
}
|
|
|
|
|
2015-01-27 11:45:59 +03:00
|
|
|
function abgrIntToCSS(pixel) {
|
|
|
|
var a = (pixel >> 24) & 0xff;
|
|
|
|
var b = (pixel >> 16) & 0xff;
|
|
|
|
var g = (pixel >> 8) & 0xff;
|
|
|
|
var r = pixel & 0xff;
|
|
|
|
return "rgba(" + r + "," + g + "," + b + "," + (a/255) + ")";
|
|
|
|
}
|
|
|
|
|
2014-07-06 12:29:36 +04:00
|
|
|
return {
|
2014-07-14 04:02:57 +04:00
|
|
|
INT_MAX: INT_MAX,
|
|
|
|
INT_MIN: INT_MIN,
|
2014-07-11 10:21:12 +04:00
|
|
|
decodeUtf8: decodeUtf8,
|
2014-11-19 21:22:08 +03:00
|
|
|
decodeUtf8Array: decodeUtf8Array,
|
2014-11-14 00:24:59 +03:00
|
|
|
javaUTF8Decode: javaUTF8Decode,
|
2014-07-12 07:20:41 +04:00
|
|
|
defaultValue: defaultValue,
|
|
|
|
double2int: double2int,
|
|
|
|
double2long: double2long,
|
2014-08-01 20:52:24 +04:00
|
|
|
fromJavaChars: fromJavaChars,
|
2014-07-18 09:00:32 +04:00
|
|
|
fromJavaString: fromJavaString,
|
2014-10-27 15:04:14 +03:00
|
|
|
newMultiArray: newMultiArray,
|
2014-09-30 21:00:20 +04:00
|
|
|
stringToCharArray: stringToCharArray,
|
2014-07-28 11:53:37 +04:00
|
|
|
id: id,
|
|
|
|
tag: tag,
|
2014-09-03 10:19:21 +04:00
|
|
|
compareTypedArrays: compareTypedArrays,
|
2014-09-06 03:06:38 +04:00
|
|
|
pad: pad,
|
2014-12-14 21:01:24 +03:00
|
|
|
toCodePointArray: toCodePointArray,
|
2015-01-27 11:45:59 +03:00
|
|
|
abgrIntToCSS: abgrIntToCSS,
|
2014-07-06 12:29:36 +04:00
|
|
|
};
|
|
|
|
})();
|