divine 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE.txt +2 -1
- data/Rakefile +49 -0
- data/lib/divine/code_generators/code_generator.rb +29 -6
- data/lib/divine/code_generators/java.rb +121 -14
- data/lib/divine/code_generators/javascript.rb +100 -11
- data/lib/divine/code_generators/ruby.rb +89 -4
- data/lib/divine/version.rb +1 -1
- data/test/basic_complex_test/basic_complex_test.rb +34 -0
- data/test/basic_complex_test/java_test/JavaTest.java +96 -0
- data/test/basic_complex_test/java_test/test_babel.java +368 -0
- data/test/basic_complex_test/js_test/js_testBasic.js +58 -0
- data/test/basic_complex_test/js_test/js_testComplex.js +68 -0
- data/test/basic_complex_test/js_test/test_babel.js +523 -0
- data/test/basic_complex_test/ruby_test/ruby_test.rb +68 -0
- data/test/basic_complex_test/ruby_test/test_babel.rb +368 -0
- data/test/binaryTree_test/binaryTree_test.rb +19 -0
- data/test/binaryTree_test/java_test/JavaTest.java +114 -0
- data/test/binaryTree_test/java_test/test_binaryTree.java +301 -0
- data/test/binaryTree_test/js_test/js_test.js +76 -0
- data/test/binaryTree_test/js_test/test_binaryTree.js +447 -0
- data/test/binaryTree_test/ruby_test/ruby_test.rb +68 -0
- data/test/binaryTree_test/ruby_test/test_binaryTree.rb +303 -0
- data/test/complex_test/complex_test.rb +23 -0
- data/test/complex_test/java_test/JavaTest.java +126 -0
- data/test/complex_test/java_test/test_complex.java +331 -0
- data/test/complex_test/js_test/js_test.js +69 -0
- data/test/complex_test/js_test/test_complex.js +478 -0
- data/test/complex_test/ruby_test/ruby_test.rb +55 -0
- data/test/complex_test/ruby_test/test_complex.rb +330 -0
- data/test/ipv6_test/ipv6_test.rb +14 -0
- data/test/ipv6_test/java_test/JavaTest.java +77 -0
- data/test/ipv6_test/java_test/junit.jar +0 -0
- data/test/ipv6_test/java_test/test_ipv6.java +270 -0
- data/test/ipv6_test/js_test/js_test.js +60 -0
- data/test/ipv6_test/js_test/test_ipv6.js +409 -0
- data/test/ipv6_test/ruby_test/ruby_test.rb +42 -0
- data/test/ipv6_test/ruby_test/test_ipv6.rb +267 -0
- data/test/java_lib/junit.jar +0 -0
- data/test/unify_test/java_test/test_unify.java +171 -0
- data/test/unify_test/js_test/js_test.js +56 -0
- data/test/unify_test/js_test/test_unify.js +326 -0
- data/test/unify_test/ruby_test/ruby_test.rb +35 -0
- data/test/unify_test/ruby_test/test_unify.rb +187 -0
- data/test/unify_test/unify_test.rb +17 -0
- metadata +77 -3
@@ -0,0 +1,58 @@
|
|
1
|
+
eval(require('fs').readFileSync('test/basic_complex_test/js_test/test_babel.js', 'utf8'));
|
2
|
+
var assert = require('assert');
|
3
|
+
var fs = require('fs');
|
4
|
+
|
5
|
+
console.log("Basic Test");
|
6
|
+
|
7
|
+
var testBasic_ser = new TestBasic();
|
8
|
+
testBasic_ser.i8 = 10;
|
9
|
+
testBasic_ser.i16 = 100;
|
10
|
+
testBasic_ser.i32 = 10000;
|
11
|
+
testBasic_ser.str = "Test String";
|
12
|
+
testBasic_ser.guid = [1,15,4];
|
13
|
+
|
14
|
+
var ca = testBasic_ser.serialize();
|
15
|
+
|
16
|
+
serialize(ca);
|
17
|
+
var read = deserialize();
|
18
|
+
|
19
|
+
|
20
|
+
var testBasic_deser = new TestBasic();
|
21
|
+
testBasic_deser.deserialize(new BabelDataReader(read));
|
22
|
+
|
23
|
+
assert.equal(testBasic_ser.i8, testBasic_deser.i8);
|
24
|
+
assert.equal(testBasic_ser.i16, testBasic_deser.i16);
|
25
|
+
assert.equal(testBasic_ser.i32, testBasic_deser.i32);
|
26
|
+
assert.equal(testBasic_ser.str, testBasic_deser.str);
|
27
|
+
compare_list(testBasic_ser.guid, testBasic_deser.guid);
|
28
|
+
|
29
|
+
function compare_list(lst_1, lst_2){
|
30
|
+
assert.equal(lst_1.length, lst_2.length)
|
31
|
+
for (var i = 0; i < lst_1.length; i++){
|
32
|
+
assert.equal(lst_1[i], lst_2[i]);
|
33
|
+
}
|
34
|
+
}
|
35
|
+
|
36
|
+
function serialize(obj){
|
37
|
+
var bBuffer = new Buffer(obj);
|
38
|
+
fs.writeFileSync(__dirname + '/bin.babel.js', bBuffer, function (err) {
|
39
|
+
if (err) {
|
40
|
+
return console.log(err);
|
41
|
+
}
|
42
|
+
});
|
43
|
+
}
|
44
|
+
|
45
|
+
function deserialize(){
|
46
|
+
var data = fs.readFileSync(__dirname + '/bin.babel.js');
|
47
|
+
data = toArray(data);
|
48
|
+
return data;
|
49
|
+
|
50
|
+
}
|
51
|
+
|
52
|
+
function toArray(buffer) {
|
53
|
+
var view = new Uint8Array(buffer.length);
|
54
|
+
for (var i = 0; i < buffer.length; ++i) {
|
55
|
+
view[i] = buffer[i];
|
56
|
+
}
|
57
|
+
return view;
|
58
|
+
}
|
@@ -0,0 +1,68 @@
|
|
1
|
+
eval(require('fs').readFileSync('test/basic_complex_test/js_test/test_babel.js', 'utf8'));
|
2
|
+
var assert = require('assert');
|
3
|
+
var fs = require('fs');
|
4
|
+
|
5
|
+
console.log("Basic Test Part 2");
|
6
|
+
|
7
|
+
var testComplex_ser = new TestComplex();
|
8
|
+
testComplex_ser.list1 = [0, 1, 255, 0x7FFFFFFF, 0x7FFFFFFF+1, 0xFFFFFFFE, 0xFFFFFFFF];
|
9
|
+
testComplex_ser.list2 = [0,1, 15,16, 127,128, 254,255];
|
10
|
+
testComplex_ser.map1[0] = 10;
|
11
|
+
testComplex_ser.map1[10] = 100;
|
12
|
+
testComplex_ser.map2["FooBar"] = [new Entry(), new Entry()];
|
13
|
+
|
14
|
+
var ca = testComplex_ser.serialize();
|
15
|
+
serialize(ca);
|
16
|
+
var read = deserialize();
|
17
|
+
|
18
|
+
var testComplex_deser = new TestComplex();
|
19
|
+
testComplex_deser.deserialize(new BabelDataReader(read));
|
20
|
+
|
21
|
+
compare_list(testComplex_ser.list1, testComplex_deser.list1);
|
22
|
+
compare_list(testComplex_ser.list2, testComplex_deser.list2);
|
23
|
+
compare_map1(testComplex_ser.map1, testComplex_deser.map1);
|
24
|
+
compare_map2(testComplex_ser.map2, testComplex_deser.map2);
|
25
|
+
|
26
|
+
function compare_list(lst_1, lst_2){
|
27
|
+
assert.equal(lst_1.length, lst_2.length)
|
28
|
+
for (var i = 0; i < lst_1.length; i++){
|
29
|
+
assert.equal(lst_1[i], lst_2[i]);
|
30
|
+
}
|
31
|
+
}
|
32
|
+
|
33
|
+
function compare_map1(map_1, map_2){
|
34
|
+
for (var m in map_1) {
|
35
|
+
assert.equal(map_1[m], map_2[m]);
|
36
|
+
}
|
37
|
+
}
|
38
|
+
|
39
|
+
function compare_map2(map_1, map_2){
|
40
|
+
for (var m in map_1) {
|
41
|
+
assert.equal(map_1[m].length, map_2[m].length);
|
42
|
+
}
|
43
|
+
}
|
44
|
+
|
45
|
+
function serialize(obj){
|
46
|
+
var bBuffer = new Buffer(obj);
|
47
|
+
fs.writeFileSync(__dirname + '/bin.babel.js', bBuffer, function (err) {
|
48
|
+
if (err) {
|
49
|
+
return console.log(err);
|
50
|
+
}
|
51
|
+
});
|
52
|
+
}
|
53
|
+
|
54
|
+
function deserialize(){
|
55
|
+
var data = fs.readFileSync(__dirname + '/bin.babel.js');
|
56
|
+
data = toArray(data);
|
57
|
+
return data;
|
58
|
+
|
59
|
+
}
|
60
|
+
|
61
|
+
function toArray(buffer) {
|
62
|
+
var view = new Uint8Array(buffer.length);
|
63
|
+
for (var i = 0; i < buffer.length; ++i) {
|
64
|
+
view[i] = buffer[i];
|
65
|
+
}
|
66
|
+
return view;
|
67
|
+
}
|
68
|
+
|
@@ -0,0 +1,523 @@
|
|
1
|
+
// ------------------------------------------------------------ BabelDataReader
|
2
|
+
function BabelDataReader(data) {
|
3
|
+
this.data = data;
|
4
|
+
this.index = 0;
|
5
|
+
}
|
6
|
+
|
7
|
+
BabelDataReader.prototype.getbyte = function () {
|
8
|
+
return this.data[this.index++];
|
9
|
+
};
|
10
|
+
|
11
|
+
BabelDataReader.prototype.read = function (items) {
|
12
|
+
var from = this.index;
|
13
|
+
this.index += items
|
14
|
+
return this.data.subarray(from, this.index)
|
15
|
+
};
|
16
|
+
|
17
|
+
|
18
|
+
// ------------------------------------------------------------ BabelDataWriter
|
19
|
+
function BabelDataWriter(data) {
|
20
|
+
this.data = data;
|
21
|
+
this.index = 0;
|
22
|
+
this.data = new Uint8Array(4096);
|
23
|
+
}
|
24
|
+
|
25
|
+
BabelDataWriter.prototype._realloc = function (size) {
|
26
|
+
size = size || 4096;
|
27
|
+
var old_data = this.data;
|
28
|
+
this.data = new Uint8Array(Math.max(size, 4096) + this.data.length);
|
29
|
+
this.data.set(old_data, 0);
|
30
|
+
};
|
31
|
+
|
32
|
+
BabelDataWriter.prototype.writeByte = function (a_byte) {
|
33
|
+
if (this.index + 1 >= this.data.length) this._realloc();
|
34
|
+
this.data[this.index++] = a_byte;
|
35
|
+
};
|
36
|
+
|
37
|
+
BabelDataWriter.prototype.write = function (bytes) {
|
38
|
+
if (this.index + bytes.length >= this.data.length) this._realloc(bytes.length);
|
39
|
+
this.data.set(bytes, this.index);
|
40
|
+
this.index += bytes.length;
|
41
|
+
};
|
42
|
+
|
43
|
+
BabelDataWriter.prototype.get_data = function () {
|
44
|
+
return this.data.subarray(0, this.index);
|
45
|
+
};
|
46
|
+
|
47
|
+
|
48
|
+
// ------------------------------------------------------------ BabelHelper
|
49
|
+
function BabelHelper() {}
|
50
|
+
|
51
|
+
BabelHelper.prototype.serialize = function () {
|
52
|
+
var out = new BabelDataWriter();
|
53
|
+
this.serialize_internal(out);
|
54
|
+
return out.get_data();
|
55
|
+
}
|
56
|
+
|
57
|
+
BabelHelper.prototype.read_int8 = function (data) {
|
58
|
+
return data.getbyte();
|
59
|
+
};
|
60
|
+
|
61
|
+
BabelHelper.prototype.read_int16 = function (data) {
|
62
|
+
return (data.getbyte() << 8) | this.read_int8(data);
|
63
|
+
};
|
64
|
+
|
65
|
+
BabelHelper.prototype.read_int24 = function (data) {
|
66
|
+
return (data.getbyte() << 16) | this.read_int16(data);
|
67
|
+
};
|
68
|
+
|
69
|
+
BabelHelper.prototype.read_int32 = function (data) {
|
70
|
+
// return (data.getbyte() << 24) | this.read_int24(data); // See notes about numbers above
|
71
|
+
return (data.getbyte() * (256*256*256)) + this.read_int24(data);
|
72
|
+
};
|
73
|
+
|
74
|
+
BabelHelper.prototype.read_binary = function (data) {
|
75
|
+
return data.read(this.read_int32(data));
|
76
|
+
};
|
77
|
+
|
78
|
+
BabelHelper.prototype.read_short_binary = function (data) {
|
79
|
+
return data.read(this.read_int8(data));
|
80
|
+
};
|
81
|
+
|
82
|
+
BabelHelper.prototype.read_ip_number = function (data) {
|
83
|
+
var ip_array = this.read_short_binary(data);
|
84
|
+
if(ip_array.length == 4){
|
85
|
+
return this.read_ipv4_number(ip_array);
|
86
|
+
}else{
|
87
|
+
return this.read_ipv6_number(ip_array);
|
88
|
+
}
|
89
|
+
};
|
90
|
+
|
91
|
+
BabelHelper.prototype.read_ipv4_number = function (ip_array) {
|
92
|
+
ip = "";
|
93
|
+
for (i = 0, len = ip_array.length; i < len; i++) {
|
94
|
+
b = ip_array[i];
|
95
|
+
if (ip.length > 0) {
|
96
|
+
ip += ".";
|
97
|
+
}
|
98
|
+
ip += b.toString();
|
99
|
+
}
|
100
|
+
return ip;
|
101
|
+
};
|
102
|
+
BabelHelper.prototype.read_ipv6_number = function (ip_array) {
|
103
|
+
var ip = "";
|
104
|
+
var part1, part2;
|
105
|
+
for (i = 0, len = ip_array.length; i < len; i+=2) {
|
106
|
+
part1 = ip_array[i];
|
107
|
+
part2 = ip_array[i+1];
|
108
|
+
ip += part1 == 0? "" : part1.toString(16);
|
109
|
+
ip += (part1 == 0 && part2 == 0)? "" : (part2 < 10 && part1 != 0? "0" + part2.toString(16): part2.toString(16));
|
110
|
+
if (i < ip_array.length-2)
|
111
|
+
ip += ":";
|
112
|
+
}
|
113
|
+
ip = ip.replace(/:{3,}/g, "::");
|
114
|
+
return ip;
|
115
|
+
};
|
116
|
+
|
117
|
+
BabelHelper.prototype.read_string = function (data) {
|
118
|
+
return this.decode_utf8(data.read(this.read_int16(data)))
|
119
|
+
};
|
120
|
+
|
121
|
+
BabelHelper.prototype.write_int8 = function (v, out) {
|
122
|
+
if (v > 0xFF) // Max 255
|
123
|
+
this.raise_error("Too large int8 number: " + v);
|
124
|
+
if(v < 0)
|
125
|
+
this.raise_error("a negative number passed to int8 number: " + v);
|
126
|
+
out.writeByte(v);
|
127
|
+
}
|
128
|
+
|
129
|
+
BabelHelper.prototype.write_int16 = function (v, out) {
|
130
|
+
if (v > 0xFFFF) // Max 65.535
|
131
|
+
this.raise_error("Too large int16 number: " + v);
|
132
|
+
if(v < 0)
|
133
|
+
this.raise_error("a negative number passed to int16 number: " + v);
|
134
|
+
this.write_int8(v >> 8 & 0xFF, out);
|
135
|
+
this.write_int8(v & 0xFF, out);
|
136
|
+
}
|
137
|
+
|
138
|
+
BabelHelper.prototype.write_int24 = function (v, out) {
|
139
|
+
if (v > 0xFFFFFF) // Max 16.777.215
|
140
|
+
this.raise_error("Too large int24 number: " + v);
|
141
|
+
if (v < 0) // In Case added to JavaScript declaration
|
142
|
+
this.raise_error("a negative number passed to int24 number: " + v);
|
143
|
+
this.write_int8(v >> 16 & 0xFF, out);
|
144
|
+
this.write_int16(v & 0xFFFF, out);
|
145
|
+
}
|
146
|
+
|
147
|
+
BabelHelper.prototype.write_int32 = function (v, out) {
|
148
|
+
if (v > 0xFFFFFFFF) // Max 4.294.967.295
|
149
|
+
this.raise_error("Too large int32 number: " + v);
|
150
|
+
if(v < 0)
|
151
|
+
this.raise_error("a negative number passed to int32 number: " + v);
|
152
|
+
this.write_int8(v >> 24 & 0xFF, out);
|
153
|
+
this.write_int24(v & 0xFFFFFF, out);
|
154
|
+
}
|
155
|
+
|
156
|
+
BabelHelper.prototype.write_bool = function (v, out) {
|
157
|
+
this.write_int8(v ? 1 : 0, out)
|
158
|
+
}
|
159
|
+
|
160
|
+
BabelHelper.prototype.write_string = function (v, out) {
|
161
|
+
var s = this.encode_utf8(v);
|
162
|
+
if (s.length > 0xFFFF) this.raise_error("Too large string: " + s.length + " bytes");
|
163
|
+
this.write_int16(s.length, out);
|
164
|
+
out.write(s);
|
165
|
+
}
|
166
|
+
|
167
|
+
BabelHelper.prototype.write_binary = function (v, out) {
|
168
|
+
if ((v instanceof Array) || (v instanceof Uint8Array)) {
|
169
|
+
if (v.length > 0xFFFFFFFF) this.raise_error("Too large binary: " + v.length + " (" + v.constructor.name + ")");
|
170
|
+
this.write_int32(v.length, out)
|
171
|
+
out.write(v);
|
172
|
+
} else if (v.constructor == String) {
|
173
|
+
if (v.length > 0xFFFFFFFF) this.raise_error("Too large binary: " + v.length + " (" + v.constructor.name + ")");
|
174
|
+
this.write_int32(v.length, out)
|
175
|
+
out.write(v);
|
176
|
+
} else if (v == null) {
|
177
|
+
this.raise_error("Unsupported binary 'null'");
|
178
|
+
} else {
|
179
|
+
this.raise_error("Unsupported binary of type '" + v.constructor.name + "'");
|
180
|
+
}
|
181
|
+
}
|
182
|
+
|
183
|
+
BabelHelper.prototype.write_16_binary = function (v, out) {
|
184
|
+
if ((v instanceof Array) || (v instanceof Uint8Array)) {
|
185
|
+
if (v.length > 0xFF) this.raise_error("Too large 16 binary: " + v.length*2 + " (" + v.constructor.name + ")");
|
186
|
+
this.write_int8(v.length * 2, out)
|
187
|
+
for (i = 0, len = v.length; i < len; i++) {
|
188
|
+
this.write_int16(v[i], out);
|
189
|
+
}
|
190
|
+
|
191
|
+
} else if (v == null) {
|
192
|
+
this.raise_error("Unsupported binary 'null'");
|
193
|
+
} else {
|
194
|
+
this.raise_error("Unsupported binary of type '" + v.constructor.name + "'");
|
195
|
+
}
|
196
|
+
}
|
197
|
+
|
198
|
+
BabelHelper.prototype.write_short_binary = function (v, out) {
|
199
|
+
if ((v instanceof Array) || (v instanceof Uint8Array)) {
|
200
|
+
if (v.length > 0xFF) this.raise_error("Too large binary: " + v.length + " (" + v.constructor.name + ")");
|
201
|
+
this.write_int8(v.length, out)
|
202
|
+
out.write(v);
|
203
|
+
} else if (v.constructor == String) {
|
204
|
+
if (v.length > 0xFF) this.raise_error("Too large binary: " + v.length + " (" + v.constructor.name + ")");
|
205
|
+
this.write_int8(v.length, out)
|
206
|
+
out.write(v);
|
207
|
+
} else if (v == null) {
|
208
|
+
this.raise_error("Unsupported binary 'null'");
|
209
|
+
} else {
|
210
|
+
this.raise_error("Unsupported binary of type '" + v.constructor.name + "'");
|
211
|
+
}
|
212
|
+
}
|
213
|
+
|
214
|
+
BabelHelper.prototype.write_ip_number = function (v, out) {
|
215
|
+
if ((v instanceof Array) || (v instanceof Uint8Array)){
|
216
|
+
if(v.length == 4){
|
217
|
+
this.write_ipv4_number(v, out);
|
218
|
+
}else{
|
219
|
+
this.write_ipv6_number(v, out);
|
220
|
+
}
|
221
|
+
}else if(v.constructor == String){
|
222
|
+
if(/:/g.test(v)){
|
223
|
+
this.write_ipv6_number(v, out);
|
224
|
+
}else{
|
225
|
+
this.write_ipv4_number(v, out);
|
226
|
+
}
|
227
|
+
}else{
|
228
|
+
this.raise_error("Unknown IP number '" + v + "'");
|
229
|
+
}
|
230
|
+
}
|
231
|
+
|
232
|
+
BabelHelper.prototype.write_ipv4_number = function (v, out) {
|
233
|
+
if ((v instanceof Array) || (v instanceof Uint8Array)) {
|
234
|
+
if (v.length != 4 && v.length != 0) this.raise_error("Unknown IP v4 number " + v);
|
235
|
+
this.write_short_binary(v, out)
|
236
|
+
} else if (v.constructor == String) {
|
237
|
+
var ss = [];
|
238
|
+
if (v.length > 0) {
|
239
|
+
ss = v.split(".").map(Number);
|
240
|
+
}
|
241
|
+
this.write_ipv4_number(ss, out);
|
242
|
+
} else {
|
243
|
+
this.raise_error("Unknown IP number '" + v + "'");
|
244
|
+
}
|
245
|
+
};
|
246
|
+
|
247
|
+
BabelHelper.prototype.write_ipv6_number = function (v, out) {
|
248
|
+
if ((v instanceof Array) || (v instanceof Uint8Array)) {
|
249
|
+
this.write_16_binary(v, out)
|
250
|
+
} else if (v.constructor == String) {
|
251
|
+
var ss = [];
|
252
|
+
var contains_ipv6_letters = /[0-9a-f]+/gi.test(v);
|
253
|
+
var contains_other_letters = /[^:0-9a-f]+/gi.test(v);
|
254
|
+
if (v.length > 0 && v.split(/:{3,}/g).length == 1 && v.split(/:{2}/g).length <= 2 && !contains_other_letters &&
|
255
|
+
contains_ipv6_letters) {
|
256
|
+
v = v.replace(/ /g, "");
|
257
|
+
ss = v.split(":").map(function (t){
|
258
|
+
if (t.length > 4)
|
259
|
+
new BabelHelper().raise_error("Unknown IP Group number '" + t + "'");
|
260
|
+
if (t.length == 0)
|
261
|
+
t = "0";
|
262
|
+
return parseInt(t, 16);
|
263
|
+
});
|
264
|
+
}
|
265
|
+
if (!contains_other_letters &&
|
266
|
+
( !/::/g.test(v) && ss.length == 0 || ss.length == 8) || ( /::/g.test(v) && ss.length > 2 && ss.length < 8) ) {
|
267
|
+
this.write_ipv6_number(ss, out);
|
268
|
+
} else {
|
269
|
+
this.raise_error("Unknown IP v6 number '" + v + "'");
|
270
|
+
}
|
271
|
+
|
272
|
+
} else {
|
273
|
+
this.raise_error("Unknown IP v6 number '" + v + "'");
|
274
|
+
}
|
275
|
+
}
|
276
|
+
|
277
|
+
BabelHelper.prototype.encode_utf8 = function (str) {
|
278
|
+
var utf8 = [];
|
279
|
+
var chr, next_chr;
|
280
|
+
var x, y, z;
|
281
|
+
for (var i = 0; i < str.length; i++) {
|
282
|
+
chr = str.charCodeAt(i);
|
283
|
+
if ((chr & 0xFF80) == 0) {
|
284
|
+
utf8.push(chr);
|
285
|
+
} else {
|
286
|
+
if ((chr & 0xFC00) == 0xD800) {
|
287
|
+
next_chr = str.charCodeAt(i + 1);
|
288
|
+
if ((next_chr & 0xFC00) == 0xDC00) {
|
289
|
+
// UTF-16 surrogate pair
|
290
|
+
chr = (((chr & 0x03FF) << 10) | (next_chr & 0X3FF)) + 0x10000;
|
291
|
+
i++;
|
292
|
+
} else {
|
293
|
+
this.raise_error("Error decoding surrogate pair: " + chr + ", " + next_chr);
|
294
|
+
}
|
295
|
+
}
|
296
|
+
x = chr & 0xFF;
|
297
|
+
y = chr & 0xFF00;
|
298
|
+
z = chr & 0xFF0000;
|
299
|
+
|
300
|
+
if (chr <= 0x0007FF) {
|
301
|
+
utf8.push(0xC0 | (y >> 6) | (x >> 6));
|
302
|
+
utf8.push(0x80 | (x & 63));
|
303
|
+
} else if (chr <= 0x00FFFF) {
|
304
|
+
utf8.push(0xe0 | (y >> 12));
|
305
|
+
utf8.push(0x80 | ((y >> 6) & 63) | (x >> 6));
|
306
|
+
utf8.push(0x80 | (x & 63));
|
307
|
+
} else if (chr <= 0x10FFFF) {
|
308
|
+
utf8.push(0xF0 | (z >> 18));
|
309
|
+
utf8.push(0x80 | ((z >> 12) & 63) | (y >> 12));
|
310
|
+
utf8.push(0x80 | ((y >> 6) & 63) | (x >> 6));
|
311
|
+
utf8.push(0x80 | (x & 63));
|
312
|
+
} else {
|
313
|
+
this.raise_error("Error encoding to UTF8: " + chr + " is greater than U+10FFFF");
|
314
|
+
}
|
315
|
+
}
|
316
|
+
}
|
317
|
+
return utf8;
|
318
|
+
}
|
319
|
+
|
320
|
+
BabelHelper.prototype.decode_utf8 = function (utf8_data) {
|
321
|
+
var str = "";
|
322
|
+
var chr, b2, b3, b4;
|
323
|
+
for (var i = 0; i < utf8_data.length; i++) {
|
324
|
+
chr = utf8_data[i];
|
325
|
+
if ((chr & 0x80) == 0x00) {}
|
326
|
+
else if ((chr & 0xF8) == 0xF0) {
|
327
|
+
// 4 bytes: U+10000 - U+10FFFF
|
328
|
+
b2 = utf8_data[i + 1];
|
329
|
+
b3 = utf8_data[i + 2];
|
330
|
+
b4 = utf8_data[i + 3];
|
331
|
+
if ((b2 & 0xc0) == 0x80 && (b3 & 0xC0) == 0x80 && (b4 & 0xC0) == 0x80) {
|
332
|
+
chr = (chr & 7) << 18 | (b2 & 63) << 12 | (b3 & 63) << 6 | (b4 & 63);
|
333
|
+
i += 3;
|
334
|
+
} else {
|
335
|
+
this.raise_error("Error decoding from UTF8: " + chr + "," + b2 + "," + b3 + "," + b4);
|
336
|
+
}
|
337
|
+
} else if ((chr & 0xF0) == 0xE0) {
|
338
|
+
// 3 bytes: U+0800 - U+FFFF
|
339
|
+
b2 = utf8_data[i + 1];
|
340
|
+
b3 = utf8_data[i + 2];
|
341
|
+
if ((b2 & 0xC0) == 0x80 && (b3 & 0xC0) == 0x80) {
|
342
|
+
chr = (chr & 15) << 12 | (b2 & 63) << 6 | (b3 & 63);
|
343
|
+
i += 2;
|
344
|
+
} else {
|
345
|
+
this.raise_error("Error decoding from UTF8: " + chr + "," + b2 + "," + b3);
|
346
|
+
}
|
347
|
+
} else if ((chr & 0xE0) == 0xC0) {
|
348
|
+
// 2 bytes: U+0080 - U+07FF
|
349
|
+
b2 = utf8_data[i + 1];
|
350
|
+
if ((b2 & 0xC0) == 0x80) {
|
351
|
+
chr = (chr & 31) << 6 | (b2 & 63);
|
352
|
+
i += 1;
|
353
|
+
} else {
|
354
|
+
this.raise_error("Error decoding from UTF8: " + chr + "," + b2);
|
355
|
+
}
|
356
|
+
} else {
|
357
|
+
// 80-BF: Second, third, or fourth byte of a multi-byte sequence
|
358
|
+
// F5-FF: Start of 4, 5, or 6 byte sequence
|
359
|
+
this.raise_error("Error decoding from UTF8: " + chr + " encountered not in multi-byte sequence");
|
360
|
+
}
|
361
|
+
if (chr <= 0xFFFF) {
|
362
|
+
str += String.fromCharCode(chr);
|
363
|
+
} else if (chr > 0xFFFF && chr <= 0x10FFFF) {
|
364
|
+
// Must be encoded into UTF-16 surrogate pair.
|
365
|
+
chr -= 0x10000;
|
366
|
+
str += (String.fromCharCode(0xD800 | (chr >> 10)) + String.fromCharCode(0xDC00 | (chr & 1023)));
|
367
|
+
} else {
|
368
|
+
this.raise_error("Error encoding surrogate pair: " + chr + " is greater than U+10ffff");
|
369
|
+
}
|
370
|
+
}
|
371
|
+
return str;
|
372
|
+
}
|
373
|
+
|
374
|
+
BabelHelper.prototype.raise_error = function (msg) {
|
375
|
+
throw "[" + this.constructor.name + "] " + msg;
|
376
|
+
}
|
377
|
+
|
378
|
+
|
379
|
+
// ------------------------------------------------------------ Entry
|
380
|
+
function Entry() {
|
381
|
+
BabelHelper.call(this);
|
382
|
+
}
|
383
|
+
|
384
|
+
// Inherit BabelHelper
|
385
|
+
Entry.prototype = new BabelHelper();
|
386
|
+
|
387
|
+
// Correct the constructor pointer because it points to BabelHelper
|
388
|
+
Entry.prototype.constructor = Entry;
|
389
|
+
|
390
|
+
// Define the methods of Entry
|
391
|
+
Entry.prototype.deserialize = function (data) {
|
392
|
+
}
|
393
|
+
|
394
|
+
Entry.prototype.serialize_internal = function(out) {
|
395
|
+
}
|
396
|
+
|
397
|
+
|
398
|
+
// ------------------------------------------------------------ TestBasic
|
399
|
+
function TestBasic() {
|
400
|
+
BabelHelper.call(this);
|
401
|
+
this.i8 = 0;
|
402
|
+
this.i16 = 0;
|
403
|
+
this.i32 = 0;
|
404
|
+
this.str = "";
|
405
|
+
this.ip = "";
|
406
|
+
this.guid = [];
|
407
|
+
}
|
408
|
+
|
409
|
+
// Inherit BabelHelper
|
410
|
+
TestBasic.prototype = new BabelHelper();
|
411
|
+
|
412
|
+
// Correct the constructor pointer because it points to BabelHelper
|
413
|
+
TestBasic.prototype.constructor = TestBasic;
|
414
|
+
|
415
|
+
// Define the methods of TestBasic
|
416
|
+
TestBasic.prototype.deserialize = function (data) {
|
417
|
+
this.i8 = this.read_int8(data);
|
418
|
+
this.i16 = this.read_int16(data);
|
419
|
+
this.i32 = this.read_int32(data);
|
420
|
+
this.str = this.read_string(data);
|
421
|
+
this.ip = this.read_ip_number(data);
|
422
|
+
this.guid = this.read_binary(data);
|
423
|
+
}
|
424
|
+
|
425
|
+
TestBasic.prototype.serialize_internal = function(out) {
|
426
|
+
this.write_int8(this.i8, out);
|
427
|
+
this.write_int16(this.i16, out);
|
428
|
+
this.write_int32(this.i32, out);
|
429
|
+
this.write_string(this.str, out);
|
430
|
+
this.write_ip_number(this.ip, out);
|
431
|
+
this.write_binary(this.guid, out);
|
432
|
+
}
|
433
|
+
|
434
|
+
|
435
|
+
// ------------------------------------------------------------ TestComplex
|
436
|
+
function TestComplex() {
|
437
|
+
BabelHelper.call(this);
|
438
|
+
this.list1 = [];
|
439
|
+
this.list2 = [];
|
440
|
+
this.map1 = {};
|
441
|
+
this.map2 = {};
|
442
|
+
}
|
443
|
+
|
444
|
+
// Inherit BabelHelper
|
445
|
+
TestComplex.prototype = new BabelHelper();
|
446
|
+
|
447
|
+
// Correct the constructor pointer because it points to BabelHelper
|
448
|
+
TestComplex.prototype.constructor = TestComplex;
|
449
|
+
|
450
|
+
// Define the methods of TestComplex
|
451
|
+
TestComplex.prototype.deserialize = function (data) {
|
452
|
+
// Deserialize list 'list1'
|
453
|
+
this.list1 = [];
|
454
|
+
var var_100 = this.read_int32(data);
|
455
|
+
for(var var_102=0; var_102<var_100; var_102++) {
|
456
|
+
var var_101 = this.read_int32(data);
|
457
|
+
this.list1.push(var_101);
|
458
|
+
}
|
459
|
+
// Deserialize list 'list2'
|
460
|
+
this.list2 = [];
|
461
|
+
var var_103 = this.read_int32(data);
|
462
|
+
for(var var_105=0; var_105<var_103; var_105++) {
|
463
|
+
var var_104 = this.read_int8(data);
|
464
|
+
this.list2.push(var_104);
|
465
|
+
}
|
466
|
+
// Deserialize map 'map1'
|
467
|
+
this.map1 = {};
|
468
|
+
var var_106 = this.read_int32(data);
|
469
|
+
for(var var_109=0; var_109<var_106; var_109++) {
|
470
|
+
var var_107 = this.read_int8(data);
|
471
|
+
var var_108 = this.read_int32(data);
|
472
|
+
this.map1[var_107] = var_108;
|
473
|
+
}
|
474
|
+
// Deserialize map 'map2'
|
475
|
+
this.map2 = {};
|
476
|
+
var var_10a = this.read_int32(data);
|
477
|
+
for(var var_10d=0; var_10d<var_10a; var_10d++) {
|
478
|
+
var var_10b = this.read_string(data);
|
479
|
+
var var_10c = [];
|
480
|
+
var var_10e = this.read_int32(data);
|
481
|
+
for(var var_110=0; var_110<var_10e; var_110++) {
|
482
|
+
var var_10f = new Entry();
|
483
|
+
var_10f.deserialize(data);
|
484
|
+
var_10c.push(var_10f);
|
485
|
+
}
|
486
|
+
this.map2[var_10b] = var_10c;
|
487
|
+
}
|
488
|
+
}
|
489
|
+
|
490
|
+
TestComplex.prototype.serialize_internal = function(out) {
|
491
|
+
// Serialize list 'list1'
|
492
|
+
this.write_int32(this.list1.length, out);
|
493
|
+
for(var var_112=0; var_112<this.list1.length; var_112++) {
|
494
|
+
var var_111 = this.list1[var_112];
|
495
|
+
this.write_int32(var_111, out)
|
496
|
+
}
|
497
|
+
// Serialize list 'list2'
|
498
|
+
this.write_int32(this.list2.length, out);
|
499
|
+
for(var var_114=0; var_114<this.list2.length; var_114++) {
|
500
|
+
var var_113 = this.list2[var_114];
|
501
|
+
this.write_int8(var_113, out)
|
502
|
+
}
|
503
|
+
// Serialize map 'map1'
|
504
|
+
var var_115 = Object.keys(this.map1).length;
|
505
|
+
this.write_int32(var_115, out);
|
506
|
+
for(var var_116 in this.map1) {
|
507
|
+
var var_117 = this.map1[var_116];
|
508
|
+
this.write_int8(var_116, out)
|
509
|
+
this.write_int32(var_117, out)
|
510
|
+
}
|
511
|
+
// Serialize map 'map2'
|
512
|
+
var var_119 = Object.keys(this.map2).length;
|
513
|
+
this.write_int32(var_119, out);
|
514
|
+
for(var var_11a in this.map2) {
|
515
|
+
var var_11b = this.map2[var_11a];
|
516
|
+
this.write_string(var_11a, out)
|
517
|
+
this.write_int32(var_11b.length, out);
|
518
|
+
for(var var_11e=0; var_11e<var_11b.length; var_11e++) {
|
519
|
+
var var_11d = var_11b[var_11e];
|
520
|
+
var_11d.serialize_internal(out)
|
521
|
+
}
|
522
|
+
}
|
523
|
+
}
|