From 56c8ec4196d461383c3e1f271da02553d877ae81 Mon Sep 17 00:00:00 2001 From: dcodeIO Date: Fri, 20 Jan 2017 04:56:48 +0100 Subject: [PATCH] New: Added a (highly experimental) debug build as a starting point for #653 --- debug.js | 1 + src/index-debug.js | 53 +++++++++++++++++++ ...{other_basics.js => other_basics-debug.js} | 16 +++++- 3 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 debug.js create mode 100644 src/index-debug.js rename tests/{other_basics.js => other_basics-debug.js} (81%) diff --git a/debug.js b/debug.js new file mode 100644 index 000000000..b7939c251 --- /dev/null +++ b/debug.js @@ -0,0 +1 @@ +module.exports = require("./src/index-debug"); \ No newline at end of file diff --git a/src/index-debug.js b/src/index-debug.js new file mode 100644 index 000000000..0ea1166a2 --- /dev/null +++ b/src/index-debug.js @@ -0,0 +1,53 @@ +// WARNING: highly experimental. +// might eventually become a starting point for a real debug build. + +"use strict"; +var protobuf = module.exports = require("."); + +// Count number of calls to any generated function +protobuf.util.codegen = (function(codegen) { return function codegen_debug() { + codegen_debug.supported = codegen.supported; + codegen_debug.verbose = codegen.verbose; + var gen = codegen.apply(null, Array.prototype.slice.call(arguments)); + gen.str = (function(str) { return function str_debug() { + return str.apply(null, Array.prototype.slice.call(arguments)).replace(/function ([^(]+)\(([^)]*)\) {/g, "function $1($2) {\n\t$1.calls=($1.calls|0)+1"); + };})(gen.str); + return gen; +};})(protobuf.util.codegen); + +/** + * Debugging utility functions. Only present in debug builds. + * @namespace + */ +var debug = protobuf.debug = {}; + +/** + * Returns a list of unused types within the specified root. + * @param {NamespaceBase} ns Namespace to search + * @returns {Type[]} Unused types + */ +debug.unusedTypes = function unusedTypes(ns) { + + /* istanbul ignore next */ + if (!(ns instanceof protobuf.Namespace)) + throw TypeError("ns must be a namespace"); + /* istanbul ignore next */ + if (!ns.nested) + return []; + + var unused = []; + Object.keys(ns.nested).forEach(function(name) { + var nested = ns.nested[name]; + if (nested instanceof protobuf.Type) { + var calls = (nested.encode.calls|0) + + (nested.decode.calls|0) + + (nested.verify.calls|0) + + (nested.toObject.calls|0) + + (nested.fromObject.calls|0); + if (!calls) + unused.push(nested); + } else if (nested instanceof protobuf.Namespace) + Array.prototype.push.apply(unused, unusedTypes(nested)); + }); + return unused; +}; diff --git a/tests/other_basics.js b/tests/other_basics-debug.js similarity index 81% rename from tests/other_basics.js rename to tests/other_basics-debug.js index bdce35949..2225f55b4 100644 --- a/tests/other_basics.js +++ b/tests/other_basics-debug.js @@ -1,6 +1,6 @@ var tape = require("tape"); -var protobuf = require(".."); +var protobuf = require("../debug"); tape.test("google.protobuf.Any type", function(test) { protobuf.load("tests/data/common.proto", function(err, root) { @@ -82,6 +82,20 @@ tape.test("google.protobuf.Any type", function(test) { test.end(); }); + test.test(test.name + " - debug", function(test) { + var unused = protobuf.debug.unusedTypes(root).map(function(type) { return type.fullName; }); + test.same(unused, [ + ".Something", + ".google.protobuf.Duration", + ".google.protobuf.Empty", + ".google.protobuf.Struct", + ".google.protobuf.Value", + ".google.protobuf.ListValue", + ".google.protobuf.Timestamp" + ], "should recognize unused types (all but .google.protobuf.Any)"); + test.end(); + }); + test.end(); });