Class: Divine::JavascriptHelperMethods

Inherits:
BabelHelperMethods show all
Defined in:
lib/divine/code_generators/javascript.rb

Overview

Support base function needed to build base divine functions and DSL structs

Direct Known Subclasses

JavascriptGenerator

Instance Method Summary collapse

Methods inherited from BabelHelperMethods

#camelize, #format_src, #get_fresh_variable_name, #get_header_comment_text, #sanity_check

Instance Method Details

#get_header_commentObject

Return the header comment



18
19
20
21
22
# File 'lib/divine/code_generators/javascript.rb', line 18

def get_header_comment
  get_header_comment_text.map do |s|
    "// #{s}"
  end.join("\n")
end

#javascript_base_class_template_strObject

  • Write Methods



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'lib/divine/code_generators/javascript.rb', line 30

def javascript_base_class_template_str
  <<EOS
// ------------------------------------------------------------ DivineDataReader
function DivineDataReader(data) {
this.data = data;
this.index = 0;
}

DivineDataReader.prototype.getbyte = function () {
return this.data[this.index++];
};

DivineDataReader.prototype.read = function (items) {
var from = this.index;
this.index += items
return this.data.subarray(from, this.index)
};


// ------------------------------------------------------------ DivineDataWriter
function DivineDataWriter(data) {
this.data = data;
this.index = 0;
this.data = new Uint8Array(4096);
}

DivineDataWriter.prototype._realloc = function (size) {
size = size || 4096;
var old_data = this.data;
this.data = new Uint8Array(Math.max(size, 4096) + this.data.length);
this.data.set(old_data, 0);
};

DivineDataWriter.prototype.writeByte = function (a_byte) {
if (this.index + 1 >= this.data.length) this._realloc();
this.data[this.index++] = a_byte;
};

DivineDataWriter.prototype.write = function (bytes) {
if (this.index + bytes.length >= this.data.length) this._realloc(bytes.length);
this.data.set(bytes, this.index);
this.index += bytes.length;
};

DivineDataWriter.prototype.get_data = function () {
return this.data.subarray(0, this.index);
};


// ------------------------------------------------------------ DivineHelper
function DivineHelper() {}

DivineHelper.prototype.serialize = function () {
var out = new DivineDataWriter();
this.serialize_internal(out);
return out.get_data();
}

DivineHelper.prototype.read_int8 = function (data) {
return data.getbyte();
};

DivineHelper.prototype.read_int16 = function (data) {
return (data.getbyte() << 8) | this.read_int8(data);
};

DivineHelper.prototype.read_int24 = function (data) {
return (data.getbyte() << 16) | this.read_int16(data);
};

DivineHelper.prototype.read_int32 = function (data) {
// return (data.getbyte() << 24) | this.read_int24(data); // See notes about numbers above
return (data.getbyte() * (256*256*256)) + this.read_int24(data);
};

DivineHelper.prototype.read_sint32 = function (data) {
// return (data.getbyte() << 24) | this.read_int24(data); // See notes about numbers above
var num = (data.getbyte() * (256*256*256)) + this.read_int24(data);
if (num > (Math.pow(2, 32 - 1) -1) ){
  return num - Math.pow(2, 32);
}
return num;
};

DivineHelper.prototype.read_sint64 = function (data) {
var part1 = this.read_int32(data).toString(2);	// read first part of 32 bit number
var part2 = this.read_int32(data).toString(2);  	// read second part of 32 bit.
if (part1.length < 32){ 				// deal with positive number
  part2 = Array(32 - part2.length + 1).join("0") + part2;
  return parseInt((part1 + part2), 2);
}else{						// deal with negative number
  part1 = part1.substr(11,part1.length);
  part2 = Array(32 - part2.length + 1).join("0") + part2;
  var binStr = part1 + part2;
  binStr = binStr.replace(/1/g,'f').replace(/0/g,'1').replace(/f/g,'0');
  val    = parseInt(binStr, 2) + 1;
  return val * -1;
}
}

