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.
Files changed (44) hide show
  1. data/.gitignore +30 -0
  2. data/README.md +57 -0
  3. data/Rakefile +45 -18
  4. data/lib/divine.rb +3 -3
  5. data/lib/divine/code_generators/code_generator.rb +112 -14
  6. data/lib/divine/code_generators/java.rb +144 -80
  7. data/lib/divine/code_generators/javascript.rb +192 -104
  8. data/lib/divine/code_generators/ruby.rb +133 -60
  9. data/lib/divine/dsl.rb +70 -12
  10. data/lib/divine/version.rb +1 -1
  11. data/test/basic_complex_test/js_test/js_testBasic.js +1 -1
  12. data/test/basic_complex_test/js_test/js_testComplex.js +1 -1
  13. data/test/basic_complex_test/ruby_test/ruby_test.rb +10 -10
  14. data/test/binaryTree_test/js_test/js_test.js +1 -1
  15. data/test/binaryTree_test/ruby_test/ruby_test.rb +4 -4
  16. data/test/complex_test/js_test/js_test.js +3 -2
  17. data/test/ipv6_test/java_test/JavaTest.java +4 -6
  18. data/test/ipv6_test/js_test/js_test.js +1 -1
  19. data/test/signed_float_test/ruby_test/ruby_test.rb +36 -0
  20. data/test/signed_float_test/signed_float_test.rb +14 -0
  21. data/test/signed_int_test/java_test/JavaTest.java +83 -0
  22. data/test/signed_int_test/js_test/js_test.js +55 -0
  23. data/test/signed_int_test/ruby_test/ruby_test.rb +37 -0
  24. data/test/signed_int_test/signed_int_test.rb +15 -0
  25. data/test/unify_test/unify_test.rb +24 -13
  26. metadata +14 -38
  27. data/test/basic_complex_test/java_test/test_babel.java +0 -368
  28. data/test/basic_complex_test/js_test/test_babel.js +0 -523
  29. data/test/basic_complex_test/ruby_test/test_babel.rb +0 -368
  30. data/test/binaryTree_test/java_test/test_binaryTree.java +0 -301
  31. data/test/binaryTree_test/js_test/test_binaryTree.js +0 -447
  32. data/test/binaryTree_test/ruby_test/test_binaryTree.rb +0 -303
  33. data/test/complex_test/java_test/test_complex.java +0 -331
  34. data/test/complex_test/js_test/test_complex.js +0 -478
  35. data/test/complex_test/ruby_test/test_complex.rb +0 -330
  36. data/test/ipv6_test/java_test/junit.jar +0 -0
  37. data/test/ipv6_test/java_test/test_ipv6.java +0 -270
  38. data/test/ipv6_test/js_test/test_ipv6.js +0 -409
  39. data/test/ipv6_test/ruby_test/test_ipv6.rb +0 -267
  40. data/test/unify_test/java_test/test_unify.java +0 -171
  41. data/test/unify_test/js_test/js_test.js +0 -56
  42. data/test/unify_test/js_test/test_unify.js +0 -326
  43. data/test/unify_test/ruby_test/ruby_test.rb +0 -35
  44. data/test/unify_test/ruby_test/test_unify.rb +0 -187
