Skip to content

Commit

Permalink
New: Added a (highly experimental) debug build as a starting point for
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Jan 20, 2017
1 parent 6bc5bb4 commit 56c8ec4
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
1 change: 1 addition & 0 deletions debug.js
@@ -0,0 +1 @@
module.exports = require("./src/index-debug");
53 changes: 53 additions & 0 deletions 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;
};
16 changes: 15 additions & 1 deletion tests/other_basics.js → 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) {
Expand Down Expand Up @@ -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();
});

Expand Down

0 comments on commit 56c8ec4

Please sign in to comment.