DivineHelper.prototype.read_dint63 = function (data) {
var byte = this.read_int8(data)
var part1 = part2 = "";				// To hold first 32-bit and second 32-bit respectively
var bin =  byte & 0x7F;				// tmp To hold binary string
var numBytes = 1;
while ((byte >> 7) == 1) {
  byte = this.read_int8(data)
  bin = bin << 7
  bin = bin | byte & 0x7F
  numBytes ++;
  if (numBytes == 4 && (byte >> 7) == 1){
    part1 = bin.toString(2);
    bin = 1;					// To avoid unshifting of zero
  }
}
bin = bin.toString(2);
if (bin.length > 1 && part1 != "")
  bin = bin.substr(1, bin.length);
part2 = bin;
var val = parseInt(part1 + part2, 2);
return val;
}

DivineHelper.prototype.read_binary = function (data) {
return data.read(this.read_int32(data));
};

DivineHelper.prototype.read_short_binary = function (data) {
return data.read(this.read_int8(data));
};

DivineHelper.prototype.read_ip_number = function (data) {
var ip_array = this.read_short_binary(data);
if(ip_array.length == 4){
   return this.read_ipv4_number(ip_array);
}else{
   return this.read_ipv6_number(ip_array);
}
};

DivineHelper.prototype.read_ipv4_number = function (ip_array) {
ip = "";
for (i = 0, len = ip_array.length; i < len; i++) {
    b = ip_array[i];
    if (ip.length > 0) {
        ip += ".";
    }
    ip += b.toString();
}
return ip;
};
DivineHelper.prototype.read_ipv6_number = function (ip_array) {
var ip = "";
var part1, part2;
for (i = 0, len = ip_array.length; i < len; i+=2) {
    part1 = ip_array[i];
    part2 = ip_array[i+1];
    ip += part1 == 0? "" : part1.toString(16);
    ip += (part1 == 0 && part2 == 0)? "" : (part2 < 10 && part1 != 0? "0" + part2.toString(16): part2.toString(16));
    if (i < ip_array.length-2)
        ip += ":";
}
ip = ip.replace(/:{3,}/g, "::");
return ip;
};

DivineHelper.prototype.read_string = function (data) {
return this.decode_utf8(data.read(this.read_int16(data)))
};

DivineHelper.prototype.write_int8 = function (v, out) {
if (v > 0xFF) // Max 255
  this.raise_error("Too large int8 number: " + v);
if(v < 0)
  this.raise_error("a negative number passed  to int8 number: " + v);
out.writeByte(v);
}

DivineHelper.prototype.write_int16 = function (v, out) {
if (v > 0xFFFF) // Max 65.535
  this.raise_error("Too large int16 number: " + v);
if(v < 0)
  this.raise_error("a negative number passed  to int16 number: " + v);
this.write_int8(v >> 8 & 0xFF, out);
this.write_int8(v & 0xFF, out);
}

DivineHelper.prototype.write_int24 = function (v, out) {
if (v > 0xFFFFFF) 	// Max 16.777.215
  this.raise_error("Too large int24 number: " + v);
if (v < 0)		// In Case added to JavaScript declaration
  this.raise_error("a negative number passed  to int24 number: " + v);
this.write_int8(v >> 16 & 0xFF, out);
this.write_int16(v & 0xFFFF, out);
}

DivineHelper.prototype.write_int32 = function (v, out) {
if (v > 0xFFFFFFFF) // Max 4.294.967.295
  this.raise_error("Too large int32 number: " + v);
if(v < 0)
  this.raise_error("a negative number passed  to int32 number: " + v);
this.write_int8(v >> 24 & 0xFF, out);
this.write_int24(v & 0xFFFFFF, out);
}

