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) {
|
|
|
|
Utf8TextDecoder = new TextDecoder("utf-8");
|
|
|
|
}
|
|
|
|
return Utf8TextDecoder.decode(Uint8Array(arrayBuffer));
|
|
|
|
}
|
|
|
|
|
2014-07-06 23:32:54 +04:00
|
|
|
var out = "";
|
|
|
|
function print(s) {
|
|
|
|
out += s;
|
|
|
|
var n;
|
|
|
|
while ((n = out.indexOf("\n")) != -1) {
|
|
|
|
console.log(out.substr(0, n));
|
|
|
|
out = out.substr(n+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-17 07:53:54 +04:00
|
|
|
var floatmem = Float32Array(1);
|
|
|
|
|
2014-07-12 07:20:41 +04:00
|
|
|
function double2float(d) {
|
2014-07-17 07:53:54 +04:00
|
|
|
floatmem[0] = d;
|
|
|
|
return floatmem[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-07-19 06:23:00 +04:00
|
|
|
function fromJavaString(str) {
|
|
|
|
var chars = str.value;
|
|
|
|
var offset = str.offset;
|
|
|
|
var count = str.count;
|
2014-07-15 09:13:01 +04:00
|
|
|
var s = "";
|
2014-07-15 10:37:33 +04:00
|
|
|
if (!count)
|
|
|
|
count = chars.length;
|
2014-07-19 06:23:00 +04:00
|
|
|
for (var n = offset; n < count; ++n)
|
2014-07-15 09:13:01 +04:00
|
|
|
s += String.fromCharCode(chars[n]);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2014-07-18 10:02:28 +04:00
|
|
|
function cache(obj, name, fn) {
|
|
|
|
name = "cache$" + name;
|
|
|
|
var result = obj[name];
|
|
|
|
return result ? result : obj[name] = fn();
|
|
|
|
}
|
|
|
|
|
2014-07-19 05:00:10 +04:00
|
|
|
function withPath(base, fileName) {
|
|
|
|
var i = base.lastIndexOf("/");
|
|
|
|
if (i === -1)
|
|
|
|
return fileName;
|
|
|
|
console.log(base.substr(0, i) + fileName);
|
|
|
|
return base.substr(0, i) + fileName;
|
|
|
|
}
|
|
|
|
|
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-06 23:32:54 +04:00
|
|
|
print: print,
|
2014-07-06 12:29:36 +04:00
|
|
|
debug: console.info.bind(console),
|
|
|
|
error: console.error.bind(console),
|
|
|
|
info: console.info.bind(console),
|
|
|
|
warn: console.warn.bind(console),
|
2014-07-11 10:21:12 +04:00
|
|
|
decodeUtf8: decodeUtf8,
|
2014-07-12 07:20:41 +04:00
|
|
|
defaultValue: defaultValue,
|
|
|
|
double2float: double2float,
|
|
|
|
double2int: double2int,
|
|
|
|
double2long: double2long,
|
2014-07-18 09:00:32 +04:00
|
|
|
fromJavaString: fromJavaString,
|
2014-07-19 05:00:10 +04:00
|
|
|
cache: cache,
|
|
|
|
withPath: withPath
|
2014-07-06 12:29:36 +04:00
|
|
|
};
|
|
|
|
})();
|