Permalink
Browse files

New: Added a (highly experimental) debug build as a starting point for

  • Loading branch information...
dcodeIO committed Jan 20, 2017
1 parent 6bc5bb4 commit 56c8ec4196d461383c3e1f271da02553d877ae81
Showing with 69 additions and 1 deletion.
  1. +1 −0 debug.js
  2. +53 −0 src/index-debug.js
  3. +15 −1 tests/{other_basics.js → other_basics-debug.js}
@@ -0,0 +1 @@
module.exports = require("./src/index-debug");
@@ -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;
};
@@ -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();
});

0 comments on commit 56c8ec4

Please sign in to comment.