DivineHelper.prototype.write_sint32 = function (v, out) {
var max = Math.pow(2, 32 - 1) - 1;
var min = Math.pow(2, 32 - 1) - Math.pow(2, 32);
if (v > max) 			// Max  2.147.483.647
  this.raise_error("Too large sInt32 number: " + v + ", Max = " + max);
if(v < min)				// Min -2.147.483.648
  this.raise_error("Too small sInt32 number: " + v + ", Min = " + min);
this.write_int8(v >> 24 & 0xFF, out);
this.write_int24(v & 0xFFFFFF, out);
}

DivineHelper.prototype.write_sint64 = function (v, out) {
var max =  Math.pow(2, 53) - 1;
var min = -Math.pow(2, 53);
if (v > max)			// Max  9,007,199,254,740,991
  this.raise_error("Too large sInt64 number: " + v + ", Max = " + max);
if (v < min)	 		// Min -9,007,199,254,740,992
  this.raise_error("Too small sInt64 number: " + v + ", Min = " + min);

binStr = v.toString(2);
if (v < 0){
  invBinStr = binStr.replace('-','').replace(/1/g,'f').replace(/0/g,'1').replace(/f/g,'0');
  invBinStr = (parseInt(invBinStr, 2) + 1).toString(2);
  binStr    = Array(binStr.length - invBinStr.length).join("0") + invBinStr;
  binStr    = Array(64 - binStr.length + 1).join("1") + binStr;
}else{
  binStr    = Array(64 - binStr.length + 1).join("0") + binStr;
}
part1  = binStr.substr(0, binStr.length - 32);
part2  = binStr.substr(binStr.length - 32, binStr.length);
this.write_int32(parseInt(part1, 2), out);
this.write_int32(parseInt(part2, 2), out);
}

DivineHelper.prototype.write_dint63 = function (v, out) {
var max =  Math.pow(2, 53) - 1;
var min = 0;
if (v > max)			// Max  9,007,199,254,740,991
  this.raise_error("Too large Dynamic Int53 number: " + v + ", Max = " + max);
if (v < min)	 		// Min 0
  this.raise_error("Too small Dynamic Int53 number: " + v + ", Min = " + min);

bytes = v.toString(2).split("").reverse().join("").split(/(.{7})/).filter(function(t){if (t != "" ) return true});
for (var i = bytes.length - 1; i >= 0; i--){
   var bin = bytes[i];
   bin += Array(8 - bin.length).join("0") + Math.min(i,1);
   var val = parseInt(bin.split("").reverse().join(""), 2);
   this.write_int8(val, out);  
}
}

DivineHelper.prototype.write_bool = function (v, out) {
this.write_int8(v ? 1 : 0, out)
}

DivineHelper.prototype.write_string = function (v, out) {
var s = this.encode_utf8(v);
if (s.length > 0xFFFF) this.raise_error("Too large string: " + s.length + " bytes");
this.write_int16(s.length, out);
out.write(s);
}

DivineHelper.prototype.write_binary = function (v, out) {
if ((v instanceof Array) || (v instanceof Uint8Array)) {
    if (v.length > 0xFFFFFFFF) this.raise_error("Too large binary: " + v.length + " (" + v.constructor.name + ")");
    this.write_int32(v.length, out)
    out.write(v);
} else if (v.constructor == String) {
    if (v.length > 0xFFFFFFFF) this.raise_error("Too large binary: " + v.length + " (" + v.constructor.name + ")");
    this.write_int32(v.length, out)
    out.write(v);
} else if (v == null) {
    this.raise_error("Unsupported binary 'null'");
} else {
    this.raise_error("Unsupported binary of type '" + v.constructor.name + "'");
}
}

DivineHelper.prototype.write_16_binary = function (v, out) {
if ((v instanceof Array) || (v instanceof Uint8Array)) {
    if (v.length > 0xFF) this.raise_error("Too large 16 binary: " + v.length*2 + " (" + v.constructor.name + ")");
    this.write_int8(v.length * 2, out)
    for (i = 0, len = v.length; i < len; i++) {
	  this.write_int16(v[i], out);
    }
    
} else if (v == null) {
    this.raise_error("Unsupported binary 'null'");
} else {
    this.raise_error("Unsupported binary of type '" + v.constructor.name + "'");
}
}

