divine 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
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,171 +0,0 @@
1
- import java.io.ByteArrayInputStream;
2
- import java.io.ByteArrayOutputStream;
3
- import java.io.IOException;
4
- import java.nio.charset.Charset;
5
- import java.util.ArrayList;
6
- import java.util.HashMap;
7
-
8
- abstract class BabelBase {
9
- private static final Charset UTF8 = Charset.forName("UTF-8");
10
-
11
- public byte[] serialize() throws IOException {
12
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
13
- serializeInternal(baos);
14
- baos.close();
15
- return baos.toByteArray();
16
- }
17
-
18
- abstract void serializeInternal(ByteArrayOutputStream baos) throws IOException;
19
-
20
- abstract void deserialize(ByteArrayInputStream baos) throws IOException;
21
-
22
- protected int readInt8(ByteArrayInputStream data) {
23
- return data.read() & 0xff;
24
- }
25
-
26
- protected int readInt16(ByteArrayInputStream data) {
27
- return (data.read() << 8) | readInt8(data);
28
- }
29
-
30
- protected int readInt24(ByteArrayInputStream data) {
31
- return (data.read() << 16) | readInt16(data);
32
- }
33
-
34
- protected long readInt32(ByteArrayInputStream data) {
35
- return (data.read() << 24) | readInt24(data);
36
- }
37
-
38
- protected boolean readBool(ByteArrayInputStream data) {
39
- return readInt8(data) == 1;
40
- }
41
-
42
- protected String readString(ByteArrayInputStream data) throws IOException {
43
- // Force utf8
44
- return new String(readBytes(readInt16(data), data), UTF8);
45
- }
46
-
47
- private byte[] readBytes(int size, ByteArrayInputStream data) throws IOException {
48
- byte[] bs = new byte[size];
49
- data.read(bs);
50
- return bs;
51
- }
52
-
53
- protected byte[] readBinary(ByteArrayInputStream data) throws IOException {
54
- long c = readInt32(data);
55
- if (c > Integer.MAX_VALUE) {
56
- throw new IndexOutOfBoundsException("Binary data to big for java");
57
- }
58
- return readBytes((int) c, data);
59
- }
60
-
61
- protected byte[] readShortBinary(ByteArrayInputStream data) throws IOException {
62
- return readBytes(readInt8(data), data);
63
- }
64
-
65
- protected String readIpNumber(ByteArrayInputStream data) throws IOException {
66
- byte[] ips = readShortBinary(data);
67
- String ip = "";
68
- for (byte b : ips) {
69
- if (ip.length() > 0) {
70
- ip += ".";
71
- }
72
- ip += (b & 0xFF);
73
- }
74
- return ip;
75
- }
76
-
77
- protected void writeInt8(int v, ByteArrayOutputStream out) {
78
- if (v > 0xFF) { // Max 255
79
- raiseError("Too large int8 number: " + v);
80
- }
81
- out.write(v);
82
- }
83
-
84
- protected void writeInt16(int v, ByteArrayOutputStream out) {
85
- if (v > 0xFFFF) { // Max 65.535
86
- raiseError("Too large int16 number: " + v);
87
- }
88
- writeInt8(v >> 8 & 0xFF, out);
89
- writeInt8(v & 0xFF, out);
90
- }
91
-
92
- protected void writeInt24(int v, ByteArrayOutputStream out) {
93
- if (v > 0xFFFFFF) { // Max 16.777.215
94
- raiseError("Too large int24 number: " + v);
95
- }
96
- writeInt8(v >> 16 & 0xFF, out);
97
- writeInt16(v & 0xFFFF, out);
98
- }
99
-
100
- protected void writeInt32(long v, ByteArrayOutputStream out) {
101
- if (v > 0xFFFFFFFFL) { // Max 4.294.967.295
102
- raiseError("Too large int32 number: " + v);
103
- }
104
- writeInt8((int) ((v >> 24) & 0xFF), out);
105
- writeInt24((int) (v & 0xFFFFFF), out);
106
- }
107
-
108
- protected void writeBool(boolean v, ByteArrayOutputStream out) {
109
- writeInt8(v ? 1 : 0, out);
110
- }
111
-
112
- protected void writeString(String v, ByteArrayOutputStream out) throws IOException {
113
- byte[] bs = v.getBytes(UTF8);
114
- if (bs.length > 0xFFFF) {
115
- raiseError("Too large string: " + bs.length + " bytes");
116
- }
117
- writeInt16(bs.length, out);
118
- out.write(bs);
119
- }
120
-
121
- protected void writeBinary(byte[] v, ByteArrayOutputStream out) throws IOException {
122
- if (v.length > 0xFFFFFFFFL) {
123
- raiseError("Too large binary: " + v.length + " bytes");
124
- }
125
- writeInt32(v.length, out);
126
- out.write(v);
127
- }
128
-
129
- protected void writeShortBinary(byte[] v, ByteArrayOutputStream out) throws IOException {
130
- if (v.length > 0xFF) {
131
- raiseError("Too large short_binary: " + v.length + " bytes");
132
- }
133
- writeInt8(v.length, out);
134
- out.write(v);
135
- }
136
-
137
- protected void writeIpNumber(String v, ByteArrayOutputStream out) throws IOException {
138
- byte[] ss = new byte[0];
139
- if(!v.isEmpty()){
140
- String[] bs = v.split("\\.");
141
- ss = new byte[bs.length];
142
- for (int i = 0; i < bs.length; i++) {
143
- ss[i] = (byte) (Integer.parseInt(bs[i]) & 0xFF);
144
- }
145
- }
146
- if (ss.length == 0 || ss.length == 4) {
147
- writeShortBinary(ss, out);
148
- } else {
149
- raiseError("Unknown IP v4 number " + v); // Only IPv4 for now
150
- }
151
- }
152
-
153
- protected void raiseError(String msg) {
154
- throw new IllegalArgumentException("[" + this.getClass().getCanonicalName() + "] " + msg);
155
- }
156
- }
157
-
158
-
159
- class UnifySer extends BabelBase {
160
- public int i8 = 0;
161
-
162
- @Override
163
- void serializeInternal(ByteArrayOutputStream baos) throws IOException {
164
- writeInt8(this.i8, baos);
165
- }
166
-
167
- @Override
168
- void deserialize(ByteArrayInputStream bais) throws IOException {
169
- this.i8 = readInt8(bais);
170
- }
171
- }
@@ -1,56 +0,0 @@
1
- eval(require('fs').readFileSync('./test_unify.js', 'utf8'));
2
- var fs = require('fs');
3
- var assert = require('assert');
4
-
5
- var obj_ser = buildObject();
6
-
7
- var ca = obj_ser.serialize();
8
- serialize(ca);
9
- var read = deserialize();
10
-
11
- var obj_deser = new UnifySer();
12
- obj_deser.deserialize(new BabelDataReader(read));
13
- compare(obj_ser, obj_deser);
14
-
15
- function buildObject(){
16
- var obj = new UnifySer();
17
- //obj.i8 = 15;
18
- //obj.i32 = 154210145;
19
- //obj.i16 = 15485;
20
- obj.str = "What is This?";
21
- //obj.ip = "";
22
- obj.map1[2] = 15;
23
- obj.map1[3] = 16;
24
- return obj;
25
- }
26
-
27
- function compare(obj1, obj2){
28
- console.log(obj2);
29
- console.log(obj1);
30
- assert.equal(obj1.map1[2], obj2.map1[2]);
31
- assert.equal(obj1.str, obj2.str);
32
- }
33
-
34
- function serialize(obj){
35
- var bBuffer = new Buffer(obj);
36
- fs.writeFileSync(__dirname + '/bin.babel.js', bBuffer, function (err) {
37
- if (err) {
38
- return console.log(err);
39
- }
40
- });
41
- }
42
-
43
- function deserialize(){
44
- var data = fs.readFileSync(__dirname + '/bin.babel.js');
45
- data = toArray(data);
46
- return data;
47
-
48
- }
49
-
50
- function toArray(buffer) {
51
- var view = new Uint8Array(buffer.length);
52
- for (var i = 0; i < buffer.length; ++i) {
53
- view[i] = buffer[i];
54
- }
55
- return view;
56
- }
@@ -1,326 +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
- ip = "";
85
- for (i = 0, len = ip_array.length; i < len; i++) {
86
- b = ip_array[i];
87
- if (ip.length > 0) {
88
- ip += ".";
89
- }
90
- ip += b.toString();
91
- }
92
- return ip;
93
- };
94
-
95
- BabelHelper.prototype.read_string = function (data) {
96
- return this.decode_utf8(data.read(this.read_int16(data)))
97
- };
98
-
99
- BabelHelper.prototype.write_int8 = function (v, out) {
100
- if (v > 0xFF) // Max 255
101
- this.raise_error("Too large int8 number: " + v);
102
- out.writeByte(v);
103
- }
104
-
105
- BabelHelper.prototype.write_int16 = function (v, out) {
106
- if (v > 0xFFFF) // Max 65.535
107
- this.raise_error("Too large int16 number: " + v);
108
- this.write_int8(v >> 8 & 0xFF, out);
109
- this.write_int8(v & 0xFF, out);
110
- }
111
-
112
- BabelHelper.prototype.write_int24 = function (v, out) {
113
- if (v > 0xFFFFFF) // Max 16.777.215
114
- this.raise_error("Too large int24 number: " + v);
115
- this.write_int8(v >> 16 & 0xFF, out);
116
- this.write_int16(v & 0xFFFF, out);
117
- }
118
-
119
- BabelHelper.prototype.write_int32 = function (v, out) {
120
- if (v > 0xFFFFFFFF) // Max 4.294.967.295
121
- this.raise_error("Too large int32 number: " + v);
122
- this.write_int8(v >> 24 & 0xFF, out);
123
- this.write_int24(v & 0xFFFFFF, out);
124
- }
125
-
126
- BabelHelper.prototype.write_bool = function (v, out) {
127
- this.write_int8(v ? 1 : 0, out)
128
- }
129
-
130
- BabelHelper.prototype.write_bool = function (v, out) {
131
- this.write_int8(v ? 1 : 0, out)
132
- }
133
-
134
- BabelHelper.prototype.write_string = function (v, out) {
135
- var s = this.encode_utf8(v);
136
- if (s.length > 0xFFFF) this.raise_error("Too large string: " + s.length + " bytes");
137
- this.write_int16(s.length, out);
138
- out.write(s);
139
- }
140
-
141
- BabelHelper.prototype.write_binary = function (v, out) {
142
- if ((v instanceof Array) || (v instanceof Uint8Array)) {
143
- if (v.length > 0xFFFFFFFF) this.raise_error("Too large binary: " + v.length + " (" + v.constructor.name + ")");
144
- this.write_int32(v.length, out)
145
- out.write(v);
146
- } else if (v.constructor == String) {
147
- if (v.length > 0xFFFFFFFF) this.raise_error("Too large binary: " + v.length + " (" + v.constructor.name + ")");
148
- this.write_int32(v.length, out)
149
- out.write(v);
150
- } else if (v == null) {
151
- this.raise_error("Unsupported binary 'null'");
152
- } else {
153
- this.raise_error("Unsupported binary of type '" + v.constructor.name + "'");
154
- }
155
- }
156
-
157
- BabelHelper.prototype.write_short_binary = function (v, out) {
158
- if ((v instanceof Array) || (v instanceof Uint8Array)) {
159
- if (v.length > 0xFF) this.raise_error("Too large binary: " + v.length + " (" + v.constructor.name + ")");
160
- this.write_int8(v.length, out)
161
- out.write(v);
162
- } else if (v.constructor == String) {
163
- if (v.length > 0xFF) this.raise_error("Too large binary: " + v.length + " (" + v.constructor.name + ")");
164
- this.write_int8(v.length, out)
165
- out.write(v);
166
- } else if (v == null) {
167
- this.raise_error("Unsupported binary 'null'");
168
- } else {
169
- this.raise_error("Unsupported binary of type '" + v.constructor.name + "'");
170
- }
171
- }
172
-
173
- BabelHelper.prototype.write_ip_number = function (v, out) {
174
- if ((v instanceof Array) || (v instanceof Uint8Array)) {
175
- if (v.length != 4 && v.length != 0) this.raise_error("Unknown IP v4 number " + v);
176
- this.write_short_binary(v, out)
177
- } else if (v.constructor == String) {
178
- var ss = [];
179
- if (v.length > 0) {
180
- ss = v.split(".").map(Number);
181
- }
182
- this.write_ip_number(ss, out);
183
- } else {
184
- this.raise_error("Unknown IP number '" + v + "'");
185
- }
186
- }
187
-
188
- BabelHelper.prototype.encode_utf8 = function (str) {
189
- var utf8 = [];
190
- var chr, next_chr;
191
- var x, y, z;
192
- for (var i = 0; i < str.length; i++) {
193
- chr = str.charCodeAt(i);
194
- if ((chr & 0xFF80) == 0) {
195
- utf8.push(chr);
196
- } else {
197
- if ((chr & 0xFC00) == 0xD800) {
198
- next_chr = str.charCodeAt(i + 1);
199
- if ((next_chr & 0xFC00) == 0xDC00) {
200
- // UTF-16 surrogate pair
201
- chr = (((chr & 0x03FF) << 10) | (next_chr & 0X3FF)) + 0x10000;
202
- i++;
203
- } else {
204
- this.raise_error("Error decoding surrogate pair: " + chr + ", " + next_chr);
205
- }
206
- }
207
- x = chr & 0xFF;
208
- y = chr & 0xFF00;
209
- z = chr & 0xFF0000;
210
-
211
- if (chr <= 0x0007FF) {
212
- utf8.push(0xC0 | (y >> 6) | (x >> 6));
213
- utf8.push(0x80 | (x & 63));
214
- } else if (chr <= 0x00FFFF) {
215
- utf8.push(0xe0 | (y >> 12));
216
- utf8.push(0x80 | ((y >> 6) & 63) | (x >> 6));
217
- utf8.push(0x80 | (x & 63));
218
- } else if (chr <= 0x10FFFF) {
219
- utf8.push(0xF0 | (z >> 18));
220
- utf8.push(0x80 | ((z >> 12) & 63) | (y >> 12));
221
- utf8.push(0x80 | ((y >> 6) & 63) | (x >> 6));
222
- utf8.push(0x80 | (x & 63));
223
- } else {
224
- this.raise_error("Error encoding to UTF8: " + chr + " is greater than U+10FFFF");
225
- }
226
- }
227
- }
228
- return utf8;
229
- }
230
-
231
- BabelHelper.prototype.decode_utf8 = function (utf8_data) {
232
- var str = "";
233
- var chr, b2, b3, b4;
234
- for (var i = 0; i < utf8_data.length; i++) {
235
- chr = utf8_data[i];
236
- if ((chr & 0x80) == 0x00) {}
237
- else if ((chr & 0xF8) == 0xF0) {
238
- // 4 bytes: U+10000 - U+10FFFF
239
- b2 = utf8_data[i + 1];
240
- b3 = utf8_data[i + 2];
241
- b4 = utf8_data[i + 3];
242
- if ((b2 & 0xc0) == 0x80 && (b3 & 0xC0) == 0x80 && (b4 & 0xC0) == 0x80) {
243
- chr = (chr & 7) << 18 | (b2 & 63) << 12 | (b3 & 63) << 6 | (b4 & 63);
244
- i += 3;
245
- } else {
246
- this.raise_error("Error decoding from UTF8: " + chr + "," + b2 + "," + b3 + "," + b4);
247
- }
248
- } else if ((chr & 0xF0) == 0xE0) {
249
- // 3 bytes: U+0800 - U+FFFF
250
- b2 = utf8_data[i + 1];
251
- b3 = utf8_data[i + 2];
252
- if ((b2 & 0xC0) == 0x80 && (b3 & 0xC0) == 0x80) {
253
- chr = (chr & 15) << 12 | (b2 & 63) << 6 | (b3 & 63);
254
- i += 2;
255
- } else {
256
- this.raise_error("Error decoding from UTF8: " + chr + "," + b2 + "," + b3);
257
- }
258
- } else if ((chr & 0xE0) == 0xC0) {
259
- // 2 bytes: U+0080 - U+07FF
260
- b2 = utf8_data[i + 1];
261
- if ((b2 & 0xC0) == 0x80) {
262
- chr = (chr & 31) << 6 | (b2 & 63);
263
- i += 1;
264
- } else {
265
- this.raise_error("Error decoding from UTF8: " + chr + "," + b2);
266
- }
267
- } else {
268
- // 80-BF: Second, third, or fourth byte of a multi-byte sequence
269
- // F5-FF: Start of 4, 5, or 6 byte sequence
270
- this.raise_error("Error decoding from UTF8: " + chr + " encountered not in multi-byte sequence");
271
- }
272
- if (chr <= 0xFFFF) {
273
- str += String.fromCharCode(chr);
274
- } else if (chr > 0xFFFF && chr <= 0x10FFFF) {
275
- // Must be encoded into UTF-16 surrogate pair.
276
- chr -= 0x10000;
277
- str += (String.fromCharCode(0xD800 | (chr >> 10)) + String.fromCharCode(0xDC00 | (chr & 1023)));
278
- } else {
279
- this.raise_error("Error encoding surrogate pair: " + chr + " is greater than U+10ffff");
280
- }
281
- }
282
- return str;
283
- }
284
-
285
- BabelHelper.prototype.raise_error = function (msg) {
286
- throw "[" + this.constructor.name + "] " + msg;
287
- }
288
-
289
-
290
- // ------------------------------------------------------------ UnifySer
291
- function UnifySer() {
292
- BabelHelper.call(this);
293
- this.str = "";
294
- this.map1 = {};
295
- }
296
-
297
- // Inherit BabelHelper
298
- UnifySer.prototype = new BabelHelper();
299
-
300
- // Correct the constructor pointer because it points to BabelHelper
301
- UnifySer.prototype.constructor = UnifySer;
302
-
303
- // Define the methods of UnifySer
304
- UnifySer.prototype.deserialize = function (data) {
305
- this.str = this.read_string(data);
306
- // Deserialize map 'map1'
307
- this.map1 = {};
308
- var var_100 = this.read_int32(data);
309
- for(var var_103=0; var_103<var_100; var_103++) {
310
- var var_101 = this.read_int8(data);
311
- var var_102 = this.read_int32(data);
312
- this.map1[var_101] = var_102;
313
- }
314
- }
315
-
316
- UnifySer.prototype.serialize_internal = function(out) {
317
- this.write_string(this.str, out);
318
- // Serialize map 'map1'
319
- var var_104 = Object.keys(this.map1).length;
320
- this.write_int32(var_104, out);
321
- for(var var_105 in this.map1) {
322
- var var_106 = this.map1[var_105];
323
- this.write_int8(var_105, out)
324
- this.write_int32(var_106, out)
325
- }
326
- }