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
@@ -4,96 +4,125 @@
4
4
  # . They are 64-bit floating point values, the largest exact integral value is 2^53, or 9007199254740992 (-9007199254740992 to 9007199254740992)
5
5
 
6
6
 
7
- require 'pp'
8
-
9
7
  module Divine
10
8
  $debug_javascript = false
11
9
 
12
10
  class JavascriptHelperMethods < BabelHelperMethods
11
+ def get_header_comment
12
+ get_header_comment_text.map do |s|
13
+ "// #{s}"
14
+ end.join("\n")
15
+ end
16
+
13
17
  def javascript_base_class_template_str
14
18
  <<EOS
15
- // ------------------------------------------------------------ BabelDataReader
16
- function BabelDataReader(data) {
19
+ // ------------------------------------------------------------ DivineDataReader
20
+ function DivineDataReader(data) {
17
21
  this.data = data;
18
22
  this.index = 0;
19
23
  }
20
24
 
21
- BabelDataReader.prototype.getbyte = function () {
25
+ DivineDataReader.prototype.getbyte = function () {
22
26
  return this.data[this.index++];
23
27
  };
24
28
 
25
- BabelDataReader.prototype.read = function (items) {
29
+ DivineDataReader.prototype.read = function (items) {
26
30
  var from = this.index;
27
31
  this.index += items
28
32
  return this.data.subarray(from, this.index)
29
33
  };
30
34
 
31
35
 
32
- // ------------------------------------------------------------ BabelDataWriter
33
- function BabelDataWriter(data) {
36
+ // ------------------------------------------------------------ DivineDataWriter
37
+ function DivineDataWriter(data) {
34
38
  this.data = data;
35
39
  this.index = 0;
36
40
  this.data = new Uint8Array(4096);
37
41
  }
38
42
 
39
- BabelDataWriter.prototype._realloc = function (size) {
43
+ DivineDataWriter.prototype._realloc = function (size) {
40
44
  size = size || 4096;
41
45
  var old_data = this.data;
42
46
  this.data = new Uint8Array(Math.max(size, 4096) + this.data.length);
43
47
  this.data.set(old_data, 0);
44
48
  };
45
49
 
46
- BabelDataWriter.prototype.writeByte = function (a_byte) {
50
+ DivineDataWriter.prototype.writeByte = function (a_byte) {
47
51
  if (this.index + 1 >= this.data.length) this._realloc();
48
52
  this.data[this.index++] = a_byte;
49
53
  };
50
54
 
51
- BabelDataWriter.prototype.write = function (bytes) {
55
+ DivineDataWriter.prototype.write = function (bytes) {
52
56
  if (this.index + bytes.length >= this.data.length) this._realloc(bytes.length);
53
57
  this.data.set(bytes, this.index);
54
58
  this.index += bytes.length;
55
59
  };
56
60
 
57
- BabelDataWriter.prototype.get_data = function () {
61
+ DivineDataWriter.prototype.get_data = function () {
58
62
  return this.data.subarray(0, this.index);
59
63
  };
60
64
 
61
65
 
62
- // ------------------------------------------------------------ BabelHelper
63
- function BabelHelper() {}
66
+ // ------------------------------------------------------------ DivineHelper
67
+ function DivineHelper() {}
64
68
 
65
- BabelHelper.prototype.serialize = function () {
66
- var out = new BabelDataWriter();
69
+ DivineHelper.prototype.serialize = function () {
70
+ var out = new DivineDataWriter();
67
71
  this.serialize_internal(out);
68
72
  return out.get_data();
69
73
  }
70
74
 
71
- BabelHelper.prototype.read_int8 = function (data) {
75
+ DivineHelper.prototype.read_int8 = function (data) {
72
76
  return data.getbyte();
73
77
  };
74
78
 
75
- BabelHelper.prototype.read_int16 = function (data) {
79
+ DivineHelper.prototype.read_int16 = function (data) {
76
80
  return (data.getbyte() << 8) | this.read_int8(data);
77
81
  };
78
82
 
79
- BabelHelper.prototype.read_int24 = function (data) {
83
+ DivineHelper.prototype.read_int24 = function (data) {
80
84
  return (data.getbyte() << 16) | this.read_int16(data);
81
85
  };
82
86
 
83
- BabelHelper.prototype.read_int32 = function (data) {
87
+ DivineHelper.prototype.read_int32 = function (data) {
84
88
  // return (data.getbyte() << 24) | this.read_int24(data); // See notes about numbers above
85
89
  return (data.getbyte() * (256*256*256)) + this.read_int24(data);
86
90
  };
87
91
 
88
- BabelHelper.prototype.read_binary = function (data) {
92
+ DivineHelper.prototype.read_sint32 = function (data) {
93
+ // return (data.getbyte() << 24) | this.read_int24(data); // See notes about numbers above
94
+ var num = (data.getbyte() * (256*256*256)) + this.read_int24(data);
95
+ if (num > (Math.pow(2, 32 - 1) -1) ){
96
+ return num - Math.pow(2, 32);
97
+ }
98
+ return num;
99
+ };
100
+
101
+ DivineHelper.prototype.read_sint64 = function (data) {
102
+ var part1 = this.read_int32(data).toString(2); // read first part of 32 bit number
103
+ var part2 = this.read_int32(data).toString(2); // read second part of 32 bit.
104
+ if (part1.length < 32){ // deal with positive number
105
+ part2 = Array(32 - part2.length + 1).join("0") + part2;
106
+ return parseInt((part1 + part2), 2);
107
+ }else{ // deal with negative number
108
+ part1 = part1.substr(11,part1.length);
109
+ part2 = Array(32 - part2.length + 1).join("0") + part2;
110
+ var binStr = part1 + part2;
111
+ binStr = binStr.replace(/1/g,'f').replace(/0/g,'1').replace(/f/g,'0');
112
+ val = parseInt(binStr, 2) + 1;
113
+ return val * -1;
114
+ }
115
+ }
116
+
117
+ DivineHelper.prototype.read_binary = function (data) {
89
118
  return data.read(this.read_int32(data));
90
119
  };
91
120
 
92
- BabelHelper.prototype.read_short_binary = function (data) {
121
+ DivineHelper.prototype.read_short_binary = function (data) {
93
122
  return data.read(this.read_int8(data));
94
123
  };
95
124
 
96
- BabelHelper.prototype.read_ip_number = function (data) {
125
+ DivineHelper.prototype.read_ip_number = function (data) {
97
126
  var ip_array = this.read_short_binary(data);
98
127
  if(ip_array.length == 4){
99
128
  return this.read_ipv4_number(ip_array);
@@ -102,7 +131,7 @@ BabelHelper.prototype.read_ip_number = function (data) {
102
131
  }
103
132
  };
104
133
 
105
- BabelHelper.prototype.read_ipv4_number = function (ip_array) {
134
+ DivineHelper.prototype.read_ipv4_number = function (ip_array) {
106
135
  ip = "";
107
136
  for (i = 0, len = ip_array.length; i < len; i++) {
108
137
  b = ip_array[i];
@@ -113,7 +142,7 @@ BabelHelper.prototype.read_ipv4_number = function (ip_array) {
113
142
  }
114
143
  return ip;
115
144
  };
116
- BabelHelper.prototype.read_ipv6_number = function (ip_array) {
145
+ DivineHelper.prototype.read_ipv6_number = function (ip_array) {
117
146
  var ip = "";
118
147
  var part1, part2;
119
148
  for (i = 0, len = ip_array.length; i < len; i+=2) {
@@ -128,11 +157,11 @@ BabelHelper.prototype.read_ipv6_number = function (ip_array) {
128
157
  return ip;
129
158
  };
130
159
 
131
- BabelHelper.prototype.read_string = function (data) {
160
+ DivineHelper.prototype.read_string = function (data) {
132
161
  return this.decode_utf8(data.read(this.read_int16(data)))
133
162
  };
134
163
 
135
- BabelHelper.prototype.write_int8 = function (v, out) {
164
+ DivineHelper.prototype.write_int8 = function (v, out) {
136
165
  if (v > 0xFF) // Max 255
137
166
  this.raise_error("Too large int8 number: " + v);
138
167
  if(v < 0)
@@ -140,7 +169,7 @@ BabelHelper.prototype.write_int8 = function (v, out) {
140
169
  out.writeByte(v);
141
170
  }
142
171
 
143
- BabelHelper.prototype.write_int16 = function (v, out) {
172
+ DivineHelper.prototype.write_int16 = function (v, out) {
144
173
  if (v > 0xFFFF) // Max 65.535
145
174
  this.raise_error("Too large int16 number: " + v);
146
175
  if(v < 0)
@@ -149,7 +178,7 @@ BabelHelper.prototype.write_int16 = function (v, out) {
149
178
  this.write_int8(v & 0xFF, out);
150
179
  }
151
180
 
152
- BabelHelper.prototype.write_int24 = function (v, out) {
181
+ DivineHelper.prototype.write_int24 = function (v, out) {
153
182
  if (v > 0xFFFFFF) // Max 16.777.215
154
183
  this.raise_error("Too large int24 number: " + v);
155
184
  if (v < 0) // In Case added to JavaScript declaration
@@ -158,7 +187,7 @@ BabelHelper.prototype.write_int24 = function (v, out) {
158
187
  this.write_int16(v & 0xFFFF, out);
159
188
  }
160
189
 
161
- BabelHelper.prototype.write_int32 = function (v, out) {
190
+ DivineHelper.prototype.write_int32 = function (v, out) {
162
191
  if (v > 0xFFFFFFFF) // Max 4.294.967.295
163
192
  this.raise_error("Too large int32 number: " + v);
164
193
  if(v < 0)
@@ -167,18 +196,52 @@ BabelHelper.prototype.write_int32 = function (v, out) {
167
196
  this.write_int24(v & 0xFFFFFF, out);
168
197
  }
169
198
 
170
- BabelHelper.prototype.write_bool = function (v, out) {
199
+ DivineHelper.prototype.write_sint32 = function (v, out) {
200
+ var max = Math.pow(2, 32 - 1) - 1;
201
+ var min = Math.pow(2, 32 - 1) - Math.pow(2, 32);
202
+ if (v > max) // Max 2.147.483.647
203
+ this.raise_error("Too large sInt32 number: " + v + ", Max = " + max);
204
+ if(v < min) // Min -2.147.483.648
205
+ this.raise_error("Too small sInt32 number: " + v + ", Min = " + min);
206
+ this.write_int8(v >> 24 & 0xFF, out);
207
+ this.write_int24(v & 0xFFFFFF, out);
208
+ }
209
+
210
+ DivineHelper.prototype.write_sint64 = function (v, out) {
211
+ var max = Math.pow(2, 53) - 1;
212
+ var min = -Math.pow(2, 53);
213
+ if (v > max) // Max 9,007,199,254,740,991
214
+ this.raise_error("Too large sInt64 number: " + v + ", Max = " + max);
215
+ if (v < min) // Min -9,007,199,254,740,992
216
+ this.raise_error("Too small sInt64 number: " + v + ", Min = " + min);
217
+
218
+ binStr = v.toString(2);
219
+ if (v < 0){
220
+ invBinStr = binStr.replace('-','').replace(/1/g,'f').replace(/0/g,'1').replace(/f/g,'0');
221
+ invBinStr = (parseInt(invBinStr, 2) + 1).toString(2);
222
+ binStr = Array(binStr.length - invBinStr.length).join("0") + invBinStr;
223
+ binStr = Array(64 - binStr.length + 1).join("1") + binStr;
224
+ }else{
225
+ binStr = Array(64 - binStr.length + 1).join("0") + binStr;
226
+ }
227
+ part1 = binStr.substr(0, binStr.length - 32);
228
+ part2 = binStr.substr(binStr.length - 32, binStr.length);
229
+ this.write_int32(parseInt(part1, 2), out);
230
+ this.write_int32(parseInt(part2, 2), out);
231
+ }
232
+
233
+ DivineHelper.prototype.write_bool = function (v, out) {
171
234
  this.write_int8(v ? 1 : 0, out)
172
235
  }
173
236
 
174
- BabelHelper.prototype.write_string = function (v, out) {
237
+ DivineHelper.prototype.write_string = function (v, out) {
175
238
  var s = this.encode_utf8(v);
176
239
  if (s.length > 0xFFFF) this.raise_error("Too large string: " + s.length + " bytes");
177
240
  this.write_int16(s.length, out);
178
241
  out.write(s);
179
242
  }
180
243
 
181
- BabelHelper.prototype.write_binary = function (v, out) {
244
+ DivineHelper.prototype.write_binary = function (v, out) {
182
245
  if ((v instanceof Array) || (v instanceof Uint8Array)) {
183
246
  if (v.length > 0xFFFFFFFF) this.raise_error("Too large binary: " + v.length + " (" + v.constructor.name + ")");
184
247
  this.write_int32(v.length, out)
@@ -194,7 +257,7 @@ BabelHelper.prototype.write_binary = function (v, out) {
194
257
  }
195
258
  }
196
259
 
197
- BabelHelper.prototype.write_16_binary = function (v, out) {
260
+ DivineHelper.prototype.write_16_binary = function (v, out) {
198
261
  if ((v instanceof Array) || (v instanceof Uint8Array)) {
199
262
  if (v.length > 0xFF) this.raise_error("Too large 16 binary: " + v.length*2 + " (" + v.constructor.name + ")");
200
263
  this.write_int8(v.length * 2, out)
@@ -209,7 +272,7 @@ BabelHelper.prototype.write_16_binary = function (v, out) {
209
272
  }
210
273
  }
211
274
 
212
- BabelHelper.prototype.write_short_binary = function (v, out) {
275
+ DivineHelper.prototype.write_short_binary = function (v, out) {
213
276
  if ((v instanceof Array) || (v instanceof Uint8Array)) {
214
277
  if (v.length > 0xFF) this.raise_error("Too large binary: " + v.length + " (" + v.constructor.name + ")");
215
278
  this.write_int8(v.length, out)
@@ -225,7 +288,7 @@ BabelHelper.prototype.write_short_binary = function (v, out) {
225
288
  }
226
289
  }
227
290
 
228
- BabelHelper.prototype.write_ip_number = function (v, out) {
291
+ DivineHelper.prototype.write_ip_number = function (v, out) {
229
292
  if ((v instanceof Array) || (v instanceof Uint8Array)){
230
293
  if(v.length == 4){
231
294
  this.write_ipv4_number(v, out);
@@ -243,7 +306,7 @@ BabelHelper.prototype.write_ip_number = function (v, out) {
243
306
  }
244
307
  }
245
308
 
246
- BabelHelper.prototype.write_ipv4_number = function (v, out) {
309
+ DivineHelper.prototype.write_ipv4_number = function (v, out) {
247
310
  if ((v instanceof Array) || (v instanceof Uint8Array)) {
248
311
  if (v.length != 4 && v.length != 0) this.raise_error("Unknown IP v4 number " + v);
249
312
  this.write_short_binary(v, out)
@@ -258,7 +321,7 @@ BabelHelper.prototype.write_ipv4_number = function (v, out) {
258
321
  }
259
322
  };
260
323
 
261
- BabelHelper.prototype.write_ipv6_number = function (v, out) {
324
+ DivineHelper.prototype.write_ipv6_number = function (v, out) {
262
325
  if ((v instanceof Array) || (v instanceof Uint8Array)) {
263
326
  this.write_16_binary(v, out)
264
327
  } else if (v.constructor == String) {
@@ -270,7 +333,7 @@ BabelHelper.prototype.write_ipv6_number = function (v, out) {
270
333
  v = v.replace(/ /g, "");
271
334
  ss = v.split(":").map(function (t){
272
335
  if (t.length > 4)
273
- new BabelHelper().raise_error("Unknown IP Group number '" + t + "'");
336
+ new DivineHelper().raise_error("Unknown IP Group number '" + t + "'");
274
337
  if (t.length == 0)
275
338
  t = "0";
276
339
  return parseInt(t, 16);
@@ -288,7 +351,7 @@ BabelHelper.prototype.write_ipv6_number = function (v, out) {
288
351
  }
289
352
  }
290
353
 
291
- BabelHelper.prototype.encode_utf8 = function (str) {
354
+ DivineHelper.prototype.encode_utf8 = function (str) {
292
355
  var utf8 = [];
293
356
  var chr, next_chr;
294
357
  var x, y, z;
@@ -331,7 +394,7 @@ BabelHelper.prototype.encode_utf8 = function (str) {
331
394
  return utf8;
332
395
  }
333
396
 
334
- BabelHelper.prototype.decode_utf8 = function (utf8_data) {
397
+ DivineHelper.prototype.decode_utf8 = function (utf8_data) {
335
398
  var str = "";
336
399
  var chr, b2, b3, b4;
337
400
  for (var i = 0; i < utf8_data.length; i++) {
@@ -385,56 +448,103 @@ BabelHelper.prototype.decode_utf8 = function (utf8_data) {
385
448
  return str;
386
449
  }
387
450
 
388
- BabelHelper.prototype.raise_error = function (msg) {
451
+ DivineHelper.prototype.raise_error = function (msg) {
389
452
  throw "[" + this.constructor.name + "] " + msg;
390
453
  }
391
454
  EOS
392
455
  end
393
456
 
394
- def javascript_class_template_str
395
- <<EOS2
396
- // ------------------------------------------------------------ <%= c.name %>
397
- function <%= c.name %>() {
398
- BabelHelper.call(this);
399
- <% c.fields.each do |f| %>
400
- this.<%= f.name %> = <%= this.javascript_get_empty_declaration(f) %>;
401
- <% end %>
402
- }
403
-
404
- // Inherit BabelHelper
405
- <%= c.name %>.prototype = new BabelHelper();
406
-
407
- // Correct the constructor pointer because it points to BabelHelper
408
- <%= c.name %>.prototype.constructor = <%= c.name %>;
409
-
410
- // Define the methods of <%= c.name %>
411
- <%= c.name %>.prototype.deserialize = function (data) {
412
- <% c.simple_fields.each do |f| %>
413
- this.<%= f.name %> = this.read_<%= f.type %>(data);
414
- <% end %>
415
- <% c.complex_fields.each do |f| %>
416
- <%= this.javascript_deserialize_complex f %>
417
- <% end %>
418
- }
419
-
420
- <%= c.name %>.prototype.serialize_internal = function(out) {
421
- <% c.simple_fields.each do |f| %>
422
- this.write_<%= f.type %>(this.<%= f.name %>, out);
423
- <% end %>
424
- <% c.complex_fields.each do |f| %>
425
- <%= this.javascript_serialize_complex f %>
426
- <% end %>
427
- }
428
- EOS2
457
+ def javascript_class_template(sh)
458
+ code = [
459
+ "",
460
+ "// ------------------------------------------------------------ #{sh.name}",
461
+ "function #{sh.name}() {",
462
+ :indent,
463
+ "DivineHelper.call(this);",
464
+ "",
465
+ # PROPERTIES
466
+ "this.struct_version = #{sh.latest_version};",
467
+ sh.field_names.map do |fn|
468
+ f = sh.field(fn).last
469
+ "this.#{f.name} = #{javascript_get_empty_declaration(f)};"
470
+ end,
471
+ :deindent,
472
+ "}", "",
473
+
474
+ "// Inherit DivineHelper",
475
+ "#{sh.name}.prototype = new DivineHelper();", "",
476
+
477
+ "// Correct the constructor pointer because it points to DivineHelper",
478
+ "#{sh.name}.prototype.constructor = #{sh.name};", "",
479
+
480
+ # SERiALIZE INTERNAL
481
+ "// Serialize",
482
+ "#{sh.name}.prototype.serialize_internal = function(out) {",
483
+ :indent,
484
+ "this.write_int8(this.struct_version, out);",
485
+ sh.structs.map do |s|
486
+ [
487
+ "if(this.struct_version == #{s.version}) {",
488
+ :indent,
489
+ s.simple_fields.map do |f|
490
+ "this.write_#{f.type}(this.#{f.name}, out);"
491
+ end,
492
+ s.complex_fields.map do |f|
493
+ [
494
+ "", "// Serialize #{f.type} '#{f.name}'",
495
+ $debug_javascript ? "console.log(\"Serialize '#{field.name}'\");" : nil,
496
+ javascript_serialize_internal("this.#{f.name}", f.referenced_types)
497
+ ]
498
+ end,
499
+ "return;",
500
+ :deindent,
501
+ "}", ""
502
+ ]
503
+ end, "",
504
+ "throw \"Unsupported version \" + this.struct_version + \" for type '#{sh.name}'\";",
505
+ :deindent,
506
+ "}", "",
507
+
508
+ # DESERIALIZE
509
+ "// Deserialize",
510
+ "#{sh.name}.prototype.deserialize = function (data) {",
511
+ :indent,
512
+ "this.struct_version = this.read_int8(data);",
513
+ sh.structs.map do |s|
514
+ [
515
+ "if(this.struct_version == #{s.version}) {",
516
+ :indent,
517
+ s.simple_fields.map do |f|
518
+ "this.#{f.name} = this.read_#{f.type}(data);"
519
+ end,
520
+ s.complex_fields.map do |f|
521
+ [
522
+ "", "// Read #{f.type} '#{f.name}'",
523
+ $debug_javascript ? "console.log(\"Deserialize '#{field.name}'\");" : nil,
524
+ javascript_deserialize_internal("this.#{f.name}", f.referenced_types)
525
+ ]
526
+ end,
527
+ "return;",
528
+ :deindent,
529
+ "}"
530
+ ]
531
+ end, "",
532
+ "throw \"Unsupported version \" + this.struct_version + \" for type '#{sh.name}'\";",
533
+ :deindent,
534
+ "}"
535
+ ]
536
+
537
+ format_src(0, 3, code)
429
538
  end
430
539
 
540
+
431
541
  def javascript_get_empty_declaration(field)
432
542
  case field.type
433
543
  when :list, :binary, :short_binary
434
544
  "[]"
435
545
  when :map
436
546
  "{}"
437
- when :int8, :int16, :int32
547
+ when :int8, :int16, :int32, :sint32, :sint64
438
548
  "0"
439
549
  when :string, :ip_number
440
550
  "\"\""
@@ -443,16 +553,6 @@ EOS2
443
553
  end
444
554
  end
445
555
 
446
- def javascript_serialize_complex(field)
447
- types = field.referenced_types
448
- as = [
449
- "// Serialize #{field.type} '#{field.name}'",
450
- $debug_javascript ? "console.log(\"Serialize '#{field.name}'\");" : nil,
451
- javascript_serialize_internal("this.#{field.name}", types)
452
- ]
453
- format_src(4, 4, as)
454
- end
455
-
456
556
  def javascript_serialize_internal(var, types)
457
557
  if types.respond_to? :first
458
558
  case types.first
@@ -500,16 +600,6 @@ EOS2
500
600
  end
501
601
  end
502
602
 
503
- def javascript_deserialize_complex(field)
504
- types = field.referenced_types
505
- as = [
506
- "// Deserialize #{field.type} '#{field.name}'",
507
- $debug_javascript ? "console.log(\"Deserialize '#{field.name}'\");" : nil,
508
- javascript_deserialize_internal("this.#{field.name}", types)
509
- ]
510
- format_src(4, 4, as)
511
- end
512
-
513
603
  def javascript_deserialize_internal(var, types)
514
604
  if types.respond_to? :first
515
605
  case types.first
@@ -569,16 +659,14 @@ EOS2
569
659
 
570
660
  class JavascriptGenerator < JavascriptHelperMethods
571
661
  def generate_code(structs, opts)
572
- pp opts
573
662
  $debug_javascript = true if opts[:debug]
574
663
  base_template = Erubis::Eruby.new(javascript_base_class_template_str)
575
- class_template = Erubis::Eruby.new(javascript_class_template_str)
576
664
  keys = structs.keys.sort
577
665
  src = keys.map do |k|
578
666
  ss = structs[k]
579
- # TODO: Should we merge different versions and deduce deprecated methods, warn for incompatible changes, etc?
580
- raise "Duplicate definitions of struct #{k}" if ss.size > 1
581
- class_template.result( c: ss.first, this: self )
667
+ # Check different aspects the the structs
668
+ vss = sanity_check(ss)
669
+ javascript_class_template(StructHandler.new(vss))
582
670
  end
583
671
 
584
672
  # User defined super class?
@@ -588,7 +676,7 @@ EOS2
588
676
  end
589
677
 
590
678
  def javascript_get_begin_module(opts)
591
- nil
679
+ "#{get_header_comment}\n\n"
592
680
  end
593
681
 
594
682
  def javascript_get_end_module(opts)