DivineHelper.prototype.write_short_binary = function (v, out) {
if ((v instanceof Array) || (v instanceof Uint8Array)) {
    if (v.length > 0xFF) this.raise_error("Too large binary: " + v.length + " (" + v.constructor.name + ")");
    this.write_int8(v.length, out)
    out.write(v);
} else if (v.constructor == String) {
    if (v.length > 0xFF) this.raise_error("Too large binary: " + v.length + " (" + v.constructor.name + ")");
    this.write_int8(v.length, out)
    out.write(v);
} else if (v == null) {
    this.raise_error("Unsupported binary 'null'");
} else {
    this.raise_error("Unsupported binary of type '" + v.constructor.name + "'");
}
}

DivineHelper.prototype.write_ip_number = function (v, out) {
if ((v instanceof Array) || (v instanceof Uint8Array)){
  if(v.length == 4){
     this.write_ipv4_number(v, out);
  }else{
     this.write_ipv6_number(v, out);
  }
}else if(v.constructor == String){
  if(/:/g.test(v)){
     this.write_ipv6_number(v, out);
  }else{
     this.write_ipv4_number(v, out);
  }
}else{
  this.raise_error("Unknown IP number '" + v + "'");
}
}

DivineHelper.prototype.write_ipv4_number = function (v, out) {
if ((v instanceof Array) || (v instanceof Uint8Array)) {
    if (v.length != 4 && v.length != 0) this.raise_error("Unknown IP v4 number " + v);
    this.write_short_binary(v, out)
} else if (v.constructor == String) {
    var ss = [];
    if (v.length > 0) {
        ss = v.split(".").map(Number);
    }
    this.write_ipv4_number(ss, out);
} else {
    this.raise_error("Unknown IP number '" + v + "'");
}
};

DivineHelper.prototype.write_ipv6_number = function (v, out) {
if ((v instanceof Array) || (v instanceof Uint8Array)) {
    this.write_16_binary(v, out)
} else if (v.constructor == String) {
    var ss = [];
    var contains_ipv6_letters  = /[0-9a-f]+/gi.test(v);
    var contains_other_letters = /[^:0-9a-f]+/gi.test(v);
    if (v.length > 0 && v.split(/:{3,}/g).length == 1 && v.split(/:{2}/g).length <= 2 && !contains_other_letters && 
        contains_ipv6_letters) {
        v = v.replace(/ /g, "");
        ss = v.split(":").map(function (t){
           if (t.length > 4)
		 new DivineHelper().raise_error("Unknown IP Group number '" + t + "'");
           if (t.length == 0)
             t = "0";
           return parseInt(t, 16);
        });
    }
    if (!contains_other_letters &&
       ( !/::/g.test(v) && ss.length == 0 || ss.length == 8) || ( /::/g.test(v) && ss.length > 2 && ss.length < 8) ) {
      this.write_ipv6_number(ss, out);
    } else {
      this.raise_error("Unknown IP v6 number '" + v + "'");
    }

} else {
    this.raise_error("Unknown IP v6 number '" + v + "'");
}
}

