Brak opisu
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.

index.js 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. "use strict";
  2. var protobuf = require("../..");
  3. /**
  4. * Debugging utility functions. Only present in debug builds.
  5. * @namespace
  6. */
  7. var debug = protobuf.debug = module.exports = {};
  8. var codegen = protobuf.util.codegen;
  9. var debugFnRe = /function ([^(]+)\(([^)]*)\) {/g;
  10. // Counts number of calls to any generated function
  11. function codegen_debug() {
  12. codegen_debug.supported = codegen.supported;
  13. codegen_debug.verbose = codegen.verbose;
  14. var gen = codegen.apply(null, Array.prototype.slice.call(arguments));
  15. gen.str = (function(str) { return function str_debug() {
  16. return str.apply(null, Array.prototype.slice.call(arguments)).replace(debugFnRe, "function $1($2) {\n\t$1.calls=($1.calls|0)+1");
  17. };})(gen.str);
  18. return gen;
  19. }
  20. /**
  21. * Returns a list of unused types within the specified root.
  22. * @param {NamespaceBase} ns Namespace to search
  23. * @returns {Type[]} Unused types
  24. */
  25. debug.unusedTypes = function unusedTypes(ns) {
  26. /* istanbul ignore if */
  27. if (!(ns instanceof protobuf.Namespace))
  28. throw TypeError("ns must be a Namespace");
  29. /* istanbul ignore if */
  30. if (!ns.nested)
  31. return [];
  32. var unused = [];
  33. for (var names = Object.keys(ns.nested), i = 0; i < names.length; ++i) {
  34. var nested = ns.nested[names[i]];
  35. if (nested instanceof protobuf.Type) {
  36. var calls = (nested.encode.calls|0)
  37. + (nested.decode.calls|0)
  38. + (nested.verify.calls|0)
  39. + (nested.toObject.calls|0)
  40. + (nested.fromObject.calls|0);
  41. if (!calls)
  42. unused.push(nested);
  43. } else if (nested instanceof protobuf.Namespace)
  44. Array.prototype.push.apply(unused, unusedTypes(nested));
  45. }
  46. return unused;
  47. };
  48. /**
  49. * Enables debugging extensions.
  50. * @returns {undefined}
  51. */
  52. debug.enable = function enable() {
  53. protobuf.util.codegen = codegen_debug;
  54. };
  55. /**
  56. * Disables debugging extensions.
  57. * @returns {undefined}
  58. */
  59. debug.disable = function disable() {
  60. protobuf.util.codegen = codegen;
  61. };