Skip to content

Commit 33da148

Browse files
committed
Other: Added utility to enable/disable debugging extensions to experimental debug build
1 parent fdb1a72 commit 33da148

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

Diff for: src/index-debug.js

+24-4
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,43 @@
44
"use strict";
55
var protobuf = module.exports = require("./index");
66

7-
// Count number of calls to any generated function
8-
protobuf.util.codegen = (function(codegen) { return function codegen_debug() {
7+
var codegen = protobuf.util.codegen;
8+
9+
// Counts number of calls to any generated function
10+
function codegen_debug() {
911
codegen_debug.supported = codegen.supported;
1012
codegen_debug.verbose = codegen.verbose;
1113
var gen = codegen.apply(null, Array.prototype.slice.call(arguments));
1214
gen.str = (function(str) { return function str_debug() {
1315
return str.apply(null, Array.prototype.slice.call(arguments)).replace(/function ([^(]+)\(([^)]*)\) {/g, "function $1($2) {\n\t$1.calls=($1.calls|0)+1");
1416
};})(gen.str);
1517
return gen;
16-
};})(protobuf.util.codegen);
18+
}
1719

1820
/**
1921
* Debugging utility functions. Only present in debug builds.
2022
* @namespace
2123
*/
2224
var debug = protobuf.debug = {};
2325

26+
/**
27+
* Enables debugging extensions.
28+
* @returns {undefined}
29+
*/
30+
debug.enable = function enable() {
31+
protobuf.util.codegen = codegen_debug;
32+
return protobuf;
33+
};
34+
35+
/**
36+
* Disables debugging extensions.
37+
* @returns {undefined}
38+
*/
39+
debug.disable = function disable() {
40+
protobuf.util.codegen = codegen;
41+
return protobuf;
42+
};
43+
2444
/**
2545
* Returns a list of unused types within the specified root.
2646
* @param {NamespaceBase} ns Namespace to search
@@ -30,7 +50,7 @@ debug.unusedTypes = function unusedTypes(ns) {
3050

3151
/* istanbul ignore next */
3252
if (!(ns instanceof protobuf.Namespace))
33-
throw TypeError("ns must be a namespace");
53+
throw TypeError("ns must be a Namespace");
3454
/* istanbul ignore next */
3555
if (!ns.nested)
3656
return [];

Diff for: tests/other_basics-debug.js

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var tape = require("tape");
33
var protobuf = require("../debug");
44

55
tape.test("google.protobuf.Any type", function(test) {
6+
protobuf.debug.enable();
67
protobuf.load("tests/data/common.proto", function(err, root) {
78
if (err)
89
return test.fail(err.message);
@@ -93,6 +94,8 @@ tape.test("google.protobuf.Any type", function(test) {
9394
".google.protobuf.ListValue",
9495
".google.protobuf.Timestamp"
9596
], "should recognize unused types (all but .google.protobuf.Any)");
97+
98+
protobuf.debug.disable();
9699
test.end();
97100
});
98101

0 commit comments

Comments
 (0)