From 33da148e2b750ce06591c1c66ce4c46ccecc3c8f Mon Sep 17 00:00:00 2001 From: dcodeIO Date: Fri, 20 Jan 2017 05:22:45 +0100 Subject: [PATCH] Other: Added utility to enable/disable debugging extensions to experimental debug build --- src/index-debug.js | 28 ++++++++++++++++++++++++---- tests/other_basics-debug.js | 3 +++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/index-debug.js b/src/index-debug.js index 27c966075..0f8106aeb 100644 --- a/src/index-debug.js +++ b/src/index-debug.js @@ -4,8 +4,10 @@ "use strict"; var protobuf = module.exports = require("./index"); -// Count number of calls to any generated function -protobuf.util.codegen = (function(codegen) { return function codegen_debug() { +var codegen = protobuf.util.codegen; + +// Counts number of calls to any generated function +function codegen_debug() { codegen_debug.supported = codegen.supported; codegen_debug.verbose = codegen.verbose; var gen = codegen.apply(null, Array.prototype.slice.call(arguments)); @@ -13,7 +15,7 @@ protobuf.util.codegen = (function(codegen) { return function codegen_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. @@ -21,6 +23,24 @@ protobuf.util.codegen = (function(codegen) { return function codegen_debug() { */ var debug = protobuf.debug = {}; +/** + * Enables debugging extensions. + * @returns {undefined} + */ +debug.enable = function enable() { + protobuf.util.codegen = codegen_debug; + return protobuf; +}; + +/** + * Disables debugging extensions. + * @returns {undefined} + */ +debug.disable = function disable() { + protobuf.util.codegen = codegen; + return protobuf; +}; + /** * Returns a list of unused types within the specified root. * @param {NamespaceBase} ns Namespace to search @@ -30,7 +50,7 @@ debug.unusedTypes = function unusedTypes(ns) { /* istanbul ignore next */ if (!(ns instanceof protobuf.Namespace)) - throw TypeError("ns must be a namespace"); + throw TypeError("ns must be a Namespace"); /* istanbul ignore next */ if (!ns.nested) return []; diff --git a/tests/other_basics-debug.js b/tests/other_basics-debug.js index 2225f55b4..a00ae3b4e 100644 --- a/tests/other_basics-debug.js +++ b/tests/other_basics-debug.js @@ -3,6 +3,7 @@ var tape = require("tape"); var protobuf = require("../debug"); tape.test("google.protobuf.Any type", function(test) { + protobuf.debug.enable(); protobuf.load("tests/data/common.proto", function(err, root) { if (err) return test.fail(err.message); @@ -93,6 +94,8 @@ tape.test("google.protobuf.Any type", function(test) { ".google.protobuf.ListValue", ".google.protobuf.Timestamp" ], "should recognize unused types (all but .google.protobuf.Any)"); + + protobuf.debug.disable(); test.end(); });