DivineHelper.prototype.encode_utf8 = function (str) {
var utf8 = [];
var chr, next_chr;
var x, y, z;
for (var i = 0; i < str.length; i++) {
    chr = str.charCodeAt(i);
    if ((chr & 0xFF80) == 0) {
        utf8.push(chr);
    } else {
        if ((chr & 0xFC00) == 0xD800) {
            next_chr = str.charCodeAt(i + 1);
            if ((next_chr & 0xFC00) == 0xDC00) {
                // UTF-16 surrogate pair
                chr = (((chr & 0x03FF) << 10) | (next_chr & 0X3FF)) + 0x10000;
                i++;
            } else {
                this.raise_error("Error decoding surrogate pair: " + chr + ", " + next_chr);
            }
        }
        x = chr & 0xFF;
        y = chr & 0xFF00;
        z = chr & 0xFF0000;

        if (chr <= 0x0007FF) {
            utf8.push(0xC0 | (y >> 6) | (x >> 6));
            utf8.push(0x80 | (x & 63));
        } else if (chr <= 0x00FFFF) {
            utf8.push(0xe0 | (y >> 12));
            utf8.push(0x80 | ((y >> 6) & 63) | (x >> 6));
            utf8.push(0x80 | (x & 63));
        } else if (chr <= 0x10FFFF) {
            utf8.push(0xF0 | (z >> 18));
            utf8.push(0x80 | ((z >> 12) & 63) | (y >> 12));
            utf8.push(0x80 | ((y >> 6) & 63) | (x >> 6));
            utf8.push(0x80 | (x & 63));
        } else {
            this.raise_error("Error encoding to UTF8: " + chr + " is greater than U+10FFFF");
        }
    }
}
return utf8;
}

DivineHelper.prototype.decode_utf8 = function (utf8_data) {
var str = "";
var chr, b2, b3, b4;
for (var i = 0; i < utf8_data.length; i++) {
    chr = utf8_data[i];
    if ((chr & 0x80) == 0x00) {} 
    else if ((chr & 0xF8) == 0xF0) {
        // 4 bytes: U+10000 - U+10FFFF
        b2 = utf8_data[i + 1];
        b3 = utf8_data[i + 2];
        b4 = utf8_data[i + 3];
        if ((b2 & 0xc0) == 0x80 && (b3 & 0xC0) == 0x80 && (b4 & 0xC0) == 0x80) {
            chr = (chr & 7) << 18 | (b2 & 63) << 12 | (b3 & 63) << 6 | (b4 & 63);
            i += 3;
        } else {
            this.raise_error("Error decoding from UTF8: " + chr + "," + b2 + "," + b3 + "," + b4);
        }
    } else if ((chr & 0xF0) == 0xE0) {
        // 3 bytes: U+0800 - U+FFFF
        b2 = utf8_data[i + 1];
        b3 = utf8_data[i + 2];
        if ((b2 & 0xC0) == 0x80 && (b3 & 0xC0) == 0x80) {
            chr = (chr & 15) << 12 | (b2 & 63) << 6 | (b3 & 63);
            i += 2;
        } else {
            this.raise_error("Error decoding from UTF8: " + chr + "," + b2 + "," + b3);
        }
    } else if ((chr & 0xE0) == 0xC0) {
        // 2 bytes: U+0080 - U+07FF
        b2 = utf8_data[i + 1];
        if ((b2 & 0xC0) == 0x80) {
            chr = (chr & 31) << 6 | (b2 & 63);
            i += 1;
        } else {
            this.raise_error("Error decoding from UTF8: " + chr + "," + b2);
        }
    } else {
        // 80-BF: Second, third, or fourth byte of a multi-byte sequence
        // F5-FF: Start of 4, 5, or 6 byte sequence
        this.raise_error("Error decoding from UTF8: " + chr + " encountered not in multi-byte sequence");
    }
    if (chr <= 0xFFFF) {
        str += String.fromCharCode(chr);
    } else if (chr > 0xFFFF && chr <= 0x10FFFF) {
        // Must be encoded into UTF-16 surrogate pair.
        chr -= 0x10000;
        str += (String.fromCharCode(0xD800 | (chr >> 10)) + String.fromCharCode(0xDC00 | (chr & 1023)));
    } else {
        this.raise_error("Error encoding surrogate pair: " + chr + " is greater than U+10ffff");
    }
}
return str;
}

DivineHelper.prototype.raise_error = function (msg) {
throw "[" + this.constructor.name + "] " + msg;
}
EOS
end

#javascript_class_template(sh) ⇒ Object

Generate required functions that corresponding to the struct definition

* *Args*    :
 - +sh+ -> Struct Name


515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
# File 'lib/divine/code_generators/javascript.rb', line 515

