divine 0.0.2 → 0.0.3
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/.gitignore +30 -0
- data/README.md +57 -0
- data/Rakefile +45 -18
- data/lib/divine.rb +3 -3
- data/lib/divine/code_generators/code_generator.rb +112 -14
- data/lib/divine/code_generators/java.rb +144 -80
- data/lib/divine/code_generators/javascript.rb +192 -104
- data/lib/divine/code_generators/ruby.rb +133 -60
- data/lib/divine/dsl.rb +70 -12
- data/lib/divine/version.rb +1 -1
- data/test/basic_complex_test/js_test/js_testBasic.js +1 -1
- data/test/basic_complex_test/js_test/js_testComplex.js +1 -1
- data/test/basic_complex_test/ruby_test/ruby_test.rb +10 -10
- data/test/binaryTree_test/js_test/js_test.js +1 -1
- data/test/binaryTree_test/ruby_test/ruby_test.rb +4 -4
- data/test/complex_test/js_test/js_test.js +3 -2
- data/test/ipv6_test/java_test/JavaTest.java +4 -6
- data/test/ipv6_test/js_test/js_test.js +1 -1
- data/test/signed_float_test/ruby_test/ruby_test.rb +36 -0
- data/test/signed_float_test/signed_float_test.rb +14 -0
- data/test/signed_int_test/java_test/JavaTest.java +83 -0
- data/test/signed_int_test/js_test/js_test.js +55 -0
- data/test/signed_int_test/ruby_test/ruby_test.rb +37 -0
- data/test/signed_int_test/signed_int_test.rb +15 -0
- data/test/unify_test/unify_test.rb +24 -13
- metadata +14 -38
- data/test/basic_complex_test/java_test/test_babel.java +0 -368
- data/test/basic_complex_test/js_test/test_babel.js +0 -523
- data/test/basic_complex_test/ruby_test/test_babel.rb +0 -368
- data/test/binaryTree_test/java_test/test_binaryTree.java +0 -301
- data/test/binaryTree_test/js_test/test_binaryTree.js +0 -447
- data/test/binaryTree_test/ruby_test/test_binaryTree.rb +0 -303
- data/test/complex_test/java_test/test_complex.java +0 -331
- data/test/complex_test/js_test/test_complex.js +0 -478
- data/test/complex_test/ruby_test/test_complex.rb +0 -330
- data/test/ipv6_test/java_test/junit.jar +0 -0
- data/test/ipv6_test/java_test/test_ipv6.java +0 -270
- data/test/ipv6_test/js_test/test_ipv6.js +0 -409
- data/test/ipv6_test/ruby_test/test_ipv6.rb +0 -267
- data/test/unify_test/java_test/test_unify.java +0 -171
- data/test/unify_test/js_test/js_test.js +0 -56
- data/test/unify_test/js_test/test_unify.js +0 -326
- data/test/unify_test/ruby_test/ruby_test.rb +0 -35
- data/test/unify_test/ruby_test/test_unify.rb +0 -187
@@ -1,447 +0,0 @@
|
|
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
|
-
// ------------------------------------------------------------ BinaryTree
|
380
|
-
function BinaryTree() {
|
381
|
-
BabelHelper.call(this);
|
382
|
-
this.root_node = [];
|
383
|
-
}
|
384
|
-
|
385
|
-
// Inherit BabelHelper
|
386
|
-
BinaryTree.prototype = new BabelHelper();
|
387
|
-
|
388
|
-
// Correct the constructor pointer because it points to BabelHelper
|
389
|
-
BinaryTree.prototype.constructor = BinaryTree;
|
390
|
-
|
391
|
-
// Define the methods of BinaryTree
|
392
|
-
BinaryTree.prototype.deserialize = function (data) {
|
393
|
-
// Deserialize list 'root_node'
|
394
|
-
this.root_node = [];
|
395
|
-
var var_100 = this.read_int32(data);
|
396
|
-
for(var var_102=0; var_102<var_100; var_102++) {
|
397
|
-
var var_101 = new Node();
|
398
|
-
var_101.deserialize(data);
|
399
|
-
this.root_node.push(var_101);
|
400
|
-
}
|
401
|
-
}
|
402
|
-
|
403
|
-
BinaryTree.prototype.serialize_internal = function(out) {
|
404
|
-
// Serialize list 'root_node'
|
405
|
-
this.write_int32(this.root_node.length, out);
|
406
|
-
for(var var_104=0; var_104<this.root_node.length; var_104++) {
|
407
|
-
var var_103 = this.root_node[var_104];
|
408
|
-
var_103.serialize_internal(out)
|
409
|
-
}
|
410
|
-
}
|
411
|
-
|
412
|
-
|
413
|
-
// ------------------------------------------------------------ Node
|
414
|
-
function Node() {
|
415
|
-
BabelHelper.call(this);
|
416
|
-
this.i32 = 0;
|
417
|
-
this.next_node = [];
|
418
|
-
}
|
419
|
-
|
420
|
-
// Inherit BabelHelper
|
421
|
-
Node.prototype = new BabelHelper();
|
422
|
-
|
423
|
-
// Correct the constructor pointer because it points to BabelHelper
|
424
|
-
Node.prototype.constructor = Node;
|
425
|
-
|
426
|
-
// Define the methods of Node
|
427
|
-
Node.prototype.deserialize = function (data) {
|
428
|
-
this.i32 = this.read_int32(data);
|
429
|
-
// Deserialize list 'next_node'
|
430
|
-
this.next_node = [];
|
431
|
-
var var_105 = this.read_int32(data);
|
432
|
-
for(var var_107=0; var_107<var_105; var_107++) {
|
433
|
-
var var_106 = new Node();
|
434
|
-
var_106.deserialize(data);
|
435
|
-
this.next_node.push(var_106);
|
436
|
-
}
|
437
|
-
}
|
438
|
-
|
439
|
-
Node.prototype.serialize_internal = function(out) {
|
440
|
-
this.write_int32(this.i32, out);
|
441
|
-
// Serialize list 'next_node'
|
442
|
-
this.write_int32(this.next_node.length, out);
|
443
|
-
for(var var_109=0; var_109<this.next_node.length; var_109++) {
|
444
|
-
var var_108 = this.next_node[var_109];
|
445
|
-
var_108.serialize_internal(out)
|
446
|
-
}
|
447
|
-
}
|
@@ -1,303 +0,0 @@
|
|
1
|
-
module BabelTest
|
2
|
-
|
3
|
-
|
4
|
-
class BabelBase < Object
|
5
|
-
public
|
6
|
-
def serialize
|
7
|
-
out = []
|
8
|
-
serialize_internal(out)
|
9
|
-
out.flatten.pack("C*")
|
10
|
-
end
|
11
|
-
|
12
|
-
protected
|
13
|
-
### Read methods ###
|
14
|
-
def read_int8(data)
|
15
|
-
data.getbyte
|
16
|
-
end
|
17
|
-
|
18
|
-
def read_int16(data)
|
19
|
-
(data.getbyte << 8) | read_int8(data)
|
20
|
-
end
|
21
|
-
|
22
|
-
def read_int24(data)
|
23
|
-
(data.getbyte << 16) | read_int16(data)
|
24
|
-
end
|
25
|
-
|
26
|
-
def read_int32(data)
|
27
|
-
(data.getbyte << 24) | read_int24(data)
|
28
|
-
end
|
29
|
-
|
30
|
-
def read_bool(data)
|
31
|
-
read_int8(data) == 1
|
32
|
-
end
|
33
|
-
|
34
|
-
def read_string(data)
|
35
|
-
data.read(read_int16(data)).force_encoding('UTF-8')
|
36
|
-
end
|
37
|
-
|
38
|
-
def read_binary(data)
|
39
|
-
data.read(read_int32(data))
|
40
|
-
end
|
41
|
-
|
42
|
-
def read_short_binary(data)
|
43
|
-
data.read(read_int8(data))
|
44
|
-
end
|
45
|
-
|
46
|
-
def read_ip_number(data)
|
47
|
-
ips = read_short_binary(data)
|
48
|
-
if ips.size == 4
|
49
|
-
read_ipv4_number(ips)
|
50
|
-
else
|
51
|
-
read_ipv6_number(ips)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
def read_ipv4_number(ips)
|
56
|
-
ips.bytes.to_a.join('.')
|
57
|
-
end
|
58
|
-
|
59
|
-
def read_ipv6_number(ips)
|
60
|
-
ipv6 = []
|
61
|
-
ips.bytes.each_slice(2) do |t|
|
62
|
-
fst = t[0]
|
63
|
-
lst = t[1]
|
64
|
-
tmp = ""
|
65
|
-
tmp = fst.to_s 16 if fst != 0
|
66
|
-
if fst != 0 and lst < 10
|
67
|
-
tmp << "0#{lst.to_s 16}"
|
68
|
-
elsif fst != 0 and lst > 10 or fst == 0 and lst > 0
|
69
|
-
tmp << lst.to_s(16)
|
70
|
-
end
|
71
|
-
ipv6.push(tmp)
|
72
|
-
end
|
73
|
-
ipv6.join(':').gsub(/:{2,}/, "::")
|
74
|
-
end
|
75
|
-
|
76
|
-
### Write methods ###
|
77
|
-
def write_int8(v, out)
|
78
|
-
v = v.to_i
|
79
|
-
raise_error "Too large int8 number: #{v}" if v > 0xFF # Max 255
|
80
|
-
raise_error "a negative number passed to int8 number: #{v}" if v < 0
|
81
|
-
out << v
|
82
|
-
end
|
83
|
-
|
84
|
-
def write_int16(v, out)
|
85
|
-
v = v.to_i
|
86
|
-
raise_error "Too large int16 number: #{v}" if v > 0xFFFF # Max 65.535
|
87
|
-
raise_error "a negative number passed to int16 number: #{v}" if v < 0
|
88
|
-
write_int8( v >> 8 & 0xFF, out)
|
89
|
-
write_int8( v & 0xFF, out)
|
90
|
-
end
|
91
|
-
|
92
|
-
def write_int24(v, out)
|
93
|
-
v = v.to_i
|
94
|
-
raise_error "Too large int24 number: #{v}" if v > 0xFFFFFF # Max 16.777.215
|
95
|
-
raise_error "a negative number passed to int24 number: #{v}" if v < 0 # In Case added to ruby declaration
|
96
|
-
write_int8( v >> 16 & 0xFF, out)
|
97
|
-
write_int16( v & 0xFFFF, out)
|
98
|
-
end
|
99
|
-
|
100
|
-
def write_int32(v, out)
|
101
|
-
v = v.to_i
|
102
|
-
raise_error "Too large int32 number: #{v}" if v > 0xFFFFFFFF # Max 4.294.967.295
|
103
|
-
raise_error "a negative number passed to int32 number: #{v}" if v < 0
|
104
|
-
write_int8( v >> 24 & 0xFF, out)
|
105
|
-
write_int24( v & 0xFFFFFF, out)
|
106
|
-
end
|
107
|
-
|
108
|
-
def write_bool(v, out)
|
109
|
-
write_int8(v ? 1 : 0, out)
|
110
|
-
end
|
111
|
-
|
112
|
-
def write_string(v, out)
|
113
|
-
s = force_to_utf8_string(v)
|
114
|
-
raise_error "Too large string: #{s.bytesize} bytes" if s.bytesize > 0xFFFF
|
115
|
-
write_int16(s.bytesize, out)
|
116
|
-
out << s.bytes.to_a
|
117
|
-
end
|
118
|
-
|
119
|
-
def write_binary(v, out)
|
120
|
-
if v.is_a?(Array)
|
121
|
-
raise_error "Too large binary: #{v.size} (#{v.class.name})" unless v.size < 0xFFFFFFFF
|
122
|
-
write_int32(v.size, out)
|
123
|
-
v.each do |x|
|
124
|
-
write_int8(x, out)
|
125
|
-
end
|
126
|
-
elsif v.is_a?(String)
|
127
|
-
raise_error "Too large binary: #{v.size} (#{v.class.name})" unless v.size < 0xFFFFFFFF
|
128
|
-
write_int32(v.size, out)
|
129
|
-
out << v.bytes.to_a
|
130
|
-
else
|
131
|
-
raise_error "Unsupported binary 'nil'" if v == nil
|
132
|
-
raise_error "Unsupported binary of type '#{v.class.name}'"
|
133
|
-
end
|
134
|
-
end
|
135
|
-
|
136
|
-
def write_16_binary(v, out)
|
137
|
-
if v.is_a?(Array)
|
138
|
-
raise_error "Too large 16 binary: #{v.size} (#{v.class.name})" unless v.size*2 < 0xFF
|
139
|
-
write_int8(v.size * 2, out) # IPv6 consists of 8 parts each of them has zise of 2 bytes
|
140
|
-
v.each do |x|
|
141
|
-
write_int16(x, out)
|
142
|
-
end
|
143
|
-
else
|
144
|
-
raise_error "Unsupported binary 'nil'" if v == nil
|
145
|
-
raise_error "Unsupported binary of type '#{v.class.name}'"
|
146
|
-
end
|
147
|
-
end
|
148
|
-
|
149
|
-
def write_short_binary(v, out)
|
150
|
-
if v.is_a?(Array)
|
151
|
-
raise_error "Too large short_binary: #{v.size} (#{v.class.name})" unless v.size < 0xFF
|
152
|
-
write_int8(v.size, out)
|
153
|
-
v.each do |x|
|
154
|
-
write_int8(x, out)
|
155
|
-
end
|
156
|
-
elsif v.is_a?(String)
|
157
|
-
raise_error "To large short_binary: #{v.size} (#{v.class.name})" unless v.size < 0xFF
|
158
|
-
write_int8(v.size, out)
|
159
|
-
out << v.bytes.to_a
|
160
|
-
else
|
161
|
-
raise_error "Unsupported binary 'nil'" if v == nil
|
162
|
-
raise_error "Unsupported binary of type '#{v.class.name}'"
|
163
|
-
end
|
164
|
-
end
|
165
|
-
|
166
|
-
def write_ip_number(v, out)
|
167
|
-
if v.is_a?(Array)
|
168
|
-
if v.size == 4
|
169
|
-
write_ipv4_number(v, out);
|
170
|
-
else
|
171
|
-
write_ipv6_number(v, out);
|
172
|
-
end
|
173
|
-
elsif v.is_a?(String)
|
174
|
-
if v.include?":"
|
175
|
-
write_ipv6_number(v, out);
|
176
|
-
else
|
177
|
-
write_ipv4_number(v, out);
|
178
|
-
end
|
179
|
-
else
|
180
|
-
raise_error "Unknown IP number '#{v}'"
|
181
|
-
end
|
182
|
-
end
|
183
|
-
|
184
|
-
def write_ipv4_number(v,out)
|
185
|
-
if v.is_a?(Array)
|
186
|
-
raise_error "Unknown IP v4 number #{v}" unless v.size == 0 || v.size == 4 # Only IPv4 for now
|
187
|
-
write_short_binary(v, out)
|
188
|
-
elsif v.is_a?(String)
|
189
|
-
ss = v.split(/\./).map do |s|
|
190
|
-
s.to_i
|
191
|
-
end
|
192
|
-
write_ipv4_number(ss, out)
|
193
|
-
else
|
194
|
-
raise_error "Unknown IP number '#{v}'"
|
195
|
-
end
|
196
|
-
end
|
197
|
-
|
198
|
-
def write_ipv6_number(v, out)
|
199
|
-
if v.is_a?(Array)
|
200
|
-
write_16_binary(v, out)
|
201
|
-
elsif v.is_a?(String)
|
202
|
-
v = v.gsub(" ","") + " " # Temporary: To avoid the split problem when we have : at the end of "v"
|
203
|
-
raise_error "Unknown IPv6 number #{v}" unless v.strip.empty? ||
|
204
|
-
v.strip.match(/[^:0-9a-f]+/i) == nil && #Should not contains numbers or letters 0-9a-f
|
205
|
-
v.strip.match(/[0-9a-f]+/i) != nil && #Should contains numbers or letters 0-9a-f
|
206
|
-
v.match(":{3,}") == nil &&
|
207
|
-
v.split("::").size <= 2
|
208
|
-
ss = v.split(/:/).map do |s|
|
209
|
-
s = s.strip
|
210
|
-
raise_error "Unknown IPv6 Group #{s}" unless s.size <= 4
|
211
|
-
s.to_i 16
|
212
|
-
end
|
213
|
-
ss = [] if v.strip.empty?
|
214
|
-
raise_error "Unknown IPv6 number #{v}" unless (!v.include?("::") && ss.size == 0 || ss.size == 8) ||
|
215
|
-
(v.include?("::") && ss.size > 2 && ss.size < 8)
|
216
|
-
write_ipv6_number(ss, out)
|
217
|
-
else
|
218
|
-
raise_error "Unknown IPv6 number '#{v}'"
|
219
|
-
end
|
220
|
-
end
|
221
|
-
|
222
|
-
private
|
223
|
-
def raise_error(msg)
|
224
|
-
raise "[#{self.class.name}] #{msg}"
|
225
|
-
end
|
226
|
-
|
227
|
-
def force_to_utf8_string(string)
|
228
|
-
if string.encoding != Encoding::UTF_8
|
229
|
-
string = string.encode(Encoding::UTF_8)
|
230
|
-
end
|
231
|
-
return string
|
232
|
-
end
|
233
|
-
end
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
class BinaryTree < BabelBase
|
238
|
-
attr_accessor :root_node
|
239
|
-
|
240
|
-
def initialize()
|
241
|
-
super
|
242
|
-
@root_node ||= []
|
243
|
-
end
|
244
|
-
|
245
|
-
def serialize_internal(out)
|
246
|
-
print "+"
|
247
|
-
# Serialize list 'root_node'
|
248
|
-
write_int32(root_node.size, out)
|
249
|
-
root_node.each do |var_100|
|
250
|
-
var_100.serialize_internal(out)
|
251
|
-
end
|
252
|
-
end
|
253
|
-
|
254
|
-
def deserialize(data)
|
255
|
-
print "-"
|
256
|
-
# Deserialize list 'root_node'
|
257
|
-
@root_node = []
|
258
|
-
var_101 = read_int32(data)
|
259
|
-
(1..var_101).each do
|
260
|
-
var_102 = Node.new
|
261
|
-
var_102.deserialize(data)
|
262
|
-
@root_node << var_102
|
263
|
-
end
|
264
|
-
end
|
265
|
-
end
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
class Node < BabelBase
|
270
|
-
attr_accessor :i32, :next_node
|
271
|
-
|
272
|
-
def initialize()
|
273
|
-
super
|
274
|
-
@i32 ||= 0
|
275
|
-
@next_node ||= []
|
276
|
-
end
|
277
|
-
|
278
|
-
def serialize_internal(out)
|
279
|
-
print "+"
|
280
|
-
write_int32(i32, out)
|
281
|
-
# Serialize list 'next_node'
|
282
|
-
write_int32(next_node.size, out)
|
283
|
-
next_node.each do |var_103|
|
284
|
-
var_103.serialize_internal(out)
|
285
|
-
end
|
286
|
-
end
|
287
|
-
|
288
|
-
def deserialize(data)
|
289
|
-
print "-"
|
290
|
-
@i32 = read_int32(data)
|
291
|
-
# Deserialize list 'next_node'
|
292
|
-
@next_node = []
|
293
|
-
var_104 = read_int32(data)
|
294
|
-
(1..var_104).each do
|
295
|
-
var_105 = Node.new
|
296
|
-
var_105.deserialize(data)
|
297
|
-
@next_node << var_105
|
298
|
-
end
|
299
|
-
end
|
300
|
-
end
|
301
|
-
|
302
|
-
|
303
|
-
end
|