Skip to content

Commit 56c8ec4

Browse files
committed
New: Added a (highly experimental) debug build as a starting point for #653
1 parent 6bc5bb4 commit 56c8ec4

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

debug.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require("./src/index-debug");

src/index-debug.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
};

tests/other_basics.js tests/other_basics-debug.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var tape = require("tape");
22

3-
var protobuf = require("..");
3+
var protobuf = require("../debug");
44

55
tape.test("google.protobuf.Any type", function(test) {
66
protobuf.load("tests/data/common.proto", function(err, root) {
@@ -82,6 +82,20 @@ tape.test("google.protobuf.Any type", function(test) {
8282
test.end();
8383
});
8484

85+
test.test(test.name + " - debug", function(test) {
86+
var unused = protobuf.debug.unusedTypes(root).map(function(type) { return type.fullName; });
87+
test.same(unused, [
88+
".Something",
89+
".google.protobuf.Duration",
90+
".google.protobuf.Empty",
91+
".google.protobuf.Struct",
92+
".google.protobuf.Value",
93+
".google.protobuf.ListValue",
94+
".google.protobuf.Timestamp"
95+
], "should recognize unused types (all but .google.protobuf.Any)");
96+
test.end();
97+
});
98+
8599
test.end();
86100
});
87101

0 commit comments

Comments
 (0)