def javascript_class_template(sh)
  code = [
    "",
    "// ------------------------------------------------------------ #{sh.name}",
    "function #{sh.name}() {",
    :indent,
    "DivineHelper.call(this);",
    "",
    # PROPERTIES
  	"this.struct_version = #{sh.latest_version};",
    sh.field_names.map do |fn|
      f = sh.field(fn).last
      "this.#{f.name} = #{javascript_get_empty_declaration(f)};"
    end,
    :deindent,
    "}", "",
    
    "// Inherit DivineHelper",
    "#{sh.name}.prototype = new DivineHelper();", "",

    "// Correct the constructor pointer because it points to DivineHelper",
    "#{sh.name}.prototype.constructor = #{sh.name};", "",

    # SERiALIZE INTERNAL
    "// Serialize",
    "#{sh.name}.prototype.serialize_internal = function(out) {",
    :indent,
    "this.write_int8(this.struct_version, out);",
    sh.structs.map do |s|
      [
        "if(this.struct_version == #{s.version}) {",
        :indent,
        s.simple_fields.map do |f|
          "this.write_#{f.type}(this.#{f.name}, out);"
        end,
        s.complex_fields.map do |f|
          [
            "", "// Serialize #{f.type} '#{f.name}'",
            $debug_javascript ? "console.log(\"Serialize '#{field.name}'\");" : nil,
            javascript_serialize_internal("this.#{f.name}",  f.referenced_types)
          ]
        end,
        "return;",
        :deindent,
        "}", ""
      ]
    end, "",
    "throw \"Unsupported version \" + this.struct_version + \" for type '#{sh.name}'\";",
    :deindent,
    "}", "",

    # DESERIALIZE
    "// Deserialize",
    "#{sh.name}.prototype.deserialize = function (data) {",
    :indent,
    "this.struct_version = this.read_int8(data);",
    sh.structs.map do |s|
      [
        "if(this.struct_version == #{s.version}) {",
        :indent,
        s.simple_fields.map do |f|
          "this.#{f.name} = this.read_#{f.type}(data);"
        end,
        s.complex_fields.map do |f|
          [
            "", "// Read #{f.type} '#{f.name}'",
            $debug_javascript ? "console.log(\"Deserialize '#{field.name}'\");" : nil,
            javascript_deserialize_internal("this.#{f.name}",  f.referenced_types)
          ]
        end,
        "return;",
        :deindent,
        "}"
      ]
    end, "",
    "throw \"Unsupported version \" + this.struct_version + \" for type '#{sh.name}'\";",
    :deindent,
    "}"
  ]
    
  format_src(0, 3, code)
end

#javascript_deserialize_internal(var, types) ⇒ Object

Generate the way of deserializing different DSL types

* *Args*    :
 - +var+   -> variable name
 - +types+ -> variable type


696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
# File 'lib/divine/code_generators/javascript.rb', line 696

def javascript_deserialize_internal(var, types)
  if types.respond_to? :first
    case types.first
    when :list
      count = get_fresh_variable_name
      nv = get_fresh_variable_name
      iter = get_fresh_variable_name
      return [
              "#{"var " unless var.include? "this."}#{var} = [];",
              "var #{count} = this.read_int32(data);",
              "for(var #{iter}=0; #{iter}<#{count}; #{iter}++) {",
              :indent,
              javascript_deserialize_internal(nv, types[1]),
              "#{var}.push(#{nv});",
              :deindent,
              "}"
             ]
    when :map
      count = get_fresh_variable_name
      nv1 = get_fresh_variable_name      
      nv2 = get_fresh_variable_name
      iter = get_fresh_variable_name
      return ["#{"var " unless var.include? "this."}#{var} = {};",
              "var #{count} = this.read_int32(data);",
              "for(var #{iter}=0; #{iter}<#{count}; #{iter}++) {",
              :indent,
              javascript_deserialize_internal(nv1, types[1]),
              javascript_deserialize_internal(nv2, types[2]),
              "#{var}[#{nv1}] = #{nv2};",
              :deindent,
              "}"
             ]
    else
      raise "Missing serialization for #{var}"
    end
  else
    case types
    when :map
      "#{"var " unless var.include? "this."}#{var} = {}"
    when :list
      "#{"var " unless var.include? "this."}#{var} = []"
    else
      if $all_structs.key? types
        [
         "#{"var " unless var.include? "this."}#{var} = new #{types}();",
         "#{var}.deserialize(data);"
        ]
      else
        "#{"var " unless var.include? "this."}#{var} = this.read_#{types}(data);"
      end
    end
  end
