This repository has been archived by the owner on Dec 8, 2022. It is now read-only.
forked from 280north/narwhal
-
Notifications
You must be signed in to change notification settings - Fork 16
/
binary-engine.js
83 lines (67 loc) · 2.64 KB
/
binary-engine.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
exports.B_LENGTH = function(bytes) {
return bytes.length;
}
exports.B_ALLOC = function(length) {
return Packages.java.lang.reflect.Array.newInstance(Packages.java.lang.Byte.TYPE, length);
}
exports.B_FILL = function(bytes, length, offset, value) {
Packages.java.util.Arrays.fill(bytes, length, offset, value);
}
exports.B_COPY = function(src, srcOffset, dst, dstOffset, length) {
Packages.java.lang.System.arraycopy(src, srcOffset, dst, dstOffset, length);
}
exports.B_GET = function(bytes, index) {
return (bytes[index] >>> 0) & 0xFF;
}
exports.B_SET = function(bytes, index, value) {
return bytes[index] = ((value & 0x80) ? -1 - (value ^ 0xFF) : value);
}
exports.B_DECODE = function(bytes, offset, length, codec) {
return String(new Packages.java.lang.String(bytes, offset, length, codec));
}
exports.B_DECODE_DEFAULT = function(bytes, offset, length) {
return String(new Packages.java.lang.String(bytes, offset, length));
}
exports.B_ENCODE = function(string, codec) {
return new Packages.java.lang.String(string).getBytes(codec);
}
exports.B_ENCODE_DEFAULT = function(string) {
return new Packages.java.lang.String(string).getBytes();
}
exports.B_TRANSCODE = function(bytes, offset, length, sourceCodec, targetCodec) {
return new Packages.java.lang.String(bytes, offset, length, sourceCodec).getBytes(targetCodec);
}
function wrapper(that) {
var obj = new JavaAdapter(
Packages.org.mozilla.javascript.ScriptableObject,
Packages.org.mozilla.javascript.Wrapper,
{
get : function(index, start) {
if (typeof index === "number")
return that.get(index);
return that[index];
},
has : function(index, start) {
if (typeof index === "number")
return index < that._length;
return (that[index] !== undefined)
},
put : function(index, start, value) {
if (typeof index === "number")
that.set(index, value);
else
that[index] = value;
},
unwrap : function() {
var bytes = Packages.java.lang.reflect.Array.newInstance(Packages.java.lang.Byte.TYPE, that._length);
Packages.java.lang.System.arraycopy(that._bytes, that._offset, bytes, 0, that._length);
return bytes;
}
}
);
obj.__proto__ = that;
return obj;
}
// FIXME: disabling these because it broke several tests. figure out why.
//exports.ByteStringWrapper = wrapper;
//exports.ByteArrayWrapper = wrapper;