forked from mbebenita/j2me.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.js
221 lines (192 loc) · 5.8 KB
/
util.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
/* -*- 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(new Uint8Array(arrayBuffer));
}
/**
* 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);
}
// 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;
}
function defaultValue(type) {
if (type === 'J')
return Long.ZERO;
if (type[0] === '[' || type[0] === 'L')
return null;
return 0;
}
var INT_MAX = Math.pow(2, 31) - 1;
var INT_MIN = -INT_MAX - 1;
function double2int(d) {
if (d > INT_MAX)
return INT_MAX;
if (d < INT_MIN)
return INT_MIN;
return d|0;
}
function double2long(d) {
if (d === Number.POSITIVE_INFINITY)
return Long.MAX_VALUE;
if (d === Number.NEGATIVE_INFINITY)
return Long.MIN_VALUE;
return Long.fromNumber(d);
}
var jStringEncoder = new TextEncoder('utf-16');
var jStringDecoder = new TextDecoder('utf-16');
function fromJavaChars(chars, offset, count) {
if (!chars) {
return null;
}
if (typeof count !== 'number')
count = chars.length;
if (typeof offset !== 'number')
offset = 0;
return jStringDecoder.decode(chars.subarray(offset, offset + count));
}
function fromJavaString(jStr) {
return J2ME.fromJavaString(jStr);
}
function newPrimitiveArray(type, size) {
// !!! ask mbx should this be using newArray instead?
var constructor = J2ME.getArrayConstructor(type);
return new constructor(size);
}
// Remove
function newArray(classInfo, size) {
return J2ME.newArray(classInfo.klass, size);
}
function newMultiArray(classInfo, lengths) {
var length = lengths[0];
var array = newArray(classInfo.elementClass, length);
if (lengths.length > 1) {
lengths = lengths.slice(1);
for (var i=0; i<length; i++)
array[i] = newMultiArray(classInfo.elementClass, lengths);
}
return array;
}
// Remove
function newObject(classInfo) {
return J2ME.newObject(classInfo.klass);
}
function newString(s) {
return J2ME.newString(s);
}
/**
* 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.
*/
function stringToCharArray(str) {
return new Uint16Array(jStringEncoder.encode(str).buffer);
}
var id = (function() {
var gen = 0;
return function() {
return ++gen;
}
})();
function tag(obj) {
if (!obj.tag)
obj.tag = id();
return obj.tag;
}
/**
* 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;
}
function pad(num, len) {
return "0".repeat(len - num.toString().length) + num;
}
return {
INT_MAX: INT_MAX,
INT_MIN: INT_MIN,
decodeUtf8: decodeUtf8,
decodeUtf8Array: decodeUtf8Array,
javaUTF8Decode: javaUTF8Decode,
defaultValue: defaultValue,
double2int: double2int,
double2long: double2long,
fromJavaChars: fromJavaChars,
fromJavaString: fromJavaString,
newPrimitiveArray: newPrimitiveArray,
newArray: newArray,
newMultiArray: newMultiArray,
newObject: newObject,
newString: newString,
stringToCharArray: stringToCharArray,
id: id,
tag: tag,
compareTypedArrays: compareTypedArrays,
pad: pad,
};
})();