pluotsorbet/util.js

121 строка
2.9 KiB
JavaScript
Исходник Обычный вид История

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
}
function defaultValue(type) {
if (type === 'J')
2014-07-17 01:13:18 +04:00
return Long.ZERO;
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
}
function fromJavaChars(chars, offset, count) {
if (typeof count !== 'number')
count = chars.length;
if (typeof offset !== 'number')
offset = 0;
return String.fromCharCode.apply(null, chars.subarray(offset, offset + count));
}
2014-07-19 06:23:00 +04:00
function fromJavaString(str) {
2014-08-01 19:53:35 +04:00
if (!str)
return null;
2014-08-02 09:47:26 +04:00
var chars = CLASSES.java_lang_String.getField("value", "[C").get(str);
var offset = CLASSES.java_lang_String.getField("offset", "I").get(str);
var count = CLASSES.java_lang_String.getField("count", "I").get(str);
return fromJavaChars(chars, offset, count);
2014-07-15 09:13:01 +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-07-06 12:29:36 +04:00
return {
2014-07-14 04:02:57 +04:00
INT_MAX: INT_MAX,
INT_MIN: INT_MIN,
decodeUtf8: decodeUtf8,
2014-07-12 07:20:41 +04:00
defaultValue: defaultValue,
double2int: double2int,
double2long: double2long,
fromJavaChars: fromJavaChars,
2014-07-18 09:00:32 +04:00
fromJavaString: fromJavaString,
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-07-06 12:29:36 +04:00
};
})();