@@ -1,368 +0,0 @@
1
- import java.io.ByteArrayInputStream;
2
- import java.io.ByteArrayOutputStream;
3
- import java.io.IOException;
4
- import java.util.ArrayList;
5
- import java.util.HashMap;
6
- import java.util.regex.Pattern;
7
- import java.nio.charset.Charset;
8
-
9
- abstract class BabelBase {
10
- private static final Charset UTF8 = Charset.forName("UTF-8");
11
-
12
- public byte[] serialize() throws IOException {
13
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
14
- serializeInternal(baos);
15
- baos.close();
16
- return baos.toByteArray();
17
- }
18
-
19
- abstract void serializeInternal(ByteArrayOutputStream baos) throws IOException;
20
-
21
- abstract void deserialize(ByteArrayInputStream baos) throws IOException;
22
-
23
- protected int readInt8(ByteArrayInputStream data) {
24
- return data.read() & 0xff;
25
- }
26
-
27
- protected int readInt16(ByteArrayInputStream data) {
28
- return (data.read() << 8) | readInt8(data);
29
- }
30
-
31
- protected int readInt24(ByteArrayInputStream data) {
32
- return (data.read() << 16) | readInt16(data);
33
- }
34
-
35
- protected long readInt32(ByteArrayInputStream data) {
36
- return (data.read() << 24) | readInt24(data);
37
- }
38
-
39
- protected boolean readBool(ByteArrayInputStream data) {
40
- return readInt8(data) == 1;
41
- }
42
-
43
- protected String readString(ByteArrayInputStream data) throws IOException {
44
- // Force utf8
45
- return new String(readBytes(readInt16(data), data), UTF8);
46
- }
47
-
48
- private byte[] readBytes(int size, ByteArrayInputStream data) throws IOException {
49
- byte[] bs = new byte[size];
50
- data.read(bs);
51
- return bs;
52
- }
53
-
54
- protected byte[] readBinary(ByteArrayInputStream data) throws IOException {
55
- long c = readInt32(data);
56
- if (c > Integer.MAX_VALUE) {
57
- throw new IndexOutOfBoundsException("Binary data to big for java");
58
- }
59
- return readBytes((int) c, data);
60
- }
61
-
62
- protected byte[] readShortBinary(ByteArrayInputStream data) throws IOException {
63
- return readBytes(readInt8(data), data);
64
- }
65
-
66
- protected String readIpNumber(ByteArrayInputStream data) throws IOException {
67
- byte[] ips = readShortBinary(data);
68
- if(ips.length == 4){
69
- return readIpv4Number(ips);
70
- } else{
71
- return readIpv6Number(ips);
72
- }
73
- }
74
-
75
- protected String readIpv4Number(byte[] ips){
76
- String ip = "";
77
- for (byte b : ips) {
78
- if (ip.length() > 0) {
79
- ip += ".";
80
- }
81
- ip += (b & 0xFF);
82
- }
83
- return ip;
84
- }
85
-
86
- protected String readIpv6Number(byte[] ips) throws IOException {
87
- String ip = "";
88
- int part1, part2;
89
- for (int i = 0; i < ips.length; i+=2) {
90
- part1 = ips[i] & 0xFF;
91
- part2 = ips[i+1] & 0xFF;
92
- ip += part1 == 0? "" : Integer.toHexString(part1);
93
- ip += (part1 == 0 && part2 == 0)? "" : (part2 < 10 && part1 != 0? "0" + Integer.toHexString(part2): Integer.toHexString(part2));
94
- if (i < ips.length-2) {
95
- ip += ":";
96
- }
97
- }
98
- ip = ip.replaceAll(":{3,}", "::");
99
- return ip;
100
- }
101
-
102
- protected void writeInt8(int v, ByteArrayOutputStream out) {
103
- if (v > 0xFF) { // Max 255
104
- raiseError("Too large int8 number: " + v);
105
- }else if(v < 0){
106
- raiseError("a negative number passed to int8 number: " + v);
107
- }
108
- out.write(v);
109
- }
110
-
111
- protected void writeInt16(int v, ByteArrayOutputStream out) {
112
- if (v > 0xFFFF) { // Max 65.535
113
- raiseError("Too large int16 number: " + v);
114
- }else if(v < 0){
115
- raiseError("a negative number passed to int16 number: " + v);
116
- }
117
- writeInt8(v >> 8 & 0xFF, out);
118
- writeInt8(v & 0xFF, out);
119
- }
120
-
121
- protected void writeInt24(int v, ByteArrayOutputStream out) {
122
- if (v > 0xFFFFFF) { // Max 16.777.215
123
- raiseError("Too large int24 number: " + v);
124
- }else if(v < 0){ // In Case added to Java declaration
125
- raiseError("a negative number passed to int24 number: " + v);
126
- }
127
- writeInt8(v >> 16 & 0xFF, out);
128
- writeInt16(v & 0xFFFF, out);
129
- }
130
-
131
- protected void writeInt32(long v, ByteArrayOutputStream out) {
132
- if (v > 0xFFFFFFFFL) { // Max 4.294.967.295
133
- raiseError("Too large int32 number: " + v);
134
- }else if(v < 0){
135
- raiseError("a negative number passed to int32 number: " + v);
136
- }
137
- writeInt8((int) ((v >> 24) & 0xFF), out);
138
- writeInt24((int) (v & 0xFFFFFF), out);
139
- }
140
-
141
- protected void writeBool(boolean v, ByteArrayOutputStream out) {
142
- writeInt8(v ? 1 : 0, out);
143
- }
144
-
145
- protected void writeString(String v, ByteArrayOutputStream out) throws IOException {
146
- byte[] bs = v.getBytes(UTF8);
147
- if (bs.length > 0xFFFF) {
148
- raiseError("Too large string: " + bs.length + " bytes");
149
- }
150
- writeInt16(bs.length, out);
151
- out.write(bs);
152
- }
153
-
154
- protected void writeBinary(byte[] v, ByteArrayOutputStream out) throws IOException {
155
- if (v.length > 0xFFFFFFFFL) {
156
- raiseError("Too large binary: " + v.length + " bytes");
157
- }
158
- writeInt32(v.length, out);
159
- out.write(v);
160
- }
161
-
162
-
163
- protected void write16Binary(int[] v, ByteArrayOutputStream out) throws IOException {
164
- if (v.length > 0xFF) {
165
- raiseError("Too large 16_binary: " + (v.length*2) + " bytes");
166
- }
167
- writeInt8(v.length*2, out);
168
- for(int i = 0; i < v.length; i++){
169
- this.writeInt16(v[i], out);
170
- }
171
- }
172
-
173
- protected void writeShortBinary(byte[] v, ByteArrayOutputStream out) throws IOException {
174
- if (v.length > 0xFF) {
175
- raiseError("Too large short_binary: " + v.length + " bytes");
176
- }
177
- writeInt8(v.length, out);
178
- out.write(v);
179
- }
180
-
181
- protected void writeIpNumber(String v, ByteArrayOutputStream out) throws IOException {
182
- if(v.contains(":")){
183
- writeIpv6Number( v, out);
184
- }else{
185
- writeIpv4Number( v, out);
186
- }
187
- }
188
-
189
- protected void writeIpv4Number(String v, ByteArrayOutputStream out) throws IOException {
190
- byte[] ss = new byte[0];
191
- if(!v.isEmpty()){
192
- String[] bs = v.split("\\.");
193
- ss = new byte[bs.length];
194
- for (int i = 0; i < bs.length; i++) {
195
- ss[i] = (byte) (Integer.parseInt(bs[i]) & 0xFF);
196
- }
197
- }
198
- if (ss.length == 0 || ss.length == 4) {
199
- writeShortBinary(ss, out);
200
- } else {
201
- raiseError("Unknown IP v4 number " + v); // Only IPv4 for now
202
- }
203
- }
204
-
205
- protected void writeIpv6Number(String v, ByteArrayOutputStream out)
206
- throws IOException {
207
- v = v.replaceAll(" ", "") + " "; // Temporary: To avoid the split problem when we have : at the
208
- // end of "v"
209
- int[] ss = new int[0];
210
- boolean contains_ipv6_letters = Pattern.compile("[0-9a-f]+").matcher(
211
- v.trim().toLowerCase()).find();
212
- boolean contains_other_letters = Pattern.compile("[^:0-9a-f]+")
213
- .matcher(v.trim().toLowerCase()).find();
214
- // make sure of v must have only one "::" and no more than two of ":".
215
- // e.g. 1::1::1 & 1:::1:205
216
- if (!v.trim().isEmpty() && v.split(":{3,}").length == 1
217
- && v.split(":{2}").length <= 2 && !contains_other_letters
218
- && contains_ipv6_letters) {
219
- String[] bs = v.split(":");
220
- ss = new int[bs.length];
221
- for (int i = 0; i < bs.length; i++) {
222
- String s = bs[i].trim();
223
- if (s.length() <= 4) // to avoid such number 0125f
224
- ss[i] = Integer.parseInt(
225
- (s.isEmpty() ? "0" : bs[i].trim()), 16);
226
- else
227
- raiseError("Unknown IPv6 Group " + i + " which is " + s);
228
- }
229
- }
230
- // Check for make sure of the size of the IP groups in case "::" is used
231
- // [> 2 & < 8]or not [must == 8]
232
- if (!contains_other_letters
233
- && (!v.contains("::") && ss.length == 0 || ss.length == 8)
234
- || (v.contains("::") && ss.length > 2 && ss.length < 8)) {
235
- write16Binary(ss, out);
236
- } else {
237
- raiseError("Unknown IP v6 number " + v);
238
- }
239
- }
240
-
241
- protected void raiseError(String msg) {
242
- throw new IllegalArgumentException("[" + this.getClass().getCanonicalName() + "] " + msg);
243
- }
244
- }
245
-
246
-
247
- class Entry extends BabelBase {
248
-
249
- @Override
250
- void serializeInternal(ByteArrayOutputStream baos) throws IOException {
251
- }
252
-
253
- @Override
254
- public void deserialize(ByteArrayInputStream bais) throws IOException {
255
- }
256
- }
257
-
258
-
259
- class TestBasic extends BabelBase {
260
- public int i8 = 0;
261
- public int i16 = 0;
262
- public long i32 = 0L;
263
- public String str = "";
264
- public String ip = "";
265
- public byte[] guid = new byte[0];
266
-
267
- @Override
268
- void serializeInternal(ByteArrayOutputStream baos) throws IOException {
269
- writeInt8(this.i8, baos);
270
- writeInt16(this.i16, baos);
271
- writeInt32(this.i32, baos);
272
- writeString(this.str, baos);
273
- writeIpNumber(this.ip, baos);
274
- writeBinary(this.guid, baos);
275
- }
276
-
277
- @Override
278
- public void deserialize(ByteArrayInputStream bais) throws IOException {
279
- this.i8 = readInt8(bais);
280
- this.i16 = readInt16(bais);
281
- this.i32 = readInt32(bais);
282
- this.str = readString(bais);
283
- this.ip = readIpNumber(bais);
284
- this.guid = readBinary(bais);
285
- }
286
- }
287
-
288
-
289
- class TestComplex extends BabelBase {
290
- public ArrayList<Long> list1 = new ArrayList<Long>();
291
- public ArrayList<Integer> list2 = new ArrayList<Integer>();
292
- public HashMap<Integer, Long> map1 = new HashMap<Integer, Long>();
293
- public HashMap<String, ArrayList<Entry>> map2 = new HashMap<String, ArrayList<Entry>>();
294
-
295
- @Override
296
- void serializeInternal(ByteArrayOutputStream baos) throws IOException {
297
- // Serialize list 'list1'
298
- writeInt32(list1.size(), baos);
299
- for(int var_101=0; var_101<list1.size(); var_101++) {
300
- long var_100 = list1.get(var_101);
301
- writeInt32(var_100, baos);
302
- }
303
- // Serialize list 'list2'
304
- writeInt32(list2.size(), baos);
305
- for(int var_103=0; var_103<list2.size(); var_103++) {
306
- int var_102 = list2.get(var_103);
307
- writeInt8(var_102, baos);
308
- }
309
- // Serialize map 'map1'
310
- writeInt32(map1.size(), baos);
311
- for(int var_104 : map1.keySet()) {
312
- long var_105 = map1.get(var_104);
313
- writeInt8(var_104, baos);
314
- writeInt32(var_105, baos);
315
- }
316
- // Serialize map 'map2'
317
- writeInt32(map2.size(), baos);
318
- for(String var_106 : map2.keySet()) {
319
- ArrayList<Entry> var_107 = map2.get(var_106);
320
- writeString(var_106, baos);
321
- writeInt32(var_107.size(), baos);
322
- for(int var_109=0; var_109<var_107.size(); var_109++) {
323
- Entry var_108 = var_107.get(var_109);
324
- var_108.serializeInternal(baos);
325
- }
326
- }
327
- }
328
-
329
- @Override
330
- public void deserialize(ByteArrayInputStream bais) throws IOException {
331
- // Deserialize list 'list1'
332
- this.list1 = new ArrayList<Long>();
333
- int var_10a = (int)this.readInt32(bais);
334
- for(int var_10c=0; var_10c<var_10a; var_10c++) {
335
- long var_10b = readInt32(bais);
336
- this.list1.add(var_10b);
337
- }
338
- // Deserialize list 'list2'
339
- this.list2 = new ArrayList<Integer>();
340
- int var_10d = (int)this.readInt32(bais);
341
- for(int var_10f=0; var_10f<var_10d; var_10f++) {
342
- int var_10e = readInt8(bais);
343
- this.list2.add(var_10e);
344
- }
345
- // Deserialize map 'map1'
346
- this.map1 = new HashMap<Integer, Long>();
347
- int var_110 = (int)readInt32(bais);
348
- for(int var_113=0; var_113<var_110; var_113++) {
349
- int var_111 = readInt8(bais);
350
- long var_112 = readInt32(bais);
351
- this.map1.put(var_111, var_112);
352
- }
353
- // Deserialize map 'map2'
354
- this.map2 = new HashMap<String, ArrayList<Entry>>();
355
- int var_114 = (int)readInt32(bais);
356
- for(int var_117=0; var_117<var_114; var_117++) {
357
- String var_115 = readString(bais);
358
- ArrayList<Entry> var_116 = new ArrayList<Entry>();
359
- int var_118 = (int)this.readInt32(bais);
360
- for(int var_11a=0; var_11a<var_118; var_11a++) {
361
- Entry var_119 = new Entry();
362
- var_119.deserialize(bais);
363
- var_116.add(var_119);
364
- }
365
- this.map2.put(var_115, var_116);
366
- }
367
- }
368
- }
@@ -1,523 +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
- // ------------------------------------------------------------ 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
- }