|
| 1 | +// WARNING: highly experimental. |
| 2 | +// might eventually become a starting point for a real debug build. |
| 3 | + |
| 4 | +"use strict"; |
| 5 | +var protobuf = module.exports = require("."); |
| 6 | + |
| 7 | +// Count number of calls to any generated function |
| 8 | +protobuf.util.codegen = (function(codegen) { return function codegen_debug() { |
| 9 | + codegen_debug.supported = codegen.supported; |
| 10 | + codegen_debug.verbose = codegen.verbose; |
| 11 | + var gen = codegen.apply(null, Array.prototype.slice.call(arguments)); |
| 12 | + gen.str = (function(str) { return function str_debug() { |
| 13 | + return str.apply(null, Array.prototype.slice.call(arguments)).replace(/function ([^(]+)\(([^)]*)\) {/g, "function $1($2) {\n\t$1.calls=($1.calls|0)+1"); |
| 14 | + };})(gen.str); |
| 15 | + return gen; |
| 16 | +};})(protobuf.util.codegen); |
| 17 | + |
| 18 | +/** |
| 19 | + * Debugging utility functions. Only present in debug builds. |
| 20 | + * @namespace |
| 21 | + */ |
| 22 | +var debug = protobuf.debug = {}; |
| 23 | + |
| 24 | +/** |
| 25 | + * Returns a list of unused types within the specified root. |
| 26 | + * @param {NamespaceBase} ns Namespace to search |
| 27 | + * @returns {Type[]} Unused types |
| 28 | + */ |
| 29 | +debug.unusedTypes = function unusedTypes(ns) { |
| 30 | + |
| 31 | + /* istanbul ignore next */ |
| 32 | + if (!(ns instanceof protobuf.Namespace)) |
| 33 | + throw TypeError("ns must be a namespace"); |
| 34 | + /* istanbul ignore next */ |
| 35 | + if (!ns.nested) |
| 36 | + return []; |
| 37 | + |
| 38 | + var unused = []; |
| 39 | + Object.keys(ns.nested).forEach(function(name) { |
| 40 | + var nested = ns.nested[name]; |
| 41 | + if (nested instanceof protobuf.Type) { |
| 42 | + var calls = (nested.encode.calls|0) |
| 43 | + + (nested.decode.calls|0) |
| 44 | + + (nested.verify.calls|0) |
| 45 | + + (nested.toObject.calls|0) |
| 46 | + + (nested.fromObject.calls|0); |
| 47 | + if (!calls) |
| 48 | + unused.push(nested); |
| 49 | + } else if (nested instanceof protobuf.Namespace) |
| 50 | + Array.prototype.push.apply(unused, unusedTypes(nested)); |
| 51 | + }); |
| 52 | + return unused; |
| 53 | +}; |
0 commit comments