end

#javascript_get_empty_declaration(field) ⇒ Object

Generate default JS data types declaration values corresponding to each DSL types:

* DSL Type --> Corresponding Default JS Value
* dint63   --> 0 range -> [0 - 9.007.199.254.740.991]
 * 1 byte:  range ->  [0 - 127]
 * 2 bytes: range ->  [0 - 16,383]
 * 3 bytes: range ->  [0 - 2,097,151]
 * 4 bytes: range ->  [0 - 268,435,455]
 * 5 bytes: range ->  [0 - 34,359,738,367]
 * 6 bytes: range ->  [0 - 4,398,046,511,103]
 * 7 bytes: range ->  [0 - 562,949,953,421,311]
 * 8 bytes: range ->  [0 - 9.007.199.254.740.991] (limited by 53-bit)
* int8     --> 0 Range -> [0 - 255]
* int16    --> 0 Range -> [0 - 65535]
* int32    --> 0 Range -> [0 - 4.294.967.295]
* sint32   --> 0 Range -> [-2.147.483.648 - 2.147.483.647]
* sint64   --> 0 Range -> [-9.007.199.254.740.992, 9.007.199.254.740.991] (limited by 53-bit)
* string   --> ""
* ip_number--> ""
* binary   --> []
* short_binary --> []
* list     --> []
* map      --> {}


622
623
624
625
626
627
628
629
630
631
632
633
634
635
# File 'lib/divine/code_generators/javascript.rb', line 622

def javascript_get_empty_declaration(field)
  case field.type
  when :list, :binary, :short_binary
    "[]"
  when :map
    "{}"
  when :int8, :int16, :int32, :sint32, :sint64, :dint63
    "0"
  when :string, :ip_number
    "\"\""
  else
    raise "Unkown field type #{field.type}"
  end
end

#javascript_serialize_internal(var, types) ⇒ Object

Generate the way of serializing different DSL types

* *Args*    :
 - +var+   -> variable name
 - +types+ -> variable type


643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
# File 'lib/divine/code_generators/javascript.rb', line 643

def javascript_serialize_internal(var, types)
  if types.respond_to? :first
    case types.first
    when :list
      nv = get_fresh_variable_name
      idx = get_fresh_variable_name
      return [
              "this.write_int32(#{var}.length, out);",
              "for(var #{idx}=0; #{idx}<#{var}.length; #{idx}++) {",
              :indent,
              "var #{nv} = #{var}[#{idx}];",
              javascript_serialize_internal(nv, types[1]),
              :deindent,
              "}"
             ]
    when :map
      len = get_fresh_variable_name
      nv1 = get_fresh_variable_name      
      nv2 = get_fresh_variable_name
      key = get_fresh_variable_name
      return [
              "var #{len} = Object.keys(#{var}).length;",
              "this.write_int32(#{len}, out);",
              "for(var #{nv1} in #{var}) {",
              :indent,
              "var #{nv2} = #{var}[#{nv1}];",
              javascript_serialize_internal(nv1, types[1]),
              javascript_serialize_internal(nv2, types[2]),
              :deindent,
              "}"
             ]
    else
      raise "Missing serialization for #{var}"
    end
  else
    if $all_structs[types]
      "#{var}.serialize_internal(out)"
  
    elsif $available_types[types] && $available_types[types].ancestors.include?(SimpleDefinition)
      "this.write_#{types}(#{var}, out)"
  
    else
      raise "Missing code generation case #{types}"
    end
  end
end