Nessuna descrizione
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

decoder.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. "use strict";
  2. module.exports = decoder;
  3. var Enum = require("./enum"),
  4. types = require("./types"),
  5. util = require("./util");
  6. function missing(field) {
  7. return "missing required '" + field.name + "'";
  8. }
  9. /**
  10. * Generates a decoder specific to the specified message type.
  11. * @param {Type} mtype Message type
  12. * @returns {Codegen} Codegen instance
  13. */
  14. function decoder(mtype) {
  15. /* eslint-disable no-unexpected-multiline */
  16. var gen = util.codegen(["r", "l"], mtype.name + "$decode")
  17. ("if(!(r instanceof Reader))")
  18. ("r=Reader.create(r)")
  19. ("var c=l===undefined?r.len:r.pos+l,m=new this.ctor" + (mtype.fieldsArray.filter(function(field) { return field.map; }).length ? ",k,value" : ""))
  20. ("while(r.pos<c){")
  21. ("var t=r.uint32()");
  22. if (mtype.group) gen
  23. ("if((t&7)===4)")
  24. ("break");
  25. gen
  26. ("switch(t>>>3){");
  27. var i = 0;
  28. for (; i < /* initializes */ mtype.fieldsArray.length; ++i) {
  29. var field = mtype._fieldsArray[i].resolve(),
  30. type = field.resolvedType instanceof Enum ? "int32" : field.type,
  31. ref = "m" + util.safeProp(field.name); gen
  32. ("case %i:", field.id);
  33. // Map fields
  34. if (field.map) { gen
  35. ("if(%s===util.emptyObject)", ref)
  36. ("%s={}", ref)
  37. ("var c2 = r.uint32()+r.pos");
  38. if (types.defaults[field.keyType] !== undefined) gen
  39. ("k=%j", types.defaults[field.keyType]);
  40. else gen
  41. ("k=null");
  42. if (types.defaults[type] !== undefined) gen
  43. ("value=%j", types.defaults[type]);
  44. else gen
  45. ("value=null");
  46. gen
  47. ("while(r.pos<c2){")
  48. ("var tag2=r.uint32()")
  49. ("switch(tag2>>>3){")
  50. ("case 1: k=r.%s(); break", field.keyType)
  51. ("case 2:");
  52. if (types.basic[type] === undefined) gen
  53. ("value=types[%i].decode(r,r.uint32())", i); // can't be groups
  54. else gen
  55. ("value=r.%s()", type);
  56. gen
  57. ("break")
  58. ("default:")
  59. ("r.skipType(tag2&7)")
  60. ("break")
  61. ("}")
  62. ("}");
  63. if (types.long[field.keyType] !== undefined) gen
  64. ("%s[typeof k===\"object\"?util.longToHash(k):k]=value", ref);
  65. else gen
  66. ("%s[k]=value", ref);
  67. // Repeated fields
  68. } else if (field.repeated) { gen
  69. ("if(!(%s&&%s.length))", ref, ref)
  70. ("%s=[]", ref);
  71. // Packable (always check for forward and backward compatiblity)
  72. if (types.packed[type] !== undefined) gen
  73. ("if((t&7)===2){")
  74. ("var c2=r.uint32()+r.pos")
  75. ("while(r.pos<c2)")
  76. ("%s.push(r.%s())", ref, type)
  77. ("}else");
  78. // Non-packed
  79. if (types.basic[type] === undefined) gen(field.resolvedType.group
  80. ? "%s.push(types[%i].decode(r))"
  81. : "%s.push(types[%i].decode(r,r.uint32()))", ref, i);
  82. else gen
  83. ("%s.push(r.%s())", ref, type);
  84. // Non-repeated
  85. } else if (types.basic[type] === undefined) gen(field.resolvedType.group
  86. ? "%s=types[%i].decode(r)"
  87. : "%s=types[%i].decode(r,r.uint32())", ref, i);
  88. else gen
  89. ("%s=r.%s()", ref, type);
  90. gen
  91. ("break");
  92. // Unknown fields
  93. } gen
  94. ("default:")
  95. ("r.skipType(t&7)")
  96. ("break")
  97. ("}")
  98. ("}");
  99. // Field presence
  100. for (i = 0; i < mtype._fieldsArray.length; ++i) {
  101. var rfield = mtype._fieldsArray[i];
  102. if (rfield.required) gen
  103. ("if(!m.hasOwnProperty(%j))", rfield.name)
  104. ("throw util.ProtocolError(%j,{instance:m})", missing(rfield));
  105. }
  106. return gen
  107. ("return m");
  108. /* eslint-enable no-unexpected-multiline */
  109. }